ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ns3-socket-fd-factory.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3-socket-fd-factory.h"
21 #include "unix-fd.h"
22 #include "unix-socket-fd.h"
24 #include "unix-stream-socket-fd.h"
25 #include "ns3/netlink-socket-factory.h"
26 #include "ns3/socket-factory.h"
27 #include "ns3/socket.h"
28 #include "ns3/log.h"
29 #include "ns3/node.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/packet-socket-address.h"
32 #include <netpacket/packet.h>
33 #include <net/ethernet.h>
34 
35 NS_LOG_COMPONENT_DEFINE ("Ns3SocketFdFactory");
36 
37 namespace ns3 {
38 
39 NS_OBJECT_ENSURE_REGISTERED (Ns3SocketFdFactory);
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::Ns3SocketFdFactory")
45  .SetParent<SocketFdFactory> ()
46  .AddConstructor<Ns3SocketFdFactory> ()
47  ;
48  return tid;
49 }
50 
52  : m_netlink (0)
53 {
54 }
55 
56 //this is necessary to assign appropriate Node to NetlinkSocketFactory
57 void
59 {
60  Ptr<Node> node = this->GetObject<Node> ();
61  if (m_netlink == 0)
62  {
63  m_netlink = CreateObject<NetlinkSocketFactory> ();
64  node->AggregateObject (m_netlink);
65  }
66 }
67 
68 UnixFd *
69 Ns3SocketFdFactory::CreateSocket (int domain, int type, int protocol)
70 {
71  UnixSocketFd *socket = 0;
72  Ptr<Socket> sock;
73 
74  if (domain == PF_INET)
75  {
76  switch (type)
77  {
78  case SOCK_DGRAM:
79  {
80  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
81  Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
82  sock = factory->CreateSocket ();
83  socket = new UnixDatagramSocketFd (sock);
84  } break;
85  case SOCK_RAW:
86  {
87  TypeId tid = TypeId::LookupByName ("ns3::Ipv4RawSocketFactory");
88  Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
89  sock = factory->CreateSocket ();
90  sock->SetAttribute ("Protocol", UintegerValue (protocol));
91  socket = new UnixDatagramSocketFd (sock);
92  } break;
93  case SOCK_STREAM:
94  {
95  TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory");
96  Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
97  sock = factory->CreateSocket ();
98  socket = new UnixStreamSocketFd (sock);
99  } break;
100  default:
101  NS_FATAL_ERROR ("missing socket type");
102  break;
103  }
104  }
105  else if (domain == PF_INET6)
106  {
107  switch (type)
108  {
109  case SOCK_RAW:
110  {
111  TypeId tid = TypeId::LookupByName ("ns3::Ipv6RawSocketFactory");
112  Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
113  sock = factory->CreateSocket ();
114  sock->SetAttribute ("Protocol", UintegerValue (protocol));
115  socket = new UnixDatagramSocketFd (sock);
116  } break;
117  case SOCK_DGRAM:
118  {
119  } break;
120  case SOCK_STREAM:
121  {
122  TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory");
123  Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
124  sock = factory->CreateSocket ();
125  socket = new UnixStreamSocketFd (sock);
126  } break;
127  default:
128  NS_FATAL_ERROR ("missing socket type");
129  break;
130  }
131  }
132  else if (domain == PF_NETLINK)
133  {
134  switch (type)
135  {
136  case SOCK_RAW:
137  case SOCK_DGRAM:
138  {
139  NS_LOG_INFO ("Requesting for PF_NETLINK");
140  sock = m_netlink->CreateSocket ();
141  socket = new UnixDatagramSocketFd (sock);
142  } break;
143  default:
144  NS_FATAL_ERROR ("missing socket type");
145  break;
146  }
147  }
148  else if (domain == AF_PACKET)
149  {
150  switch (type)
151  {
152  case SOCK_RAW:
153  {
154  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
155  Ptr<SocketFactory> factory = GetObject<SocketFactory> (tid);
156  sock = factory->CreateSocket ();
157 
158  PacketSocketAddress a;
159  a.SetAllDevices ();
160  if (protocol == htons (ETH_P_ALL))
161  {
162  a.SetProtocol (0);
163  }
164  else
165  {
166  a.SetProtocol (ntohs (protocol));
167  }
168 
169  sock->Bind (a);
170 
171  socket = new UnixDatagramSocketFd (sock);
172  } break;
173  default:
174  NS_FATAL_ERROR ("missing socket type");
175  break;
176  }
177  }
178  else
179  {
180  NS_FATAL_ERROR ("unsupported domain");
181  return 0;
182  }
183 
184  return socket;
185 }
186 
187 } // namespace ns3