blob: c2d1e3d29afe46adb72de758be4c95ad8f3667be [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
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/constructor_magic.h"
29#include "rtc_base/socket_address.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030
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 07:52:50 +020042#undef EMSGSIZE
43#define EMSGSIZE WSAEMSGSIZE
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044#undef EADDRINUSE
45#define EADDRINUSE WSAEADDRINUSE
46#undef EADDRNOTAVAIL
47#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
48#undef ENETDOWN
49#define ENETDOWN WSAENETDOWN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050#undef ECONNABORTED
51#define ECONNABORTED WSAECONNABORTED
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052#undef ENOBUFS
53#define ENOBUFS WSAENOBUFS
54#undef EISCONN
55#define EISCONN WSAEISCONN
56#undef ENOTCONN
57#define ENOTCONN WSAENOTCONN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058#undef ECONNREFUSED
59#define ECONNREFUSED WSAECONNREFUSED
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060#undef EHOSTUNREACH
61#define EHOSTUNREACH WSAEHOSTUNREACH
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062#define SOCKET_EACCES WSAEACCES
63#endif // WEBRTC_WIN
64
65#if defined(WEBRTC_POSIX)
66#define INVALID_SOCKET (-1)
67#define SOCKET_ERROR (-1)
68#define closesocket(s) close(s)
69#endif // WEBRTC_POSIX
70
71namespace rtc {
72
73inline bool IsBlockingError(int e) {
74 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
75}
76
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077// General interface for the socket implementations of various networks. The
78// methods match those of normal UNIX sockets very closely.
79class Socket {
80 public:
81 virtual ~Socket() {}
82
83 // Returns the address to which the socket is bound. If the socket is not
84 // bound, then the any-address is returned.
85 virtual SocketAddress GetLocalAddress() const = 0;
86
87 // Returns the address to which the socket is connected. If the socket is
88 // not connected, then the any-address is returned.
89 virtual SocketAddress GetRemoteAddress() const = 0;
90
91 virtual int Bind(const SocketAddress& addr) = 0;
92 virtual int Connect(const SocketAddress& addr) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +020093 virtual int Send(const void* pv, size_t cb) = 0;
94 virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095 // |timestamp| is in units of microseconds.
96 virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
97 virtual int RecvFrom(void* pv,
98 size_t cb,
99 SocketAddress* paddr,
100 int64_t* timestamp) = 0;
101 virtual int Listen(int backlog) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200102 virtual Socket* Accept(SocketAddress* paddr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 virtual int Close() = 0;
104 virtual int GetError() const = 0;
105 virtual void SetError(int error) = 0;
106 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
107
Yves Gerey665174f2018-06-19 15:03:05 +0200108 enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED };
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109 virtual ConnState GetState() const = 0;
110
111 enum Option {
112 OPT_DONTFRAGMENT,
Yves Gerey665174f2018-06-19 15:03:05 +0200113 OPT_RCVBUF, // receive buffer size
114 OPT_SNDBUF, // send buffer size
115 OPT_NODELAY, // whether Nagle algorithm is enabled
116 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
117 OPT_DSCP, // DSCP code
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
119 // This is specific to libjingle and will be used
120 // if SendTime option is needed at socket level.
121 };
122 virtual int GetOption(Option opt, int* value) = 0;
123 virtual int SetOption(Option opt, int value) = 0;
124
125 protected:
126 Socket() {}
127
128 private:
129 RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
130};
131
132} // namespace rtc
133
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200134#endif // RTC_BASE_SOCKET_H_