2007-04-30 16:23:10 +02:00
|
|
|
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2007 INRIA
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation;
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
|
|
|
|
*/
|
2007-06-04 16:21:05 +02:00
|
|
|
#include "ns3/node.h"
|
2007-07-30 13:29:36 +02:00
|
|
|
#include "ns3/inet-socket-address.h"
|
2007-09-03 23:32:23 -07:00
|
|
|
#include "ns3/ipv4-route.h"
|
|
|
|
|
#include "ns3/ipv4.h"
|
2007-04-30 16:23:10 +02:00
|
|
|
#include "udp-socket.h"
|
2007-06-04 17:18:02 +02:00
|
|
|
#include "udp-l4-protocol.h"
|
2007-05-02 19:14:34 +02:00
|
|
|
#include "ipv4-end-point.h"
|
2007-04-30 16:23:10 +02:00
|
|
|
#include "ipv4-l4-demux.h"
|
|
|
|
|
|
|
|
|
|
namespace ns3 {
|
|
|
|
|
|
2007-06-04 17:22:52 +02:00
|
|
|
UdpSocket::UdpSocket (Ptr<Node> node, Ptr<UdpL4Protocol> udp)
|
2007-04-30 16:23:10 +02:00
|
|
|
: m_endPoint (0),
|
|
|
|
|
m_node (node),
|
2007-05-03 14:23:41 +02:00
|
|
|
m_udp (udp),
|
2007-05-17 15:50:20 +01:00
|
|
|
m_errno (ERROR_NOTERROR),
|
2007-04-30 16:23:10 +02:00
|
|
|
m_shutdownSend (false),
|
|
|
|
|
m_shutdownRecv (false),
|
|
|
|
|
m_connected (false)
|
2007-05-10 20:19:26 +02:00
|
|
|
{}
|
2007-04-30 16:23:10 +02:00
|
|
|
UdpSocket::~UdpSocket ()
|
|
|
|
|
{
|
2007-05-10 07:51:59 +02:00
|
|
|
m_node = 0;
|
2007-05-03 13:11:50 +02:00
|
|
|
if (m_endPoint != 0)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_udp != 0);
|
|
|
|
|
/**
|
|
|
|
|
* Note that this piece of code is a bit tricky:
|
|
|
|
|
* when DeAllocate is called, it will call into
|
|
|
|
|
* Ipv4EndPointDemux::Deallocate which triggers
|
|
|
|
|
* a delete of the associated endPoint which triggers
|
|
|
|
|
* in turn a call to the method ::Destroy below
|
|
|
|
|
* will will zero the m_endPoint field.
|
|
|
|
|
*/
|
|
|
|
|
NS_ASSERT (m_endPoint != 0);
|
|
|
|
|
m_udp->DeAllocate (m_endPoint);
|
|
|
|
|
NS_ASSERT (m_endPoint == 0);
|
|
|
|
|
}
|
2007-05-10 20:19:26 +02:00
|
|
|
m_udp = 0;
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
2007-08-01 18:48:24 +02:00
|
|
|
enum Socket::SocketErrno
|
|
|
|
|
UdpSocket::GetErrno (void) const
|
|
|
|
|
{
|
|
|
|
|
return m_errno;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-04 16:17:01 +02:00
|
|
|
Ptr<Node>
|
|
|
|
|
UdpSocket::GetNode (void) const
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
|
|
|
|
return m_node;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-02 19:14:34 +02:00
|
|
|
void
|
2007-05-02 22:32:25 +02:00
|
|
|
UdpSocket::Destroy (void)
|
2007-05-02 19:14:34 +02:00
|
|
|
{
|
2007-05-10 07:51:59 +02:00
|
|
|
m_node = 0;
|
2007-05-02 19:14:34 +02:00
|
|
|
m_endPoint = 0;
|
2007-05-10 20:19:26 +02:00
|
|
|
m_udp = 0;
|
2007-05-02 19:14:34 +02:00
|
|
|
}
|
2007-04-30 16:23:10 +02:00
|
|
|
int
|
2007-05-02 19:14:34 +02:00
|
|
|
UdpSocket::FinishBind (void)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
|
|
|
|
if (m_endPoint == 0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2007-07-31 10:47:29 +02:00
|
|
|
m_endPoint->SetRxCallback (MakeCallback (&UdpSocket::ForwardUp, this));
|
|
|
|
|
m_endPoint->SetDestroyCallback (MakeCallback (&UdpSocket::Destroy, this));
|
2007-04-30 16:23:10 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-05-02 19:14:34 +02:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
UdpSocket::Bind (void)
|
|
|
|
|
{
|
2007-05-02 23:42:22 +02:00
|
|
|
m_endPoint = m_udp->Allocate ();
|
2007-05-02 19:14:34 +02:00
|
|
|
return FinishBind ();
|
|
|
|
|
}
|
2007-04-30 16:23:10 +02:00
|
|
|
int
|
2007-07-27 08:23:20 +02:00
|
|
|
UdpSocket::Bind (const Address &address)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2007-07-30 13:55:28 +02:00
|
|
|
if (!InetSocketAddress::IsMatchingType (address))
|
|
|
|
|
{
|
|
|
|
|
return ERROR_INVAL;
|
|
|
|
|
}
|
2007-07-30 13:29:36 +02:00
|
|
|
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
|
2007-07-27 08:23:20 +02:00
|
|
|
Ipv4Address ipv4 = transport.GetIpv4 ();
|
|
|
|
|
uint16_t port = transport.GetPort ();
|
|
|
|
|
if (ipv4 == Ipv4Address::GetAny () && port == 0)
|
|
|
|
|
{
|
|
|
|
|
m_endPoint = m_udp->Allocate ();
|
|
|
|
|
}
|
|
|
|
|
else if (ipv4 == Ipv4Address::GetAny () && port != 0)
|
|
|
|
|
{
|
|
|
|
|
m_endPoint = m_udp->Allocate (port);
|
|
|
|
|
}
|
|
|
|
|
else if (ipv4 != Ipv4Address::GetAny () && port == 0)
|
|
|
|
|
{
|
|
|
|
|
m_endPoint = m_udp->Allocate (ipv4);
|
|
|
|
|
}
|
|
|
|
|
else if (ipv4 != Ipv4Address::GetAny () && port != 0)
|
|
|
|
|
{
|
|
|
|
|
m_endPoint = m_udp->Allocate (ipv4, port);
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-02 19:14:34 +02:00
|
|
|
return FinishBind ();
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
UdpSocket::ShutdownSend (void)
|
|
|
|
|
{
|
|
|
|
|
m_shutdownSend = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int
|
|
|
|
|
UdpSocket::ShutdownRecv (void)
|
|
|
|
|
{
|
|
|
|
|
m_shutdownRecv = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-01 08:58:18 +02:00
|
|
|
int
|
2007-08-01 18:48:24 +02:00
|
|
|
UdpSocket::Close(void)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2007-08-01 18:48:24 +02:00
|
|
|
NotifyCloseCompleted ();
|
2007-08-01 08:58:18 +02:00
|
|
|
return 0;
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-08-01 18:48:24 +02:00
|
|
|
|
2007-08-01 08:58:18 +02:00
|
|
|
int
|
2007-08-01 18:48:24 +02:00
|
|
|
UdpSocket::Connect(const Address & address)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2007-09-03 23:32:23 -07:00
|
|
|
Ipv4Route routeToDest;
|
2007-07-30 13:29:36 +02:00
|
|
|
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
|
2007-07-27 08:23:20 +02:00
|
|
|
m_defaultAddress = transport.GetIpv4 ();
|
|
|
|
|
m_defaultPort = transport.GetPort ();
|
2007-09-03 23:32:23 -07:00
|
|
|
if (m_defaultAddress.IsBroadcast () )
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT_MSG(false, "UdpSocket::Connect, can't connect to broadcast");
|
|
|
|
|
}
|
2007-08-01 18:48:24 +02:00
|
|
|
NotifyConnectionSucceeded ();
|
2007-04-30 16:23:10 +02:00
|
|
|
m_connected = true;
|
2007-09-03 23:32:23 -07:00
|
|
|
if (GetIpv4RouteToDestination (m_node, routeToDest, m_defaultAddress) )
|
|
|
|
|
{
|
|
|
|
|
uint32_t localIfIndex = routeToDest.GetInterface ();
|
|
|
|
|
Ptr<Ipv4> ipv4 = m_node->QueryInterface<Ipv4> (Ipv4::iid);
|
|
|
|
|
m_endPoint->SetLocalAddress (ipv4->GetAddress(localIfIndex) );
|
|
|
|
|
}
|
2007-08-01 08:58:18 +02:00
|
|
|
return 0;
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-03 23:32:23 -07:00
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
int
|
2007-08-29 09:35:53 +02:00
|
|
|
UdpSocket::Send (const Packet &p)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
|
|
|
|
if (!m_connected)
|
|
|
|
|
{
|
2007-05-17 15:50:20 +01:00
|
|
|
m_errno = ERROR_NOTCONN;
|
2007-04-30 16:23:10 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2007-09-03 23:32:23 -07:00
|
|
|
return DoSend (p);
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-03 23:32:23 -07:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
UdpSocket::DoSend (const Packet &p)
|
|
|
|
|
{
|
|
|
|
|
if (m_endPoint == 0)
|
|
|
|
|
{
|
|
|
|
|
if (Bind () == -1)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_endPoint == 0);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
NS_ASSERT (m_endPoint != 0);
|
|
|
|
|
}
|
|
|
|
|
if (m_shutdownSend)
|
|
|
|
|
{
|
|
|
|
|
m_errno = ERROR_SHUTDOWN;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
m_udp->Send (p, m_endPoint->GetLocalAddress (), m_defaultAddress,
|
|
|
|
|
m_endPoint->GetLocalPort (), m_defaultPort);
|
|
|
|
|
NotifyDataSent (p.GetSize ());
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
int
|
2007-08-29 09:35:53 +02:00
|
|
|
UdpSocket::DoSendTo (const Packet &p, const Address &address)
|
2007-07-27 08:23:20 +02:00
|
|
|
{
|
2007-07-30 13:29:36 +02:00
|
|
|
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
|
2007-07-27 08:23:20 +02:00
|
|
|
Ipv4Address ipv4 = transport.GetIpv4 ();
|
|
|
|
|
uint16_t port = transport.GetPort ();
|
2007-08-01 18:48:24 +02:00
|
|
|
return DoSendTo (p, ipv4, port);
|
2007-07-27 08:23:20 +02:00
|
|
|
}
|
|
|
|
|
int
|
2007-09-03 23:32:23 -07:00
|
|
|
UdpSocket::DoSendTo (const Packet &p, Ipv4Address dest, uint16_t port)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
|
|
|
|
if (m_endPoint == 0)
|
|
|
|
|
{
|
|
|
|
|
if (Bind () == -1)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_endPoint == 0);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
NS_ASSERT (m_endPoint != 0);
|
|
|
|
|
}
|
|
|
|
|
if (m_shutdownSend)
|
|
|
|
|
{
|
2007-05-17 15:50:20 +01:00
|
|
|
m_errno = ERROR_SHUTDOWN;
|
2007-04-30 16:23:10 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2007-09-03 23:32:23 -07:00
|
|
|
Ipv4Route routeToDest;
|
|
|
|
|
if (GetIpv4RouteToDestination (m_node, routeToDest, dest) )
|
|
|
|
|
{
|
|
|
|
|
uint32_t localIfIndex = routeToDest.GetInterface ();
|
|
|
|
|
Ptr<Ipv4> ipv4 = m_node->QueryInterface<Ipv4> (Ipv4::iid);
|
|
|
|
|
m_udp->Send (p, ipv4->GetAddress (localIfIndex), dest,
|
2007-07-27 08:23:20 +02:00
|
|
|
m_endPoint->GetLocalPort (), port);
|
2007-09-03 23:32:23 -07:00
|
|
|
NotifyDataSent (p.GetSize ());
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_errno = ERROR_NOROUTETOHOST;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2007-04-30 16:23:10 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-09-03 23:32:23 -07:00
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
int
|
2007-08-29 09:35:53 +02:00
|
|
|
UdpSocket::SendTo(const Address &address, const Packet &p)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2007-07-30 13:29:36 +02:00
|
|
|
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
|
2007-07-27 08:23:20 +02:00
|
|
|
Ipv4Address ipv4 = transport.GetIpv4 ();
|
|
|
|
|
uint16_t port = transport.GetPort ();
|
2007-08-01 18:48:24 +02:00
|
|
|
return DoSendTo (p, ipv4, port);
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-29 09:35:53 +02:00
|
|
|
UdpSocket::ForwardUp (const Packet &packet, Ipv4Address ipv4, uint16_t port)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
|
|
|
|
if (m_shutdownRecv)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-07-27 08:23:20 +02:00
|
|
|
|
2007-07-30 14:07:39 +02:00
|
|
|
Address address = InetSocketAddress (ipv4, port);
|
2007-08-29 09:35:53 +02:00
|
|
|
Packet p = packet;
|
|
|
|
|
NotifyDataReceived (p, address);
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//namespace ns3
|