diff options
-rw-r--r-- | net/base/winsock_util.cc | 13 | ||||
-rw-r--r-- | net/base/winsock_util.h | 22 | ||||
-rw-r--r-- | net/socket/tcp_client_socket_win.cc | 5 | ||||
-rw-r--r-- | net/udp/udp_socket_win.cc | 3 |
4 files changed, 38 insertions, 5 deletions
diff --git a/net/base/winsock_util.cc b/net/base/winsock_util.cc index 02f59d6..8a4083a 100644 --- a/net/base/winsock_util.cc +++ b/net/base/winsock_util.cc @@ -30,6 +30,8 @@ void CheckEventWait(WSAEVENT hEvent, DWORD wait_rv, DWORD expected) { #pragma optimize( "", on ) #pragma warning (default: 4748) +net::PlatformSocketFactory* g_socket_factory = NULL; + } // namespace void AssertEventNotSignaled(WSAEVENT hEvent) { @@ -48,4 +50,15 @@ bool ResetEventIfSignaled(WSAEVENT hEvent) { return true; } +void PlatformSocketFactory::SetInstance(PlatformSocketFactory* factory) { + g_socket_factory = factory; +} + +SOCKET CreatePlatformSocket(int family, int type, int protocol) { + if (g_socket_factory) + return g_socket_factory->CreateSocket(family, type, protocol); + else + return ::WSASocket(family, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED); +} + } // namespace net diff --git a/net/base/winsock_util.h b/net/base/winsock_util.h index 8baa802..06ac448 100644 --- a/net/base/winsock_util.h +++ b/net/base/winsock_util.h @@ -7,6 +7,8 @@ #include <winsock2.h> +#include "net/base/net_export.h" + namespace net { // Assert that the (manual-reset) event object is not signaled. @@ -22,6 +24,26 @@ void AssertEventNotSignaled(WSAEVENT hEvent); // optimization. The code still works if this function simply returns false. bool ResetEventIfSignaled(WSAEVENT hEvent); +// Interface to create Windows Socket. +// Usually such factories are used for testing purposes, which is not true in +// this case. This interface is used to substitute WSASocket to make possible +// execution of some network code in sandbox. +class NET_EXPORT PlatformSocketFactory { + public: + PlatformSocketFactory() {} + virtual ~PlatformSocketFactory() {} + + // Creates Windows socket. See WSASocket documentation of parameters. + virtual SOCKET CreateSocket(int family, int type, int protocol) = 0; + + // Replace WSASocket with given factory. The factory will be used by + // CreatePlatformSocket. + static void SetInstance(PlatformSocketFactory* factory); +}; + +// Creates Windows Socket. See WSASocket documentation of parameters. +SOCKET CreatePlatformSocket(int family, int type, int protocol); + } // namespace net #endif // NET_BASE_WINSOCK_UTIL_H_ diff --git a/net/socket/tcp_client_socket_win.cc b/net/socket/tcp_client_socket_win.cc index da4c6ee..9b0a5b5 100644 --- a/net/socket/tcp_client_socket_win.cc +++ b/net/socket/tcp_client_socket_win.cc @@ -122,11 +122,10 @@ int SetupSocket(SOCKET socket) { // Creates a new socket and sets default parameters for it. Returns // the OS error code (or 0 on success). int CreateSocket(int family, SOCKET* socket) { - *socket = WSASocket(family, SOCK_STREAM, IPPROTO_TCP, NULL, 0, - WSA_FLAG_OVERLAPPED); + *socket = CreatePlatformSocket(family, SOCK_STREAM, IPPROTO_TCP); if (*socket == INVALID_SOCKET) { int os_error = WSAGetLastError(); - LOG(ERROR) << "WSASocket failed: " << os_error; + LOG(ERROR) << "CreatePlatformSocket failed: " << os_error; return os_error; } int error = SetupSocket(*socket); diff --git a/net/udp/udp_socket_win.cc b/net/udp/udp_socket_win.cc index c3fd561..fe97d0b 100644 --- a/net/udp/udp_socket_win.cc +++ b/net/udp/udp_socket_win.cc @@ -358,8 +358,7 @@ int UDPSocketWin::Bind(const IPEndPoint& address) { int UDPSocketWin::CreateSocket(const IPEndPoint& address) { addr_family_ = address.GetSockAddrFamily(); - socket_ = WSASocket(addr_family_, SOCK_DGRAM, IPPROTO_UDP, - NULL, 0, WSA_FLAG_OVERLAPPED); + socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, IPPROTO_UDP); if (socket_ == INVALID_SOCKET) return MapSystemError(WSAGetLastError()); core_ = new Core(this); |