Botan  1.11.15
src/lib/entropy/win32_stats/es_win32.cpp
Go to the documentation of this file.
00001 /*
00002 * Win32 EntropySource
00003 * (C) 1999-2009 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/internal/es_win32.h>
00009 #include <windows.h>
00010 #include <tlhelp32.h>
00011 
00012 namespace Botan {
00013 
00014 /**
00015 * Win32 poll using stats functions including Tooltip32
00016 */
00017 void Win32_EntropySource::poll(Entropy_Accumulator& accum)
00018    {
00019    /*
00020    First query a bunch of basic statistical stuff, though
00021    don't count it for much in terms of contributed entropy.
00022    */
00023    accum.add(GetTickCount(), 0);
00024    accum.add(GetMessagePos(), 0);
00025    accum.add(GetMessageTime(), 0);
00026    accum.add(GetInputState(), 0);
00027    accum.add(GetCurrentProcessId(), 0);
00028    accum.add(GetCurrentThreadId(), 0);
00029 
00030    SYSTEM_INFO sys_info;
00031    GetSystemInfo(&sys_info);
00032    accum.add(sys_info, 1);
00033 
00034    MEMORYSTATUS mem_info;
00035    GlobalMemoryStatus(&mem_info);
00036    accum.add(mem_info, 1);
00037 
00038    POINT point;
00039    GetCursorPos(&point);
00040    accum.add(point, 1);
00041 
00042    GetCaretPos(&point);
00043    accum.add(point, 1);
00044 
00045    LARGE_INTEGER perf_counter;
00046    QueryPerformanceCounter(&perf_counter);
00047    accum.add(perf_counter, 0);
00048 
00049    /*
00050    Now use the Tooltip library to iterate throug various objects on
00051    the system, including processes, threads, and heap objects.
00052    */
00053 
00054    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
00055 
00056 #define TOOLHELP32_ITER(DATA_TYPE, FUNC_FIRST, FUNC_NEXT) \
00057    if(!accum.polling_goal_achieved())                     \
00058       {                                                   \
00059       DATA_TYPE info;                                     \
00060       info.dwSize = sizeof(DATA_TYPE);                    \
00061       if(FUNC_FIRST(snapshot, &info))                     \
00062          {                                                \
00063          do                                               \
00064             {                                             \
00065             accum.add(info, 1);                           \
00066             } while(FUNC_NEXT(snapshot, &info));          \
00067          }                                                \
00068       }
00069 
00070    TOOLHELP32_ITER(MODULEENTRY32, Module32First, Module32Next);
00071    TOOLHELP32_ITER(PROCESSENTRY32, Process32First, Process32Next);
00072    TOOLHELP32_ITER(THREADENTRY32, Thread32First, Thread32Next);
00073 
00074 #undef TOOLHELP32_ITER
00075 
00076    if(!accum.polling_goal_achieved())
00077       {
00078       size_t heap_lists_found = 0;
00079       HEAPLIST32 heap_list;
00080       heap_list.dwSize = sizeof(HEAPLIST32);
00081 
00082       const size_t HEAP_LISTS_MAX = 32;
00083       const size_t HEAP_OBJS_PER_LIST = 128;
00084 
00085       if(Heap32ListFirst(snapshot, &heap_list))
00086          {
00087          do
00088             {
00089             accum.add(heap_list, 1);
00090 
00091             if(++heap_lists_found > HEAP_LISTS_MAX)
00092                break;
00093 
00094             size_t heap_objs_found = 0;
00095             HEAPENTRY32 heap_entry;
00096             heap_entry.dwSize = sizeof(HEAPENTRY32);
00097             if(Heap32First(&heap_entry, heap_list.th32ProcessID,
00098                            heap_list.th32HeapID))
00099                {
00100                do
00101                   {
00102                   if(heap_objs_found++ > HEAP_OBJS_PER_LIST)
00103                      break;
00104                   accum.add(heap_entry, 1);
00105                   } while(Heap32Next(&heap_entry));
00106                }
00107 
00108             if(accum.polling_goal_achieved())
00109                break;
00110 
00111             } while(Heap32ListNext(snapshot, &heap_list));
00112          }
00113       }
00114 
00115    CloseHandle(snapshot);
00116    }
00117 
00118 }