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)

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 148 of file common.h.


Member Function Documentation

char * Address::convertHWAddrToColonFormat  ) 
 

Convert HW Address to Colon Seperated format

Definition at line 218 of file common.cpp.

References macaddr_.

00219 {
00220   char *colonformat =  new char[17];
00221   //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]);
00222   sprintf(colonformat,"%02X:%02X:%02X:%02X:%02X:%02X",
00223           macaddr_[0],macaddr_[1],macaddr_[2],macaddr_[3],macaddr_[4],macaddr_[5]);
00224   // cout << colonformat << endl;
00225   return colonformat;
00226 
00227 }

bool Address::isSame Address addr  )  [inline]
 

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

Definition at line 186 of file common.h.

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

00187   {
00188     if ( port_==  addr->getPort()) return false;
00189     if ( strcmp( hostname_, addr->getHostname() ) == 0 ) return true;
00190     return false;
00191   } 

bool Address::isSameMACAddr Address addr  ) 
 

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

Definition at line 241 of file common.cpp.

References getHWAddr(), and macaddr_.

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

bool Address::isSet  )  [inline]
 

Check if an address has already been set or remain uninitialized

Definition at line 161 of file common.h.

References hostname_, and port_.

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

00161 { 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 172 of file common.h.

References hostname_.

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

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

void Address::setHWAddr unsigned char *  hwaddr  ) 
 

copy mac address

Definition at line 232 of file common.cpp.

References macaddr_.

Referenced by clone().

00233 {
00234   memcpy(macaddr_, hwaddr , MAC_ADDR_LENGTH*sizeof(unsigned char));
00235 }

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

References macaddr_.

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

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

set the port # of the Address

Definition at line 165 of file common.h.

References port_.

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

00165 { port_ = port; }


The documentation for this class was generated from the following files:
Generated on Fri Feb 17 17:55:49 2006 for Common_Classes_Projects_ECE544 by  doxygen 1.4.2