You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef TALK_BASE_SOCKET_H__
  28. #define TALK_BASE_SOCKET_H__
  29. #include <errno.h>
  30. #ifdef POSIX
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <arpa/inet.h>
  34. #include <netinet/in.h>
  35. #define SOCKET_EACCES EACCES
  36. #endif
  37. #ifdef WIN32
  38. #include "talk/base/win32.h"
  39. #endif
  40. #include "talk/base/basictypes.h"
  41. #include "talk/base/socketaddress.h"
  42. // Rather than converting errors into a private namespace,
  43. // Reuse the POSIX socket api errors. Note this depends on
  44. // Win32 compatibility.
  45. #ifdef WIN32
  46. #undef EWOULDBLOCK // Remove errno.h's definition for each macro below.
  47. #define EWOULDBLOCK WSAEWOULDBLOCK
  48. #undef EINPROGRESS
  49. #define EINPROGRESS WSAEINPROGRESS
  50. #undef EALREADY
  51. #define EALREADY WSAEALREADY
  52. #undef ENOTSOCK
  53. #define ENOTSOCK WSAENOTSOCK
  54. #undef EDESTADDRREQ
  55. #define EDESTADDRREQ WSAEDESTADDRREQ
  56. #undef EMSGSIZE
  57. #define EMSGSIZE WSAEMSGSIZE
  58. #undef EPROTOTYPE
  59. #define EPROTOTYPE WSAEPROTOTYPE
  60. #undef ENOPROTOOPT
  61. #define ENOPROTOOPT WSAENOPROTOOPT
  62. #undef EPROTONOSUPPORT
  63. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  64. #undef ESOCKTNOSUPPORT
  65. #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
  66. #undef EOPNOTSUPP
  67. #define EOPNOTSUPP WSAEOPNOTSUPP
  68. #undef EPFNOSUPPORT
  69. #define EPFNOSUPPORT WSAEPFNOSUPPORT
  70. #undef EAFNOSUPPORT
  71. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  72. #undef EADDRINUSE
  73. #define EADDRINUSE WSAEADDRINUSE
  74. #undef EADDRNOTAVAIL
  75. #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
  76. #undef ENETDOWN
  77. #define ENETDOWN WSAENETDOWN
  78. #undef ENETUNREACH
  79. #define ENETUNREACH WSAENETUNREACH
  80. #undef ENETRESET
  81. #define ENETRESET WSAENETRESET
  82. #undef ECONNABORTED
  83. #define ECONNABORTED WSAECONNABORTED
  84. #undef ECONNRESET
  85. #define ECONNRESET WSAECONNRESET
  86. #undef ENOBUFS
  87. #define ENOBUFS WSAENOBUFS
  88. #undef EISCONN
  89. #define EISCONN WSAEISCONN
  90. #undef ENOTCONN
  91. #define ENOTCONN WSAENOTCONN
  92. #undef ESHUTDOWN
  93. #define ESHUTDOWN WSAESHUTDOWN
  94. #undef ETOOMANYREFS
  95. #define ETOOMANYREFS WSAETOOMANYREFS
  96. #undef ETIMEDOUT
  97. #define ETIMEDOUT WSAETIMEDOUT
  98. #undef ECONNREFUSED
  99. #define ECONNREFUSED WSAECONNREFUSED
  100. #undef ELOOP
  101. #define ELOOP WSAELOOP
  102. #undef ENAMETOOLONG
  103. #define ENAMETOOLONG WSAENAMETOOLONG
  104. #undef EHOSTDOWN
  105. #define EHOSTDOWN WSAEHOSTDOWN
  106. #undef EHOSTUNREACH
  107. #define EHOSTUNREACH WSAEHOSTUNREACH
  108. #undef ENOTEMPTY
  109. #define ENOTEMPTY WSAENOTEMPTY
  110. #undef EPROCLIM
  111. #define EPROCLIM WSAEPROCLIM
  112. #undef EUSERS
  113. #define EUSERS WSAEUSERS
  114. #undef EDQUOT
  115. #define EDQUOT WSAEDQUOT
  116. #undef ESTALE
  117. #define ESTALE WSAESTALE
  118. #undef EREMOTE
  119. #define EREMOTE WSAEREMOTE
  120. #undef EACCES
  121. #define SOCKET_EACCES WSAEACCES
  122. #endif // WIN32
  123. #ifdef POSIX
  124. #define INVALID_SOCKET (-1)
  125. #define SOCKET_ERROR (-1)
  126. #define closesocket(s) close(s)
  127. #endif // POSIX
  128. namespace talk_base {
  129. inline bool IsBlockingError(int e) {
  130. return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
  131. }
  132. // General interface for the socket implementations of various networks. The
  133. // methods match those of normal UNIX sockets very closely.
  134. class Socket {
  135. public:
  136. virtual ~Socket() {}
  137. // Returns the address to which the socket is bound. If the socket is not
  138. // bound, then the any-address is returned.
  139. virtual SocketAddress GetLocalAddress() const = 0;
  140. // Returns the address to which the socket is connected. If the socket is
  141. // not connected, then the any-address is returned.
  142. virtual SocketAddress GetRemoteAddress() const = 0;
  143. virtual int Bind(const SocketAddress& addr) = 0;
  144. virtual int Connect(const SocketAddress& addr) = 0;
  145. virtual int Send(const void *pv, size_t cb) = 0;
  146. virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
  147. virtual int Recv(void *pv, size_t cb) = 0;
  148. virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr) = 0;
  149. virtual int Listen(int backlog) = 0;
  150. virtual Socket *Accept(SocketAddress *paddr) = 0;
  151. virtual int Close() = 0;
  152. virtual int GetError() const = 0;
  153. virtual void SetError(int error) = 0;
  154. inline bool IsBlocking() const { return IsBlockingError(GetError()); }
  155. enum ConnState {
  156. CS_CLOSED,
  157. CS_CONNECTING,
  158. CS_CONNECTED
  159. };
  160. virtual ConnState GetState() const = 0;
  161. // Fills in the given uint16 with the current estimate of the MTU along the
  162. // path to the address to which this socket is connected. NOTE: This method
  163. // can block for up to 10 seconds on Windows.
  164. virtual int EstimateMTU(uint16* mtu) = 0;
  165. enum Option {
  166. OPT_DONTFRAGMENT,
  167. OPT_RCVBUF, // receive buffer size
  168. OPT_SNDBUF, // send buffer size
  169. OPT_NODELAY, // whether Nagle algorithm is enabled
  170. OPT_IPV6_V6ONLY // Whether the socket is IPv6 only.
  171. };
  172. virtual int GetOption(Option opt, int* value) = 0;
  173. virtual int SetOption(Option opt, int value) = 0;
  174. protected:
  175. Socket() {}
  176. private:
  177. DISALLOW_EVIL_CONSTRUCTORS(Socket);
  178. };
  179. } // namespace talk_base
  180. #endif // TALK_BASE_SOCKET_H__