4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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 INet[46]Address and INet[46]AddressingPolicy non-inline non-template implementation
27 #include "INetAddressing.hh"
28 //#include "INetAddressing.ih"
33 #include <sys/socket.h>
35 #include <boost/lexical_cast.hpp>
36 #include <boost/tokenizer.hpp>
37 #include <boost/range.hpp>
39 //#include "INetAddressing.mpp"
41 ///////////////////////////////cc.p////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////
46 prefix_ senf::INet4Address::INet4Address(std::string host, unsigned port)
49 /** \todo gethostbyname support */
50 if (::inet_aton(host.c_str(), &addr_.sin_addr) == 0)
51 throw InvalidINetAddressException();
52 addr_.sin_port = htons(port);
55 prefix_ std::string senf::INet4Address::str()
59 s << host() << ':' << port();
63 prefix_ void senf::INet4Address::clear()
65 ::memset(&addr_,0,sizeof(addr_));
66 addr_.sin_family = AF_INET;
69 prefix_ void senf::INet4Address::assignString(std::string address)
72 unsigned i = address.find(':');
73 if (i == std::string::npos)
74 throw InvalidINetAddressException();
75 // The temporary string in the next expr is guaranteed to live
76 // until end-of-statement
77 if (::inet_aton(std::string(address,0,i).c_str(), &addr_.sin_addr) == 0)
78 throw InvalidINetAddressException();
80 // Replace lexical_cast with strtoul ?
81 addr_.sin_port = htons(boost::lexical_cast< ::u_int16_t >(std::string(address,i+1)));
83 catch (boost::bad_lexical_cast const &) {
84 throw InvalidINetAddressException();
88 ///////////////////////////////////////////////////////////////////////////
91 prefix_ senf::INet6Address::INet6Address(std::string const & addr)
93 if (inet_pton(AF_INET6,addr.c_str(),&addr_) <= 0)
94 throw InvalidINetAddressException();
97 prefix_ senf::INet6Address::INet6Address(char const * addr)
99 if (inet_pton(AF_INET6,addr,&addr_) <= 0)
100 throw InvalidINetAddressException();
103 prefix_ void senf::INet6Address::clear()
105 ::memset(&addr_,0,sizeof(addr_));
108 prefix_ std::string senf::INet6Address::address()
112 BOOST_ASSERT( inet_ntop(AF_INET6, &addr_, buffer, sizeof(buffer)) );
113 return std::string(buffer);
116 prefix_ bool senf::INet6Address::operator==(INet6Address const & other)
119 return ::memcmp(&addr_,&other.addr_,sizeof(addr_))==0;
122 prefix_ bool senf::INet6Address::operator!=(INet6Address const & other)
125 return !operator==(other);
128 ///////////////////////////////////////////////////////////////////////////
129 // senf::INet6SocketAddress
131 prefix_ bool senf::INet6SocketAddress::operator==(INet6SocketAddress const & other)
134 return ::memcmp(&sockaddr_.sin6_addr, &other.sockaddr_.sin6_addr, sizeof(sockaddr_.sin6_addr))==0 &&
135 sockaddr_.sin6_port == other.sockaddr_.sin6_port &&
136 sockaddr_.sin6_scope_id == other.sockaddr_.sin6_scope_id;
139 prefix_ bool senf::INet6SocketAddress::operator!=(INet6SocketAddress const & other)
142 return ! operator==(other);
145 prefix_ void senf::INet6SocketAddress::clear()
147 ::memset(&sockaddr_,0,sizeof(sockaddr_));
148 sockaddr_.sin6_family = AF_INET6;
151 prefix_ std::string senf::INet6SocketAddress::address()
154 std::stringstream ss;
156 if (sockaddr_.sin6_scope_id != 0)
162 prefix_ std::string senf::INet6SocketAddress::iface()
165 if (sockaddr_.sin6_scope_id == 0)
167 char buffer[IFNAMSIZ];
168 BOOST_ASSERT( if_indextoname(sockaddr_.sin6_scope_id,buffer) );
169 return std::string(buffer);
172 prefix_ void senf::INet6SocketAddress::assignAddr(std::string const & addr)
174 // Format of addr: "[" address [ "@" interface ] "]" ":" port
175 typedef boost::char_separator<char> separator;
176 typedef boost::tokenizer<separator> tokenizer;
177 // we don't add ':' to the list of separators since that would give as the IPv6 address
178 // as a list of tokens. We just strip the : from the port number manually
179 separator sep ("", "@[]");
180 tokenizer tokens (addr, sep);
181 tokenizer::iterator token (tokens.begin());
182 if (token == tokens.end()
184 || ++token == tokens.end()
185 || inet_pton(AF_INET6, std::string(boost::begin(*token),boost::end(*token)).c_str(),
186 &sockaddr_.sin6_addr) <= 0
187 || ++token == tokens.end())
188 throw InvalidINetAddressException();
190 if (++token == tokens.end())
191 throw InvalidINetAddressException();
192 assignIface(std::string(boost::begin(*token),boost::end(*token)));
193 if (++token == tokens.end()
195 throw InvalidINetAddressException();
196 } else if (*token != "]")
197 throw InvalidINetAddressException();
198 if (++token == tokens.end()
199 || *boost::begin(*token) != ':')
200 throw InvalidINetAddressException();
202 sockaddr_.sin6_port = htons(
203 boost::lexical_cast<unsigned>(std::string(boost::next(boost::begin(*token)),
204 boost::end(*token))));
205 } catch(boost::bad_lexical_cast const &) {
206 throw InvalidINetAddressException();
208 if (++token != tokens.end())
209 throw InvalidINetAddressException();
212 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
215 sockaddr_.sin6_scope_id = 0;
217 sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
218 if (sockaddr_.sin6_scope_id == 0)
219 throw InvalidINetAddressException();
223 ///////////////////////////////cc.e////////////////////////////////////////
225 //#include "INetAddressing.mpp"
231 // c-file-style: "senf"
232 // indent-tabs-mode: nil
233 // ispell-local-dictionary: "american"
234 // compile-command: "scons -u test"
235 // comment-column: 40