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