Implement INet6SocketAddress
[senf.git] / Socket / INetAddressing.cc
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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/tokenizer.hpp>
37 #include <boost/range.hpp>
38
39 //#include "INetAddressing.mpp"
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 ///////////////////////////////////////////////////////////////////////////
44 // senf::INet4Address
45
46 prefix_ senf::INet4Address::INet4Address(std::string host, unsigned port)
47 {
48     clear();
49     /** \todo  gethostbyname support */
50     if (::inet_aton(host.c_str(), &addr_.sin_addr) == 0)
51         throw InvalidINetAddressException();
52     addr_.sin_port = htons(port);
53 }
54
55 prefix_ std::string senf::INet4Address::str()
56     const
57 {
58     std::stringstream s;
59     s << host() << ':' << port();
60     return s.str();
61 }
62
63 prefix_ void senf::INet4Address::clear()
64 {
65     ::memset(&addr_,0,sizeof(addr_));
66     addr_.sin_family = AF_INET;
67 }
68
69 prefix_ void senf::INet4Address::assignString(std::string address)
70 {
71     clear();
72     unsigned i = address.find(':');
73     if (i == std::string::npos)
74         throw InvalidINetAddressException();
75     // The temporary string in the next expr is guaranteed to live
76     // until end-of-statement
77     if (::inet_aton(std::string(address,0,i).c_str(), &addr_.sin_addr) == 0)
78         throw InvalidINetAddressException();
79     try {
80         // Replace lexical_cast with strtoul ?
81         addr_.sin_port = htons(boost::lexical_cast< ::u_int16_t >(std::string(address,i+1)));
82     } 
83     catch (boost::bad_lexical_cast const &) {
84         throw InvalidINetAddressException();
85     }
86 }
87
88 ///////////////////////////////////////////////////////////////////////////
89 // senf::INet6Address
90
91 prefix_ senf::INet6Address::INet6Address(std::string const & addr)
92 {
93     if (inet_pton(AF_INET6,addr.c_str(),&addr_) <= 0)
94         throw InvalidINetAddressException();
95 }
96
97 prefix_ senf::INet6Address::INet6Address(char const * addr)
98 {
99     if (inet_pton(AF_INET6,addr,&addr_) <= 0)
100         throw InvalidINetAddressException();
101 }
102
103 prefix_ void senf::INet6Address::clear()
104 {
105     ::memset(&addr_,0,sizeof(addr_));
106 }
107
108 prefix_ std::string senf::INet6Address::address()
109     const
110 {
111     char buffer[8*5];
112     BOOST_ASSERT( inet_ntop(AF_INET6, &addr_, buffer, sizeof(buffer)) );
113     return std::string(buffer);
114 }
115
116 prefix_ bool senf::INet6Address::operator==(INet6Address const & other)
117     const
118 {
119     return ::memcmp(&addr_,&other.addr_,sizeof(addr_))==0;
120 }
121
122 prefix_ bool senf::INet6Address::operator!=(INet6Address const & other)
123     const
124 {
125     return !operator==(other);
126 }
127
128 ///////////////////////////////////////////////////////////////////////////
129 // senf::INet6SocketAddress
130
131 prefix_ bool senf::INet6SocketAddress::operator==(INet6SocketAddress const & other)
132     const
133 {
134     return ::memcmp(&sockaddr_.sin6_addr, &other.sockaddr_.sin6_addr, sizeof(sockaddr_.sin6_addr))==0 &&
135         sockaddr_.sin6_port == other.sockaddr_.sin6_port && 
136         sockaddr_.sin6_scope_id == other.sockaddr_.sin6_scope_id;
137 }
138
139 prefix_ bool senf::INet6SocketAddress::operator!=(INet6SocketAddress const & other)
140     const
141 {
142     return ! operator==(other);
143 }
144
145 prefix_ void senf::INet6SocketAddress::clear()
146 {
147     ::memset(&sockaddr_,0,sizeof(sockaddr_));
148     sockaddr_.sin6_family = AF_INET6;
149 }
150
151 prefix_ void senf::INet6SocketAddress::address(std::string const & addr)
152 {
153     if (addr[0]=='[')
154         assignAddr(addr);
155     else
156         host(addr);
157 }
158
159 prefix_ std::string senf::INet6SocketAddress::address()
160     const
161 {
162     std::stringstream ss;
163     ss << '[' << host();
164     if (sockaddr_.sin6_scope_id != 0)
165         ss << '@' << iface()
166     << "]:" << port();
167     return ss.str();
168 }
169
170 prefix_ std::string senf::INet6SocketAddress::iface()
171     const
172 {
173     if (sockaddr_.sin6_scope_id == 0)
174         return "";
175     char buffer[IFNAMSIZ];
176     BOOST_ASSERT( if_indextoname(sockaddr_.sin6_scope_id,buffer) );
177     return std::string(buffer);
178 }
179
180 prefix_ void senf::INet6SocketAddress::assignAddr(std::string const & addr)
181 {
182     // Format of addr: "[" address [ "@" interface ] "]" ":" port
183     typedef boost::char_separator<char> separator;
184     typedef boost::tokenizer<separator> tokenizer;
185     // we don't add ':' to the list of separators since that would give as the IPv6 address
186     // as a list of tokens. We just strip the : from the port number manually
187     separator sep ("", "@[]"); 
188     tokenizer tokens (addr, sep);
189     tokenizer::iterator token (tokens.begin());
190     if (token == tokens.end() 
191         || *token != "["
192         || ++token == tokens.end()
193         || inet_pton(AF_INET6, std::string(boost::begin(*token),boost::end(*token)).c_str(), 
194                      &sockaddr_.sin6_addr) <= 0
195         || ++token == tokens.end())
196         throw InvalidINetAddressException();
197     if (*token == "@") {
198         if (++token == tokens.end())
199             throw InvalidINetAddressException();
200         assignIface(std::string(boost::begin(*token),boost::end(*token)));
201         if (++token == tokens.end()
202             || *token != "]")
203             throw InvalidINetAddressException();
204     } else if (*token != "]")
205         throw InvalidINetAddressException();
206     if (++token == tokens.end()
207         || *boost::begin(*token) != ':')
208         throw InvalidINetAddressException();
209     try {
210         sockaddr_.sin6_port = htons(
211             boost::lexical_cast<unsigned>(std::string(boost::next(boost::begin(*token)),
212                                                       boost::end(*token))));
213     } catch(boost::bad_lexical_cast const &) {
214         throw InvalidINetAddressException();
215     }
216     if (++token != tokens.end())
217         throw InvalidINetAddressException();
218 }
219
220 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
221 {
222     if (iface.empty())
223         sockaddr_.sin6_scope_id = 0;
224     else {
225         sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
226         if (sockaddr_.sin6_scope_id == 0)
227             throw InvalidINetAddressException();
228     }
229 }
230
231 ///////////////////////////////cc.e////////////////////////////////////////
232 #undef prefix_
233 //#include "INetAddressing.mpp"
234
235 \f
236 // Local Variables:
237 // mode: c++
238 // c-file-style: "senf"
239 // End: