4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief INet6Address non-inline non-template implementation */
31 #include "INet6Address.hh"
32 #include "INet6Address.ih"
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
40 #include <boost/lexical_cast.hpp>
41 #include <senf/Socket/Protocols/AddressExceptions.hh>
42 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
43 #include <senf/Socket/Protocols/Raw/EUI64.hh>
45 //#include "INet6Address.mpp"
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
49 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s,
56 if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
57 return INet6Address::from_data(&ina.s6_addr[0]);
60 throw AddressSyntaxException() << ": empty string";
64 // If available, we use the reentrant GNU variant. This has the additional advantage, that we
65 // can explicitly ask for IPv4 addresses
69 struct hostent entbuf;
71 struct hostent * ent (0);
72 ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
77 static boost::mutex mutex;
78 boost::mutex::scoped_lock lock(mutex);
80 struct hostent * ent (::gethostbyname(s.c_str()));
85 if (ent && ent->h_addrtype == AF_INET6)
86 // We are only interested in the first address ...
87 return INet6Address::from_data(
88 &reinterpret_cast<in6_addr*>(*(ent->h_addr_list))->s6_addr[0]);
90 if (resolve == ResolveINet4)
91 return from_inet4address(INet4Address::from_string(s));
93 throw UnknownHostnameException(s);
96 prefix_ in6_addr senf:: INet6Address::toin6_addr() const {
98 std::copy((*this).begin(), (*this).end(), &ina.s6_addr[0]);
102 prefix_ senf::INet6Address senf::INet6Address::from_mac(MACAddress const & mac)
107 addr[8] = mac[0] ^ 0x2; // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
118 prefix_ senf::INet6Address senf::INet6Address::from_eui64(EUI64 const & eui)
123 addr[8] = eui[0] ^ 0x2; // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
124 std::copy(eui.begin()+1, eui.end(), addr.begin()+9);
128 prefix_ senf::EUI64 senf::INet6Address::id()
131 return EUI64::from_data(begin()+8);
134 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
138 std::copy(addr.begin(),addr.end(),&ina.s6_addr[0]);
139 ::inet_ntop(AF_INET6,&ina,buffer,sizeof(buffer));
140 buffer[sizeof(buffer)-1] = 0;
145 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Address & addr)
151 addr = INet6Address::from_string(s);
153 catch (AddressException &) {
154 is.setstate(std::ios::failbit);
159 senf::INet6Address const senf::INet6Address::None;
160 senf::INet6Address const senf::INet6Address::Loopback (0u,0u,0u,0u,0u,0u,0u,1u);
161 senf::INet6Address const senf::INet6Address::AllNodes (0xFF02u,0u,0u,0u,0u,0u,0u,1u);
162 senf::INet6Address const senf::INet6Address::AllRouters (0xFF02u,0u,0u,0u,0u,0u,0u,2u);
164 //-/////////////////////////////////////////////////////////////////////////////////////////////////
165 // senf::INet6Network
167 prefix_ senf::INet6Network::INet6Network(std::string const & s)
169 using boost::lambda::_1;
170 using boost::lambda::_2;
171 std::string::size_type i (s.find('/'));
172 if (i == std::string::npos)
173 throw AddressSyntaxException(s);
175 prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
176 } catch (boost::bad_lexical_cast const &) {
177 throw AddressSyntaxException(s);
179 address_ = INet6Address::from_string(std::string(s, 0, i));
180 detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
183 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Network & addr)
189 addr = INet6Network(s);
191 catch (AddressException &) {
192 is.setstate(std::ios::failbit);
197 //-/////////////////////////////////////////////////////////////////////////////////////////////////
199 //#include "INet6Address.mpp"
205 // comment-column: 40
206 // c-file-style: "senf"
207 // indent-tabs-mode: nil
208 // ispell-local-dictionary: "american"
209 // compile-command: "scons -u test"