MACAddress.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003 Andras Varga; CTIE, Monash University, Australia
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00018 #ifndef MACADDRESS_H_
00019 #define MACADDRESS_H_
00020 
00021 #include <string>
00022 #include <omnetpp.h>
00023 #include "INETDefs.h"
00024 
00025 
00026 #define MAC_ADDRESS_BYTES 6
00027 
00028 class InterfaceToken;
00029 
00030 
00034 class INET_API MACAddress
00035 {
00036   private:
00037     unsigned char address[6];   // 6*8=48 bit address
00038     static unsigned int autoAddressCtr; // global counter for generateAutoAddress()
00039 
00040   public:
00042     static const MACAddress UNSPECIFIED_ADDRESS;
00043 
00045     static const MACAddress BROADCAST_ADDRESS;
00046 
00050     MACAddress();
00051 
00056     MACAddress(const char *hexstr);
00057 
00061     MACAddress(const MACAddress& other) {operator=(other);}
00062 
00066     MACAddress& operator=(const MACAddress& other);
00067 
00071     unsigned int getAddressSize() const;
00072 
00076     unsigned char getAddressByte(unsigned int k) const;
00077 
00081     void setAddressByte(unsigned int k, unsigned char addrbyte);
00082 
00087     bool tryParse(const char *hexstr);
00088 
00093     void setAddress(const char *hexstr);
00094 
00099     unsigned char *getAddressBytes() {return address;}
00100 
00104     void setAddressBytes(unsigned char *addrbytes);
00105 
00109     void setBroadcast();
00110 
00114     bool isBroadcast() const;
00115 
00119     bool isMulticast() const  {return address[0]&0x80;};
00120 
00124     bool isUnspecified() const;
00125 
00129     std::string str() const;
00130 
00134     bool equals(const MACAddress& other) const;
00135 
00139     bool operator==(const MACAddress& other) const {return (*this).equals(other);}
00140 
00144     bool operator!=(const MACAddress& other) const {return !(*this).equals(other);}
00145 
00149     int compareTo(const MACAddress& other) const;
00150 
00155     InterfaceToken formInterfaceIdentifier() const;
00156 
00161     static MACAddress generateAutoAddress();
00162 
00163 };
00164 
00165 inline std::ostream& operator<<(std::ostream& os, const MACAddress& mac)
00166 {
00167     return os << mac.str();
00168 }
00169 
00170 #endif
00171 
00172