model/ns3-socket-fd-factory.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 05 May 2011 09:28:21 +0200
changeset 66 2fe1f3e576c9
parent 63 e89dca438df6
permissions -rw-r--r--
make it somewhat build sanely
mathieu@0
     1
#include "ns3-socket-fd-factory.h"
mathieu@0
     2
#include "unix-fd.h"
mathieu@0
     3
#include "unix-socket-fd.h"
mathieu@0
     4
#include "unix-datagram-socket-fd.h"
mathieu@0
     5
#include "unix-stream-socket-fd.h"
mathieu@66
     6
#include "ns3/netlink-socket-factory.h"
mathieu@0
     7
#include "ns3/socket-factory.h"
mathieu@0
     8
#include "ns3/socket.h"
mathieu@0
     9
#include "ns3/uinteger.h"
mathieu@0
    10
mathieu@0
    11
namespace ns3 {
mathieu@0
    12
mathieu@0
    13
NS_OBJECT_ENSURE_REGISTERED(Ns3SocketFdFactory);
mathieu@0
    14
mathieu@0
    15
TypeId 
mathieu@0
    16
Ns3SocketFdFactory::GetTypeId (void)
mathieu@0
    17
{
mathieu@0
    18
  static TypeId tid = TypeId ("ns3::Ns3SocketFdFactory")
mathieu@0
    19
    .SetParent<SocketFdFactory> ()
mathieu@0
    20
    .AddConstructor<Ns3SocketFdFactory> ()
mathieu@0
    21
    ;
mathieu@0
    22
  return tid;
mathieu@0
    23
}
mathieu@0
    24
mathieu@0
    25
Ns3SocketFdFactory::Ns3SocketFdFactory ()
mathieu@0
    26
{
mathieu@0
    27
  m_netlink = CreateObject<NetlinkSocketFactory> ();
mathieu@0
    28
}
mathieu@0
    29
UnixFd *
mathieu@0
    30
Ns3SocketFdFactory::CreateSocket (int domain, int type, int protocol)
mathieu@0
    31
{
mathieu@0
    32
  UnixSocketFd *socket = 0;
mathieu@0
    33
  Ptr<Socket> sock;
mathieu@0
    34
mathieu@66
    35
  if (domain == PF_INET)
mathieu@66
    36
    {
mathieu@66
    37
      switch (type) {
mathieu@66
    38
      case SOCK_DGRAM: {
mathieu@66
    39
	TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
mathieu@66
    40
	Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
mathieu@66
    41
	sock = factory->CreateSocket ();
mathieu@66
    42
	socket = new UnixDatagramSocketFd (sock);
mathieu@66
    43
      } break;
mathieu@66
    44
      case SOCK_RAW: {
mathieu@66
    45
	TypeId tid = TypeId::LookupByName ("ns3::Ipv4RawSocketFactory");
mathieu@66
    46
	Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
mathieu@66
    47
	sock = factory->CreateSocket ();
mathieu@66
    48
	sock->SetAttribute ("Protocol", UintegerValue (protocol));
mathieu@66
    49
	socket = new UnixDatagramSocketFd (sock);
mathieu@66
    50
      } break;
mathieu@66
    51
      case SOCK_STREAM: {
mathieu@66
    52
	TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory");
mathieu@66
    53
	Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
mathieu@66
    54
	sock = factory->CreateSocket ();
mathieu@66
    55
	socket = new UnixStreamSocketFd (sock);
mathieu@66
    56
      } break;
mathieu@66
    57
      default:
mathieu@66
    58
	NS_FATAL_ERROR ("missing socket type");
mathieu@66
    59
	break;
mathieu@66
    60
      }
mathieu@66
    61
    }
mathieu@66
    62
  else if (domain == PF_NETLINK)
mathieu@66
    63
    {
mathieu@66
    64
      switch (type) {
mathieu@66
    65
      case SOCK_DGRAM: {
mathieu@66
    66
	sock = m_netlink->CreateSocket ();
mathieu@66
    67
	socket = new UnixDatagramSocketFd (sock);
mathieu@66
    68
      } break;
mathieu@66
    69
      default:
mathieu@66
    70
	NS_FATAL_ERROR ("missing socket type");
mathieu@66
    71
	break;
mathieu@66
    72
      }
mathieu@66
    73
    }
mathieu@66
    74
  else
mathieu@66
    75
    {
mathieu@66
    76
      NS_FATAL_ERROR ("unsupported domain");
mathieu@66
    77
    }
mathieu@0
    78
mathieu@0
    79
  return socket;
mathieu@0
    80
}
mathieu@0
    81
mathieu@0
    82
} // namespace ns3