4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief INet[46]Address and INet[46]AddressingPolicy non-inline non-template implementation
32 #include "INetAddressing.hh"
33 //#include "INetAddressing.ih"
38 #include <sys/socket.h>
40 #include <boost/lexical_cast.hpp>
41 #include <boost/regex.hpp>
42 #include <senf/Utils/senfassert.hh>
43 #include <senf/Socket/Protocols/AddressExceptions.hh>
45 //#include "INetAddressing.mpp"
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
49 //-/////////////////////////////////////////////////////////////////////////////////////////////////
50 // senf::INet4SocketAddress
52 prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string const & addr)
53 : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
55 std::string::size_type portIx = addr.find(':');
57 port( boost::lexical_cast< ::u_int16_t >(portIx == std::string::npos
58 ? addr : std::string(addr,portIx+1)) );
60 catch (boost::bad_lexical_cast const &) {
61 throw AddressSyntaxException(addr) << ": invalid port number";
63 if (portIx != std::string::npos)
64 address( INet4Address::from_string(std::string(addr,0,portIx)) );
67 prefix_ senf::INet4SocketAddress::INet4SocketAddress(INet4Address const & addr, unsigned p)
68 : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
74 prefix_ senf::INet4SocketAddress::INet4SocketAddress(unsigned p)
75 : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
80 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4SocketAddress const & addr)
82 os << addr.address() << ":" << addr.port();
86 prefix_ std::istream & senf::operator>>(std::istream & is, INet4SocketAddress & addr)
92 addr = INet4SocketAddress(s);
94 catch (AddressException &) {
95 is.setstate(std::ios::failbit);
100 //-/////////////////////////////////////////////////////////////////////////////////////////////////
101 // senf::INet6SocketAddress
103 prefix_ senf::INet6SocketAddress::INet6SocketAddress(std::string const & addr,
104 INet6Address::Resolve_t resolve)
105 : BSDSocketAddress (sizeof(sockaddr_in6), AF_INET6)
107 // Format of addr: "[" address [ "%" interface ] "]" ":" port
111 static boost::regex const addressRx ("(?:(?:\\[([^%]+)(?:%(.+))?\\]|(.+)):)?([0-9]+)");
112 // Subexpression numbers:
119 if (! regex_match(addr, match, addressRx))
120 throw AddressSyntaxException(addr);
122 if (match[ZoneId].matched)
123 assignIface(match[ZoneId]);
125 sockaddr_.sin6_port = htons(boost::lexical_cast<boost::uint16_t>(match[Port]));
127 if (match[Address].matched || match[Hostname].matched) {
128 INet6Address a (INet6Address::from_string(
129 match[Address].matched ? match[Address] : match[Hostname],
131 std::copy(a.begin(), a.end(), &sockaddr_.sin6_addr.s6_addr[0]);
135 prefix_ std::string senf::INet6SocketAddress::iface()
138 if (sockaddr_.sin6_scope_id == 0)
140 char buffer[IFNAMSIZ];
141 SENF_ASSERT_EXPRESSION(if_indextoname(sockaddr_.sin6_scope_id,buffer),
142 "Internal failure: Invalid interface index (how does it get here?)");
143 return std::string(buffer);
146 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
149 sockaddr_.sin6_scope_id = 0;
151 sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
152 if (sockaddr_.sin6_scope_id == 0)
153 throw AddressSyntaxException(iface);
157 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6SocketAddress const & addr)
159 os << '[' << addr.address();
160 std::string iface (addr.iface());
163 os << "]:" << addr.port();
167 prefix_ std::istream & senf::operator>>(std::istream & is, INet6SocketAddress & addr)
173 addr = INet6SocketAddress(s);
175 catch (AddressException &) {
176 is.setstate(std::ios::failbit);
181 //-/////////////////////////////////////////////////////////////////////////////////////////////////
183 //#include "INetAddressing.mpp"
189 // c-file-style: "senf"
190 // indent-tabs-mode: nil
191 // ispell-local-dictionary: "american"
192 // compile-command: "scons -u test"
193 // comment-column: 40