1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_BASE_SOCKET_H__
12 #define WEBRTC_BASE_SOCKET_H__
13 
14 #include <errno.h>
15 
16 #if defined(WEBRTC_POSIX)
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21 #define SOCKET_EACCES EACCES
22 #endif
23 
24 #if defined(WEBRTC_WIN)
25 #include "webrtc/base/win32.h"
26 #endif
27 
28 #include "webrtc/base/basictypes.h"
29 #include "webrtc/base/constructormagic.h"
30 #include "webrtc/base/socketaddress.h"
31 
32 // Rather than converting errors into a private namespace,
33 // Reuse the POSIX socket api errors. Note this depends on
34 // Win32 compatibility.
35 
36 #if defined(WEBRTC_WIN)
37 #undef EWOULDBLOCK  // Remove errno.h's definition for each macro below.
38 #define EWOULDBLOCK WSAEWOULDBLOCK
39 #undef EINPROGRESS
40 #define EINPROGRESS WSAEINPROGRESS
41 #undef EALREADY
42 #define EALREADY WSAEALREADY
43 #undef ENOTSOCK
44 #define ENOTSOCK WSAENOTSOCK
45 #undef EDESTADDRREQ
46 #define EDESTADDRREQ WSAEDESTADDRREQ
47 #undef EMSGSIZE
48 #define EMSGSIZE WSAEMSGSIZE
49 #undef EPROTOTYPE
50 #define EPROTOTYPE WSAEPROTOTYPE
51 #undef ENOPROTOOPT
52 #define ENOPROTOOPT WSAENOPROTOOPT
53 #undef EPROTONOSUPPORT
54 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
55 #undef ESOCKTNOSUPPORT
56 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
57 #undef EOPNOTSUPP
58 #define EOPNOTSUPP WSAEOPNOTSUPP
59 #undef EPFNOSUPPORT
60 #define EPFNOSUPPORT WSAEPFNOSUPPORT
61 #undef EAFNOSUPPORT
62 #define EAFNOSUPPORT WSAEAFNOSUPPORT
63 #undef EADDRINUSE
64 #define EADDRINUSE WSAEADDRINUSE
65 #undef EADDRNOTAVAIL
66 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
67 #undef ENETDOWN
68 #define ENETDOWN WSAENETDOWN
69 #undef ENETUNREACH
70 #define ENETUNREACH WSAENETUNREACH
71 #undef ENETRESET
72 #define ENETRESET WSAENETRESET
73 #undef ECONNABORTED
74 #define ECONNABORTED WSAECONNABORTED
75 #undef ECONNRESET
76 #define ECONNRESET WSAECONNRESET
77 #undef ENOBUFS
78 #define ENOBUFS WSAENOBUFS
79 #undef EISCONN
80 #define EISCONN WSAEISCONN
81 #undef ENOTCONN
82 #define ENOTCONN WSAENOTCONN
83 #undef ESHUTDOWN
84 #define ESHUTDOWN WSAESHUTDOWN
85 #undef ETOOMANYREFS
86 #define ETOOMANYREFS WSAETOOMANYREFS
87 #undef ETIMEDOUT
88 #define ETIMEDOUT WSAETIMEDOUT
89 #undef ECONNREFUSED
90 #define ECONNREFUSED WSAECONNREFUSED
91 #undef ELOOP
92 #define ELOOP WSAELOOP
93 #undef ENAMETOOLONG
94 #define ENAMETOOLONG WSAENAMETOOLONG
95 #undef EHOSTDOWN
96 #define EHOSTDOWN WSAEHOSTDOWN
97 #undef EHOSTUNREACH
98 #define EHOSTUNREACH WSAEHOSTUNREACH
99 #undef ENOTEMPTY
100 #define ENOTEMPTY WSAENOTEMPTY
101 #undef EPROCLIM
102 #define EPROCLIM WSAEPROCLIM
103 #undef EUSERS
104 #define EUSERS WSAEUSERS
105 #undef EDQUOT
106 #define EDQUOT WSAEDQUOT
107 #undef ESTALE
108 #define ESTALE WSAESTALE
109 #undef EREMOTE
110 #define EREMOTE WSAEREMOTE
111 #undef EACCES
112 #define SOCKET_EACCES WSAEACCES
113 #endif  // WEBRTC_WIN
114 
115 #if defined(WEBRTC_POSIX)
116 #define INVALID_SOCKET (-1)
117 #define SOCKET_ERROR (-1)
118 #define closesocket(s) close(s)
119 #endif  // WEBRTC_POSIX
120 
121 namespace rtc {
122 
IsBlockingError(int e)123 inline bool IsBlockingError(int e) {
124   return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
125 }
126 
127 struct SentPacket {
SentPacketSentPacket128   SentPacket() : packet_id(-1), send_time_ms(-1) {}
SentPacketSentPacket129   SentPacket(int packet_id, int64_t send_time_ms)
130       : packet_id(packet_id), send_time_ms(send_time_ms) {}
131 
132   int packet_id;
133   int64_t send_time_ms;
134 };
135 
136 // General interface for the socket implementations of various networks.  The
137 // methods match those of normal UNIX sockets very closely.
138 class Socket {
139  public:
~Socket()140   virtual ~Socket() {}
141 
142   // Returns the address to which the socket is bound.  If the socket is not
143   // bound, then the any-address is returned.
144   virtual SocketAddress GetLocalAddress() const = 0;
145 
146   // Returns the address to which the socket is connected.  If the socket is
147   // not connected, then the any-address is returned.
148   virtual SocketAddress GetRemoteAddress() const = 0;
149 
150   virtual int Bind(const SocketAddress& addr) = 0;
151   virtual int Connect(const SocketAddress& addr) = 0;
152   virtual int Send(const void *pv, size_t cb) = 0;
153   virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
154   // |timestamp| is in units of microseconds.
155   virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
156   virtual int RecvFrom(void* pv,
157                        size_t cb,
158                        SocketAddress* paddr,
159                        int64_t* timestamp) = 0;
160   virtual int Listen(int backlog) = 0;
161   virtual Socket *Accept(SocketAddress *paddr) = 0;
162   virtual int Close() = 0;
163   virtual int GetError() const = 0;
164   virtual void SetError(int error) = 0;
IsBlocking()165   inline bool IsBlocking() const { return IsBlockingError(GetError()); }
166 
167   enum ConnState {
168     CS_CLOSED,
169     CS_CONNECTING,
170     CS_CONNECTED
171   };
172   virtual ConnState GetState() const = 0;
173 
174   // Fills in the given uint16_t with the current estimate of the MTU along the
175   // path to the address to which this socket is connected. NOTE: This method
176   // can block for up to 10 seconds on Windows.
177   virtual int EstimateMTU(uint16_t* mtu) = 0;
178 
179   enum Option {
180     OPT_DONTFRAGMENT,
181     OPT_RCVBUF,      // receive buffer size
182     OPT_SNDBUF,      // send buffer size
183     OPT_NODELAY,     // whether Nagle algorithm is enabled
184     OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
185     OPT_DSCP,        // DSCP code
186     OPT_RTP_SENDTIME_EXTN_ID,  // This is a non-traditional socket option param.
187                                // This is specific to libjingle and will be used
188                                // if SendTime option is needed at socket level.
189   };
190   virtual int GetOption(Option opt, int* value) = 0;
191   virtual int SetOption(Option opt, int value) = 0;
192 
193  protected:
Socket()194   Socket() {}
195 
196  private:
197   RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
198 };
199 
200 }  // namespace rtc
201 
202 #endif  // WEBRTC_BASE_SOCKET_H__
203