Main Page | Class Hierarchy | Class List | File List | Class Members

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)


Detailed Description

An address class.

Address Class is to handle addresses. 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 69 of file common.h.


Member Function Documentation

char * Address::convertHWAddrToColonFormat  ) 
 

Convert HW Address to Colon Seperated format

Definition at line 138 of file common.cpp.

00139 {
00140   char *colonformat =  new char[17];
00141   //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]);
00142   sprintf(colonformat,"%02X:%02X:%02X:%02X:%02X:%02X",
00143           macaddr_[0],macaddr_[1],macaddr_[2],macaddr_[3],macaddr_[4],macaddr_[5]);
00144   // cout << colonformat << endl;
00145   return colonformat;
00146 
00147 }

bool Address::isSame Address addr  )  [inline]
 

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

Definition at line 107 of file common.h.

References getHostname(), and getPort().

00108   {
00109     if ( port_==  addr->getPort()) return false;
00110     if ( strcmp( hostname_, addr->getHostname() ) == 0 ) return true;
00111     return false;
00112   } 

bool Address::isSameMACAddr Address addr  ) 
 

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

Definition at line 161 of file common.cpp.

References getHWAddr().

00162 {
00163   if ( memcmp(macaddr_, addr->getHWAddr(), MAC_ADDR_LENGTH*sizeof(unsigned char))  == 0 )
00164            return true;
00165   return false;
00166 
00167 }

bool Address::isSet  )  [inline]
 

Check if an address has already been set or remain uninitialized

Definition at line 82 of file common.h.

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

00082 { 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 93 of file common.h.

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

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

void Address::setHWAddr unsigned char *  hwaddr  ) 
 

copy mac address

Definition at line 152 of file common.cpp.

Referenced by clone().

00153 {
00154   memcpy(macaddr_, hwaddr , MAC_ADDR_LENGTH*sizeof(unsigned char));
00155 }

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 83 of file common.cpp.

00084 {  
00085   char HexChar;
00086   //First verify the address
00087   int Count  = 0;
00088   int num_mac_char = 0;
00089   /* Two ASCII characters per byte of binary data */
00090   bool error_end = false;
00091   while (!error_end)
00092     { /* Scan string for first non-hex character.  Also stop scanning at end
00093          of string (HexChar == 0), or when out of six binary storage space */
00094       HexChar = (char)colonmac[Count++];
00095       if (HexChar == ':') continue;     
00096       if (HexChar > 0x39) HexChar = HexChar | 0x20;  /* Convert upper case to lower */
00097       if ( (HexChar == 0x00) || num_mac_char  >= (MAC_ADDR_LENGTH * 2) ||
00098            (!(((HexChar >= 0x30) && (HexChar <= 0x39))||  /* 0 - 9 */
00099              ((HexChar >= 0x61) && (HexChar <= 0x66))) ) ) /* a - f */ 
00100         {
00101           error_end = true;
00102         } else 
00103             num_mac_char++;
00104     }
00105   if (num_mac_char != MAC_ADDR_LENGTH * 2 )
00106     throw "Given Wrong MAC address Format.";
00107 
00108   // Conversion
00109   unsigned char HexValue = 0x00;
00110   Count = 0;
00111   num_mac_char = 0;
00112   int mac_byte_num = 0;
00113   while (mac_byte_num < MAC_ADDR_LENGTH )
00114     {
00115       HexChar = (char)colonmac[Count++];
00116       if (HexChar == ':') continue;
00117       num_mac_char++;  // locate a HEX character
00118       if (HexChar > 0x39)
00119         HexChar = HexChar | 0x20;  /* Convert upper case to lower */
00120       HexChar -= 0x30;
00121       if (HexChar > 0x09)  /* HexChar is "a" - "f" */
00122         HexChar -= 0x27;
00123       HexValue = (HexValue << 4) | HexChar;
00124       if (num_mac_char % 2  == 0 ) /* If we've converted two ASCII chars... */
00125         {
00126           macaddr_[mac_byte_num] = HexValue;
00127           HexValue = 0x0;
00128           mac_byte_num++;
00129         }
00130     }  
00131   return;   
00132 }

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

set the port # of the Address

Definition at line 86 of file common.h.

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

00086 { port_ = port; }


The documentation for this class was generated from the following files:
Generated on Thu Jan 26 17:08:01 2006 for Common_Classes_Projects_ECE544 by  doxygen 1.4.2