henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_SOCKET_H_ |
| 12 | #define RTC_BASE_SOCKET_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <errno.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #if defined(WEBRTC_POSIX) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | #include <arpa/inet.h> |
| 18 | #include <netinet/in.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 19 | #include <sys/socket.h> |
| 20 | #include <sys/types.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | #define SOCKET_EACCES EACCES |
| 22 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/win32.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | #endif |
| 27 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/constructor_magic.h" |
| 29 | #include "rtc_base/socket_address.h" |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 30 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 31 | |
| 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 |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | #undef EMSGSIZE |
| 44 | #define EMSGSIZE WSAEMSGSIZE |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 45 | #undef EADDRINUSE |
| 46 | #define EADDRINUSE WSAEADDRINUSE |
| 47 | #undef EADDRNOTAVAIL |
| 48 | #define EADDRNOTAVAIL WSAEADDRNOTAVAIL |
| 49 | #undef ENETDOWN |
| 50 | #define ENETDOWN WSAENETDOWN |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 51 | #undef ECONNABORTED |
| 52 | #define ECONNABORTED WSAECONNABORTED |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 53 | #undef ENOBUFS |
| 54 | #define ENOBUFS WSAENOBUFS |
| 55 | #undef EISCONN |
| 56 | #define EISCONN WSAEISCONN |
| 57 | #undef ENOTCONN |
| 58 | #define ENOTCONN WSAENOTCONN |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 59 | #undef ECONNREFUSED |
| 60 | #define ECONNREFUSED WSAECONNREFUSED |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 61 | #undef EHOSTUNREACH |
| 62 | #define EHOSTUNREACH WSAEHOSTUNREACH |
Berthold Herrmann | 1184b55 | 2021-02-05 10:46:26 +0100 | [diff] [blame] | 63 | #undef ENETUNREACH |
| 64 | #define ENETUNREACH WSAENETUNREACH |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 65 | #define SOCKET_EACCES WSAEACCES |
| 66 | #endif // WEBRTC_WIN |
| 67 | |
| 68 | #if defined(WEBRTC_POSIX) |
| 69 | #define INVALID_SOCKET (-1) |
| 70 | #define SOCKET_ERROR (-1) |
| 71 | #define closesocket(s) close(s) |
| 72 | #endif // WEBRTC_POSIX |
| 73 | |
| 74 | namespace rtc { |
| 75 | |
| 76 | inline bool IsBlockingError(int e) { |
| 77 | return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS); |
| 78 | } |
| 79 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 80 | // General interface for the socket implementations of various networks. The |
| 81 | // methods match those of normal UNIX sockets very closely. |
| 82 | class Socket { |
| 83 | public: |
| 84 | virtual ~Socket() {} |
| 85 | |
| 86 | // Returns the address to which the socket is bound. If the socket is not |
| 87 | // bound, then the any-address is returned. |
| 88 | virtual SocketAddress GetLocalAddress() const = 0; |
| 89 | |
| 90 | // Returns the address to which the socket is connected. If the socket is |
| 91 | // not connected, then the any-address is returned. |
| 92 | virtual SocketAddress GetRemoteAddress() const = 0; |
| 93 | |
| 94 | virtual int Bind(const SocketAddress& addr) = 0; |
| 95 | virtual int Connect(const SocketAddress& addr) = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 96 | virtual int Send(const void* pv, size_t cb) = 0; |
| 97 | virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0; |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 98 | // `timestamp` is in units of microseconds. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 99 | virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0; |
| 100 | virtual int RecvFrom(void* pv, |
| 101 | size_t cb, |
| 102 | SocketAddress* paddr, |
| 103 | int64_t* timestamp) = 0; |
| 104 | virtual int Listen(int backlog) = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 105 | virtual Socket* Accept(SocketAddress* paddr) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 106 | virtual int Close() = 0; |
| 107 | virtual int GetError() const = 0; |
| 108 | virtual void SetError(int error) = 0; |
| 109 | inline bool IsBlocking() const { return IsBlockingError(GetError()); } |
| 110 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 111 | enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 112 | virtual ConnState GetState() const = 0; |
| 113 | |
| 114 | enum Option { |
| 115 | OPT_DONTFRAGMENT, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 116 | OPT_RCVBUF, // receive buffer size |
| 117 | OPT_SNDBUF, // send buffer size |
| 118 | OPT_NODELAY, // whether Nagle algorithm is enabled |
| 119 | OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only. |
| 120 | OPT_DSCP, // DSCP code |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 121 | OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param. |
| 122 | // This is specific to libjingle and will be used |
| 123 | // if SendTime option is needed at socket level. |
| 124 | }; |
| 125 | virtual int GetOption(Option opt, int* value) = 0; |
| 126 | virtual int SetOption(Option opt, int value) = 0; |
| 127 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 128 | // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow |
| 129 | // access concurrently from different thread. |
| 130 | // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor |
| 131 | // but at the same time the SocketDispatcher may be signaling the read event. |
| 132 | // ready to read |
| 133 | sigslot::signal1<Socket*, sigslot::multi_threaded_local> SignalReadEvent; |
| 134 | // ready to write |
| 135 | sigslot::signal1<Socket*, sigslot::multi_threaded_local> SignalWriteEvent; |
| 136 | sigslot::signal1<Socket*> SignalConnectEvent; // connected |
| 137 | sigslot::signal2<Socket*, int> SignalCloseEvent; // closed |
| 138 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 139 | protected: |
| 140 | Socket() {} |
| 141 | |
| 142 | private: |
| 143 | RTC_DISALLOW_COPY_AND_ASSIGN(Socket); |
| 144 | }; |
| 145 | |
| 146 | } // namespace rtc |
| 147 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 148 | #endif // RTC_BASE_SOCKET_H_ |