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
00054 read(is);
00055 if (not is)
00056 throw std::invalid_argument ("string is not a MAC address");
00057 }
00058
00059 inline
00060 addr::addr(std::istream& is)
00061 {
00062 read(is);
00063 if (not is)
00064 throw std::invalid_argument ("string is not a MAC address");
00065 }
00066
00067 inline
00068 std::istream&
00069 addr::read(std::istream& is)
00070 {
00071 is >> std::hex;
00072 for (unsigned i = 0; i < sizeof (addr); ++i)
00073 {
00074 unsigned t;
00075
00076 if (i and is.get() != ':')
00077 {
00078 is.setstate(is.rdstate() | std::istream::failbit);
00079 return is;
00080 }
00081 if (not (is >> t))
00082 return is;
00083 addr_[i] = t;
00084 }
00085 return is;
00086 }
00087
00088 inline
00089 bool
00090 addr::operator < (const addr& rhs) const
00091 {
00092 return memcmp(addr_, rhs.addr_, sizeof (addr_)) < 0;
00093 }
00094
00095 inline
00096 bool
00097 addr::operator == (const addr& rhs) const
00098 {
00099 return memcmp(addr_, rhs.addr_, sizeof (addr_)) == 0;
00100 }
00101
00102 inline
00103 bool
00104 addr::operator != (const addr& rhs) const
00105 {
00106 return not operator == (rhs);
00107 }
00108
00109 inline
00110 uint8_t&
00111 addr::operator [] (unsigned i)
00112 {
00113 assert(i < sizeof (addr_));
00114
00115 return addr_[i];
00116 }
00117
00118 inline
00119 const uint8_t&
00120 addr::operator [] (unsigned i) const
00121 {
00122 assert(i < sizeof (addr_));
00123
00124 return addr_[i];
00125 }
00126
00127 inline
00128 void
00129 addr::dump(void *a) const
00130 {
00131 memcpy(a, addr_, sizeof (addr_));
00132 }
00133
00134 inline
00135 const addr&
00136 addr::null()
00137 {
00138 static const addr null;
00139
00140 return null;
00141 }
00142
00143 inline
00144 const addr&
00145 addr::broadcast()
00146 {
00147 static const uint8_t raw[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
00148 static const addr bcast (raw);
00149
00150 return bcast;
00151 }
00152
00153 inline
00154 std::ostream&
00155 operator << (std::ostream& os, const addr& a)
00156 {
00157 os << std::hex << std::setfill ('0');
00158 for (unsigned i = 0; i < sizeof (a) - 1; ++i)
00159 os << std::setw (2) << unsigned (a[i]) << ':';
00160 return os << std::setw (2) << unsigned (a[sizeof (a) - 1])
00161 << std::dec << std::setfill (' ');
00162 }
00163
00164 inline
00165 std::istream&
00166 operator >> (std::istream& is, addr& a)
00167 {
00168 return a.read(is);
00169 }
00170
00171 inline
00172 std::string
00173 make_string(const addr& a)
00174 {
00175 std::ostringstream os;
00176
00177 os << a;
00178 return os.str();
00179 }
00180
00181 }
00182
00183 #endif // ! WIFI_ADDR_HXX_