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