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 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 28 | #include "api/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/constructormagic.h" |
| 30 | #include "rtc_base/socketaddress.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 |
| 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 111 | #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 | |
| 120 | namespace rtc { |
| 121 | |
| 122 | inline bool IsBlockingError(int e) { |
| 123 | return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS); |
| 124 | } |
| 125 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 126 | enum class PacketType { |
| 127 | kUnknown, |
| 128 | kData, |
| 129 | kIceConnectivityCheck, |
| 130 | kIceConnectivityCheckResponse, |
| 131 | kStunMessage, |
| 132 | kTurnMessage, |
| 133 | }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 134 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 135 | enum class PacketInfoProtocolType { |
| 136 | kUnknown, |
| 137 | kUdp, |
| 138 | kTcp, |
| 139 | kSsltcp, |
| 140 | kTls, |
| 141 | }; |
| 142 | |
| 143 | struct 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 | |
| 158 | struct SentPacket { |
| 159 | SentPacket(); |
Bjorn Mellem | 3a9c46d | 2018-04-25 13:24:48 -0700 | [diff] [blame] | 160 | 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 Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 164 | |
Bjorn Mellem | 3a9c46d | 2018-04-25 13:24:48 -0700 | [diff] [blame] | 165 | int64_t packet_id = -1; |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 166 | int64_t send_time_ms = -1; |
| 167 | rtc::PacketInfo info; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | // General interface for the socket implementations of various networks. The |
| 171 | // methods match those of normal UNIX sockets very closely. |
| 172 | class 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 186 | 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 188 | // |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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 195 | virtual Socket* Accept(SocketAddress* paddr) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 196 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 201 | enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 202 | virtual ConnState GetState() const = 0; |
| 203 | |
| 204 | enum Option { |
| 205 | OPT_DONTFRAGMENT, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 206 | 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 211 | 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 227 | #endif // RTC_BASE_SOCKET_H_ |