Fixed whitespace in all files (no tabs)
[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_ std::string senf::INet6SocketAddress::address()
152     const
153 {
154     std::stringstream ss;
155     ss << '[' << host();
156     if (sockaddr_.sin6_scope_id != 0)
157         ss << '@' << iface()
158     << "]:" << port();
159     return ss.str();
160 }
161
162 prefix_ std::string senf::INet6SocketAddress::iface()
163     const
164 {
165     if (sockaddr_.sin6_scope_id == 0)
166         return "";
167     char buffer[IFNAMSIZ];
168     BOOST_ASSERT( if_indextoname(sockaddr_.sin6_scope_id,buffer) );
169     return std::string(buffer);
170 }
171
172 prefix_ void senf::INet6SocketAddress::assignAddr(std::string const & addr)
173 {
174     // Format of addr: "[" address [ "@" interface ] "]" ":" port
175     typedef boost::char_separator<char> separator;
176     typedef boost::tokenizer<separator> tokenizer;
177     // we don't add ':' to the list of separators since that would give as the IPv6 address
178     // as a list of tokens. We just strip the : from the port number manually
179     separator sep ("", "@[]");
180     tokenizer tokens (addr, sep);
181     tokenizer::iterator token (tokens.begin());
182     if (token == tokens.end()
183         || *token != "["
184         || ++token == tokens.end()
185         || inet_pton(AF_INET6, std::string(boost::begin(*token),boost::end(*token)).c_str(),
186                      &sockaddr_.sin6_addr) <= 0
187         || ++token == tokens.end())
188         throw InvalidINetAddressException();
189     if (*token == "@") {
190         if (++token == tokens.end())
191             throw InvalidINetAddressException();
192         assignIface(std::string(boost::begin(*token),boost::end(*token)));
193         if (++token == tokens.end()
194             || *token != "]")
195             throw InvalidINetAddressException();
196     } else if (*token != "]")
197         throw InvalidINetAddressException();
198     if (++token == tokens.end()
199         || *boost::begin(*token) != ':')
200         throw InvalidINetAddressException();
201     try {
202         sockaddr_.sin6_port = htons(
203             boost::lexical_cast<unsigned>(std::string(boost::next(boost::begin(*token)),
204                                                       boost::end(*token))));
205     } catch(boost::bad_lexical_cast const &) {
206         throw InvalidINetAddressException();
207     }
208     if (++token != tokens.end())
209         throw InvalidINetAddressException();
210 }
211
212 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
213 {
214     if (iface.empty())
215         sockaddr_.sin6_scope_id = 0;
216     else {
217         sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
218         if (sockaddr_.sin6_scope_id == 0)
219             throw InvalidINetAddressException();
220     }
221 }
222
223 ///////////////////////////////cc.e////////////////////////////////////////
224 #undef prefix_
225 //#include "INetAddressing.mpp"
226
227 \f
228 // Local Variables:
229 // mode: c++
230 // fill-column: 100
231 // c-file-style: "senf"
232 // indent-tabs-mode: nil
233 // ispell-local-dictionary: "american"
234 // End: