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