Socket/Protocols/INet: Completely new implementation of INet6Address
[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 {
43     struct in6_addr ina;
44     if (::inet_pton(AF_INET6,s.c_str(),&ina) > 0)
45         return senf::INet6Address::from_data(&ina.s6_addr[0]);
46     int herr (0);
47
48     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
49     // can explicitly ask for IpV4 addresses
50
51 #   ifdef __GLIBC__
52
53     struct hostent entbuf;
54     char buffer[4096];
55     struct hostent * ent (0);
56     ::gethostbyname2_r(s.c_str(), AF_INET6, &entbuf, buffer, sizeof(buffer), &ent, &herr);
57
58 #   else // ! __GLIBC__
59
60 #   ifdef _REENTRANT
61     static boost::mutex mutex;
62     boost::mutex::scoped_lock lock(mutex);
63 #   endif
64     struct hostent * ent (::gethostbyname(s.c_str()));
65     herr = h_errno;
66
67 #   endif // __GLIBC__
68
69     if (!ent)
70         ///\fixme Need to give better exception here
71         throw SyntaxException(); 
72     if (ent->h_addrtype != AF_INET6)
73         throw SyntaxException();    
74
75     // We are only interested in the first address ...
76     return senf::INet6Address::from_data(
77         &reinterpret_cast<in6_addr*>(*(ent->h_addr_list))->s6_addr[0]);
78 }
79
80 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Address const & addr)
81 {
82     ::in6_addr ina;
83     char buffer[5*8];
84     std::copy(addr.begin(),addr.end(),&ina.s6_addr[0]);
85     ::inet_ntop(AF_INET6,&ina,buffer,sizeof(buffer));
86     buffer[5*8] = 0;
87     os << buffer;
88     return os;
89 }
90
91 senf::INet6Address const senf::INet6Address::None;
92 senf::INet6Address const senf::INet6Address::Loopback   (0u,0u,0u,0u,0u,0u,0u,1u);
93 senf::INet6Address const senf::INet6Address::AllNodes   (0xFF02u,0u,0u,0u,0u,0u,0u,1u);
94 senf::INet6Address const senf::INet6Address::AllRouters (0xFF02u,0u,0u,0u,0u,0u,0u,2u);
95
96 ///////////////////////////////cc.e////////////////////////////////////////
97 #undef prefix_
98 //#include "INet6Address.mpp"
99
100 \f
101 // Local Variables:
102 // mode: c++
103 // fill-column: 100
104 // comment-column: 40
105 // c-file-style: "senf"
106 // indent-tabs-mode: nil
107 // ispell-local-dictionary: "american"
108 // compile-command: "scons -u test"
109 // End: