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
121namespace rtc {
122
123inline bool IsBlockingError(int e) {
124  return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
125}
126
127struct SentPacket {
128  SentPacket() : packet_id(-1), send_time_ms(-1) {}
129  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.
138class Socket {
139 public:
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  virtual int Recv(void *pv, size_t cb) = 0;
155  virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr) = 0;
156  virtual int Listen(int backlog) = 0;
157  virtual Socket *Accept(SocketAddress *paddr) = 0;
158  virtual int Close() = 0;
159  virtual int GetError() const = 0;
160  virtual void SetError(int error) = 0;
161  inline bool IsBlocking() const { return IsBlockingError(GetError()); }
162
163  enum ConnState {
164    CS_CLOSED,
165    CS_CONNECTING,
166    CS_CONNECTED
167  };
168  virtual ConnState GetState() const = 0;
169
170  // Fills in the given uint16_t with the current estimate of the MTU along the
171  // path to the address to which this socket is connected. NOTE: This method
172  // can block for up to 10 seconds on Windows.
173  virtual int EstimateMTU(uint16_t* mtu) = 0;
174
175  enum Option {
176    OPT_DONTFRAGMENT,
177    OPT_RCVBUF,      // receive buffer size
178    OPT_SNDBUF,      // send buffer size
179    OPT_NODELAY,     // whether Nagle algorithm is enabled
180    OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
181    OPT_DSCP,        // DSCP code
182    OPT_RTP_SENDTIME_EXTN_ID,  // This is a non-traditional socket option param.
183                               // This is specific to libjingle and will be used
184                               // if SendTime option is needed at socket level.
185  };
186  virtual int GetOption(Option opt, int* value) = 0;
187  virtual int SetOption(Option opt, int value) = 0;
188
189 protected:
190  Socket() {}
191
192 private:
193  RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
194};
195
196}  // namespace rtc
197
198#endif  // WEBRTC_BASE_SOCKET_H__
199