Files
unison/src/node/packet-socket.cc

310 lines
6.3 KiB
C++
Raw Normal View History

2007-08-01 09:02:03 +02:00
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 Emmanuelle Laprise, 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: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#include "packet-socket.h"
#include "packet-socket-address.h"
2007-09-13 21:36:32 -07:00
#include "ns3/log.h"
2007-08-01 09:02:03 +02:00
#include "ns3/node.h"
#include "ns3/packet.h"
2007-08-01 09:02:03 +02:00
2007-09-13 21:36:32 -07:00
NS_LOG_COMPONENT_DEFINE ("PacketSocket");
2007-08-01 09:02:03 +02:00
namespace ns3 {
2008-03-11 13:30:12 -07:00
PacketSocket::PacketSocket ()
2007-08-01 09:02:03 +02:00
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
m_state = STATE_OPEN;
m_shutdownSend = false;
m_shutdownRecv = false;
m_errno = ERROR_NOTERROR;
}
2008-03-11 13:30:12 -07:00
void
PacketSocket::SetNode (Ptr<Node> node)
{
m_node = node;
}
2007-08-01 09:02:03 +02:00
PacketSocket::~PacketSocket ()
2007-09-13 21:36:32 -07:00
{
NS_LOG_FUNCTION;
}
2007-08-01 09:02:03 +02:00
void
PacketSocket::DoDispose (void)
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
m_device = 0;
}
enum Socket::SocketErrno
PacketSocket::GetErrno (void) const
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
return m_errno;
}
2007-08-01 09:02:03 +02:00
Ptr<Node>
PacketSocket::GetNode (void) const
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
return m_node;
}
int
PacketSocket::Bind (void)
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
PacketSocketAddress address;
address.SetProtocol (0);
2007-08-01 13:53:48 +02:00
address.SetAllDevices ();
2007-08-01 09:02:03 +02:00
return DoBind (address);
}
2007-09-13 21:36:32 -07:00
2007-08-01 09:02:03 +02:00
int
PacketSocket::Bind (const Address &address)
2007-09-13 21:36:32 -07:00
{
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
if (!PacketSocketAddress::IsMatchingType (address))
{
m_errno = ERROR_INVAL;
return -1;
}
PacketSocketAddress ad = PacketSocketAddress::ConvertFrom (address);
return DoBind (ad);
}
int
PacketSocket::DoBind (const PacketSocketAddress &address)
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
if (m_state == STATE_BOUND ||
m_state == STATE_CONNECTED)
{
m_errno = ERROR_INVAL;
return -1;
}
if (m_state == STATE_CLOSED)
{
m_errno = ERROR_BADF;
return -1;
}
2007-08-01 13:53:48 +02:00
Ptr<NetDevice> dev ;
if (address.IsSingleDevice ())
{
dev = 0;
}
else
{
dev = m_node->GetDevice (address.GetSingleDevice ());
2007-08-01 13:53:48 +02:00
}
2007-08-01 09:02:03 +02:00
m_node->RegisterProtocolHandler (MakeCallback (&PacketSocket::ForwardUp, this),
address.GetProtocol (), dev);
m_state = STATE_BOUND;
m_protocol = address.GetProtocol ();
2007-08-01 13:53:48 +02:00
m_isSingleDevice = address.IsSingleDevice ();
m_device = address.GetSingleDevice ();
2007-08-01 09:02:03 +02:00
return 0;
}
int
PacketSocket::ShutdownSend (void)
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
if (m_state == STATE_CLOSED)
{
m_errno = ERROR_BADF;
return -1;
}
m_shutdownSend = true;
return 0;
}
2007-09-13 21:36:32 -07:00
2007-08-01 09:02:03 +02:00
int
PacketSocket::ShutdownRecv (void)
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
if (m_state == STATE_CLOSED)
{
m_errno = ERROR_BADF;
return -1;
}
m_shutdownRecv = false;
return 0;
}
2007-09-13 21:36:32 -07:00
2007-08-01 09:02:03 +02:00
int
PacketSocket::Close(void)
2007-08-01 09:02:03 +02:00
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
if (m_state == STATE_CLOSED)
{
m_errno = ERROR_BADF;
return -1;
}
m_state = STATE_CLOSED;
NotifyCloseCompleted ();
2007-08-01 09:02:03 +02:00
return 0;
}
int
PacketSocket::Connect(const Address &ad)
2007-08-01 09:02:03 +02:00
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
PacketSocketAddress address;
if (m_state == STATE_CLOSED)
{
m_errno = ERROR_BADF;
goto error;
}
if (m_state == STATE_OPEN)
{
// connect should happen _after_ bind.
m_errno = ERROR_INVAL; // generic error condition.
goto error;
}
if (m_state == STATE_CONNECTED)
{
m_errno = ERROR_ISCONN;
goto error;
}
if (!PacketSocketAddress::IsMatchingType (ad))
{
m_errno = ERROR_AFNOSUPPORT;
goto error;
}
m_destAddr = ad;
m_state = STATE_CONNECTED;
NotifyConnectionSucceeded ();
2007-08-01 09:02:03 +02:00
return 0;
error:
NotifyConnectionFailed ();
2007-08-01 09:02:03 +02:00
return -1;
}
int
PacketSocket::Send (Ptr<Packet> p)
2007-08-01 09:02:03 +02:00
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
if (m_state == STATE_OPEN ||
m_state == STATE_BOUND)
{
m_errno = ERROR_NOTCONN;
return -1;
}
return SendTo (m_destAddr, p);
2007-08-01 09:02:03 +02:00
}
int
PacketSocket::SendTo(const Address &address, Ptr<Packet> p)
2007-08-01 09:02:03 +02:00
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
PacketSocketAddress ad;
if (m_state == STATE_CLOSED)
{
NS_LOG_LOGIC ("ERROR_BADF");
2007-08-01 09:02:03 +02:00
m_errno = ERROR_BADF;
return -1;
}
if (m_state == STATE_OPEN)
{
// XXX should return another error here.
NS_LOG_LOGIC ("ERROR_INVAL");
2007-08-01 09:02:03 +02:00
m_errno = ERROR_INVAL;
return -1;
}
if (m_shutdownSend)
{
NS_LOG_LOGIC ("ERROR_SHUTDOWN");
2007-08-01 09:02:03 +02:00
m_errno = ERROR_SHUTDOWN;
return -1;
}
if (!PacketSocketAddress::IsMatchingType (address))
{
NS_LOG_LOGIC ("ERROR_AFNOSUPPORT");
2007-08-01 09:02:03 +02:00
m_errno = ERROR_AFNOSUPPORT;
return -1;
}
ad = PacketSocketAddress::ConvertFrom (address);
bool error = false;
Address dest = ad.GetPhysicalAddress ();
2007-08-01 13:53:48 +02:00
if (ad.IsSingleDevice ())
2007-08-01 09:02:03 +02:00
{
2007-08-01 13:53:48 +02:00
Ptr<NetDevice> device = m_node->GetDevice (ad.GetSingleDevice ());
if (!device->Send (p, dest, ad.GetProtocol ()))
2007-08-01 09:02:03 +02:00
{
NS_LOG_LOGIC ("error: NetDevice::Send error");
2007-08-01 13:53:48 +02:00
error = true;
2007-08-01 09:02:03 +02:00
}
}
else
{
2007-08-01 13:53:48 +02:00
for (uint32_t i = 0; i < m_node->GetNDevices (); i++)
2007-08-01 09:02:03 +02:00
{
2007-08-01 13:53:48 +02:00
Ptr<NetDevice> device = m_node->GetDevice (i);
if (!device->Send (p, dest, ad.GetProtocol ()))
{
NS_LOG_LOGIC ("error: NetDevice::Send error");
2007-08-01 13:53:48 +02:00
error = true;
}
2007-08-01 09:02:03 +02:00
}
}
if (!error)
2007-08-01 09:02:03 +02:00
{
NotifyDataSent (p->GetSize ());
2007-08-01 09:02:03 +02:00
}
if (error)
{
NS_LOG_LOGIC ("ERROR_INVAL 2");
2007-08-01 09:02:03 +02:00
m_errno = ERROR_INVAL;
return -1;
}
else
{
return 0;
}
}
void
PacketSocket::ForwardUp (Ptr<NetDevice> device, Ptr<Packet> packet,
2007-08-01 09:02:03 +02:00
uint16_t protocol, const Address &from)
{
2007-09-13 21:36:32 -07:00
NS_LOG_FUNCTION;
2007-08-01 09:02:03 +02:00
if (m_shutdownRecv)
{
return;
}
PacketSocketAddress address;
address.SetPhysicalAddress (from);
2007-08-01 13:53:48 +02:00
address.SetSingleDevice (device->GetIfIndex ());
2007-08-01 09:02:03 +02:00
address.SetProtocol (protocol);
NS_LOG_LOGIC ("UID is " << packet->GetUid() << " PacketSocket " << this);
NotifyDataReceived (packet, address);
2007-08-01 09:02:03 +02:00
}
}//namespace ns3