Session.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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
18{
19 memset(static_cast<snmp_session *>(this), 0, sizeof(snmp_session));
20}
21
23{
24 operator =(session);
25}
26
29{
30 if (&session == this)
31 return *this;
32
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;
43}
44
45void
47{
48 if (community_len > 0) {
49 Must(community != nullptr);
50 xfree(community);
51 }
52 xfree(peername);
53 memset(static_cast<snmp_session *>(this), 0, sizeof(snmp_session));
54}
55
56void
58{
59 msg.putPod(Version);
60 msg.putInt(community_len);
61 if (community_len > 0) {
62 Must(community != nullptr);
63 msg.putFixed(community, community_len);
64 }
65 msg.putPod(retries);
66 msg.putPod(timeout);
67 int len = peername != nullptr ? 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
77{
78 reset();
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 != nullptr);
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 != nullptr);
93 msg.getFixed(peername, len);
94 peername[len] = 0;
95 }
96 msg.getPod(remote_port);
97 msg.getPod(local_port);
98}
99
#define Must(condition)
Definition: TextException.h:71
static int retries
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:35
void getFixed(void *raw, size_t size) const
always load size bytes
Definition: TypedMsgHdr.cc:151
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:119
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:118
void putFixed(const void *raw, size_t size)
always store size bytes
Definition: TypedMsgHdr.cc:158
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:128
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:111
snmp_session wrapper add pack/unpack feature
Definition: Session.h:23
void reset()
free internal members
Definition: Session.cc:46
void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Session.cc:57
Session & operator=(const Session &session)
Definition: Session.cc:28
void unpack(const Ipc::TypedMsgHdr &msg)
restore struct from the message
Definition: Session.cc:76
#define xfree
#define xstrdup
#define xmalloc
char * peername
Definition: snmp_session.h:43
u_char * community
Definition: snmp_session.h:39

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors