Address Class Reference

An address class. More...

#include <common.h>

List of all members.

Public Member Functions

 Address ()
 Constructor.
 Address (const char *hostname, short port)
 Alternative construcor with parameters.
bool isSet ()
void setPort (const short port)
short getPort ()
 get the port #
void setHostname (const char *hostname)
 set the hostname
char * getHostname ()
 get the hostname string pointer
unsigned char * getHWAddr ()
 get the MAC address
void setHWAddr (unsigned char *hwaddr)
void setHWAddrFromColonFormat (const char *colon_seperated_macaddr)
char * convertHWAddrToColonFormat ()
Addressclone ()
 function to clone this address
bool isSame (Address *addr)
bool isSameMACAddr (Address *addr)

Protected Attributes

char hostname_ [MAX_HOSTNAME_LENGTH]
 both hostname and ipaddress format (10.0.0.1) could be given as a string
short port_
 port number for UDP or TCP (Transport layer)
char * ipaddr_
 optional use... ignore....
unsigned char macaddr_ [MAC_ADDR_LENGTH]
 optional use for Ethernet Socket


Detailed Description

An address class.

Address Class is to handle addresses of unix/linux sockets. For normal sockets, the address used will be a combination of IP address and port. In Socket Programming, IP address itself is usually not enough to distinguish an connection, port # is also needed. For PF_PACKET sockets, the address used is MAC address (HW address). So, we also put macaddr_ as a member variable.

Definition at line 138 of file common.h.


Member Function Documentation

char * Address::convertHWAddrToColonFormat  ) 
 

Convert HW Address to Colon Seperated format

Definition at line 216 of file common.cpp.

References macaddr_.

00217 {
00218   char *colonformat =  new char[17];
00219   //printf("HW Address: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",u[0], u[1], u[2], u[3], u[4], u[5]);
00220   sprintf(colonformat,"%02X:%02X:%02X:%02X:%02X:%02X",
00221           macaddr_[0],macaddr_[1],macaddr_[2],macaddr_[3],macaddr_[4],macaddr_[5]);
00222   // cout << colonformat << endl;
00223   return colonformat;
00224 
00225 }

bool Address::isSame Address addr  )  [inline]
 

Compare whether the two normal "name+port" address is same or not

Definition at line 176 of file common.h.

References getHostname(), getPort(), hostname_, and port_.

00177   {
00178     if ( port_==  addr->getPort()) return false;
00179     if ( strcmp( hostname_, addr->getHostname() ) == 0 ) return true;
00180     return false;
00181   } 

bool Address::isSameMACAddr Address addr  ) 
 

Compare if two mac address is same or not. use memcmp to compare each byte.

Definition at line 239 of file common.cpp.

References getHWAddr(), and macaddr_.

00240 {
00241   if ( memcmp(macaddr_, addr->getHWAddr(), MAC_ADDR_LENGTH*sizeof(unsigned char))  == 0 )
00242            return true;
00243   return false;
00244 
00245 }

bool Address::isSet  )  [inline]
 

Check if an address has already been set or remain uninitialized

Definition at line 151 of file common.h.

References hostname_, and port_.

Referenced by ReceivingPort::init(), and SendingPort::init().

00151 { return (hostname_[0] != '\0' && port_ >= 0); }

void Address::setHostname const char *  hostname  )  [inline]
 

set the hostname

use strcpy function to duplicate a string

Definition at line 162 of file common.h.

References hostname_.

Referenced by Address(), Port::decodeSockAddress(), Port::setHostname(), and Port::setRemoteHostname().

00162                                                 { 
00163     if (hostname == NULL) hostname_[0] = '\0'; else strcpy(hostname_, hostname); }

void Address::setHWAddr unsigned char *  hwaddr  ) 
 

copy mac address

Definition at line 230 of file common.cpp.

References macaddr_.

Referenced by clone().

00231 {
00232   memcpy(macaddr_, hwaddr , MAC_ADDR_LENGTH*sizeof(unsigned char));
00233 }

void Address::setHWAddrFromColonFormat const char *  colonmac  ) 
 

Function to convert the input MAC address string to bytes. First, check the MAC address is valid

  • there are at least 12 Hex characters
  • there are no other charcter except colon

Definition at line 161 of file common.cpp.

References macaddr_.

00162 {  
00163   char HexChar;
00164   //First verify the address
00165   int Count  = 0;
00166   int num_mac_char = 0;
00167   /* Two ASCII characters per byte of binary data */
00168   bool error_end = false;
00169   while (!error_end)
00170     { /* Scan string for first non-hex character.  Also stop scanning at end
00171          of string (HexChar == 0), or when out of six binary storage space */
00172       HexChar = (char)colonmac[Count++];
00173       if (HexChar == ':') continue;     
00174       if (HexChar > 0x39) HexChar = HexChar | 0x20;  /* Convert upper case to lower */
00175       if ( (HexChar == 0x00) || num_mac_char  >= (MAC_ADDR_LENGTH * 2) ||
00176            (!(((HexChar >= 0x30) && (HexChar <= 0x39))||  /* 0 - 9 */
00177              ((HexChar >= 0x61) && (HexChar <= 0x66))) ) ) /* a - f */ 
00178         {
00179           error_end = true;
00180         } else 
00181             num_mac_char++;
00182     }
00183   if (num_mac_char != MAC_ADDR_LENGTH * 2 )
00184     throw "Given Wrong MAC address Format.";
00185 
00186   // Conversion
00187   unsigned char HexValue = 0x00;
00188   Count = 0;
00189   num_mac_char = 0;
00190   int mac_byte_num = 0;
00191   while (mac_byte_num < MAC_ADDR_LENGTH )
00192     {
00193       HexChar = (char)colonmac[Count++];
00194       if (HexChar == ':') continue;
00195       num_mac_char++;  // locate a HEX character
00196       if (HexChar > 0x39)
00197         HexChar = HexChar | 0x20;  /* Convert upper case to lower */
00198       HexChar -= 0x30;
00199       if (HexChar > 0x09)  /* HexChar is "a" - "f" */
00200         HexChar -= 0x27;
00201       HexValue = (HexValue << 4) | HexChar;
00202       if (num_mac_char % 2  == 0 ) /* If we've converted two ASCII chars... */
00203         {
00204           macaddr_[mac_byte_num] = HexValue;
00205           HexValue = 0x0;
00206           mac_byte_num++;
00207         }
00208     }  
00209   return;   
00210 }

void Address::setPort const short  port  )  [inline]
 

set the port # of the Address

Definition at line 155 of file common.h.

References port_.

Referenced by Address(), Port::decodeSockAddress(), Port::setPort(), and Port::setRemotePort().

00155 { port_ = port; }


The documentation for this class was generated from the following files:
Generated on Fri Feb 9 12:49:37 2007 for Common_Classes_Projects_ECE544 by  doxygen 1.4.6