Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / socket.h
1 /*
2  * libjingle
3  * Copyright 2004--2005, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27
28 #ifndef TALK_BASE_SOCKET_H__
29 #define TALK_BASE_SOCKET_H__
30
31 #include <errno.h>
32
33 #ifdef POSIX
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
39 #endif
40
41 #ifdef WIN32
42 #include "talk/base/win32.h"
43 #endif
44
45 #include "talk/base/basictypes.h"
46 #include "talk/base/socketaddress.h"
47
48 // Rather than converting errors into a private namespace,
49 // Reuse the POSIX socket api errors. Note this depends on
50 // Win32 compatibility.
51
52 #ifdef WIN32
53 #undef EWOULDBLOCK  // Remove errno.h's definition for each macro below.
54 #define EWOULDBLOCK WSAEWOULDBLOCK
55 #undef EINPROGRESS
56 #define EINPROGRESS WSAEINPROGRESS
57 #undef EALREADY
58 #define EALREADY WSAEALREADY
59 #undef ENOTSOCK
60 #define ENOTSOCK WSAENOTSOCK
61 #undef EDESTADDRREQ
62 #define EDESTADDRREQ WSAEDESTADDRREQ
63 #undef EMSGSIZE
64 #define EMSGSIZE WSAEMSGSIZE
65 #undef EPROTOTYPE
66 #define EPROTOTYPE WSAEPROTOTYPE
67 #undef ENOPROTOOPT
68 #define ENOPROTOOPT WSAENOPROTOOPT
69 #undef EPROTONOSUPPORT
70 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
71 #undef ESOCKTNOSUPPORT
72 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
73 #undef EOPNOTSUPP
74 #define EOPNOTSUPP WSAEOPNOTSUPP
75 #undef EPFNOSUPPORT
76 #define EPFNOSUPPORT WSAEPFNOSUPPORT
77 #undef EAFNOSUPPORT
78 #define EAFNOSUPPORT WSAEAFNOSUPPORT
79 #undef EADDRINUSE
80 #define EADDRINUSE WSAEADDRINUSE
81 #undef EADDRNOTAVAIL
82 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
83 #undef ENETDOWN
84 #define ENETDOWN WSAENETDOWN
85 #undef ENETUNREACH
86 #define ENETUNREACH WSAENETUNREACH
87 #undef ENETRESET
88 #define ENETRESET WSAENETRESET
89 #undef ECONNABORTED
90 #define ECONNABORTED WSAECONNABORTED
91 #undef ECONNRESET
92 #define ECONNRESET WSAECONNRESET
93 #undef ENOBUFS
94 #define ENOBUFS WSAENOBUFS
95 #undef EISCONN
96 #define EISCONN WSAEISCONN
97 #undef ENOTCONN
98 #define ENOTCONN WSAENOTCONN
99 #undef ESHUTDOWN
100 #define ESHUTDOWN WSAESHUTDOWN
101 #undef ETOOMANYREFS
102 #define ETOOMANYREFS WSAETOOMANYREFS
103 #undef ETIMEDOUT
104 #define ETIMEDOUT WSAETIMEDOUT
105 #undef ECONNREFUSED
106 #define ECONNREFUSED WSAECONNREFUSED
107 #undef ELOOP
108 #define ELOOP WSAELOOP
109 #undef ENAMETOOLONG
110 #define ENAMETOOLONG WSAENAMETOOLONG
111 #undef EHOSTDOWN
112 #define EHOSTDOWN WSAEHOSTDOWN
113 #undef EHOSTUNREACH
114 #define EHOSTUNREACH WSAEHOSTUNREACH
115 #undef ENOTEMPTY
116 #define ENOTEMPTY WSAENOTEMPTY
117 #undef EPROCLIM
118 #define EPROCLIM WSAEPROCLIM
119 #undef EUSERS
120 #define EUSERS WSAEUSERS
121 #undef EDQUOT
122 #define EDQUOT WSAEDQUOT
123 #undef ESTALE
124 #define ESTALE WSAESTALE
125 #undef EREMOTE
126 #define EREMOTE WSAEREMOTE
127 #undef EACCES
128 #define SOCKET_EACCES WSAEACCES
129 #endif  // WIN32
130
131 #ifdef POSIX
132 #define INVALID_SOCKET (-1)
133 #define SOCKET_ERROR (-1)
134 #define closesocket(s) close(s)
135 #endif  // POSIX
136
137 namespace talk_base {
138
139 inline bool IsBlockingError(int e) {
140   return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
141 }
142
143 // General interface for the socket implementations of various networks.  The
144 // methods match those of normal UNIX sockets very closely.
145 class Socket {
146  public:
147   virtual ~Socket() {}
148
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;
152
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;
156
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()); }
169
170   enum ConnState {
171     CS_CLOSED,
172     CS_CONNECTING,
173     CS_CONNECTED
174   };
175   virtual ConnState GetState() const = 0;
176
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;
181
182   enum Option {
183     OPT_DONTFRAGMENT,
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.
192   };
193   virtual int GetOption(Option opt, int* value) = 0;
194   virtual int SetOption(Option opt, int value) = 0;
195
196  protected:
197   Socket() {}
198
199  private:
200   DISALLOW_EVIL_CONSTRUCTORS(Socket);
201 };
202
203 }  // namespace talk_base
204
205 #endif  // TALK_BASE_SOCKET_H__