Var.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 "Debug.h"
14 #include "ipc/TypedMsgHdr.h"
15 #include "snmp/Var.h"
16 #include "tools.h"
17 
18 #include <algorithm>
19 
21 {
22  init();
23 }
24 
25 Snmp::Var::Var(const Var& var)
26 {
27  init();
28  assign(var);
29 }
30 
32 {
33  clear();
34 }
35 
36 Snmp::Var&
38 {
39  clear();
40  assign(var);
41  return *this;
42 }
43 
44 void
46 {
47  memset(this, 0, sizeof(*this));
48 }
49 
50 Snmp::Var&
52 {
53  switch (type) {
54  case SMI_INTEGER:
55  setInt(asInt() + var.asInt());
56  break;
57  case SMI_GAUGE32:
58  setGauge(asGauge() + var.asGauge());
59  break;
60  case SMI_COUNTER32:
61  setCounter(asCounter() + var.asCounter());
62  break;
63  case SMI_COUNTER64:
64  setCounter64(asCounter64() + var.asCounter64());
65  break;
66  case SMI_TIMETICKS:
67  setTimeTicks(asTimeTicks() + var.asTimeTicks());
68  break;
69  default:
70  debugs(49, DBG_CRITICAL, HERE << "Unsupported type: " << type);
71  throw TexcHere("Unsupported type");
72  break;
73  }
74  return *this;
75 }
76 
77 Snmp::Var&
79 {
80  Must(num != 0);
81  switch (type) {
82  case SMI_INTEGER:
83  setInt(asInt() / num);
84  break;
85  case SMI_GAUGE32:
86  setGauge(asGauge() / num);
87  break;
88  case SMI_COUNTER32:
89  setCounter(asCounter() / num);
90  break;
91  case SMI_COUNTER64:
92  setCounter64(asCounter64() / num);
93  break;
94  case SMI_TIMETICKS:
95  setTimeTicks(asTimeTicks() / num);
96  break;
97  default:
98  debugs(49, DBG_CRITICAL, HERE << "Unsupported type: " << type);
99  throw TexcHere("Unsupported type");
100  break;
101  }
102  return *this;
103 }
104 
105 bool
106 Snmp::Var::operator < (const Var& var) const
107 {
108  switch (type) {
109  case SMI_INTEGER:
110  return asInt() < var.asInt();
111  case SMI_GAUGE32:
112  return asGauge() < var.asGauge();
113  case SMI_COUNTER32:
114  return asCounter() < var.asCounter();
115  case SMI_COUNTER64:
116  return asCounter64() < var.asCounter64();
117  case SMI_TIMETICKS:
118  return asTimeTicks() < var.asTimeTicks();
119  default:
120  debugs(49, DBG_CRITICAL, HERE << "Unsupported type: " << type);
121  throw TexcHere("Unsupported type");
122  break;
123  }
124  return false; // unreachable
125 }
126 
127 bool
128 Snmp::Var::operator > (const Var& var) const
129 {
130  switch (type) {
131  case SMI_INTEGER:
132  return asInt() > var.asInt();
133  case SMI_GAUGE32:
134  return asGauge() > var.asGauge();
135  case SMI_COUNTER32:
136  return asCounter() > var.asCounter();
137  case SMI_COUNTER64:
138  return asCounter64() > var.asCounter64();
139  case SMI_TIMETICKS:
140  return asTimeTicks() > var.asTimeTicks();
141  default:
142  debugs(49, DBG_CRITICAL, HERE << "Unsupported type: " << type);
143  throw TexcHere("Unsupported type");
144  break;
145  }
146  return false; // unreachable
147 }
148 
149 void
151 {
152  setName(var.getName());
153  copyValue(var);
154 }
155 
156 void
158 {
159  if (name != NULL) {
160  xfree(name);
161  name = NULL;
162  }
163  name_length = 0;
164 }
165 
168 {
169  return Range<const oid*>(name, name + name_length);
170 }
171 
172 void
174 {
175  clearName();
176  if (aName.start != NULL && aName.size() != 0) {
177  name_length = aName.size();
178  name = static_cast<oid*>(xmalloc(name_length * sizeof(oid)));
179  std::copy(aName.start, aName.end, name);
180  }
181 }
182 
183 void
185 {
186  if (val.string != NULL) {
187  xfree(val.string);
188  val.string = NULL;
189  }
190  val_len = 0;
191  type = 0;
192 }
193 
194 bool
196 {
197  return type == SMI_NULLOBJ;
198 }
199 
200 int
202 {
203  Must(type == SMI_INTEGER);
204  Must(val.integer != NULL && val_len == sizeof(int));
205  return *val.integer;
206 }
207 
208 unsigned int
210 {
211  Must(type == SMI_GAUGE32);
212  Must(val.integer != NULL && val_len == 4);
213  return *reinterpret_cast<unsigned int*>(val.integer);
214 }
215 
216 int
218 {
219  Must(type == SMI_COUNTER32);
220  Must(val.integer != NULL && val_len == 4);
221  return *reinterpret_cast<int*>(val.integer);
222 }
223 
224 long long int
226 {
227  Must(type == SMI_COUNTER64);
228  Must(val.integer != NULL && val_len == 8);
229  return *reinterpret_cast<long long int*>(val.integer);
230 }
231 
232 unsigned int
234 {
235  Must(type == SMI_TIMETICKS);
236  Must(val.integer != NULL && val_len == sizeof(unsigned int));
237  return *reinterpret_cast<unsigned int*>(val.integer);
238 }
239 
242 {
243  Must(type == SMI_OBJID);
244  Must(val_len % sizeof(oid) == 0);
245  int length = val_len / sizeof(oid);
246  Must(val.objid != NULL && length > 0);
247  return Range<const oid*>(val.objid, val.objid + length);
248 }
249 
252 {
253  Must(type == SMI_STRING);
254  Must(val.string != NULL && val_len > 0);
255  return Range<const u_char*>(val.string, val.string + val_len);
256 }
257 
258 void
260 {
261  setValue(&value, sizeof(value), SMI_INTEGER);
262 }
263 
264 void
266 {
267  setValue(&value, sizeof(value), SMI_COUNTER32);
268 }
269 
270 void
271 Snmp::Var::setGauge(unsigned int value)
272 {
273  setValue(&value, sizeof(value), SMI_GAUGE32);
274 }
275 
276 void
278 {
279  setValue(string.start, string.size(), SMI_STRING);
280 }
281 
282 void
284 {
285  setValue(object.start, object.size() * sizeof(oid), SMI_OBJID);
286 }
287 
288 void
289 Snmp::Var::setCounter64(long long int counter)
290 {
291  setValue(&counter, sizeof(counter), SMI_COUNTER64);
292 }
293 
294 void
295 Snmp::Var::setTimeTicks(unsigned int ticks)
296 {
297  setValue(&ticks, sizeof(ticks), SMI_TIMETICKS);
298 }
299 
300 void
302 {
303  setValue(var.val.string, var.val_len, var.type);
304 }
305 
306 void
307 Snmp::Var::setValue(const void* value, int length, int aType)
308 {
309  clearValue();
310  if (value != NULL) {
311  Must(length > 0 && aType > 0);
312  val.string = static_cast<u_char*>(xmalloc(length));
313  memcpy(val.string, value, length);
314  }
315  val_len = length;
316  type = aType;
317 }
318 
319 void
321 {
322  clearName();
323  clearValue();
324  init();
325 }
326 
327 void
329 {
330  msg.putInt(name_length);
331  if (name_length > 0) {
332  Must(name != NULL);
333  msg.putFixed(name, name_length * sizeof(oid));
334  }
335  msg.putPod(type);
336  msg.putPod(val_len);
337  if (val_len > 0) {
338  Must(val.string != NULL);
339  msg.putFixed(val.string, val_len);
340  }
341 }
342 
343 void
345 {
346  clearName();
347  clearValue();
348  name_length = msg.getInt();
349  Must(name_length >= 0);
350  if (name_length > 0) {
351  name = static_cast<oid*>(xmalloc(name_length * sizeof(oid)));
352  msg.getFixed(name, name_length * sizeof(oid));
353  }
354  msg.getPod(type);
355  val_len = msg.getInt();
356  Must(val_len >= 0);
357  if (val_len > 0) {
358  val.string = static_cast<u_char*>(xmalloc(val_len));
359  msg.getFixed(val.string, val_len);
360  }
361 }
362 
void clear()
clear all internal members
Definition: Var.cc:320
void init()
initialize members
Definition: Var.cc:45
~Var()
Definition: Var.cc:31
void unpack(const Ipc::TypedMsgHdr &msg)
restore struct from the message
Definition: Var.cc:344
void assign(const Var &var)
perform full assignment
Definition: Var.cc:150
bool operator>(const Var &var) const
Definition: Var.cc:128
unsigned int asTimeTicks() const
returns variable value as time ticks
Definition: Var.cc:233
int type
Definition: errorpage.cc:79
#define SMI_STRING
Definition: snmp_vars.h:72
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:109
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:50
int asCounter() const
returns variable value as Counter32
Definition: Var.cc:217
long long int asCounter64() const
returns variable value as Counter64
Definition: Var.cc:225
Range< const oid * > asObject() const
returns variable value as object oid
Definition: Var.cc:241
Range< const u_char * > asString() const
returns variable value as chars string
Definition: Var.cc:251
Definition: Range.h:18
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:52
void setString(const Range< const u_char * > &string)
assign string to variable
Definition: Var.cc:277
#define DBG_CRITICAL
Definition: Debug.h:44
void getFixed(void *raw, size_t size) const
always load size bytes
Definition: TypedMsgHdr.cc:141
S size() const
Definition: Range.h:61
void setTimeTicks(unsigned int ticks)
assign unsigned int (time) value to variable
Definition: Var.cc:295
u_char type
Definition: snmp_vars.h:48
unsigned int asGauge() const
returns variable value as unsigned int
Definition: Var.cc:209
void setCounter(int value)
assign Counter32 value to variable
Definition: Var.cc:265
#define SMI_INTEGER
Definition: snmp_vars.h:71
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
void setName(const Range< const oid * > &aName)
set new variable name
Definition: Var.cc:173
union variable_list::@20 val
void setGauge(unsigned int value)
assign unsigned int value to variable
Definition: Var.cc:271
#define SMI_COUNTER64
Definition: snmp_vars.h:81
bool operator<(const Var &var) const
Definition: Var.cc:106
int asInt() const
returns variable value as integer
Definition: Var.cc:201
C start
Definition: Range.h:24
void clearValue()
clear .val member
Definition: Var.cc:184
void setInt(int value)
assign int value to variable
Definition: Var.cc:259
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:101
C end
Definition: Range.h:25
void setCounter64(long long int counter)
assign Counter64 value to variable
Definition: Var.cc:289
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
u_int oid
Definition: asn1.h:42
#define SMI_TIMETICKS
Definition: snmp_vars.h:79
#define Must(cond)
Definition: TextException.h:89
Var()
Definition: Var.cc:20
#define xmalloc
void setObject(const Range< const oid * > &object)
assign object oid to variable
Definition: Var.cc:283
Definition: Var.h:23
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:31
Var & operator=(const Var &var)
Definition: Var.cc:37
Range< const oid * > getName() const
returns variable name
Definition: Var.cc:167
#define SMI_COUNTER32
Definition: snmp_vars.h:76
void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Var.cc:328
#define TexcHere(msg)
Definition: TextException.h:81
Var & operator+=(const Var &var)
Definition: Var.cc:51
Var & operator/=(int num)
Definition: Var.cc:78
bool isNull() const
Definition: Var.cc:195
#define xfree
#define SMI_NULLOBJ
Definition: snmp_vars.h:74
void setValue(const void *value, int length, int aType)
set new variable value
Definition: Var.cc:307
#define SMI_GAUGE32
Definition: snmp_vars.h:77
void clearName()
clear variable name
Definition: Var.cc:157
#define NULL
Definition: types.h:166
void putFixed(const void *raw, size_t size)
always store size bytes
Definition: TypedMsgHdr.cc:148
int size
Definition: ModDevPoll.cc:77
u_char * string
Definition: snmp_vars.h:51
#define SMI_OBJID
Definition: snmp_vars.h:73
void copyValue(const Var &var)
copy variable from another one
Definition: Var.cc:301

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors