blob: 0ed3a7fa6a52c315732a3c33e410706db45739b2 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#ifndef RTC_BASE_SOCKET_H_
12#define RTC_BASE_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <errno.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2615
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#if defined(WEBRTC_POSIX)
Henrik Kjellanderec78f1c2017-06-29 05:52:5017#include <arpa/inet.h>
18#include <netinet/in.h>
Yves Gerey665174f2018-06-19 13:03:0519#include <sys/socket.h>
20#include <sys/types.h>
Henrik Kjellanderec78f1c2017-06-29 05:52:5021#define SOCKET_EACCES EACCES
22#endif
henrike@webrtc.orgf0488722014-05-13 18:00:2623
Henrik Kjellanderec78f1c2017-06-29 05:52:5024#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "rtc_base/win32.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5026#endif
27
Steve Anton10542f22019-01-11 17:11:0028#include "rtc_base/socket_address.h"
Niels Möllerd0b88792021-08-12 08:32:3029#include "rtc_base/third_party/sigslot/sigslot.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5030
31// Rather than converting errors into a private namespace,
32// Reuse the POSIX socket api errors. Note this depends on
33// Win32 compatibility.
34
35#if defined(WEBRTC_WIN)
36#undef EWOULDBLOCK // Remove errno.h's definition for each macro below.
37#define EWOULDBLOCK WSAEWOULDBLOCK
38#undef EINPROGRESS
39#define EINPROGRESS WSAEINPROGRESS
40#undef EALREADY
41#define EALREADY WSAEALREADY
Henrik Kjellanderec78f1c2017-06-29 05:52:5042#undef EMSGSIZE
43#define EMSGSIZE WSAEMSGSIZE
Henrik Kjellanderec78f1c2017-06-29 05:52:5044#undef EADDRINUSE
45#define EADDRINUSE WSAEADDRINUSE
46#undef EADDRNOTAVAIL
47#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
48#undef ENETDOWN
49#define ENETDOWN WSAENETDOWN
Henrik Kjellanderec78f1c2017-06-29 05:52:5050#undef ECONNABORTED
51#define ECONNABORTED WSAECONNABORTED
Henrik Kjellanderec78f1c2017-06-29 05:52:5052#undef ENOBUFS
53#define ENOBUFS WSAENOBUFS
54#undef EISCONN
55#define EISCONN WSAEISCONN
56#undef ENOTCONN
57#define ENOTCONN WSAENOTCONN
Henrik Kjellanderec78f1c2017-06-29 05:52:5058#undef ECONNREFUSED
59#define ECONNREFUSED WSAECONNREFUSED
Henrik Kjellanderec78f1c2017-06-29 05:52:5060#undef EHOSTUNREACH
61#define EHOSTUNREACH WSAEHOSTUNREACH
Berthold Herrmann1184b552021-02-05 09:46:2662#undef ENETUNREACH
63#define ENETUNREACH WSAENETUNREACH
Henrik Kjellanderec78f1c2017-06-29 05:52:5064#define SOCKET_EACCES WSAEACCES
65#endif // WEBRTC_WIN
66
67#if defined(WEBRTC_POSIX)
68#define INVALID_SOCKET (-1)
69#define SOCKET_ERROR (-1)
70#define closesocket(s) close(s)
71#endif // WEBRTC_POSIX
72
73namespace rtc {
74
75inline bool IsBlockingError(int e) {
76 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
77}
78
Henrik Kjellanderec78f1c2017-06-29 05:52:5079// General interface for the socket implementations of various networks. The
80// methods match those of normal UNIX sockets very closely.
81class Socket {
82 public:
83 virtual ~Socket() {}
84
Byoungchan Lee14af7622022-01-11 20:24:5885 Socket(const Socket&) = delete;
86 Socket& operator=(const Socket&) = delete;
87
Henrik Kjellanderec78f1c2017-06-29 05:52:5088 // Returns the address to which the socket is bound. If the socket is not
89 // bound, then the any-address is returned.
90 virtual SocketAddress GetLocalAddress() const = 0;
91
92 // Returns the address to which the socket is connected. If the socket is
93 // not connected, then the any-address is returned.
94 virtual SocketAddress GetRemoteAddress() const = 0;
95
96 virtual int Bind(const SocketAddress& addr) = 0;
97 virtual int Connect(const SocketAddress& addr) = 0;
Yves Gerey665174f2018-06-19 13:03:0598 virtual int Send(const void* pv, size_t cb) = 0;
99 virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0;
Artem Titov96e3b992021-07-26 14:03:14100 // `timestamp` is in units of microseconds.
Henrik Kjellanderec78f1c2017-06-29 05:52:50101 virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
102 virtual int RecvFrom(void* pv,
103 size_t cb,
104 SocketAddress* paddr,
105 int64_t* timestamp) = 0;
106 virtual int Listen(int backlog) = 0;
Yves Gerey665174f2018-06-19 13:03:05107 virtual Socket* Accept(SocketAddress* paddr) = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50108 virtual int Close() = 0;
109 virtual int GetError() const = 0;
110 virtual void SetError(int error) = 0;
111 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
112
Yves Gerey665174f2018-06-19 13:03:05113 enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED };
Henrik Kjellanderec78f1c2017-06-29 05:52:50114 virtual ConnState GetState() const = 0;
115
116 enum Option {
117 OPT_DONTFRAGMENT,
Yves Gerey665174f2018-06-19 13:03:05118 OPT_RCVBUF, // receive buffer size
119 OPT_SNDBUF, // send buffer size
120 OPT_NODELAY, // whether Nagle algorithm is enabled
121 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
122 OPT_DSCP, // DSCP code
Henrik Kjellanderec78f1c2017-06-29 05:52:50123 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
124 // This is specific to libjingle and will be used
125 // if SendTime option is needed at socket level.
126 };
127 virtual int GetOption(Option opt, int* value) = 0;
128 virtual int SetOption(Option opt, int value) = 0;
129
Niels Möllerd0b88792021-08-12 08:32:30130 // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow
131 // access concurrently from different thread.
132 // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor
133 // but at the same time the SocketDispatcher may be signaling the read event.
134 // ready to read
135 sigslot::signal1<Socket*, sigslot::multi_threaded_local> SignalReadEvent;
136 // ready to write
137 sigslot::signal1<Socket*, sigslot::multi_threaded_local> SignalWriteEvent;
138 sigslot::signal1<Socket*> SignalConnectEvent; // connected
139 sigslot::signal2<Socket*, int> SignalCloseEvent; // closed
140
Henrik Kjellanderec78f1c2017-06-29 05:52:50141 protected:
142 Socket() {}
Henrik Kjellanderec78f1c2017-06-29 05:52:50143};
144
145} // namespace rtc
146
Mirko Bonadei92ea95e2017-09-15 04:47:31147#endif // RTC_BASE_SOCKET_H_