Fixed whitespace in all files (no tabs)
[senf.git] / Socket / LLAddressing.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 LLSocketAddress and LLAddressingPolicy non-inline non-template implementation
25  */
26
27 #include "LLAddressing.hh"
28 #include "LLAddressing.ih"
29
30 // Custom includes
31 #include <net/if.h>
32 #include <sys/socket.h>
33
34 #include <boost/algorithm/string/classification.hpp>
35 #include <boost/algorithm/string/finder.hpp>
36
37 #include "Utils/Exception.hh"
38
39 //#include "LLAddressing.mpp"
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 prefix_ unsigned char senf::detail::hexnibble(char c)
44 {
45     if (c>='0' && c<='9')
46         return c - '0';
47     if (c>='A' && c<='F')
48         return c - 'A' + 10;
49     if (c>='a' && c<='f')
50         return c - 'a' + 10;
51     throw InvalidLLSocketAddressException();
52 }
53
54 prefix_ std::string senf::LLSocketAddress::interface()
55     const
56 {
57     if (addr_.sll_ifindex == 0)
58         return std::string();
59     char name[IFNAMSIZ];
60     if (! ::if_indextoname(addr_.sll_ifindex, name))
61         throw InvalidLLSocketAddressException();
62     return std::string(name);
63 }
64
65 /*
66 {
67     if (addr_.sll_halen == 0)
68         return std::string();
69     std::stringstream s;
70
71     unsigned char const * i = &addr_.sll_addr[0];
72     while (1) {
73         s << std::hex << std::setw(2) << std::setfill('0') << unsigned(*i);
74         ++i;
75         if (i == &addr_.sll_addr[addr_.sll_halen]) break;
76         s << '-';
77     }
78     return s.str();
79 }
80 */
81
82
83 /*
84 prefix_ void senf::LLSocketAddress::address(std::string address)
85 {
86     typedef boost::split_iterator<std::string::iterator> StringSplitIterator;
87     StringSplitIterator i = boost::make_split_iterator(address, boost::token_finder(boost::is_any_of("-: ")));
88     unsigned char * j = &addr_.sll_addr[0];
89     for (; ! i.eof() && addr_.sll_halen<8; ++i, ++j, ++addr_.sll_halen) {
90         if ( i->size() != 2 || ! boost::all(*i, boost::is_xdigit()) )
91             throw InvalidLLSocketAddressException();
92         *j = hex(*i);
93     }
94     if (! i.eof())
95         throw InvalidLLSocketAddressException();
96 }
97 */
98
99 prefix_ void senf::LLSocketAddress::interface(std::string interface)
100 {
101     if (! interface.empty()) {
102         addr_.sll_ifindex = if_nametoindex(interface.c_str());
103         if (addr_.sll_ifindex == 0)
104             throw InvalidLLSocketAddressException();
105     }
106 }
107
108
109 prefix_ senf::detail::LLAddressFromStringRange
110 senf::llAddress(std::string address)
111 {
112     detail::StringSplitIterator i =
113         boost::make_split_iterator(address, boost::token_finder(boost::is_any_of("-: ")));
114     detail::StringSplitIterator i_end;
115
116     detail::HexSplitIterator j (i,detail::HexConverter());
117     detail::HexSplitIterator j_end (i_end);
118
119     return detail::LLAddressFromStringRange(j,j_end);
120 }
121
122 ///////////////////////////////cc.e////////////////////////////////////////
123 #undef prefix_
124 //#include "LLAddressing.mpp"
125
126 \f
127 // Local Variables:
128 // mode: c++
129 // fill-column: 100
130 // c-file-style: "senf"
131 // indent-tabs-mode: nil
132 // ispell-local-dictionary: "american"
133 // End: