Session.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 
45 void
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 
56 void
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 
75 void
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 xmalloc
snmp_session wrapper add pack/unpack feature
Definition: Session.h:22
void putFixed(const void *raw, size_t size)
always store size bytes
Definition: TypedMsgHdr.cc:158
char * peername
Definition: snmp_session.h:65
#define xstrdup
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:126
u_char * community
Definition: snmp_session.h:61
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:119
void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Session.cc:57
void unpack(const Ipc::TypedMsgHdr &msg)
restore struct from the message
Definition: Session.cc:76
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:111
#define xfree
void reset()
free internal members
Definition: Session.cc:46
static int retries
#define Must(condition)
Definition: TextException.h:75
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:34
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:118
void getFixed(void *raw, size_t size) const
always load size bytes
Definition: TypedMsgHdr.cc:151
Session & operator=(const Session &session)
Definition: Session.cc:28

 

Introduction

Documentation

Support

Miscellaneous