blob: d735d3f107962950c4bd8c3b73672f9886a1c5d9 [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();
161 SentPacket(int packet_id, int64_t send_time_ms);
162 SentPacket(int packet_id, int64_t send_time_ms, const rtc::PacketInfo& info);
163
164 int packet_id = -1;
165 int64_t send_time_ms = -1;
166 rtc::PacketInfo info;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200167};
168
169// General interface for the socket implementations of various networks. The
170// methods match those of normal UNIX sockets very closely.
171class Socket {
172 public:
173 virtual ~Socket() {}
174
175 // Returns the address to which the socket is bound. If the socket is not
176 // bound, then the any-address is returned.
177 virtual SocketAddress GetLocalAddress() const = 0;
178
179 // Returns the address to which the socket is connected. If the socket is
180 // not connected, then the any-address is returned.
181 virtual SocketAddress GetRemoteAddress() const = 0;
182
183 virtual int Bind(const SocketAddress& addr) = 0;
184 virtual int Connect(const SocketAddress& addr) = 0;
185 virtual int Send(const void *pv, size_t cb) = 0;
186 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
187 // |timestamp| is in units of microseconds.
188 virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
189 virtual int RecvFrom(void* pv,
190 size_t cb,
191 SocketAddress* paddr,
192 int64_t* timestamp) = 0;
193 virtual int Listen(int backlog) = 0;
194 virtual Socket *Accept(SocketAddress *paddr) = 0;
195 virtual int Close() = 0;
196 virtual int GetError() const = 0;
197 virtual void SetError(int error) = 0;
198 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
199
200 enum ConnState {
201 CS_CLOSED,
202 CS_CONNECTING,
203 CS_CONNECTED
204 };
205 virtual ConnState GetState() const = 0;
206
207 enum Option {
208 OPT_DONTFRAGMENT,
209 OPT_RCVBUF, // receive buffer size
210 OPT_SNDBUF, // send buffer size
211 OPT_NODELAY, // whether Nagle algorithm is enabled
212 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
213 OPT_DSCP, // DSCP code
214 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
215 // This is specific to libjingle and will be used
216 // if SendTime option is needed at socket level.
217 };
218 virtual int GetOption(Option opt, int* value) = 0;
219 virtual int SetOption(Option opt, int value) = 0;
220
221 protected:
222 Socket() {}
223
224 private:
225 RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
226};
227
228} // namespace rtc
229
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200230#endif // RTC_BASE_SOCKET_H_