license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be | ||||
3 | // found in the LICENSE file. | ||||
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
5 | #include <winsock2.h> | ||||
6 | |||||
7 | #include "net/base/winsock_init.h" | ||||
8 | |||||
[email protected] | 6b0c5e6c | 2009-04-14 18:02:56 | [diff] [blame] | 9 | #include "base/logging.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | #include "base/singleton.h" |
11 | |||||
[email protected] | 14d5cfb | 2008-10-06 10:41:35 | [diff] [blame] | 12 | namespace { |
13 | |||||
14 | class WinsockInitSingleton { | ||||
15 | public: | ||||
16 | WinsockInitSingleton() : did_init_(false) { | ||||
[email protected] | 6b0c5e6c | 2009-04-14 18:02:56 | [diff] [blame] | 17 | WORD winsock_ver = MAKEWORD(2, 2); |
[email protected] | 14d5cfb | 2008-10-06 10:41:35 | [diff] [blame] | 18 | WSAData wsa_data; |
19 | did_init_ = (WSAStartup(winsock_ver, &wsa_data) == 0); | ||||
[email protected] | 6b0c5e6c | 2009-04-14 18:02:56 | [diff] [blame] | 20 | if (did_init_) { |
21 | DCHECK(wsa_data.wVersion == winsock_ver); | ||||
[email protected] | 14d5cfb | 2008-10-06 10:41:35 | [diff] [blame] | 22 | |
[email protected] | 6b0c5e6c | 2009-04-14 18:02:56 | [diff] [blame] | 23 | // The first time WSAGetLastError is called, the delay load helper will |
24 | // resolve the address with GetProcAddress and fixup the import. If a | ||||
25 | // third party application hooks system functions without correctly | ||||
26 | // restoring the error code, it is possible that the error code will be | ||||
27 | // overwritten during delay load resolution. The result of the first | ||||
28 | // call may be incorrect, so make sure the function is bound and future | ||||
29 | // results will be correct. | ||||
30 | WSAGetLastError(); | ||||
31 | } | ||||
[email protected] | 14d5cfb | 2008-10-06 10:41:35 | [diff] [blame] | 32 | } |
33 | |||||
34 | ~WinsockInitSingleton() { | ||||
35 | if (did_init_) | ||||
36 | WSACleanup(); | ||||
37 | } | ||||
38 | |||||
39 | private: | ||||
40 | bool did_init_; | ||||
41 | }; | ||||
42 | |||||
43 | } // namespace | ||||
44 | |||||
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 45 | namespace net { |
46 | |||||
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | void EnsureWinsockInit() { |
[email protected] | 14d5cfb | 2008-10-06 10:41:35 | [diff] [blame] | 48 | Singleton<WinsockInitSingleton>::get(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | } |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 50 | |
51 | } // namespace net |