blob: ee2c73f08e9601f85024e439208f9a89be6b7192 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_SOCKET_H_
12#define RTC_BASE_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <errno.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/win32.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026#endif
27
Qingsi Wang6e641e62018-04-11 20:14:17 -070028#include "api/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/basictypes.h"
30#include "rtc_base/constructormagic.h"
31#include "rtc_base/socketaddress.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032
33// Rather than converting errors into a private namespace,
34// Reuse the POSIX socket api errors. Note this depends on
35// Win32 compatibility.
36
37#if defined(WEBRTC_WIN)
38#undef EWOULDBLOCK // Remove errno.h's definition for each macro below.
39#define EWOULDBLOCK WSAEWOULDBLOCK
40#undef EINPROGRESS
41#define EINPROGRESS WSAEINPROGRESS
42#undef EALREADY
43#define EALREADY WSAEALREADY
44#undef ENOTSOCK
45#define ENOTSOCK WSAENOTSOCK
46#undef EDESTADDRREQ
47#define EDESTADDRREQ WSAEDESTADDRREQ
48#undef EMSGSIZE
49#define EMSGSIZE WSAEMSGSIZE
50#undef EPROTOTYPE
51#define EPROTOTYPE WSAEPROTOTYPE
52#undef ENOPROTOOPT
53#define ENOPROTOOPT WSAENOPROTOOPT
54#undef EPROTONOSUPPORT
55#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
56#undef ESOCKTNOSUPPORT
57#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
58#undef EOPNOTSUPP
59#define EOPNOTSUPP WSAEOPNOTSUPP
60#undef EPFNOSUPPORT
61#define EPFNOSUPPORT WSAEPFNOSUPPORT
62#undef EAFNOSUPPORT
63#define EAFNOSUPPORT WSAEAFNOSUPPORT
64#undef EADDRINUSE
65#define EADDRINUSE WSAEADDRINUSE
66#undef EADDRNOTAVAIL
67#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
68#undef ENETDOWN
69#define ENETDOWN WSAENETDOWN
70#undef ENETUNREACH
71#define ENETUNREACH WSAENETUNREACH
72#undef ENETRESET
73#define ENETRESET WSAENETRESET
74#undef ECONNABORTED
75#define ECONNABORTED WSAECONNABORTED
76#undef ECONNRESET
77#define ECONNRESET WSAECONNRESET
78#undef ENOBUFS
79#define ENOBUFS WSAENOBUFS
80#undef EISCONN
81#define EISCONN WSAEISCONN
82#undef ENOTCONN
83#define ENOTCONN WSAENOTCONN
84#undef ESHUTDOWN
85#define ESHUTDOWN WSAESHUTDOWN
86#undef ETOOMANYREFS
87#define ETOOMANYREFS WSAETOOMANYREFS
88#undef ETIMEDOUT
89#define ETIMEDOUT WSAETIMEDOUT
90#undef ECONNREFUSED
91#define ECONNREFUSED WSAECONNREFUSED
92#undef ELOOP
93#define ELOOP WSAELOOP
94#undef ENAMETOOLONG
95#define ENAMETOOLONG WSAENAMETOOLONG
96#undef EHOSTDOWN
97#define EHOSTDOWN WSAEHOSTDOWN
98#undef EHOSTUNREACH
99#define EHOSTUNREACH WSAEHOSTUNREACH
100#undef ENOTEMPTY
101#define ENOTEMPTY WSAENOTEMPTY
102#undef EPROCLIM
103#define EPROCLIM WSAEPROCLIM
104#undef EUSERS
105#define EUSERS WSAEUSERS
106#undef EDQUOT
107#define EDQUOT WSAEDQUOT
108#undef ESTALE
109#define ESTALE WSAESTALE
110#undef EREMOTE
111#define EREMOTE WSAEREMOTE
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112#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
Qingsi Wang6e641e62018-04-11 20:14:17 -0700127enum class PacketType {
128 kUnknown,
129 kData,
130 kIceConnectivityCheck,
131 kIceConnectivityCheckResponse,
132 kStunMessage,
133 kTurnMessage,
134};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135
Qingsi Wang6e641e62018-04-11 20:14:17 -0700136enum class PacketInfoProtocolType {
137 kUnknown,
138 kUdp,
139 kTcp,
140 kSsltcp,
141 kTls,
142};
143
144struct PacketInfo {
145 PacketInfo();
146 PacketInfo(const PacketInfo& info);
147 ~PacketInfo();
148
149 PacketType packet_type = PacketType::kUnknown;
150 PacketInfoProtocolType protocol = PacketInfoProtocolType::kUnknown;
151 // A unique id assigned by the network manager, and rtc::nullopt if not set.
152 rtc::Optional<uint16_t> network_id;
153 size_t packet_size_bytes = 0;
154 size_t turn_overhead_bytes = 0;
155 SocketAddress local_socket_address;
156 SocketAddress remote_socket_address;
157};
158
159struct SentPacket {
160 SentPacket();
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -0700161 SentPacket(int64_t packet_id, int64_t send_time_ms);
162 SentPacket(int64_t packet_id,
163 int64_t send_time_ms,
164 const rtc::PacketInfo& info);
Qingsi Wang6e641e62018-04-11 20:14:17 -0700165
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -0700166 int64_t packet_id = -1;
Qingsi Wang6e641e62018-04-11 20:14:17 -0700167 int64_t send_time_ms = -1;
168 rtc::PacketInfo info;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200169};
170
171// General interface for the socket implementations of various networks. The
172// methods match those of normal UNIX sockets very closely.
173class Socket {
174 public:
175 virtual ~Socket() {}
176
177 // Returns the address to which the socket is bound. If the socket is not
178 // bound, then the any-address is returned.
179 virtual SocketAddress GetLocalAddress() const = 0;
180
181 // Returns the address to which the socket is connected. If the socket is
182 // not connected, then the any-address is returned.
183 virtual SocketAddress GetRemoteAddress() const = 0;
184
185 virtual int Bind(const SocketAddress& addr) = 0;
186 virtual int Connect(const SocketAddress& addr) = 0;
187 virtual int Send(const void *pv, size_t cb) = 0;
188 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
189 // |timestamp| is in units of microseconds.
190 virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
191 virtual int RecvFrom(void* pv,
192 size_t cb,
193 SocketAddress* paddr,
194 int64_t* timestamp) = 0;
195 virtual int Listen(int backlog) = 0;
196 virtual Socket *Accept(SocketAddress *paddr) = 0;
197 virtual int Close() = 0;
198 virtual int GetError() const = 0;
199 virtual void SetError(int error) = 0;
200 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
201
202 enum ConnState {
203 CS_CLOSED,
204 CS_CONNECTING,
205 CS_CONNECTED
206 };
207 virtual ConnState GetState() const = 0;
208
209 enum Option {
210 OPT_DONTFRAGMENT,
211 OPT_RCVBUF, // receive buffer size
212 OPT_SNDBUF, // send buffer size
213 OPT_NODELAY, // whether Nagle algorithm is enabled
214 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
215 OPT_DSCP, // DSCP code
216 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
217 // This is specific to libjingle and will be used
218 // if SendTime option is needed at socket level.
219 };
220 virtual int GetOption(Option opt, int* value) = 0;
221 virtual int SetOption(Option opt, int value) = 0;
222
223 protected:
224 Socket() {}
225
226 private:
227 RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
228};
229
230} // namespace rtc
231
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200232#endif // RTC_BASE_SOCKET_H_