]> git.ipfire.org Git - thirdparty/squid.git/blame - src/snmp/Session.cc
Maintenance: Fix mkrelease.sh script (#237)
[thirdparty/squid.git] / src / snmp / Session.cc
CommitLineData
51ea0904 1/*
5b74111a 2 * Copyright (C) 1996-2018 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
51ea0904
CT
17Snmp::Session::Session()
18{
19 clear();
20}
21
22Snmp::Session::Session(const Session& session)
23{
24 assign(session);
25}
26
27Snmp::Session::~Session()
28{
29 free();
30}
31
32Snmp::Session&
33Snmp::Session::operator = (const Session& session)
34{
35 free();
36 assign(session);
37 return *this;
38}
39
40void
41Snmp::Session::clear()
42{
e297be13 43 memset(this, 0, sizeof(*this));
51ea0904
CT
44}
45
46void
47Snmp::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
58void
59Snmp::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
72void
73Snmp::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
91void
92Snmp::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}
f53969cc 115