4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief INet4Address non-inline non-template implementation */
26 #include "INet4Address.hh"
27 //#include "INet4Address.ih"
30 #include <arpa/inet.h>
32 #include <sys/socket.h>
33 #include <boost/lexical_cast.hpp>
35 #include <boost/thread/mutex.hpp>
38 //#include "INet4Address.mpp"
40 ///////////////////////////////cc.p////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::INet4Address::INet4Address
45 prefix_ senf::INet4Address::INet4Address(address_type value)
47 iref() = htonl(value);
50 prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s)
53 if (::inet_pton(AF_INET,s.c_str(),&ina) > 0)
54 return senf::INet4Address::from_inaddr(ina.s_addr);
57 // If available, we use the reentrant GNU variant. This has the additional advantage, that we
58 // can explicitly ask for IpV4 addresses
62 struct hostent entbuf;
64 struct hostent * ent (0);
65 ::gethostbyname2_r(s.c_str(), AF_INET, &entbuf, buffer, sizeof(buffer), &ent, &herr);
70 static boost::mutex mutex;
71 boost::mutex::scoped_lock lock(mutex);
73 struct hostent * ent (::gethostbyname(s.c_str()));
79 ///\fixme Need to give better exception here
80 throw SyntaxException();
81 if (ent->h_addrtype != AF_INET)
82 throw SyntaxException();
84 // We are only interested in the first address ...
85 return senf::INet4Address::from_inaddr(
86 reinterpret_cast<in_addr*>(*(ent->h_addr_list))->s_addr);
89 prefix_ bool senf::INet4Address::local()
92 address_type l (address());
94 (l & 0xFF000000u) == 0x0A000000u ||
95 (l & 0xFFF00000u) == 0xAC100000u ||
96 (l & 0xFFFF0000u) == 0xA9FE0000u ||
97 (l & 0xFFFF0000u) == 0xC0A80000u;
100 prefix_ bool senf::INet4Address::loopback()
103 return (address() & 0xFF000000u) == 0x7F000000u;
106 prefix_ bool senf::INet4Address::multicast()
109 return (address() & 0xF0000000u) == 0xE0000000u;
112 prefix_ senf::INet4Address::address_type senf::INet4Address::address()
115 return ntohl(iref());
118 senf::INet4Address const senf::INet4Address::None;
119 senf::INet4Address const senf::INet4Address::Loopback (0x7F000001u);
120 senf::INet4Address const senf::INet4Address::Broadcast (0xFFFFFFFFu);
122 ///////////////////////////////////////////////////////////////////////////
123 // senf::INet4Network
125 prefix_ senf::INet4Network::INet4Network(std::string s)
127 std::string::size_type i (s.find('/'));
128 if (i == std::string::npos)
129 throw INet4Address::SyntaxException();
131 prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
132 } catch (boost::bad_lexical_cast const &) {
133 throw INet4Address::SyntaxException();
135 address_ = INet4Address(INet4Address::from_string(std::string(s, 0, i)).address() & mask());
138 ///////////////////////////////////////////////////////////////////////////
141 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & addr)
145 ina.s_addr = addr.inaddr();
146 ::inet_ntop(AF_INET,&ina,buffer,16);
152 ///////////////////////////////cc.e////////////////////////////////////////
154 //#include "INet4Address.mpp"
160 // comment-column: 40
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -u test"