Socket/Protocols/INet: Change INet4Address to use a boost::array for storage
[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 {
44     iref() = htonl(value);
45 }
46
47 prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s)
48 {
49     struct in_addr ina;
50     if (::inet_pton(AF_INET,s.c_str(),&ina) > 0)
51         return senf::INet4Address::from_inaddr(ina.s_addr);
52     int herr (0);
53
54     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
55     // can explicitly ask for IpV4 addresses
56
57 #   ifdef __GLIBC__
58
59     struct hostent entbuf;
60     char buffer[4096];
61     struct hostent * ent (0);
62     ::gethostbyname2_r(s.c_str(), AF_INET, &entbuf, buffer, sizeof(buffer), &ent, &herr);
63
64 #   else // ! __GLIBC__
65
66 #   ifdef _REENTRANT
67     static boost::mutex mutex;
68     boost::mutex::scoped_lock lock(mutex);
69 #   endif
70     struct hostent * ent (::gethostbyname(s.c_str()));
71     herr = h_errno;
72
73 #   endif // __GLIBC__
74
75     if (!ent)
76         ///\fixme Need to give better exception here
77         throw SyntaxException(); 
78     if (ent->h_addrtype != AF_INET)
79         throw SyntaxException();    
80
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);
84 }
85
86 prefix_ bool senf::INet4Address::local()
87     const
88 {
89     address_type l (address());
90     return 
91         (l & 0xFF000000u) == 0x0A000000u ||
92         (l & 0xFFF00000u) == 0xAC100000u ||
93         (l & 0xFFFF0000u) == 0xA9FE0000u ||
94         (l & 0xFFFF0000u) == 0xC0A80000u;
95 }
96
97 prefix_ bool senf::INet4Address::loopback()
98     const
99 {
100     return (address() & 0xFF000000u) == 0x7F000000u;
101 }
102
103 prefix_ bool senf::INet4Address::multicast()
104     const
105 {
106     return (address() & 0xF0000000u) == 0xE0000000u;
107 }
108
109 prefix_ senf::INet4Address::address_type senf::INet4Address::address()
110     const
111 {
112     return ntohl(iref());
113 }
114
115 senf::INet4Address const senf::INet4Address::None;
116 senf::INet4Address const senf::INet4Address::Loopback = senf::INet4Address(0x7F000001u);
117 senf::INet4Address const senf::INet4Address::Broadcast = senf::INet4Address(0xFFFFFFFFu);
118
119
120 ///////////////////////////////////////////////////////////////////////////
121 // namespace members
122
123 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & addr)
124 {
125     ::in_addr ina;
126     char buffer[16];
127     ina.s_addr = addr.inaddr();
128     ::inet_ntop(AF_INET,&ina,buffer,16);
129     buffer[15] = 0; 
130     os << buffer;
131     return os;
132 }
133
134 ///////////////////////////////cc.e////////////////////////////////////////
135 #undef prefix_
136 //#include "INet4Address.mpp"
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
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"
147 // End: