henrike@webrtc.org | f7795df | 2014-05-13 18:00:26 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_SOCKET_H__ |
| 12 | #define WEBRTC_BASE_SOCKET_H__ |
| 13 | |
| 14 | #include <errno.h> |
| 15 | |
| 16 | #if defined(WEBRTC_POSIX) |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <arpa/inet.h> |
| 20 | #include <netinet/in.h> |
| 21 | #define SOCKET_EACCES EACCES |
| 22 | #endif |
| 23 | |
| 24 | #if defined(WEBRTC_WIN) |
| 25 | #include "webrtc/base/win32.h" |
| 26 | #endif |
| 27 | |
| 28 | #include "webrtc/base/basictypes.h" |
| 29 | #include "webrtc/base/socketaddress.h" |
| 30 | |
| 31 | // Rather than converting errors into a private namespace, |
| 32 | // Reuse the POSIX socket api errors. Note this depends on |
| 33 | // Win32 compatibility. |
| 34 | |
| 35 | #if defined(WEBRTC_WIN) |
| 36 | #undef EWOULDBLOCK // Remove errno.h's definition for each macro below. |
| 37 | #define EWOULDBLOCK WSAEWOULDBLOCK |
| 38 | #undef EINPROGRESS |
| 39 | #define EINPROGRESS WSAEINPROGRESS |
| 40 | #undef EALREADY |
| 41 | #define EALREADY WSAEALREADY |
| 42 | #undef ENOTSOCK |
| 43 | #define ENOTSOCK WSAENOTSOCK |
| 44 | #undef EDESTADDRREQ |
| 45 | #define EDESTADDRREQ WSAEDESTADDRREQ |
| 46 | #undef EMSGSIZE |
| 47 | #define EMSGSIZE WSAEMSGSIZE |
| 48 | #undef EPROTOTYPE |
| 49 | #define EPROTOTYPE WSAEPROTOTYPE |
| 50 | #undef ENOPROTOOPT |
| 51 | #define ENOPROTOOPT WSAENOPROTOOPT |
| 52 | #undef EPROTONOSUPPORT |
| 53 | #define EPROTONOSUPPORT WSAEPROTONOSUPPORT |
| 54 | #undef ESOCKTNOSUPPORT |
| 55 | #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT |
| 56 | #undef EOPNOTSUPP |
| 57 | #define EOPNOTSUPP WSAEOPNOTSUPP |
| 58 | #undef EPFNOSUPPORT |
| 59 | #define EPFNOSUPPORT WSAEPFNOSUPPORT |
| 60 | #undef EAFNOSUPPORT |
| 61 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 62 | #undef EADDRINUSE |
| 63 | #define EADDRINUSE WSAEADDRINUSE |
| 64 | #undef EADDRNOTAVAIL |
| 65 | #define EADDRNOTAVAIL WSAEADDRNOTAVAIL |
| 66 | #undef ENETDOWN |
| 67 | #define ENETDOWN WSAENETDOWN |
| 68 | #undef ENETUNREACH |
| 69 | #define ENETUNREACH WSAENETUNREACH |
| 70 | #undef ENETRESET |
| 71 | #define ENETRESET WSAENETRESET |
| 72 | #undef ECONNABORTED |
| 73 | #define ECONNABORTED WSAECONNABORTED |
| 74 | #undef ECONNRESET |
| 75 | #define ECONNRESET WSAECONNRESET |
| 76 | #undef ENOBUFS |
| 77 | #define ENOBUFS WSAENOBUFS |
| 78 | #undef EISCONN |
| 79 | #define EISCONN WSAEISCONN |
| 80 | #undef ENOTCONN |
| 81 | #define ENOTCONN WSAENOTCONN |
| 82 | #undef ESHUTDOWN |
| 83 | #define ESHUTDOWN WSAESHUTDOWN |
| 84 | #undef ETOOMANYREFS |
| 85 | #define ETOOMANYREFS WSAETOOMANYREFS |
| 86 | #undef ETIMEDOUT |
| 87 | #define ETIMEDOUT WSAETIMEDOUT |
| 88 | #undef ECONNREFUSED |
| 89 | #define ECONNREFUSED WSAECONNREFUSED |
| 90 | #undef ELOOP |
| 91 | #define ELOOP WSAELOOP |
| 92 | #undef ENAMETOOLONG |
| 93 | #define ENAMETOOLONG WSAENAMETOOLONG |
| 94 | #undef EHOSTDOWN |
| 95 | #define EHOSTDOWN WSAEHOSTDOWN |
| 96 | #undef EHOSTUNREACH |
| 97 | #define EHOSTUNREACH WSAEHOSTUNREACH |
| 98 | #undef ENOTEMPTY |
| 99 | #define ENOTEMPTY WSAENOTEMPTY |
| 100 | #undef EPROCLIM |
| 101 | #define EPROCLIM WSAEPROCLIM |
| 102 | #undef EUSERS |
| 103 | #define EUSERS WSAEUSERS |
| 104 | #undef EDQUOT |
| 105 | #define EDQUOT WSAEDQUOT |
| 106 | #undef ESTALE |
| 107 | #define ESTALE WSAESTALE |
| 108 | #undef EREMOTE |
| 109 | #define EREMOTE WSAEREMOTE |
| 110 | #undef EACCES |
| 111 | #define SOCKET_EACCES WSAEACCES |
| 112 | #endif // WEBRTC_WIN |
| 113 | |
| 114 | #if defined(WEBRTC_POSIX) |
| 115 | #define INVALID_SOCKET (-1) |
| 116 | #define SOCKET_ERROR (-1) |
| 117 | #define closesocket(s) close(s) |
| 118 | #endif // WEBRTC_POSIX |
| 119 | |
| 120 | namespace rtc { |
| 121 | |
| 122 | inline bool IsBlockingError(int e) { |
| 123 | return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS); |
| 124 | } |
| 125 | |
| 126 | // General interface for the socket implementations of various networks. The |
| 127 | // methods match those of normal UNIX sockets very closely. |
| 128 | class Socket { |
| 129 | public: |
| 130 | virtual ~Socket() {} |
| 131 | |
| 132 | // Returns the address to which the socket is bound. If the socket is not |
| 133 | // bound, then the any-address is returned. |
| 134 | virtual SocketAddress GetLocalAddress() const = 0; |
| 135 | |
| 136 | // Returns the address to which the socket is connected. If the socket is |
| 137 | // not connected, then the any-address is returned. |
| 138 | virtual SocketAddress GetRemoteAddress() const = 0; |
| 139 | |
| 140 | virtual int Bind(const SocketAddress& addr) = 0; |
| 141 | virtual int Connect(const SocketAddress& addr) = 0; |
| 142 | virtual int Send(const void *pv, size_t cb) = 0; |
| 143 | virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0; |
| 144 | virtual int Recv(void *pv, size_t cb) = 0; |
| 145 | virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr) = 0; |
| 146 | virtual int Listen(int backlog) = 0; |
| 147 | virtual Socket *Accept(SocketAddress *paddr) = 0; |
| 148 | virtual int Close() = 0; |
| 149 | virtual int GetError() const = 0; |
| 150 | virtual void SetError(int error) = 0; |
| 151 | inline bool IsBlocking() const { return IsBlockingError(GetError()); } |
| 152 | |
| 153 | enum ConnState { |
| 154 | CS_CLOSED, |
| 155 | CS_CONNECTING, |
| 156 | CS_CONNECTED |
| 157 | }; |
| 158 | virtual ConnState GetState() const = 0; |
| 159 | |
| 160 | // Fills in the given uint16 with the current estimate of the MTU along the |
| 161 | // path to the address to which this socket is connected. NOTE: This method |
| 162 | // can block for up to 10 seconds on Windows. |
| 163 | virtual int EstimateMTU(uint16* mtu) = 0; |
| 164 | |
| 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 | OPT_DSCP, // DSCP code |
| 172 | OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param. |
| 173 | // This is specific to libjingle and will be used |
| 174 | // if SendTime option is needed at socket level. |
| 175 | }; |
| 176 | virtual int GetOption(Option opt, int* value) = 0; |
| 177 | virtual int SetOption(Option opt, int value) = 0; |
| 178 | |
| 179 | protected: |
| 180 | Socket() {} |
| 181 | |
| 182 | private: |
| 183 | DISALLOW_EVIL_CONSTRUCTORS(Socket); |
| 184 | }; |
| 185 | |
| 186 | } // namespace rtc |
| 187 | |
| 188 | #endif // WEBRTC_BASE_SOCKET_H__ |