00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #include "INet6Address.hh"
00027 #include "INet6Address.ih"
00028
00029
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
00041 #define prefix_
00042
00043
00044
00045
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
00060
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
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;
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;
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
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
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205