blob: e38f8afe3ca70313db7d14dc387d2177cc91d6d5 [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
Niels Möllerd0b88792021-08-12 10:32:30 +020028#include "absl/base/attributes.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/constructor_magic.h"
30#include "rtc_base/socket_address.h"
Niels Möllerd0b88792021-08-12 10:32:30 +020031#include "rtc_base/third_party/sigslot/sigslot.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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044#undef EMSGSIZE
45#define EMSGSIZE WSAEMSGSIZE
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046#undef EADDRINUSE
47#define EADDRINUSE WSAEADDRINUSE
48#undef EADDRNOTAVAIL
49#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
50#undef ENETDOWN
51#define ENETDOWN WSAENETDOWN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052#undef ECONNABORTED
53#define ECONNABORTED WSAECONNABORTED
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054#undef ENOBUFS
55#define ENOBUFS WSAENOBUFS
56#undef EISCONN
57#define EISCONN WSAEISCONN
58#undef ENOTCONN
59#define ENOTCONN WSAENOTCONN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060#undef ECONNREFUSED
61#define ECONNREFUSED WSAECONNREFUSED
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062#undef EHOSTUNREACH
63#define EHOSTUNREACH WSAEHOSTUNREACH
Berthold Herrmann1184b552021-02-05 10:46:26 +010064#undef ENETUNREACH
65#define ENETUNREACH WSAENETUNREACH
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066#define SOCKET_EACCES WSAEACCES
67#endif // WEBRTC_WIN
68
69#if defined(WEBRTC_POSIX)
70#define INVALID_SOCKET (-1)
71#define SOCKET_ERROR (-1)
72#define closesocket(s) close(s)
73#endif // WEBRTC_POSIX
74
75namespace rtc {
76
77inline bool IsBlockingError(int e) {
78 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
79}
80
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081// General interface for the socket implementations of various networks. The
82// methods match those of normal UNIX sockets very closely.
83class Socket {
84 public:
85 virtual ~Socket() {}
86
87 // Returns the address to which the socket is bound. If the socket is not
88 // bound, then the any-address is returned.
89 virtual SocketAddress GetLocalAddress() const = 0;
90
91 // Returns the address to which the socket is connected. If the socket is
92 // not connected, then the any-address is returned.
93 virtual SocketAddress GetRemoteAddress() const = 0;
94
95 virtual int Bind(const SocketAddress& addr) = 0;
96 virtual int Connect(const SocketAddress& addr) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +020097 virtual int Send(const void* pv, size_t cb) = 0;
98 virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0;
Artem Titov96e3b992021-07-26 16:03:14 +020099 // `timestamp` is in units of microseconds.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
101 virtual int RecvFrom(void* pv,
102 size_t cb,
103 SocketAddress* paddr,
104 int64_t* timestamp) = 0;
105 virtual int Listen(int backlog) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200106 virtual Socket* Accept(SocketAddress* paddr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 virtual int Close() = 0;
108 virtual int GetError() const = 0;
109 virtual void SetError(int error) = 0;
110 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
111
Yves Gerey665174f2018-06-19 15:03:05 +0200112 enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED };
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113 virtual ConnState GetState() const = 0;
114
115 enum Option {
116 OPT_DONTFRAGMENT,
Yves Gerey665174f2018-06-19 15:03:05 +0200117 OPT_RCVBUF, // receive buffer size
118 OPT_SNDBUF, // send buffer size
119 OPT_NODELAY, // whether Nagle algorithm is enabled
120 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
121 OPT_DSCP, // DSCP code
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
123 // This is specific to libjingle and will be used
124 // if SendTime option is needed at socket level.
125 };
126 virtual int GetOption(Option opt, int* value) = 0;
127 virtual int SetOption(Option opt, int value) = 0;
128
Niels Möllerd0b88792021-08-12 10:32:30 +0200129 // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow
130 // access concurrently from different thread.
131 // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor
132 // but at the same time the SocketDispatcher may be signaling the read event.
133 // ready to read
134 sigslot::signal1<Socket*, sigslot::multi_threaded_local> SignalReadEvent;
135 // ready to write
136 sigslot::signal1<Socket*, sigslot::multi_threaded_local> SignalWriteEvent;
137 sigslot::signal1<Socket*> SignalConnectEvent; // connected
138 sigslot::signal2<Socket*, int> SignalCloseEvent; // closed
139
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140 protected:
141 Socket() {}
142
143 private:
144 RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
145};
146
Niels Möllerd0b88792021-08-12 10:32:30 +0200147// TODO(bugs.webrtc.org/13065): Old alias, delete ASAP, when downstream code is
148// updated.
149using AsyncSocket ABSL_DEPRECATED("bugs.webrtc.org/13065") = Socket;
150
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151} // namespace rtc
152
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200153#endif // RTC_BASE_SOCKET_H_