03f8944dda9c143bc8086a8ba9727ca183504506
[senf.git] / Socket / Protocols / INet / INet6Address.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
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.
10 //
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.
15 //
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.
20
21 /** \file
22     \brief INet6Address non-inline non-template implementation */
23
24 #include "INet6Address.hh"
25 #include "INet6Address.ih"
26
27 // Custom includes
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <netdb.h>
33
34 //#include "INet6Address.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::INet6Address
40
41 prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s,
42                                                            Resolve_t resolve)
43 {
44     struct in6_addr ina;
45     if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
46         return senf::INet6Address::from_data(&ina.s6_addr[0]);
47     int herr (0);
48
49     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
50     // can explicitly ask for IpV4 addresses
51
52 #   ifdef __GLIBC__
53
54     struct hostent entbuf;
55     char buffer[4096];
56     struct hostent * ent (0);
57     ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
58
59 #   else // ! __GLIBC__
60
61 #   ifdef _REENTRANT
62     static boost::mutex mutex;
63     boost::mutex::scoped_lock lock(mutex);
64 #   endif
65     struct hostent * ent (::gethostbyname(s.c_str()));
66     herr = h_errno;
67
68 #   endif // __GLIBC__
69
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]);
74
75     ///\todo Throw better exceptions here ?
76
77     if (resolve == ResolveINet4)
78         try {
79             return from_inet4address(INet4Address::from_string(s));
80         } catch (INet4Address::SyntaxException const & ex) {
81             throw SyntaxException();
82         }
83     else
84         throw SyntaxException();
85 }
86
87 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
88 {
89     ::in6_addr ina;
90     char buffer[5*8];
91     std::copy(addr.begin(),addr.end(),&ina.s6_addr[0]);
92     ::inet_ntop(AF_INET6,&ina,buffer,sizeof(buffer));
93     buffer[5*8] = 0;
94     os << buffer;
95     return os;
96 }
97
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);
102
103 ///////////////////////////////cc.e////////////////////////////////////////
104 #undef prefix_
105 //#include "INet6Address.mpp"
106
107 \f
108 // Local Variables:
109 // mode: c++
110 // fill-column: 100
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"
116 // End: