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