9319489d2b7b911e998c9712b96ea8fc7c437f59
[senf.git] / senf / Socket / Protocols / INet / INet6Address.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief INet6Address non-inline non-template implementation */
25
26 #include "INet6Address.hh"
27 #include "INet6Address.ih"
28
29 // Custom includes
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <netdb.h>
35 #include <boost/lexical_cast.hpp>
36 #include <senf/Socket/Protocols/AddressExceptions.hh>
37 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
38 #include <senf/Socket/Protocols/Raw/EUI64.hh>
39
40 //#include "INet6Address.mpp"
41 #define prefix_
42 ///////////////////////////////cc.p////////////////////////////////////////
43
44 ///////////////////////////////////////////////////////////////////////////
45 // senf::INet6Address
46
47 prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s,
48                                                            Resolve_t resolve)
49 {
50     struct in6_addr ina;
51     if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
52         return senf::INet6Address::from_data(&ina.s6_addr[0]);
53
54     if (s.empty())
55         throw AddressSyntaxException() << ": empty string";
56
57     int herr (0);
58
59     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
60     // can explicitly ask for IPv4 addresses
61
62 #   ifdef __GLIBC__
63
64     struct hostent entbuf;
65     char buffer[4096];
66     struct hostent * ent (0);
67     ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
68
69 #   else // ! __GLIBC__
70
71 #   ifdef _REENTRANT
72     static boost::mutex mutex;
73     boost::mutex::scoped_lock lock(mutex);
74 #   endif
75     struct hostent * ent (::gethostbyname(s.c_str()));
76     herr = h_errno;
77
78 #   endif // __GLIBC__
79
80     if (ent && ent->h_addrtype == AF_INET6)
81         // We are only interested in the first address ...
82         return senf::INet6Address::from_data(
83             &reinterpret_cast<in6_addr*>(*(ent->h_addr_list))->s6_addr[0]);
84
85     if (resolve == ResolveINet4)
86         return from_inet4address(INet4Address::from_string(s));
87     else
88         throw UnknownHostnameException(s);
89 }
90
91 prefix_ in6_addr senf:: INet6Address::toin6_addr() const {
92     ::in6_addr ina;
93     std::copy((*this).begin(), (*this).end(), &ina.s6_addr[0]);
94     return ina;
95 }
96
97 prefix_ senf::INet6Address senf::INet6Address::from_mac(MACAddress const & mac)
98 {
99     INet6Address addr;
100     addr[0] = 0xfe;
101     addr[1] = 0x80;
102     addr[8] = mac[0] ^ 0x2;  // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
103     addr[9] = mac[1];
104     addr[10] = mac[2];
105     addr[11] = 0xff;
106     addr[12] = 0xfe;
107     addr[13] = mac[3];
108     addr[14] = mac[4];
109     addr[15] = mac[5];
110     return addr;
111 }
112
113 prefix_ senf::INet6Address senf::INet6Address::from_eui64(EUI64 const & eui)
114 {
115     INet6Address addr;
116     addr[0] = 0xfe;
117     addr[1] = 0x80;
118     addr[8] = eui[0] ^ 0x2;  // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
119     std::copy(eui.begin()+1, eui.end(), addr.begin()+9);
120     return addr;
121 }
122
123 prefix_ senf::EUI64 senf::INet6Address::id()
124     const
125 {
126     return EUI64::from_data(begin()+8);
127 }
128
129 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
130 {
131     ::in6_addr ina;
132     char buffer[5*8];
133     std::copy(addr.begin(),addr.end(),&ina.s6_addr[0]);
134     ::inet_ntop(AF_INET6,&ina,buffer,sizeof(buffer));
135     buffer[sizeof(buffer)-1] = 0;
136     os << buffer;
137     return os;
138 }
139
140 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Address & addr)
141 {
142     std::string s;
143     if (!(is >> s))
144         return is;
145     try {
146         addr = INet6Address::from_string(s);
147     }
148     catch (AddressException &) {
149         is.setstate(std::ios::failbit);
150     }
151     return is;
152 }
153
154 senf::INet6Address const senf::INet6Address::None;
155 senf::INet6Address const senf::INet6Address::Loopback   (0u,0u,0u,0u,0u,0u,0u,1u);
156 senf::INet6Address const senf::INet6Address::AllNodes   (0xFF02u,0u,0u,0u,0u,0u,0u,1u);
157 senf::INet6Address const senf::INet6Address::AllRouters (0xFF02u,0u,0u,0u,0u,0u,0u,2u);
158
159 ///////////////////////////////////////////////////////////////////////////
160 // senf::INet6Network
161
162 prefix_ senf::INet6Network::INet6Network(std::string const & s)
163 {
164     using boost::lambda::_1;
165     using boost::lambda::_2;
166     std::string::size_type i (s.find('/'));
167     if (i == std::string::npos)
168         throw AddressSyntaxException(s);
169     try {
170         prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
171     } catch (boost::bad_lexical_cast const &) {
172         throw AddressSyntaxException(s);
173     }
174     address_ = INet6Address::from_string(std::string(s, 0, i));
175     detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
176 }
177
178 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Network & addr)
179 {
180     std::string s;
181     if (!(is >> s))
182         return is;
183     try {
184         addr = INet6Network(s);
185     }
186     catch (AddressException &) {
187         is.setstate(std::ios::failbit);
188     }
189     return is;
190 }
191
192 ///////////////////////////////cc.e////////////////////////////////////////
193 #undef prefix_
194 //#include "INet6Address.mpp"
195
196
197 // Local Variables:
198 // mode: c++
199 // fill-column: 100
200 // comment-column: 40
201 // c-file-style: "senf"
202 // indent-tabs-mode: nil
203 // ispell-local-dictionary: "american"
204 // compile-command: "scons -u test"
205 // End: