00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef WIFI_ADDR_HXX_
00023 # define WIFI_ADDR_HXX_
00024
00025 # include <cstring>
00026 # include <cassert>
00027 # include <sstream>
00028 # include <iomanip>
00029 # include <stdexcept>
00030 # include <sstream>
00031
00032 # include "addr.hh"
00033
00034 namespace wifi
00035 {
00036
00037 inline
00038 addr::addr()
00039 {
00040 memset(addr_, 0, sizeof (addr_));
00041 }
00042
00043 inline
00044 addr::addr(const void* a)
00045 {
00046 memcpy(addr_, a, sizeof (addr_));
00047 }
00048
00049 inline
00050 addr::addr(const std::string& str)
00051 {
00052 std::istringstream is (str);
00053 uint8_t mac[sizeof (addr_)];
00054
00055 is >> std::hex;
00056 for (unsigned i = 0; i < sizeof (addr); ++i)
00057 {
00058 unsigned t;
00059
00060 if (i and is.get() != ':')
00061 throw std::invalid_argument ("string is not a MAC address");
00062 is >> t;
00063 mac[i] = t;
00064 }
00065 memcpy(addr_, mac, sizeof (addr_));
00066 }
00067
00068 inline
00069 bool
00070 addr::operator < (const addr& rhs) const
00071 {
00072 return memcmp(addr_, rhs.addr_, sizeof (addr_)) < 0;
00073 }
00074
00075 inline
00076 bool
00077 addr::operator == (const addr& rhs) const
00078 {
00079 return memcmp(addr_, rhs.addr_, sizeof (addr_)) == 0;
00080 }
00081
00082 inline
00083 bool
00084 addr::operator != (const addr& rhs) const
00085 {
00086 return not operator == (rhs);
00087 }
00088
00089 inline
00090 uint8_t&
00091 addr::operator [] (unsigned i)
00092 {
00093 assert(i < sizeof (addr_));
00094
00095 return addr_[i];
00096 }
00097
00098 inline
00099 const uint8_t&
00100 addr::operator [] (unsigned i) const
00101 {
00102 assert(i < sizeof (addr_));
00103
00104 return addr_[i];
00105 }
00106
00107 inline
00108 void
00109 addr::dump(void *a) const
00110 {
00111 memcpy(a, addr_, sizeof (addr_));
00112 }
00113
00114 inline
00115 const addr&
00116 addr::null()
00117 {
00118 static const addr null;
00119
00120 return null;
00121 }
00122
00123 inline
00124 const addr&
00125 addr::broadcast()
00126 {
00127 static const uint8_t raw[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
00128 static const addr bcast (raw);
00129
00130 return bcast;
00131 }
00132
00133 inline
00134 std::ostream&
00135 operator << (std::ostream& os, const addr& a)
00136 {
00137 os << std::hex << std::setfill ('0');
00138 for (unsigned i = 0; i < sizeof (a) - 1; ++i)
00139 os << std::setw (2) << unsigned (a[i]) << ':';
00140 return os << std::setw (2) << unsigned (a[sizeof (a) - 1])
00141 << std::dec << std::setfill (' ');
00142 }
00143
00144 inline
00145 std::string
00146 make_string(const addr& a)
00147 {
00148 std::ostringstream os;
00149
00150 os << a;
00151 return os.str();
00152 }
00153
00154 }
00155
00156 #endif // ! WIFI_ADDR_HXX_