<< Back to man.ChinaUnix.net

15.  Address Class

Object Modeling Technique (OMT) view of the SNMP++ Address Class


15.1.  What is the Network Address Class?

The network address class is a set of C++ classes which provide for simple, safe, portable  and efficient use of network addresses. Most network management applications require use of network addresses for accessing and managing devices. This includes address validation, modification and user interface control. Rather than manage all the internal details of particular network addresses, the Address class encapsulates and hides the internal mechanisms freeing the application programmer to focus on the problem at hand. The motivation for the development of the Address class emerged from input and discussion at the ‘95 Interop SNMP++ Birds-of-A-Feather (BOF) and from dialog with Hewlett-Packard OpenView programmers. 

15.2. Why use the Network Address Class?

The address class provides a number of benefits including: automatic memory management, address validation, portability to any C++ environment, ease of use and extensibility. Currently, the Address class consists of four classes, the IpAddress Class, the IpxAddress Class, the MacAddress class and the GenAddress class. In the future other subclasses will be added including IP Next Generation (IPng).  

15.3. Address Classes

The address classes are based around one abstract class, the Address class. This is an abstract class. That is, there may be no instances of this class. The Address class provides a consistent interface through the use of virtual member functions. This allows passing addresses to other functions using the generic interface. This minimizes code changes to modules using addresses. 


15.4. Address Classes and Interfaces

The base class, the Address class is an abstract class. The class contains the commonality of all derived address classes. This includes an identical interface for constructing, accessing and mutating Addresses.

Address Class Member Functions


IpAddress Class Constructors


      IpAddress::IpAddress( void);

Construct an empty IpAddress object.

      IpAddress::IpAddress( const char *string);

Construct an IpAddress from a string, do DNS if needed.

      IpAddress::IpAddress( const IpAddress &ipa);

Copy constructor.

IPAddress Member Functions


      char * friendly_name( int & status);

Invokes DNS lookup for friendly name.

UdpAddress Constructors


     UdpAddress::UdpAddress( void);

Construct an invalid UdpAddress Object.

     UdpAddress::UdpAddress( const char *string);

Construct a UdpAddress using a char string.

     UdpAddress::UdpAddress( const UdpAddress &udpa);

Construct a UdpAddress using another UdpAddress.

UdpAddress Member Functions


     void UdpAddress:set_port( const unsigned int p);

Set the port number for a UdpAddress object.

     unsigned int UdpAddress::get_port();

Get the port number from a UdpAddress object.

IpxAddress Class Constructors


      IpxAddress::IpxAddress( void);

Construct an empty IPX address.

      IpxAddress::IpxAddress( const char *string);

Construct an IPX address using a char string.

      IpxAddress::IpxAddress( const IpxAddress &ipxa);

Copy constructor.

IpxSockAddress Constuctors


     IpxSockAddress::IpxSockAddress( void);

Construct an empty IpxSockAddress object.

     IpxSockAddress::IpxSockAddress( const char *string);

Construct a IpxSockAddress using a char string.

     IpxSockAddress::IpxSockAddress( const IpxSockAddress   &ipxs);                                                  

Construct a IpxSockAddress using another IpxSockAddress.

IpxSockAddress Member Functions


     IpxSockAddress::set_socket( const unsigned int s);

Get the socket number from a IpxSockAddress.

     unsigned int IpxSocketAddress::get_socket();

Set the socket number into a IpxSockAddress.

MacAddress Constructors


      MacAddress::MacAddress( void);

Construct an empty MacAddress object.

      MacAddress::MacAddress( const char * string);   

Construct a MacAddress from a string.

      MacAddress::MacAddress( const MacAddress &mac);

Copy constructor.

GenAddress Constructors


     GenAddress::GenAddress( void);

Construct a invalid GenAddress object.

     GenAddress::GenAddress( const char * addr);

Construct a GenAddress using a string.

     GenAddress::GenAddress( const GenAddress &addr);

Copy constructor.

Common Member Functions, applicable to all Address classes


      int operator == ( const Address &lhs, const Address rhs);

Determine if two Addresses are equal.

      int operator != ( const Address &lhs, const Address &rhs);

Determine if two Addresses are not equal.

      int operator > ( const Address &lhs, const Address &rhs);

Determine if one Address is greater than another.

      int operator >= (const Address &lhs, const Address &rhs);

Determine if one Address is greater than or equal.

      int operator < ( const Address &lhs, const Address &rhs);

Determine if one Address is less than another.

      int operator<=( const Address &lhs, const Address &rhs);

Determine if one Address is less than or equal to another.

      int operator == ( const Address &lhs, cosnt char *inaddr);

Determine if two Addresses are equal.

      int operator > ( const Address &lhs, const char *inaddr);

Determine if an Address if greater than a string.

      int operator < ( const Address &lhs, const char *inaddr);

Determine if an Address is less than a string.

      virtual int valid( );        

Determine if an Address is valid.

      unsigned char& operator[]( int position);

Allow access to an Address object using array like access.

      char * get_printable ( );

Returns Address formatted for output.


15.5. IpAddress Class Special Features

The IpAddress class will do automatic Domain Name Services (DNS) lookup when calling the Address::get_printable() member function. If the DNS is not active or if the address cannot be resolved, the dotted format will be returned. Alternatively, an IpAddress can be constructed with a friendly name. In this case the constructor will invoke the DNS lookup. If the friendly name cannot be found, the address is invalid. This powerful feature allows you to utilize friendly names in your IpAddress user presentation.

15.6. GenAddress

The GenAddress class allows creation and usage of generic addresses where a GenAddress may take on the behavior and attributes of any of the other Address classes ( IpAddress, IpxAddress and MacAddress). When working with arbitrary addresses, you may use a GenAddress. The constructor for the GenAddress class allows creating an Address with any character string. The constructor determines the specific type of Address which matches the string and thereafter gives the GenAddress the attributes and behavior of that Address. This saves the programmer from having to write code which explicitly deals with the differences across Addresses.

GenAddress Examples


GenAddress address1(“”);                                     // make an IP GenAddress

GenAddress address2(“01020304-10111213141516”);     // make an IPX GenAddress

GenAddress address3(01:02:03:04:05:06”);                        // make a MAC GenAddress


cout << address3.get_printable();                                   // print out the GenAddress


if ( !address1.valid())                                                              // check validity

    cout << “address1 ! valid”;




15.7. Address Class Validation

All address classes support the ::valid() member function. The ::valid() member function returns the validity of the particular address object. Validation is determined when constructing or assigning  address objects.  After assignment, the ::valid() member function may be used to determine validity.

Address Class Validation Examples


MacAddress mac;

mac = “01.010a0d”;                                    // invalid MAC address

printf(“%s”, (mac.valid() ? “Valid” :”Invalid”));





15.8. UdpAddresses and IpxSockAddresses

For most usage, users of SNMP++ will utilize the well know port and socket numbers for SNMP operations. For the Internet Protocol (IP) this includes using port 161 for an agent’s destination port and port 162 for the trap / notification reception port. There are time when alternate port / socket specification is required. For these instances, the UdpAddress class and IpxSockAddress class allow definition of port or socket information.


15.8.1.Using UdpAddresses for Making Requests

When requesting information from an agent which does not listen on the standard well know port, the UdpAddress class should be used. The UdpAddress class supports two member functions for setting and getting custom port information. Attaching a UdpAddress to a Target and using it for requests will cause SNMP++ to utilize the custom port number.

15.8.2.Using IpxSockAddresses for Making Requests

When requesting information from an agent which does not listen on the standard well know IPX socket number, the IpxSockAddress class should be used. The IpxSockAddress class supports two member functions for setting and getting custom socket number information. Attaching a IpxSockAddress to a Target and using it for requests will cause SNMP++ to utilize the custom socket number.


15.8.3.Using UdpAddress and IpxSockAddress for Notification Reception

UdpAddresses and IpxSockAddresses may also be used to specify alternate ports and sockets for notification reception. This allows applications to receive traps and informs on non standard ports and socket numbers.

15.9.  Valid Address Formats

Valid Address formats for addresses are currently defined as:


Valid IP format BNF Grammar XXX.XXX.XXX.XXX

ip-address : ip-token DOT ip-token DOT ip-token DOT ip-token

DOT : ‘.’

ip-token : [0-255]



ipx-address: net-id SEPARATOR mac-id

SEPARATOR : ‘ ‘ | ‘:’ | ‘-’ | ‘.’

net_id : 1{byte-token}4

mac-id: 1{byte-token}6

byte-token: 1{byte}2

byte: [0-9|a-f|A-F]


Valid MAC format BNF Grammar XX:XX:XX:XX:XX:XX

mac-id: byte_token colon byte_token colon byte_token colon byte_token colon byte_token

byte-token: 1{byte}2

byte: [0-9|a-f|A-F]

colon: ‘:’


15.10.           Address Class Examples

// address class examples


void address_examples()


   //--------------[ IPAddress construction ]------------------------------------------------------

   IpAddress ip1();                                    // makes an invalid IpAddress object

   IpAddress ip2(“”);                     // makes a IpAddress verifies dotted format

   IpAddress ip3(ip2);                                     // makes an IpAddress using another IpAddress

   IpAddress ip4(“trout.rose.hp.com”);        // makes an IpAddress does DNS on string


   //-------------[ IPX Address construction ]-----------------------------------------------------

   IpxAddress ipx1();                                                        // makes an invalid IPX address

   IpxAddress ipx2(”);                             // makes and verifies an IPX address

   IpxAddress ipx3( ipx2);                                           // makes an IPX from another IPX


   //--------------[ MAC Address construction ]-----------------------------------------------------

   MacAddress mac1();                                              // makes an invalid MAC address

   MacAddress mac2(08:09:12:34:52:12”);          // makes and verifies a MAC address

   MacAddress mac3( mac2);                                          // makes a MAC from another MAC


  //---------------[ Gen Address Construction ]-----------------------------------------------------

  GenAddress addr1(“”);

  GenAddress addr2(“01020304:050607080900”);


   //--------------[ printing addresses ]----------------------------------------------------------------

   cout << (char *) ip2;

   cout << (char *) ipx2;

   cout << (char *) mac2;


   //---------------[ assigning Addresses ]------------------------------------------------------------

   ip1 = “”;

   ipx1 = “00000001-080912345212”;

   mac1 = “08:09:12:34:52:12”;


   //--------------[ comparing Addresses ]----------------------------------------------------------

   if ( ip1 == ip2)

       cout << “ip1 == ip2”;


   if (ipx1 != ipx2)

      cout << “ipx1 != ipx2”;


   if ( mac1 <= mac2)

      cout << “mac1 < mac2”;


//---------------[ modifying an address ]-----------------------------------------------------------

   mac1[4] = 15;

   cout << mac2[2];



};  // end address examples