1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16 
17 #ifndef NSC_TCP_L4_PROTOCOL_H
18 #define NSC_TCP_L4_PROTOCOL_H
19 
20 #include <stdint.h>
21 
22 #include "ns3/packet.h"
23 #include "ns3/ipv4-address.h"
24 #include "ns3/ptr.h"
25 #include "ns3/object-factory.h"
26 #include "ns3/timer.h"
27 #include "ip-l4-protocol.h"
28 
29 struct INetStack;
30 
31 namespace ns3 {
32 
33 class Node;
34 class Socket;
35 class Ipv4EndPointDemux;
36 class Ipv4Interface;
37 class NscTcpSocketImpl;
38 class Ipv4EndPoint;
39 class NscInterfaceImpl;
40 class NetDevice;
41 
42 /**
43  * \ingroup nsctcp
44  *
45  * \brief Nsc wrapper glue, to interface with the Ipv4 protocol underneath.
46  */
47 class NscTcpL4Protocol : public IpL4Protocol {
48 public:
49   static const uint8_t PROT_NUMBER; //!< protocol number (0x6)
50   /**
51    * \brief Get the type ID.
52    * \return the object TypeId
53    */
54   static TypeId GetTypeId (void);
55 
56   NscTcpL4Protocol ();
57   virtual ~NscTcpL4Protocol ();
58 
59   /**
60    * Set node associated with this stack
61    * \param node the node
62    */
63   void SetNode (Ptr<Node> node);
64 
65   /**
66    * Set the NSC library to be used
67    * \param lib the library path
68    */
69   void SetNscLibrary (const std::string &lib);
70 
71   /**
72    * Get the NSC library being used
73    * \returns the library path
74    */
75   std::string GetNscLibrary (void) const;
76   virtual int GetProtocolNumber (void) const;
77 
78   /**
79    * Get the NSC version
80    * \returns the NSC version
81    */
82   virtual int GetVersion (void) const;
83 
84   /**
85    * \return A smart Socket pointer to a NscTcpSocketImpl, allocated by this instance
86    * of the TCP protocol
87    */
88   Ptr<Socket> CreateSocket (void);
89 
90   /**
91    * \brief Allocate an IPv4 Endpoint
92    * \return the Endpoint
93    */
94   Ipv4EndPoint *Allocate (void);
95   /**
96    * \brief Allocate an IPv4 Endpoint
97    * \param address address to use
98    * \return the Endpoint
99    */
100   Ipv4EndPoint *Allocate (Ipv4Address address);
101   /**
102    * \brief Allocate an IPv4 Endpoint
103    * \param boundNetDevice Bound NetDevice (if any)
104    * \param port port to use
105    * \return the Endpoint
106    */
107   Ipv4EndPoint *Allocate (Ptr<NetDevice> boundNetDevice, uint16_t port);
108   /**
109    * \brief Allocate an IPv4 Endpoint
110    * \param boundNetDevice Bound NetDevice (if any)
111    * \param address address to use
112    * \param port port to use
113    * \return the Endpoint
114    */
115   Ipv4EndPoint *Allocate (Ptr<NetDevice> boundNetDevice, Ipv4Address address, uint16_t port);
116   /**
117    * \brief Allocate an IPv4 Endpoint
118    * \param boundNetDevice Bound NetDevice (if any)
119    * \param localAddress local address to use
120    * \param localPort local port to use
121    * \param peerAddress remote address to use
122    * \param peerPort remote port to use
123    * \return the Endpoint
124    */
125   Ipv4EndPoint *Allocate (Ptr<NetDevice> boundNetDevice,
126                           Ipv4Address localAddress, uint16_t localPort,
127                           Ipv4Address peerAddress, uint16_t peerPort);
128 
129 
130   /**
131    * \brief Remove an IPv4 Endpoint.
132    * \param endPoint the end point to remove
133    */
134   void DeAllocate (Ipv4EndPoint *endPoint);
135 
136   virtual IpL4Protocol::RxStatus Receive (Ptr<Packet> p,
137                                           Ipv4Header const &header,
138                                           Ptr<Ipv4Interface> incomingInterface);
139   virtual IpL4Protocol::RxStatus Receive (Ptr<Packet> p,
140                                           Ipv6Header const &header,
141                                           Ptr<Ipv6Interface> interface);
142 
143   // From IpL4Protocol
144   virtual void SetDownTarget (IpL4Protocol::DownTargetCallback cb);
145   virtual void SetDownTarget6 (IpL4Protocol::DownTargetCallback6 cb);
146   // From IpL4Protocol
147   virtual IpL4Protocol::DownTargetCallback GetDownTarget (void) const;
148   virtual IpL4Protocol::DownTargetCallback6 GetDownTarget6 (void) const;
149 protected:
150   virtual void DoDispose (void);
151   virtual void NotifyNewAggregate ();
152 private:
153   /**
154    * \brief Copy constructor
155    *
156    * Defined and not implemented to avoid misuse
157    */
158   NscTcpL4Protocol (NscTcpL4Protocol const &);
159   /**
160    * \brief Copy constructor
161    *
162    * Defined and not implemented to avoid misuse
163    * \returns
164    */
165   NscTcpL4Protocol& operator= (NscTcpL4Protocol const &);
166 
167   // NSC callbacks.
168   // NSC invokes these hooks to interact with the simulator.
169   // In any case, these methods are only to be called by NSC.
170 
171   /**
172    * \brief Invoked by NSCs 'ethernet driver' to re-inject a packet into ns-3.
173    *
174    * A packet is an octet soup consisting of an IP Header, TCP Header
175    * and user payload, if any
176    *
177    * \param data the data
178    * \param datalen the data length
179    */
180   void send_callback (const void *data, int datalen);
181   /**
182    * \brief Called by the NSC stack whenever something of interest has happened
183    *
184    * Examples: when data arrives on a socket, a listen socket
185    * has a new connection pending, etc.
186    */
187   void wakeup ();
188   /**
189    * \brief Called by the Linux stack RNG initialization
190    *
191    * Its also used by the cradle code to add a timestamp to
192    * printk/printf/debug output.
193    * \param sec seconds
194    * \param usec microseconds
195    */
196   void gettime (unsigned int *sec, unsigned int *usec);
197   /**
198    * \brief Add an interface
199    *
200    * Actually NSC only supports one interface per node (\bugid{1398})
201    */
202   void AddInterface (void);
203 
204   /**
205    * \brief Provide a "soft" interrupt to NSC
206    */
207   void SoftInterrupt (void);
208   /**
209    * \brief NscInterfaceImpl friend class.
210    * \relates NscInterfaceImpl
211    */
212   friend class NscInterfaceImpl;
213   /**
214    * \brief NscTcpSocketImpl friend class.
215    * \relates NscTcpSocketImpl
216    */
217   friend class NscTcpSocketImpl;
218 
219   Ptr<Node> m_node; //!< the node this stack is associated with
220   Ipv4EndPointDemux *m_endPoints; //!< A list of IPv4 end points.
221   INetStack* m_nscStack; //!< the NSC stack.
222   NscInterfaceImpl *m_nscInterface; //!< the NSC Interface.
223   void *m_dlopenHandle; //!< dynamic library handle.
224   std::string m_nscLibrary; //!< path to the NSC library.
225   Timer m_softTimer; //!< Soft interrupt timer
226   std::vector<Ptr<NscTcpSocketImpl> > m_sockets; //!< list of sockets
227   IpL4Protocol::DownTargetCallback m_downTarget; //!< Callback to send packets over IPv4
228 };
229 
230 } // namespace ns3
231 
232 #endif /* NSC_TCP_L4_PROTOCOL_H */
233