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