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