some documentation updates
[senf.git] / Socket / Protocols / INet / INet4Address.cc
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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 #ifdef _REENTRANT
34 #include <boost/thread/mutex.hpp>
35 #endif
36
37 //#include "INet4Address.mpp"
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 ///////////////////////////////////////////////////////////////////////////
42 // senf::INet4Address::INet4Address
43
44 prefix_ senf::INet4Address::INet4Address(address_type value)
45 {
46     iref() = htonl(value);
47 }
48
49 prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s)
50 {
51     struct in_addr ina;
52     if (::inet_pton(AF_INET,s.c_str(),&ina) > 0)
53         return senf::INet4Address::from_inaddr(ina.s_addr);
54     int herr (0);
55
56     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
57     // can explicitly ask for IpV4 addresses
58
59 #   ifdef __GLIBC__
60
61     struct hostent entbuf;
62     char buffer[4096];
63     struct hostent * ent (0);
64     ::gethostbyname2_r(s.c_str(), AF_INET, &entbuf, buffer, sizeof(buffer), &ent, &herr);
65
66 #   else // ! __GLIBC__
67
68 #   ifdef _REENTRANT
69     static boost::mutex mutex;
70     boost::mutex::scoped_lock lock(mutex);
71 #   endif
72     struct hostent * ent (::gethostbyname(s.c_str()));
73     herr = h_errno;
74
75 #   endif // __GLIBC__
76
77     if (!ent)
78         ///\fixme Need to give better exception here
79         throw SyntaxException(); 
80     if (ent->h_addrtype != AF_INET)
81         throw SyntaxException();    
82
83     // We are only interested in the first address ...
84     return senf::INet4Address::from_inaddr(
85         reinterpret_cast<in_addr*>(*(ent->h_addr_list))->s_addr);
86 }
87
88 prefix_ bool senf::INet4Address::local()
89     const
90 {
91     address_type l (address());
92     return 
93         (l & 0xFF000000u) == 0x0A000000u ||
94         (l & 0xFFF00000u) == 0xAC100000u ||
95         (l & 0xFFFF0000u) == 0xA9FE0000u ||
96         (l & 0xFFFF0000u) == 0xC0A80000u;
97 }
98
99 prefix_ bool senf::INet4Address::loopback()
100     const
101 {
102     return (address() & 0xFF000000u) == 0x7F000000u;
103 }
104
105 prefix_ bool senf::INet4Address::multicast()
106     const
107 {
108     return (address() & 0xF0000000u) == 0xE0000000u;
109 }
110
111 prefix_ senf::INet4Address::address_type senf::INet4Address::address()
112     const
113 {
114     return ntohl(iref());
115 }
116
117 senf::INet4Address const senf::INet4Address::None;
118 senf::INet4Address const senf::INet4Address::Loopback (0x7F000001u);
119 senf::INet4Address const senf::INet4Address::Broadcast (0xFFFFFFFFu);
120
121
122 ///////////////////////////////////////////////////////////////////////////
123 // namespace members
124
125 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & addr)
126 {
127     ::in_addr ina;
128     char buffer[16];
129     ina.s_addr = addr.inaddr();
130     ::inet_ntop(AF_INET,&ina,buffer,16);
131     buffer[15] = 0; 
132     os << buffer;
133     return os;
134 }
135
136 ///////////////////////////////cc.e////////////////////////////////////////
137 #undef prefix_
138 //#include "INet4Address.mpp"
139
140 \f
141 // Local Variables:
142 // mode: c++
143 // fill-column: 100
144 // comment-column: 40
145 // c-file-style: "senf"
146 // indent-tabs-mode: nil
147 // ispell-local-dictionary: "american"
148 // compile-command: "scons -u test"
149 // End: