8a60a8fa4b68300f43731a1b50e4deb5176ee4d6
[senf.git] / senf / Socket / Protocols / Raw / LLAddressing.hh
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 public header
30  */
31
32 #ifndef HH_SENF_Socket_Protocols_Raw_LLAddressing_
33 #define HH_SENF_Socket_Protocols_Raw_LLAddressing_ 1
34
35 // Custom includes
36 #include <sys/socket.h>
37 #include <netpacket/packet.h>
38 #include <senf/Socket/Protocols/BSDAddressingPolicy.hh>
39 #include <senf/Socket/Protocols/BSDSocketAddress.hh>
40 #include "MACAddress.hh"
41
42 //#include "LLAddressing.mpp"
43 //#include "LLAddressing.ih"
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 namespace senf {
47
48     /// \addtogroup addr_group
49     //\{
50
51     /** \brief Link local address
52
53         LLSocketAddress wraps the standard sockaddr_ll data type. An LLSocketAddress provides quite
54         some information, only part of which is necessary for sending packets. The LLSocketAddress
55         class only allows changing those fields which need to be changed. The other fields are
56         read-only. They are filled by the operating system when receiving a packet
57
58         \nosubgrouping
59      */
60     class LLSocketAddress
61         : public BSDSocketAddress
62     {
63     public:
64         static short const addressFamily = AF_PACKET;
65
66         /** \brief Valid pkttype() values
67
68             These are the possible values returned by pkttype()
69          */
70         enum PktType { Undefined = 0
71                      , Host      = PACKET_HOST      /**< Packet destined for this host */
72                      , Broadcast = PACKET_BROADCAST /**< Packet sent to the broadcast address */
73                      , Multicast = PACKET_MULTICAST /**< Packet sent to a (link local) multicast
74                                                          address */
75                      , OtherHost = PACKET_OTHERHOST /**< Packet sent to another host (promisc) */
76                      , Outgoing  = PACKET_OUTGOING  /**< Packet sent out from this host */
77         };
78
79         //-////////////////////////////////////////////////////////////////////////
80         ///\name Structors and default members
81         //\{
82
83         LLSocketAddress();              ///< Create empty address
84         explicit LLSocketAddress(unsigned proto, std::string const & iface="");
85                                         ///< Create address for \c bind()
86                                         /**< This constructs an LLSocketAddress valid for calling
87                                              PacketSocketHandle::bind() with.
88                                              \param[in] proto Protocol (Ethertype) to listen for
89                                              \param[in] iface Interface name to bind to */
90         explicit LLSocketAddress(std::string const & iface);
91                                         ///< Create address for \c bind()
92                                         /**< This constructs an LLSocketAddress valid for calling
93                                              \c PacketSocketHandle::bind() with.
94                                              \param[in] iface Interface name to bind to
95                                              \throws UnknownInterfaceException if \a iface is not
96                                                  a valid interface name. */
97
98         // This constructor is for sending packets
99         explicit LLSocketAddress(MACAddress const & addr, std::string const & iface="");
100                                         ///< Create address valid to send raw packets
101                                         /**< Addresses created with this constructor are valid for
102                                              use with \c PacketSocketHandle::sendto() on a \c
103                                              SOCK_DGRAM packet socket.
104                                              \param addr Address to send data to
105                                              \param iface Interface to send packet from
106                                              \throws UnknownInterfaceException if \a iface is not
107                                                  a valid interface name. */
108
109         LLSocketAddress(LLSocketAddress const & other);
110         LLSocketAddress& operator=(LLSocketAddress const & other);
111
112         //\}
113         //-////////////////////////////////////////////////////////////////////////
114
115         MACAddress address() const;     ///< Return address
116         std::string interface() const;  ///< Return interface name
117         unsigned protocol() const;      ///< Return address protocol (ethertype)
118
119         unsigned arptype() const;       ///< Return the arptype field (ARP hardware type)
120         PktType pkttype() const;        ///< Return type of packet
121
122         // The mutating interface is purposely restricted to allow only
123         // changing those members, which are sensible to be changed.
124
125         void address(MACAddress const & addr); ///< Change address
126         void interface(std::string const & iface); ///< Change interface
127                                                    /**< \throws UnknownInterfaceException if \a iface
128                                                              is not a valid interface name. */
129         void protocol(unsigned prot);   ///< Change protocol
130
131         using BSDSocketAddress::sockaddr_p;
132         using BSDSocketAddress::socklen_p;
133
134     private:
135         struct ::sockaddr_ll addr_;
136     };
137
138     //\}
139
140     /// \addtogroup policy_impl_group
141     //\{
142
143     /** \brief Addressing policy supporting link-local addressing
144
145         \par Address Type:
146             LLSocketAddress
147
148         This addressing policy implements generic link local
149         addressing. The predominant type of link local addressing is
150         Ethernet addressing.
151
152         Since the link layer does not support the notion of
153         connections, link local addresses do not support the connect()
154         or peer() members.
155      */
156     struct LLAddressingPolicy
157         : public BSDAddressingPolicy,
158           private BSDAddressingPolicyMixin<LLSocketAddress>
159     {
160         typedef LLSocketAddress Address;
161
162         using BSDAddressingPolicyMixin<LLSocketAddress>::local;
163         using BSDAddressingPolicyMixin<LLSocketAddress>::bind;
164     };
165
166     //\}
167
168     /** \brief Write link layer address
169         \related LLSocketAddress
170      */
171     std::ostream & operator<<(std::ostream & os, LLSocketAddress const & llAddr);
172 }
173
174 //-/////////////////////////////////////////////////////////////////////////////////////////////////
175 #include "LLAddressing.cci"
176 //#include "LLAddressing.ct"
177 //#include "LLAddressing.cti"
178 //#include "LLAddressing.mpp"
179 #endif
180
181 \f
182 // Local Variables:
183 // mode: c++
184 // fill-column: 100
185 // c-file-style: "senf"
186 // indent-tabs-mode: nil
187 // ispell-local-dictionary: "american"
188 // compile-command: "scons -u test"
189 // comment-column: 40
190 // End: