4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.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>
37 #include "../../../Utils/senfassert.hh"
39 //#include "INetAddressing.mpp"
41 ///////////////////////////////cc.p////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////
44 // senf::INet4SocketAddress
46 prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string const & addr)
47 : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
49 unsigned portIx = addr.find(':');
51 port( boost::lexical_cast< ::u_int16_t >(portIx == std::string::npos
52 ? addr : std::string(addr,portIx+1)) );
54 catch (boost::bad_lexical_cast const &) {
55 throw AddressSyntaxException() << "invalid port number";
57 if (portIx != std::string::npos)
58 address( INet4Address::from_string(std::string(addr,0,portIx)) );
61 prefix_ senf::INet4SocketAddress::INet4SocketAddress(INet4Address const & addr, unsigned p)
62 : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
68 prefix_ senf::INet4SocketAddress::INet4SocketAddress(unsigned p)
69 : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
74 ///////////////////////////////////////////////////////////////////////////
75 // senf::INet6SocketAddress
77 prefix_ senf::INet6SocketAddress::INet6SocketAddress(std::string const & addr,
78 INet6Address::Resolve_t resolve)
79 : BSDSocketAddress (sizeof(sockaddr_in6), AF_INET6)
81 // Format of addr: "[" address [ "%" interface ] "]" ":" port
85 static boost::regex const addressRx ("(?:(?:\\[([^%]+)(?:%(.+))?\\]|(.+)):)?([0-9]+)");
86 // Subexpression numbers:
93 if (! regex_match(addr, match, addressRx))
94 throw AddressSyntaxException();
96 if (match[ZoneId].matched)
97 assignIface(match[ZoneId]);
99 sockaddr_.sin6_port = htons(boost::lexical_cast<boost::uint16_t>(match[Port]));
101 if (match[Address].matched || match[Hostname].matched) {
102 INet6Address a (INet6Address::from_string(
103 match[Address].matched ? match[Address] : match[Hostname],
105 std::copy(a.begin(), a.end(), &sockaddr_.sin6_addr.s6_addr[0]);
109 prefix_ std::string senf::INet6SocketAddress::iface()
112 if (sockaddr_.sin6_scope_id == 0)
114 char buffer[IFNAMSIZ];
116 SENF_ASSERT( if_indextoname(sockaddr_.sin6_scope_id,buffer) );
118 if_indextoname(sockaddr_.sin6_scope_id,buffer);
120 return std::string(buffer);
123 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
126 sockaddr_.sin6_scope_id = 0;
128 sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
129 if (sockaddr_.sin6_scope_id == 0)
130 throw AddressSyntaxException();
134 ///////////////////////////////cc.e////////////////////////////////////////
136 //#include "INetAddressing.mpp"
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"
146 // comment-column: 40