3 //=============================================================================
7 * Address class definition. Encapsulates various network
8 * addresses into easy to use, safe and portable classes.
10 * @author Peter E Mellquist
12 //=============================================================================
15 /*===================================================================
17 Hewlett-Packard Company
19 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
20 Permission to use, copy, modify, distribute and/or sell this software
21 and/or its documentation is hereby granted without fee. User agrees
22 to display the above copyright notice and this license notice in all
23 copies of the software and any documentation of the software. User
24 agrees to assume all liability for the use of the software; Hewlett-Packard
25 makes no representations about the suitability of this software for any
26 purpose. It is provided "AS-IS without warranty of any kind,either express
27 or implied. User hereby grants a royalty-free license to any and all
28 derivatives based upon this software code base.
29 =====================================================================*/
31 //----[ includes ]-----------------------------------------------------
32 // ACE OS Adaption layer
33 #include "ace/os_include/netinet/os_in.h"
34 #include "ace/os_include/os_netdb.h"
35 #include "ace/Log_Msg.h"
37 #if !defined (ACE_LACKS_PRAGMA_ONCE)
39 #endif /* ACE_LACKS_PRAGMA_ONCE */
41 #include "asnmp/smival.h"
42 #include "asnmp/octet.h"
44 //----[ enumerated types ] --------------------------------
45 enum address_lengths
{
60 MAX_DISPLAY_SZ
=MAXHOSTNAMELEN
+1
77 // TODO: get rest of nb service types added here
79 nb_workstation
= 0x0, nb_server
= 0x20
82 //---[ forward declarations ]-----------------------------------------
87 class SIPAddress
; // aka ipv6
90 // class AtmE164Address;
93 //--------------------------------------------------------------------
94 //----[ Address class ]-----------------------------------------------
95 //--------------------------------------------------------------------
99 * @brief Defines the member functions for the abstract base class
100 * Address. An Address is a unique network endpoint.
102 class ASNMP_Export Address
: public SnmpSyntax
105 /// allow destruction of derived classes
108 /// overloaded equivlence operator, are two addresses equal?
109 friend ASNMP_Export
bool operator==( const Address
&lhs
,const Address
&rhs
);
111 /// overloaded not equivlence operator, are two addresses not equal?
112 friend ASNMP_Export
bool operator!=( const Address
&lhs
,const Address
&rhs
);
114 /// overloaded > operator, is a1 > a2
115 friend ASNMP_Export
bool operator>( const Address
&lhs
,const Address
&rhs
);
117 /// overloaded >= operator, is a1 >= a2
118 friend ASNMP_Export
bool operator>=( const Address
&lhs
,const Address
&rhs
);
120 /// overloaded < operator, is a1 < a2
121 friend ASNMP_Export
bool operator<( const Address
&lhs
,const Address
&rhs
);
123 /// overloaded <= operator, is a1 <= a2
124 friend ASNMP_Export
bool operator<=( const Address
&lhs
,const Address
&rhs
);
126 /// equivlence operator overloaded, are an address and a string equal?
127 friend ASNMP_Export
bool operator==( const Address
&lhs
,const char *rhs
);
129 /// overloaded not equivlence operator, are an address and string not equal?
130 friend ASNMP_Export
bool operator!=( const Address
&lhs
,const char *rhs
);
132 /// overloaded < , is an address greater than a string?
133 friend ASNMP_Export
bool operator>( const Address
&lhs
,const char *rhs
);
135 /// overloaded >=, is an address greater than or equal to a string?
136 friend ASNMP_Export
bool operator>=( const Address
&lhs
,const char *rhs
);
138 /// overloaded < , is an address less than a string?
139 friend ASNMP_Export
bool operator<( const Address
&lhs
,const char *rhs
);
141 /// overloaded <=, is an address less than or equal to a string?
142 friend ASNMP_Export
bool operator<=( const Address
&lhs
,const char *rhs
);
144 /// overloaded const char * cast
145 virtual operator const char *() const = 0;
147 /// verify the is the address object constructed ok
148 virtual int valid() const;
150 /// return a suitable buffer to contain the address
151 virtual void to_octet(OctetStr
& octet
) const = 0;
153 /// (pure virtual) syntax type
154 virtual SmiUINT32
get_syntax() = 0;
156 /// for non const [], allows reading and writing
157 unsigned char& operator[]( const int position
);
159 /// get a printable ASCII value
160 virtual const char *to_string() = 0;
162 /// create a new instance of this Value
163 virtual SnmpSyntax
*clone() const = 0;
165 /// return the type of address
166 virtual addr_type
get_type() const = 0;
168 /// overloaded assignment operator
169 virtual SnmpSyntax
& operator=( SnmpSyntax
&val
) = 0;
171 /// return a hash key
172 virtual unsigned int hashFunction() const { return 0;};
176 /// state of constructed object (1/0)
177 /// addr internal representation
179 unsigned char address_buffer
[MAX_ADDR_SZ
];
181 /// parse the address string
182 /// redefined for each specific address subclass
183 virtual int parse_address( const char * inaddr
) =0;
185 /// format the output
186 /// redefined for each specific address subclass
187 virtual void format_output() =0;
189 /// a reused trimm white space method
190 void trim_white_space( char * ptr
);
194 //-----------------------------------------------------------------------
195 //---------[ IPv4 Address Class ]----------------------------------------
196 //-----------------------------------------------------------------------
200 * @brief Defines the member functions for the concrete class IpAddress
201 * An IP Version 4 Address is 4 bytes long and consists of a
202 * Network, Sub Network, and host component.
204 class ASNMP_Export IpAddress
: public Address
207 /// default construct an IP address with a string
208 IpAddress( const char *inaddr
= "");
210 /// construct an IP address with another IP address
211 IpAddress( const IpAddress
&ipaddr
);
213 /// construct an IP address with a GenAddress
214 IpAddress( const GenAddress
&genaddr
);
216 /// destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden)
219 /// copy an instance of this Value
220 SnmpSyntax
& operator=( SnmpSyntax
&val
);
222 /// assignment to another IpAddress object overloaded
223 IpAddress
& operator=( const IpAddress
&ipaddress
);
225 // TODO: add ability to set addr given long
227 /// create a new instance of this Value
228 SnmpSyntax
*clone() const;
230 /// return the DNS Fully Qualified Domain Name (host.domain)
231 /// on failure returns dotted_quad string
232 const char *resolve_hostname(int& was_found
);
234 /// return string representation of object (dotted quad returned)
235 virtual const char *to_string() ;
237 /// const char * operator overloaded for streaming output
238 virtual operator const char *() const;
240 /// logically AND two IPaddresses and
241 /// return the new one
242 void mask( const IpAddress
& ipaddr
);
245 virtual addr_type
get_type() const;
248 virtual SmiUINT32
get_syntax();
250 /// is this the loopback address? 127.0.0.1/loopback/1.0.0.127.in-addr.arpa
251 int is_loopback() const;
253 /// determine if this is a multicast address
254 int is_multicast() const;
256 /// determine if this a broadcast address
257 int is_broadcast() const;
259 /// per RFC 1597, private addresses are:: 10, 172.16, and 192.168.0
260 int is_private() const;
262 /// convert address into octet string format in network byte order
263 virtual void to_octet(OctetStr
& octet
) const;
266 char output_buffer
[MAX_DISPLAY_SZ
]; // output buffer
268 /// friendly name storage
269 char iv_friendly_name_
[MAX_DISPLAY_SZ
];
271 /// did resolver call work? some addrs won't resolve
272 int iv_friendly_name_status_
;
274 /// redefined parse address
275 /// specific to IP addresses
276 virtual int parse_address( const char *inaddr
);
278 /// redefined format output
279 /// specific to IP addresses
280 virtual void format_output();
282 /// parse a dotted string
283 int parse_dotted_ipstring( const char *inaddr
);
285 /// using the currently defined address, do a gethostbyname()
286 /// and try to fill up the name
287 int addr_to_friendly();
289 /// thread safe routine to lookup ip address given hostname
290 /// return <> 0 on error
291 static int resolve_to_address(const char *hostname
, in_addr
& quad_addr
);
293 /// thread safe routine to lookup name given ip address
294 /// return <> 0 on error
295 static int resolve_to_hostname(const in_addr
& quad_addr
, char *hostname
);
298 //------------------------------------------------------------------------
299 //--------------[ DNS Iterator Class ]------------------------------------
300 //------------------------------------------------------------------------
303 * @class Address_Iter
305 * @brief Defines routines to obtain information on a hostname/FQDN
306 * such as multiple addresses
308 class ASNMP_Export Address_Iter
311 Address_Iter(const char *hostname
); // fully qualified domain name, hostname
313 /// did hostname resolve via DNS?
316 /// how many addresses associated with this hostname
317 int how_many_addresses();
319 /// return next address
320 int next(IpAddress
& addr
);
323 Address_Iter(const Address_Iter
&);
324 int valid_
; // ctor status
325 int count_
; // number of addresses
326 char **entry_
; // ptr to current address
327 struct hostent lookupResult_
;
328 ACE_HOSTENT_DATA buffer_
;
329 int query_dns(const char *hostname
);
332 //------------------------------------------------------------------------
333 //---------[ UDP/IPv4 Address Class ]-------------------------------------
334 //------------------------------------------------------------------------
338 * @brief Defines the member functions for the concrete class UdpAddress
339 * A Udp Address consists of an IP Version 4 Address (IpAddress)
340 * and a 2 byte unsigned port number. (see /etc/services file)
341 * User Datagram Protocol (UDP) is a best effort transport
343 class ASNMP_Export UdpAddress
: public IpAddress
346 /// default constructor with a dotted string in the form of addr:port
347 UdpAddress( const char *inaddr
= "");
349 /// construct an Udp address with another Udp address
350 UdpAddress( const UdpAddress
&udpaddr
);
352 /// construct a Udp address with a GenAddress
353 UdpAddress( const GenAddress
&genaddr
);
355 /// construct a Udp address with an IpAddress
356 /// default port # to zero
357 UdpAddress( const IpAddress
&ipaddr
);
363 SmiUINT32
get_syntax();
365 /// copy an instance of this Value
366 SnmpSyntax
& operator=( SnmpSyntax
&val
);
368 /// assignment to another IpAddress object overloaded
369 UdpAddress
& operator=( const UdpAddress
&udpaddr
);
371 /// create a new instance of this Value
372 SnmpSyntax
*clone() const;
374 /// output in the form of address:port
375 virtual const char *to_string() ;
377 /// const char * operator overloaded for streaming output
378 virtual operator const char *() const;
380 /// set the port number
381 void set_port( const unsigned short p
);
383 /// get the port number
384 unsigned short get_port() const;
387 virtual addr_type
get_type() const;
391 char output_buffer
[MAX_DISPLAY_SZ
];
393 /// redefined parse address
394 /// specific to IP addresses
395 virtual int parse_address( const char *inaddr
);
397 /// redefined format output
398 /// specific to IP addresses
399 virtual void format_output();
403 //-------------------------------------------------------------------------
404 //---------[ 802.3 MAC Address Class ]-------------------------------------
405 //-------------------------------------------------------------------------
409 * @brief Defines the member functions for the concrete class MacAddress.
410 * A Media Access Control Address consists of 48 bits as defined
411 * in IEEE 802.3 specifications.
413 class ASNMP_Export MacAddress
: public Address
416 /// constructor with a string argument
417 MacAddress( const char *inaddr
= "");
419 /// constructor with another MAC object
420 MacAddress( const MacAddress
&macaddr
);
422 /// construct a MacAddress with a GenAddress
423 MacAddress( const GenAddress
&genaddr
);
429 SmiUINT32
get_syntax();
431 /// copy an instance of this Value
432 SnmpSyntax
& operator=( SnmpSyntax
&val
);
434 /// assignment to another IpAddress object overloaded
435 MacAddress
& operator=( const MacAddress
&macaddress
);
437 /// create a new instance of this Value
438 SnmpSyntax
*clone() const;
440 /// create a string to internal class storage representing object
441 virtual const char *to_string();
443 /// const char * operator overloaded for streaming output
444 virtual operator const char *() const;
447 virtual addr_type
get_type() const;
449 /// return a hash key
450 unsigned int hashFunction() const;
452 /// return byte array of the mac address
453 virtual void to_octet(OctetStr
& octet
) const;
456 /// output buffer containing string representation of object
457 char output_buffer
[MAX_DISPLAY_SZ
];
459 /// redefined parse address for macs
460 virtual int parse_address( const char *inaddr
);
462 /// redefined format output for MACs
463 virtual void format_output();
466 //------------------------------------------------------------------------
467 //---------[ Netbios Address Class ]--------------------------------------
468 //------------------------------------------------------------------------
472 * @brief Defines the member functions for the concrete class NetbiosAddress.
473 * The IBM/Microsoft address for NETBIOS, NETBEUI protocol transport.
475 class ASNMP_Export NetbiosAddress
: public Address
478 /// default constructor with string arg
479 NetbiosAddress( const char *inaddr
= "");
481 /// set name and service type
482 NetbiosAddress( const char *inaddr
, nb_service svc
);
485 NetbiosAddress( const NetbiosAddress
& nbaddr
);
487 /// construct with a GenAddress
488 NetbiosAddress( const GenAddress
& genaddr
);
492 virtual const char *to_string();
494 NetbiosAddress
& operator=( const NetbiosAddress
&nbaddr
);
496 /// retrieve the network service type
497 nb_service
get_service_type() const;
499 /// set the service type (workstation, server, etc)
500 void set_service_type(nb_service nbservice
);
502 /// const char * operator overloaded for streaming output
503 virtual operator const char *() const;
506 virtual SmiUINT32
get_syntax();
508 /// copy an instance of this Value
509 SnmpSyntax
& operator=( SnmpSyntax
&val
);
511 /// create a new instance of this Value
512 SnmpSyntax
*clone() const;
514 /// output byte buffer containing netbios name
515 virtual void to_octet(OctetStr
& octet
) const;
518 void InitNBAddr(const char *inaddr
);
519 char output_buffer
[MAX_DISPLAY_SZ
];
521 /// output buffer to hold string representation
522 virtual void format_output();
523 virtual int parse_address( const char *inaddr
);
524 virtual addr_type
get_type() const;
527 //------------------------------------------------------------------------
528 //---------[ DecNet Address Class ]---------------------------------------
529 //------------------------------------------------------------------------
533 * @brief Defines the member functions for the concrete class DecNetAddress.
534 * DecNet Phase ? address consists of two octets (CISCO-TC.my)
536 class ASNMP_Export DecNetAddress
: public Address
539 /// default constructor with string arg
540 DecNetAddress( const char *inaddr
= "");
543 DecNetAddress( const DecNetAddress
& decaddr
);
545 /// construct with a GenAddress
546 DecNetAddress( const GenAddress
& genaddr
);
550 virtual const char *to_string();
552 DecNetAddress
& operator=( const DecNetAddress
&decaddr
);
554 /// convert address into octet string format 2 bytes of decnet address
555 virtual void to_octet(OctetStr
& octet
) const;
557 /// const char * operator overloaded for streaming output
558 virtual operator const char *() const;
561 virtual SmiUINT32
get_syntax();
563 /// copy an instance of this Value
564 SnmpSyntax
& operator=( SnmpSyntax
&val
);
566 /// create a new instance of this Value
567 SnmpSyntax
*clone() const;
570 virtual int parse_address( const char *inaddr
);
571 virtual addr_type
get_type() const;
572 virtual void format_output();
573 char output_buffer
[MAX_DISPLAY_SZ
];
576 //------------------------------------------------------------------------
577 //---------[ AppleTalk Address Class ]------------------------------------
578 //------------------------------------------------------------------------
582 * @brief Defines the member functions for the concrete class DecNetAddress.
583 * DecNet Phase ? address consists of two octets (CISCO-TC.my)
585 class ASNMP_Export AppleTalkAddress
: public Address
588 /// default constructor with string arg
589 AppleTalkAddress( const char *inaddr
= "");
592 AppleTalkAddress( const AppleTalkAddress
& atkaddr
);
594 /// construct with a GenAddress
595 AppleTalkAddress( const GenAddress
& genaddr
);
599 virtual const char *to_string();
601 AppleTalkAddress
& operator=( const AppleTalkAddress
&atkaddr
);
603 /// convert address into octet string format 3 bytes of atk address
604 virtual void to_octet(OctetStr
& octet
) const;
606 /// get the host part of the address
607 char get_host_address() const;
609 /// set the host part of the address
610 void set_host_address(const char);
612 /// get the 2 byte atk network address
613 short get_net_address() const;
615 /// set the host 2 byte atk network address
616 void set_net_address(const short atknet
);
618 /// const char * operator overloaded for streaming output
619 virtual operator const char *() const;
622 virtual SmiUINT32
get_syntax();
624 /// copy an instance of this Value
625 SnmpSyntax
& operator=( SnmpSyntax
&val
);
627 /// create a new instance of this Value
628 SnmpSyntax
*clone() const;
631 virtual int parse_address( const char *inaddr
);
632 virtual addr_type
get_type() const;
633 virtual void format_output();
634 char output_buffer
[MAX_DISPLAY_SZ
];
637 //------------------------------------------------------------------------
638 //---------[ IPX Address Class ]------------------------------------------
639 //------------------------------------------------------------------------
643 * @brief Defines the member functions for the concrete class IpxAddress.
644 * Novell's IPX (version ?) network protocol endpoint
646 class ASNMP_Export IpxAddress
: public Address
649 /// default constructor with a string arg
650 IpxAddress( const char *inaddr
= "");
652 /// constructor with another ipx object
653 IpxAddress( const IpxAddress
&ipxaddr
);
655 /// construct with a GenAddress
656 IpxAddress( const GenAddress
&genaddr
);
662 virtual SmiUINT32
get_syntax();
664 /// copy an instance of this Value
665 SnmpSyntax
& operator=( SnmpSyntax
&val
);
667 /// assignment to another IpxAddress object overloaded
668 IpxAddress
& operator=( const IpxAddress
&ipxaddress
);
670 /// get the host id portion of an ipx address
671 int get_hostid( MacAddress
& mac
);
673 /// create a new instance of this Value
674 SnmpSyntax
*clone() const;
676 /// create string represtation of object value
677 virtual const char *to_string();
679 /// const char * operator overloaded for streaming output
680 virtual operator const char *() const;
683 virtual addr_type
get_type() const;
685 /// return byte sequence containing ipx address
686 virtual void to_octet(OctetStr
& octet
) const;
689 /// ipx format separator {:,/}
692 /// output buffer to hold string representation
693 char output_buffer
[MAX_DISPLAY_SZ
];
695 /// redefined parse address for ipx strings
696 virtual int parse_address( const char *inaddr
);
698 /// redefined format output for ipx strings
699 /// uses same separator as when constructed
700 virtual void format_output();
704 //------------------------------------------------------------------------
705 //---------[ IpxSock Address Class ]--------------------------------------
706 //------------------------------------------------------------------------
708 * @class IpxSockAddress
710 * @brief Defines the member functions for the concrete class IpxAddress.
711 * Novell's IPX (version ?) network protocol endpoint
713 class ASNMP_Export IpxSockAddress
: public IpxAddress
716 /// constructor with a dotted string
717 IpxSockAddress( const char *inaddr
= "");
719 /// construct an Udp address with another Udp address
720 IpxSockAddress( const IpxSockAddress
&ipxaddr
);
722 ///constructor with a GenAddress
723 IpxSockAddress( const GenAddress
&genaddr
);
725 ///constructor with a IpxAddress
726 /// default socket # is 0
727 IpxSockAddress( const IpxAddress
&ipxaddr
);
733 virtual SmiUINT32
get_syntax();
735 /// copy an instance of this Value
736 SnmpSyntax
& operator=( SnmpSyntax
&val
);
738 /// assignment to another IpxAddress object overloaded
739 IpxSockAddress
& operator=( const IpxSockAddress
&ipxaddr
);
741 /// create a new instance of this Value
742 SnmpSyntax
*clone() const;
744 /// set the socket number
745 void set_socket( const unsigned short s
);
747 /// get the socket number
748 unsigned short get_socket() const;
750 /// create string representation of object value
751 virtual const char *to_string();
753 /// const char * operator overloaded for streaming output
754 virtual operator const char *() const;
757 virtual addr_type
get_type() const;
760 /// output buffer to hold string representation of object
761 char output_buffer
[MAX_DISPLAY_SZ
];
763 /// redefined parse address for ipx strings
764 virtual int parse_address( const char *inaddr
);
766 /// redefined format output
767 /// specific to IP addresses
768 virtual void format_output();
771 //-------------------------------------------------------------------------
772 //--------[ Generic Address ]----------------------------------------------
773 //-------------------------------------------------------------------------
777 * @brief Defines the member functions for the concrete class GenAddress.
778 * This class attempts to determine an address type given a char string.
780 class ASNMP_Export GenAddress
: public Address
783 /// constructor with a string argument
784 GenAddress( const char *addr
= "");
786 /// constructor with an Address
787 GenAddress( const Address
&addr
);
789 /// constructor with another GenAddress
790 GenAddress( const GenAddress
&addr
);
795 /// get the snmp syntax of the contained address
796 SmiUINT32
get_syntax();
798 /// create a new instance of this Value
799 SnmpSyntax
*clone() const;
801 /// assignment of a GenAddress
802 GenAddress
& operator=( const GenAddress
&addr
);
804 /// copy an instance of this Value
805 SnmpSyntax
& operator=( SnmpSyntax
&val
);
807 /// string representation of object value
808 virtual const char *to_string();
810 /// const char * operator overloaded for streaming output
811 virtual operator const char *() const;
814 virtual addr_type
get_type() const;
816 /// return the address as a octet sequence
817 virtual void to_octet(OctetStr
& octet
) const;
820 /// pointer to a a concrete address
823 /// output buffer of objects value
824 char output_buffer
[MAX_DISPLAY_SZ
];
826 /// redefined parse address for macs
827 virtual int parse_address( const char *addr
);
829 /// format output for a generic address
830 virtual void format_output();
832 /// initialize smi data structure