eddc78bb0368d73b1d75bc21ec2a299371e68bc9
[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
38 //#include "INet6Address.mpp"
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::INet6Address
44
45 prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s,
46                                                            Resolve_t resolve)
47 {
48     struct in6_addr ina;
49     if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
50         return senf::INet6Address::from_data(&ina.s6_addr[0]);
51
52     if (s.empty())
53         throw AddressSyntaxException() << ": empty string";
54
55     int herr (0);
56
57     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
58     // can explicitly ask for IPv4 addresses
59
60 #   ifdef __GLIBC__
61
62     struct hostent entbuf;
63     char buffer[4096];
64     struct hostent * ent (0);
65     ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
66
67 #   else // ! __GLIBC__
68
69 #   ifdef _REENTRANT
70     static boost::mutex mutex;
71     boost::mutex::scoped_lock lock(mutex);
72 #   endif
73     struct hostent * ent (::gethostbyname(s.c_str()));
74     herr = h_errno;
75
76 #   endif // __GLIBC__
77
78     if (ent && ent->h_addrtype == AF_INET6)
79         // We are only interested in the first address ...
80         return senf::INet6Address::from_data(
81             &reinterpret_cast<in6_addr*>(*(ent->h_addr_list))->s6_addr[0]);
82
83     if (resolve == ResolveINet4)
84         return from_inet4address(INet4Address::from_string(s));
85     else
86         throw UnknownHostnameException(s);
87 }
88
89 prefix_ in6_addr senf:: INet6Address::toin6_addr() const {
90     ::in6_addr ina;
91     std::copy((*this).begin(), (*this).end(), &ina.s6_addr[0]);
92     return ina;
93 }
94
95 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
96 {
97     ::in6_addr ina;
98     char buffer[5*8];
99     std::copy(addr.begin(),addr.end(),&ina.s6_addr[0]);
100     ::inet_ntop(AF_INET6,&ina,buffer,sizeof(buffer));
101     buffer[sizeof(buffer)-1] = 0;
102     os << buffer;
103     return os;
104 }
105
106 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Address & addr)
107 {
108     std::string s;
109     if (!(is >> s))
110         return is;
111     try {
112         addr = INet6Address::from_string(s);
113     }
114     catch (AddressException &) {
115         is.setstate(std::ios::failbit);
116     }
117     return is;
118 }
119
120 senf::INet6Address const senf::INet6Address::None;
121 senf::INet6Address const senf::INet6Address::Loopback   (0u,0u,0u,0u,0u,0u,0u,1u);
122 senf::INet6Address const senf::INet6Address::AllNodes   (0xFF02u,0u,0u,0u,0u,0u,0u,1u);
123 senf::INet6Address const senf::INet6Address::AllRouters (0xFF02u,0u,0u,0u,0u,0u,0u,2u);
124
125 ///////////////////////////////////////////////////////////////////////////
126 // senf::INet6Network
127
128 prefix_ senf::INet6Network::INet6Network(std::string const & s)
129 {
130     using boost::lambda::_1;
131     using boost::lambda::_2;
132     std::string::size_type i (s.find('/'));
133     if (i == std::string::npos)
134         throw AddressSyntaxException(s);
135     try {
136         prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
137     } catch (boost::bad_lexical_cast const &) {
138         throw AddressSyntaxException(s);
139     }
140     address_ = INet6Address::from_string(std::string(s, 0, i));
141     detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
142 }
143
144 prefix_ std::istream & senf::operator>>(std::istream & is, INet6Network & addr)
145 {
146     std::string s;
147     if (!(is >> s))
148         return is;
149     try {
150         addr = INet6Network(s);
151     }
152     catch (AddressException &) {
153         is.setstate(std::ios::failbit);
154     }
155     return is;
156 }
157
158 ///////////////////////////////cc.e////////////////////////////////////////
159 #undef prefix_
160 //#include "INet6Address.mpp"
161
162
163 // Local Variables:
164 // mode: c++
165 // fill-column: 100
166 // comment-column: 40
167 // c-file-style: "senf"
168 // indent-tabs-mode: nil
169 // ispell-local-dictionary: "american"
170 // compile-command: "scons -u test"
171 // End: