]> git.ipfire.org Git - thirdparty/squid.git/blame - src/snmp/Session.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / snmp / Session.cc
CommitLineData
51ea0904 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
51ea0904 3 *
bbc27441
AJ
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.
51ea0904
CT
7 */
8
bbc27441
AJ
9/* DEBUG: section 49 SNMP Interface */
10
f7f3304a 11#include "squid.h"
51ea0904
CT
12#include "base/TextException.h"
13#include "ipc/TypedMsgHdr.h"
d6e3ad20 14#include "snmp/Session.h"
5bed43d6 15#include "tools.h"
51ea0904 16
b56b37cf 17Snmp::Session::Session()
51ea0904 18{
b56b37cf 19 memset(static_cast<snmp_session *>(this), 0, sizeof(snmp_session));
51ea0904
CT
20}
21
b56b37cf 22Snmp::Session::Session(const Snmp::Session& session) : Session()
51ea0904 23{
b56b37cf 24 operator =(session);
51ea0904
CT
25}
26
27Snmp::Session&
28Snmp::Session::operator = (const Session& session)
29{
b56b37cf
AJ
30 if (&session == this)
31 return *this;
51ea0904 32
b56b37cf
AJ
33 reset();
34 memcpy(static_cast<snmp_session *>(this), &session, sizeof(snmp_session));
35 // memcpy did a shallow copy, make sure we have our own allocations
36 if (session.community) {
37 community = (u_char*)xstrdup((char*)session.community);
38 }
39 if (session.peername) {
40 peername = xstrdup(session.peername);
41 }
42 return *this;
51ea0904
CT
43}
44
45void
b56b37cf 46Snmp::Session::reset()
51ea0904
CT
47{
48 if (community_len > 0) {
49 Must(community != NULL);
50 xfree(community);
51 }
b56b37cf
AJ
52 xfree(peername);
53 memset(static_cast<snmp_session *>(this), 0, sizeof(snmp_session));
51ea0904
CT
54}
55
56void
57Snmp::Session::pack(Ipc::TypedMsgHdr& msg) const
58{
59 msg.putPod(Version);
60 msg.putInt(community_len);
61 if (community_len > 0) {
62 Must(community != NULL);
63 msg.putFixed(community, community_len);
64 }
65 msg.putPod(retries);
66 msg.putPod(timeout);
67 int len = peername != NULL ? strlen(peername) : 0;
68 msg.putInt(len);
69 if (len > 0)
70 msg.putFixed(peername, len);
71 msg.putPod(remote_port);
72 msg.putPod(local_port);
73}
74
75void
76Snmp::Session::unpack(const Ipc::TypedMsgHdr& msg)
77{
b56b37cf 78 reset();
51ea0904
CT
79 msg.getPod(Version);
80 community_len = msg.getInt();
81 if (community_len > 0) {
82 community = static_cast<u_char*>(xmalloc(community_len + 1));
83 Must(community != NULL);
84 msg.getFixed(community, community_len);
85 community[community_len] = 0;
86 }
87 msg.getPod(retries);
88 msg.getPod(timeout);
89 int len = msg.getInt();
90 if (len > 0) {
91 peername = static_cast<char*>(xmalloc(len + 1));
92 Must(peername != NULL);
93 msg.getFixed(peername, len);
94 peername[len] = 0;
95 }
96 msg.getPod(remote_port);
97 msg.getPod(local_port);
98}
f53969cc 99