include/wipal/wifi/addr.hxx

00001 /*
00002  * WiPal - A library and a set of tools to manipulate wireless traces.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
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 } // End of namespace wifi.
00155 
00156 #endif // ! WIFI_ADDR_HXX_

Generated on Tue Jan 15 19:32:31 2008 for wipal by  doxygen 1.5.4