blob: 56e3ebceea123caeaee191a9604fc75d9dc53496 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
mallinath@webrtc.org0dac5372014-01-28 06:58:42 +00005 * Redistribution and use in source and binary forms, with or without
henrike@webrtc.org28e20752013-07-10 00:45:36 +00006 * modification, are permitted provided that the following conditions are met:
7 *
mallinath@webrtc.org0dac5372014-01-28 06:58:42 +00008 * 1. Redistributions of source code must retain the above copyright notice,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
mallinath@webrtc.org0dac5372014-01-28 06:58:42 +000013 * 3. The name of the author may not be used to endorse or promote products
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
mallinath@webrtc.org0dac5372014-01-28 06:58:42 +000017 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
mallinath@webrtc.org0dac5372014-01-28 06:58:42 +000019 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
mallinath@webrtc.org0dac5372014-01-28 06:58:42 +000023 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_BASE_SOCKET_H__
29#define TALK_BASE_SOCKET_H__
30
mallinath@webrtc.org0dac5372014-01-28 06:58:42 +000031#if defined(__native_client__)
32namespace talk_base {
33// These should never be defined or instantiated.
34class Socket;
35class AsyncSocket;
36} // namespace talk_base
37#else
38
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include <errno.h>
40
41#ifdef POSIX
42#include <sys/types.h>
43#include <sys/socket.h>
44#include <arpa/inet.h>
45#include <netinet/in.h>
46#define SOCKET_EACCES EACCES
47#endif
48
49#ifdef WIN32
50#include "talk/base/win32.h"
51#endif
52
53#include "talk/base/basictypes.h"
54#include "talk/base/socketaddress.h"
55
56// Rather than converting errors into a private namespace,
57// Reuse the POSIX socket api errors. Note this depends on
58// Win32 compatibility.
59
60#ifdef WIN32
61#undef EWOULDBLOCK // Remove errno.h's definition for each macro below.
62#define EWOULDBLOCK WSAEWOULDBLOCK
63#undef EINPROGRESS
64#define EINPROGRESS WSAEINPROGRESS
65#undef EALREADY
66#define EALREADY WSAEALREADY
67#undef ENOTSOCK
68#define ENOTSOCK WSAENOTSOCK
69#undef EDESTADDRREQ
70#define EDESTADDRREQ WSAEDESTADDRREQ
71#undef EMSGSIZE
72#define EMSGSIZE WSAEMSGSIZE
73#undef EPROTOTYPE
74#define EPROTOTYPE WSAEPROTOTYPE
75#undef ENOPROTOOPT
76#define ENOPROTOOPT WSAENOPROTOOPT
77#undef EPROTONOSUPPORT
78#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
79#undef ESOCKTNOSUPPORT
80#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
81#undef EOPNOTSUPP
82#define EOPNOTSUPP WSAEOPNOTSUPP
83#undef EPFNOSUPPORT
84#define EPFNOSUPPORT WSAEPFNOSUPPORT
85#undef EAFNOSUPPORT
86#define EAFNOSUPPORT WSAEAFNOSUPPORT
87#undef EADDRINUSE
88#define EADDRINUSE WSAEADDRINUSE
89#undef EADDRNOTAVAIL
90#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
91#undef ENETDOWN
92#define ENETDOWN WSAENETDOWN
93#undef ENETUNREACH
94#define ENETUNREACH WSAENETUNREACH
95#undef ENETRESET
96#define ENETRESET WSAENETRESET
97#undef ECONNABORTED
98#define ECONNABORTED WSAECONNABORTED
99#undef ECONNRESET
100#define ECONNRESET WSAECONNRESET
101#undef ENOBUFS
102#define ENOBUFS WSAENOBUFS
103#undef EISCONN
104#define EISCONN WSAEISCONN
105#undef ENOTCONN
106#define ENOTCONN WSAENOTCONN
107#undef ESHUTDOWN
108#define ESHUTDOWN WSAESHUTDOWN
109#undef ETOOMANYREFS
110#define ETOOMANYREFS WSAETOOMANYREFS
111#undef ETIMEDOUT
112#define ETIMEDOUT WSAETIMEDOUT
113#undef ECONNREFUSED
114#define ECONNREFUSED WSAECONNREFUSED
115#undef ELOOP
116#define ELOOP WSAELOOP
117#undef ENAMETOOLONG
118#define ENAMETOOLONG WSAENAMETOOLONG
119#undef EHOSTDOWN
120#define EHOSTDOWN WSAEHOSTDOWN
121#undef EHOSTUNREACH
122#define EHOSTUNREACH WSAEHOSTUNREACH
123#undef ENOTEMPTY
124#define ENOTEMPTY WSAENOTEMPTY
125#undef EPROCLIM
126#define EPROCLIM WSAEPROCLIM
127#undef EUSERS
128#define EUSERS WSAEUSERS
129#undef EDQUOT
130#define EDQUOT WSAEDQUOT
131#undef ESTALE
132#define ESTALE WSAESTALE
133#undef EREMOTE
134#define EREMOTE WSAEREMOTE
135#undef EACCES
136#define SOCKET_EACCES WSAEACCES
137#endif // WIN32
138
139#ifdef POSIX
140#define INVALID_SOCKET (-1)
141#define SOCKET_ERROR (-1)
142#define closesocket(s) close(s)
143#endif // POSIX
144
145namespace talk_base {
146
147inline bool IsBlockingError(int e) {
148 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
149}
150
151// General interface for the socket implementations of various networks. The
152// methods match those of normal UNIX sockets very closely.
153class Socket {
154 public:
155 virtual ~Socket() {}
156
157 // Returns the address to which the socket is bound. If the socket is not
158 // bound, then the any-address is returned.
159 virtual SocketAddress GetLocalAddress() const = 0;
160
161 // Returns the address to which the socket is connected. If the socket is
162 // not connected, then the any-address is returned.
163 virtual SocketAddress GetRemoteAddress() const = 0;
164
165 virtual int Bind(const SocketAddress& addr) = 0;
166 virtual int Connect(const SocketAddress& addr) = 0;
167 virtual int Send(const void *pv, size_t cb) = 0;
168 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
169 virtual int Recv(void *pv, size_t cb) = 0;
170 virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr) = 0;
171 virtual int Listen(int backlog) = 0;
172 virtual Socket *Accept(SocketAddress *paddr) = 0;
173 virtual int Close() = 0;
174 virtual int GetError() const = 0;
175 virtual void SetError(int error) = 0;
176 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
177
178 enum ConnState {
179 CS_CLOSED,
180 CS_CONNECTING,
181 CS_CONNECTED
182 };
183 virtual ConnState GetState() const = 0;
184
185 // Fills in the given uint16 with the current estimate of the MTU along the
186 // path to the address to which this socket is connected. NOTE: This method
187 // can block for up to 10 seconds on Windows.
188 virtual int EstimateMTU(uint16* mtu) = 0;
189
190 enum Option {
191 OPT_DONTFRAGMENT,
192 OPT_RCVBUF, // receive buffer size
193 OPT_SNDBUF, // send buffer size
194 OPT_NODELAY, // whether Nagle algorithm is enabled
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000195 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
196 OPT_DSCP // DSCP code
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 };
198 virtual int GetOption(Option opt, int* value) = 0;
199 virtual int SetOption(Option opt, int value) = 0;
200
201 protected:
202 Socket() {}
203
204 private:
205 DISALLOW_EVIL_CONSTRUCTORS(Socket);
206};
207
208} // namespace talk_base
209
mallinath@webrtc.org0dac5372014-01-28 06:58:42 +0000210#endif // !__native_client__
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211#endif // TALK_BASE_SOCKET_H__