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__
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
38 #define SOCKET_EACCES EACCES
42 #include "talk/base/win32.h"
45 #include "talk/base/basictypes.h"
46 #include "talk/base/socketaddress.h"
48 // Rather than converting errors into a private namespace,
49 // Reuse the POSIX socket api errors. Note this depends on
50 // Win32 compatibility.
53 #undef EWOULDBLOCK // Remove errno.h's definition for each macro below.
54 #define EWOULDBLOCK WSAEWOULDBLOCK
56 #define EINPROGRESS WSAEINPROGRESS
58 #define EALREADY WSAEALREADY
60 #define ENOTSOCK WSAENOTSOCK
62 #define EDESTADDRREQ WSAEDESTADDRREQ
64 #define EMSGSIZE WSAEMSGSIZE
66 #define EPROTOTYPE WSAEPROTOTYPE
68 #define ENOPROTOOPT WSAENOPROTOOPT
69 #undef EPROTONOSUPPORT
70 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
71 #undef ESOCKTNOSUPPORT
72 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
74 #define EOPNOTSUPP WSAEOPNOTSUPP
76 #define EPFNOSUPPORT WSAEPFNOSUPPORT
78 #define EAFNOSUPPORT WSAEAFNOSUPPORT
80 #define EADDRINUSE WSAEADDRINUSE
82 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
84 #define ENETDOWN WSAENETDOWN
86 #define ENETUNREACH WSAENETUNREACH
88 #define ENETRESET WSAENETRESET
90 #define ECONNABORTED WSAECONNABORTED
92 #define ECONNRESET WSAECONNRESET
94 #define ENOBUFS WSAENOBUFS
96 #define EISCONN WSAEISCONN
98 #define ENOTCONN WSAENOTCONN
100 #define ESHUTDOWN WSAESHUTDOWN
102 #define ETOOMANYREFS WSAETOOMANYREFS
104 #define ETIMEDOUT WSAETIMEDOUT
106 #define ECONNREFUSED WSAECONNREFUSED
108 #define ELOOP WSAELOOP
110 #define ENAMETOOLONG WSAENAMETOOLONG
112 #define EHOSTDOWN WSAEHOSTDOWN
114 #define EHOSTUNREACH WSAEHOSTUNREACH
116 #define ENOTEMPTY WSAENOTEMPTY
118 #define EPROCLIM WSAEPROCLIM
120 #define EUSERS WSAEUSERS
122 #define EDQUOT WSAEDQUOT
124 #define ESTALE WSAESTALE
126 #define EREMOTE WSAEREMOTE
128 #define SOCKET_EACCES WSAEACCES
132 #define INVALID_SOCKET (-1)
133 #define SOCKET_ERROR (-1)
134 #define closesocket(s) close(s)
137 namespace talk_base {
139 inline bool IsBlockingError(int e) {
140 return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
143 // General interface for the socket implementations of various networks. The
144 // methods match those of normal UNIX sockets very closely.
149 // Returns the address to which the socket is bound. If the socket is not
150 // bound, then the any-address is returned.
151 virtual SocketAddress GetLocalAddress() const = 0;
153 // Returns the address to which the socket is connected. If the socket is
154 // not connected, then the any-address is returned.
155 virtual SocketAddress GetRemoteAddress() const = 0;
157 virtual int Bind(const SocketAddress& addr) = 0;
158 virtual int Connect(const SocketAddress& addr) = 0;
159 virtual int Send(const void *pv, size_t cb) = 0;
160 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
161 virtual int Recv(void *pv, size_t cb) = 0;
162 virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr) = 0;
163 virtual int Listen(int backlog) = 0;
164 virtual Socket *Accept(SocketAddress *paddr) = 0;
165 virtual int Close() = 0;
166 virtual int GetError() const = 0;
167 virtual void SetError(int error) = 0;
168 inline bool IsBlocking() const { return IsBlockingError(GetError()); }
175 virtual ConnState GetState() const = 0;
177 // Fills in the given uint16 with the current estimate of the MTU along the
178 // path to the address to which this socket is connected. NOTE: This method
179 // can block for up to 10 seconds on Windows.
180 virtual int EstimateMTU(uint16* mtu) = 0;
184 OPT_RCVBUF, // receive buffer size
185 OPT_SNDBUF, // send buffer size
186 OPT_NODELAY, // whether Nagle algorithm is enabled
187 OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
188 OPT_DSCP, // DSCP code
189 OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
190 // This is specific to libjingle and will be used
191 // if SendTime option is needed at socket level.
193 virtual int GetOption(Option opt, int* value) = 0;
194 virtual int SetOption(Option opt, int value) = 0;
200 DISALLOW_EVIL_CONSTRUCTORS(Socket);
203 } // namespace talk_base
205 #endif // TALK_BASE_SOCKET_H__