1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #ifndef IPV6_RAW_SOCKET_IMPL_H
22 #define IPV6_RAW_SOCKET_IMPL_H
23 
24 #include <list>
25 
26 #include "ns3/socket.h"
27 #include "ns3/ipv6-address.h"
28 #include "ns3/ipv6-header.h"
29 
30 namespace ns3
31 {
32 
33 class NetDevice;
34 class Node;
35 
36 /**
37  * \ingroup socket
38  * \ingroup ipv6
39  *
40  * \brief IPv6 raw socket.
41  *
42  * A RAW Socket typically is used to access specific IP layers not usually
43  * available through L4 sockets, e.g., ICMP. The implementer should take
44  * particular care to define the Ipv6RawSocketImpl Attributes, and in
45  * particular the Protocol attribute.
46  * Not setting it will result in a zero protocol at IP level (corresponding
47  * to the HopByHop IPv6 Extension header, i.e., Ipv6ExtensionHopByHopHeader)
48  * when sending data through the socket, which is probably not the intended
49  * behavior.
50  *
51  * A correct example is (from src/applications/model/radvd.cc):
52  * \code
53    if (!m_socket)
54      {
55        TypeId tid = TypeId::LookupByName ("ns3::Ipv6RawSocketFactory");
56        m_socket = Socket::CreateSocket (GetNode (), tid);
57 
58        NS_ASSERT (m_socket);
59 
60        m_socket->SetAttribute ("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
61        m_socket->SetRecvCallback (MakeCallback (&Radvd::HandleRead, this));
62      }
63  * \endcode
64  *
65  */
66 class Ipv6RawSocketImpl : public Socket
67 {
68 public:
69   /**
70    * \brief Get the type ID of this class.
71    * \return type ID
72    */
73   static TypeId GetTypeId ();
74 
75   Ipv6RawSocketImpl ();
76   virtual ~Ipv6RawSocketImpl ();
77 
78   /**
79    * \brief Set the node associated with this socket.
80    * \param node node to set
81    */
82   void SetNode (Ptr<Node> node);
83 
84   virtual enum Socket::SocketErrno GetErrno () const;
85 
86   /**
87    * \brief Get socket type (NS3_SOCK_RAW)
88    * \return socket type
89    */
90   virtual enum Socket::SocketType GetSocketType () const;
91 
92   virtual Ptr<Node> GetNode () const;
93 
94   virtual int Bind (const Address& address);
95   virtual int Bind ();
96   virtual int Bind6 ();
97 
98   virtual int GetSockName (Address& address) const;
99   virtual int GetPeerName (Address& address) const;
100 
101   virtual int Close ();
102   virtual int ShutdownSend ();
103   virtual int ShutdownRecv ();
104   virtual int Connect (const Address& address);
105   virtual int Listen ();
106   virtual uint32_t GetTxAvailable () const;
107   virtual uint32_t GetRxAvailable () const;
108   virtual int Send (Ptr<Packet> p, uint32_t flags);
109   virtual int SendTo (Ptr<Packet> p, uint32_t flags, const Address& toAddress);
110   virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags);
111   virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags, Address& fromAddress);
112   virtual void Ipv6JoinGroup (Ipv6Address address, Socket::Ipv6MulticastFilterMode filterMode, std::vector<Ipv6Address> sourceAddresses);
113 
114   /**
115    * \brief Set protocol field.
116    * \param protocol protocol to set
117    */
118   void SetProtocol (uint16_t protocol);
119 
120   /**
121    * \brief Forward up to receive method.
122    * \param p packet
123    * \param hdr IPv6 header
124    * \param device device
125    * \return true if forwarded, false otherwise
126    */
127   bool ForwardUp (Ptr<const Packet> p, Ipv6Header hdr, Ptr<NetDevice> device);
128 
129   virtual bool SetAllowBroadcast (bool allowBroadcast);
130   virtual bool GetAllowBroadcast () const;
131 
132   /**
133    * \brief Clean the ICMPv6 filter structure
134    */
135   void Icmpv6FilterSetPassAll();
136 
137   /**
138    * \brief Set the filter to block all the ICMPv6 types
139    */
140   void Icmpv6FilterSetBlockAll();
141 
142   /**
143    * \brief Set the filter to pass one ICMPv6 type
144    * \param type the ICMPv6 type to pass
145    */
146   void Icmpv6FilterSetPass(uint8_t type);
147 
148   /**
149    * \brief Set the filter to block one ICMPv6 type
150    * \param type the ICMPv6 type to block
151    */
152   void Icmpv6FilterSetBlock(uint8_t type);
153 
154   /**
155    * \brief Ask the filter about the status of one ICMPv6 type
156    * \param type the ICMPv6 type
157    * \return true if the ICMP type is passing through
158    */
159   bool Icmpv6FilterWillPass(uint8_t type);
160 
161   /**
162    * \brief Ask the filter about the status of one ICMPv6 type
163    * \param type the ICMPv6 type
164    * \return true if the ICMP type is being blocked
165    */
166   bool Icmpv6FilterWillBlock(uint8_t type);
167 
168 
169 private:
170   /**
171    * \brief IPv6 raw data and additional information.
172    */
173   typedef struct
174   {
175     Ptr<Packet> packet;   /**< Packet data */
176     Ipv6Address fromIp;   /**< Source address */
177     uint16_t fromProtocol;   /**< Protocol used */
178   } Data;
179 
180   /**
181    * \brief Dispose object.
182    */
183   virtual void DoDispose ();
184 
185   /**
186    * \brief Last error number.
187    */
188   mutable enum Socket::SocketErrno m_err;
189 
190   /**
191    * \brief Node.
192    */
193   Ptr<Node> m_node;
194 
195   /**
196    * \brief Source address.
197    */
198   Ipv6Address m_src;
199 
200   /**
201    * \brief Destination address.
202    */
203   Ipv6Address m_dst;
204 
205   /**
206    * \brief Protocol.
207    */
208   uint16_t m_protocol;
209 
210   /**
211    * \brief Packet waiting to be processed.
212    */
213   std::list<Data> m_data;
214 
215   /**
216    * \brief Flag to shutdown send capability.
217    */
218   bool m_shutdownSend;
219 
220   /**
221    * \brief Flag to shutdown receive capability.
222    */
223   bool m_shutdownRecv;
224 
225   /**
226    * \brief Struct to hold the ICMPv6 filter
227    */
228   typedef struct
229   {
230     uint32_t icmpv6Filt[8]; //!< ICMPv6 filter specification
231   } Icmpv6Filter;
232 
233   /**
234    * \brief ICMPv6 filter.
235    */
236   Icmpv6Filter m_icmpFilter;
237 };
238 
239 } /* namespace ns3 */
240 
241 #endif /* IPV6_RAW_SOCKET_IMPL_H */
242 
243