3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * 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.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * 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,
23 * 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
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_SOCKET_H__
29 #define TALK_BASE_SOCKET_H__
31 #if defined(__native_client__)
33 // These should never be defined or instantiated.
36 } // namespace talk_base
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
50 #include "talk/base/win32.h"
53 #include "talk/base/basictypes.h"
54 #include "talk/base/socketaddress.h"
56 // Rather than converting errors into a private namespace,
57 // Reuse the POSIX socket api errors. Note this depends on
58 // Win32 compatibility.
61 #undef EWOULDBLOCK // Remove errno.h's definition for each macro below.
62 #define EWOULDBLOCK WSAEWOULDBLOCK
64 #define EINPROGRESS WSAEINPROGRESS
66 #define EALREADY WSAEALREADY
68 #define ENOTSOCK WSAENOTSOCK
70 #define EDESTADDRREQ WSAEDESTADDRREQ
72 #define EMSGSIZE WSAEMSGSIZE
74 #define EPROTOTYPE WSAEPROTOTYPE
76 #define ENOPROTOOPT WSAENOPROTOOPT
77 #undef EPROTONOSUPPORT
78 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
79 #undef ESOCKTNOSUPPORT
80 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
82 #define EOPNOTSUPP WSAEOPNOTSUPP
84 #define EPFNOSUPPORT WSAEPFNOSUPPORT
86 #define EAFNOSUPPORT WSAEAFNOSUPPORT
88 #define EADDRINUSE WSAEADDRINUSE
90 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
92 #define ENETDOWN WSAENETDOWN
94 #define ENETUNREACH WSAENETUNREACH
96 #define ENETRESET WSAENETRESET
98 #define ECONNABORTED WSAECONNABORTED
100 #define ECONNRESET WSAECONNRESET
102 #define ENOBUFS WSAENOBUFS
104 #define EISCONN WSAEISCONN
106 #define ENOTCONN WSAENOTCONN
108 #define ESHUTDOWN WSAESHUTDOWN
110 #define ETOOMANYREFS WSAETOOMANYREFS
112 #define ETIMEDOUT WSAETIMEDOUT
114 #define ECONNREFUSED WSAECONNREFUSED
116 #define ELOOP WSAELOOP
118 #define ENAMETOOLONG WSAENAMETOOLONG
120 #define EHOSTDOWN WSAEHOSTDOWN
122 #define EHOSTUNREACH WSAEHOSTUNREACH
124 #define ENOTEMPTY WSAENOTEMPTY
126 #define EPROCLIM WSAEPROCLIM
128 #define EUSERS WSAEUSERS
130 #define EDQUOT WSAEDQUOT
132 #define ESTALE WSAESTALE
134 #define EREMOTE WSAEREMOTE
136 #define SOCKET_EACCES WSAEACCES
140 #define INVALID_SOCKET (-1)
141 #define SOCKET_ERROR (-1)
142 #define closesocket(s) close(s)
145 namespace talk_base {
147 inline bool IsBlockingError(int e) {
148 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
151 // General interface for the socket implementations of various networks. The
152 // methods match those of normal UNIX sockets very closely.
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;
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;
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()); }
183 virtual ConnState GetState() const = 0;
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;
192 OPT_RCVBUF, // receive buffer size
193 OPT_SNDBUF, // send buffer size
194 OPT_NODELAY, // whether Nagle algorithm is enabled
195 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
196 OPT_DSCP // DSCP code
198 virtual int GetOption(Option opt, int* value) = 0;
199 virtual int SetOption(Option opt, int value) = 0;
205 DISALLOW_EVIL_CONSTRUCTORS(Socket);
208 } // namespace talk_base
210 #endif // !__native_client__
211 #endif // TALK_BASE_SOCKET_H__