Session.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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  clear();
20 }
21 
23 {
24  assign(session);
25 }
26 
28 {
29  free();
30 }
31 
34 {
35  free();
36  assign(session);
37  return *this;
38 }
39 
40 void
42 {
43  memset(this, 0, sizeof(*this));
44 }
45 
46 void
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 
58 void
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 
72 void
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 
91 void
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 }
115 
snmp_session wrapper add pack/unpack feature
Definition: Session.h:22
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:109
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:50
#define xstrdup
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:52
void getFixed(void *raw, size_t size) const
always load size bytes
Definition: TypedMsgHdr.cc:141
void unpack(const Ipc::TypedMsgHdr &msg)
restore struct from the message
Definition: Session.cc:92
void assign(const Session &session)
perform full assignment
Definition: Session.cc:59
void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Session.cc:73
void clear()
clear internal members
Definition: Session.cc:41
Session & operator=(const Session &session)
Definition: Session.cc:33
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:101
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
#define Must(cond)
Definition: TextException.h:89
#define xmalloc
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:31
char * peername
Definition: snmp_session.h:43
u_char * community
Definition: snmp_session.h:39
static int retries
void free()
free internal members
Definition: Session.cc:47
#define xfree
#define NULL
Definition: types.h:166
void putFixed(const void *raw, size_t size)
always store size bytes
Definition: TypedMsgHdr.cc:148
#define free(a)
Definition: hash.c:31

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors