some documentation updates
[senf.git] / Socket / Protocols / INet / INet6Address.cc
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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
36 //#include "INet6Address.mpp"
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 ///////////////////////////////////////////////////////////////////////////
41 // senf::INet6Address
42
43 prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s,
44                                                            Resolve_t resolve)
45 {
46     struct in6_addr ina;
47     if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
48         return senf::INet6Address::from_data(&ina.s6_addr[0]);
49     int herr (0);
50
51     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
52     // can explicitly ask for IpV4 addresses
53
54 #   ifdef __GLIBC__
55
56     struct hostent entbuf;
57     char buffer[4096];
58     struct hostent * ent (0);
59     ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
60
61 #   else // ! __GLIBC__
62
63 #   ifdef _REENTRANT
64     static boost::mutex mutex;
65     boost::mutex::scoped_lock lock(mutex);
66 #   endif
67     struct hostent * ent (::gethostbyname(s.c_str()));
68     herr = h_errno;
69
70 #   endif // __GLIBC__
71
72     if (ent && ent->h_addrtype == AF_INET6)
73         // We are only interested in the first address ...
74         return senf::INet6Address::from_data(
75             &reinterpret_cast<in6_addr*>(*(ent->h_addr_list))->s6_addr[0]);
76
77     ///\todo Throw better exceptions here ?
78
79     if (resolve == ResolveINet4)
80         try {
81             return from_inet4address(INet4Address::from_string(s));
82         } catch (INet4Address::SyntaxException const & ex) {
83             throw SyntaxException();
84         }
85     else
86         throw SyntaxException();
87 }
88
89 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
90 {
91     ::in6_addr ina;
92     char buffer[5*8];
93     std::copy(addr.begin(),addr.end(),&ina.s6_addr[0]);
94     ::inet_ntop(AF_INET6,&ina,buffer,sizeof(buffer));
95     buffer[5*8] = 0;
96     os << buffer;
97     return os;
98 }
99
100 senf::INet6Address const senf::INet6Address::None;
101 senf::INet6Address const senf::INet6Address::Loopback   (0u,0u,0u,0u,0u,0u,0u,1u);
102 senf::INet6Address const senf::INet6Address::AllNodes   (0xFF02u,0u,0u,0u,0u,0u,0u,1u);
103 senf::INet6Address const senf::INet6Address::AllRouters (0xFF02u,0u,0u,0u,0u,0u,0u,2u);
104
105 ///////////////////////////////cc.e////////////////////////////////////////
106 #undef prefix_
107 //#include "INet6Address.mpp"
108
109 \f
110 // Local Variables:
111 // mode: c++
112 // fill-column: 100
113 // comment-column: 40
114 // c-file-style: "senf"
115 // indent-tabs-mode: nil
116 // ispell-local-dictionary: "american"
117 // compile-command: "scons -u test"
118 // End: