From 807146f0b0781981f5d05c55ea682e286f33111c Mon Sep 17 00:00:00 2001 From: Borovkova Elena Date: Mon, 27 Jul 2009 14:47:14 +0400 Subject: [PATCH] Raw socket nearly work --- src/internet-stack/ipv4-l3-protocol.cc | 15 ++++++ src/internet-stack/ipv4-l3-protocol.h | 5 ++ src/internet-stack/raw-socket-factory-impl.cc | 24 ++++++++++ src/internet-stack/raw-socket-factory-impl.h | 26 +++++++++++ src/internet-stack/raw-socket-impl.h | 4 +- src/internet-stack/wscript | 1 + src/node/raw-socket-factory.cc | 35 ++++++++++++++ src/node/raw-socket-factory.h | 46 +++++++++++++++++++ src/node/socket.cc | 3 ++ src/node/wscript | 2 + src/routing/aodv/aodv-routing-protocol.cc | 15 +++++- 11 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 src/internet-stack/raw-socket-factory-impl.cc create mode 100644 src/internet-stack/raw-socket-factory-impl.h create mode 100644 src/node/raw-socket-factory.cc create mode 100644 src/node/raw-socket-factory.h diff --git a/src/internet-stack/ipv4-l3-protocol.cc b/src/internet-stack/ipv4-l3-protocol.cc index 1ee4acbb3..bf1927b33 100644 --- a/src/internet-stack/ipv4-l3-protocol.cc +++ b/src/internet-stack/ipv4-l3-protocol.cc @@ -40,6 +40,9 @@ #include "icmpv4-l4-protocol.h" #include "ipv4-interface.h" #include "ipv4-raw-socket-impl.h" +#include "raw-socket-impl.h" + + NS_LOG_COMPONENT_DEFINE ("Ipv4L3Protocol"); @@ -125,6 +128,18 @@ Ipv4L3Protocol::CreateRawSocket (void) m_sockets.push_back (socket); return socket; } + +Ptr +Ipv4L3Protocol::CreateRawSocket2 (void) +{ + NS_LOG_FUNCTION (this); + Ptr socket = CreateObject (); + socket->SetNode (m_node); + m_rawSocket.push_back (socket); + return socket; +} + + void Ipv4L3Protocol::DeleteRawSocket (Ptr socket) { diff --git a/src/internet-stack/ipv4-l3-protocol.h b/src/internet-stack/ipv4-l3-protocol.h index a95999778..89d1a7a13 100644 --- a/src/internet-stack/ipv4-l3-protocol.h +++ b/src/internet-stack/ipv4-l3-protocol.h @@ -30,6 +30,8 @@ #include "ns3/traced-callback.h" #include "ns3/ipv4-header.h" #include "ns3/ipv4-routing-protocol.h" +#include "raw-socket-impl.h" + namespace ns3 { @@ -71,6 +73,7 @@ public: Ptr GetRoutingProtocol (void) const; Ptr CreateRawSocket (void); + Ptr CreateRawSocket2 (void); void DeleteRawSocket (Ptr socket); /** @@ -206,6 +209,7 @@ private: typedef std::list > Ipv4InterfaceList; typedef std::list > SocketList; + typedef std::list > RawSocketList; typedef std::list > L4List_t; bool m_ipForward; @@ -222,6 +226,7 @@ private: Ptr m_routingProtocol; SocketList m_sockets; + RawSocketList m_rawSocket; }; } // Namespace ns3 diff --git a/src/internet-stack/raw-socket-factory-impl.cc b/src/internet-stack/raw-socket-factory-impl.cc new file mode 100644 index 000000000..ce726f65b --- /dev/null +++ b/src/internet-stack/raw-socket-factory-impl.cc @@ -0,0 +1,24 @@ +#include "raw-socket-factory-impl.h" +#include "raw-socket-impl.h" +#include "ipv4-l3-protocol.h" +#include "ns3/socket.h" +#include "ns3/log.h" + +namespace ns3 { + +void +RawSocketFactoryImpl::DoDispose (void) +{ + m_ipv4 = 0; + RawSocketFactory::DoDispose (); +} + +Ptr +RawSocketFactoryImpl::CreateSocket (void) +{ + + Ptr socket = m_ipv4->CreateRawSocket2 (); + return socket; +} + +} // namespace ns3 diff --git a/src/internet-stack/raw-socket-factory-impl.h b/src/internet-stack/raw-socket-factory-impl.h new file mode 100644 index 000000000..5456d9129 --- /dev/null +++ b/src/internet-stack/raw-socket-factory-impl.h @@ -0,0 +1,26 @@ +#ifndef RAW_SOCKET_FACTORY_IMPL_H +#define RAW_SOCKET_FACTORY_IMPL_H + +#include "ns3/raw-socket-factory.h" +#include "ipv4-l3-protocol.h" + + +namespace ns3 { + +class Ipv4L3Protocol; + +class RawSocketFactoryImpl : public RawSocketFactory +{ +protected: + virtual void DoDispose (void); +public: + virtual Ptr CreateSocket (void); +private: + Ptr m_ipv4; + +}; + + +} // namespace ns3 + +#endif /* RAW_SOCKET_FACTORY_IMPL_H */ diff --git a/src/internet-stack/raw-socket-impl.h b/src/internet-stack/raw-socket-impl.h index 39249c84d..fb67af581 100644 --- a/src/internet-stack/raw-socket-impl.h +++ b/src/internet-stack/raw-socket-impl.h @@ -1,5 +1,5 @@ -#ifndef IPV4_RAW_SOCKET_IMPL_H -#define IPV4_RAW_SOCKET_IMPL_H +#ifndef RAW_SOCKET_IMPL_H +#define RAW_SOCKET_IMPL_H #include "ns3/socket.h" #include "ns3/ipv4-header.h" diff --git a/src/internet-stack/wscript b/src/internet-stack/wscript index be1042b76..d1d171263 100644 --- a/src/internet-stack/wscript +++ b/src/internet-stack/wscript @@ -94,6 +94,7 @@ def build(bld): 'pending-data.cc', 'sequence-number.cc', 'rtt-estimator.cc', + 'raw-socket-factory-impl.cc', 'raw-socket-impl.cc', 'ipv4-raw-socket-factory-impl.cc', 'ipv4-raw-socket-impl.cc', diff --git a/src/node/raw-socket-factory.cc b/src/node/raw-socket-factory.cc new file mode 100644 index 000000000..35535aa8e --- /dev/null +++ b/src/node/raw-socket-factory.cc @@ -0,0 +1,35 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 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 + * + * Author: Mathieu Lacage + */ +#include "raw-socket-factory.h" +#include "ns3/uinteger.h" + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (RawSocketFactory); + +TypeId RawSocketFactory::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::RawSocketFactory") + .SetParent () + ; + return tid; +} + +} // namespace ns3 diff --git a/src/node/raw-socket-factory.h b/src/node/raw-socket-factory.h new file mode 100644 index 000000000..0a2f6c89e --- /dev/null +++ b/src/node/raw-socket-factory.h @@ -0,0 +1,46 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 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 + * + * Author: Mathieu Lacage + */ +#ifndef RAW_SOCKET_FACTORY_H +#define RAW_SOCKET_FACTORY_H + +#include "socket-factory.h" + +namespace ns3 { + +class Socket; + +/** + * \ingroup socket + * + * \brief API to create RAW socket instances + * + * This abstract class defines the API for RAW socket factory. + * + */ +class RawSocketFactory : public SocketFactory +{ +public: + static TypeId GetTypeId (void); + +}; + +} // namespace ns3 + +#endif /* RAW_SOCKET_FACTORY_H */ diff --git a/src/node/socket.cc b/src/node/socket.cc index 093c501d7..d7141f0d7 100644 --- a/src/node/socket.cc +++ b/src/node/socket.cc @@ -45,7 +45,10 @@ Ptr Socket::CreateSocket (Ptr node, TypeId tid) { Ptr s; + NS_LOG_UNCOND(tid); + NS_ASSERT(node != 0); Ptr socketFactory = node->GetObject (tid); + NS_ASSERT(socketFactory != 0); s = socketFactory->CreateSocket (); NS_ASSERT (s != 0); return s; diff --git a/src/node/wscript b/src/node/wscript index 4c1dc1e1e..8543f82bd 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -30,6 +30,7 @@ def build(bld): 'packet-socket.cc', 'udp-socket.cc', 'udp-socket-factory.cc', + 'raw-socket-factory.cc', 'tcp-socket.cc', 'tcp-socket-factory.cc', 'ipv4.cc', @@ -71,6 +72,7 @@ def build(bld): 'packet-socket-factory.h', 'udp-socket.h', 'udp-socket-factory.h', + 'raw-socket-factory.h', 'tcp-socket.h', 'tcp-socket-factory.h', 'ipv4.h', diff --git a/src/routing/aodv/aodv-routing-protocol.cc b/src/routing/aodv/aodv-routing-protocol.cc index 43b7f75d2..77fd61e28 100644 --- a/src/routing/aodv/aodv-routing-protocol.cc +++ b/src/routing/aodv/aodv-routing-protocol.cc @@ -43,6 +43,11 @@ #include "ns3/nstime.h" #include "ns3/net-device.h" +#include "ns3/raw-socket-factory.h" +#include "ns3/ipv4-raw-socket-factory.h" + + + /// UDP Port for AODV control traffic #define AODV_PORT 654 @@ -66,6 +71,7 @@ RoutingProtocol::InsertBroadcastId (Ipv4Address id, uint32_t bid) bool RoutingProtocol::LookupBroadcastId (Ipv4Address id, uint32_t bid) { + NS_LOG_FUNCTION(this); PurgeBroadcastId (); for (std::vector::const_iterator i = m_broadcastIdCache.begin (); i != m_broadcastIdCache.end (); ++i) if (i->src == id && i->id == bid) @@ -229,7 +235,9 @@ RoutingProtocol::Start () continue; // Create a socket to listen only on this interface - Ptr socket = Socket::CreateSocket (GetObject (), TypeId::LookupByName ("ns3::UdpSocketFactory")); + Ptr socket = CreateObject (); + socket->SetNode(GetObject ()); + //Ptr socket = Socket::CreateSocket (GetObject (), RawSocketFactory::GetTypeId()); NS_ASSERT (socket != 0); int status = socket->Bind (InetSocketAddress (iface.GetLocal (), AODV_PORT)); NS_ASSERT (status != -1); @@ -436,6 +444,8 @@ RoutingProtocol::SendRequest (Ipv4Address dst, bool D, bool G) // Create RREQ header TypeHeader tHeader (AODVTYPE_RREQ); RreqHeader rreqHeader; + Ipv4Header ipv4Header; + ipv4Header.SetTtl (NET_DIAMETER); rreqHeader.SetDst (dst); RoutingTableEntry rt; @@ -488,6 +498,9 @@ RoutingProtocol::SendRequest (Ipv4Address dst, bool D, bool G) rreqHeader.SetOrigin (iface.GetLocal ()); InsertBroadcastId (iface.GetLocal (), m_broadcastID); + ipv4Header.SetSource (iface.GetLocal ()); + ipv4Header.SetDestination (iface.GetBroadcast()); + packet->AddHeader (rreqHeader); packet->AddHeader (tHeader); socket->Send (packet);