]> git.ipfire.org Git - thirdparty/squid.git/blob - src/snmp/Session.cc
2690989beedbdc011db2a0daccb4eabbaf6923c8
[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
18 Snmp::Session::Session()
19 {
20 memset(static_cast<snmp_session *>(this), 0, sizeof(snmp_session));
21 }
22
23 Snmp::Session::Session(const Snmp::Session& session) : Session()
24 {
25 operator =(session);
26 }
27
28 Snmp::Session&
29 Snmp::Session::operator = (const Session& session)
30 {
31 if (&session == this)
32 return *this;
33
34 reset();
35 memcpy(static_cast<snmp_session *>(this), &session, sizeof(snmp_session));
36 // memcpy did a shallow copy, make sure we have our own allocations
37 if (session.community) {
38 community = (u_char*)xstrdup((char*)session.community);
39 }
40 if (session.peername) {
41 peername = xstrdup(session.peername);
42 }
43 return *this;
44 }
45
46 void
47 Snmp::Session::reset()
48 {
49 if (community_len > 0) {
50 Must(community != NULL);
51 xfree(community);
52 }
53 xfree(peername);
54 memset(static_cast<snmp_session *>(this), 0, sizeof(snmp_session));
55 }
56
57 void
58 Snmp::Session::pack(Ipc::TypedMsgHdr& msg) const
59 {
60 msg.putPod(Version);
61 msg.putInt(community_len);
62 if (community_len > 0) {
63 Must(community != NULL);
64 msg.putFixed(community, community_len);
65 }
66 msg.putPod(retries);
67 msg.putPod(timeout);
68 int len = peername != NULL ? strlen(peername) : 0;
69 msg.putInt(len);
70 if (len > 0)
71 msg.putFixed(peername, len);
72 msg.putPod(remote_port);
73 msg.putPod(local_port);
74 }
75
76 void
77 Snmp::Session::unpack(const Ipc::TypedMsgHdr& msg)
78 {
79 reset();
80 msg.getPod(Version);
81 community_len = msg.getInt();
82 if (community_len > 0) {
83 community = static_cast<u_char*>(xmalloc(community_len + 1));
84 Must(community != NULL);
85 msg.getFixed(community, community_len);
86 community[community_len] = 0;
87 }
88 msg.getPod(retries);
89 msg.getPod(timeout);
90 int len = msg.getInt();
91 if (len > 0) {
92 peername = static_cast<char*>(xmalloc(len + 1));
93 Must(peername != NULL);
94 msg.getFixed(peername, len);
95 peername[len] = 0;
96 }
97 msg.getPod(remote_port);
98 msg.getPod(local_port);
99 }
100