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" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 30 | |
| 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 42 | #undef EMSGSIZE |
| 43 | #define EMSGSIZE WSAEMSGSIZE |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 44 | #undef EADDRINUSE |
| 45 | #define EADDRINUSE WSAEADDRINUSE |
| 46 | #undef EADDRNOTAVAIL |
| 47 | #define EADDRNOTAVAIL WSAEADDRNOTAVAIL |
| 48 | #undef ENETDOWN |
| 49 | #define ENETDOWN WSAENETDOWN |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 50 | #undef ECONNABORTED |
| 51 | #define ECONNABORTED WSAECONNABORTED |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 52 | #undef ENOBUFS |
| 53 | #define ENOBUFS WSAENOBUFS |
| 54 | #undef EISCONN |
| 55 | #define EISCONN WSAEISCONN |
| 56 | #undef ENOTCONN |
| 57 | #define ENOTCONN WSAENOTCONN |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 58 | #undef ECONNREFUSED |
| 59 | #define ECONNREFUSED WSAECONNREFUSED |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 60 | #undef EHOSTUNREACH |
| 61 | #define EHOSTUNREACH WSAEHOSTUNREACH |
Berthold Herrmann | 1184b55 | 2021-02-05 10:46:26 +0100 | [diff] [blame] | 62 | #undef ENETUNREACH |
| 63 | #define ENETUNREACH WSAENETUNREACH |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 64 | #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 | |
| 73 | namespace rtc { |
| 74 | |
| 75 | inline bool IsBlockingError(int e) { |
| 76 | return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS); |
| 77 | } |
| 78 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | // General interface for the socket implementations of various networks. The |
| 80 | // methods match those of normal UNIX sockets very closely. |
| 81 | class Socket { |
| 82 | public: |
| 83 | virtual ~Socket() {} |
| 84 | |
| 85 | // Returns the address to which the socket is bound. If the socket is not |
| 86 | // bound, then the any-address is returned. |
| 87 | virtual SocketAddress GetLocalAddress() const = 0; |
| 88 | |
| 89 | // Returns the address to which the socket is connected. If the socket is |
| 90 | // not connected, then the any-address is returned. |
| 91 | virtual SocketAddress GetRemoteAddress() const = 0; |
| 92 | |
| 93 | virtual int Bind(const SocketAddress& addr) = 0; |
| 94 | virtual int Connect(const SocketAddress& addr) = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 95 | virtual int Send(const void* pv, size_t cb) = 0; |
| 96 | 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^] | 97 | // `timestamp` is in units of microseconds. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 98 | virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0; |
| 99 | virtual int RecvFrom(void* pv, |
| 100 | size_t cb, |
| 101 | SocketAddress* paddr, |
| 102 | int64_t* timestamp) = 0; |
| 103 | virtual int Listen(int backlog) = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 104 | virtual Socket* Accept(SocketAddress* paddr) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 105 | virtual int Close() = 0; |
| 106 | virtual int GetError() const = 0; |
| 107 | virtual void SetError(int error) = 0; |
| 108 | inline bool IsBlocking() const { return IsBlockingError(GetError()); } |
| 109 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 110 | enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 111 | virtual ConnState GetState() const = 0; |
| 112 | |
| 113 | enum Option { |
| 114 | OPT_DONTFRAGMENT, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 115 | OPT_RCVBUF, // receive buffer size |
| 116 | OPT_SNDBUF, // send buffer size |
| 117 | OPT_NODELAY, // whether Nagle algorithm is enabled |
| 118 | OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only. |
| 119 | OPT_DSCP, // DSCP code |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 120 | OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param. |
| 121 | // This is specific to libjingle and will be used |
| 122 | // if SendTime option is needed at socket level. |
| 123 | }; |
| 124 | virtual int GetOption(Option opt, int* value) = 0; |
| 125 | virtual int SetOption(Option opt, int value) = 0; |
| 126 | |
| 127 | protected: |
| 128 | Socket() {} |
| 129 | |
| 130 | private: |
| 131 | RTC_DISALLOW_COPY_AND_ASSIGN(Socket); |
| 132 | }; |
| 133 | |
| 134 | } // namespace rtc |
| 135 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 136 | #endif // RTC_BASE_SOCKET_H_ |