Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / 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 <senf/Utils/senfassert.hh>
38 #include <senf/Socket/Protocols/AddressExceptions.hh>
39
40 //#include "INetAddressing.mpp"
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 // senf::INet4SocketAddress
46
47 prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string const & addr)
48     : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
49 {
50     std::string::size_type portIx = addr.find(':');
51     try {
52         port( boost::lexical_cast< ::u_int16_t >(portIx == std::string::npos
53                                                  ? addr : std::string(addr,portIx+1)) );
54     }
55     catch (boost::bad_lexical_cast const &) {
56         throw AddressSyntaxException(addr) << ": invalid port number";
57     }
58     if (portIx != std::string::npos)
59         address( INet4Address::from_string(std::string(addr,0,portIx)) );
60 }
61
62 prefix_ senf::INet4SocketAddress::INet4SocketAddress(INet4Address const & addr, unsigned p)
63     : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
64 {
65     address(addr);
66     port(p);
67 }
68
69 prefix_ senf::INet4SocketAddress::INet4SocketAddress(unsigned p)
70     : BSDSocketAddress (sizeof(sockaddr_in), AF_INET)
71 {
72     port(p);
73 }
74
75 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4SocketAddress const & addr)
76 {
77     os << addr.address() << ":" << addr.port();
78     return os;
79 }
80
81 prefix_ std::istream & senf::operator>>(std::istream & is, INet4SocketAddress & addr)
82 {
83     std::string s;
84     if (!(is >> s))
85         return is;
86     try {
87         addr = INet4SocketAddress(s);
88     }
89     catch (AddressException &) {
90         is.setstate(std::ios::failbit);
91     }
92     return is;
93 }
94
95 //-/////////////////////////////////////////////////////////////////////////////////////////////////
96 // senf::INet6SocketAddress
97
98 prefix_ senf::INet6SocketAddress::INet6SocketAddress(std::string const & addr,
99                                                      INet6Address::Resolve_t resolve)
100     : BSDSocketAddress (sizeof(sockaddr_in6), AF_INET6)
101 {
102     // Format of addr: "[" address [ "%" interface ] "]" ":" port
103     //             or: host ":" port
104     //             or: port
105
106     static boost::regex const addressRx ("(?:(?:\\[([^%]+)(?:%(.+))?\\]|(.+)):)?([0-9]+)");
107     // Subexpression numbers:
108     enum { Address  = 1,
109            ZoneId   = 2,
110            Hostname = 3,
111            Port     = 4 };
112
113     boost::smatch match;
114     if (! regex_match(addr, match, addressRx))
115         throw AddressSyntaxException(addr);
116
117     if (match[ZoneId].matched)
118         assignIface(match[ZoneId]);
119
120     sockaddr_.sin6_port = htons(boost::lexical_cast<boost::uint16_t>(match[Port]));
121
122     if (match[Address].matched || match[Hostname].matched) {
123         INet6Address a (INet6Address::from_string(
124                             match[Address].matched ? match[Address] : match[Hostname],
125                             resolve));
126         std::copy(a.begin(), a.end(), &sockaddr_.sin6_addr.s6_addr[0]);
127     }
128 }
129
130 prefix_ std::string senf::INet6SocketAddress::iface()
131     const
132 {
133     if (sockaddr_.sin6_scope_id == 0)
134         return "";
135     char buffer[IFNAMSIZ];
136     SENF_ASSERT_EXPRESSION(if_indextoname(sockaddr_.sin6_scope_id,buffer),
137                            "Internal failure: Invalid interface index (how does it get here?)");
138     return std::string(buffer);
139 }
140
141 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
142 {
143     if (iface.empty())
144         sockaddr_.sin6_scope_id = 0;
145     else {
146         sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
147         if (sockaddr_.sin6_scope_id == 0)
148             throw AddressSyntaxException(iface);
149     }
150 }
151
152 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6SocketAddress const & addr)
153 {
154     os << '[' << addr.address();
155     std::string iface (addr.iface());
156     if (! iface.empty())
157         os << '%' << iface;
158     os << "]:" << addr.port();
159     return os;
160 }
161
162 prefix_ std::istream & senf::operator>>(std::istream & is, INet6SocketAddress & addr)
163 {
164     std::string s;
165     if (!(is >> s))
166         return is;
167     try {
168         addr = INet6SocketAddress(s);
169     }
170     catch (AddressException &) {
171         is.setstate(std::ios::failbit);
172     }
173     return is;
174 }
175
176 //-/////////////////////////////////////////////////////////////////////////////////////////////////
177 #undef prefix_
178 //#include "INetAddressing.mpp"
179
180 \f
181 // Local Variables:
182 // mode: c++
183 // fill-column: 100
184 // c-file-style: "senf"
185 // indent-tabs-mode: nil
186 // ispell-local-dictionary: "american"
187 // compile-command: "scons -u test"
188 // comment-column: 40
189 // End: