A Discrete-Event Network Simulator
API
ipv4-end-point.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #include "ipv4-end-point.h"
21 
22 #include "ns3/log.h"
23 #include "ns3/packet.h"
24 #include "ns3/simulator.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("Ipv4EndPoint");
30 
32  : m_localAddr(address),
33  m_localPort(port),
34  m_peerAddr(Ipv4Address::GetAny()),
35  m_peerPort(0),
36  m_rxEnabled(true)
37 {
38  NS_LOG_FUNCTION(this << address << port);
39 }
40 
42 {
43  NS_LOG_FUNCTION(this);
45  {
47  }
48  m_rxCallback.Nullify();
49  m_icmpCallback.Nullify();
51 }
52 
55 {
56  NS_LOG_FUNCTION(this);
57  return m_localAddr;
58 }
59 
60 void
62 {
63  NS_LOG_FUNCTION(this << address);
65 }
66 
67 uint16_t
69 {
70  NS_LOG_FUNCTION(this);
71  return m_localPort;
72 }
73 
76 {
77  NS_LOG_FUNCTION(this);
78  return m_peerAddr;
79 }
80 
81 uint16_t
83 {
84  NS_LOG_FUNCTION(this);
85  return m_peerPort;
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION(this << address << port);
93  m_peerPort = port;
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION(this << netdevice);
100  m_boundnetdevice = netdevice;
101 }
102 
105 {
106  NS_LOG_FUNCTION(this);
107  return m_boundnetdevice;
108 }
109 
110 void
112  Callback<void, Ptr<Packet>, Ipv4Header, uint16_t, Ptr<Ipv4Interface>> callback)
113 {
114  NS_LOG_FUNCTION(this << &callback);
115  m_rxCallback = callback;
116 }
117 
118 void
121 {
122  NS_LOG_FUNCTION(this << &callback);
123  m_icmpCallback = callback;
124 }
125 
126 void
128 {
129  NS_LOG_FUNCTION(this << &callback);
130  m_destroyCallback = callback;
131 }
132 
133 void
135  const Ipv4Header& header,
136  uint16_t sport,
137  Ptr<Ipv4Interface> incomingInterface)
138 {
139  NS_LOG_FUNCTION(this << p << &header << sport << incomingInterface);
140 
141  if (!m_rxCallback.IsNull())
142  {
143  m_rxCallback(p, header, sport, incomingInterface);
144  }
145 }
146 
147 void
149  uint8_t icmpTtl,
150  uint8_t icmpType,
151  uint8_t icmpCode,
152  uint32_t icmpInfo)
153 {
154  NS_LOG_FUNCTION(this << icmpSource << (uint32_t)icmpTtl << (uint32_t)icmpType
155  << (uint32_t)icmpCode << icmpInfo);
156  if (!m_icmpCallback.IsNull())
157  {
158  m_icmpCallback(icmpSource, icmpTtl, icmpType, icmpCode, icmpInfo);
159  }
160 }
161 
162 void
164 {
165  m_rxEnabled = enabled;
166 }
167 
168 bool
170 {
171  return m_rxEnabled;
172 }
173 
174 } // namespace ns3
Callback template class.
Definition: callback.h:438
void Nullify()
Discard the implementation, set it to null.
Definition: callback.h:575
bool IsNull() const
Check for null implementation.
Definition: callback.h:569
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
bool m_rxEnabled
true if the endpoint can receive packets.
void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
Callback< void, Ipv4Address, uint8_t, uint8_t, uint8_t, uint32_t > m_icmpCallback
The ICMPv6 callback.
void SetDestroyCallback(Callback< void > callback)
Set the default destroy callback.
Ipv4Address GetLocalAddress() const
Get the local address.
void SetLocalAddress(Ipv4Address address)
Set the local address.
uint16_t GetPeerPort() const
Get the peer port.
void ForwardUp(Ptr< Packet > p, const Ipv4Header &header, uint16_t sport, Ptr< Ipv4Interface > incomingInterface)
Forward the packet to the upper level.
void ForwardIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo)
Forward the ICMP packet to the upper level.
uint16_t m_localPort
The local port.
Ptr< NetDevice > GetBoundNetDevice() const
Returns socket's bound netdevice, if any.
Ipv4EndPoint(Ipv4Address address, uint16_t port)
Constructor.
uint16_t GetLocalPort() const
Get the local port.
bool IsRxEnabled() const
Checks if the endpoint can receive packets.
void SetRxEnabled(bool enabled)
Enable or Disable the endpoint Rx capability.
Ipv4Address GetPeerAddress() const
Get the peer address.
Ipv4Address m_peerAddr
The peer address.
uint16_t m_peerPort
The peer port.
Ptr< NetDevice > m_boundnetdevice
The NetDevice the EndPoint is bound to (if any).
void SetIcmpCallback(Callback< void, Ipv4Address, uint8_t, uint8_t, uint8_t, uint32_t > callback)
Set the ICMP callback.
void SetPeer(Ipv4Address address, uint16_t port)
Set the peer information (address and port).
Ipv4Address m_localAddr
The local address.
Callback< void > m_destroyCallback
The destroy callback.
Callback< void, Ptr< Packet >, Ipv4Header, uint16_t, Ptr< Ipv4Interface > > m_rxCallback
The RX callback.
void SetRxCallback(Callback< void, Ptr< Packet >, Ipv4Header, uint16_t, Ptr< Ipv4Interface >> callback)
Set the reception callback.
Packet header for IPv4.
Definition: ipv4-header.h:34
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.