2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 // Stefan Bund <g0dil@berlios.de>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 \brief INet6Address non-inline non-template implementation */
24 #include "INet6Address.hh"
25 #include "INet6Address.ih"
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 //#include "INet6Address.mpp"
36 ///////////////////////////////cc.p////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////
41 prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s,
45 if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
46 return senf::INet6Address::from_data(&ina.s6_addr[0]);
49 // If available, we use the reentrant GNU variant. This has the additional advantage, that we
50 // can explicitly ask for IpV4 addresses
54 struct hostent entbuf;
56 struct hostent * ent (0);
57 ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
62 static boost::mutex mutex;
63 boost::mutex::scoped_lock lock(mutex);
65 struct hostent * ent (::gethostbyname(s.c_str()));
70 if (ent && ent->h_addrtype == AF_INET6)
71 // We are only interested in the first address ...
72 return senf::INet6Address::from_data(
73 &reinterpret_cast<in6_addr*>(*(ent->h_addr_list))->s6_addr[0]);
75 ///\todo Throw better exceptions here ?
77 if (resolve == ResolveINet4)
79 return from_inet4address(INet4Address::from_string(s));
80 } catch (INet4Address::SyntaxException const & ex) {
81 throw SyntaxException();
84 throw SyntaxException();
87 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
91 std::copy(addr.begin(),addr.end(),&ina.s6_addr[0]);
92 ::inet_ntop(AF_INET6,&ina,buffer,sizeof(buffer));
98 senf::INet6Address const senf::INet6Address::None;
99 senf::INet6Address const senf::INet6Address::Loopback (0u,0u,0u,0u,0u,0u,0u,1u);
100 senf::INet6Address const senf::INet6Address::AllNodes (0xFF02u,0u,0u,0u,0u,0u,0u,1u);
101 senf::INet6Address const senf::INet6Address::AllRouters (0xFF02u,0u,0u,0u,0u,0u,0u,2u);
103 ///////////////////////////////cc.e////////////////////////////////////////
105 //#include "INet6Address.mpp"
111 // comment-column: 40
112 // c-file-style: "senf"
113 // indent-tabs-mode: nil
114 // ispell-local-dictionary: "american"
115 // compile-command: "scons -u test"