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 #include "ipv4-end-point.h"
22 #include "ns3/packet.h"
23 #include "ns3/log.h"
24 #include "ns3/simulator.h"
25
26 namespace ns3 {
27
28 NS_LOG_COMPONENT_DEFINE ("Ipv4EndPoint");
29
Ipv4EndPoint(Ipv4Address address,uint16_t port)30 Ipv4EndPoint::Ipv4EndPoint (Ipv4Address address, uint16_t port)
31 : m_localAddr (address),
32 m_localPort (port),
33 m_peerAddr (Ipv4Address::GetAny ()),
34 m_peerPort (0),
35 m_rxEnabled (true)
36 {
37 NS_LOG_FUNCTION (this << address << port);
38 }
39
~Ipv4EndPoint()40 Ipv4EndPoint::~Ipv4EndPoint ()
41 {
42 NS_LOG_FUNCTION (this);
43 if (!m_destroyCallback.IsNull ())
44 {
45 m_destroyCallback ();
46 }
47 m_rxCallback.Nullify ();
48 m_icmpCallback.Nullify ();
49 m_destroyCallback.Nullify ();
50 }
51
52 Ipv4Address
GetLocalAddress(void)53 Ipv4EndPoint::GetLocalAddress (void)
54 {
55 NS_LOG_FUNCTION (this);
56 return m_localAddr;
57 }
58
59 void
SetLocalAddress(Ipv4Address address)60 Ipv4EndPoint::SetLocalAddress (Ipv4Address address)
61 {
62 NS_LOG_FUNCTION (this << address);
63 m_localAddr = address;
64 }
65
66 uint16_t
GetLocalPort(void)67 Ipv4EndPoint::GetLocalPort (void)
68 {
69 NS_LOG_FUNCTION (this);
70 return m_localPort;
71 }
72
73 Ipv4Address
GetPeerAddress(void)74 Ipv4EndPoint::GetPeerAddress (void)
75 {
76 NS_LOG_FUNCTION (this);
77 return m_peerAddr;
78 }
79
80 uint16_t
GetPeerPort(void)81 Ipv4EndPoint::GetPeerPort (void)
82 {
83 NS_LOG_FUNCTION (this);
84 return m_peerPort;
85 }
86
87 void
SetPeer(Ipv4Address address,uint16_t port)88 Ipv4EndPoint::SetPeer (Ipv4Address address, uint16_t port)
89 {
90 NS_LOG_FUNCTION (this << address << port);
91 m_peerAddr = address;
92 m_peerPort = port;
93 }
94
95 void
BindToNetDevice(Ptr<NetDevice> netdevice)96 Ipv4EndPoint::BindToNetDevice (Ptr<NetDevice> netdevice)
97 {
98 NS_LOG_FUNCTION (this << netdevice);
99 m_boundnetdevice = netdevice;
100 return;
101 }
102
103 Ptr<NetDevice>
GetBoundNetDevice(void)104 Ipv4EndPoint::GetBoundNetDevice (void)
105 {
106 NS_LOG_FUNCTION (this);
107 return m_boundnetdevice;
108 }
109
110 void
SetRxCallback(Callback<void,Ptr<Packet>,Ipv4Header,uint16_t,Ptr<Ipv4Interface>> callback)111 Ipv4EndPoint::SetRxCallback (Callback<void,Ptr<Packet>, Ipv4Header, uint16_t, Ptr<Ipv4Interface> > callback)
112 {
113 NS_LOG_FUNCTION (this << &callback);
114 m_rxCallback = callback;
115 }
116
117 void
SetIcmpCallback(Callback<void,Ipv4Address,uint8_t,uint8_t,uint8_t,uint32_t> callback)118 Ipv4EndPoint::SetIcmpCallback (Callback<void,Ipv4Address,uint8_t,uint8_t,uint8_t,uint32_t> callback)
119 {
120 NS_LOG_FUNCTION (this << &callback);
121 m_icmpCallback = callback;
122 }
123
124 void
SetDestroyCallback(Callback<void> callback)125 Ipv4EndPoint::SetDestroyCallback (Callback<void> callback)
126 {
127 NS_LOG_FUNCTION (this << &callback);
128 m_destroyCallback = callback;
129 }
130
131 void
ForwardUp(Ptr<Packet> p,const Ipv4Header & header,uint16_t sport,Ptr<Ipv4Interface> incomingInterface)132 Ipv4EndPoint::ForwardUp (Ptr<Packet> p, const Ipv4Header& header, uint16_t sport,
133 Ptr<Ipv4Interface> incomingInterface)
134 {
135 NS_LOG_FUNCTION (this << p << &header << sport << incomingInterface);
136
137 if (!m_rxCallback.IsNull ())
138 {
139 m_rxCallback (p, header, sport, incomingInterface);
140 }
141 }
142
143 void
ForwardIcmp(Ipv4Address icmpSource,uint8_t icmpTtl,uint8_t icmpType,uint8_t icmpCode,uint32_t icmpInfo)144 Ipv4EndPoint::ForwardIcmp (Ipv4Address icmpSource, uint8_t icmpTtl,
145 uint8_t icmpType, uint8_t icmpCode,
146 uint32_t icmpInfo)
147 {
148 NS_LOG_FUNCTION (this << icmpSource << (uint32_t)icmpTtl << (uint32_t)icmpType <<
149 (uint32_t)icmpCode << icmpInfo);
150 if (!m_icmpCallback.IsNull ())
151 {
152 m_icmpCallback (icmpSource, icmpTtl, icmpType, icmpCode, icmpInfo);
153 }
154 }
155
156 void
SetRxEnabled(bool enabled)157 Ipv4EndPoint::SetRxEnabled (bool enabled)
158 {
159 m_rxEnabled = enabled;
160 }
161
162 bool
IsRxEnabled()163 Ipv4EndPoint::IsRxEnabled ()
164 {
165 return m_rxEnabled;
166 }
167
168 } // namespace ns3
169