Socket/Protocols/INet: Implement INet4Address add rename old INet4Address to INet4Soc...
[senf.git] / Socket / Protocols / INet / INet4Address.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 INet4Address non-inline non-template implementation */
23
24 #include "INet4Address.hh"
25 //#include "INet4Address.ih"
26
27 // Custom includes
28 #include <arpa/inet.h>
29 #include <netdb.h>
30 #include <sys/socket.h>
31 #ifdef _REENTRANT
32 #include <boost/thread/mutex.hpp>
33 #endif
34
35 //#include "INet4Address.mpp"
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::INet4Address::INet4Address
41
42 prefix_ senf::INet4Address::INet4Address(address_type value)
43     : addr_(htonl(value))
44 {}
45
46 prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s)
47 {
48     ::in_addr ina;
49     if (::inet_pton(AF_INET,s.c_str(),&ina) > 0)
50         return senf::INet4Address::from_inaddr(ina.s_addr);
51 #   ifdef _REENTRANT
52     static boost::mutex mutex;
53     boost::mutex::scoped_lock lock(mutex);
54 #   endif
55     ::hostent * ent (::gethostbyname(s.c_str()));
56     if (!ent)
57         ///\fixme Need to give better exception here
58         throw SyntaxException(); 
59     if (ent->h_addrtype != AF_INET)
60         throw SyntaxException();    
61     // We are only interested in the first address ...
62     return senf::INet4Address::from_inaddr(
63         reinterpret_cast<in_addr*>(*(ent->h_addr_list))->s_addr);
64 }
65
66 prefix_ bool senf::INet4Address::local()
67     const
68 {
69     address_type l (ntohl(addr_));
70     return 
71         (l & 0xFF000000u) == 0x0A000000u ||
72         (l & 0xFFF00000u) == 0xAC100000u ||
73         (l & 0xFFFF0000u) == 0xA9FE0000u ||
74         (l & 0xFFFF0000u) == 0xC0A80000u;
75 }
76
77 prefix_ bool senf::INet4Address::loopback()
78     const
79 {
80     return (ntohl(addr_) & 0xFF000000u) == 0x7F000000u;
81 }
82
83 prefix_ bool senf::INet4Address::multicast()
84     const
85 {
86     return (ntohl(addr_) & 0xF0000000u) == 0xE0000000u;
87 }
88
89 senf::INet4Address const senf::INet4Address::None;
90 senf::INet4Address const senf::INet4Address::Loopback = senf::INet4Address(0x7F000001u);
91 senf::INet4Address const senf::INet4Address::Broadcast = senf::INet4Address(0xFFFFFFFFu);
92
93
94 ///////////////////////////////////////////////////////////////////////////
95 // namespace members
96
97 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & addr)
98 {
99     ::in_addr ina;
100     char buffer[16];
101     ina.s_addr = addr.raw();
102     ::inet_ntop(AF_INET,&ina,buffer,16);
103     buffer[15] = 0; 
104     os << buffer;
105     return os;
106 }
107
108 ///////////////////////////////cc.e////////////////////////////////////////
109 #undef prefix_
110 //#include "INet4Address.mpp"
111
112 \f
113 // Local Variables:
114 // mode: c++
115 // fill-column: 100
116 // comment-column: 40
117 // c-file-style: "senf"
118 // indent-tabs-mode: nil
119 // ispell-local-dictionary: "american"
120 // compile-command: "scons -u test"
121 // End: