PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/network/model/socket.cc

https://github.com/scarletttu/ns-3-codel-dev
C++ | 521 lines | 439 code | 54 blank | 28 comment | 24 complexity | 993bf3b0cee4034d9c163b6658a72e3d MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3. * Copyright (c) 2006 Georgia Tech Research Corporation
  4. * 2007 INRIA
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation;
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Authors: George F. Riley<riley@ece.gatech.edu>
  20. * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
  21. */
  22. #include "ns3/log.h"
  23. #include "ns3/packet.h"
  24. #include "node.h"
  25. #include "socket.h"
  26. #include "socket-factory.h"
  27. #include <limits>
  28. NS_LOG_COMPONENT_DEFINE ("Socket");
  29. namespace ns3 {
  30. NS_OBJECT_ENSURE_REGISTERED (Socket);
  31. TypeId
  32. Socket::GetTypeId (void)
  33. {
  34. static TypeId tid = TypeId ("ns3::Socket")
  35. .SetParent<Object> ();
  36. return tid;
  37. }
  38. Socket::Socket (void)
  39. {
  40. m_boundnetdevice = 0;
  41. m_recvPktInfo = false;
  42. NS_LOG_FUNCTION_NOARGS ();
  43. }
  44. Socket::~Socket ()
  45. {
  46. NS_LOG_FUNCTION_NOARGS ();
  47. }
  48. Ptr<Socket>
  49. Socket::CreateSocket (Ptr<Node> node, TypeId tid)
  50. {
  51. Ptr<Socket> s;
  52. NS_ASSERT (node != 0);
  53. Ptr<SocketFactory> socketFactory = node->GetObject<SocketFactory> (tid);
  54. NS_ASSERT (socketFactory != 0);
  55. s = socketFactory->CreateSocket ();
  56. NS_ASSERT (s != 0);
  57. return s;
  58. }
  59. void
  60. Socket::SetConnectCallback (
  61. Callback<void, Ptr<Socket> > connectionSucceeded,
  62. Callback<void, Ptr<Socket> > connectionFailed)
  63. {
  64. NS_LOG_FUNCTION_NOARGS ();
  65. m_connectionSucceeded = connectionSucceeded;
  66. m_connectionFailed = connectionFailed;
  67. }
  68. void
  69. Socket::SetCloseCallbacks (
  70. Callback<void, Ptr<Socket> > normalClose,
  71. Callback<void, Ptr<Socket> > errorClose)
  72. {
  73. NS_LOG_FUNCTION_NOARGS ();
  74. m_normalClose = normalClose;
  75. m_errorClose = errorClose;
  76. }
  77. void
  78. Socket::SetAcceptCallback (
  79. Callback<bool, Ptr<Socket>, const Address &> connectionRequest,
  80. Callback<void, Ptr<Socket>, const Address&> newConnectionCreated)
  81. {
  82. NS_LOG_FUNCTION_NOARGS ();
  83. m_connectionRequest = connectionRequest;
  84. m_newConnectionCreated = newConnectionCreated;
  85. }
  86. void
  87. Socket::SetDataSentCallback (Callback<void, Ptr<Socket>, uint32_t> dataSent)
  88. {
  89. NS_LOG_FUNCTION_NOARGS ();
  90. m_dataSent = dataSent;
  91. }
  92. void
  93. Socket::SetSendCallback (Callback<void, Ptr<Socket>, uint32_t> sendCb)
  94. {
  95. NS_LOG_FUNCTION_NOARGS ();
  96. m_sendCb = sendCb;
  97. }
  98. void
  99. Socket::SetRecvCallback (Callback<void, Ptr<Socket> > receivedData)
  100. {
  101. NS_LOG_FUNCTION_NOARGS ();
  102. m_receivedData = receivedData;
  103. }
  104. int
  105. Socket::Send (Ptr<Packet> p)
  106. {
  107. NS_LOG_FUNCTION_NOARGS ();
  108. return Send (p, 0);
  109. }
  110. int
  111. Socket::Send (const uint8_t* buf, uint32_t size, uint32_t flags)
  112. {
  113. NS_LOG_FUNCTION_NOARGS ();
  114. Ptr<Packet> p;
  115. if (buf)
  116. {
  117. p = Create<Packet> (buf, size);
  118. }
  119. else
  120. {
  121. p = Create<Packet> (size);
  122. }
  123. return Send (p, flags);
  124. }
  125. int
  126. Socket::SendTo (const uint8_t* buf, uint32_t size, uint32_t flags,
  127. const Address &toAddress)
  128. {
  129. NS_LOG_FUNCTION_NOARGS ();
  130. Ptr<Packet> p;
  131. if(buf)
  132. {
  133. p = Create<Packet> (buf, size);
  134. }
  135. else
  136. {
  137. p = Create<Packet> (size);
  138. }
  139. return SendTo (p, flags, toAddress);
  140. }
  141. Ptr<Packet>
  142. Socket::Recv (void)
  143. {
  144. NS_LOG_FUNCTION_NOARGS ();
  145. return Recv (std::numeric_limits<uint32_t>::max (), 0);
  146. }
  147. int
  148. Socket::Recv (uint8_t* buf, uint32_t size, uint32_t flags)
  149. {
  150. NS_LOG_FUNCTION_NOARGS ();
  151. Ptr<Packet> p = Recv (size, flags); // read up to "size" bytes
  152. if (p == 0)
  153. {
  154. return 0;
  155. }
  156. p->CopyData (buf, p->GetSize ());
  157. return p->GetSize ();
  158. }
  159. Ptr<Packet>
  160. Socket::RecvFrom (Address &fromAddress)
  161. {
  162. NS_LOG_FUNCTION_NOARGS ();
  163. return RecvFrom (std::numeric_limits<uint32_t>::max (), 0, fromAddress);
  164. }
  165. int
  166. Socket::RecvFrom (uint8_t* buf, uint32_t size, uint32_t flags,
  167. Address &fromAddress)
  168. {
  169. NS_LOG_FUNCTION_NOARGS ();
  170. Ptr<Packet> p = RecvFrom (size, flags, fromAddress);
  171. if (p == 0)
  172. {
  173. return 0;
  174. }
  175. p->CopyData (buf, p->GetSize ());
  176. return p->GetSize ();
  177. }
  178. void
  179. Socket::NotifyConnectionSucceeded (void)
  180. {
  181. NS_LOG_FUNCTION_NOARGS ();
  182. if (!m_connectionSucceeded.IsNull ())
  183. {
  184. m_connectionSucceeded (this);
  185. }
  186. }
  187. void
  188. Socket::NotifyConnectionFailed (void)
  189. {
  190. NS_LOG_FUNCTION_NOARGS ();
  191. if (!m_connectionFailed.IsNull ())
  192. {
  193. m_connectionFailed (this);
  194. }
  195. }
  196. void
  197. Socket::NotifyNormalClose (void)
  198. {
  199. NS_LOG_FUNCTION_NOARGS ();
  200. if (!m_normalClose.IsNull ())
  201. {
  202. m_normalClose (this);
  203. }
  204. }
  205. void
  206. Socket::NotifyErrorClose (void)
  207. {
  208. NS_LOG_FUNCTION_NOARGS ();
  209. if (!m_errorClose.IsNull ())
  210. {
  211. m_errorClose (this);
  212. }
  213. }
  214. bool
  215. Socket::NotifyConnectionRequest (const Address &from)
  216. {
  217. NS_LOG_FUNCTION_NOARGS ();
  218. if (!m_connectionRequest.IsNull ())
  219. {
  220. return m_connectionRequest (this, from);
  221. }
  222. else
  223. {
  224. // accept all incoming connections by default.
  225. // this way people writing code don't have to do anything
  226. // special like register a callback that returns true
  227. // just to get incoming connections
  228. return true;
  229. }
  230. }
  231. void
  232. Socket::NotifyNewConnectionCreated (Ptr<Socket> socket, const Address &from)
  233. {
  234. NS_LOG_FUNCTION_NOARGS ();
  235. if (!m_newConnectionCreated.IsNull ())
  236. {
  237. m_newConnectionCreated (socket, from);
  238. }
  239. }
  240. void
  241. Socket::NotifyDataSent (uint32_t size)
  242. {
  243. NS_LOG_FUNCTION_NOARGS ();
  244. if (!m_dataSent.IsNull ())
  245. {
  246. m_dataSent (this, size);
  247. }
  248. }
  249. void
  250. Socket::NotifySend (uint32_t spaceAvailable)
  251. {
  252. NS_LOG_FUNCTION_NOARGS ();
  253. if (!m_sendCb.IsNull ())
  254. {
  255. m_sendCb (this, spaceAvailable);
  256. }
  257. }
  258. void
  259. Socket::NotifyDataRecv (void)
  260. {
  261. NS_LOG_FUNCTION_NOARGS ();
  262. if (!m_receivedData.IsNull ())
  263. {
  264. m_receivedData (this);
  265. }
  266. }
  267. void
  268. Socket::DoDispose (void)
  269. {
  270. m_connectionSucceeded = MakeNullCallback<void,Ptr<Socket> > ();
  271. m_connectionFailed = MakeNullCallback<void,Ptr<Socket> > ();
  272. m_normalClose = MakeNullCallback<void,Ptr<Socket> > ();
  273. m_errorClose = MakeNullCallback<void,Ptr<Socket> > ();
  274. m_connectionRequest = MakeNullCallback<bool,Ptr<Socket>, const Address &> ();
  275. m_newConnectionCreated = MakeNullCallback<void,Ptr<Socket>, const Address &> ();
  276. m_dataSent = MakeNullCallback<void,Ptr<Socket>, uint32_t> ();
  277. m_sendCb = MakeNullCallback<void,Ptr<Socket>, uint32_t> ();
  278. m_receivedData = MakeNullCallback<void,Ptr<Socket> > ();
  279. }
  280. void
  281. Socket::BindToNetDevice (Ptr<NetDevice> netdevice)
  282. {
  283. if (netdevice != 0)
  284. {
  285. bool found = false;
  286. for (uint32_t i = 0; i < GetNode ()->GetNDevices (); i++)
  287. {
  288. if (GetNode ()->GetDevice (i) == netdevice)
  289. {
  290. found = true;
  291. break;
  292. }
  293. }
  294. NS_ASSERT_MSG (found, "Socket cannot be bound to a NetDevice not existing on the Node");
  295. }
  296. m_boundnetdevice = netdevice;
  297. return;
  298. }
  299. Ptr<NetDevice>
  300. Socket::GetBoundNetDevice ()
  301. {
  302. return m_boundnetdevice;
  303. }
  304. void
  305. Socket::SetRecvPktInfo (bool flag)
  306. {
  307. NS_LOG_FUNCTION_NOARGS ();
  308. m_recvPktInfo = flag;
  309. }
  310. bool Socket::IsRecvPktInfo () const
  311. {
  312. NS_LOG_FUNCTION_NOARGS ();
  313. return m_recvPktInfo;
  314. }
  315. /***************************************************************
  316. * Socket Tags
  317. ***************************************************************/
  318. SocketAddressTag::SocketAddressTag ()
  319. {
  320. }
  321. void
  322. SocketAddressTag::SetAddress (Address addr)
  323. {
  324. m_address = addr;
  325. }
  326. Address
  327. SocketAddressTag::GetAddress (void) const
  328. {
  329. return m_address;
  330. }
  331. NS_OBJECT_ENSURE_REGISTERED (SocketAddressTag);
  332. TypeId
  333. SocketAddressTag::GetTypeId (void)
  334. {
  335. static TypeId tid = TypeId ("ns3::SocketAddressTag")
  336. .SetParent<Tag> ()
  337. .AddConstructor<SocketAddressTag> ()
  338. ;
  339. return tid;
  340. }
  341. TypeId
  342. SocketAddressTag::GetInstanceTypeId (void) const
  343. {
  344. return GetTypeId ();
  345. }
  346. uint32_t
  347. SocketAddressTag::GetSerializedSize (void) const
  348. {
  349. return m_address.GetSerializedSize ();
  350. }
  351. void
  352. SocketAddressTag::Serialize (TagBuffer i) const
  353. {
  354. m_address.Serialize (i);
  355. }
  356. void
  357. SocketAddressTag::Deserialize (TagBuffer i)
  358. {
  359. m_address.Deserialize (i);
  360. }
  361. void
  362. SocketAddressTag::Print (std::ostream &os) const
  363. {
  364. os << "address=" << m_address;
  365. }
  366. SocketIpTtlTag::SocketIpTtlTag ()
  367. {
  368. }
  369. void
  370. SocketIpTtlTag::SetTtl (uint8_t ttl)
  371. {
  372. m_ttl = ttl;
  373. }
  374. uint8_t
  375. SocketIpTtlTag::GetTtl (void) const
  376. {
  377. return m_ttl;
  378. }
  379. NS_OBJECT_ENSURE_REGISTERED (SocketIpTtlTag);
  380. TypeId
  381. SocketIpTtlTag::GetTypeId (void)
  382. {
  383. static TypeId tid = TypeId ("ns3::SocketIpTtlTag")
  384. .SetParent<Tag> ()
  385. .AddConstructor<SocketIpTtlTag> ()
  386. ;
  387. return tid;
  388. }
  389. TypeId
  390. SocketIpTtlTag::GetInstanceTypeId (void) const
  391. {
  392. return GetTypeId ();
  393. }
  394. uint32_t
  395. SocketIpTtlTag::GetSerializedSize (void) const
  396. {
  397. return 1;
  398. }
  399. void
  400. SocketIpTtlTag::Serialize (TagBuffer i) const
  401. {
  402. i.WriteU8 (m_ttl);
  403. }
  404. void
  405. SocketIpTtlTag::Deserialize (TagBuffer i)
  406. {
  407. m_ttl = i.ReadU8 ();
  408. }
  409. void
  410. SocketIpTtlTag::Print (std::ostream &os) const
  411. {
  412. os << "Ttl=" << (uint32_t) m_ttl;
  413. }
  414. SocketSetDontFragmentTag::SocketSetDontFragmentTag ()
  415. {
  416. }
  417. void
  418. SocketSetDontFragmentTag::Enable (void)
  419. {
  420. m_dontFragment = true;
  421. }
  422. void
  423. SocketSetDontFragmentTag::Disable (void)
  424. {
  425. m_dontFragment = false;
  426. }
  427. bool
  428. SocketSetDontFragmentTag::IsEnabled (void) const
  429. {
  430. return m_dontFragment;
  431. }
  432. NS_OBJECT_ENSURE_REGISTERED (SocketSetDontFragmentTag);
  433. TypeId
  434. SocketSetDontFragmentTag::GetTypeId (void)
  435. {
  436. static TypeId tid = TypeId ("ns3::SocketSetDontFragmentTag")
  437. .SetParent<Tag> ()
  438. .AddConstructor<SocketSetDontFragmentTag> ()
  439. ;
  440. return tid;
  441. }
  442. TypeId
  443. SocketSetDontFragmentTag::GetInstanceTypeId (void) const
  444. {
  445. return GetTypeId ();
  446. }
  447. uint32_t
  448. SocketSetDontFragmentTag::GetSerializedSize (void) const
  449. {
  450. return 1;
  451. }
  452. void
  453. SocketSetDontFragmentTag::Serialize (TagBuffer i) const
  454. {
  455. i.WriteU8 (m_dontFragment ? 1 : 0);
  456. }
  457. void
  458. SocketSetDontFragmentTag::Deserialize (TagBuffer i)
  459. {
  460. m_dontFragment = (i.ReadU8 () == 1) ? true : false;
  461. }
  462. void
  463. SocketSetDontFragmentTag::Print (std::ostream &os) const
  464. {
  465. os << (m_dontFragment ? "true" : "false");
  466. }
  467. } // namespace ns3