PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/network/model/socket.h

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