aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/installer/fetcher/systeminfo.c
blob: 39f343898434540a6959f1a04af0e04e88ccb38a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020 Jason A. Donenfeld. All Rights Reserved.
 */

#include "systeminfo.h"
#include "version.h"
#include <windows.h>
#include <stdbool.h>
#include <stdio.h>

extern NTAPI __declspec(dllimport) void RtlGetNtVersionNumbers(DWORD *MajorVersion, DWORD *MinorVersion, DWORD *BuildNumber);

const char *architecture(void)
{
	static const char *cached_arch;
	HMODULE kernel32;
	BOOL(WINAPI *IsWow64Process2)(HANDLE hProcess, USHORT *pProcessMachine, USHORT *pNativeMachine);
	USHORT process_machine, native_machine;
	BOOL is_wow64_process;

	if (cached_arch)
		return cached_arch;

	kernel32 = GetModuleHandleA("kernel32.dll");
	if (!kernel32)
		return NULL;
	IsWow64Process2 = (void *)GetProcAddress(kernel32, "IsWow64Process2");
	if (IsWow64Process2) {
		if (!IsWow64Process2(GetCurrentProcess(), &process_machine, &native_machine))
			return NULL;
		switch (native_machine) {
		case IMAGE_FILE_MACHINE_I386:
			return cached_arch = "x86";
		case IMAGE_FILE_MACHINE_AMD64:
			return cached_arch = "amd64";
		case IMAGE_FILE_MACHINE_ARMNT:
			return cached_arch = "arm";
		case IMAGE_FILE_MACHINE_ARM64:
			return cached_arch = "arm64";
		}
	} else {
		if (!IsWow64Process(GetCurrentProcess(), &is_wow64_process))
			return NULL;
		return cached_arch = is_wow64_process ? "amd64" : "x86";
	}
	return NULL;
}

const char *useragent(void)
{
	static char useragent[0x200];
	DWORD maj, min, build;

	if (useragent[0])
		return useragent;
	RtlGetNtVersionNumbers(&maj, &min, &build);
	_snprintf_s(useragent, sizeof(useragent), _TRUNCATE, "WireGuard-Fetcher/" VERSION_STR " (Windows %lu.%lu.%lu; %s)", maj, min, build & 0xffff, architecture());
	return useragent;
}

bool is_win7(void)
{
	DWORD maj, min, build;
	RtlGetNtVersionNumbers(&maj, &min, &build);
	return maj == 6 && min == 1;
}