1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef IPV4_END_POINT_H
22 #define IPV4_END_POINT_H
23 
24 #include <stdint.h>
25 #include "ns3/ipv4-address.h"
26 #include "ns3/callback.h"
27 #include "ns3/net-device.h"
28 #include "ns3/ipv4-header.h"
29 #include "ns3/ipv4-interface.h"
30 
31 namespace ns3 {
32 
33 class Header;
34 class Packet;
35 
36 /**
37  * \ingroup ipv4
38  *
39  * \brief A representation of an internet endpoint/connection
40  *
41  * This class provides an internet four-tuple (source and destination ports
42  * and addresses).  These are used in the ns3::Ipv4EndPointDemux as targets
43  * of lookups.  The class also has a callback for notification to higher
44  * layers that a packet from a lower layer was received.  In the ns3
45  * internet-stack, these notifications are automatically registered to be
46  * received by the corresponding socket.
47  */
48 
49 class Ipv4EndPoint {
50 public:
51   /**
52    * \brief Constructor.
53    * \param address the IPv4 address
54    * \param port the port
55    */
56   Ipv4EndPoint (Ipv4Address address, uint16_t port);
57   ~Ipv4EndPoint ();
58 
59   /**
60    * \brief Get the local address.
61    * \return the local address
62    */
63   Ipv4Address GetLocalAddress (void);
64 
65   /**
66    * \brief Set the local address.
67    * \param address the address to set
68    */
69   void SetLocalAddress (Ipv4Address address);
70 
71   /**
72    * \brief Get the local port.
73    * \return the local port
74    */
75   uint16_t GetLocalPort (void);
76 
77   /**
78    * \brief Get the peer address.
79    * \return the peer address
80    */
81   Ipv4Address GetPeerAddress (void);
82 
83   /**
84    * \brief Get the peer port.
85    * \return the peer port
86    */
87   uint16_t GetPeerPort (void);
88 
89   /**
90    * \brief Set the peer information (address and port).
91    * \param address peer address
92    * \param port peer port
93    */
94   void SetPeer (Ipv4Address address, uint16_t port);
95 
96   /**
97    * \brief Bind a socket to specific device.
98    *
99    * This method corresponds to using setsockopt() SO_BINDTODEVICE
100    * of real network or BSD sockets.   If set on a socket, this option will
101    * force packets to leave the bound device regardless of the device that
102    * IP routing would naturally choose.  In the receive direction, only
103    * packets received from the bound interface will be delivered.
104    *
105    * This option has no particular relationship to binding sockets to
106    * an address via Socket::Bind ().  It is possible to bind sockets to a
107    * specific IP address on the bound interface by calling both
108    * Socket::Bind (address) and Socket::BindToNetDevice (device), but it
109    * is also possible to bind to mismatching device and address, even if
110    * the socket can not receive any packets as a result.
111    *
112    * \param netdevice Pointer to Netdevice of desired interface
113    */
114   void BindToNetDevice (Ptr<NetDevice> netdevice);
115 
116   /**
117    * \brief Returns socket's bound netdevice, if any.
118    *
119    * This method corresponds to using getsockopt() SO_BINDTODEVICE
120    * of real network or BSD sockets.
121    *
122    *
123    * \returns Pointer to interface.
124    */
125   Ptr<NetDevice> GetBoundNetDevice (void);
126 
127   // Called from socket implementations to get notified about important events.
128   /**
129    * \brief Set the reception callback.
130    * \param callback callback function
131    */
132   void SetRxCallback (Callback<void,Ptr<Packet>, Ipv4Header, uint16_t, Ptr<Ipv4Interface> > callback);
133   /**
134    * \brief Set the ICMP callback.
135    * \param callback callback function
136    */
137   void SetIcmpCallback (Callback<void,Ipv4Address,uint8_t,uint8_t,uint8_t,uint32_t> callback);
138   /**
139    * \brief Set the default destroy callback.
140    * \param callback callback function
141    */
142   void SetDestroyCallback (Callback<void> callback);
143 
144   /**
145    * \brief Forward the packet to the upper level.
146    *
147    * Called from an L4Protocol implementation to notify an endpoint of a
148    * packet reception.
149    * \param p the packet
150    * \param header the packet header
151    * \param sport source port
152    * \param incomingInterface incoming interface
153    */
154   void ForwardUp (Ptr<Packet> p, const Ipv4Header& header, uint16_t sport,
155                   Ptr<Ipv4Interface> incomingInterface);
156 
157   /**
158    * \brief Forward the ICMP packet to the upper level.
159    *
160    * Called from an L4Protocol implementation to notify an endpoint of
161    * an icmp message reception.
162    *
163    * \param icmpSource source IP address
164    * \param icmpTtl time-to-live
165    * \param icmpType ICMP type
166    * \param icmpCode ICMP code
167    * \param icmpInfo ICMP info
168    */
169   void ForwardIcmp (Ipv4Address icmpSource, uint8_t icmpTtl,
170                     uint8_t icmpType, uint8_t icmpCode,
171                     uint32_t icmpInfo);
172 
173   /**
174    * \brief Enable or Disable the endpoint Rx capability.
175    * \param enabled true if Rx is enabled
176    */
177   void SetRxEnabled (bool enabled);
178 
179   /**
180    * \brief Checks if the endpoint can receive packets.
181    * \returns true if the endpoint can receive packets.
182    */
183   bool IsRxEnabled (void);
184 
185 private:
186   /**
187    * \brief The local address.
188    */
189   Ipv4Address m_localAddr;
190 
191   /**
192    * \brief The local port.
193    */
194   uint16_t m_localPort;
195 
196   /**
197    * \brief The peer address.
198    */
199   Ipv4Address m_peerAddr;
200 
201   /**
202    * \brief The peer port.
203    */
204   uint16_t m_peerPort;
205 
206   /**
207    * \brief The NetDevice the EndPoint is bound to (if any).
208    */
209   Ptr<NetDevice> m_boundnetdevice;
210 
211   /**
212    * \brief The RX callback.
213    */
214   Callback<void,Ptr<Packet>, Ipv4Header, uint16_t, Ptr<Ipv4Interface> > m_rxCallback;
215 
216   /**
217    * \brief The ICMPv6 callback.
218    */
219   Callback<void,Ipv4Address,uint8_t,uint8_t,uint8_t,uint32_t> m_icmpCallback;
220 
221   /**
222    * \brief The destroy callback.
223    */
224   Callback<void> m_destroyCallback;
225 
226   /**
227    * \brief true if the endpoint can receive packets.
228    */
229   bool m_rxEnabled;
230 };
231 
232 } // namespace ns3
233 
234 
235 #endif /* IPV4_END_POINT_H */
236