SNMP++  3.3.4
address.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## address.h
4  _##
5  _## SNMP++ v3.3
6  _## -----------------------------------------------
7  _## Copyright (c) 2001-2013 Jochen Katz, Frank Fock
8  _##
9  _## This software is based on SNMP++2.6 from Hewlett Packard:
10  _##
11  _## Copyright (c) 1996
12  _## Hewlett-Packard Company
13  _##
14  _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15  _## Permission to use, copy, modify, distribute and/or sell this software
16  _## and/or its documentation is hereby granted without fee. User agrees
17  _## to display the above copyright notice and this license notice in all
18  _## copies of the software and any documentation of the software. User
19  _## agrees to assume all liability for the use of the software;
20  _## Hewlett-Packard and Jochen Katz make no representations about the
21  _## suitability of this software for any purpose. It is provided
22  _## "AS-IS" without warranty of any kind, either express or implied. User
23  _## hereby grants a royalty-free license to any and all derivatives based
24  _## upon this software code base.
25  _##
26  _##########################################################################*/
27 /*
28  Copyright (c) 1999
29  Hewlett-Packard Company
30 
31  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
32  Permission to use, copy, modify, distribute and/or sell this software
33  and/or its documentation is hereby granted without fee. User agrees
34  to display the above copyright notice and this license notice in all
35  copies of the software and any documentation of the software. User
36  agrees to assume all liability for the use of the software; Hewlett-Packard
37  makes no representations about the suitability of this software for any
38  purpose. It is provided "AS-IS without warranty of any kind,either express
39  or implied. User hereby grants a royalty-free license to any and all
40  derivatives based upon this software code base.
41 
42 
43  SNMP++ A D D R E S S . H
44 
45  ADDRESS CLASS DEFINITION
46 
47  DESIGN + AUTHOR: Peter E Mellquist
48 
49  DESCRIPTION:
50  Address class definition. Encapsulates various network
51  addresses into easy to use, safe and portable classes.
52 
53 =====================================================================*/
54 // $Id: address.h 2359 2013-05-09 20:07:01Z fock $
55 
56 #ifndef _ADDRESS
57 #define _ADDRESS
58 
59 
60 //----[ includes ]-----------------------------------------------------
61 #include <string.h>
62 
63 #include "snmp_pp/config_snmp_pp.h" // for _IPX_ADDRESS and _MAC_ADDRESS
64 #include "snmp_pp/smival.h"
65 #include "snmp_pp/collect.h"
66 #include "snmp_pp/reentrant.h"
67 
68 // include sockets header files
69 // for Windows16 and Windows32 include Winsock
70 // otherwise assume UNIX
71 #if defined (CPU) && CPU == PPC603
72 #include <inetLib.h>
73 #include <hostLib.h>
74 #endif
75 
76 #ifdef __unix
77 #if !defined(_AIX)
78 #include <unistd.h>
79 #endif
80 #include <sys/socket.h>
81 #include <netinet/in.h>
82 #include <netdb.h>
83 #include <arpa/inet.h>
84 #if defined _AIX
85 #include <strings.h> // This is needed for FD_SET, bzero
86 #endif
87 
88 #if !defined __CYGWIN32__ && !defined __hpux && !defined linux && !defined _AIX
89 extern int h_errno; // defined in WinSock header, but not for UX?!
90 #endif
91 #endif // __unix
92 
93 #ifdef SNMP_PP_NAMESPACE
94 namespace Snmp_pp {
95 #endif
96 
97 //----[ macros ]-------------------------------------------------------
98 #define ADDRBUF 50 // worst case of address lens
99 #define OUTBUFF 80 // worst case of output lens
100 
101 #define IPLEN 4
102 #define UDPIPLEN 6
103 #define IP6LEN_NO_SCOPE 16
104 #define IP6LEN_WITH_SCOPE 20
105 #define UDPIP6LEN_NO_SCOPE 18
106 #define UDPIP6LEN_WITH_SCOPE 22
107 #define IS_IP6LEN(n) ((n==16) || (n==20))
108 #define IS_UDPIP6LEN(n) ((n==18) || (n==22))
109 #define IPXLEN 10
110 #define IPXSOCKLEN 12
111 #define MACLEN 6
112 #define MAX_FRIENDLY_NAME 80
113 #define PP_MAC_HASH0 19
114 #define PP_MAC_HASH1 13
115 #define PP_MAC_HASH2 7
116 
117 //---[ forward declarations ]-----------------------------------------
118 class GenAddress;
119 
120 //----[ Address class ]-----------------------------------------------
121 
122 /**
123  * Base class of all Address classes.
124  */
125 class DLLOPT Address : public SnmpSyntax
126 {
127  friend class GenAddress;
128 
129  public:
130  //----[ enumerated types for address types ]---------------------------
131  /**
132  * Type returned by Address::get_type().
133  */
135  {
136  type_ip, ///< IpAddress (IPv4 or IPv6)
137  type_ipx, ///< IpxAddress
138  type_udp, ///< UdpAddress (IPv4 or IPv6)
139  type_ipxsock, ///< IpxSockAddress
140  type_mac, ///< MacAddress
141  type_invalid ///< Used by GenAddress::get_type() if address is not valid
142  };
143 
144  /**
145  * Type returned by IpAddress::get_ip_version() and
146  * UdpAddress::get_ip_version().
147  */
149  {
150  version_ipv4, ///< IPv4
151  version_ipv6 ///< IPv6
152  };
153 
154  /**
155  * Default constructor, clears the buffer and sets valid flag to false.
156  */
157  Address();
158 
159  /**
160  * Allow destruction of derived classes.
161  */
162  virtual ~Address() {}
163 
164  /// overloaded equivlence operator, are two addresses equal?
165  DLLOPT friend int operator==(const Address &lhs,const Address &rhs);
166 
167  /// overloaded not equivlence operator, are two addresses not equal?
168  DLLOPT friend int operator!=(const Address &lhs, const Address &rhs)
169  { return !(lhs == rhs); }
170 
171  /// overloaded > operator, is a1 > a2
172  DLLOPT friend int operator>(const Address &lhs,const Address &rhs);
173 
174  /// overloaded >= operator, is a1 >= a2
175  DLLOPT friend int operator>=(const Address &lhs,const Address &rhs)
176  { if ((lhs > rhs) || (lhs == rhs)) return true; return false; }
177 
178  /// overloaded < operator, is a1 < a2
179  DLLOPT friend int operator<(const Address &lhs,const Address &rhs);
180 
181  /// overloaded <= operator, is a1 <= a2
182  DLLOPT friend int operator<=(const Address &lhs, const Address &rhs)
183  { if ((lhs < rhs) || (lhs == rhs)) return true; return false; }
184 
185  /// equivlence operator overloaded, are an address and a string equal?
186  DLLOPT friend int operator==(const Address &lhs,const char *rhs);
187 
188  /// overloaded not equivlence operator, are an address and string not equal?
189  DLLOPT friend int operator!=(const Address &lhs,const char *rhs)
190  { return !(lhs == rhs); }
191 
192  /// overloaded < , is an address greater than a string?
193  DLLOPT friend int operator>(const Address &lhs,const char *rhs);
194 
195  /// overloaded >=, is an address greater than or equal to a string?
196  DLLOPT friend int operator>=(const Address &lhs,const char *rhs);
197 
198  /// overloaded < , is an address less than a string?
199  DLLOPT friend int operator<(const Address &lhs,const char *rhs);
200 
201  /// overloaded <=, is an address less than or equal to a string?
202  DLLOPT friend int operator<=(const Address &lhs,const char *rhs);
203 
204  /**
205  * Overloaded operator for streaming output.
206  *
207  * @return String containing the numerical address
208  */
209  virtual operator const char *() const = 0;
210 
211  /**
212  * Return if the object contains a valid address.
213  *
214  * @return true if the object is valid
215  */
216  virtual bool valid() const { return valid_flag; }
217 
218  /**
219  * Return the space needed for serialization.
220  */
221  virtual int get_asn1_length() const = 0;
222 
223  /**
224  * Access as an array (read and write).
225  * @note Only pass in values between 0 and get_length().
226  *
227  * @param position - pos to return
228  * @return reference to the byte at the given position
229  */
230  unsigned char& operator[](const int position)
231  { addr_changed = true; valid_flag = true;
232  return (position < ADDRBUF) ? address_buffer[position]
233  : address_buffer[0]; }
234 
235  /**
236  * Access as an array (read only).
237  * @note Only pass in values between 0 and get_length().
238  *
239  * @param position - pos to return
240  * @return the byte at the given position
241  */
242  unsigned char operator[](const int position) const
243  { return (unsigned char)((position < ADDRBUF) ? address_buffer[ position] : 0); }
244 
245 
246  /**
247  * Get the length of the binary address (accessible through operator[]).
248  */
249  virtual int get_length() const = 0;
250 
251  /**
252  * Get the type of the address.
253  * @see Address::addr_type
254  */
255  virtual addr_type get_type() const = 0;
256 
257  using SnmpSyntax::operator =;
258  /**
259  * Overloaded assignment operator.
260  */
261  virtual Address & operator = (const Address &val) = 0;
262  virtual Address & operator = (const char *str) { valid_flag = parse_address(str); addr_changed = true; return *this; }
263 
264  // return a hash key
265  virtual unsigned int hashFunction() const { return 0; }
266 
267  protected:
270  unsigned char address_buffer[ADDRBUF]; // internal representation
271 
272  // parse the address string
273  // redefined for each specific address subclass
274  virtual bool parse_address(const char * inaddr) = 0;
275 
276  // format the output
277  // redefined for each specific address subclass
278  virtual void format_output() const = 0;
279 
280  /**
281  * Trim of whitespaces at the start and the end of the string.
282  *
283  * @param ptr - string to trim
284  */
285  void trim_white_space(char * ptr);
286 
287  /**
288  * Is this a GenAddress object.
289  */
290  virtual bool is_gen_address() const { return false; }
291 
292  /**
293  * Reset the object.
294  */
295  void clear();
296 };
297 
298 
299 //-----------------------------------------------------------------------
300 //---------[ IP Address Class ]------------------------------------------
301 //-----------------------------------------------------------------------
302 class DLLOPT IpAddress : public Address
303 {
304  public:
305  /**
306  * Construct an empty invalid IP address.
307  */
308  IpAddress();
309 
310  /**
311  * Construct an IP address from a string.
312  *
313  * The following formats can be used:
314  * - hostname with or without domain ("www.agentpp.com", "printsrv")
315  * - Numerical IPv4 address ("192.168.17.1")
316  * - Numerical IPv6 address ("abcd:1234::a:b:1", "::abcd:1")
317  * - Numerical IPv6 address with scope ("abcd:1234::a:b:1%3", "::abcd:1%1")
318  *
319  * @param inaddr - Hostname or IP address
320  */
321  IpAddress(const char *inaddr);
322 
323  /**
324  * Construct an IP address from another IP address.
325  *
326  * @param ipaddr - address to copy
327  */
328  IpAddress(const IpAddress &ipaddr);
329 
330  /**
331  * Construct an IP address from a GenAddress.
332  *
333  * @param genaddr - address to copy
334  */
335  IpAddress(const GenAddress &genaddr);
336 
337  /**
338  * Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
339  */
341 
342  using Address::operator =;
343 
344  /**
345  * Map other SnmpSyntax objects to IpAddress.
346  */
347  virtual SnmpSyntax& operator=(const SnmpSyntax &val);
348 
349  /**
350  * Map other Address objects to IpAddress.
351  */
352  virtual Address& operator = (const Address &val);
353 
354  /**
355  * Overloaded assignment operator for other IP addresses.
356  */
357  virtual IpAddress& operator=(const IpAddress &ipaddress);
358 
359  /**
360  * Clone this object.
361  *
362  * @return Pointer to the newly created object (allocated through new).
363  */
364  SnmpSyntax *clone() const { return (SnmpSyntax *) new IpAddress(*this); }
365 
366  /**
367  * Return the friendly name. Does a reverse DNS lookup for the IP address.
368  *
369  * @param status - The errno value for the lookup
370  *
371  * @return the friendly name or a zero length string (no null pointer)
372  */
373  char *friendly_name(int &status);
374 
375  /**
376  * Get a printable ASCII value of the address.
377  *
378  * @return String containing the numerical address
379  */
380  virtual const char *get_printable() const
381  { if (addr_changed) format_output(); return output_buffer; }
382 
383  /**
384  * Overloaded operator for streaming output.
385  *
386  * @return String containing the numerical address
387  */
388  virtual operator const char *() const
389  { if (addr_changed) format_output(); return output_buffer; }
390 
391  /**
392  * Logically AND the address with the param.
393  *
394  * @param ipaddr - address to use as mask
395  */
396  void mask(const IpAddress &ipaddr);
397 
398 
399  /**
400  * Get the count of matching bits from the left.
401  *
402  * @param match_ip - address to match with
403  */
404  int get_match_bits(const IpAddress match_ip) const;
405 
406  /**
407  * Get the length of the binary address (accessible through operator[]).
408  */
409  virtual int get_length() const
410  { return (ip_version == version_ipv4) ? IPLEN :
411  (have_ipv6_scope ? IP6LEN_WITH_SCOPE : IP6LEN_NO_SCOPE); }
412 
413  /**
414  * Return the type of the address.
415  * @see Address::addr_type
416  * @return Always Address:type_ip
417  */
418  virtual addr_type get_type() const { return type_ip; }
419 
420  /**
421  * Return the syntax.
422  *
423  * @return This method always returns sNMP_SYNTAX_IPADDR.
424  */
425  virtual SmiUINT32 get_syntax() const { return sNMP_SYNTAX_IPADDR; }
426 
427  /**
428  * Return the space needed for serialization.
429  */
430  virtual int get_asn1_length() const
431  { return get_length() + 2; }
432 
433  /**
434  * Return the IP version of the address.
435  *
436  * @return one of Address::version_type
437  */
438  virtual version_type get_ip_version() const { return ip_version; }
439 
440  /**
441  * Map a IPv4 address to a IPv6 address.
442  *
443  * @return - TRUE if no error occured.
444  */
445  virtual bool map_to_ipv6();
446 
447  /**
448  * Get the IPv6 scope
449  */
450  virtual unsigned int get_scope() const;
451 
452  /**
453  * Set the IPv6 scope
454  */
455  virtual bool set_scope(const unsigned int scope);
456 
457  /**
458  * Reset the object.
459  */
460  void clear();
461 
462  bool has_ipv6_scope() const
463  { return (ip_version == version_ipv6) && have_ipv6_scope; }
464 
465  protected:
466  SNMP_PP_MUTABLE char output_buffer[OUTBUFF]; // output buffer
467 
468  // friendly name storage
469  char iv_friendly_name[MAX_FRIENDLY_NAME];
471 
472  // redefined parse address
473  // specific to IP addresses
474  virtual bool parse_address(const char *inaddr);
475 
476  // redefined format output
477  // specific to IP addresses
478  virtual void format_output() const;
479 
480  // parse a dotted string
481  int parse_dotted_ipstring(const char *inaddr);
482 
483  // parse a coloned string
484  int parse_coloned_ipstring(const char *inaddr);
485 
486  // using the currently defined address, do a DNS
487  // and try to fill up the name
488  int addr_to_friendly();
489 
490  // support both ipv4 and ipv6 addresses
492 
494 };
495 
496 //------------------------------------------------------------------------
497 //---------[ UDP Address Class ]------------------------------------------
498 //------------------------------------------------------------------------
500 {
501  public:
502  /**
503  * Construct an empty invalid UDP address.
504  */
505  UdpAddress();
506 
507  /**
508  * Construct an UDP address from a string.
509  *
510  * The following formats can be used additional to those recognized by
511  * IpAdress:
512  * - Port added to IPv4 address with '/' or ':'
513  * ("192.168.17.1:161", "192.168.17.1/161", "printsrv/161")
514  * - Port added to IPv6 address with '/' or using '[...]:'
515  * ("::1/162", "[::1]/162", "[::1]:162")
516  *
517  * @param inaddr - Hostname or IP address
518  */
519  UdpAddress(const char *inaddr);
520 
521  /**
522  * Construct an UDP address from another UDP address.
523  *
524  * @param udpaddr - address to copy
525  */
526  UdpAddress(const UdpAddress &udpaddr);
527 
528  /**
529  * Construct an UDP address from a GenAddress.
530  *
531  * @param genaddr - address to copy
532  */
533  UdpAddress(const GenAddress &genaddr);
534 
535  /**
536  * Construct an UDP address from a IP address.
537  * The port will be set to 0.
538  *
539  * @param ipaddr - address to copy
540  */
541  UdpAddress(const IpAddress &ipaddr);
542 
543  /**
544  * Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
545  */
547 
548  using IpAddress::operator =;
549 
550  /**
551  * Map other SnmpSyntax objects to UdpAddress.
552  */
553  virtual SnmpSyntax& operator=(const SnmpSyntax &val);
554 
555  /**
556  * Map other Address objects to UdpAddress.
557  */
558  virtual Address & operator = (const Address &val);
559 
560  /**
561  * Overloaded assignment operator for UdpAddress.
562  */
563  virtual UdpAddress& operator=(const UdpAddress &udpaddr);
564 
565  /**
566  * Overloaded assignment operator for IpAddress.
567  */
568  virtual UdpAddress& operator=(const IpAddress &ipaddr);
569 
570  /**
571  * Return the syntax.
572  *
573  * @return This method always returns sNMP_SYNTAX_OCTETS.
574  */
576 
577  /**
578  * Return the space needed for serialization.
579  */
580  virtual int get_asn1_length() const { return get_length() + 2; }
581 
582  /**
583  * Clone this object.
584  *
585  * @return Pointer to the newly created object (allocated through new).
586  */
587  SnmpSyntax *clone() const { return (SnmpSyntax *) new UdpAddress(*this); }
588 
589  /**
590  * Get a printable ASCII value of the address.
591  *
592  * @return String containing the numerical address
593  */
594  virtual const char *get_printable() const
595  { if (addr_changed) format_output(); return output_buffer; }
596 
597  /**
598  * Overloaded operator for streaming output.
599  *
600  * @return String containing the numerical address
601  */
602  virtual operator const char *() const
603  { if (addr_changed) format_output(); return output_buffer; }
604 
605  /**
606  * Set the port number.
607  *
608  * @note If the object is not valid(), the port may not be set.
609  */
610  void set_port(const unsigned short p);
611 
612  /**
613  * Get the port number.
614  *
615  * @return The port number, or 0 is the object is not valid.
616  */
617  unsigned short get_port() const;
618 
619  /**
620  * Get the length of the binary address (accessible through operator[]).
621  */
622  virtual int get_length() const
623  { return (ip_version == version_ipv4) ? UDPIPLEN :
624  (have_ipv6_scope ? UDPIP6LEN_WITH_SCOPE : UDPIP6LEN_NO_SCOPE); }
625 
626  /**
627  * Return the type of the address.
628  * @see Address::addr_type
629  * @return Always Address:type_udp
630  */
631  virtual addr_type get_type() const { return type_udp; }
632 
633  /**
634  * Map a IPv4 UDP address to a IPv6 UDP address.
635  *
636  * @return - TRUE if no error occured.
637  */
638  virtual bool map_to_ipv6();
639 
640  /**
641  * Reset the object.
642  */
643  void clear()
644  { Address::clear(); memset(output_buffer, 0, sizeof(output_buffer)); }
645 
646  /**
647  * Set the IPv6 scope
648  */
649  virtual bool set_scope(const unsigned int scope);
650 
651  protected:
652  SNMP_PP_MUTABLE char output_buffer[OUTBUFF]; // output buffer
653  char sep; // separator
654 
655  // redefined parse address
656  // specific to IP addresses
657  virtual bool parse_address(const char *inaddr);
658 
659  // redefined format output
660  // specific to IP addresses
661  virtual void format_output() const;
662 };
663 
664 #ifdef _MAC_ADDRESS
665 //-------------------------------------------------------------------------
666 //---------[ 802.3 MAC Address Class ]-------------------------------------
667 //-------------------------------------------------------------------------
668 class DLLOPT MacAddress : public Address {
669 
670 public:
671  // constructor, no arguments
672  MacAddress();
673 
674  // constructor with a string argument
675  MacAddress(const char *inaddr);
676 
677  // constructor with another MAC object
678  MacAddress(const MacAddress &macaddr);
679 
680  // construct a MacAddress with a GenAddress
681  MacAddress(const GenAddress &genaddr);
682 
683  // destructor
684  ~MacAddress() {}
685 
686  /**
687  * Return the syntax.
688  *
689  * @return This method always returns sNMP_SYNTAX_OCTETS.
690  */
691  SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OCTETS; }
692 
693  /**
694  * Return the space needed for serialization.
695  */
696  virtual int get_asn1_length() const { return MACLEN + 2; }
697 
698  using Address::operator =;
699  /**
700  * Map other SnmpSyntax objects to MacAddress.
701  */
702  virtual SnmpSyntax& operator=(const SnmpSyntax &val);
703 
704  // assignment to another IpAddress object overloaded
705  MacAddress& operator=(const MacAddress &macaddress);
706 
707  /**
708  * Clone this object.
709  *
710  * @return Pointer to the newly created object (allocated through new).
711  */
712  SnmpSyntax *clone() const { return (SnmpSyntax *) new MacAddress(*this); }
713 
714  /**
715  * Get a printable ASCII value of the address.
716  *
717  * @return String containing the numerical address
718  */
719  virtual const char *get_printable() const
720  { if (addr_changed) format_output(); return output_buffer; }
721 
722  /**
723  * Overloaded operator for streaming output.
724  *
725  * @return String containing the numerical address
726  */
727  virtual operator const char *() const
728  { if (addr_changed) format_output(); return output_buffer; }
729 
730  /**
731  * Get the length of the binary address (accessible through operator[]).
732  */
733  virtual int get_length() const { return MACLEN; }
734 
735  /**
736  * Return the type of the address.
737  * @see Address::addr_type
738  * @return Always Address:type_mac
739  */
740  virtual addr_type get_type() const { return type_mac; }
741 
742  // return a hash key
743  unsigned int hashFunction() const;
744 
745  /**
746  * Reset the object.
747  */
748  void clear()
749  { Address::clear(); memset(output_buffer, 0, sizeof(output_buffer)); }
750 
751  protected:
752  SNMP_PP_MUTABLE char output_buffer[OUTBUFF]; // output buffer
753 
754  // redefined parse address for macs
755  virtual bool parse_address(const char *inaddr);
756 
757  // redefined format output for MACs
758  virtual void format_output() const;
759 };
760 #endif // _MAC_ADDRESS
761 
762 #ifdef _IPX_ADDRESS
763 //------------------------------------------------------------------------
764 //---------[ IPX Address Class ]------------------------------------------
765 //------------------------------------------------------------------------
766 class DLLOPT IpxAddress : public Address {
767 
768 public:
769  // constructor no args
770  IpxAddress();
771 
772  // constructor with a string arg
773  IpxAddress(const char *inaddr);
774 
775  // constructor with another ipx object
776  IpxAddress(const IpxAddress &ipxaddr);
777 
778  // construct with a GenAddress
779  IpxAddress(const GenAddress &genaddr);
780 
781  // destructor
782  ~IpxAddress() {}
783 
784  /**
785  * Return the syntax.
786  *
787  * @return This method always returns sNMP_SYNTAX_OCTETS.
788  */
789  virtual SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OCTETS; }
790 
791  /**
792  * Return the space needed for serialization.
793  */
794  virtual int get_asn1_length() const { return IPXLEN + 2; }
795 
796  using Address::operator =;
797  /**
798  * Map other SnmpSyntax objects to IpxAddress.
799  */
800  virtual SnmpSyntax& operator=(const SnmpSyntax &val);
801 
802  // assignment to another IpAddress object overloaded
803  virtual IpxAddress& operator=(const IpxAddress &ipxaddress);
804 
805 #ifdef _MAC_ADDRESS
806  // get the host id portion of an ipx address
807  int get_hostid(MacAddress& mac) const;
808 #endif
809 
810  /**
811  * Clone this object.
812  *
813  * @return Pointer to the newly created object (allocated through new).
814  */
815  SnmpSyntax *clone() const { return (SnmpSyntax *) new IpxAddress(*this); }
816 
817  /**
818  * Get a printable ASCII value of the address.
819  *
820  * @return String containing the numerical address
821  */
822  virtual const char *get_printable() const
823  { if (addr_changed) format_output(); return output_buffer; }
824 
825  /**
826  * Overloaded operator for streaming output.
827  *
828  * @return String containing the numerical address
829  */
830  virtual operator const char *() const
831  { if (addr_changed) format_output(); return output_buffer; }
832 
833  /**
834  * Get the length of the binary address (accessible through operator[]).
835  */
836  virtual int get_length() const { return IPXLEN; }
837 
838  /**
839  * Return the type of the address.
840  * @see Address::addr_type
841  * @return Always Address:type_ipx
842  */
843  virtual addr_type get_type() const { return type_ipx; }
844 
845  /**
846  * Reset the object.
847  */
848  void clear()
849  { Address::clear(); memset(output_buffer, 0, sizeof(output_buffer)); }
850 
851  protected:
852  // ipx format separator
853  char separator;
854  SNMP_PP_MUTABLE char output_buffer[OUTBUFF]; // output buffer
855 
856  // redefined parse address for ipx strings
857  virtual bool parse_address(const char *inaddr);
858 
859  // redefined format output for ipx strings
860  // uses same separator as when constructed
861  virtual void format_output() const;
862 
863 };
864 
865 
866 
867 //------------------------------------------------------------------------
868 //---------[ IpxSock Address Class ]--------------------------------------
869 //------------------------------------------------------------------------
870 class DLLOPT IpxSockAddress : public IpxAddress {
871 
872 public:
873  // constructor, no args
874  IpxSockAddress();
875 
876  // constructor with a dotted string
877  IpxSockAddress(const char *inaddr);
878 
879  // construct an Udp address with another Udp address
880  IpxSockAddress(const IpxSockAddress &ipxaddr);
881 
882  //constructor with a GenAddress
883  IpxSockAddress(const GenAddress &genaddr);
884 
885  //constructor with a IpxAddress
886  // default socket # is 0
887  IpxSockAddress(const IpxAddress &ipxaddr);
888 
889  // destructor
890  ~IpxSockAddress() {}
891 
892  // syntax type
893  //virtual SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OCTETS; }
894 
895  /**
896  * Return the space needed for serialization.
897  */
898  virtual int get_asn1_length() const { return IPXSOCKLEN + 2; }
899 
900  using IpxAddress::operator =;
901  /**
902  * Map other SnmpSyntax objects to IpxSockAddress.
903  */
904  virtual SnmpSyntax& operator=(const SnmpSyntax &val);
905 
906  // assignment to another IpAddress object overloaded
907  virtual IpxSockAddress& operator=(const IpxSockAddress &ipxaddr);
908 
909  /**
910  * Clone this object.
911  *
912  * @return Pointer to the newly created object (allocated through new).
913  */
914  SnmpSyntax *clone() const { return (SnmpSyntax *)new IpxSockAddress(*this); }
915 
916  // set the socket number
917  void set_socket(const unsigned short s);
918 
919  // get the socket number
920  unsigned short get_socket() const;
921 
922  /**
923  * Get a printable ASCII value of the address.
924  *
925  * @return String containing the numerical address
926  */
927  virtual const char *get_printable() const
928  { if (addr_changed) format_output(); return output_buffer; }
929 
930  /**
931  * Overloaded operator for streaming output.
932  *
933  * @return String containing the numerical address
934  */
935  virtual operator const char *() const
936  { if (addr_changed) format_output(); return output_buffer; }
937 
938  /**
939  * Get the length of the binary address (accessible through operator[]).
940  */
941  virtual int get_length() const { return IPXSOCKLEN; }
942 
943  /**
944  * Return the type of the address.
945  * @see Address::addr_type
946  * @return Always Address:type_ipxsock
947  */
948  virtual addr_type get_type() const { return type_ipxsock; }
949 
950  /**
951  * Reset the object.
952  */
953  void clear()
954  { Address::clear(); memset(output_buffer, 0, sizeof(output_buffer)); }
955 
956  protected:
957  SNMP_PP_MUTABLE char output_buffer[OUTBUFF]; // output buffer
958 
959  // redefined parse address for ipx strings
960  virtual bool parse_address(const char *inaddr);
961 
962  // redefined format output
963  // specific to IP addresses
964  virtual void format_output() const;
965 };
966 #endif // _IPX_ADDRESS
967 
968 
969 
970 
971 //-------------------------------------------------------------------------
972 //--------[ Generic Address ]----------------------------------------------
973 //-------------------------------------------------------------------------
974 class DLLOPT GenAddress : public Address
975 {
976  public:
977  /**
978  * Construct an empty invalid generic address object.
979  */
980  GenAddress();
981 
982  /**
983  * Construct a generic address from a string.
984  *
985  * To optimize the speed of the parsing method, use_type can be used
986  * to indicate that the address string is of the specified type.
987  *
988  * @param addr - address string
989  * @param use_type - if this value is set, the input string is only
990  * parsed for the given type
991  */
992  GenAddress(const char *addr,
993  const Address::addr_type use_type = Address::type_invalid);
994 
995  /**
996  * Construct a generic address from an Address object.
997  *
998  * @param addr - Any address object
999  */
1000  GenAddress(const Address &addr);
1001 
1002  /**
1003  * Construct a generic address from another generic address object.
1004  *
1005  * @param addr - Generic address object to copy
1006  */
1007  GenAddress(const GenAddress &addr);
1008 
1009  /**
1010  * Destructor, free memory.
1011  */
1012  ~GenAddress() { if (address) delete address; }
1013 
1014  /**
1015  * Return the syntax.
1016  *
1017  * @return This method returns sNMP_SYNTAX_IPADDR, sNMP_SYNTAX_OCTETS
1018  * or sNMP_SYNTAX_NULL if the generic address does not have
1019  * an address object.
1020  */
1022  { return address ? address->get_syntax() : sNMP_SYNTAX_NULL; }
1023 
1024  /**
1025  * Return the space needed for serialization.
1026  */
1027  virtual int get_asn1_length() const
1028  { return address ? address->get_asn1_length() : 2; }
1029 
1030  /**
1031  * Clone this object.
1032  *
1033  * @return Pointer to the newly created object (allocated through new).
1034  */
1035  SnmpSyntax *clone() const { return (SnmpSyntax *)new GenAddress(*this); }
1036 
1037  using Address::operator =;
1038  /**
1039  * Overloaded assignment operator for a GenAddress.
1040  */
1041  virtual GenAddress& operator=(const GenAddress &addr);
1042 
1043  /**
1044  * Overloaded assignment operator for a Address.
1045  */
1046  virtual Address& operator=(const Address &addr);
1047 
1048  /**
1049  * Map other SnmpSyntax objects to GenAddress.
1050  */
1051  virtual SnmpSyntax& operator=(const SnmpSyntax &val);
1052 
1053  /**
1054  * Get a printable ASCII value of the address.
1055  *
1056  * @return String containing the numerical address
1057  */
1058  virtual const char *get_printable() const
1059  { return (address) ? address->get_printable() : output_buffer; }
1060 
1061  /**
1062  * Overloaded operator for streaming output.
1063  *
1064  * @return String containing the numerical address
1065  */
1066  virtual operator const char *() const
1067  { return address ? (const char *)*address : output_buffer; }
1068 
1069  /**
1070  * Get the length of the binary address (accessible through operator[]).
1071  */
1072  virtual int get_length() const
1073  { return (address) ? address->get_length() : 0; }
1074 
1075  /**
1076  * Reset the object.
1077  */
1078  void clear() { if (address) address->clear(); }
1079 
1080  /**
1081  * Return the type of the address.
1082  * @see Address::addr_type
1083  * @return Type of the contained address object or Address::type_invalid
1084  * if it is not valid().
1085  */
1086  virtual addr_type get_type() const
1087  { return (valid()) ? address->get_type() : type_invalid; }
1088 
1089  /**
1090  * Access the protected address.
1091  * The caller must make sure that this GenAddress object ist valid()
1092  * and is of the right type (get_type()).
1093  */
1094  const IpAddress &cast_ipaddress() const { return (IpAddress& )*address; }
1095 
1096  /**
1097  * Access the protected address.
1098  * The caller must make sure that this GenAddress object ist valid()
1099  * and is of the right type (get_type()).
1100  */
1101  const UdpAddress &cast_udpaddress() const { return (UdpAddress&)*address; }
1102 
1103 #ifdef _MAC_ADDRESS
1104  /**
1105  * Access the protected address.
1106  * The caller must make sure that this GenAddress object ist valid()
1107  * and is of the right type (get_type()).
1108  */
1109  const MacAddress &cast_macaddress() const { return (MacAddress&)*address; }
1110 #endif
1111 
1112 #ifdef _IPX_ADDRESS
1113  /**
1114  * Access the protected address.
1115  * The caller must make sure that this GenAddress object ist valid()
1116  * and is of the right type (get_type()).
1117  */
1118  const IpxAddress &cast_ipxaddress() const { return (IpxAddress&)*address; }
1119 
1120  /**
1121  * Access the protected address.
1122  * The caller must make sure that this GenAddress object ist valid()
1123  * and is of the right type (get_type()).
1124  */
1125  const IpxSockAddress &cast_ipxsockaddress() const
1126  { return (IpxSockAddress&)*address; }
1127 #endif
1128 
1129 protected:
1130  // pointer to a concrete address
1132  char output_buffer[1]; // output buffer
1133 
1134  // redefined parse address for generic address
1135  virtual bool parse_address(const char *addr)
1136  { return parse_address(addr, Address::type_invalid); }
1137 
1138  virtual bool parse_address(const char *addr,
1139  const Address::addr_type use_type);
1140 
1141  // format output for a generic address
1142  virtual void format_output() const {}
1143 
1144  /**
1145  * Is this a GenAddress object.
1146  */
1147  virtual bool is_gen_address() const { return true; }
1148 };
1149 
1150 // create AddressCollection type
1153 
1154 #ifdef SNMP_PP_NAMESPACE
1155 } // end of namespace Snmp_pp
1156 #endif
1157 
1158 #endif //_ADDRESS
unsigned long SmiUINT32
Definition: smi.h:157
SNMP_PP_MUTABLE bool addr_changed
Definition: address.h:268
virtual void format_output() const
Definition: address.h:1142
virtual version_type get_ip_version() const
Return the IP version of the address.
Definition: address.h:438
~IpAddress()
Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
Definition: address.h:340
#define MACLEN
Definition: address.h:111
#define IP6LEN_WITH_SCOPE
Definition: address.h:104
#define SNMP_PP_MUTABLE
#define sNMP_SYNTAX_NULL
Definition: smi.h:103
unsigned char & operator[](const int position)
Access as an array (read and write).
Definition: address.h:230
SmiUINT32 get_syntax() const
Return the syntax.
Definition: address.h:575
IpAddress (IPv4 or IPv6)
Definition: address.h:136
#define UDPIP6LEN_WITH_SCOPE
Definition: address.h:106
virtual bool valid() const
Return if the object contains a valid address.
Definition: address.h:216
virtual SmiUINT32 get_syntax() const
Return the syntax.
Definition: address.h:425
#define sNMP_SYNTAX_IPADDR
Definition: smi.h:106
void clear()
Reset the object.
bool valid_flag
Definition: address.h:269
virtual const char * get_printable() const
Get a printable ASCII value of the address.
Definition: address.h:1058
DLLOPT friend int operator!=(const Address &lhs, const Address &rhs)
overloaded not equivlence operator, are two addresses not equal?
Definition: address.h:168
Address * address
Definition: address.h:1131
MacAddress.
Definition: address.h:140
#define sNMP_SYNTAX_OCTETS
Definition: smi.h:102
DLLOPT friend int operator<=(const Address &lhs, const Address &rhs)
overloaded <= operator, is a1 <= a2
Definition: address.h:182
UdpAddress (IPv4 or IPv6)
Definition: address.h:138
virtual int get_asn1_length() const
Return the space needed for serialization.
Definition: address.h:1027
int iv_friendly_name_status
Definition: address.h:470
virtual GenAddress & operator=(const GenAddress &addr)
Overloaded assignment operator for a GenAddress.
void trim_white_space(char *ptr)
Trim of whitespaces at the start and the end of the string.
#define MAX_FRIENDLY_NAME
Definition: address.h:112
#define IPLEN
Definition: address.h:101
DLLOPT friend int operator!=(const Address &lhs, const char *rhs)
overloaded not equivlence operator, are an address and string not equal?
Definition: address.h:189
#define DLLOPT
SnmpCollection< GenAddress > AddressCollection
Definition: address.h:1151
SmiUINT32 get_syntax() const
Return the syntax.
Definition: address.h:1021
virtual const char * get_printable() const
Get a printable ASCII value of the address.
Definition: address.h:594
Base class of all Address classes.
Definition: address.h:125
IpxSockAddress.
Definition: address.h:139
#define UDPIPLEN
Definition: address.h:102
addr_type
Type returned by Address::get_type().
Definition: address.h:134
version_type ip_version
Definition: address.h:491
#define ADDRBUF
Definition: address.h:98
virtual addr_type get_type() const
Return the type of the address.
Definition: address.h:631
IpxAddress.
Definition: address.h:137
SnmpCollection< UdpAddress > UdpAddressCollection
Definition: address.h:1152
virtual unsigned int hashFunction() const
Definition: address.h:265
const UdpAddress & cast_udpaddress() const
Access the protected address.
Definition: address.h:1101
bool has_ipv6_scope() const
Definition: address.h:462
virtual bool is_gen_address() const
Is this a GenAddress object.
Definition: address.h:1147
bool have_ipv6_scope
Definition: address.h:493
Used by GenAddress::get_type() if address is not valid.
Definition: address.h:141
const IpAddress & cast_ipaddress() const
Access the protected address.
Definition: address.h:1094
virtual int get_length() const
Get the length of the binary address (accessible through operator[]).
Definition: address.h:622
char sep
Definition: address.h:653
void clear()
Reset the object.
Definition: address.h:1078
virtual ~Address()
Allow destruction of derived classes.
Definition: address.h:162
unsigned char operator[](const int position) const
Access as an array (read only).
Definition: address.h:242
virtual int get_asn1_length() const
Return the space needed for serialization.
Definition: address.h:430
DLLOPT friend int operator>(const Address &lhs, const Address &rhs)
overloaded > operator, is a1 > a2
#define UDPIP6LEN_NO_SCOPE
Definition: address.h:105
virtual addr_type get_type() const
Return the type of the address.
Definition: address.h:418
virtual const char * get_printable() const
Get a printable ASCII value of the address.
Definition: address.h:380
virtual int get_asn1_length() const
Return the space needed for serialization.
Definition: address.h:580
unsigned char address_buffer[ADDRBUF]
Definition: address.h:270
#define IPXLEN
Definition: address.h:109
DLLOPT friend int operator<(const Address &lhs, const Address &rhs)
overloaded < operator, is a1 < a2
SnmpSyntax * clone() const
Clone this object.
Definition: address.h:587
SnmpSyntax * clone() const
Clone this object.
Definition: address.h:364
virtual int get_length() const
Get the length of the binary address (accessible through operator[]).
Definition: address.h:1072
struct sockaddr_in ipaddr
Definition: asn1.h:131
Address()
Default constructor, clears the buffer and sets valid flag to false.
#define IPXSOCKLEN
Definition: address.h:110
DLLOPT friend int operator>=(const Address &lhs, const Address &rhs)
overloaded >= operator, is a1 >= a2
Definition: address.h:175
virtual bool parse_address(const char *addr)
Definition: address.h:1135
virtual addr_type get_type() const
Return the type of the address.
Definition: address.h:1086
An "abstract" (pure virtual) class that serves as the base class for all specific SNMP syntax types...
Definition: smival.h:116
~GenAddress()
Destructor, free memory.
Definition: address.h:1012
virtual bool is_gen_address() const
Is this a GenAddress object.
Definition: address.h:290
DLLOPT friend int operator==(const Address &lhs, const Address &rhs)
overloaded equivlence operator, are two addresses equal?
SnmpSyntax * clone() const
Clone this object.
Definition: address.h:1035
#define OUTBUFF
Definition: address.h:99
#define IP6LEN_NO_SCOPE
Definition: address.h:103
void clear()
Reset the object.
Definition: address.h:643
version_type
Type returned by IpAddress::get_ip_version() and UdpAddress::get_ip_version().
Definition: address.h:148
virtual int get_length() const
Get the length of the binary address (accessible through operator[]).
Definition: address.h:409
~UdpAddress()
Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
Definition: address.h:546