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