INet6Address.cc

Go to the documentation of this file.
00001 // $Id: INet6Address.cc 1772 2011-03-10 12:45:21Z tho $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "INet6Address.hh"
00027 #include "INet6Address.ih"
00028 
00029 // Custom includes
00030 #include <sys/types.h>
00031 #include <sys/socket.h>
00032 #include <netinet/in.h>
00033 #include <arpa/inet.h>
00034 #include <netdb.h>
00035 #include <boost/lexical_cast.hpp>
00036 #include <senf/Socket/Protocols/AddressExceptions.hh>
00037 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
00038 #include <senf/Socket/Protocols/Raw/EUI64.hh>
00039 
00040 //#include "INet6Address.mpp"
00041 #define prefix_
00042 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00043 
00044 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00045 // senf::INet6Address
00046 
00047 prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s,
00048                                                            Resolve_t resolve)
00049 {
00050     struct in6_addr ina;
00051     if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
00052         return INet6Address::from_data(&ina.s6_addr[0]);
00053 
00054     if (s.empty())
00055         throw AddressSyntaxException() << ": empty string";
00056 
00057     int herr (0);
00058 
00059     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
00060     // can explicitly ask for IPv4 addresses
00061 
00062 #   ifdef __GLIBC__
00063 
00064     struct hostent entbuf;
00065     char buffer[4096];
00066     struct hostent * ent (0);
00067     ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
00068 
00069 #   else // ! __GLIBC__
00070 
00071 #   ifdef _REENTRANT
00072     static boost::mutex mutex;
00073     boost::mutex::scoped_lock lock(mutex);
00074 #   endif
00075     struct hostent * ent (::gethostbyname(s.c_str()));
00076     herr = h_errno;
00077 
00078 #   endif // __GLIBC__
00079 
00080     if (ent && ent->h_addrtype == AF_INET6)
00081         // We are only interested in the first address ...
00082         return INet6Address::from_data(
00083             &reinterpret_cast<in6_addr*>(*(ent->h_addr_list))->s6_addr[0]);
00084 
00085     if (resolve == ResolveINet4)
00086         return from_inet4address(INet4Address::from_string(s));
00087     else
00088         throw UnknownHostnameException(s);
00089 }
00090 
00091 prefix_ in6_addr senf:: INet6Address::toin6_addr() const {
00092     ::in6_addr ina;
00093     std::copy((*this).begin(), (*this).end(), &ina.s6_addr[0]);
00094     return ina;
00095 }
00096 
00097 prefix_ senf::INet6Address senf::INet6Address::from_mac(MACAddress const & mac)
00098 {
00099     INet6Address addr;
00100     addr[0] = 0xfe;
00101     addr[1] = 0x80;
00102     addr[8] = mac[0] ^ 0x2;  // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
00103     addr[9] = mac[1];
00104     addr[10] = mac[2];
00105     addr[11] = 0xff;
00106     addr[12] = 0xfe;
00107     addr[13] = mac[3];
00108     addr[14] = mac[4];
00109     addr[15] = mac[5];
00110     return addr;
00111 }
00112 
00113 prefix_ senf::INet6Address senf::INet6Address::from_eui64(EUI64 const & eui)
00114 {
00115     INet6Address addr;
00116     addr[0] = 0xfe;
00117     addr[1] = 0x80;
00118     addr[8] = eui[0] ^ 0x2;  // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
00119     std::copy(eui.begin()+1, eui.end(), addr.begin()+9);
00120     return addr;
00121 }
00122 
00123 prefix_ senf::EUI64 senf::INet6Address::id()
00124     const
00125 {
00126     return EUI64::from_data(begin()+8);
00127 }
00128 
00129 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
00130 {
00131     ::in6_addr ina;
00132     char buffer[5*8];
00133     std::copy(addr.begin(),addr.end(),&ina.s6_addr[0]);
00134     ::inet_ntop(AF_INET6,&ina,buffer,sizeof(buffer));
00135     buffer[sizeof(buffer)-1] = 0;
00136     os << buffer;
00137     return os;
00138 }
00139 
00140 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Address & addr)
00141 {
00142     std::string s;
00143     if (!(is >> s))
00144         return is;
00145     try {
00146         addr = INet6Address::from_string(s);
00147     }
00148     catch (AddressException &) {
00149         is.setstate(std::ios::failbit);
00150     }
00151     return is;
00152 }
00153 
00154 senf::INet6Address const senf::INet6Address::None;
00155 senf::INet6Address const senf::INet6Address::Loopback   (0u,0u,0u,0u,0u,0u,0u,1u);
00156 senf::INet6Address const senf::INet6Address::AllNodes   (0xFF02u,0u,0u,0u,0u,0u,0u,1u);
00157 senf::INet6Address const senf::INet6Address::AllRouters (0xFF02u,0u,0u,0u,0u,0u,0u,2u);
00158 
00159 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00160 // senf::INet6Network
00161 
00162 prefix_ senf::INet6Network::INet6Network(std::string const & s)
00163 {
00164     using boost::lambda::_1;
00165     using boost::lambda::_2;
00166     std::string::size_type i (s.find('/'));
00167     if (i == std::string::npos)
00168         throw AddressSyntaxException(s);
00169     try {
00170         prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
00171     } catch (boost::bad_lexical_cast const &) {
00172         throw AddressSyntaxException(s);
00173     }
00174     address_ = INet6Address::from_string(std::string(s, 0, i));
00175     detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
00176 }
00177 
00178 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Network & addr)
00179 {
00180     std::string s;
00181     if (!(is >> s))
00182         return is;
00183     try {
00184         addr = INet6Network(s);
00185     }
00186     catch (AddressException &) {
00187         is.setstate(std::ios::failbit);
00188     }
00189     return is;
00190 }
00191 
00192 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00193 #undef prefix_
00194 //#include "INet6Address.mpp"
00195 
00196 
00197 // Local Variables:
00198 // mode: c++
00199 // fill-column: 100
00200 // comment-column: 40
00201 // c-file-style: "senf"
00202 // indent-tabs-mode: nil
00203 // ispell-local-dictionary: "american"
00204 // compile-command: "scons -u test"
00205 // End: