blob: c78963b7688eaac5c1afbf1c35f7e26041f8e613 [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)
17#include <sys/types.h>
18#include <sys/socket.h>
19#include <arpa/inet.h>
20#include <netinet/in.h>
21#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/basictypes.h"
29#include "rtc_base/constructormagic.h"
30#include "rtc_base/socketaddress.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031
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
111#undef EACCES
112#define SOCKET_EACCES WSAEACCES
113#endif // WEBRTC_WIN
114
115#if defined(WEBRTC_POSIX)
116#define INVALID_SOCKET (-1)
117#define SOCKET_ERROR (-1)
118#define closesocket(s) close(s)
119#endif // WEBRTC_POSIX
120
121namespace rtc {
122
123inline bool IsBlockingError(int e) {
124 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
125}
126
127struct SentPacket {
128 SentPacket() : packet_id(-1), send_time_ms(-1) {}
129 SentPacket(int packet_id, int64_t send_time_ms)
130 : packet_id(packet_id), send_time_ms(send_time_ms) {}
131
132 int packet_id;
133 int64_t send_time_ms;
134};
135
136// General interface for the socket implementations of various networks. The
137// methods match those of normal UNIX sockets very closely.
138class Socket {
139 public:
140 virtual ~Socket() {}
141
142 // Returns the address to which the socket is bound. If the socket is not
143 // bound, then the any-address is returned.
144 virtual SocketAddress GetLocalAddress() const = 0;
145
146 // Returns the address to which the socket is connected. If the socket is
147 // not connected, then the any-address is returned.
148 virtual SocketAddress GetRemoteAddress() const = 0;
149
150 virtual int Bind(const SocketAddress& addr) = 0;
151 virtual int Connect(const SocketAddress& addr) = 0;
152 virtual int Send(const void *pv, size_t cb) = 0;
153 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
154 // |timestamp| is in units of microseconds.
155 virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
156 virtual int RecvFrom(void* pv,
157 size_t cb,
158 SocketAddress* paddr,
159 int64_t* timestamp) = 0;
160 virtual int Listen(int backlog) = 0;
161 virtual Socket *Accept(SocketAddress *paddr) = 0;
162 virtual int Close() = 0;
163 virtual int GetError() const = 0;
164 virtual void SetError(int error) = 0;
165 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
166
167 enum ConnState {
168 CS_CLOSED,
169 CS_CONNECTING,
170 CS_CONNECTED
171 };
172 virtual ConnState GetState() const = 0;
173
174 enum Option {
175 OPT_DONTFRAGMENT,
176 OPT_RCVBUF, // receive buffer size
177 OPT_SNDBUF, // send buffer size
178 OPT_NODELAY, // whether Nagle algorithm is enabled
179 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
180 OPT_DSCP, // DSCP code
181 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
182 // This is specific to libjingle and will be used
183 // if SendTime option is needed at socket level.
184 };
185 virtual int GetOption(Option opt, int* value) = 0;
186 virtual int SetOption(Option opt, int value) = 0;
187
188 protected:
189 Socket() {}
190
191 private:
192 RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
193};
194
195} // namespace rtc
196
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200197#endif // RTC_BASE_SOCKET_H_