A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
socket.h
Go to the documentation of this 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 
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 {
90  };
91 
92  enum SocketType {
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 
812 
813 private:
823 
824  //IPv4 options
829 
830  uint8_t m_ipTos;
831  uint8_t m_ipTtl;
832 
833  //IPv6 options
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:
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:
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 */