src/node/packet-socket.h
changeset 1188 34386185bc1f
child 1195 53f1175dbe94
equal deleted inserted replaced
1187:8ea0f4d4fd34 1188:34386185bc1f
       
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2007 Emmanuelle Laprise, INRIA
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License version 2 as
       
     7  * published by the Free Software Foundation;
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    17  *
       
    18  * Authors: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>,
       
    19  *          Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 #ifndef PACKET_SOCKET_H
       
    22 #define PACKET_SOCKET_H
       
    23 
       
    24 #include <stdint.h>
       
    25 #include "ns3/callback.h"
       
    26 #include "ns3/ptr.h"
       
    27 #include "ns3/socket.h"
       
    28 
       
    29 namespace ns3 {
       
    30 
       
    31 class Node;
       
    32 class Packet;
       
    33 class NetDevice;
       
    34 class PacketSocketAddress;
       
    35 
       
    36 /**
       
    37  * \brief A PacketSocket is a link between an application and a net device.
       
    38  *
       
    39  * A PacketSocket can be used to connect an application to a net
       
    40  * device. The application provides the buffers of data, the socket
       
    41  * conserts them to a raw packet and the net device then adds the
       
    42  * protocol specific headers and trailers. This socket type
       
    43  * is very similar to the linux and BSD "packet" sockets.
       
    44  *
       
    45  * Here is a summary of the semantics of this class:
       
    46  * - Bind: Bind uses only the protocol and device fields of the 
       
    47  *       PacketSocketAddress. If none are provided, Bind uses 
       
    48  *       zero for both, which means that the socket is bound
       
    49  *       to all protocols on all devices on the node.
       
    50  *
       
    51  * - Connect: uses only the protocol, device and "physical address" 
       
    52  *       field of the PacketSocketAddress. It is used to set the default
       
    53  *       destination address for outgoing packets.
       
    54  *
       
    55  * - Send: send the input packet to the underlying NetDevices
       
    56  *       with the default destination address. The socket must
       
    57  *       be bound and connected.
       
    58  *
       
    59  * - SendTo: uses the protocol, device, and "physical address" 
       
    60  *       fields of the PacketSocketAddress. The device value is 
       
    61  *       used to specialize the packet transmission to a single 
       
    62  *       device, the protocol value specifies the protocol of this
       
    63  *       packet only and the "physical address" field is used to override the 
       
    64  *       default destination address. The socket must be bound.
       
    65  *
       
    66  * - Recv: The address represents the address of the packer originator.
       
    67  *       The fields "physical address", device, and protocol are filled.
       
    68  *
       
    69  * - Accept: not allowed
       
    70  */
       
    71 class PacketSocket : public Socket
       
    72 {
       
    73 public:
       
    74   PacketSocket (Ptr<Node> node);
       
    75   virtual ~PacketSocket ();
       
    76 
       
    77   virtual enum SocketErrno GetErrno (void) const;
       
    78   virtual Ptr<Node> GetNode (void) const;
       
    79   virtual int Bind (void);
       
    80   virtual int Bind (const Address & address);
       
    81   virtual int ShutdownSend (void);
       
    82   virtual int ShutdownRecv (void);
       
    83 
       
    84 private:
       
    85   virtual int DoClose(Callback<void, Ptr<Socket> > closeCompleted);
       
    86   virtual int DoConnect(const Address & address,
       
    87                         Callback<void, Ptr<Socket> > connectionSucceeded,
       
    88                         Callback<void, Ptr<Socket> > connectionFailed,
       
    89                         Callback<void, Ptr<Socket> > halfClose);
       
    90   virtual int DoAccept(Callback<bool, Ptr<Socket>, const Address&> connectionRequest,
       
    91                        Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
       
    92                        Callback<void, Ptr<Socket> > closeRequested);
       
    93   virtual int DoSend (const uint8_t* buffer,
       
    94                       uint32_t size,
       
    95                       Callback<void, Ptr<Socket>, uint32_t> dataSent);
       
    96   virtual int DoSendTo(const Address &address,
       
    97                        const uint8_t *buffer,
       
    98                        uint32_t size,
       
    99                        Callback<void, Ptr<Socket>, uint32_t> dataSent);
       
   100   virtual void DoRecv(Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> receive);
       
   101   virtual void DoRecvDummy(Callback<void, Ptr<Socket>, uint32_t,const Address&>);
       
   102 
       
   103 private:
       
   104   void Init (void);
       
   105   void ForwardUp (Ptr<NetDevice> device, const Packet &packet, 
       
   106                   uint16_t protocol, const Address &from);
       
   107   int DoBind (const PacketSocketAddress &address);
       
   108   virtual void DoDispose (void);
       
   109 
       
   110   enum State {
       
   111     STATE_OPEN,
       
   112     STATE_BOUND,     // open and bound
       
   113     STATE_CONNECTED, // open, bound and connected
       
   114     STATE_CLOSED
       
   115   };
       
   116   Ptr<Node> m_node;
       
   117   Callback<void,Ptr<Socket>,uint32_t,const Address &> m_dummyRxCallback;
       
   118   Callback<void,Ptr<Socket>,uint8_t const*,uint32_t, const Address &> m_rxCallback;
       
   119   enum SocketErrno m_errno;
       
   120   bool m_shutdownSend;
       
   121   bool m_shutdownRecv;
       
   122   enum State m_state;
       
   123   uint16_t m_protocol;
       
   124   uint32_t m_device;
       
   125   Address m_destAddr; /// Default destination address
       
   126 };
       
   127 
       
   128 }//namespace ns3
       
   129 
       
   130 #endif /* PACKET_SOCKET_H */
       
   131 
       
   132