PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/network/model/socket.h

https://bitbucket.org/cttc-lena/ns-3-lena-dev
C Header | 1234 lines | 240 code | 136 blank | 858 comment | 0 complexity | bd8420af99fc99b57b6ffa9e04890cc9 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. #ifndef NS3_SOCKET_H
  23. #define NS3_SOCKET_H
  24. #include "ns3/callback.h"
  25. #include "ns3/ptr.h"
  26. #include "ns3/tag.h"
  27. #include "ns3/object.h"
  28. #include "ns3/net-device.h"
  29. #include "address.h"
  30. #include <stdint.h>
  31. #include "ns3/inet-socket-address.h"
  32. #include "ns3/inet6-socket-address.h"
  33. namespace ns3 {
  34. class Node;
  35. class Packet;
  36. /**
  37. * \ingroup network
  38. * \defgroup socket Socket
  39. */
  40. /**
  41. * \brief A low-level Socket API based loosely on the BSD Socket API.
  42. * \ingroup socket
  43. *
  44. * A few things to keep in mind about this type of socket:
  45. * - it uses ns-3 API constructs such as class ns3::Address instead of
  46. * C-style structs
  47. * - in contrast to the original BSD socket API, this API is asynchronous:
  48. * it does not contain blocking calls. Sending and receiving operations
  49. * must make use of the callbacks provided.
  50. * - It also uses class ns3::Packet as a fancy byte buffer, allowing
  51. * data to be passed across the API using an ns-3 Packet instead of
  52. * a raw data pointer.
  53. * - Not all of the full POSIX sockets API is supported
  54. *
  55. * Other than that, it tries to stick to the BSD API to make it
  56. * easier for those who know the BSD API to use this API.
  57. * More details are provided in the ns-3 tutorial.
  58. */
  59. class Socket : public Object
  60. {
  61. public:
  62. /**
  63. * \brief Get the type ID.
  64. * \return the object TypeId
  65. */
  66. static TypeId GetTypeId (void);
  67. Socket (void);
  68. virtual ~Socket (void);
  69. /**
  70. * \enum SocketErrno
  71. * \brief Enumeration of the possible errors returned by a socket.
  72. */
  73. enum SocketErrno {
  74. ERROR_NOTERROR,
  75. ERROR_ISCONN,
  76. ERROR_NOTCONN,
  77. ERROR_MSGSIZE,
  78. ERROR_AGAIN,
  79. ERROR_SHUTDOWN,
  80. ERROR_OPNOTSUPP,
  81. ERROR_AFNOSUPPORT,
  82. ERROR_INVAL,
  83. ERROR_BADF,
  84. ERROR_NOROUTETOHOST,
  85. ERROR_NODEV,
  86. ERROR_ADDRNOTAVAIL,
  87. ERROR_ADDRINUSE,
  88. SOCKET_ERRNO_LAST
  89. };
  90. /**
  91. * \enum SocketType
  92. * \brief Enumeration of the possible socket types.
  93. */
  94. enum SocketType {
  95. NS3_SOCK_STREAM,
  96. NS3_SOCK_SEQPACKET,
  97. NS3_SOCK_DGRAM,
  98. NS3_SOCK_RAW
  99. };
  100. /**
  101. * This method wraps the creation of sockets that is performed
  102. * on a given node by a SocketFactory specified by TypeId.
  103. *
  104. * \return A smart pointer to a newly created socket.
  105. *
  106. * \param node The node on which to create the socket
  107. * \param tid The TypeId of a SocketFactory class to use
  108. */
  109. static Ptr<Socket> CreateSocket (Ptr<Node> node, TypeId tid);
  110. /**
  111. * \brief Get last error number.
  112. *
  113. * \return the errno associated to the last call which failed in this
  114. * socket. Each socket's errno is initialized to zero
  115. * when the socket is created.
  116. */
  117. virtual enum Socket::SocketErrno GetErrno (void) const = 0;
  118. /**
  119. * \return the socket type, analogous to getsockopt (SO_TYPE)
  120. */
  121. virtual enum Socket::SocketType GetSocketType (void) const = 0;
  122. /**
  123. * \brief Return the node this socket is associated with.
  124. * \returns the node
  125. */
  126. virtual Ptr<Node> GetNode (void) const = 0;
  127. /**
  128. * \brief Specify callbacks to allow the caller to determine if
  129. * the connection succeeds of fails.
  130. * \param connectionSucceeded this callback is invoked when the
  131. * connection request initiated by the user is successfully
  132. * completed. The callback is passed back a pointer to
  133. * the same socket object.
  134. * \param connectionFailed this callback is invoked when the
  135. * connection request initiated by the user is unsuccessfully
  136. * completed. The callback is passed back a pointer to the
  137. * same socket object.
  138. */
  139. void SetConnectCallback (Callback<void, Ptr<Socket> > connectionSucceeded,
  140. Callback<void, Ptr<Socket> > connectionFailed);
  141. /**
  142. * \brief Detect socket recv() events such as graceful shutdown or error.
  143. *
  144. * For connection-oriented sockets, the first callback is used to signal
  145. * that the remote side has gracefully shut down the connection, and the
  146. * second callback denotes an error corresponding to cases in which
  147. * a traditional recv() socket call might return -1 (error), such
  148. * as a connection reset. For datagram sockets, these callbacks may
  149. * never be invoked.
  150. *
  151. * \param normalClose this callback is invoked when the
  152. * peer closes the connection gracefully
  153. * \param errorClose this callback is invoked when the
  154. * connection closes abnormally
  155. */
  156. void SetCloseCallbacks (Callback<void, Ptr<Socket> > normalClose,
  157. Callback<void, Ptr<Socket> > errorClose);
  158. /**
  159. * \brief Accept connection requests from remote hosts
  160. * \param connectionRequest Callback for connection request from peer.
  161. * This user callback is passed a pointer to this socket, the
  162. * ip address and the port number of the connection originator.
  163. * This callback must return true to accept the incoming connection,
  164. * false otherwise. If the connection is accepted, the
  165. * "newConnectionCreated" callback will be invoked later to
  166. * give access to the user to the socket created to match
  167. * this new connection. If the user does not explicitly
  168. * specify this callback, all incoming connections will be refused.
  169. * \param newConnectionCreated Callback for new connection: when a new
  170. * is accepted, it is created and the corresponding socket is passed
  171. * back to the user through this callback. This user callback is
  172. * passed a pointer to the new socket, and the ip address and
  173. * port number of the connection originator.
  174. */
  175. void SetAcceptCallback (Callback<bool, Ptr<Socket>,
  176. const Address &> connectionRequest,
  177. Callback<void, Ptr<Socket>,
  178. const Address&> newConnectionCreated);
  179. /**
  180. * \brief Notify application when a packet has been sent from transport
  181. * protocol (non-standard socket call)
  182. * \param dataSent Callback for the event that data is sent from the
  183. * underlying transport protocol. This callback is passed a
  184. * pointer to the socket, and the number of bytes sent.
  185. */
  186. void SetDataSentCallback (Callback<void, Ptr<Socket>,
  187. uint32_t> dataSent);
  188. /**
  189. * \brief Notify application when space in transmit buffer is added
  190. *
  191. * This callback is intended to notify a
  192. * socket that would have been blocked in a blocking socket model
  193. * that space is available in the transmit buffer and that it
  194. * can call Send() again.
  195. *
  196. * \param sendCb Callback for the event that the socket transmit buffer
  197. * fill level has decreased. This callback is passed a pointer to
  198. * the socket, and the number of bytes available for writing
  199. * into the buffer (an absolute value). If there is no transmit
  200. * buffer limit, a maximum-sized integer is always returned.
  201. */
  202. void SetSendCallback (Callback<void, Ptr<Socket>, uint32_t> sendCb);
  203. /**
  204. * \brief Notify application when new data is available to be read.
  205. *
  206. * This callback is intended to notify a socket that would
  207. * have been blocked in a blocking socket model that data
  208. * is available to be read.
  209. */
  210. void SetRecvCallback (Callback<void, Ptr<Socket> >);
  211. /**
  212. * \brief Allocate a local endpoint for this socket.
  213. * \param address the address to try to allocate
  214. * \returns 0 on success, -1 on failure.
  215. */
  216. virtual int Bind (const Address &address) = 0;
  217. /**
  218. * \brief Allocate a local IPv4 endpoint for this socket.
  219. *
  220. * \returns 0 on success, -1 on failure.
  221. */
  222. virtual int Bind () = 0;
  223. /**
  224. * \brief Allocate a local IPv6 endpoint for this socket.
  225. *
  226. * \returns 0 on success, -1 on failure.
  227. */
  228. virtual int Bind6 () = 0;
  229. /**
  230. * \brief Close a socket.
  231. * \returns zero on success, -1 on failure.
  232. *
  233. * After the Close call, the socket is no longer valid, and cannot
  234. * safely be used for subsequent operations.
  235. */
  236. virtual int Close (void) = 0;
  237. /**
  238. * \returns zero on success, -1 on failure.
  239. *
  240. * Do not allow any further Send calls. This method is typically
  241. * implemented for Tcp sockets by a half close.
  242. */
  243. virtual int ShutdownSend (void) = 0;
  244. /**
  245. * \returns zero on success, -1 on failure.
  246. *
  247. * Do not allow any further Recv calls. This method is typically
  248. * implemented for Tcp sockets by a half close.
  249. */
  250. virtual int ShutdownRecv (void) = 0;
  251. /**
  252. * \brief Initiate a connection to a remote host
  253. * \param address Address of remote.
  254. * \returns 0 on success, -1 on error (in which case errno is set).
  255. */
  256. virtual int Connect (const Address &address) = 0;
  257. /**
  258. * \brief Listen for incoming connections.
  259. * \returns 0 on success, -1 on error (in which case errno is set).
  260. */
  261. virtual int Listen (void) = 0;
  262. /**
  263. * \brief Returns the number of bytes which can be sent in a single call
  264. * to Send.
  265. *
  266. * For datagram sockets, this returns the number of bytes that
  267. * can be passed atomically through the underlying protocol.
  268. *
  269. * For stream sockets, this returns the available space in bytes
  270. * left in the transmit buffer.
  271. *
  272. * \returns The number of bytes which can be sent in a single Send call.
  273. */
  274. virtual uint32_t GetTxAvailable (void) const = 0;
  275. /**
  276. * \brief Send data (or dummy data) to the remote host
  277. *
  278. * This function matches closely in semantics to the send() function
  279. * call in the standard C library (libc):
  280. * ssize_t send (int s, const void *msg, size_t len, int flags);
  281. * except that the send I/O is asynchronous. This is the
  282. * primary Send method at this low-level API and must be implemented
  283. * by subclasses.
  284. *
  285. * In a typical blocking sockets model, this call would block upon
  286. * lack of space to hold the message to be sent. In ns-3 at this
  287. * API, the call returns immediately in such a case, but the callback
  288. * registered with SetSendCallback() is invoked when the socket
  289. * has space (when it conceptually unblocks); this is an asynchronous
  290. * I/O model for send().
  291. *
  292. * This variant of Send() uses class ns3::Packet to encapsulate
  293. * data, rather than providing a raw pointer and length field.
  294. * This allows an ns-3 application to attach tags if desired (such
  295. * as a flow ID) and may allow the simulator to avoid some data
  296. * copies. Despite the appearance of sending Packets on a stream
  297. * socket, just think of it as a fancy byte buffer with streaming
  298. * semantics.
  299. *
  300. * If either the message buffer within the Packet is too long to pass
  301. * atomically through the underlying protocol (for datagram sockets),
  302. * or the message buffer cannot entirely fit in the transmit buffer
  303. * (for stream sockets), -1 is returned and SocketErrno is set
  304. * to ERROR_MSGSIZE. If the packet does not fit, the caller can
  305. * split the Packet (based on information obtained from
  306. * GetTxAvailable) and reattempt to send the data.
  307. *
  308. * The flags argument is formed by or'ing one or more of the values:
  309. * MSG_OOB process out-of-band data
  310. * MSG_DONTROUTE bypass routing, use direct interface
  311. * These flags are _unsupported_ as of ns-3.1.
  312. *
  313. * \param p ns3::Packet to send
  314. * \param flags Socket control flags
  315. * \returns the number of bytes accepted for transmission if no error
  316. * occurs, and -1 otherwise.
  317. *
  318. * \see SetSendCallback
  319. */
  320. virtual int Send (Ptr<Packet> p, uint32_t flags) = 0;
  321. /**
  322. * \brief Send data to a specified peer.
  323. *
  324. * This method has similar semantics to Send () but subclasses may
  325. * want to provide checks on socket state, so the implementation is
  326. * pushed to subclasses.
  327. *
  328. * \param p packet to send
  329. * \param flags Socket control flags
  330. * \param toAddress IP Address of remote host
  331. * \returns -1 in case of error or the number of bytes copied in the
  332. * internal buffer and accepted for transmission.
  333. */
  334. virtual int SendTo (Ptr<Packet> p, uint32_t flags,
  335. const Address &toAddress) = 0;
  336. /**
  337. * Return number of bytes which can be returned from one or
  338. * multiple calls to Recv.
  339. * Must be possible to call this method from the Recv callback.
  340. *
  341. * \returns the number of bytes which can be returned from one or
  342. * multiple Recv calls.
  343. */
  344. virtual uint32_t GetRxAvailable (void) const = 0;
  345. /**
  346. * \brief Read data from the socket
  347. *
  348. * This function matches closely in semantics to the recv() function
  349. * call in the standard C library (libc):
  350. * ssize_t recv (int s, void *buf, size_t len, int flags);
  351. * except that the receive I/O is asynchronous. This is the
  352. * primary Recv method at this low-level API and must be implemented
  353. * by subclasses.
  354. *
  355. * This method is normally used only on a connected socket.
  356. * In a typical blocking sockets model, this call would block until
  357. * at least one byte is returned or the connection closes.
  358. * In ns-3 at this API, the call returns immediately in such a case
  359. * and returns 0 if nothing is available to be read.
  360. * However, an application can set a callback, ns3::SetRecvCallback,
  361. * to be notified of data being available to be read
  362. * (when it conceptually unblocks); this is an asynchronous
  363. * I/O model for recv().
  364. *
  365. * This variant of Recv() uses class ns3::Packet to encapsulate
  366. * data, rather than providing a raw pointer and length field.
  367. * This allows an ns-3 application to attach tags if desired (such
  368. * as a flow ID) and may allow the simulator to avoid some data
  369. * copies. Despite the appearance of receiving Packets on a stream
  370. * socket, just think of it as a fancy byte buffer with streaming
  371. * semantics.
  372. *
  373. * The semantics depend on the type of socket. For a datagram socket,
  374. * each Recv() returns the data from at most one Send(), and order
  375. * is not necessarily preserved. For a stream socket, the bytes
  376. * are delivered in order, and on-the-wire packet boundaries are
  377. * not preserved.
  378. *
  379. * The flags argument is formed by or'ing one or more of the values:
  380. * MSG_OOB process out-of-band data
  381. * MSG_PEEK peek at incoming message
  382. * None of these flags are supported for now.
  383. *
  384. * Some variants of Recv() are supported as additional API,
  385. * including RecvFrom(), overloaded Recv() without arguments,
  386. * and variants that use raw character buffers.
  387. *
  388. * \param maxSize reader will accept packet up to maxSize
  389. * \param flags Socket control flags
  390. * \returns Ptr<Packet> of the next in-sequence packet. Returns
  391. * 0 if the socket cannot return a next in-sequence packet conforming
  392. * to the maxSize and flags.
  393. *
  394. * \see SetRecvCallback
  395. */
  396. virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags) = 0;
  397. /**
  398. * \brief Read a single packet from the socket and retrieve the sender
  399. * address.
  400. *
  401. * Calls Recv(maxSize, flags) with maxSize
  402. * implicitly set to maximum sized integer, and flags set to zero.
  403. *
  404. * This method has similar semantics to Recv () but subclasses may
  405. * want to provide checks on socket state, so the implementation is
  406. * pushed to subclasses.
  407. *
  408. * \param maxSize reader will accept packet up to maxSize
  409. * \param flags Socket control flags
  410. * \param fromAddress output parameter that will return the
  411. * address of the sender of the received packet, if any. Remains
  412. * untouched if no packet is received.
  413. * \returns Ptr<Packet> of the next in-sequence packet. Returns
  414. * 0 if the socket cannot return a next in-sequence packet.
  415. */
  416. virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags,
  417. Address &fromAddress) = 0;
  418. /////////////////////////////////////////////////////////////////////
  419. // The remainder of these public methods are overloaded methods //
  420. // or variants of Send() and Recv(), and they are non-virtual //
  421. /////////////////////////////////////////////////////////////////////
  422. /**
  423. * \brief Send data (or dummy data) to the remote host
  424. *
  425. * Overloaded version of Send(..., flags) with flags set to zero.
  426. *
  427. * \param p ns3::Packet to send
  428. * \returns the number of bytes accepted for transmission if no error
  429. * occurs, and -1 otherwise.
  430. */
  431. int Send (Ptr<Packet> p);
  432. /**
  433. * \brief Send data (or dummy data) to the remote host
  434. *
  435. * This method is provided so as to have an API which is closer in
  436. * appearance to that of real network or BSD sockets.
  437. *
  438. * \param buf A pointer to a raw byte buffer of some data to send. If
  439. * this buffer is 0, we send dummy data whose size is specified by the
  440. * second parameter
  441. * \param size the number of bytes to copy from the buffer
  442. * \param flags Socket control flags
  443. * \returns the number of bytes accepted for transmission if no error
  444. * occurs, and -1 otherwise.
  445. */
  446. int Send (const uint8_t* buf, uint32_t size, uint32_t flags);
  447. /**
  448. * \brief Send data to a specified peer.
  449. *
  450. * This method is provided so as to have an API which is closer in
  451. * appearance to that of real network or BSD sockets.
  452. *
  453. * \param buf A pointer to a raw byte buffer of some data to send.
  454. * If this is 0, we send dummy data whose size is specified by the
  455. * third parameter
  456. * \param size the number of bytes to copy from the buffer
  457. * \param flags Socket control flags
  458. * \param address IP Address of remote host
  459. * \returns -1 in case of error or the number of bytes copied in the
  460. * internal buffer and accepted for transmission.
  461. *
  462. */
  463. int SendTo (const uint8_t* buf, uint32_t size, uint32_t flags,
  464. const Address &address);
  465. /**
  466. * \brief Read a single packet from the socket
  467. *
  468. * Overloaded version of Recv(maxSize, flags) with maxSize
  469. * implicitly set to maximum sized integer, and flags set to zero.
  470. *
  471. * \returns Ptr<Packet> of the next in-sequence packet. Returns
  472. * 0 if the socket cannot return a next in-sequence packet.
  473. */
  474. Ptr<Packet> Recv (void);
  475. /**
  476. * \brief Recv data (or dummy data) from the remote host
  477. *
  478. * This method is provided so as to have an API which is closer in
  479. * appearance to that of real network or BSD sockets.
  480. *
  481. * If the underlying packet was carring null (fake) data, this buffer
  482. * will be zeroed up to the length specified by the return value.
  483. *
  484. * \param buf A pointer to a raw byte buffer to write the data to.
  485. * \param size Number of bytes (at most) to copy to buf
  486. * \param flags any flags to pass to the socket
  487. * \returns number of bytes copied into buf
  488. */
  489. int Recv (uint8_t* buf, uint32_t size, uint32_t flags);
  490. /**
  491. * \brief Read a single packet from the socket and retrieve the sender
  492. * address.
  493. *
  494. * Calls RecvFrom (maxSize, flags, fromAddress) with maxSize
  495. * implicitly set to maximum sized integer, and flags set to zero.
  496. *
  497. * \param fromAddress output parameter that will return the
  498. * address of the sender of the received packet, if any. Remains
  499. * untouched if no packet is received.
  500. * \returns Ptr<Packet> of the next in-sequence packet. Returns
  501. * 0 if the socket cannot return a next in-sequence packet.
  502. */
  503. Ptr<Packet> RecvFrom (Address &fromAddress);
  504. /**
  505. * \brief Read a single packet from the socket and retrieve the sender
  506. * address.
  507. *
  508. * This method is provided so as to have an API which is closer in
  509. * appearance to that of real network or BSD sockets.
  510. *
  511. * \param buf A pointer to a raw byte buffer to write the data to.
  512. * If the underlying packet was carring null (fake) data, this buffer
  513. * will be zeroed up to the length specified by the return value.
  514. * \param size Number of bytes (at most) to copy to buf
  515. * \param flags any flags to pass to the socket
  516. * \param fromAddress output parameter that will return the
  517. * address of the sender of the received packet, if any. Remains
  518. * untouched if no packet is received.
  519. * \returns number of bytes copied into buf
  520. */
  521. int RecvFrom (uint8_t* buf, uint32_t size, uint32_t flags,
  522. Address &fromAddress);
  523. /**
  524. * \brief Get socket address.
  525. * \param address the address name this socket is associated with.
  526. * \returns 0 if success, -1 otherwise
  527. */
  528. virtual int GetSockName (Address &address) const = 0;
  529. /**
  530. * \brief Bind a socket to specific device.
  531. *
  532. * This method corresponds to using setsockopt() SO_BINDTODEVICE
  533. * of real network or BSD sockets. If set on a socket, this option will
  534. * force packets to leave the bound device regardless of the device that
  535. * IP routing would naturally choose. In the receive direction, only
  536. * packets received from the bound interface will be delivered.
  537. *
  538. * This option has no particular relationship to binding sockets to
  539. * an address via Socket::Bind (). It is possible to bind sockets to a
  540. * specific IP address on the bound interface by calling both
  541. * Socket::Bind (address) and Socket::BindToNetDevice (device), but it
  542. * is also possible to bind to mismatching device and address, even if
  543. * the socket can not receive any packets as a result.
  544. *
  545. * \param netdevice Pointer to Netdevice of desired interface
  546. * \returns nothing
  547. */
  548. virtual void BindToNetDevice (Ptr<NetDevice> netdevice);
  549. /**
  550. * \brief Returns socket's bound netdevice, if any.
  551. *
  552. * This method corresponds to using getsockopt() SO_BINDTODEVICE
  553. * of real network or BSD sockets.
  554. *
  555. *
  556. * \returns Pointer to interface.
  557. */
  558. Ptr<NetDevice> GetBoundNetDevice ();
  559. /**
  560. * \brief Configure whether broadcast datagram transmissions are allowed
  561. *
  562. * This method corresponds to using setsockopt() SO_BROADCAST of
  563. * real network or BSD sockets. If set on a socket, this option
  564. * will enable or disable packets to be transmitted to broadcast
  565. * destination addresses.
  566. *
  567. * \param allowBroadcast Whether broadcast is allowed
  568. * \return true if operation succeeds
  569. */
  570. virtual bool SetAllowBroadcast (bool allowBroadcast) = 0;
  571. /**
  572. * \brief Query whether broadcast datagram transmissions are allowed
  573. *
  574. * This method corresponds to using getsockopt() SO_BROADCAST of
  575. * real network or BSD sockets.
  576. *
  577. * \returns true if broadcast is allowed, false otherwise
  578. */
  579. virtual bool GetAllowBroadcast () const = 0;
  580. /**
  581. * \brief Enable/Disable receive packet information to socket.
  582. *
  583. * For IP_PKTINFO/IP6_PKTINFO. This method is only usable for
  584. * Raw socket and Datagram Socket. Not supported for Stream socket.
  585. *
  586. * Method doesn't make distinction between IPv4 and IPv6. If it is enabled,
  587. * it is enabled for all types of sockets that supports packet information
  588. *
  589. * \param flag Enable/Disable receive information
  590. * \returns nothing
  591. */
  592. void SetRecvPktInfo (bool flag);
  593. /**
  594. * \brief Get status indicating whether enable/disable packet information to socket
  595. *
  596. * \returns True if packet information should be sent to socket
  597. */
  598. bool IsRecvPktInfo () const;
  599. /**
  600. * \brief Manually set IP Type of Service field
  601. *
  602. * This method corresponds to using setsockopt () IP_TOS of
  603. * real network or BSD sockets. This option is for IPv4 only.
  604. * Setting the IP TOS should also change the socket queueing
  605. * priority as stated in the man page. However, socket priority
  606. * is not yet supported.
  607. *
  608. * \param ipTos The desired TOS value for IP headers
  609. */
  610. void SetIpTos (uint8_t ipTos);
  611. /**
  612. * \brief Query the value of IP Type of Service of this socket
  613. *
  614. * This method corresponds to using getsockopt () IP_TOS of real network
  615. * or BSD sockets.
  616. *
  617. * \return The raw IP TOS value
  618. */
  619. uint8_t GetIpTos (void) const;
  620. /**
  621. * \brief Tells a socket to pass information about IP Type of Service up the stack
  622. *
  623. * This method corresponds to using setsockopt () IP_RECVTOS of real
  624. * network or BSD sockets. In our implementation, the socket simply
  625. * adds a SocketIpTosTag tag to the packet before passing the
  626. * packet up the stack.
  627. *
  628. * \param ipv4RecvTos Whether the socket should add SocketIpv4TosTag tag
  629. * to the packet
  630. */
  631. void SetIpRecvTos (bool ipv4RecvTos);
  632. /**
  633. * \brief Ask if the socket is currently passing information about IP Type of Service up the stack
  634. *
  635. * This method corresponds to using getsockopt () IP_RECVTOS of real
  636. * network or BSD sockets.
  637. *
  638. * \return Whether the IP_RECVTOS is set
  639. */
  640. bool IsIpRecvTos (void) const;
  641. /**
  642. * \brief Manually set IPv6 Traffic Class field
  643. *
  644. * This method corresponds to using setsockopt () IPV6_TCLASS of
  645. * real network or BSD sockets. This option is for IPv6 only.
  646. * Setting the IPV6_TCLASSS to -1 clears the option and let the socket
  647. * uses the default value.
  648. *
  649. * \param ipTclass The desired TCLASS value for IPv6 headers
  650. */
  651. void SetIpv6Tclass (int ipTclass);
  652. /**
  653. * \brief Query the value of IPv6 Traffic Class field of this socket
  654. *
  655. * This method corresponds to using getsockopt () IPV6_TCLASS of real network
  656. * or BSD sockets.
  657. *
  658. * \return The raw IPV6_TCLASS value
  659. */
  660. uint8_t GetIpv6Tclass (void) const;
  661. /**
  662. * \brief Tells a socket to pass information about IPv6 Traffic Class up the stack
  663. *
  664. * This method corresponds to using setsockopt () IPV6_RECVTCLASS of real
  665. * network or BSD sockets. In our implementation, the socket simply
  666. * adds a SocketIpv6TclasssTag tag to the packet before passing the
  667. * packet up the stack.
  668. *
  669. * \param ipv6RecvTclass Whether the socket should add SocketIpv6TclassTag tag
  670. * to the packet
  671. */
  672. void SetIpv6RecvTclass (bool ipv6RecvTclass);
  673. /**
  674. * \brief Ask if the socket is currently passing information about IPv6 Traffic Class up the stack
  675. *
  676. * This method corresponds to using getsockopt () IPV6_RECVTCLASS of real
  677. * network or BSD sockets.
  678. *
  679. * \return Whether the IPV6_RECVTCLASS is set
  680. */
  681. bool IsIpv6RecvTclass (void) const;
  682. /**
  683. * \brief Manually set IP Time to Live field
  684. *
  685. * This method corresponds to using setsockopt () IP_TTL of
  686. * real network or BSD sockets.
  687. *
  688. * \param ipTtl The desired TTL value for IP headers
  689. */
  690. virtual void SetIpTtl (uint8_t ipTtl);
  691. /**
  692. * \brief Query the value of IP Time to Live field of this socket
  693. *
  694. * This method corresponds to using getsockopt () IP_TTL of real network
  695. * or BSD sockets.
  696. *
  697. * \return The raw IP TTL value
  698. */
  699. virtual uint8_t GetIpTtl (void) const;
  700. /**
  701. * \brief Tells a socket to pass information about IP_TTL up the stack
  702. *
  703. * This method corresponds to using setsockopt () IP_RECVTTL of real
  704. * network or BSD sockets. In our implementation, the socket simply
  705. * adds a SocketIpTtlTag tag to the packet before passing the
  706. * packet up the stack.
  707. *
  708. * \param ipv4RecvTtl Whether the socket should add SocketIpv4TtlTag tag
  709. * to the packet
  710. */
  711. void SetIpRecvTtl (bool ipv4RecvTtl);
  712. /**
  713. * \brief Ask if the socket is currently passing information about IP_TTL up the stack
  714. *
  715. * This method corresponds to using getsockopt () IP_RECVTTL of real
  716. * network or BSD sockets.
  717. *
  718. * \return Whether the IP_RECVTTL is set
  719. */
  720. bool IsIpRecvTtl (void) const;
  721. /**
  722. * \brief Manually set IPv6 Hop Limit
  723. *
  724. * This method corresponds to using setsockopt () IPV6_HOPLIMIT of
  725. * real network or BSD sockets.
  726. *
  727. * \param ipHopLimit The desired Hop Limit value for IPv6 headers
  728. */
  729. virtual void SetIpv6HopLimit (uint8_t ipHopLimit);
  730. /**
  731. * \brief Query the value of IP Hop Limit field of this socket
  732. *
  733. * This method corresponds to using getsockopt () IPV6_HOPLIMIT of real network
  734. * or BSD sockets.
  735. *
  736. * \return The raw IPv6 Hop Limit value
  737. */
  738. virtual uint8_t GetIpv6HopLimit (void) const;
  739. /**
  740. * \brief Tells a socket to pass information about IPv6 Hop Limit up the stack
  741. *
  742. * This method corresponds to using setsockopt () IPV6_RECVHOPLIMIT of real
  743. * network or BSD sockets. In our implementation, the socket simply
  744. * adds a SocketIpv6HopLimitTag tag to the packet before passing the
  745. * packet up the stack.
  746. *
  747. * \param ipv6RecvHopLimit Whether the socket should add SocketIpv6HopLimitTag tag
  748. * to the packet
  749. */
  750. void SetIpv6RecvHopLimit (bool ipv6RecvHopLimit);
  751. /**
  752. * \brief Ask if the socket is currently passing information about IPv6 Hop Limit up the stack
  753. *
  754. * This method corresponds to using getsockopt () IPV6_RECVHOPLIMIT of real
  755. * network or BSD sockets.
  756. *
  757. * \return Whether the IPV6_RECVHOPLIMIT is set
  758. */
  759. bool IsIpv6RecvHopLimit (void) const;
  760. protected:
  761. /**
  762. * \brief Notify through the callback (if set) that the connection has been
  763. * established.
  764. */
  765. void NotifyConnectionSucceeded (void);
  766. /**
  767. * \brief Notify through the callback (if set) that the connection has not been
  768. * established due to an error.
  769. */
  770. void NotifyConnectionFailed (void);
  771. /**
  772. * \brief Notify through the callback (if set) that the connection has been
  773. * closed.
  774. */
  775. void NotifyNormalClose (void);
  776. /**
  777. * \brief Notify through the callback (if set) that the connection has been
  778. * closed due to an error.
  779. */
  780. void NotifyErrorClose (void);
  781. /**
  782. * \brief Notify through the callback (if set) that an incoming connection
  783. * is being requested by a remote host.
  784. *
  785. * This function returns true by default (i.e., accept all the incoming connections).
  786. * The callback (if set) might restrict this behaviour by returning zero for a
  787. * connection that should be refused.
  788. *
  789. * \param from the address the connection is incoming from
  790. * \returns true if the connection must be accepted, false otherwise.
  791. */
  792. bool NotifyConnectionRequest (const Address &from);
  793. /**
  794. * \brief Notify through the callback (if set) that a new connection has been
  795. * created.
  796. */
  797. void NotifyNewConnectionCreated (Ptr<Socket> socket, const Address &from);
  798. /**
  799. * \brief Notify through the callback (if set) that some data have been sent.
  800. *
  801. * \param size number of sent bytes.
  802. */
  803. void NotifyDataSent (uint32_t size);
  804. /**
  805. * \brief Notify through the callback (if set) that some data have been sent.
  806. *
  807. * \param spaceAvailable the number of bytes available in the transmission buffer.
  808. */
  809. void NotifySend (uint32_t spaceAvailable);
  810. /**
  811. * \brief Notify through the callback (if set) that some data have been received.
  812. */
  813. void NotifyDataRecv (void);
  814. // inherited function, no doc necessary
  815. virtual void DoDispose (void);
  816. /**
  817. * \brief Checks if the socket has a specific IPv4 ToS set
  818. *
  819. * \returns true if the socket has a IPv4 ToS set, false otherwise.
  820. */
  821. bool IsManualIpTos (void) const;
  822. /**
  823. * \brief Checks if the socket has a specific IPv6 Tclass set
  824. *
  825. * \returns true if the socket has a IPv6 Tclass set, false otherwise.
  826. */
  827. bool IsManualIpv6Tclass (void) const;
  828. /**
  829. * \brief Checks if the socket has a specific IPv4 TTL set
  830. *
  831. * \returns true if the socket has a IPv4 TTL set, false otherwise.
  832. */
  833. bool IsManualIpTtl (void) const;
  834. /**
  835. * \brief Checks if the socket has a specific IPv6 Hop Limit set
  836. *
  837. * \returns true if the socket has a IPv6 Hop Limit set, false otherwise.
  838. */
  839. bool IsManualIpv6HopLimit (void) const;
  840. Ptr<NetDevice> m_boundnetdevice; //!< the device this socket is bound to (might be null).
  841. bool m_recvPktInfo; //!< if the socket should add packet info tags to the packet forwarded to L4.
  842. private:
  843. Callback<void, Ptr<Socket> > m_connectionSucceeded; //!< connection succeeded callback
  844. Callback<void, Ptr<Socket> > m_connectionFailed; //!< connection failed callback
  845. Callback<void, Ptr<Socket> > m_normalClose; //!< connection closed callback
  846. Callback<void, Ptr<Socket> > m_errorClose; //!< connection closed due to errors callback
  847. Callback<bool, Ptr<Socket>, const Address &> m_connectionRequest; //!< connection request callback
  848. Callback<void, Ptr<Socket>, const Address&> m_newConnectionCreated; //!< connection created callback
  849. Callback<void, Ptr<Socket>, uint32_t> m_dataSent; //!< data sent callback
  850. Callback<void, Ptr<Socket>, uint32_t > m_sendCb; //!< packet sent callback
  851. Callback<void, Ptr<Socket> > m_receivedData; //!< data received callback
  852. //IPv4 options
  853. bool m_manualIpTos; //!< socket has IPv4 TOS set
  854. bool m_manualIpTtl; //!< socket has IPv4 TTL set
  855. bool m_ipRecvTos; //!< socket forwards IPv4 TOS tag to L4
  856. bool m_ipRecvTtl; //!< socket forwards IPv4 TTL tag to L4
  857. uint8_t m_ipTos; //!< the socket IPv4 TOS
  858. uint8_t m_ipTtl; //!< the socket IPv4 TTL
  859. //IPv6 options
  860. bool m_manualIpv6Tclass; //!< socket has IPv6 Tclass set
  861. bool m_manualIpv6HopLimit; //!< socket has IPv6 Hop Limit set
  862. bool m_ipv6RecvTclass; //!< socket forwards IPv6 Tclass tag to L4
  863. bool m_ipv6RecvHopLimit; //!< socket forwards IPv6 Hop Limit tag to L4
  864. uint8_t m_ipv6Tclass; //!< the socket IPv6 Tclass
  865. uint8_t m_ipv6HopLimit; //!< the socket IPv6 Hop Limit
  866. };
  867. /**
  868. * \brief This class implements a tag that carries an address
  869. * of a packet across the socket interface.
  870. */
  871. class SocketAddressTag : public Tag
  872. {
  873. public:
  874. SocketAddressTag ();
  875. /**
  876. * \brief Set the tag's address
  877. *
  878. * \param addr the address
  879. */
  880. void SetAddress (Address addr);
  881. /**
  882. * \brief Get the tag's address
  883. *
  884. * \returns the address
  885. */
  886. Address GetAddress (void) const;
  887. /**
  888. * \brief Get the type ID.
  889. * \return the object TypeId
  890. */
  891. static TypeId GetTypeId (void);
  892. // inherited function, no need to doc.
  893. virtual TypeId GetInstanceTypeId (void) const;
  894. // inherited function, no need to doc.
  895. virtual uint32_t GetSerializedSize (void) const;
  896. // inherited function, no need to doc.
  897. virtual void Serialize (TagBuffer i) const;
  898. // inherited function, no need to doc.
  899. virtual void Deserialize (TagBuffer i);
  900. // inherited function, no need to doc.
  901. virtual void Print (std::ostream &os) const;
  902. private:
  903. Address m_address; //!< the address carried by the tag
  904. };
  905. /**
  906. * \brief This class implements a tag that carries the socket-specific
  907. * TTL of a packet to the IP layer
  908. */
  909. class SocketIpTtlTag : public Tag
  910. {
  911. public:
  912. SocketIpTtlTag ();
  913. /**
  914. * \brief Set the tag's TTL
  915. *
  916. * \param ttl the TTL
  917. */
  918. void SetTtl (uint8_t ttl);
  919. /**
  920. * \brief Get the tag's TTL
  921. *
  922. * \returns the TTL
  923. */
  924. uint8_t GetTtl (void) const;
  925. /**
  926. * \brief Get the type ID.
  927. * \return the object TypeId
  928. */
  929. static TypeId GetTypeId (void);
  930. // inherited function, no need to doc.
  931. virtual TypeId GetInstanceTypeId (void) const;
  932. // inherited function, no need to doc.
  933. virtual uint32_t GetSerializedSize (void) const;
  934. // inherited function, no need to doc.
  935. virtual void Serialize (TagBuffer i) const;
  936. // inherited function, no need to doc.
  937. virtual void Deserialize (TagBuffer i);
  938. // inherited function, no need to doc.
  939. virtual void Print (std::ostream &os) const;
  940. private:
  941. uint8_t m_ttl; //!< the ttl carried by the tag
  942. };
  943. /**
  944. * \brief This class implements a tag that carries the socket-specific
  945. * HOPLIMIT of a packet to the IPv6 layer
  946. */
  947. class SocketIpv6HopLimitTag : public Tag
  948. {
  949. public:
  950. SocketIpv6HopLimitTag ();
  951. /**
  952. * \brief Set the tag's Hop Limit
  953. *
  954. * \param hopLimit the Hop Limit
  955. */
  956. void SetHopLimit (uint8_t hopLimit);
  957. /**
  958. * \brief Get the tag's Hop Limit
  959. *
  960. * \returns the Hop Limit
  961. */
  962. uint8_t GetHopLimit (void) const;
  963. /**
  964. * \brief Get the type ID.
  965. * \return the object TypeId
  966. */
  967. static TypeId GetTypeId (void);
  968. // inherited function, no need to doc.
  969. virtual TypeId GetInstanceTypeId (void) const;
  970. // inherited function, no need to doc.
  971. virtual uint32_t GetSerializedSize (void) const;
  972. // inherited function, no need to doc.
  973. virtual void Serialize (TagBuffer i) const;
  974. // inherited function, no need to doc.
  975. virtual void Deserialize (TagBuffer i);
  976. // inherited function, no need to doc.
  977. virtual void Print (std::ostream &os) const;
  978. private:
  979. uint8_t m_hopLimit; //!< the Hop Limit carried by the tag
  980. };
  981. /**
  982. * \brief indicates whether packets should be sent out with
  983. * the DF (Don't Fragment) flag set.
  984. */
  985. class SocketSetDontFragmentTag : public Tag
  986. {
  987. public:
  988. SocketSetDontFragmentTag ();
  989. /**
  990. * \brief Enables the DF (Don't Fragment) flag
  991. */
  992. void Enable (void);
  993. /**
  994. * \brief Disables the DF (Don't Fragment) flag
  995. */
  996. void Disable (void);
  997. /**
  998. * \brief Checks if the DF (Don't Fragment) flag is set
  999. *
  1000. * \returns true if DF is set.
  1001. */
  1002. bool IsEnabled (void) const;
  1003. /**
  1004. * \brief Get the type ID.
  1005. * \return the object TypeId
  1006. */
  1007. static TypeId GetTypeId (void);
  1008. // inherited function, no need to doc.
  1009. virtual TypeId GetInstanceTypeId (void) const;
  1010. // inherited function, no need to doc.
  1011. virtual uint32_t GetSerializedSize (void) const;
  1012. // inherited function, no need to doc.
  1013. virtual void Serialize (TagBuffer i) const;
  1014. // inherited function, no need to doc.
  1015. virtual void Deserialize (TagBuffer i);
  1016. // inherited function, no need to doc.
  1017. virtual void Print (std::ostream &os) const;
  1018. private:
  1019. bool m_dontFragment; //!< DF bit value for outgoing packets.
  1020. };
  1021. /**
  1022. * \brief indicates whether the socket has IP_TOS set.
  1023. * This tag is for IPv4 socket.
  1024. */
  1025. class SocketIpTosTag : public Tag
  1026. {
  1027. public:
  1028. SocketIpTosTag ();
  1029. /**
  1030. * \brief Set the tag's TOS
  1031. *
  1032. * \param tos the TOS
  1033. */
  1034. void SetTos (uint8_t tos);
  1035. /**
  1036. * \brief Get the tag's TOS
  1037. *
  1038. * \returns the TOS
  1039. */
  1040. uint8_t GetTos (void) const;
  1041. /**
  1042. * \brief Get the type ID.
  1043. * \return the object TypeId
  1044. */
  1045. static TypeId GetTypeId (void);
  1046. // inherited function, no need to doc.
  1047. virtual TypeId GetInstanceTypeId (void) const;
  1048. // inherited function, no need to doc.
  1049. virtual uint32_t GetSerializedSize (void) const;
  1050. // inherited function, no need to doc.
  1051. virtual void Serialize (TagBuffer i) const;
  1052. // inherited function, no need to doc.
  1053. virtual void Deserialize (TagBuffer i);
  1054. // inherited function, no need to doc.
  1055. virtual void Print (std::ostream &os) const;
  1056. private:
  1057. uint8_t m_ipTos; //!< the TOS carried by the tag
  1058. };
  1059. /**
  1060. * \brief indicates whether the socket has IPV6_TCLASS set.
  1061. * This tag is for IPv6 socket.
  1062. */
  1063. class SocketIpv6TclassTag : public Tag
  1064. {
  1065. public:
  1066. SocketIpv6TclassTag ();
  1067. /**
  1068. * \brief Set the tag's Tclass
  1069. *
  1070. * \param tclass the Tclass
  1071. */
  1072. void SetTclass (uint8_t tclass);
  1073. /**
  1074. * \brief Get the tag's Tclass
  1075. *
  1076. * \returns the Tclass
  1077. */
  1078. uint8_t GetTclass (void) const;
  1079. /**
  1080. * \brief Get the type ID.
  1081. * \return the object TypeId
  1082. */
  1083. static TypeId GetTypeId (void);
  1084. // inherited function, no need to doc.
  1085. virtual TypeId GetInstanceTypeId (void) const;
  1086. // inherited function, no need to doc.
  1087. virtual uint32_t GetSerializedSize (void) const;
  1088. // inherited function, no need to doc.
  1089. virtual void Serialize (TagBuffer i) const;
  1090. // inherited function, no need to doc.
  1091. virtual void Deserialize (TagBuffer i);
  1092. // inherited function, no need to doc.
  1093. virtual void Print (std::ostream &os) const;
  1094. private:
  1095. uint8_t m_ipv6Tclass; //!< the Tclass carried by the tag
  1096. };
  1097. } // namespace ns3
  1098. #endif /* NS3_SOCKET_H */