9ef8c39d8e7ec15e8a683ead5a67a91574f05641
[senf.git] / Socket / Protocols / INet / 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::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     address(INet4Address::from_string(std::string(addr,0,i)));
53     try {
54         port(boost::lexical_cast< ::u_int16_t >(std::string(addr,i+1)));
55     }
56     catch (boost::bad_lexical_cast const &) {
57         throw SyntaxException();
58     }
59 }
60
61 prefix_ senf::INet4SocketAddress::INet4SocketAddress(INet4Address const & addr, unsigned p)
62 {
63     clear();
64     address(addr);
65     port(p);
66 }
67
68 prefix_ void senf::INet4SocketAddress::clear()
69 {
70     ::memset(&addr_,0,sizeof(addr_));
71     addr_.sin_family = AF_INET;
72 }
73
74 ///////////////////////////////////////////////////////////////////////////
75 // senf::INet6SocketAddress
76
77 prefix_ bool senf::INet6SocketAddress::operator==(INet6SocketAddress const & other)
78     const
79 {
80     return ::memcmp(&sockaddr_.sin6_addr, &other.sockaddr_.sin6_addr, sizeof(sockaddr_.sin6_addr))==0 &&
81         sockaddr_.sin6_port == other.sockaddr_.sin6_port &&
82         sockaddr_.sin6_scope_id == other.sockaddr_.sin6_scope_id;
83 }
84
85 prefix_ bool senf::INet6SocketAddress::operator!=(INet6SocketAddress const & other)
86     const
87 {
88     return ! operator==(other);
89 }
90
91 prefix_ void senf::INet6SocketAddress::clear()
92 {
93     ::memset(&sockaddr_,0,sizeof(sockaddr_));
94     sockaddr_.sin6_family = AF_INET6;
95 }
96
97 prefix_ std::string senf::INet6SocketAddress::address()
98     const
99 {
100     std::stringstream ss;
101     ss << '[' << host();
102     if (sockaddr_.sin6_scope_id != 0)
103         ss << '@' << iface()
104     << "]:" << port();
105     return ss.str();
106 }
107
108 prefix_ std::string senf::INet6SocketAddress::iface()
109     const
110 {
111     if (sockaddr_.sin6_scope_id == 0)
112         return "";
113     char buffer[IFNAMSIZ];
114     BOOST_ASSERT( if_indextoname(sockaddr_.sin6_scope_id,buffer) );
115     return std::string(buffer);
116 }
117
118 prefix_ void senf::INet6SocketAddress::assignAddr(std::string const & addr)
119 {
120     // Format of addr: "[" address [ "@" interface ] "]" ":" port
121     typedef boost::char_separator<char> separator;
122     typedef boost::tokenizer<separator> tokenizer;
123     // we don't add ':' to the list of separators since that would give as the IPv6 address
124     // as a list of tokens. We just strip the : from the port number manually
125     separator sep ("", "@[]");
126     tokenizer tokens (addr, sep);
127     tokenizer::iterator token (tokens.begin());
128     if (token == tokens.end()
129         || *token != "["
130         || ++token == tokens.end()
131         || inet_pton(AF_INET6, std::string(boost::begin(*token),boost::end(*token)).c_str(),
132                      &sockaddr_.sin6_addr) <= 0
133         || ++token == tokens.end())
134         throw SyntaxException();
135     if (*token == "@") {
136         if (++token == tokens.end())
137             throw SyntaxException();
138         assignIface(std::string(boost::begin(*token),boost::end(*token)));
139         if (++token == tokens.end()
140             || *token != "]")
141             throw SyntaxException();
142     } else if (*token != "]")
143         throw SyntaxException();
144     if (++token == tokens.end()
145         || *boost::begin(*token) != ':')
146         throw SyntaxException();
147     try {
148         sockaddr_.sin6_port = htons(
149             boost::lexical_cast<unsigned>(std::string(boost::next(boost::begin(*token)),
150                                                       boost::end(*token))));
151     } catch(boost::bad_lexical_cast const &) {
152         throw SyntaxException();
153     }
154     if (++token != tokens.end())
155         throw SyntaxException();
156 }
157
158 prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
159 {
160     if (iface.empty())
161         sockaddr_.sin6_scope_id = 0;
162     else {
163         sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
164         if (sockaddr_.sin6_scope_id == 0)
165             throw SyntaxException();
166     }
167 }
168
169 ///////////////////////////////cc.e////////////////////////////////////////
170 #undef prefix_
171 //#include "INetAddressing.mpp"
172
173 \f
174 // Local Variables:
175 // mode: c++
176 // fill-column: 100
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"
181 // comment-column: 40
182 // End: