d4f1149f67b589c002be2c60bd4de7d8fe3d4a14
[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 {
48     clear();
49     unsigned i = addr.find(':');
50     if (i == std::string::npos)
51         throw SyntaxException();
52     try {
53         port(boost::lexical_cast< ::u_int16_t >(std::string(addr,i+1)));
54     }
55     catch (boost::bad_lexical_cast const &) {
56         throw SyntaxException();
57     }
58     try {
59         address(INet4Address::from_string(std::string(addr,0,i)));
60     }
61     catch (INet4Address::SyntaxException const &) {
62         throw SyntaxException();
63     }
64 }
65
66 prefix_ senf::INet4SocketAddress::INet4SocketAddress(INet4Address const & addr, unsigned p)
67 {
68     clear();
69     address(addr);
70     port(p);
71 }
72
73 prefix_ void senf::INet4SocketAddress::clear()
74 {
75     ::memset(&addr_,0,sizeof(addr_));
76     addr_.sin_family = AF_INET;
77 }
78
79 ///////////////////////////////////////////////////////////////////////////
80 // senf::INet6SocketAddress
81
82 prefix_ senf::INet6SocketAddress::INet6SocketAddress(std::string const & addr,
83                                                      INet6Address::Resolve_t resolve)
84 {
85     clear();
86
87     // Format of addr: "[" address [ "%" interface ] "]" ":" port
88     //             or: host ":" port
89
90     static boost::regex const addressRx ("(?:\\[([a-f0-9A-F:]+)(?:%(.+))?\\]|(.+)):([0-9]+)");
91     // Subexpression numbers:
92     enum { NumericAddr = 1,
93            ZoneId      = 2,
94            Hostname    = 3,
95            Port        = 4 };
96     
97     boost::smatch match;
98     if (! regex_match(addr, match, addressRx))
99         throw SyntaxException();
100
101     if (match[ZoneId].matched)
102         assignIface(match[ZoneId]);
103
104     sockaddr_.sin6_port = htons(boost::lexical_cast<boost::uint16_t>(match[Port]));
105
106     try {
107         INet6Address a (INet6Address::from_string(
108                             match[NumericAddr].matched ? match[NumericAddr] : match[Hostname],
109                             resolve));
110         std::copy(a.begin(), a.end(), &sockaddr_.sin6_addr.s6_addr[0]);
111     } catch (INet6Address::SyntaxException const &) {
112           throw SyntaxException();
113     }
114 }
115
116 prefix_ bool senf::INet6SocketAddress::operator==(INet6SocketAddress const & other)
117     const
118 {
119     return ::memcmp(&sockaddr_.sin6_addr, &other.sockaddr_.sin6_addr, sizeof(sockaddr_.sin6_addr))==0 &&
120         sockaddr_.sin6_port == other.sockaddr_.sin6_port &&
121         sockaddr_.sin6_scope_id == other.sockaddr_.sin6_scope_id;
122 }
123
124 prefix_ bool senf::INet6SocketAddress::operator!=(INet6SocketAddress const & other)
125     const
126 {
127     return ! operator==(other);
128 }
129
130 prefix_ void senf::INet6SocketAddress::clear()
131 {
132     ::memset(&sockaddr_,0,sizeof(sockaddr_));
133     sockaddr_.sin6_family = AF_INET6;
134 }
135
136 prefix_ std::string senf::INet6SocketAddress::iface()
137     const
138 {
139     if (sockaddr_.sin6_scope_id == 0)
140         return "";
141     char buffer[IFNAMSIZ];
142 #ifndef SENF_NO_DEBUG
143     SENF_ASSERT( if_indextoname(sockaddr_.sin6_scope_id,buffer) );
144 #else
145     if_indextoname(sockaddr_.sin6_scope_id,buffer);
146 #endif
147     return std::string(buffer);
148 }
149
150 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
151 {
152     if (iface.empty())
153         sockaddr_.sin6_scope_id = 0;
154     else {
155         sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
156         if (sockaddr_.sin6_scope_id == 0)
157             throw SyntaxException();
158     }
159 }
160
161 ///////////////////////////////cc.e////////////////////////////////////////
162 #undef prefix_
163 //#include "INetAddressing.mpp"
164
165 \f
166 // Local Variables:
167 // mode: c++
168 // fill-column: 100
169 // c-file-style: "senf"
170 // indent-tabs-mode: nil
171 // ispell-local-dictionary: "american"
172 // compile-command: "scons -u test"
173 // comment-column: 40
174 // End: