ReactOS 0.4.15-dev-7942-gd23573b
network.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS RPC Subsystem Service
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: UUID initialization.
5 * COPYRIGHT: Copyright 2019 Pierre Schweitzer
6 */
7
8/* INCLUDES *****************************************************************/
9
10/* PSDK/NDK Headers */
11#define WIN32_NO_STATUS
12#include <windef.h>
13#include <winbase.h>
14#include <winreg.h>
15#include <winsvc.h>
16#include <ntddndis.h>
17
18#include <ndk/exfuncs.h>
19#include <ntsecapi.h>
20
21#define NDEBUG
22#include <debug.h>
23
24#define SEED_BUFFER_SIZE 6
25
26/* FUNCTIONS ****************************************************************/
27
28static BOOLEAN
29getMacAddress(UCHAR * MacAddress)
30{
31 /* FIXME: query NDIS for all the interfaces */
33 return FALSE;
34}
35
36static VOID
38{
39 CHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
40 CHAR * CurrentChar;
41 DWORD Size;
42 LARGE_INTEGER PerformanceCount;
43 PDWORD NodeBegin, NodeMiddle;
44 DWORD dwValue;
45 MEMORYSTATUS MemoryStatus;
46 LUID Luid;
47 WCHAR szSystem[MAX_PATH];
48 ULARGE_INTEGER FreeBytesToCaller, TotalBytes, TotalFreeBytes;
49
50 /* Initialize node id */
51 memset(NodeId, 0x11, SEED_BUFFER_SIZE * sizeof(UCHAR));
52
53 /* Randomize it with computer name */
55 if (GetComputerNameA(ComputerName, &Size))
56 {
57 Size = 0;
58 CurrentChar = &ComputerName[0];
59 while (*CurrentChar != ANSI_NULL)
60 {
61 NodeId[Size] ^= *CurrentChar;
62 ++CurrentChar;
63
64 /* Don't overflow our NodeId */
65 if (++Size >= SEED_BUFFER_SIZE)
66 {
67 Size = 0;
68 }
69 }
70 }
71
72 /* Now, we'll work directly on DWORD values */
73 NodeBegin = (DWORD *)&NodeId[0];
74 NodeMiddle = (DWORD *)&NodeId[2];
75
76 /* Randomize with performance counter */
77 if (QueryPerformanceCounter(&PerformanceCount))
78 {
79 *NodeMiddle = *NodeMiddle ^ PerformanceCount.u.HighPart ^ PerformanceCount.u.LowPart;
80
81 dwValue = PerformanceCount.u.HighPart ^ PerformanceCount.u.LowPart ^ *NodeBegin;
82 }
83 else
84 {
85 dwValue = *NodeBegin;
86 }
87
88 *NodeBegin = *NodeBegin ^ dwValue;
89 *NodeMiddle = *NodeMiddle ^ *NodeBegin;
90
91 /* Then, with memory status */
92 MemoryStatus.dwLength = sizeof(MemoryStatus);
93 GlobalMemoryStatus(&MemoryStatus);
94
95 *NodeBegin = *NodeBegin ^ MemoryStatus.dwMemoryLoad;
96 *NodeMiddle = *NodeMiddle ^ MemoryStatus.dwTotalPhys;
97 *NodeBegin = *NodeBegin ^ MemoryStatus.dwTotalPageFile ^ MemoryStatus.dwAvailPhys;
98 *NodeMiddle = *NodeMiddle ^ MemoryStatus.dwTotalVirtual ^ MemoryStatus.dwAvailPageFile;
99 *NodeBegin = *NodeBegin ^ MemoryStatus.dwAvailVirtual;
100
101 /* With a LUID */
102 if (AllocateLocallyUniqueId(&Luid))
103 {
104 *NodeBegin = *NodeBegin ^ Luid.LowPart;
105 *NodeMiddle = *NodeMiddle ^ Luid.HighPart;
106 }
107
108 /* Get system directory */
109 GetSystemDirectoryW(szSystem, ARRAYSIZE(szSystem));
110
111 /* And finally with free disk space */
112 if (GetDiskFreeSpaceExW(szSystem, &FreeBytesToCaller, &TotalBytes, &TotalFreeBytes))
113 {
114 *NodeMiddle ^= FreeBytesToCaller.LowPart ^ TotalFreeBytes.HighPart;
115 *NodeMiddle ^= FreeBytesToCaller.HighPart ^ TotalFreeBytes.LowPart;
116 *NodeBegin ^= TotalBytes.LowPart ^ TotalFreeBytes.LowPart;
117 *NodeBegin ^= TotalBytes.HighPart ^ TotalFreeBytes.HighPart;
118 }
119
120 /*
121 * Because it was locally generated, force the seed to be local
122 * setting this, will trigger the check for validness in the kernel
123 * and make the seed local
124 */
125 NodeId[0] |= 0x80u;
126}
127
129{
131 UCHAR UuidSeed[SEED_BUFFER_SIZE];
132
133 /* First, try to get a multicast MAC address */
134 if (!getMacAddress(UuidSeed))
135 {
136 DPRINT1("Failed finding a proper MAC address, will generate seed\n");
137 CookupNodeId(UuidSeed);
138 }
139
140 /* Seed our UUID generator */
141 Status = NtSetUuidSeed(UuidSeed);
142 if (!NT_SUCCESS(Status))
143 {
144 DPRINT1("NtSetUuidSeed failed with status: %lx\n", Status);
145 }
146}
unsigned char BOOLEAN
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define SEED_BUFFER_SIZE
Definition: network.c:24
static VOID CookupNodeId(UCHAR *NodeId)
Definition: network.c:37
static BOOLEAN getMacAddress(UCHAR *MacAddress)
Definition: network.c:29
#define UNIMPLEMENTED
Definition: debug.h:115
VOID DealWithDeviceEvent()
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
BOOL WINAPI AllocateLocallyUniqueId(PLUID Luid)
Definition: security.c:1218
#define MAX_PATH
Definition: compat.h:34
BOOL WINAPI GetDiskFreeSpaceExW(IN LPCWSTR lpDirectoryName OPTIONAL, OUT PULARGE_INTEGER lpFreeBytesAvailableToCaller, OUT PULARGE_INTEGER lpTotalNumberOfBytes, OUT PULARGE_INTEGER lpTotalNumberOfFreeBytes)
Definition: disk.c:342
UINT WINAPI GetSystemDirectoryW(OUT LPWSTR lpBuffer, IN UINT uSize)
Definition: path.c:2313
BOOL WINAPI QueryPerformanceCounter(OUT PLARGE_INTEGER lpPerformanceCount)
Definition: perfcnt.c:23
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
VOID NTAPI GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer)
Definition: heapmem.c:1365
#define ANSI_NULL
NTSTATUS NTAPI NtSetUuidSeed(IN PUCHAR Seed)
Definition: uuid.c:547
DWORD * PDWORD
Definition: pedump.c:68
#define memset(x, y, z)
Definition: compat.h:39
BOOL WINAPI SHIM_OBJ_NAME() GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
Definition: shimtest.c:21
LONG HighPart
DWORD LowPart
SIZE_T dwTotalPhys
Definition: winbase.h:1218
DWORD dwLength
Definition: winbase.h:1216
DWORD dwMemoryLoad
Definition: winbase.h:1217
SIZE_T dwAvailPageFile
Definition: winbase.h:1221
SIZE_T dwAvailVirtual
Definition: winbase.h:1223
SIZE_T dwAvailPhys
Definition: winbase.h:1219
SIZE_T dwTotalPageFile
Definition: winbase.h:1220
SIZE_T dwTotalVirtual
Definition: winbase.h:1222
$ULONG LowPart
Definition: ntbasedef.h:569
$ULONG HighPart
Definition: ntbasedef.h:570
struct _LARGE_INTEGER::@2295 u
ULONG LowPart
Definition: typedefs.h:106
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
#define MAX_COMPUTERNAME_LENGTH
Definition: winbase.h:243
_Must_inspect_result_ _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_ SIZE_T TotalBytes
Definition: mmfuncs.h:228
unsigned char UCHAR
Definition: xmlstorage.h:181
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175