switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / INet / INet6Address.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief INet6Address non-inline non-template implementation */
30
31 #include "INet6Address.hh"
32 #include "INet6Address.ih"
33
34 // Custom includes
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <netdb.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>
44
45 //#include "INet6Address.mpp"
46 #define prefix_
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48
49 //-/////////////////////////////////////////////////////////////////////////////////////////////////
50 // senf::INet6Address
51
52 prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s,
53                                                            Resolve_t resolve)
54 {
55     struct in6_addr ina;
56     if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
57         return INet6Address::from_data(&ina.s6_addr[0]);
58
59     if (s.empty())
60         throw AddressSyntaxException() << ": empty string";
61
62     int herr (0);
63
64     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
65     // can explicitly ask for IPv4 addresses
66
67 #   ifdef __GLIBC__
68
69     struct hostent entbuf;
70     char buffer[4096];
71     struct hostent * ent (0);
72     ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
73
74 #   else // ! __GLIBC__
75
76 #   ifdef _REENTRANT
77     static boost::mutex mutex;
78     boost::mutex::scoped_lock lock(mutex);
79 #   endif
80     struct hostent * ent (::gethostbyname(s.c_str()));
81     herr = h_errno;
82
83 #   endif // __GLIBC__
84
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]);
89
90     if (resolve == ResolveINet4)
91         return from_inet4address(INet4Address::from_string(s));
92     else
93         throw UnknownHostnameException(s);
94 }
95
96 prefix_ in6_addr senf:: INet6Address::toin6_addr() const {
97     ::in6_addr ina;
98     std::copy((*this).begin(), (*this).end(), &ina.s6_addr[0]);
99     return ina;
100 }
101
102 prefix_ senf::INet6Address senf::INet6Address::from_mac(MACAddress const & mac)
103 {
104     INet6Address addr;
105     addr[0] = 0xfe;
106     addr[1] = 0x80;
107     addr[8] = mac[0] ^ 0x2;  // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
108     addr[9] = mac[1];
109     addr[10] = mac[2];
110     addr[11] = 0xff;
111     addr[12] = 0xfe;
112     addr[13] = mac[3];
113     addr[14] = mac[4];
114     addr[15] = mac[5];
115     return addr;
116 }
117
118 prefix_ senf::INet6Address senf::INet6Address::from_eui64(EUI64 const & eui)
119 {
120     INet6Address addr;
121     addr[0] = 0xfe;
122     addr[1] = 0x80;
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);
125     return addr;
126 }
127
128 prefix_ senf::EUI64 senf::INet6Address::id()
129     const
130 {
131     return EUI64::from_data(begin()+8);
132 }
133
134 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
135 {
136     ::in6_addr ina;
137     char buffer[5*8];
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;
141     os << buffer;
142     return os;
143 }
144
145 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Address & addr)
146 {
147     std::string s;
148     if (!(is >> s))
149         return is;
150     try {
151         addr = INet6Address::from_string(s);
152     }
153     catch (AddressException &) {
154         is.setstate(std::ios::failbit);
155     }
156     return is;
157 }
158
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);
163
164 //-/////////////////////////////////////////////////////////////////////////////////////////////////
165 // senf::INet6Network
166
167 prefix_ senf::INet6Network::INet6Network(std::string const & s)
168 {
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);
174     try {
175         prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
176     } catch (boost::bad_lexical_cast const &) {
177         throw AddressSyntaxException(s);
178     }
179     address_ = INet6Address::from_string(std::string(s, 0, i));
180     detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
181 }
182
183 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Network & addr)
184 {
185     std::string s;
186     if (!(is >> s))
187         return is;
188     try {
189         addr = INet6Network(s);
190     }
191     catch (AddressException &) {
192         is.setstate(std::ios::failbit);
193     }
194     return is;
195 }
196
197 //-/////////////////////////////////////////////////////////////////////////////////////////////////
198 #undef prefix_
199 //#include "INet6Address.mpp"
200
201
202 // Local Variables:
203 // mode: c++
204 // fill-column: 100
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"
210 // End: