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