00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef _MSC_VER
00020 #pragma warning(disable:4786)
00021 #endif
00022
00023 #include <ctype.h>
00024 #include "MACAddress.h"
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 static int hextobin(const char *hexstr, unsigned char *destbuf, int size)
00036 {
00037 int k=0;
00038 const char *s = hexstr;
00039 for (int pos=0; pos<size; pos++)
00040 {
00041 if (!s || !*s)
00042 {
00043 destbuf[pos] = 0;
00044 }
00045 else
00046 {
00047 while (*s && !isxdigit(*s)) s++;
00048 if (!*s) {destbuf[pos]=0; continue;}
00049 unsigned char d = isdigit(*s) ? (*s-'0') : islower(*s) ? (*s-'a'+10) : (*s-'A'+10);
00050 d = d<<4;
00051 s++;
00052
00053 while (*s && !isxdigit(*s)) s++;
00054 if (!*s) {destbuf[pos]=0; continue;}
00055 d += isdigit(*s) ? (*s-'0') : islower(*s) ? (*s-'a'+10) : (*s-'A'+10);
00056 s++;
00057
00058 destbuf[pos] = d;
00059 k++;
00060 }
00061 }
00062 return k;
00063 }
00064
00065
00066 MACAddress::MACAddress() : MACAddress_Base()
00067 {
00068 address[0]=address[1]=address[2]=address[3]=address[4]=address[5]=0;
00069 }
00070
00071 MACAddress::MACAddress(const char *hexstr) : MACAddress_Base()
00072 {
00073 setAddress(hexstr);
00074 }
00075
00076 MACAddress& MACAddress::operator=(const MACAddress& other)
00077 {
00078 MACAddress_Base::operator=(other);
00079 memcpy(address, other.address, MAC_ADDRESS_BYTES);
00080 return *this;
00081 }
00082
00083 unsigned int MACAddress::getAddressArraySize() const
00084 {
00085 return 6;
00086 }
00087
00088 unsigned char MACAddress::getAddress(unsigned int k) const
00089 {
00090 if (k>=6) throw new cException("Array of size 6 indexed with %d", k);
00091 return address[k];
00092 }
00093
00094 void MACAddress::setAddress(unsigned int k, unsigned char addrbyte)
00095 {
00096 if (k>=6) throw new cException("Array of size 6 indexed with %d", k);
00097 address[k] = addrbyte;
00098 }
00099
00100 void MACAddress::setAddress(const char *hexstr)
00101 {
00102 if (!hexstr)
00103 throw new cException("MACAddress::setAddress(const char *): got null pointer");
00104 if (hextobin(hexstr, address, MAC_ADDRESS_BYTES)!=MAC_ADDRESS_BYTES)
00105 throw new cException("MACAddress::setAddress(const char *): hex string \"%s\" too short, should be 12 hex digits", hexstr);
00106 }
00107
00108 void MACAddress::setAddressBytes(unsigned char *addrbytes)
00109 {
00110 memcpy(address, addrbytes, MAC_ADDRESS_BYTES);
00111 }
00112
00113 void MACAddress::setBroadcast()
00114 {
00115 address[0]=address[1]=address[2]=address[3]=address[4]=address[5]=0xff;
00116 }
00117
00118 bool MACAddress::isBroadcast() const
00119 {
00120 return address[0]==0xff && address[1]==0xff && address[2]==0xff && address[3]==0xff &&
00121 address[4]==0xff && address[5]==0xff;
00122 }
00123
00124 bool MACAddress::isEmpty() const
00125 {
00126 return !(address[0] || address[1] || address[2] || address[3] || address[4] || address[5]);
00127 }
00128
00129 const char *MACAddress::toHexString(char *buf) const
00130 {
00131 char *s = buf;
00132 for (int i=0; i<MAC_ADDRESS_BYTES; i++, s+=3)
00133 sprintf(s,"%2.2X:",address[i]);
00134 *(s-1)='\0';
00135 return buf;
00136 }
00137
00138 bool MACAddress::equals(const MACAddress& other) const
00139 {
00140 return memcmp(address, other.address, MAC_ADDRESS_BYTES)==0;
00141 }
00142
00143 int MACAddress::compareTo(const MACAddress& other) const
00144 {
00145 return memcmp(address, other.address, MAC_ADDRESS_BYTES);
00146 }
00147
00148