2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
22 \brief INet4Address non-inline non-template implementation */
24 #include "INet4Address.hh"
25 //#include "INet4Address.ih"
28 #include <arpa/inet.h>
30 #include <sys/socket.h>
32 #include <boost/thread/mutex.hpp>
35 //#include "INet4Address.mpp"
37 ///////////////////////////////cc.p////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::INet4Address::INet4Address
42 prefix_ senf::INet4Address::INet4Address(address_type value)
44 iref() = htonl(value);
47 prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s)
50 if (::inet_pton(AF_INET,s.c_str(),&ina) > 0)
51 return senf::INet4Address::from_inaddr(ina.s_addr);
54 // If available, we use the reentrant GNU variant. This has the additional advantage, that we
55 // can explicitly ask for IpV4 addresses
59 struct hostent entbuf;
61 struct hostent * ent (0);
62 ::gethostbyname2_r(s.c_str(), AF_INET, &entbuf, buffer, sizeof(buffer), &ent, &herr);
67 static boost::mutex mutex;
68 boost::mutex::scoped_lock lock(mutex);
70 struct hostent * ent (::gethostbyname(s.c_str()));
76 ///\fixme Need to give better exception here
77 throw SyntaxException();
78 if (ent->h_addrtype != AF_INET)
79 throw SyntaxException();
81 // We are only interested in the first address ...
82 return senf::INet4Address::from_inaddr(
83 reinterpret_cast<in_addr*>(*(ent->h_addr_list))->s_addr);
86 prefix_ bool senf::INet4Address::local()
89 address_type l (address());
91 (l & 0xFF000000u) == 0x0A000000u ||
92 (l & 0xFFF00000u) == 0xAC100000u ||
93 (l & 0xFFFF0000u) == 0xA9FE0000u ||
94 (l & 0xFFFF0000u) == 0xC0A80000u;
97 prefix_ bool senf::INet4Address::loopback()
100 return (address() & 0xFF000000u) == 0x7F000000u;
103 prefix_ bool senf::INet4Address::multicast()
106 return (address() & 0xF0000000u) == 0xE0000000u;
109 prefix_ senf::INet4Address::address_type senf::INet4Address::address()
112 return ntohl(iref());
115 senf::INet4Address const senf::INet4Address::None;
116 senf::INet4Address const senf::INet4Address::Loopback (0x7F000001u);
117 senf::INet4Address const senf::INet4Address::Broadcast (0xFFFFFFFFu);
120 ///////////////////////////////////////////////////////////////////////////
123 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & addr)
127 ina.s_addr = addr.inaddr();
128 ::inet_ntop(AF_INET,&ina,buffer,16);
134 ///////////////////////////////cc.e////////////////////////////////////////
136 //#include "INet4Address.mpp"
142 // comment-column: 40
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"