switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / Raw / LLAddressing.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief LLSocketAddress and LLAddressingPolicy non-inline non-template implementation
30  */
31
32 #include "LLAddressing.hh"
33 //#include "LLAddressing.ih"
34
35 // Custom includes
36 #include <net/if.h>
37 #include <sys/socket.h>
38
39 #include <boost/algorithm/string/classification.hpp>
40 #include <boost/algorithm/string/finder.hpp>
41
42 #include <senf/Socket/Protocols/AddressExceptions.hh>
43
44 //#include "LLAddressing.mpp"
45 #define prefix_
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47
48 prefix_ std::string senf::LLSocketAddress::interface()
49     const
50 {
51     if (addr_.sll_ifindex == 0)
52         return std::string();
53     char name[IFNAMSIZ];
54     ::bzero(name, IFNAMSIZ);
55     if (! ::if_indextoname(addr_.sll_ifindex, name))
56         throw UnknownInterfaceException();
57     return std::string(name);
58 }
59
60 prefix_ void senf::LLSocketAddress::interface(std::string const & iface)
61 {
62     if (iface.empty())
63         addr_.sll_ifindex = 0;
64     else {
65         addr_.sll_ifindex = if_nametoindex(iface.c_str());
66         if (addr_.sll_ifindex == 0)
67             throw UnknownInterfaceException(iface);
68     }
69 }
70
71 prefix_ std::ostream & senf::operator<<(std::ostream & os, LLSocketAddress const & llAddr)
72 {
73     os << "[" << llAddr.address()
74        << '%' << llAddr.interface()
75        << ' ' << llAddr.protocol()
76        << ' ' << llAddr.arptype()
77        << ( llAddr.pkttype() == senf::LLSocketAddress::Host ? " Host" :
78             llAddr.pkttype() == senf::LLSocketAddress::Broadcast ? " Broadcast" :
79             llAddr.pkttype() == senf::LLSocketAddress::Multicast ? " Multicast" :
80             llAddr.pkttype() == senf::LLSocketAddress::OtherHost ? " OtherHost" :
81             llAddr.pkttype() == senf::LLSocketAddress::Outgoing ? " Outgoing" :
82             llAddr.pkttype() == senf::LLSocketAddress::Broadcast ? "Broadcast" : "" )
83        << "]";
84     return os;
85 }
86
87 //-/////////////////////////////////////////////////////////////////////////////////////////////////
88 #undef prefix_
89 //#include "LLAddressing.mpp"
90
91 \f
92 // Local Variables:
93 // mode: c++
94 // fill-column: 100
95 // c-file-style: "senf"
96 // indent-tabs-mode: nil
97 // ispell-local-dictionary: "american"
98 // compile-command: "scons -u test"
99 // comment-column: 40
100 // End: