blob: 84a0e84b68bcc9447397a0400911cbe6d96122c0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/constructormagic.h"
29#include "rtc_base/socketaddress.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
42#undef ENOTSOCK
43#define ENOTSOCK WSAENOTSOCK
44#undef EDESTADDRREQ
45#define EDESTADDRREQ WSAEDESTADDRREQ
46#undef EMSGSIZE
47#define EMSGSIZE WSAEMSGSIZE
48#undef EPROTOTYPE
49#define EPROTOTYPE WSAEPROTOTYPE
50#undef ENOPROTOOPT
51#define ENOPROTOOPT WSAENOPROTOOPT
52#undef EPROTONOSUPPORT
53#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
54#undef ESOCKTNOSUPPORT
55#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
56#undef EOPNOTSUPP
57#define EOPNOTSUPP WSAEOPNOTSUPP
58#undef EPFNOSUPPORT
59#define EPFNOSUPPORT WSAEPFNOSUPPORT
60#undef EAFNOSUPPORT
61#define EAFNOSUPPORT WSAEAFNOSUPPORT
62#undef EADDRINUSE
63#define EADDRINUSE WSAEADDRINUSE
64#undef EADDRNOTAVAIL
65#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
66#undef ENETDOWN
67#define ENETDOWN WSAENETDOWN
68#undef ENETUNREACH
69#define ENETUNREACH WSAENETUNREACH
70#undef ENETRESET
71#define ENETRESET WSAENETRESET
72#undef ECONNABORTED
73#define ECONNABORTED WSAECONNABORTED
74#undef ECONNRESET
75#define ECONNRESET WSAECONNRESET
76#undef ENOBUFS
77#define ENOBUFS WSAENOBUFS
78#undef EISCONN
79#define EISCONN WSAEISCONN
80#undef ENOTCONN
81#define ENOTCONN WSAENOTCONN
82#undef ESHUTDOWN
83#define ESHUTDOWN WSAESHUTDOWN
84#undef ETOOMANYREFS
85#define ETOOMANYREFS WSAETOOMANYREFS
86#undef ETIMEDOUT
87#define ETIMEDOUT WSAETIMEDOUT
88#undef ECONNREFUSED
89#define ECONNREFUSED WSAECONNREFUSED
90#undef ELOOP
91#define ELOOP WSAELOOP
92#undef ENAMETOOLONG
93#define ENAMETOOLONG WSAENAMETOOLONG
94#undef EHOSTDOWN
95#define EHOSTDOWN WSAEHOSTDOWN
96#undef EHOSTUNREACH
97#define EHOSTUNREACH WSAEHOSTUNREACH
98#undef ENOTEMPTY
99#define ENOTEMPTY WSAENOTEMPTY
100#undef EPROCLIM
101#define EPROCLIM WSAEPROCLIM
102#undef EUSERS
103#define EUSERS WSAEUSERS
104#undef EDQUOT
105#define EDQUOT WSAEDQUOT
106#undef ESTALE
107#define ESTALE WSAESTALE
108#undef EREMOTE
109#define EREMOTE WSAEREMOTE
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110#define SOCKET_EACCES WSAEACCES
111#endif // WEBRTC_WIN
112
113#if defined(WEBRTC_POSIX)
114#define INVALID_SOCKET (-1)
115#define SOCKET_ERROR (-1)
116#define closesocket(s) close(s)
117#endif // WEBRTC_POSIX
118
119namespace rtc {
120
121inline bool IsBlockingError(int e) {
122 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
123}
124
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200125// General interface for the socket implementations of various networks. The
126// methods match those of normal UNIX sockets very closely.
127class Socket {
128 public:
129 virtual ~Socket() {}
130
131 // Returns the address to which the socket is bound. If the socket is not
132 // bound, then the any-address is returned.
133 virtual SocketAddress GetLocalAddress() const = 0;
134
135 // Returns the address to which the socket is connected. If the socket is
136 // not connected, then the any-address is returned.
137 virtual SocketAddress GetRemoteAddress() const = 0;
138
139 virtual int Bind(const SocketAddress& addr) = 0;
140 virtual int Connect(const SocketAddress& addr) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200141 virtual int Send(const void* pv, size_t cb) = 0;
142 virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200143 // |timestamp| is in units of microseconds.
144 virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
145 virtual int RecvFrom(void* pv,
146 size_t cb,
147 SocketAddress* paddr,
148 int64_t* timestamp) = 0;
149 virtual int Listen(int backlog) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200150 virtual Socket* Accept(SocketAddress* paddr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151 virtual int Close() = 0;
152 virtual int GetError() const = 0;
153 virtual void SetError(int error) = 0;
154 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
155
Yves Gerey665174f2018-06-19 15:03:05 +0200156 enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED };
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200157 virtual ConnState GetState() const = 0;
158
159 enum Option {
160 OPT_DONTFRAGMENT,
Yves Gerey665174f2018-06-19 15:03:05 +0200161 OPT_RCVBUF, // receive buffer size
162 OPT_SNDBUF, // send buffer size
163 OPT_NODELAY, // whether Nagle algorithm is enabled
164 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
165 OPT_DSCP, // DSCP code
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200166 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
167 // This is specific to libjingle and will be used
168 // if SendTime option is needed at socket level.
169 };
170 virtual int GetOption(Option opt, int* value) = 0;
171 virtual int SetOption(Option opt, int value) = 0;
172
173 protected:
174 Socket() {}
175
176 private:
177 RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
178};
179
180} // namespace rtc
181
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200182#endif // RTC_BASE_SOCKET_H_