diff options
author | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-02 20:40:27 +0000 |
---|---|---|
committer | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-02 20:40:27 +0000 |
commit | 87bb0cede643624622da4c68f41d50497a98a126 (patch) | |
tree | 3bc86b0380b5ed670d3c3215e1b21a1681e42e4e /net | |
parent | 35d9cb23b3359d47f180b6111e7d162e81889e6d (diff) | |
download | chromium_src-87bb0cede643624622da4c68f41d50497a98a126.zip chromium_src-87bb0cede643624622da4c68f41d50497a98a126.tar.gz chromium_src-87bb0cede643624622da4c68f41d50497a98a126.tar.bz2 |
Added PlatformSocketFactory to override WSASocket calls.
This allows to run mDns code in sandbox by replacing WSASocket with pre-created sockets.
Current design of UDPSocket combines WSASocket and bind. But WSASocket must be called outside of sandbox when bind can be called inside of sandbox. So probably this factory is simplest way to make it work in sandbox without changing UDPSocket API.
BUG=245391
Review URL: https://chromiumcodereview.appspot.com/17706004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@209768 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-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); |