]> git.ipfire.org Git - thirdparty/squid.git/blob - src/snmp/Session.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / snmp / Session.cc
1 /*
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 49 SNMP Interface */
10
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "ipc/TypedMsgHdr.h"
14 #include "snmp/Session.h"
15 #include "tools.h"
16
17 Snmp::Session::Session()
18 {
19 clear();
20 }
21
22 Snmp::Session::Session(const Session& session)
23 {
24 assign(session);
25 }
26
27 Snmp::Session::~Session()
28 {
29 free();
30 }
31
32 Snmp::Session&
33 Snmp::Session::operator = (const Session& session)
34 {
35 free();
36 assign(session);
37 return *this;
38 }
39
40 void
41 Snmp::Session::clear()
42 {
43 memset(this, 0, sizeof(*this));
44 }
45
46 void
47 Snmp::Session::free()
48 {
49 if (community_len > 0) {
50 Must(community != NULL);
51 xfree(community);
52 }
53 if (peername != NULL)
54 xfree(peername);
55 clear();
56 }
57
58 void
59 Snmp::Session::assign(const Session& session)
60 {
61 memcpy(this, &session, sizeof(*this));
62 if (session.community != NULL) {
63 community = (u_char*)xstrdup((char*)session.community);
64 Must(community != NULL);
65 }
66 if (session.peername != NULL) {
67 peername = xstrdup(session.peername);
68 Must(peername != NULL);
69 }
70 }
71
72 void
73 Snmp::Session::pack(Ipc::TypedMsgHdr& msg) const
74 {
75 msg.putPod(Version);
76 msg.putInt(community_len);
77 if (community_len > 0) {
78 Must(community != NULL);
79 msg.putFixed(community, community_len);
80 }
81 msg.putPod(retries);
82 msg.putPod(timeout);
83 int len = peername != NULL ? strlen(peername) : 0;
84 msg.putInt(len);
85 if (len > 0)
86 msg.putFixed(peername, len);
87 msg.putPod(remote_port);
88 msg.putPod(local_port);
89 }
90
91 void
92 Snmp::Session::unpack(const Ipc::TypedMsgHdr& msg)
93 {
94 free();
95 msg.getPod(Version);
96 community_len = msg.getInt();
97 if (community_len > 0) {
98 community = static_cast<u_char*>(xmalloc(community_len + 1));
99 Must(community != NULL);
100 msg.getFixed(community, community_len);
101 community[community_len] = 0;
102 }
103 msg.getPod(retries);
104 msg.getPod(timeout);
105 int len = msg.getInt();
106 if (len > 0) {
107 peername = static_cast<char*>(xmalloc(len + 1));
108 Must(peername != NULL);
109 msg.getFixed(peername, len);
110 peername[len] = 0;
111 }
112 msg.getPod(remote_port);
113 msg.getPod(local_port);
114 }
115