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