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/regex.hpp>
38 //#include "INetAddressing.mpp"
40 ///////////////////////////////cc.p////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::INet4SocketAddress
45 prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string const & addr)
48 unsigned i = addr.find(':');
49 if (i == std::string::npos)
50 throw SyntaxException();
52 port(boost::lexical_cast< ::u_int16_t >(std::string(addr,i+1)));
54 catch (boost::bad_lexical_cast const &) {
55 throw SyntaxException();
58 address(INet4Address::from_string(std::string(addr,0,i)));
60 catch (INet4Address::SyntaxException const &) {
61 throw SyntaxException();
65 prefix_ senf::INet4SocketAddress::INet4SocketAddress(INet4Address const & addr, unsigned p)
72 prefix_ void senf::INet4SocketAddress::clear()
74 ::memset(&addr_,0,sizeof(addr_));
75 addr_.sin_family = AF_INET;
78 ///////////////////////////////////////////////////////////////////////////
79 // senf::INet6SocketAddress
81 prefix_ senf::INet6SocketAddress::INet6SocketAddress(std::string const & addr,
82 INet6Address::Resolve_t resolve)
86 // Format of addr: "[" address [ "%" interface ] "]" ":" port
89 static boost::regex const addressRx ("(?:\\[([a-f0-9A-F:]+)(?:%(.+))?\\]|(.+)):([0-9]+)");
90 // Subexpression numbers:
91 enum { NumericAddr = 1,
97 if (! regex_match(addr, match, addressRx))
98 throw SyntaxException();
100 if (match[ZoneId].matched)
101 assignIface(match[ZoneId]);
103 sockaddr_.sin6_port = htons(boost::lexical_cast<boost::uint16_t>(match[Port]));
106 INet6Address a (INet6Address::from_string(
107 match[NumericAddr].matched ? match[NumericAddr] : match[Hostname],
109 std::copy(a.begin(), a.end(), &sockaddr_.sin6_addr.s6_addr[0]);
110 } catch (INet6Address::SyntaxException const &) {
111 throw SyntaxException();
115 prefix_ bool senf::INet6SocketAddress::operator==(INet6SocketAddress const & other)
118 return ::memcmp(&sockaddr_.sin6_addr, &other.sockaddr_.sin6_addr, sizeof(sockaddr_.sin6_addr))==0 &&
119 sockaddr_.sin6_port == other.sockaddr_.sin6_port &&
120 sockaddr_.sin6_scope_id == other.sockaddr_.sin6_scope_id;
123 prefix_ bool senf::INet6SocketAddress::operator!=(INet6SocketAddress const & other)
126 return ! operator==(other);
129 prefix_ void senf::INet6SocketAddress::clear()
131 ::memset(&sockaddr_,0,sizeof(sockaddr_));
132 sockaddr_.sin6_family = AF_INET6;
135 prefix_ std::string senf::INet6SocketAddress::iface()
138 if (sockaddr_.sin6_scope_id == 0)
140 char buffer[IFNAMSIZ];
142 BOOST_ASSERT( if_indextoname(sockaddr_.sin6_scope_id,buffer) );
144 if_indextoname(sockaddr_.sin6_scope_id,buffer);
146 return std::string(buffer);
149 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
152 sockaddr_.sin6_scope_id = 0;
154 sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
155 if (sockaddr_.sin6_scope_id == 0)
156 throw SyntaxException();
160 ///////////////////////////////cc.e////////////////////////////////////////
162 //#include "INetAddressing.mpp"
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u test"
172 // comment-column: 40