| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/socket_descriptor.h" | 5 #include "net/socket/socket_descriptor.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
| 8 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
| 10 #endif | 10 #endif |
| 11 | 11 |
| 12 #include "base/basictypes.h" | |
| 13 | |
| 14 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
| 15 #include <ws2tcpip.h> | 13 #include <ws2tcpip.h> |
| 16 #include "base/win/windows_version.h" | 14 #include "base/win/windows_version.h" |
| 17 #include "net/base/winsock_init.h" | 15 #include "net/base/winsock_init.h" |
| 18 #endif | 16 #endif |
| 19 | 17 |
| 20 namespace net { | 18 namespace net { |
| 21 | 19 |
| 22 PlatformSocketFactory* g_socket_factory = NULL; | 20 PlatformSocketFactory* g_socket_factory = nullptr; |
| 23 | 21 |
| 24 PlatformSocketFactory::PlatformSocketFactory() { | 22 PlatformSocketFactory::PlatformSocketFactory() { |
| 25 } | 23 } |
| 26 | 24 |
| 27 PlatformSocketFactory::~PlatformSocketFactory() { | 25 PlatformSocketFactory::~PlatformSocketFactory() { |
| 28 } | 26 } |
| 29 | 27 |
| 30 void PlatformSocketFactory::SetInstance(PlatformSocketFactory* factory) { | 28 void PlatformSocketFactory::SetInstance(PlatformSocketFactory* factory) { |
| 31 g_socket_factory = factory; | 29 g_socket_factory = factory; |
| 32 } | 30 } |
| 33 | 31 |
| 34 SocketDescriptor CreateSocketDefault(int family, int type, int protocol) { | 32 SocketDescriptor CreateSocketDefault(int family, int type, int protocol) { |
| 35 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
| 36 EnsureWinsockInit(); | 34 EnsureWinsockInit(); |
| 37 SocketDescriptor result = ::WSASocket(family, type, protocol, NULL, 0, | 35 SocketDescriptor result = ::WSASocket(family, type, protocol, nullptr, 0, |
| 38 WSA_FLAG_OVERLAPPED); | 36 WSA_FLAG_OVERLAPPED); |
| 39 if (result != kInvalidSocket && family == AF_INET6 && | 37 if (result != kInvalidSocket && family == AF_INET6 && |
| 40 base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_VISTA) { | 38 base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_VISTA) { |
| 41 DWORD value = 0; | 39 DWORD value = 0; |
| 42 if (setsockopt(result, IPPROTO_IPV6, IPV6_V6ONLY, | 40 if (setsockopt(result, IPPROTO_IPV6, IPV6_V6ONLY, |
| 43 reinterpret_cast<const char*>(&value), sizeof(value))) { | 41 reinterpret_cast<const char*>(&value), sizeof(value))) { |
| 44 closesocket(result); | 42 closesocket(result); |
| 45 return kInvalidSocket; | 43 return kInvalidSocket; |
| 46 } | 44 } |
| 47 } | 45 } |
| 48 return result; | 46 return result; |
| 49 #else // OS_WIN | 47 #else // OS_WIN |
| 50 return ::socket(family, type, protocol); | 48 return ::socket(family, type, protocol); |
| 51 #endif // OS_WIN | 49 #endif // OS_WIN |
| 52 } | 50 } |
| 53 | 51 |
| 54 SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) { | 52 SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) { |
| 55 if (g_socket_factory) | 53 if (g_socket_factory) |
| 56 return g_socket_factory->CreateSocket(family, type, protocol); | 54 return g_socket_factory->CreateSocket(family, type, protocol); |
| 57 else | 55 else |
| 58 return CreateSocketDefault(family, type, protocol); | 56 return CreateSocketDefault(family, type, protocol); |
| 59 } | 57 } |
| 60 | 58 |
| 61 } // namespace net | 59 } // namespace net |
| OLD | NEW |