| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "omaha/net/net_utils.h" | |
| 17 #include <iphlpapi.h> | |
| 18 #include "base/scoped_ptr.h" | |
| 19 #include "omaha/base/logging.h" | |
| 20 #include "omaha/base/scoped_any.h" | |
| 21 #include "omaha/base/utils.h" | |
| 22 | |
| 23 namespace omaha { | |
| 24 | |
| 25 bool IsMachineConnectedToNetwork() { | |
| 26 // Get the table of information on all interfaces. | |
| 27 DWORD table_size = 0; | |
| 28 DWORD result = ::GetIfTable(NULL, &table_size, false); | |
| 29 if (result != ERROR_INSUFFICIENT_BUFFER) { | |
| 30 NET_LOG(LW, (_T("[GetIfTable failed to get the table size][%d]"), result)); | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 scoped_array<char> buffer(new char[table_size]); | |
| 35 MIB_IFTABLE* mib_table = reinterpret_cast<MIB_IFTABLE*>(buffer.get()); | |
| 36 result = ::GetIfTable(mib_table, &table_size, false); | |
| 37 if (result != NO_ERROR) { | |
| 38 NET_LOG(LW, (_T("[GetIfTable failed][%d]"), result)); | |
| 39 return true; | |
| 40 } | |
| 41 | |
| 42 // Scan the table looking for active connections. | |
| 43 bool active_LAN = false; | |
| 44 bool active_WAN = false; | |
| 45 for (size_t i = 0; i < mib_table->dwNumEntries; ++i) { | |
| 46 if (mib_table->table[i].dwType != MIB_IF_TYPE_LOOPBACK) { | |
| 47 active_WAN = active_WAN || | |
| 48 mib_table->table[i].dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED; | |
| 49 active_LAN = active_LAN || | |
| 50 mib_table->table[i].dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 return active_LAN || active_WAN; | |
| 55 } | |
| 56 | |
| 57 CString BufferToPrintableString(const void* buffer, size_t length) { | |
| 58 CString result; | |
| 59 result.Preallocate(length); | |
| 60 if (buffer) { | |
| 61 for (size_t i = 0; i != length; ++i) { | |
| 62 char ch = (static_cast<const char*>(buffer))[i]; | |
| 63 result.AppendChar((isprint(ch) || ch =='\r' || ch == '\n') ? ch : '.'); | |
| 64 } | |
| 65 } | |
| 66 return result; | |
| 67 } | |
| 68 | |
| 69 CString VectorToPrintableString(const std::vector<uint8>& response) { | |
| 70 CString str; | |
| 71 if (!response.empty()) { | |
| 72 str = BufferToPrintableString(&response.front(), response.size()); | |
| 73 } | |
| 74 return str; | |
| 75 } | |
| 76 | |
| 77 } // namespace omaha | |
| 78 | |
| OLD | NEW |