src/node/socket.h
author Tom Henderson <tomh@tomh.org>
Wed Aug 05 20:53:44 2009 -0700 (2009-08-05)
changeset 4697 6e048d6486d8
parent 4357 9c638ff9f732
child 5227 ecb08c1fc273
permissions -rw-r--r--
Implement UdpSocketImpl::Close ()
mathieu@1264
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@1264
     2
/*
mathieu@1264
     3
 * Copyright (c) 2006 Georgia Tech Research Corporation
mathieu@1264
     4
 *               2007 INRIA
mathieu@1264
     5
 *
mathieu@1264
     6
 * This program is free software; you can redistribute it and/or modify
mathieu@1264
     7
 * it under the terms of the GNU General Public License version 2 as
mathieu@1264
     8
 * published by the Free Software Foundation;
mathieu@1264
     9
 *
mathieu@1264
    10
 * This program is distributed in the hope that it will be useful,
mathieu@1264
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@1264
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@1264
    13
 * GNU General Public License for more details.
mathieu@1264
    14
 *
mathieu@1264
    15
 * You should have received a copy of the GNU General Public License
mathieu@1264
    16
 * along with this program; if not, write to the Free Software
mathieu@1264
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@1264
    18
 *
mathieu@1264
    19
 * Authors: George F. Riley<riley@ece.gatech.edu>
mathieu@1264
    20
 *          Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@1264
    21
 */
tomh@349
    22
tomh@349
    23
#ifndef __SOCKET_H__
tomh@349
    24
#define __SOCKET_H__
tomh@349
    25
tomh@349
    26
#include "ns3/callback.h"
mathieu@557
    27
#include "ns3/ptr.h"
tomh@3098
    28
#include "ns3/tag.h"
mathieu@498
    29
#include "ns3/object.h"
mathieu@1162
    30
#include "address.h"
mathieu@453
    31
#include <stdint.h>
tomh@349
    32
mathieu@453
    33
namespace ns3 {
tomh@349
    34
tomh@3098
    35
mathieu@728
    36
class Node;
mathieu@1264
    37
class Packet;
tomh@349
    38
mathieu@453
    39
/**
tomh@3143
    40
 * \ingroup node
tomh@3143
    41
 * \defgroup socket Socket
mathieu@3222
    42
 */
mathieu@3222
    43
mathieu@3222
    44
/**
tomh@3143
    45
 * \brief A low-level Socket API based loosely on the BSD Socket API.
mathieu@3222
    46
 * \ingroup socket
mathieu@453
    47
 *
tomh@3143
    48
 * A few things to keep in mind about this type of socket:
tomh@3143
    49
 * - it uses ns-3 API constructs such as class ns3::Address instead of
tomh@3143
    50
 *   C-style structs
tomh@3143
    51
 * - in contrast to the original BSD socket API, this API is asynchronous:
tomh@3143
    52
 *   it does not contain blocking calls.  Sending and receiving operations
tomh@3143
    53
 *   must make use of the callbacks provided. 
tomh@3143
    54
 * - It also uses class ns3::Packet as a fancy byte buffer, allowing 
tomh@3143
    55
 *   data to be passed across the API using an ns-3 Packet instead of 
tomh@3143
    56
 *   a raw data pointer.  
tomh@3143
    57
 * - Not all of the full POSIX sockets API is supported
tomh@3143
    58
 *
tomh@3143
    59
 * Other than that, it tries to stick to the BSD API to make it 
tomh@3143
    60
 * easier for those who know the BSD API to use this API.  
tomh@3143
    61
 * More details are provided in the ns-3 tutorial.
mathieu@453
    62
 */
mathieu@498
    63
class Socket : public Object
mathieu@498
    64
{
tomh@349
    65
public:
tomh@3127
    66
tomh@3107
    67
  Socket (void);
tomh@3107
    68
  virtual ~Socket (void);
mathieu@453
    69
mathieu@453
    70
  enum SocketErrno {
gjc@646
    71
    ERROR_NOTERROR,
gjc@646
    72
    ERROR_ISCONN,
gjc@646
    73
    ERROR_NOTCONN,
gjc@646
    74
    ERROR_MSGSIZE,
gjc@646
    75
    ERROR_AGAIN,
gjc@646
    76
    ERROR_SHUTDOWN,
gjc@646
    77
    ERROR_OPNOTSUPP,
mathieu@1186
    78
    ERROR_AFNOSUPPORT,
mathieu@1172
    79
    ERROR_INVAL,
mathieu@1186
    80
    ERROR_BADF,
tomh@1317
    81
    ERROR_NOROUTETOHOST,
mathieu@453
    82
    SOCKET_ERRNO_LAST
mathieu@453
    83
  };
mathieu@453
    84
mathieu@453
    85
  /**
tomh@3116
    86
   * This method wraps the creation of sockets that is performed
tomh@3116
    87
   * by a socket factory on a given node based on a TypeId.
tomh@3116
    88
   * 
tomh@3116
    89
   * \return A smart pointer to a newly created socket.
tomh@3116
    90
   * 
tomh@3116
    91
   * \param node The node on which to create the socket
tomh@3116
    92
   * \param tid The TypeId of the socket to create
tomh@3116
    93
   */
tomh@3116
    94
  static Ptr<Socket> CreateSocket (Ptr<Node> node, TypeId tid);
tomh@3116
    95
  /**
mathieu@453
    96
   * \return the errno associated to the last call which failed in this
mathieu@453
    97
   *         socket. Each socket's errno is initialized to zero
mathieu@453
    98
   *         when the socket is created.
mathieu@453
    99
   */
mathieu@453
   100
  virtual enum Socket::SocketErrno GetErrno (void) const = 0;
mathieu@453
   101
  /**
mathieu@453
   102
   * \returns the node this socket is associated with.
mathieu@453
   103
   */
mathieu@728
   104
  virtual Ptr<Node> GetNode (void) const = 0;
mathieu@1264
   105
  /**
tomh@2303
   106
   * \param connectionSucceeded this callback is invoked when the 
tomh@2303
   107
   *        connection request initiated by the user is successfully 
tomh@2303
   108
   *        completed. The callback is passed  back a pointer to 
tomh@2303
   109
   *        the same socket object.
tomh@2303
   110
   * \param connectionFailed this callback is invoked when the 
tomh@2303
   111
   *        connection request initiated by the user is unsuccessfully 
tomh@2303
   112
   *        completed. The callback is passed back a pointer to the 
tomh@2303
   113
   *        same socket object. 
mathieu@1264
   114
   */
mathieu@1264
   115
  void SetConnectCallback (Callback<void, Ptr<Socket> > connectionSucceeded,
craigdo@3276
   116
                           Callback<void,  Ptr<Socket> > connectionFailed);
mathieu@1264
   117
  /**
mathieu@1264
   118
   * \brief Accept connection requests from remote hosts
mathieu@1264
   119
   * \param connectionRequest Callback for connection request from peer. 
mathieu@1264
   120
   *        This user callback is passed a pointer to this socket, the 
mathieu@1264
   121
   *        ip address and the port number of the connection originator. 
mathieu@1264
   122
   *        This callback must return true to accept the incoming connection,
mathieu@1264
   123
   *        false otherwise. If the connection is accepted, the 
tomh@2303
   124
   *        "newConnectionCreated" callback will be invoked later to 
tomh@2303
   125
   *        give access to the user to the socket created to match 
tomh@2303
   126
   *        this new connection. If the user does not explicitly 
tomh@2303
   127
   *        specify this callback, all incoming  connections will be refused.
mathieu@1264
   128
   * \param newConnectionCreated Callback for new connection: when a new
mathieu@1264
   129
   *        is accepted, it is created and the corresponding socket is passed
tomh@2303
   130
   *        back to the user through this callback. This user callback is 
tomh@2303
   131
   *        passed a pointer to the new socket, and the ip address and 
tomh@2303
   132
   *        port number of the connection originator.
mathieu@1264
   133
   */
tomh@2303
   134
  void SetAcceptCallback (Callback<bool, Ptr<Socket>, 
tomh@2303
   135
                            const Address &> connectionRequest,
tomh@2303
   136
                          Callback<void, Ptr<Socket>, 
craigdo@3276
   137
                            const Address&> newConnectionCreated);
tomh@2999
   138
  /**
tomh@2999
   139
   * \brief Notify application when a packet has been sent from transport 
tomh@2999
   140
   *        protocol (non-standard socket call)
tomh@2999
   141
   * \param dataSent Callback for the event that data is sent from the
tomh@2999
   142
   *        underlying transport protocol.  This callback is passed a
tomh@2999
   143
   *        pointer to the socket, and the number of bytes sent.
tomh@2999
   144
   */
craigdo@3774
   145
  void SetDataSentCallback (Callback<void, Ptr<Socket>, 
mathieu@3521
   146
                            uint32_t> dataSent);
tomh@2999
   147
  /**
tomh@2999
   148
   * \brief Notify application when space in transmit buffer is added
tomh@2999
   149
   *
tomh@2999
   150
   *        This callback is intended to notify a 
tomh@2999
   151
   *        socket that would have been blocked in a blocking socket model
tomh@3000
   152
   *        that space is available in the transmit buffer and that it
tomh@3000
   153
   *        can call Send() again.  
tomh@2999
   154
   *
tomh@2999
   155
   * \param sendCb Callback for the event that the socket transmit buffer
tomh@2999
   156
   *        fill level has decreased.  This callback is passed a pointer to
tomh@2999
   157
   *        the socket, and the number of bytes available for writing
tomh@2999
   158
   *        into the buffer (an absolute value).  If there is no transmit
tomh@2999
   159
   *        buffer limit, a maximum-sized integer is always returned.
tomh@2999
   160
   */
tomh@2999
   161
  void SetSendCallback (Callback<void, Ptr<Socket>, uint32_t> sendCb);
mathieu@1264
   162
  /**
tomh@3102
   163
   * \brief Notify application when new data is available to be read.
mathieu@1264
   164
   *
tomh@3102
   165
   *        This callback is intended to notify a socket that would
tomh@3102
   166
   *        have been blocked in a blocking socket model that data
tomh@3102
   167
   *        is available to be read.
mathieu@1264
   168
   */
tomh@3102
   169
  void SetRecvCallback (Callback<void, Ptr<Socket> >);
mathieu@453
   170
  /** 
mathieu@1162
   171
   * \param address the address to try to allocate
mathieu@1162
   172
   * \returns 0 on success, -1 on failure.
mathieu@1162
   173
   *
mathieu@1162
   174
   * Allocate a local endpoint for this socket.
mathieu@1162
   175
   */
mathieu@1162
   176
  virtual int Bind (const Address &address) = 0;
mathieu@1162
   177
mathieu@1162
   178
  /** 
mathieu@1162
   179
   * Allocate a local endpoint for this socket.
mathieu@453
   180
   *
mathieu@453
   181
   * \returns 0 on success, -1 on failure.
mathieu@453
   182
   */
mathieu@1162
   183
  virtual int Bind () = 0;
mathieu@453
   184
mathieu@453
   185
  /** 
mathieu@453
   186
   * \brief Close a socket.
tomh@4697
   187
   * \returns zero on success, -1 on failure.
mathieu@453
   188
   *
mathieu@453
   189
   * After the Close call, the socket is no longer valid, and cannot
mathieu@453
   190
   * safely be used for subsequent operations.
mathieu@453
   191
   */
tomh@3000
   192
  virtual int Close (void) = 0;
mathieu@453
   193
mathieu@453
   194
  /**
mathieu@453
   195
   * \returns zero on success, -1 on failure.
mathieu@453
   196
   *
mathieu@453
   197
   * Do not allow any further Send calls. This method is typically
mathieu@453
   198
   * implemented for Tcp sockets by a half close.
mathieu@453
   199
   */
mathieu@453
   200
  virtual int ShutdownSend (void) = 0;
mathieu@453
   201
mathieu@453
   202
  /**
mathieu@453
   203
   * \returns zero on success, -1 on failure.
mathieu@453
   204
   *
mathieu@453
   205
   * Do not allow any further Recv calls. This method is typically
mathieu@453
   206
   * implemented for Tcp sockets by a half close.
mathieu@453
   207
   */
mathieu@453
   208
  virtual int ShutdownRecv (void) = 0;
mathieu@453
   209
mathieu@453
   210
  /**
mathieu@453
   211
   * \brief Initiate a connection to a remote host
mathieu@1162
   212
   * \param address Address of remote.
mathieu@453
   213
   */
tomh@3000
   214
  virtual int Connect (const Address &address) = 0;
mathieu@453
   215
    
mathieu@453
   216
  /**
tomh@3000
   217
   * \brief Listen for incoming connections.
mathieu@3213
   218
   * \returns 0 on success, -1 on error (in which case errno is set).
tomh@3000
   219
   */
craigdo@3772
   220
  virtual int Listen (void) = 0;
tomh@3000
   221
tomh@3000
   222
  /**
tomh@3269
   223
   * \brief Returns the number of bytes which can be sent in a single call
tomh@3269
   224
   * to Send. 
tomh@3269
   225
   * 
tomh@3269
   226
   * For datagram sockets, this returns the number of bytes that
tomh@3269
   227
   * can be passed atomically through the underlying protocol.
tomh@3269
   228
   *
tomh@3269
   229
   * For stream sockets, this returns the available space in bytes
tomh@3269
   230
   * left in the transmit buffer.
tomh@3269
   231
   */
tomh@3269
   232
  virtual uint32_t GetTxAvailable (void) const = 0;
tomh@3269
   233
 
tomh@3269
   234
  /**
mathieu@453
   235
   * \brief Send data (or dummy data) to the remote host
tomh@3122
   236
   *
tomh@3122
   237
   * This function matches closely in semantics to the send() function
tomh@3122
   238
   * call in the standard C library (libc):
tomh@3122
   239
   *   ssize_t send (int s, const void *msg, size_t len, int flags);
tomh@3269
   240
   * except that the send I/O is asynchronous.  This is the
tomh@3269
   241
   * primary Send method at this low-level API and must be implemented 
tomh@3269
   242
   * by subclasses.
tomh@3122
   243
   * 
tomh@3122
   244
   * In a typical blocking sockets model, this call would block upon
tomh@3122
   245
   * lack of space to hold the message to be sent.  In ns-3 at this
tomh@3122
   246
   * API, the call returns immediately in such a case, but the callback
tomh@3122
   247
   * registered with SetSendCallback() is invoked when the socket
tomh@3122
   248
   * has space (when it conceptually unblocks); this is an asynchronous
tomh@3122
   249
   * I/O model for send().
tomh@3122
   250
   * 
tomh@3122
   251
   * This variant of Send() uses class ns3::Packet to encapsulate
tomh@3122
   252
   * data, rather than providing a raw pointer and length field.  
tomh@3122
   253
   * This allows an ns-3 application to attach tags if desired (such
tomh@3122
   254
   * as a flow ID) and may allow the simulator to avoid some data
tomh@3122
   255
   * copies.  Despite the appearance of sending Packets on a stream
tomh@3122
   256
   * socket, just think of it as a fancy byte buffer with streaming
tomh@3122
   257
   * semantics.    
tomh@3122
   258
   *
tomh@3122
   259
   * If either the message buffer within the Packet is too long to pass 
tomh@3122
   260
   * atomically through the underlying protocol (for datagram sockets), 
tomh@3122
   261
   * or the message buffer cannot entirely fit in the transmit buffer
tomh@3122
   262
   * (for stream sockets), -1 is returned and SocketErrno is set 
tomh@3122
   263
   * to ERROR_MSGSIZE.  If the packet does not fit, the caller can
tomh@3122
   264
   * split the Packet (based on information obtained from 
tomh@3122
   265
   * GetTxAvailable) and reattempt to send the data.
tomh@3122
   266
   *
tomh@3269
   267
   * The flags argument is formed by or'ing one or more of the values:     
tomh@3269
   268
   *        MSG_OOB        process out-of-band data 
tomh@3269
   269
   *        MSG_DONTROUTE  bypass routing, use direct interface 
tomh@3269
   270
   * These flags are _unsupported_ as of ns-3.1.  
tomh@3269
   271
   *
tomh@3122
   272
   * \param p ns3::Packet to send
tomh@3269
   273
   * \param flags Socket control flags
tomh@3122
   274
   * \returns the number of bytes accepted for transmission if no error
tomh@3122
   275
   *          occurs, and -1 otherwise.
tomh@3269
   276
   *
tomh@3269
   277
   * \see SetSendCallback
mathieu@453
   278
   */
tomh@3269
   279
  virtual int Send (Ptr<Packet> p, uint32_t flags) = 0;
tomh@3105
   280
tomh@3105
   281
  /**
mathieu@453
   282
   * \brief Send data to a specified peer.
tomh@3269
   283
   *
tomh@3269
   284
   * This method has similar semantics to Send () but subclasses may
tomh@3269
   285
   * want to provide checks on socket state, so the implementation is
tomh@3269
   286
   * pushed to subclasses.
tomh@3269
   287
   *
tomh@3120
   288
   * \param p packet to send
tomh@3269
   289
   * \param flags Socket control flags
tomh@3269
   290
   * \param toAddress IP Address of remote host
mathieu@453
   291
   * \returns -1 in case of error or the number of bytes copied in the 
mathieu@453
   292
   *          internal buffer and accepted for transmission.
mathieu@453
   293
   */
tomh@3269
   294
  virtual int SendTo (Ptr<Packet> p, uint32_t flags, 
tomh@3269
   295
    const Address &toAddress) = 0;
raj@2224
   296
raj@2224
   297
  /**
tomh@3104
   298
   * Return number of bytes which can be returned from one or 
tomh@3104
   299
   * multiple calls to Recv.
tomh@3104
   300
   * Must be possible to call this method from the Recv callback.
tomh@3104
   301
   */
tomh@3104
   302
  virtual uint32_t GetRxAvailable (void) const = 0;
tomh@3269
   303
tomh@3269
   304
  /**
tomh@3269
   305
   * \brief Read data from the socket
tomh@3269
   306
   *
tomh@3269
   307
   * This function matches closely in semantics to the recv() function
tomh@3269
   308
   * call in the standard C library (libc):
tomh@3269
   309
   *   ssize_t recv (int s, void *buf, size_t len, int flags);
tomh@3269
   310
   * except that the receive I/O is asynchronous.  This is the
tomh@3269
   311
   * primary Recv method at this low-level API and must be implemented 
tomh@3269
   312
   * by subclasses.
tomh@3269
   313
   * 
tomh@3269
   314
   * This method is normally used only on a connected socket.
tomh@3269
   315
   * In a typical blocking sockets model, this call would block until
tomh@3269
   316
   * at least one byte is returned or the connection closes.  
tomh@3269
   317
   * In ns-3 at this API, the call returns immediately in such a case
tomh@3269
   318
   * and returns 0 if nothing is available to be read.
tomh@3269
   319
   * However, an application can set a callback, ns3::SetRecvCallback,
tomh@3269
   320
   * to be notified of data being available to be read
tomh@3269
   321
   * (when it conceptually unblocks); this is an asynchronous
tomh@3269
   322
   * I/O model for recv().
tomh@3269
   323
   * 
tomh@3269
   324
   * This variant of Recv() uses class ns3::Packet to encapsulate
tomh@3269
   325
   * data, rather than providing a raw pointer and length field.  
tomh@3269
   326
   * This allows an ns-3 application to attach tags if desired (such
tomh@3269
   327
   * as a flow ID) and may allow the simulator to avoid some data
tomh@3269
   328
   * copies.  Despite the appearance of receiving Packets on a stream
tomh@3269
   329
   * socket, just think of it as a fancy byte buffer with streaming
tomh@3269
   330
   * semantics.    
tomh@3269
   331
   *
tomh@3269
   332
   * The semantics depend on the type of socket.  For a datagram socket,
tomh@3269
   333
   * each Recv() returns the data from at most one Send(), and order
tomh@3269
   334
   * is not necessarily preserved.  For a stream socket, the bytes
tomh@3269
   335
   * are delivered in order, and on-the-wire packet boundaries are
tomh@3269
   336
   * not preserved.  
tomh@3269
   337
   * 
tomh@3269
   338
   * The flags argument is formed by or'ing one or more of the values:     
craigdo@3820
   339
   *        MSG_OOB             process out-of-band data
craigdo@3820
   340
   *        MSG_PEEK            peek at incoming message
craigdo@3820
   341
   * None of these flags are supported for now.
tomh@3269
   342
   *
tomh@3269
   343
   * Some variants of Recv() are supported as additional API,
tomh@3269
   344
   * including RecvFrom(), overloaded Recv() without arguments,
tomh@3269
   345
   * and variants that use raw character buffers.
tomh@3269
   346
   *
tomh@3269
   347
   * \param maxSize reader will accept packet up to maxSize
tomh@3269
   348
   * \param flags Socket control flags
tomh@3269
   349
   * \returns Ptr<Packet> of the next in-sequence packet.  Returns
tomh@3269
   350
   * 0 if the socket cannot return a next in-sequence packet conforming
tomh@3269
   351
   * to the maxSize and flags.
tomh@3269
   352
   *
tomh@3269
   353
   * \see SetRecvCallback
tomh@3269
   354
   */
tomh@3269
   355
  virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags) = 0;
tomh@3269
   356
tomh@3269
   357
  /**
tomh@3269
   358
   * \brief Read a single packet from the socket and retrieve the sender 
tomh@3269
   359
   * address.
tomh@3269
   360
   *
tomh@3269
   361
   * Calls Recv(maxSize, flags) with maxSize
tomh@3269
   362
   * implicitly set to maximum sized integer, and flags set to zero.
tomh@3269
   363
   *
tomh@3269
   364
   * This method has similar semantics to Recv () but subclasses may   
tomh@3269
   365
   * want to provide checks on socket state, so the implementation is   
tomh@3269
   366
   * pushed to subclasses.
tomh@3269
   367
   *
tomh@3269
   368
   * \param maxSize reader will accept packet up to maxSize
tomh@3269
   369
   * \param flags Socket control flags
tomh@3269
   370
   * \param fromAddress output parameter that will return the
tomh@3269
   371
   * address of the sender of the received packet, if any.  Remains
tomh@3269
   372
   * untouched if no packet is received.
tomh@3269
   373
   * \returns Ptr<Packet> of the next in-sequence packet.  Returns
tomh@3269
   374
   * 0 if the socket cannot return a next in-sequence packet.
tomh@3269
   375
   */
tomh@3269
   376
  virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags,  
tomh@3269
   377
    Address &fromAddress) = 0;
tomh@3269
   378
tomh@3269
   379
  /////////////////////////////////////////////////////////////////////
tomh@3269
   380
  //   The remainder of these public methods are overloaded methods  //
tomh@3269
   381
  //   or variants of Send() and Recv(), and they are non-virtual    //
tomh@3269
   382
  /////////////////////////////////////////////////////////////////////
tomh@3269
   383
 
tomh@3269
   384
  /**
tomh@3269
   385
   * \brief Send data (or dummy data) to the remote host
tomh@3269
   386
   * 
tomh@3269
   387
   * Overloaded version of Send(..., flags) with flags set to zero.
tomh@3269
   388
   *
tomh@3269
   389
   * \param p ns3::Packet to send
tomh@3269
   390
   * \returns the number of bytes accepted for transmission if no error
tomh@3269
   391
   *          occurs, and -1 otherwise.
tomh@3269
   392
   */
tomh@3269
   393
  int Send (Ptr<Packet> p);
tomh@3269
   394
tomh@3269
   395
  /**
tomh@3269
   396
   * \brief Send data (or dummy data) to the remote host
tomh@3269
   397
   * 
tomh@3269
   398
   * This method is provided so as to have an API which is closer in 
tomh@3269
   399
   * appearance to that of real network or BSD sockets.  
tomh@3269
   400
   *
tomh@3269
   401
   * \param buf A pointer to a raw byte buffer of some data to send.  If 
tomh@3269
   402
   * this buffer is 0, we send dummy data whose size is specified by the 
tomh@3269
   403
   * second parameter
tomh@3269
   404
   * \param size the number of bytes to copy from the buffer
tomh@3269
   405
   * \param flags Socket control flags
tomh@3269
   406
   */
tomh@3269
   407
  int Send (const uint8_t* buf, uint32_t size, uint32_t flags);
tomh@3269
   408
  
tomh@3269
   409
tomh@3269
   410
  /**
tomh@3269
   411
   * \brief Send data to a specified peer.
tomh@3269
   412
   *
tomh@3269
   413
   * This method is provided so as to have an API which is closer in 
tomh@3269
   414
   * appearance to that of real network or BSD sockets.  
tomh@3269
   415
   *
tomh@3269
   416
   * \param buf A pointer to a raw byte buffer of some data to send.  
tomh@3269
   417
   * If this is 0, we send dummy data whose size is specified by the 
tomh@3269
   418
   * third parameter
tomh@3269
   419
   * \param size the number of bytes to copy from the buffer
tomh@3269
   420
   * \param flags Socket control flags
tomh@3269
   421
   * \param address IP Address of remote host
tomh@3269
   422
   * \returns -1 in case of error or the number of bytes copied in the 
tomh@3269
   423
   *          internal buffer and accepted for transmission.
tomh@3269
   424
   *
tomh@3269
   425
   */
tomh@3269
   426
  int SendTo (const uint8_t* buf, uint32_t size, uint32_t flags, 
tomh@3269
   427
              const Address &address); 
tomh@3269
   428
tomh@3269
   429
  /**
tomh@3269
   430
   * \brief Read a single packet from the socket
tomh@3269
   431
   *
tomh@3269
   432
   * Overloaded version of Recv(maxSize, flags) with maxSize
tomh@3269
   433
   * implicitly set to maximum sized integer, and flags set to zero.
tomh@3269
   434
   *
tomh@3269
   435
   * \returns Ptr<Packet> of the next in-sequence packet.  Returns
tomh@3269
   436
   * 0 if the socket cannot return a next in-sequence packet.
tomh@3269
   437
   */
tomh@3269
   438
   Ptr<Packet> Recv (void);
tomh@3269
   439
tomh@3269
   440
  /**
tomh@3269
   441
   * \brief Recv data (or dummy data) from the remote host
tomh@3269
   442
   *
tomh@3269
   443
   * This method is provided so as to have an API which is closer in 
tomh@3269
   444
   * appearance to that of real network or BSD sockets.  
tomh@3269
   445
   * 
tomh@3269
   446
   * If the underlying packet was carring null (fake) data, this buffer
tomh@3269
   447
   * will be zeroed up to the length specified by the return value.
tomh@3269
   448
   *
tomh@3269
   449
   * \param buf A pointer to a raw byte buffer to write the data to. 
tomh@3269
   450
   * \param size Number of bytes (at most) to copy to buf
tomh@3269
   451
   * \param flags any flags to pass to the socket
tomh@3269
   452
   * \returns number of bytes copied into buf
tomh@3269
   453
   */
tomh@3269
   454
  int Recv (uint8_t* buf, uint32_t size, uint32_t flags);
tomh@3269
   455
tomh@3269
   456
  /**
tomh@3269
   457
   * \brief Read a single packet from the socket and retrieve the sender 
tomh@3269
   458
   * address.
tomh@3269
   459
   *
tomh@3269
   460
   * Calls RecvFrom (maxSize, flags, fromAddress) with maxSize
tomh@3269
   461
   * implicitly set to maximum sized integer, and flags set to zero.
tomh@3269
   462
   *
tomh@3269
   463
   * \param fromAddress output parameter that will return the
tomh@3269
   464
   * address of the sender of the received packet, if any.  Remains
tomh@3269
   465
   * untouched if no packet is received.
tomh@3269
   466
   * \returns Ptr<Packet> of the next in-sequence packet.  Returns
tomh@3269
   467
   * 0 if the socket cannot return a next in-sequence packet.
tomh@3269
   468
   */
tomh@3269
   469
  Ptr<Packet> RecvFrom (Address &fromAddress);
tomh@3269
   470
tomh@3269
   471
  /**
tomh@3269
   472
   * \brief Read a single packet from the socket and retrieve the sender
tomh@3269
   473
   * address.
tomh@3269
   474
   *
tomh@3269
   475
   * This method is provided so as to have an API which is closer in 
tomh@3269
   476
   * appearance to that of real network or BSD sockets.  
tomh@3269
   477
   * 
tomh@3269
   478
   * \param buf A pointer to a raw byte buffer to write the data to. 
tomh@3269
   479
   * If the underlying packet was carring null (fake) data, this buffer
tomh@3269
   480
   * will be zeroed up to the length specified by the return value.
tomh@3269
   481
   * \param size Number of bytes (at most) to copy to buf
tomh@3269
   482
   * \param flags any flags to pass to the socket
tomh@3269
   483
   * \param fromAddress output parameter that will return the
tomh@3269
   484
   * address of the sender of the received packet, if any.  Remains
tomh@3269
   485
   * untouched if no packet is received.
tomh@3269
   486
   * \returns number of bytes copied into buf
tomh@3269
   487
   */
tomh@3269
   488
  int RecvFrom (uint8_t* buf, uint32_t size, uint32_t flags,
tomh@3269
   489
                Address &fromAddress);
craigdo@3778
   490
    /**
craigdo@3778
   491
   * \returns the address name  this socket is associated with.
craigdo@3778
   492
   */
craigdo@3778
   493
  virtual int GetSockName (Address &address) const = 0; 
tomh@3104
   494
 
mathieu@1264
   495
protected:
mathieu@1264
   496
  void NotifyConnectionSucceeded (void);
mathieu@1264
   497
  void NotifyConnectionFailed (void);
mathieu@1264
   498
  bool NotifyConnectionRequest (const Address &from);
mathieu@1264
   499
  void NotifyNewConnectionCreated (Ptr<Socket> socket, const Address &from);
mathieu@1264
   500
  void NotifyDataSent (uint32_t size);
tomh@2999
   501
  void NotifySend (uint32_t spaceAvailable);
tomh@3097
   502
  void NotifyDataRecv (void);
mathieu@4357
   503
  virtual void DoDispose (void);
mathieu@3375
   504
private:
mathieu@1264
   505
  Callback<void, Ptr<Socket> >   m_connectionSucceeded;
mathieu@1264
   506
  Callback<void, Ptr<Socket> >   m_connectionFailed;
mathieu@1264
   507
  Callback<bool, Ptr<Socket>, const Address &>   m_connectionRequest;
mathieu@1264
   508
  Callback<void, Ptr<Socket>, const Address&>    m_newConnectionCreated;
mathieu@1264
   509
  Callback<void, Ptr<Socket>, uint32_t>          m_dataSent;
tomh@2999
   510
  Callback<void, Ptr<Socket>, uint32_t >         m_sendCb;
tomh@3102
   511
  Callback<void, Ptr<Socket> > m_receivedData;
tomh@3107
   512
tomh@349
   513
};
tomh@349
   514
tomh@3098
   515
/**
tomh@3269
   516
 * \brief This class implements a tag that carries an address
tomh@3269
   517
 * of a packet across the socket interface.
tomh@3098
   518
 */
tomh@3269
   519
class SocketAddressTag : public Tag
tomh@3098
   520
{
tomh@3098
   521
public:
tomh@3269
   522
  SocketAddressTag ();
tomh@3098
   523
  void SetAddress (Address addr);
tomh@3098
   524
  Address GetAddress (void) const;
tomh@3138
   525
tomh@3138
   526
  static TypeId GetTypeId (void);  
tomh@3138
   527
  virtual TypeId GetInstanceTypeId (void) const;
tomh@3138
   528
  virtual uint32_t GetSerializedSize (void) const;
tomh@3138
   529
  virtual void Serialize (TagBuffer i) const;
tomh@3138
   530
  virtual void Deserialize (TagBuffer i);
mathieu@3208
   531
  virtual void Print (std::ostream &os) const;
tomh@3138
   532
tomh@3098
   533
private:
tomh@3098
   534
  Address m_address;
tomh@3098
   535
};
tomh@3098
   536
tomh@3124
   537
/**
tomh@3124
   538
 * \brief This class implements a tag that carries the socket-specific
tomh@3124
   539
 * TTL of a packet to the IP layer
tomh@3124
   540
 */
tomh@3124
   541
class SocketIpTtlTag : public Tag
tomh@3124
   542
{
tomh@3124
   543
public:
tomh@3124
   544
  SocketIpTtlTag ();
tomh@3124
   545
  void SetTtl (uint8_t ttl);
tomh@3124
   546
  uint8_t GetTtl (void) const;
tomh@3138
   547
tomh@3138
   548
  static TypeId GetTypeId (void);  
tomh@3138
   549
  virtual TypeId GetInstanceTypeId (void) const;
tomh@3138
   550
  virtual uint32_t GetSerializedSize (void) const;
tomh@3138
   551
  virtual void Serialize (TagBuffer i) const;
tomh@3138
   552
  virtual void Deserialize (TagBuffer i);
mathieu@3208
   553
  virtual void Print (std::ostream &os) const;
tomh@3138
   554
tomh@3124
   555
private:
tomh@3124
   556
  uint8_t m_ttl;
tomh@3124
   557
};
tomh@3124
   558
craigdo@3820
   559
craigdo@3820
   560
/**
craigdo@3820
   561
 * \brief indicated whether packets should be sent out with
craigdo@3820
   562
 * the DF flag set.
craigdo@3820
   563
 */
craigdo@3820
   564
class SocketSetDontFragmentTag : public Tag
craigdo@3820
   565
{
craigdo@3820
   566
public:
craigdo@3820
   567
  SocketSetDontFragmentTag ();
craigdo@3820
   568
  void Enable (void);
craigdo@3820
   569
  void Disable (void);
craigdo@3820
   570
  bool IsEnabled (void) const;
craigdo@3820
   571
craigdo@3820
   572
  static TypeId GetTypeId (void);  
craigdo@3820
   573
  virtual TypeId GetInstanceTypeId (void) const;
craigdo@3820
   574
  virtual uint32_t GetSerializedSize (void) const;
craigdo@3820
   575
  virtual void Serialize (TagBuffer i) const;
craigdo@3820
   576
  virtual void Deserialize (TagBuffer i);
craigdo@3820
   577
  virtual void Print (std::ostream &os) const;
craigdo@3820
   578
private:
craigdo@3820
   579
  bool m_dontFragment;
craigdo@3820
   580
};
craigdo@3820
   581
tomh@349
   582
} //namespace ns3
tomh@349
   583
mathieu@453
   584
#endif /* SOCKET_H */
tomh@349
   585
tomh@349
   586