|
|
|
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 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: Hossam Khader <hossamkhader@gmail.com> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "udp-application.h" |
| 21 |
#include "ns3/log.h" |
| 22 |
#include "ns3/socket-factory.h" |
| 23 |
#include "ns3/packet-socket-address.h" |
| 24 |
#include "ns3/packet-socket.h" |
| 25 |
#include "ns3/ipv4-l3-protocol.h" |
| 26 |
#include "ns3/ipv6-l3-protocol.h" |
| 27 |
|
| 28 |
namespace ns3 { |
| 29 |
|
| 30 |
NS_LOG_COMPONENT_DEFINE ("UdpApplication"); |
| 31 |
|
| 32 |
NS_OBJECT_ENSURE_REGISTERED (UdpApplication); |
| 33 |
|
| 34 |
TypeId |
| 35 |
UdpApplication::GetTypeId (void) |
| 36 |
{ |
| 37 |
static TypeId tid = TypeId ("ns3::UdpApplication") |
| 38 |
.SetParent<Application> () |
| 39 |
.SetGroupName ("Applications") |
| 40 |
.AddConstructor<UdpApplication> () |
| 41 |
; |
| 42 |
return tid; |
| 43 |
} |
| 44 |
|
| 45 |
UdpApplication::UdpApplication () |
| 46 |
{ |
| 47 |
NS_LOG_FUNCTION (this); |
| 48 |
m_socket = NULL; |
| 49 |
m_packetReceivedCallback.Nullify (); |
| 50 |
} |
| 51 |
UdpApplication::~UdpApplication () |
| 52 |
{ |
| 53 |
NS_LOG_FUNCTION (this); |
| 54 |
} |
| 55 |
|
| 56 |
void |
| 57 |
UdpApplication::DoDispose (void) |
| 58 |
{ |
| 59 |
NS_LOG_FUNCTION (this); |
| 60 |
m_socket = NULL; |
| 61 |
m_packetReceivedCallback.Nullify (); |
| 62 |
Application::DoDispose (); |
| 63 |
} |
| 64 |
|
| 65 |
uint32_t |
| 66 |
UdpApplication::GetApplicationId (void) const |
| 67 |
{ |
| 68 |
NS_LOG_FUNCTION (this); |
| 69 |
Ptr<Node> node = GetNode (); |
| 70 |
for (uint32_t i = 0; i < node->GetNApplications (); ++i) |
| 71 |
{ |
| 72 |
if (node->GetApplication (i) == this) |
| 73 |
{ |
| 74 |
return i; |
| 75 |
} |
| 76 |
} |
| 77 |
NS_ASSERT_MSG (false, "forgot to add application to node"); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
void |
| 83 |
UdpApplication::Send (Address dst, const Ptr<Packet> packet) |
| 84 |
{ |
| 85 |
NS_LOG_FUNCTION (this); |
| 86 |
if(m_socket == NULL) |
| 87 |
{ |
| 88 |
StartApplication (); |
| 89 |
} |
| 90 |
|
| 91 |
if(Ipv6Address::IsMatchingType (dst)) |
| 92 |
{ |
| 93 |
Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), m_port); |
| 94 |
m_socket->SendTo (packet, 0, ipv6_destination); |
| 95 |
} |
| 96 |
else if(Ipv4Address::IsMatchingType (dst)) |
| 97 |
{ |
| 98 |
InetSocketAddress ipv4_destination = InetSocketAddress (Ipv4Address::ConvertFrom (dst), m_port); |
| 99 |
m_socket->SendTo (packet, 0, ipv4_destination); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
UdpApplication::Receive (Ptr<Socket> socket) |
| 105 |
{ |
| 106 |
NS_LOG_FUNCTION (this << socket); |
| 107 |
NS_ASSERT (m_socket != NULL); |
| 108 |
Address srcAddress; |
| 109 |
while (m_socket->GetRxAvailable () > 0) |
| 110 |
{ |
| 111 |
Ptr<Packet> packet = m_socket->RecvFrom (srcAddress); |
| 112 |
if(!m_packetReceivedCallback.IsNull ()) |
| 113 |
{ |
| 114 |
if(Inet6SocketAddress::IsMatchingType (srcAddress)) |
| 115 |
{ |
| 116 |
m_packetReceivedCallback (Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 (), packet); |
| 117 |
} |
| 118 |
else if(InetSocketAddress::IsMatchingType (srcAddress)) |
| 119 |
{ |
| 120 |
m_packetReceivedCallback (InetSocketAddress::ConvertFrom (srcAddress).GetIpv4 (), packet); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void |
| 127 |
UdpApplication::StartApplication (void) |
| 128 |
{ |
| 129 |
NS_LOG_FUNCTION (this); |
| 130 |
if (!m_socket) |
| 131 |
{ |
| 132 |
m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::UdpSocketFactory")); |
| 133 |
} |
| 134 |
|
| 135 |
if(GetNode ()->GetObject<Ipv6> () != NULL) |
| 136 |
{ |
| 137 |
Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), m_port); |
| 138 |
m_socket->Bind (ipv6_local); |
| 139 |
} |
| 140 |
|
| 141 |
if(GetNode ()->GetObject<Ipv4> () != NULL) |
| 142 |
{ |
| 143 |
InetSocketAddress ipv4_local = InetSocketAddress (Ipv4Address::GetAny (), m_port); |
| 144 |
m_socket->Bind (ipv4_local); |
| 145 |
} |
| 146 |
m_socket->SetRecvCallback (MakeCallback (&UdpApplication::Receive, this)); |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
void |
| 151 |
UdpApplication::StopApplication (void) |
| 152 |
{ |
| 153 |
NS_LOG_FUNCTION (this); |
| 154 |
if(m_socket != NULL) |
| 155 |
{ |
| 156 |
m_socket->Close (); |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
NS_LOG_WARN ("UdpApplication found null socket to close in StopApplication"); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
UdpApplication::SetPacketReceivedCallback (PacketReceivedCallback callback) |
| 166 |
{ |
| 167 |
NS_LOG_FUNCTION (this); |
| 168 |
if (callback.IsNull ()) |
| 169 |
{ |
| 170 |
NS_LOG_DEBUG ("UdpApplication:Setting NULL packet received callback!"); |
| 171 |
} |
| 172 |
m_packetReceivedCallback = callback; |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
UdpApplication::SetPort (uint16_t port) |
| 177 |
{ |
| 178 |
m_port = port; |
| 179 |
} |
| 180 |
|
| 181 |
uint16_t |
| 182 |
UdpApplication::GetPort () const |
| 183 |
{ |
| 184 |
return m_port; |
| 185 |
} |
| 186 |
|
| 187 |
} // namespace ns3 |