]>
Commit | Line | Data |
---|---|---|
51ea0904 CT |
1 | /* |
2 | * $Id$ | |
3 | * | |
4 | * DEBUG: section 49 SNMP Interface | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "config.h" | |
9 | #include "base/TextException.h" | |
10 | #include "ipc/TypedMsgHdr.h" | |
11 | #include "protos.h" | |
12 | #include "snmpx/Session.h" | |
13 | ||
14 | ||
15 | Snmp::Session::Session() | |
16 | { | |
17 | clear(); | |
18 | } | |
19 | ||
20 | Snmp::Session::Session(const Session& session) | |
21 | { | |
22 | assign(session); | |
23 | } | |
24 | ||
25 | Snmp::Session::~Session() | |
26 | { | |
27 | free(); | |
28 | } | |
29 | ||
30 | Snmp::Session& | |
31 | Snmp::Session::operator = (const Session& session) | |
32 | { | |
33 | free(); | |
34 | assign(session); | |
35 | return *this; | |
36 | } | |
37 | ||
38 | void | |
39 | Snmp::Session::clear() | |
40 | { | |
41 | xmemset(this, 0, sizeof(*this)); | |
42 | } | |
43 | ||
44 | void | |
45 | Snmp::Session::free() | |
46 | { | |
47 | if (community_len > 0) { | |
48 | Must(community != NULL); | |
49 | xfree(community); | |
50 | } | |
51 | if (peername != NULL) | |
52 | xfree(peername); | |
53 | clear(); | |
54 | } | |
55 | ||
56 | void | |
57 | Snmp::Session::assign(const Session& session) | |
58 | { | |
59 | memcpy(this, &session, sizeof(*this)); | |
60 | if (session.community != NULL) { | |
61 | community = (u_char*)xstrdup((char*)session.community); | |
62 | Must(community != NULL); | |
63 | } | |
64 | if (session.peername != NULL) { | |
65 | peername = xstrdup(session.peername); | |
66 | Must(peername != NULL); | |
67 | } | |
68 | } | |
69 | ||
70 | void | |
71 | Snmp::Session::pack(Ipc::TypedMsgHdr& msg) const | |
72 | { | |
73 | msg.putPod(Version); | |
74 | msg.putInt(community_len); | |
75 | if (community_len > 0) { | |
76 | Must(community != NULL); | |
77 | msg.putFixed(community, community_len); | |
78 | } | |
79 | msg.putPod(retries); | |
80 | msg.putPod(timeout); | |
81 | int len = peername != NULL ? strlen(peername) : 0; | |
82 | msg.putInt(len); | |
83 | if (len > 0) | |
84 | msg.putFixed(peername, len); | |
85 | msg.putPod(remote_port); | |
86 | msg.putPod(local_port); | |
87 | } | |
88 | ||
89 | void | |
90 | Snmp::Session::unpack(const Ipc::TypedMsgHdr& msg) | |
91 | { | |
92 | free(); | |
93 | msg.getPod(Version); | |
94 | community_len = msg.getInt(); | |
95 | if (community_len > 0) { | |
96 | community = static_cast<u_char*>(xmalloc(community_len + 1)); | |
97 | Must(community != NULL); | |
98 | msg.getFixed(community, community_len); | |
99 | community[community_len] = 0; | |
100 | } | |
101 | msg.getPod(retries); | |
102 | msg.getPod(timeout); | |
103 | int len = msg.getInt(); | |
104 | if (len > 0) { | |
105 | peername = static_cast<char*>(xmalloc(len + 1)); | |
106 | Must(peername != NULL); | |
107 | msg.getFixed(peername, len); | |
108 | peername[len] = 0; | |
109 | } | |
110 | msg.getPod(remote_port); | |
111 | msg.getPod(local_port); | |
112 | } |