cf38dd47bce7a4a85d10876ff54ba1d61a840cef
[senf.git] / Socket / LLAddressing.ct
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 template functions
24
25 #include "LLAddressing.ih"
26
27 // Custom includes
28 #include <sstream>
29 #include <iomanip>
30 #include <string.h>
31
32 #define prefix_
33 ///////////////////////////////ct.p////////////////////////////////////////
34
35 template <class ForwardRange>
36 prefix_ void satcom::lib::LLSocketAddress::address(ForwardRange const & address)
37 {
38     if (boost::size(address) > sizeof(addr_.sll_addr))
39         throw InvalidLLSocketAddressException();
40     typename boost::range_const_iterator<ForwardRange>::type i (boost::begin(address));
41     ::memset(&addr_.sll_addr[0],sizeof(addr_.sll_addr),0);
42     addr_.sll_halen = 0;
43     for (; i != boost::end(address) && addr_.sll_halen<8; ++i, ++addr_.sll_halen)
44         addr_.sll_addr[addr_.sll_halen] = *i;
45     if (i != boost::end(address))
46         throw InvalidLLSocketAddressException();
47 }
48
49 template <class ForwardRange>
50 prefix_ std::string
51 satcom::lib::llAddress(ForwardRange const & address,
52                        typename boost::enable_if< boost::is_class<ForwardRange> >::type *)
53 {
54     if (boost::empty(address))
55         return std::string();
56     std::stringstream s;
57     typename boost::range_const_iterator< ForwardRange >::type i (boost::begin(address));
58     while (1) {
59         s << std::hex << std::setw(2) << std::setfill('0') << unsigned(*i);
60         ++ i;
61         if (i == boost::end(address))
62             break;
63         s << '-';
64     }
65     return s.str();
66 }
67
68 template <class ForwardRange>
69 prefix_ unsigned char satcom::lib::detail::HexConverter::operator()(ForwardRange const & v)
70     const
71 {
72     if (boost::size(v) != 2)
73         throw InvalidLLSocketAddressException();
74     typename boost::range_iterator< ForwardRange >::type i (boost::begin(v));
75     unsigned char n1 = hexnibble(*i) << 4;
76     return n1 + hexnibble(*(++i));
77 }
78
79 ///////////////////////////////ct.e////////////////////////////////////////
80 #undef prefix_
81
82 \f
83 // Local Variables:
84 // mode: c++
85 // c-file-style: "satcom"
86 // End: