switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / INet / INetAddressing.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief INet[46]Address and INet[46]AddressingPolicy non-inline non-template implementation
30  */
31
32 #include "INetAddressing.hh"
33 //#include "INetAddressing.ih"
34
35 // Custom includes
36 #include <sstream>
37 #include <string.h>
38 #include <sys/socket.h>
39 #include <net/if.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>
44
45 //#include "INetAddressing.mpp"
46 #define prefix_
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48
49 //-/////////////////////////////////////////////////////////////////////////////////////////////////
50 // senf::INet4SocketAddress
51
52 prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string const & addr)
53     : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
54 {
55     std::string::size_type portIx = addr.find(':');
56     try {
57         port( boost::lexical_cast< ::u_int16_t >(portIx == std::string::npos
58                                                  ? addr : std::string(addr,portIx+1)) );
59     }
60     catch (boost::bad_lexical_cast const &) {
61         throw AddressSyntaxException(addr) << ": invalid port number";
62     }
63     if (portIx != std::string::npos)
64         address( INet4Address::from_string(std::string(addr,0,portIx)) );
65 }
66
67 prefix_ senf::INet4SocketAddress::INet4SocketAddress(INet4Address const & addr, unsigned p)
68     : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
69 {
70     address(addr);
71     port(p);
72 }
73
74 prefix_ senf::INet4SocketAddress::INet4SocketAddress(unsigned p)
75     : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
76 {
77     port(p);
78 }
79
80 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4SocketAddress const & addr)
81 {
82     os << addr.address() << ":" << addr.port();
83     return os;
84 }
85
86 prefix_ std::istream & senf::operator>>(std::istream & is, INet4SocketAddress & addr)
87 {
88     std::string s;
89     if (!(is >> s))
90         return is;
91     try {
92         addr = INet4SocketAddress(s);
93     }
94     catch (AddressException &) {
95         is.setstate(std::ios::failbit);
96     }
97     return is;
98 }
99
100 //-/////////////////////////////////////////////////////////////////////////////////////////////////
101 // senf::INet6SocketAddress
102
103 prefix_ senf::INet6SocketAddress::INet6SocketAddress(std::string const & addr,
104                                                      INet6Address::Resolve_t resolve)
105     : BSDSocketAddress (sizeof(sockaddr_in6), AF_INET6)
106 {
107     // Format of addr: "[" address [ "%" interface ] "]" ":" port
108     //             or: host ":" port
109     //             or: port
110
111     static boost::regex const addressRx ("(?:(?:\\[([^%]+)(?:%(.+))?\\]|(.+)):)?([0-9]+)");
112     // Subexpression numbers:
113     enum { Address  = 1,
114            ZoneId   = 2,
115            Hostname = 3,
116            Port     = 4 };
117
118     boost::smatch match;
119     if (! regex_match(addr, match, addressRx))
120         throw AddressSyntaxException(addr);
121
122     if (match[ZoneId].matched)
123         assignIface(match[ZoneId]);
124
125     sockaddr_.sin6_port = htons(boost::lexical_cast<boost::uint16_t>(match[Port]));
126
127     if (match[Address].matched || match[Hostname].matched) {
128         INet6Address a (INet6Address::from_string(
129                             match[Address].matched ? match[Address] : match[Hostname],
130                             resolve));
131         std::copy(a.begin(), a.end(), &sockaddr_.sin6_addr.s6_addr[0]);
132     }
133 }
134
135 prefix_ std::string senf::INet6SocketAddress::iface()
136     const
137 {
138     if (sockaddr_.sin6_scope_id == 0)
139         return "";
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);
144 }
145
146 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
147 {
148     if (iface.empty())
149         sockaddr_.sin6_scope_id = 0;
150     else {
151         sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
152         if (sockaddr_.sin6_scope_id == 0)
153             throw AddressSyntaxException(iface);
154     }
155 }
156
157 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6SocketAddress const & addr)
158 {
159     os << '[' << addr.address();
160     std::string iface (addr.iface());
161     if (! iface.empty())
162         os << '%' << iface;
163     os << "]:" << addr.port();
164     return os;
165 }
166
167 prefix_ std::istream & senf::operator>>(std::istream & is, INet6SocketAddress & addr)
168 {
169     std::string s;
170     if (!(is >> s))
171         return is;
172     try {
173         addr = INet6SocketAddress(s);
174     }
175     catch (AddressException &) {
176         is.setstate(std::ios::failbit);
177     }
178     return is;
179 }
180
181 //-/////////////////////////////////////////////////////////////////////////////////////////////////
182 #undef prefix_
183 //#include "INetAddressing.mpp"
184
185 \f
186 // Local Variables:
187 // mode: c++
188 // fill-column: 100
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
194 // End: