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