2007-08-01 18:48:24 +02:00
|
|
|
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2006 Georgia Tech Research Corporation
|
|
|
|
|
* 2007 INRIA
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
|
|
|
|
* Authors: George F. Riley<riley@ece.gatech.edu>
|
|
|
|
|
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
|
|
|
|
*/
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-09-13 21:36:32 -07:00
|
|
|
#include "ns3/log.h"
|
2007-08-01 18:48:24 +02:00
|
|
|
#include "ns3/packet.h"
|
2007-09-12 16:54:21 -07:00
|
|
|
#include "socket.h"
|
|
|
|
|
|
2007-09-13 21:36:32 -07:00
|
|
|
NS_LOG_COMPONENT_DEFINE ("Socket");
|
2007-04-30 16:23:10 +02:00
|
|
|
|
|
|
|
|
namespace ns3 {
|
|
|
|
|
|
|
|
|
|
Socket::~Socket ()
|
2007-09-12 16:54:21 -07:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-09-12 16:54:21 -07:00
|
|
|
}
|
2007-04-30 16:23:10 +02:00
|
|
|
|
2007-08-01 18:48:24 +02:00
|
|
|
void
|
|
|
|
|
Socket::SetCloseCallback (Callback<void,Ptr<Socket> > closeCompleted)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
m_closeCompleted = closeCompleted;
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-08-01 18:48:24 +02:00
|
|
|
void
|
2007-09-12 16:54:21 -07:00
|
|
|
Socket::SetConnectCallback (
|
|
|
|
|
Callback<void, Ptr<Socket> > connectionSucceeded,
|
|
|
|
|
Callback<void, Ptr<Socket> > connectionFailed,
|
|
|
|
|
Callback<void, Ptr<Socket> > halfClose)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
m_connectionSucceeded = connectionSucceeded;
|
|
|
|
|
m_connectionFailed = connectionFailed;
|
|
|
|
|
m_halfClose = halfClose;
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-08-01 18:48:24 +02:00
|
|
|
void
|
2007-09-12 16:54:21 -07:00
|
|
|
Socket::SetAcceptCallback (
|
|
|
|
|
Callback<bool, Ptr<Socket>, const Address &> connectionRequest,
|
|
|
|
|
Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
|
|
|
|
|
Callback<void, Ptr<Socket> > closeRequested)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
m_connectionRequest = connectionRequest;
|
|
|
|
|
m_newConnectionCreated = newConnectionCreated;
|
|
|
|
|
m_closeRequested = closeRequested;
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2008-04-22 22:29:16 -07:00
|
|
|
bool
|
|
|
|
|
Socket::SetDataSentCallback (Callback<void, Ptr<Socket>, uint32_t> dataSent)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
m_dataSent = dataSent;
|
2008-04-22 22:29:16 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Socket::SetSendCallback (Callback<void, Ptr<Socket>, uint32_t> sendCb)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
m_sendCb = sendCb;
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2008-04-24 08:18:01 -07:00
|
|
|
void
|
2008-05-02 09:21:01 -07:00
|
|
|
Socket::SetRecvCallback (Callback<void, Ptr<Socket> > receivedData)
|
2008-04-24 08:18:01 -07:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2008-05-02 09:21:01 -07:00
|
|
|
m_receivedData = receivedData;
|
2008-04-24 08:18:01 -07:00
|
|
|
}
|
|
|
|
|
|
2008-04-22 22:39:17 -07:00
|
|
|
int Socket::Listen (uint32_t queueLimit)
|
|
|
|
|
{
|
|
|
|
|
return 0; //XXX the base class version does nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-13 09:40:21 -05:00
|
|
|
int Socket::Send (const uint8_t* buf, uint32_t size)
|
|
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-12-13 09:40:21 -05:00
|
|
|
Ptr<Packet> p;
|
|
|
|
|
if (buf)
|
|
|
|
|
{
|
|
|
|
|
p = Create<Packet> (buf, size);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
p = Create<Packet> (size);
|
|
|
|
|
}
|
|
|
|
|
return Send (p);
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-02 09:38:18 -07:00
|
|
|
Ptr<Packet>
|
|
|
|
|
Socket::Recv (void)
|
|
|
|
|
{
|
|
|
|
|
return Recv (std::numeric_limits<uint32_t>::max(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-13 09:40:21 -05:00
|
|
|
int Socket::SendTo (const Address &address, const uint8_t* buf, uint32_t size)
|
|
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-12-13 09:40:21 -05:00
|
|
|
Ptr<Packet> p;
|
|
|
|
|
if(buf)
|
|
|
|
|
{
|
|
|
|
|
p = Create<Packet> (buf, size);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
p = Create<Packet> (size);
|
|
|
|
|
}
|
|
|
|
|
return SendTo (address,p);
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
void
|
2007-08-01 18:48:24 +02:00
|
|
|
Socket::NotifyCloseCompleted (void)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
if (!m_closeCompleted.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_closeCompleted (this);
|
|
|
|
|
}
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
void
|
2007-08-01 18:48:24 +02:00
|
|
|
Socket::NotifyConnectionSucceeded (void)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
if (!m_connectionSucceeded.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_connectionSucceeded (this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-08-01 18:48:24 +02:00
|
|
|
void
|
|
|
|
|
Socket::NotifyConnectionFailed (void)
|
|
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
if (!m_connectionFailed.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_connectionFailed (this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-08-01 18:48:24 +02:00
|
|
|
void
|
|
|
|
|
Socket::NotifyHalfClose (void)
|
|
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
if (!m_halfClose.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_halfClose (this);
|
|
|
|
|
}
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
bool
|
2007-08-01 18:48:24 +02:00
|
|
|
Socket::NotifyConnectionRequest (const Address &from)
|
2007-04-30 16:23:10 +02:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
if (!m_connectionRequest.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
return m_connectionRequest (this, from);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-03-14 16:37:02 -07:00
|
|
|
// accept all incoming connections by default.
|
|
|
|
|
// this way people writing code don't have to do anything
|
|
|
|
|
// special like register a callback that returns true
|
|
|
|
|
// just to get incoming connections
|
|
|
|
|
return true;
|
2007-08-01 18:48:24 +02:00
|
|
|
}
|
2007-04-30 16:23:10 +02:00
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
void
|
2007-08-01 18:48:24 +02:00
|
|
|
Socket::NotifyNewConnectionCreated (Ptr<Socket> socket, const Address &from)
|
|
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
if (!m_newConnectionCreated.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_newConnectionCreated (socket, from);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
void
|
2007-08-01 18:48:24 +02:00
|
|
|
Socket::NotifyCloseRequested (void)
|
|
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
if (!m_closeRequested.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_closeRequested (this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
void
|
2007-08-01 18:48:24 +02:00
|
|
|
Socket::NotifyDataSent (uint32_t size)
|
|
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2007-08-01 18:48:24 +02:00
|
|
|
if (!m_dataSent.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_dataSent (this, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-09-12 16:54:21 -07:00
|
|
|
|
2008-04-22 22:29:16 -07:00
|
|
|
void
|
|
|
|
|
Socket::NotifySend (uint32_t spaceAvailable)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
if (!m_sendCb.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_sendCb (this, spaceAvailable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-24 08:18:01 -07:00
|
|
|
void
|
|
|
|
|
Socket::NotifyDataRecv (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
2008-05-02 09:21:01 -07:00
|
|
|
if (!m_receivedData.IsNull ())
|
2008-04-24 08:18:01 -07:00
|
|
|
{
|
2008-05-02 09:21:01 -07:00
|
|
|
m_receivedData (this);
|
2008-04-24 08:18:01 -07:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-25 14:29:28 -07:00
|
|
|
|
|
|
|
|
SocketRxAddressTag::SocketRxAddressTag ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
|
SocketRxAddressTag::GetUid (void)
|
|
|
|
|
{
|
|
|
|
|
static uint32_t uid = ns3::Tag::AllocateUid<SocketRxAddressTag> ("SocketRxAddressTag.ns3");
|
|
|
|
|
return uid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SocketRxAddressTag::Print (std::ostream &os) const
|
|
|
|
|
{
|
|
|
|
|
os << "address="<< m_address;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
|
SocketRxAddressTag::GetSerializedSize (void) const
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SocketRxAddressTag::Serialize (Buffer::Iterator i) const
|
|
|
|
|
{
|
|
|
|
|
// for local use in stack only
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
|
SocketRxAddressTag::Deserialize (Buffer::Iterator i)
|
|
|
|
|
{
|
|
|
|
|
// for local use in stack only
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SocketRxAddressTag::SetAddress (Address addr)
|
|
|
|
|
{
|
|
|
|
|
m_address = addr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Address
|
|
|
|
|
SocketRxAddressTag::GetAddress (void) const
|
|
|
|
|
{
|
|
|
|
|
return m_address;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-30 16:23:10 +02:00
|
|
|
}//namespace ns3
|