79673dffdaec46ffd42160adb33a3b8d5444928e
[senf.git] / senf / Socket / Protocols / INet / INet4Address.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 INet4Address non-inline non-template implementation */
25
26 #include "INet4Address.hh"
27 //#include "INet4Address.ih"
28
29 // Custom includes
30 #include <arpa/inet.h>
31 #include <netdb.h>
32 #include <sys/socket.h>
33 #include <boost/lexical_cast.hpp>
34 #if defined(_REENTRANT) && !defined(__GLIBC__)
35 #include <boost/thread/mutex.hpp>
36 #endif
37 #include <senf/Socket/Protocols/AddressExceptions.hh>
38
39 //#include "INet4Address.mpp"
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::INet4Address::INet4Address
45
46 prefix_ senf::INet4Address::INet4Address(address_type value)
47 {
48     iref() = htonl(value);
49 }
50
51 prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s)
52 {
53     struct in_addr ina;
54     if (::inet_pton(AF_INET,s.c_str(),&ina) > 0)
55         return INet4Address::from_inaddr(ina.s_addr);
56
57     if  (s.empty())
58         throw AddressSyntaxException() << ": empty string";
59
60     int herr (0);
61
62     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
63     // can explicitly ask for IPv4 addresses
64
65 #   ifdef __GLIBC__
66
67     struct hostent entbuf;
68     char buffer[4096];
69     struct hostent * ent (0);
70     ::gethostbyname2_r(s.c_str(), AF_INET, &entbuf, buffer, sizeof(buffer), &ent, &herr);
71
72 #   else // ! __GLIBC__
73
74 #   ifdef _REENTRANT
75     static boost::mutex mutex;
76     boost::mutex::scoped_lock lock(mutex);
77 #   endif
78     struct hostent * ent (::gethostbyname(s.c_str()));
79     herr = h_errno;
80
81 #   endif // __GLIBC__
82
83     if (!ent)
84         throw UnknownHostnameException(s);
85     if (ent->h_addrtype != AF_INET)
86         throw UnknownHostnameException(s);
87
88     // We are only interested in the first address ...
89     return INet4Address::from_inaddr(
90         reinterpret_cast<in_addr*>(*(ent->h_addr_list))->s_addr);
91 }
92
93 prefix_ bool senf::INet4Address::local()
94     const
95 {
96     address_type l (address());
97     return
98         (l & 0xFF000000u) == 0x0A000000u ||
99         (l & 0xFFF00000u) == 0xAC100000u ||
100         (l & 0xFFFF0000u) == 0xA9FE0000u ||
101         (l & 0xFFFF0000u) == 0xC0A80000u;
102 }
103
104 prefix_ bool senf::INet4Address::loopback()
105     const
106 {
107     return (address() & 0xFF000000u) == 0x7F000000u;
108 }
109
110 prefix_ bool senf::INet4Address::multicast()
111     const
112 {
113     return (address() & 0xF0000000u) == 0xE0000000u;
114 }
115
116 prefix_ senf::INet4Address::address_type senf::INet4Address::address()
117     const
118 {
119     return ntohl(iref());
120 }
121
122 senf::INet4Address const senf::INet4Address::None;
123 senf::INet4Address const senf::INet4Address::Loopback (0x7F000001u);
124 senf::INet4Address const senf::INet4Address::Broadcast (0xFFFFFFFFu);
125
126 //-/////////////////////////////////////////////////////////////////////////////////////////////////
127 // senf::INet4Network
128
129 prefix_ senf::INet4Network::INet4Network(std::string const & s)
130 {
131     std::string::size_type i (s.find('/'));
132     if (i == std::string::npos)
133         throw AddressSyntaxException(s);
134     try {
135         prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
136     } catch (boost::bad_lexical_cast const &) {
137         throw AddressSyntaxException(s);
138     }
139     address_ = INet4Address(INet4Address::from_string(std::string(s, 0, i)).address() & mask());
140 }
141
142 //-/////////////////////////////////////////////////////////////////////////////////////////////////
143 // namespace members
144
145 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & addr)
146 {
147     ::in_addr ina;
148     char buffer[16];
149     ina.s_addr = addr.inaddr();
150     ::inet_ntop(AF_INET,&ina,buffer,16);
151     buffer[15] = 0;
152     os << buffer;
153     return os;
154 }
155
156 prefix_ std::istream & senf::operator>>(std::istream & is, INet4Address & addr)
157 {
158     std::string s;
159     if (!(is >> s))
160         return is;
161     try {
162         addr = INet4Address::from_string(s);
163     }
164     catch (AddressException &) {
165         is.setstate(std::ios::failbit);
166     }
167     return is;
168 }
169
170 prefix_ std::istream & senf::operator>>(std::istream & is, INet4Network & addr)
171 {
172     std::string s;
173     if (!(is >> s))
174         return is;
175     try {
176         addr = INet4Network(s);
177     }
178     catch (AddressException &) {
179         is.setstate(std::ios::failbit);
180     }
181     return is;
182 }
183
184 //-/////////////////////////////////////////////////////////////////////////////////////////////////
185 #undef prefix_
186 //#include "INet4Address.mpp"
187
188 \f
189 // Local Variables:
190 // mode: c++
191 // fill-column: 100
192 // comment-column: 40
193 // c-file-style: "senf"
194 // indent-tabs-mode: nil
195 // ispell-local-dictionary: "american"
196 // compile-command: "scons -u test"
197 // End: