AGENT++  4.0.3
snmp_pp_ext.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## AGENT++ 4.0 - snmp_pp_ext.h
4  _##
5  _## Copyright (C) 2000-2013 Frank Fock and Jochen Katz (agentpp.com)
6  _##
7  _## Licensed under the Apache License, Version 2.0 (the "License");
8  _## you may not use this file except in compliance with the License.
9  _## You may obtain a copy of the License at
10  _##
11  _## http://www.apache.org/licenses/LICENSE-2.0
12  _##
13  _## Unless required by applicable law or agreed to in writing, software
14  _## distributed under the License is distributed on an "AS IS" BASIS,
15  _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  _## See the License for the specific language governing permissions and
17  _## limitations under the License.
18  _##
19  _##########################################################################*/
20 
21 #ifndef _SNMP_PP_EXT_H_
22 #define _SNMP_PP_EXT_H_
23 
24 #include <libagent.h>
25 
26 #include <agent_pp/agent++.h>
27 #include <snmp_pp/smi.h>
28 #include <snmp_pp/pdu.h>
29 #include <snmp_pp/vb.h>
30 #include <snmp_pp/octet.h>
31 #include <snmp_pp/oid.h>
32 #include <snmp_pp/snmpmsg.h>
33 
34 #ifndef WIN32
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <unistd.h>
38 #endif
39 
40 #ifdef SNMP_PP_NAMESPACE
41 #define NS_SNMP Snmp_pp::
42 #else
43 #define NS_SNMP
44 #endif
45 
46 #ifdef AGENTPP_NAMESPACE
47 namespace Agentpp {
48 #endif
49 
50 
51 /*--------------------------- class Oidx -----------------------------*/
52 
62 class AGENTPP_DECL Oidx: public NS_SNMP Oid {
63 public:
67  Oidx() : Oid() { }
68 
74  Oidx(const char* s) : Oid(s) { }
75 
82  Oidx(const unsigned long *data, int length) : Oid(data, length) { }
83 
89  Oidx(const Oid& oid) : Oid(oid) { }
90 
94  virtual ~Oidx() { }
95 
103  Oidx cut_left(const unsigned int index) const
104  {
105  return cut_left(*this, index);
106  }
107 
115  Oidx cut_right(const unsigned int index) const
116  {
117  return cut_right(*this, index);
118  }
119 
120 
134  Oidx& mask(const NS_SNMP OctetStr &mask)
135  {
136  for (unsigned int i=0; (i<len()) && (i<mask.len()*8); i++) {
137  char m = 0x80 >> (i%8);
138  if (!(mask[i/8] & m)) {
139  (*this)[i] = 0ul;
140  }
141  }
142  return *this;
143  }
144 
145 
154  static Oidx cut_left(const Oidx &oid, const unsigned int index)
155  {
156  if (oid.valid()) {
157  Oidx retval( oid.smival.value.oid.ptr + index, oid.smival.value.oid.len - index );
158 #if 0
159  unsigned int i;
160  for (i=index; i<oid.smival.value.oid.len; i++)
161  retval += oid.smival.value.oid.ptr[i];
162 #endif
163  return retval;
164  }
165  else
166  return Oidx();
167  }
168 
177  static Oidx cut_right(const Oidx &oid, const unsigned int index)
178  {
179  if (oid.valid()) {
180  unsigned int l = oid.smival.value.oid.len >= index ? oid.smival.value.oid.len - index : 0;
181  Oidx retval( oid.smival.value.oid.ptr, l );
182 #if 0
183  Oidx retval;
184  unsigned int i;
185  unsigned int s = index;
186  if (s>oid.len())
187  s = oid.len();
188  for (i=0; i<oid.len()-s; i++)
189  retval += oid.smival.value.oid.ptr[i];
190 #endif
191  return retval;
192  }
193  else
194  return Oidx();
195  }
196 
202  unsigned long last() const
203  {
204  // check for len == 0
205  if ((!Oid::valid()) || (smival.value.oid.len<1))
206  return 0;
207 
208  return smival.value.oid.ptr[smival.value.oid.len-1];
209  }
210 
211  using NS_SNMP Oid::operator = ;
212  virtual Oidx& operator = (unsigned long l)
213  {
214  // delete the old value
215  if ( smival.value.oid.ptr ) {
216  delete [] smival.value.oid.ptr;
217  smival.value.oid.ptr = NULL;
218  }
219  smival.value.oid.len = 1;
220  smival.value.oid.ptr = (SmiLPUINT32)new unsigned long[1];
221  smival.value.oid.ptr[0] = l;
222  return *this;
223  }
224 
225  using NS_SNMP Oid::operator += ;
226  Oidx &operator+=(NS_SNMP IpAddress const &ip)
227  {
228  for (int i = 0; i < ip.get_length(); i++)
229  *this += (unsigned long)ip[i];
230  return *this;
231  }
232 #if 0
233  Oidx &operator+=(const char *);
234  Oidx &operator+=(const unsigned long i);
235  Oidx &operator+=(const Oid &);
236 #endif
237 
245  bool in_subtree_of(const Oidx& o) const
246  {
247  if (len() <= o.len()) return FALSE;
248  for (unsigned int i=0; i<o.len(); i++)
249  if ((*this)[i] != o[i]) return FALSE;
250  return TRUE;
251  }
252 
260  bool is_root_of(const Oidx& o) const
261  {
262  if (len() >= o.len()) return false;
263  for (unsigned int i=0; i<len(); i++)
264  if ((*this)[i] != o[i]) return false;
265  return true;
266  }
267 
282  int compare(const Oidx&, const NS_SNMP OctetStr&) const;
283 
296  int compare(const Oidx&, u_int) const;
297 
308  NS_SNMP OctetStr as_string(bool withoutLength = false) const
309  {
310  OctetStr str;
311 
312  int i = 0;
313  // check if the len is implied and should be ignored!
314  if (withoutLength && len() > 0 && len() == ((*this)[0] + 1))
315  i++; // first oid seems to be the len
316  for (; i<(int)len(); i++) {
317  str += (unsigned char)(*this)[i];
318  }
319  return str;
320  }
321 
332  static Oidx from_string(const NS_SNMP OctetStr &str, bool withLength = TRUE)
333  {
334  Oidx oid;
335  if (withLength)
336  oid += (long)str.len();
337  for (unsigned int i=0; i<str.len(); i++)
338  oid += (long)str[i];
339  return oid;
340  }
341 
348  Oidx successor() const
349  {
350  Oidx o(*this);
351  if (o.len()==MAX_OID_LEN) {
352  if (o[MAX_OID_LEN-1] == 0xFFFFFFFFul) {
353  int i = MAX_OID_LEN-2;
354  while ((i>=0) && (o[i] == 0xFFFFFFFFul)) {
355  i--;
356  }
357  if (i>=0) {
358  o.trim(MAX_OID_LEN-1-i);
359  o[i]++;
360  }
361  }
362  else
363  o[MAX_OID_LEN-1]++;
364  }
365  else
366  o += 0ul;
367  return o;
368  }
369 
376  Oidx predecessor() const
377  {
378  Oidx o(*this);
379  if (o.len()==0) return o;
380  if (o[o.len()-1] == 0) {
381  o.trim();
382  return o;
383  }
384  o[o.len()-1]--;
385  for (int i=o.len(); i<MAX_OID_LEN; i++)
386  o += 0xFFFFFFFFul;
387  return o;
388  }
389 
399  Oidx next_peer() const
400  {
401  Oidx o(*this);
402  o[o.len()-1]++;
403  return o;
404  }
405 };
406 
407 
408 /*--------------------------- class Vbx -----------------------------*/
409 
421 class AGENTPP_DECL Vbx: public NS_SNMP Vb {
422 public:
423  Vbx(): Vb() { }
424  Vbx(const NS_SNMP Vb& vb): Vb(vb) { }
425  Vbx(const NS_SNMP Oid& oid): Vb(oid) { }
426 
435  Vbx(const NS_SNMP Oid& oid, const NS_SNMP SnmpSyntax &val): Vb(oid) {
436  set_value(val);
437  }
438 
445  Oidx get_oid() const { return iv_vb_oid; };
446 
447  void get_oid(Oidx& oid) const { oid = iv_vb_oid; };
448 
453  void clear();
454 
455  Vbx* clone() const { return new Vbx(*this); }
456 
457  friend int operator==(const Vbx&, const Vbx&);
458 
472  static bool equal(Vbx*, Vbx*, int);
473 
492  static int to_asn1(Vbx*, int, unsigned char*&, int&);
493 
513  static int from_asn1(Vbx*&, int&, unsigned char*&, int&);
514 
515  private:
516  static unsigned char* asn_build_long_len_sequence(unsigned char *,
517  int*,
518  unsigned char,
519  int, int);
520  static unsigned char* asn_build_long_length(unsigned char*,
521  int*,
522  int,
523  int);
524 };
525 
526 
527 /*------------------------- class OidxRange ---------------------------*/
528 
538 
539 public:
543  OidxRange();
544 
554  OidxRange(const Oidx&, const Oidx&);
555 
559  OidxRange(const OidxRange&);
560 
564  virtual ~OidxRange();
565 
572  virtual OidxRange* clone() const;
573 
582  virtual bool operator==(const OidxRange&) const;
592  virtual bool operator<(const OidxRange&) const;
602  virtual bool operator>(const OidxRange&) const;
603 
613  virtual bool includes(const Oidx&) const;
614 
624  virtual bool includes_excl(const Oidx&) const;
625 
636  virtual bool covers(const OidxRange&) const;
637 
646  virtual bool overlaps(const OidxRange&) const;
647 
654  virtual Oidx get_lower() const;
655 
662  virtual Oidx get_upper() const;
663 
664  Oidx lower, upper;
665 };
666 
667 
668 /*--------------------------- class Pdux -----------------------------*/
669 
681 class AGENTPP_DECL Pdux: public NS_SNMP Pdu {
682 public:
683  Pdux(): Pdu() { }
684  Pdux(NS_SNMP Vb* pvbs, const int pvb_count): Pdu(pvbs, pvb_count) { }
685  Pdux(const Pdu& pdu): Pdu(pdu) { }
686  Pdux(const Pdux& pdu): Pdu(pdu) { }
687 
688  virtual ~Pdux() { }
689 
693  void clear();
694 
695  // const redefinitions of originals:
696  Pdux& operator+=(const NS_SNMP Vb&);
697 
704  virtual Pdux* clone() const { return new Pdux(*this); }
705 };
706 
707 
708 /*--------------------------- class Snmpx -----------------------------*/
709 
720 class AGENTPP_DECL Snmpx: public NS_SNMP Snmp {
721 public:
731  Snmpx (int &status , u_short port): Snmp(status, port) {};
732 
733 #ifdef SNMP_PP_WITH_UDPADDR
734 
744  Snmpx(int& status, const NS_SNMP UdpAddress& addr): Snmp(status, addr) { }
745 #endif
746 
747 #ifdef _SNMPv3
748 
765  int receive(struct timeval*, Pdux&, NS_SNMP UTarget&);
766 #else
767 
785  int receive(struct timeval*, Pdux&, NS_SNMP UdpAddress&,
786  NS_SNMP snmp_version&, NS_SNMP OctetStr&);
787 #endif
788 #ifdef _SNMPv3
789 
805  int send (Pdux const &, NS_SNMP SnmpTarget*);
806 #else
807 
822  int send (Pdux const &, NS_SNMP UdpAddress const &, NS_SNMP snmp_version, NS_SNMP OctetStr const &);
823 #endif
824 
830  u_short get_port();
831 
842  SnmpSocket get_session_fds() { return iv_snmp_session; }
843 
844 
845 protected:
846  unsigned long ProcessizedReqId(unsigned short);
847  unsigned long MyMakeReqId();
848 };
849 
850 #ifdef AGENTPP_NAMESPACE
851 }
852 #endif
853 
854 #endif
855 
856 
857 
858 
859 
860 
861