A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
socket.h
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 
36 namespace ns3 {
37 
38 
39 class Node;
40 class Packet;
41 
66 class Socket : public Object
67 {
68 public:
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 
108  static Ptr<Socket> CreateSocket (Ptr<Node> node, TypeId tid);
114  virtual enum Socket::SocketErrno GetErrno (void) const = 0;
118  virtual enum Socket::SocketType GetSocketType (void) const = 0;
122  virtual Ptr<Node> GetNode (void) const = 0;
135  void SetConnectCallback (Callback<void, Ptr<Socket> > connectionSucceeded,
136  Callback<void, Ptr<Socket> > connectionFailed);
152  void SetCloseCallbacks (Callback<void, Ptr<Socket> > normalClose,
153  Callback<void, Ptr<Socket> > errorClose);
172  const Address &> connectionRequest,
173  Callback<void, Ptr<Socket>,
174  const Address&> newConnectionCreated);
183  uint32_t> dataSent);
198  void SetSendCallback (Callback<void, Ptr<Socket>, uint32_t> sendCb);
206  void SetRecvCallback (Callback<void, Ptr<Socket> >);
212  virtual int Bind (const Address &address) = 0;
213 
219  virtual int Bind () = 0;
220 
226  virtual int Bind6 () = 0;
227 
235  virtual int Close (void) = 0;
236 
243  virtual int ShutdownSend (void) = 0;
244 
251  virtual int ShutdownRecv (void) = 0;
252 
257  virtual int Connect (const Address &address) = 0;
258 
263  virtual int Listen (void) = 0;
264 
275  virtual uint32_t GetTxAvailable (void) const = 0;
276 
322  virtual int Send (Ptr<Packet> p, uint32_t flags) = 0;
323 
337  virtual int SendTo (Ptr<Packet> p, uint32_t flags,
338  const Address &toAddress) = 0;
339 
345  virtual uint32_t GetRxAvailable (void) const = 0;
346 
398  virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags) = 0;
399 
419  virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags,
420  Address &fromAddress) = 0;
421 
423  // The remainder of these public methods are overloaded methods //
424  // or variants of Send() and Recv(), and they are non-virtual //
426 
436  int Send (Ptr<Packet> p);
437 
450  int Send (const uint8_t* buf, uint32_t size, uint32_t flags);
451 
452 
469  int SendTo (const uint8_t* buf, uint32_t size, uint32_t flags,
470  const Address &address);
471 
481  Ptr<Packet> Recv (void);
482 
497  int Recv (uint8_t* buf, uint32_t size, uint32_t flags);
498 
512  Ptr<Packet> RecvFrom (Address &fromAddress);
513 
531  int RecvFrom (uint8_t* buf, uint32_t size, uint32_t flags,
532  Address &fromAddress);
537  virtual int GetSockName (Address &address) const = 0;
538 
558  virtual void BindToNetDevice (Ptr<NetDevice> netdevice);
559 
570 
571 
583  virtual bool SetAllowBroadcast (bool allowBroadcast) = 0;
584 
593  virtual bool GetAllowBroadcast () const = 0;
594 
607  void SetRecvPktInfo (bool flag);
608 
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 
650  void SetIpRecvTos (bool ipv4RecvTos);
651 
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 
695  void SetIpv6RecvTclass (bool ipv6RecvTclass);
696 
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 
738  void SetIpRecvTtl (bool ipv4RecvTtl);
739 
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 
781  void SetIpv6RecvHopLimit (bool ipv6RecvHopLimit);
782 
791  bool IsIpv6RecvHopLimit (void) const;
792 
793 protected:
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 
813 private:
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 
847 class SocketAddressTag : public Tag
848 {
849 public:
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 
861 private:
862  Address m_address;
863 };
864 
869 class SocketIpTtlTag : public Tag
870 {
871 public:
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 
883 private:
884  uint8_t m_ttl;
885 };
886 
892 {
893 public:
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 
905 private:
906  uint8_t m_hopLimit;
907 };
908 
914 {
915 public:
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;
927 private:
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  */
935 class SocketIpTosTag : public Tag
936 {
937 public:
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;
948 private:
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  */
956 class SocketIpv6TclassTag : public Tag
957 {
958 public:
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;
969 private:
970  uint8_t m_ipv6Tclass;
971 };
972 
973 } // namespace ns3
974 
975 #endif /* NS3_SOCKET_H */
virtual void Serialize(TagBuffer i) const
Definition: socket.cc:850
virtual void Print(std::ostream &os) const
Definition: socket.cc:861
virtual void Print(std::ostream &os) const
Definition: socket.cc:631
bool IsIpv6RecvHopLimit(void) const
Ask if the socket is currently passing information about IPv6 Hop Limit up the stack.
Definition: socket.cc:507
virtual void Deserialize(TagBuffer i)
Definition: socket.cc:564
Callback template class.
Definition: callback.h:369
void SetIpv6RecvTclass(bool ipv6RecvTclass)
Tells a socket to pass information about IPv6 Traffic Class up the stack.
Definition: socket.cc:451
This class implements a tag that carries the socket-specific HOPLIMIT of a packet to the IPv6 layer...
Definition: socket.h:891
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
virtual uint32_t GetSerializedSize(void) const
Definition: socket.cc:730
virtual int ShutdownSend(void)=0
Ptr< Packet > Recv(void)
Read a single packet from the socket.
Definition: socket.cc:174
virtual void Serialize(TagBuffer i) const
Definition: socket.cc:558
virtual void Deserialize(TagBuffer i)
Definition: socket.cc:856
virtual int GetSockName(Address &address) const =0
virtual int ShutdownRecv(void)=0
void SetIpRecvTos(bool ipv4RecvTos)
Tells a socket to pass information about IP Type of Service up the stack.
Definition: socket.cc:409
virtual TypeId GetInstanceTypeId(void) const
Definition: socket.cc:547
virtual TypeId GetInstanceTypeId(void) const
Definition: socket.cc:782
void SetCloseCallbacks(Callback< void, Ptr< Socket > > normalClose, Callback< void, Ptr< Socket > > errorClose)
Detect socket recv() events such as graceful shutdown or error.
Definition: socket.cc:93
virtual void Serialize(TagBuffer i) const
Definition: socket.cc:736
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer...
Definition: socket.h:869
virtual void Print(std::ostream &os) const
Definition: socket.cc:570
bool IsRecvPktInfo() const
Get status indicating whether enable/disable packet information to socket.
Definition: socket.cc:363
virtual enum Socket::SocketErrno GetErrno(void) const =0
a polymophic address class
Definition: address.h:86
virtual int Listen(void)=0
Listen for incoming connections.
bool IsIpRecvTos(void) const
Ask if the socket is currently passing information about IP Type of Service up the stack...
Definition: socket.cc:415
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:357
Ptr< NetDevice > GetBoundNetDevice()
Returns socket's bound netdevice, if any.
Definition: socket.cc:350
A low-level Socket API based loosely on the BSD Socket API.A few things to keep in mind about this ty...
Definition: socket.h:66
virtual int Bind()=0
Allocate a local IPv4 endpoint for this socket.
This class implements a tag that carries an address of a packet across the socket interface...
Definition: socket.h:847
virtual bool GetAllowBroadcast() const =0
Query whether broadcast datagram transmissions are allowed.
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
Definition: socket.cc:70
virtual void Print(std::ostream &os) const
Definition: socket.cc:748
virtual enum Socket::SocketType GetSocketType(void) const =0
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual uint32_t GetSerializedSize(void) const
Definition: socket.cc:613
tag a set of bytes in a packet
Definition: tag.h:36
virtual void Serialize(TagBuffer i) const
Definition: socket.cc:676
virtual uint32_t GetSerializedSize(void) const
Definition: socket.cc:671
virtual TypeId GetInstanceTypeId(void) const
Definition: socket.cc:838
virtual void Deserialize(TagBuffer i)
Definition: socket.cc:742
bool IsIpv6RecvTclass(void) const
Ask if the socket is currently passing information about IPv6 Traffic Class up the stack...
Definition: socket.cc:457
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
Definition: socket.cc:120
void SetAcceptCallback(Callback< bool, Ptr< Socket >, const Address & > connectionRequest, Callback< void, Ptr< Socket >, const Address & > newConnectionCreated)
Accept connection requests from remote hosts.
Definition: socket.cc:103
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
Definition: socket.cc:329
void SetDataSentCallback(Callback< void, Ptr< Socket >, uint32_t > dataSent)
Notify application when a packet has been sent from transport protocol (non-standard socket call) ...
Definition: socket.cc:113
virtual void DoDispose(void)
Definition: socket.cc:314
bool IsIpRecvTtl(void) const
Ask if the socket is currently passing information about IP_TTL up the stack.
Definition: socket.cc:482
read and write tag data
Definition: tag-buffer.h:51
void SetIpv6RecvHopLimit(bool ipv6RecvHopLimit)
Tells a socket to pass information about IPv6 Hop Limit up the stack.
Definition: socket.cc:501
virtual uint32_t GetSerializedSize(void) const
Definition: socket.cc:844
virtual TypeId GetInstanceTypeId(void) const
Definition: socket.cc:607
virtual void Serialize(TagBuffer i) const
Definition: socket.cc:794
virtual Ptr< Node > GetNode(void) const =0
virtual void Print(std::ostream &os) const
Definition: socket.cc:805
virtual void Deserialize(TagBuffer i)
Definition: socket.cc:681
indicated whether packets should be sent out with the DF flag set.
Definition: socket.h:913
virtual void Serialize(TagBuffer i) const
Definition: socket.cc:619
void SetIpRecvTtl(bool ipv4RecvTtl)
Tells a socket to pass information about IP_TTL up the stack.
Definition: socket.cc:476
virtual void Deserialize(TagBuffer i)
Definition: socket.cc:625
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
virtual void Deserialize(TagBuffer i)
Definition: socket.cc:800
virtual void Print(std::ostream &os) const
Definition: socket.cc:686
void SetConnectCallback(Callback< void, Ptr< Socket > > connectionSucceeded, Callback< void, Ptr< Socket > > connectionFailed)
Specify callbacks to allow the caller to determine if the connection succeeds of fails.
Definition: socket.cc:83
a base class which provides memory management and object aggregation
Definition: object.h:63
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
virtual uint32_t GetSerializedSize(void) const
Definition: socket.cc:552
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual TypeId GetInstanceTypeId(void) const
Definition: socket.cc:725
virtual int Close(void)=0
Close a socket.
virtual uint32_t GetTxAvailable(void) const =0
Returns the number of bytes which can be sent in a single call to Send.
virtual uint32_t GetSerializedSize(void) const
Definition: socket.cc:788
virtual TypeId GetInstanceTypeId(void) const
Definition: socket.cc:665
a unique identifier for an interface.
Definition: type-id.h:44
virtual uint32_t GetRxAvailable(void) const =0