blob: 5950989e344b40b02f3f65f2d80539953ef8db2e [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)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#include <arpa/inet.h>
18#include <netinet/in.h>
Yves Gerey665174f2018-06-19 15:03:05 +020019#include <sys/socket.h>
20#include <sys/types.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021#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/constructormagic.h"
30#include "rtc_base/socketaddress.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031
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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111#define SOCKET_EACCES WSAEACCES
112#endif // WEBRTC_WIN
113
114#if defined(WEBRTC_POSIX)
115#define INVALID_SOCKET (-1)
116#define SOCKET_ERROR (-1)
117#define closesocket(s) close(s)
118#endif // WEBRTC_POSIX
119
120namespace rtc {
121
122inline bool IsBlockingError(int e) {
123 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
124}
125
Qingsi Wang6e641e62018-04-11 20:14:17 -0700126enum class PacketType {
127 kUnknown,
128 kData,
129 kIceConnectivityCheck,
130 kIceConnectivityCheckResponse,
131 kStunMessage,
132 kTurnMessage,
133};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200134
Qingsi Wang6e641e62018-04-11 20:14:17 -0700135enum class PacketInfoProtocolType {
136 kUnknown,
137 kUdp,
138 kTcp,
139 kSsltcp,
140 kTls,
141};
142
143struct PacketInfo {
144 PacketInfo();
145 PacketInfo(const PacketInfo& info);
146 ~PacketInfo();
147
148 PacketType packet_type = PacketType::kUnknown;
149 PacketInfoProtocolType protocol = PacketInfoProtocolType::kUnknown;
150 // A unique id assigned by the network manager, and rtc::nullopt if not set.
151 rtc::Optional<uint16_t> network_id;
152 size_t packet_size_bytes = 0;
153 size_t turn_overhead_bytes = 0;
154 SocketAddress local_socket_address;
155 SocketAddress remote_socket_address;
156};
157
158struct SentPacket {
159 SentPacket();
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -0700160 SentPacket(int64_t packet_id, int64_t send_time_ms);
161 SentPacket(int64_t packet_id,
162 int64_t send_time_ms,
163 const rtc::PacketInfo& info);
Qingsi Wang6e641e62018-04-11 20:14:17 -0700164
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -0700165 int64_t packet_id = -1;
Qingsi Wang6e641e62018-04-11 20:14:17 -0700166 int64_t send_time_ms = -1;
167 rtc::PacketInfo info;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200168};
169
170// General interface for the socket implementations of various networks. The
171// methods match those of normal UNIX sockets very closely.
172class Socket {
173 public:
174 virtual ~Socket() {}
175
176 // Returns the address to which the socket is bound. If the socket is not
177 // bound, then the any-address is returned.
178 virtual SocketAddress GetLocalAddress() const = 0;
179
180 // Returns the address to which the socket is connected. If the socket is
181 // not connected, then the any-address is returned.
182 virtual SocketAddress GetRemoteAddress() const = 0;
183
184 virtual int Bind(const SocketAddress& addr) = 0;
185 virtual int Connect(const SocketAddress& addr) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200186 virtual int Send(const void* pv, size_t cb) = 0;
187 virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200188 // |timestamp| is in units of microseconds.
189 virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
190 virtual int RecvFrom(void* pv,
191 size_t cb,
192 SocketAddress* paddr,
193 int64_t* timestamp) = 0;
194 virtual int Listen(int backlog) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200195 virtual Socket* Accept(SocketAddress* paddr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200196 virtual int Close() = 0;
197 virtual int GetError() const = 0;
198 virtual void SetError(int error) = 0;
199 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
200
Yves Gerey665174f2018-06-19 15:03:05 +0200201 enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED };
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200202 virtual ConnState GetState() const = 0;
203
204 enum Option {
205 OPT_DONTFRAGMENT,
Yves Gerey665174f2018-06-19 15:03:05 +0200206 OPT_RCVBUF, // receive buffer size
207 OPT_SNDBUF, // send buffer size
208 OPT_NODELAY, // whether Nagle algorithm is enabled
209 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
210 OPT_DSCP, // DSCP code
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200211 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
212 // This is specific to libjingle and will be used
213 // if SendTime option is needed at socket level.
214 };
215 virtual int GetOption(Option opt, int* value) = 0;
216 virtual int SetOption(Option opt, int value) = 0;
217
218 protected:
219 Socket() {}
220
221 private:
222 RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
223};
224
225} // namespace rtc
226
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200227#endif // RTC_BASE_SOCKET_H_