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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
// Copyright 2005-2019 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include <QtCore/QtGlobal>
#if defined(Q_OS_WIN)
# include "win.h"
#endif
#include "OSInfo.h"
#include "Version.h"
#include <QtCore/QCryptographicHash>
#include <QtCore/QProcess>
#include <QtNetwork/QNetworkInterface>
#include <QtXml/QDomDocument>
#if defined(Q_OS_WIN)
# include <intrin.h>
#endif
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
# include <sys/utsname.h>
#endif
#if defined(Q_OS_MAC)
# include <Carbon/Carbon.h>
# include <sys/types.h>
# include <sys/sysctl.h>
# include <mach-o/arch.h>
// Ignore deprecation warnings for Gestalt.
// See mumble-voip/mumble#3290 for more information.
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#if defined(Q_OS_WIN)
// regString converts a wchar_t string of size to a
// QString. If the string contains a NUL value, that
// NUL value will terminate the string.
static QString regString(wchar_t *string, int size) {
if (size <= 0) {
return QString();
}
// If string contains a NUL, adjust the size such
// that the NUL is not included in the returned
// string.
const size_t adjustedSize = wcsnlen(string, static_cast<size_t>(size));
// The return value of wcsnlen is <= size which is
// an int, so casting adjustedSize to int is safe.
return QString::fromWCharArray(string, static_cast<int>(adjustedSize));
}
/// Query for a Windows 10-style displayable version.
///
/// This returns a string of the kind:
///
/// Windows 10 Pro x86 1607 14390.0
///
/// which is:
///
/// $ProductName $Arch $Version $Build.$Ubr
///
/// Of note, $Version is formatted as YYMM.
/// So, 1607 is a Windows 10 update released in July 2016.
///
/// This function can be called on non-Windows 10 OSes.
/// On those, this functions fails to query some of the
/// registry keys used for building the version string
/// to be returned. Because of that, it is safe to call
/// this function, and if it returns an empty/null string,
/// a legacy version string can be displayed.
static QString win10DisplayableVersion() {
HKEY key = 0;
LONG err = 0;
DWORD len = 0;
wchar_t buf[64];
DWORD dw = 0;
QString productName;
QString releaseId;
QString currentBuild;
QString ubr;
QString arch;
err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &key);
if (err != ERROR_SUCCESS) {
RegCloseKey(key);
return QString();
}
len = sizeof(buf);
err = RegQueryValueEx(key, L"ProductName", NULL, NULL, reinterpret_cast<LPBYTE>(&buf[0]), &len);
if (err != ERROR_SUCCESS) {
RegCloseKey(key);
return QString();
}
productName = regString(buf, static_cast<int>(len / sizeof(buf[0])));
len = sizeof(buf);
err = RegQueryValueEx(key, L"ReleaseId", NULL, NULL, reinterpret_cast<LPBYTE>(&buf[0]), &len);
if (err != ERROR_SUCCESS) {
RegCloseKey(key);
return QString();
}
releaseId = regString(buf, static_cast<int>(len / sizeof(buf[0])));
len = sizeof(buf);
err = RegQueryValueEx(key, L"CurrentBuild", NULL, NULL, reinterpret_cast<LPBYTE>(&buf[0]), &len);
if (err != ERROR_SUCCESS) {
RegCloseKey(key);
return QString();
}
currentBuild = regString(buf, static_cast<int>(len / sizeof(buf[0])));
len = sizeof(dw);
err = RegQueryValueEx(key, L"UBR", NULL, NULL, reinterpret_cast<LPBYTE>(&dw), &len);
if (err != ERROR_SUCCESS) {
RegCloseKey(key);
return QString();
}
ubr = QString::number(static_cast<ulong>(dw), 10);
RegCloseKey(key);
_SYSTEM_INFO si;
GetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
arch = QLatin1String("x64");
} else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) {
arch = QLatin1String("x86");
} else {
arch = QLatin1String("(unknown arch)");
}
return QString::fromLatin1("%1 %2 %3 %4.%5").arg(productName, arch, releaseId, currentBuild, ubr);
}
#endif
QString OSInfo::getMacHash(const QList<QHostAddress> &qlBind) {
QString first, second, third;
foreach(const QNetworkInterface &qni, QNetworkInterface::allInterfaces()) {
if (! qni.isValid())
continue;
if (qni.flags() & QNetworkInterface::IsLoopBack)
continue;
if (qni.hardwareAddress().isEmpty())
continue;
QString hash = QString::fromUtf8(QCryptographicHash::hash(qni.hardwareAddress().toUtf8(), QCryptographicHash::Sha1).toHex());
if (third.isEmpty() || third > hash)
third = hash;
if (!(qni.flags() & (QNetworkInterface::IsUp | QNetworkInterface::IsRunning)))
continue;
if (second.isEmpty() || second > hash)
second = hash;
foreach(const QNetworkAddressEntry &qnae, qni.addressEntries()) {
const QHostAddress &qha = qnae.ip();
if (qlBind.isEmpty() || qlBind.contains(qha)) {
if (first.isEmpty() || first > hash)
first = hash;
}
}
}
if (! first.isEmpty())
return first;
if (! second.isEmpty())
return second;
if (! third.isEmpty())
return third;
return QString();
}
QString OSInfo::getOS() {
#if defined(Q_OS_WIN)
# if defined(Q_OS_WIN64)
return QLatin1String("WinX64");
# else
return QLatin1String("Win");
# endif
#elif defined(Q_OS_MAC)
return QLatin1String("OSX");
#else
return QLatin1String("X11");
#endif
}
QString OSInfo::getOSVersion() {
static QString qsCached;
if (! qsCached.isNull())
return qsCached.isEmpty() ? QString() : qsCached;
QString os;
#if defined(Q_OS_WIN)
OSVERSIONINFOEXW ovi;
memset(&ovi, 0, sizeof(ovi));
ovi.dwOSVersionInfoSize=sizeof(ovi);
if (!GetVersionEx(reinterpret_cast<OSVERSIONINFOW *>(&ovi))) {
return QString();
}
os.sprintf("%lu.%lu.%lu.%lu", static_cast<unsigned long>(ovi.dwMajorVersion), static_cast<unsigned long>(ovi.dwMinorVersion), static_cast<unsigned long>(ovi.dwBuildNumber), (ovi.wProductType == VER_NT_WORKSTATION) ? 1UL : 0UL);
#elif defined(Q_OS_MAC)
SInt32 major, minor, bugfix;
OSErr err = Gestalt(gestaltSystemVersionMajor, &major);
if (err == noErr)
err = Gestalt(gestaltSystemVersionMinor, &minor);
if (err == noErr)
err = Gestalt(gestaltSystemVersionBugFix, &bugfix);
if (err != noErr)
return QString::number(QSysInfo::MacintoshVersion, 16);
char *buildno = NULL;
char buildno_buf[32];
size_t sz_buildno_buf = sizeof(buildno);
int ret = sysctlbyname("kern.osversion", buildno_buf, &sz_buildno_buf, NULL, 0);
if (ret == 0) {
buildno = &buildno_buf[0];
}
os.sprintf("%lu.%lu.%lu %s",
static_cast<unsigned long>(major),
static_cast<unsigned long>(minor),
static_cast<unsigned long>(bugfix),
buildno ? buildno : "unknown");
#else
#ifdef Q_OS_LINUX
QProcess qp;
QStringList args;
args << QLatin1String("-s");
args << QLatin1String("-d");
qp.start(QLatin1String("lsb_release"), args);
if (qp.waitForFinished(5000)) {
os = QString::fromUtf8(qp.readAll()).simplified();
if (os.startsWith(QLatin1Char('"')) && os.endsWith(QLatin1Char('"')))
os = os.mid(1, os.length() - 2).trimmed();
}
if (os.isEmpty())
qWarning("OSInfo: Failed to execute lsb_release");
qp.terminate();
if (! qp.waitForFinished(1000))
qp.kill();
#endif
if (os.isEmpty()) {
struct utsname un;
#ifdef Q_OS_SOLARIS
// Solaris's uname() returns a non-negative number on success.
if (uname(&un) >= 0) {
#else
// other UNIX-like systems return a 0 on success.
if (uname(&un) == 0) {
#endif
os.sprintf("%s %s", un.sysname, un.release);
}
}
#endif
if (! os.isNull())
qsCached = os;
else
qsCached = QLatin1String("");
return qsCached;
}
QString OSInfo::getOSDisplayableVersion() {
#if defined(Q_OS_WIN)
QString osdispver;
// Try to query for a Windows 10-style
// displayable version. If this call
// returns a non-empty string, we're
// on Windows 10 or greater and should
// show that string.
osdispver = win10DisplayableVersion();
if (!osdispver.isEmpty()) {
return osdispver;
}
OSVERSIONINFOEXW ovi;
memset(&ovi, 0, sizeof(ovi));
ovi.dwOSVersionInfoSize = sizeof(ovi);
if (!GetVersionEx(reinterpret_cast<OSVERSIONINFOW *>(&ovi))) {
return QString();
}
_SYSTEM_INFO si;
GetNativeSystemInfo(&si);
if (ovi.dwMajorVersion >= 6) {
if (ovi.dwMajorVersion == 10) {
if (ovi.wProductType == VER_NT_WORKSTATION) {
osdispver = QLatin1String("Windows 10");
} else {
osdispver = QLatin1String("Windows 10 Server");
}
} else if (ovi.dwMajorVersion == 6) {
if (ovi.dwMinorVersion == 0) {
if (ovi.wProductType == VER_NT_WORKSTATION) {
osdispver = QLatin1String("Windows Vista");
} else {
osdispver = QLatin1String("Windows Server 2008");
}
} else if (ovi.dwMinorVersion == 1) {
if (ovi.wProductType == VER_NT_WORKSTATION) {
osdispver = QLatin1String("Windows 7");
} else {
osdispver = QLatin1String("Windows Server 2008 R2");
}
} else if (ovi.dwMinorVersion == 2) {
if (ovi.wProductType == VER_NT_WORKSTATION) {
osdispver = QLatin1String("Windows 8");
} else {
osdispver = QLatin1String("Windows Server 2012");
}
} else if (ovi.dwMinorVersion == 3) {
if (ovi.wProductType == VER_NT_WORKSTATION) {
osdispver = QLatin1String("Windows 8.1");
} else {
osdispver = QLatin1String("Windows Server 2012 R2");
}
} else if (ovi.dwMinorVersion == 4) {
if (ovi.wProductType == VER_NT_WORKSTATION) {
osdispver = QLatin1String("Windows 10");
} else {
osdispver = QLatin1String("Windows 10 Server");
}
}
}
typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
PGPI pGetProductInfo = (PGPI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
if (pGetProductInfo == NULL) {
return QString();
}
DWORD dwType = 0;
if (!pGetProductInfo(ovi.dwMajorVersion, ovi.dwMinorVersion, 0, 0, &dwType)) {
return QString();
}
switch(dwType) {
case PRODUCT_ULTIMATE:
osdispver.append(QLatin1String(" Ultimate Edition"));
break;
case PRODUCT_PROFESSIONAL:
osdispver.append(QLatin1String(" Professional"));
break;
case PRODUCT_HOME_PREMIUM:
osdispver.append(QLatin1String(" Home Premium Edition"));
break;
case PRODUCT_HOME_BASIC:
osdispver.append(QLatin1String(" Home Basic Edition"));
break;
case PRODUCT_ENTERPRISE:
osdispver.append(QLatin1String(" Enterprise Edition"));
break;
case PRODUCT_BUSINESS:
osdispver.append(QLatin1String(" Business Edition"));
break;
case PRODUCT_STARTER:
osdispver.append(QLatin1String(" Starter Edition"));
break;
case PRODUCT_CLUSTER_SERVER:
osdispver.append(QLatin1String(" Cluster Server Edition"));
break;
case PRODUCT_DATACENTER_SERVER:
osdispver.append(QLatin1String(" Datacenter Edition"));
break;
case PRODUCT_DATACENTER_SERVER_CORE:
osdispver.append(QLatin1String(" Datacenter Edition (core installation)"));
break;
case PRODUCT_ENTERPRISE_SERVER:
osdispver.append(QLatin1String(" Enterprise Edition"));
break;
case PRODUCT_ENTERPRISE_SERVER_CORE:
osdispver.append(QLatin1String(" Enterprise Edition (core installation)"));
break;
case PRODUCT_ENTERPRISE_SERVER_IA64:
osdispver.append(QLatin1String(" Enterprise Edition for Itanium-based Systems"));
break;
case PRODUCT_SMALLBUSINESS_SERVER:
osdispver.append(QLatin1String(" Small Business Server"));
break;
case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
osdispver.append(QLatin1String(" Small Business Server Premium Edition"));
break;
case PRODUCT_STANDARD_SERVER:
osdispver.append(QLatin1String(" Standard Edition"));
break;
case PRODUCT_STANDARD_SERVER_CORE:
osdispver.append(QLatin1String(" Standard Edition (core installation)"));
break;
case PRODUCT_WEB_SERVER:
osdispver.append(QLatin1String(" Web Server Edition"));
break;
}
} else if (ovi.dwMajorVersion == 5 && ovi.dwMinorVersion == 0) {
osdispver = QLatin1String("Windows 2000");
if (ovi.wProductType == VER_NT_WORKSTATION) {
osdispver.append(QLatin1String(" Professional"));
} else {
if (ovi.wSuiteMask & VER_SUITE_DATACENTER) {
osdispver.append(QLatin1String(" Datacenter Server"));
} else if (ovi.wSuiteMask & VER_SUITE_ENTERPRISE) {
osdispver.append(QLatin1String(" Advanced Server"));
} else {
osdispver.append(QLatin1String(" Server"));
}
}
} else if (ovi.dwMajorVersion == 5 && ovi.dwMinorVersion == 1) {
osdispver = QLatin1String("Windows XP");
if (ovi.wSuiteMask & VER_SUITE_PERSONAL) {
osdispver.append(QLatin1String(" Home Edition"));
} else {
osdispver.append(QLatin1String(" Professional"));
}
} else if (ovi.dwMajorVersion == 5 && ovi.dwMinorVersion == 2) {
if (GetSystemMetrics(SM_SERVERR2)) {
osdispver = QLatin1String("Windows Server 2003 R2");
} else if (ovi.wSuiteMask & VER_SUITE_STORAGE_SERVER) {
osdispver = QLatin1String("Windows Storage Server 2003");
} else if (ovi.wSuiteMask & VER_SUITE_WH_SERVER) {
osdispver = QLatin1String("Windows Home Server");
} else if (ovi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
osdispver = QLatin1String("Windows XP Professional x64 Edition");
} else {
osdispver = QLatin1String("Windows Server 2003");
}
if (ovi.wProductType != VER_NT_WORKSTATION) {
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
if (ovi.wSuiteMask & VER_SUITE_DATACENTER) {
osdispver.append(QLatin1String(" Datacenter x64 Edition"));
} else if (ovi.wSuiteMask & VER_SUITE_ENTERPRISE) {
osdispver.append(QLatin1String(" Enterprise x64 Edition"));
} else {
osdispver.append(QLatin1String(" Standard x64 Edition"));
}
} else {
if (ovi.wSuiteMask & VER_SUITE_COMPUTE_SERVER) {
osdispver.append(QLatin1String(" Compute Cluster Edition"));
} else if (ovi.wSuiteMask & VER_SUITE_DATACENTER) {
osdispver.append(QLatin1String(" Datacenter Edition"));
} else if (ovi.wSuiteMask & VER_SUITE_ENTERPRISE) {
osdispver.append(QLatin1String(" Enterprise Edition"));
} else if (ovi.wSuiteMask & VER_SUITE_BLADE) {
osdispver.append(QLatin1String(" Web Edition"));
} else {
osdispver.append(QLatin1String(" Standard Edition"));
}
}
}
}
// Service Packs (may be empty)
static_assert(sizeof(TCHAR) == sizeof(wchar_t), "Expected Unicode TCHAR.");
QString sp = QString::fromWCharArray(ovi.szCSDVersion);
if (!sp.isEmpty()) {
osdispver.append(QLatin1String(" "));
osdispver.append(sp);
}
// Architecture
if (ovi.dwMajorVersion >= 6) {
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
osdispver.append(QLatin1String(" x64"));
} else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) {
osdispver.append(QLatin1String(" x86"));
}
}
QString osv;
osv.sprintf(" - %lu.%lu.%lu", static_cast<unsigned long>(ovi.dwMajorVersion), static_cast<unsigned long>(ovi.dwMinorVersion), static_cast<unsigned long>(ovi.dwBuildNumber));
osdispver.append(osv);
return osdispver;
#else
return OSInfo::getOSVersion();
#endif
}
void OSInfo::fillXml(QDomDocument &doc, QDomElement &root, const QString &os, const QString &osver, const QList<QHostAddress> &qlBind) {
QDomElement tag;
QDomText t;
bool bIs64;
bool bSSE2 = false;
QString cpu_id, cpu_extid;
tag=doc.createElement(QLatin1String("machash"));
root.appendChild(tag);
t=doc.createTextNode(OSInfo::getMacHash(qlBind));
tag.appendChild(t);
tag=doc.createElement(QLatin1String("version"));
root.appendChild(tag);
t=doc.createTextNode(QLatin1String(MUMTEXT(MUMBLE_VERSION_STRING)));
tag.appendChild(t);
tag=doc.createElement(QLatin1String("release"));
root.appendChild(tag);
t=doc.createTextNode(QLatin1String(MUMBLE_RELEASE));
tag.appendChild(t);
tag=doc.createElement(QLatin1String("os"));
root.appendChild(tag);
t=doc.createTextNode(os);
tag.appendChild(t);
tag=doc.createElement(QLatin1String("osver"));
root.appendChild(tag);
t=doc.createTextNode(osver);
tag.appendChild(t);
tag=doc.createElement(QLatin1String("qt"));
root.appendChild(tag);
t=doc.createTextNode(QString::fromLatin1(qVersion()));
tag.appendChild(t);
#if defined(Q_OS_WIN)
BOOL bIsWow64 = FALSE;
IsWow64Process(GetCurrentProcess(), &bIsWow64);
bIs64 = bIsWow64;
#else
bIs64 = (QSysInfo::WordSize == 64);
#endif
tag=doc.createElement(QLatin1String("is64bit"));
root.appendChild(tag);
t=doc.createTextNode(QString::number(bIs64 ? 1 : 0));
tag.appendChild(t);
#if defined(Q_OS_WIN)
#define regstr(x) QString::fromLatin1(reinterpret_cast<const char *>(& x), 4)
int chop;
int cpuinfo[4];
__cpuid(cpuinfo, 1);
bSSE2 = (cpuinfo[3] & 0x04000000);
__cpuid(cpuinfo, 0);
cpu_id = regstr(cpuinfo[1]) + regstr(cpuinfo[3]) + regstr(cpuinfo[2]);
for (unsigned int j=2; j<=4;++j) {
__cpuid(cpuinfo, 0x80000000 + j);
cpu_extid += regstr(cpuinfo[0]) + regstr(cpuinfo[1]) + regstr(cpuinfo[2]) + regstr(cpuinfo[3]);
}
cpu_id = cpu_id.trimmed();
chop = cpu_id.indexOf(QLatin1Char('\0'));
if (chop != -1)
cpu_id.truncate(chop);
cpu_extid = cpu_extid.trimmed();
chop = cpu_extid.indexOf(QLatin1Char('\0'));
if (chop != -1)
cpu_extid.truncate(chop);
#endif
tag=doc.createElement(QLatin1String("cpu_id"));
root.appendChild(tag);
t=doc.createTextNode(cpu_id);
tag.appendChild(t);
tag=doc.createElement(QLatin1String("cpu_extid"));
root.appendChild(tag);
t=doc.createTextNode(cpu_extid);
tag.appendChild(t);
tag=doc.createElement(QLatin1String("cpu_sse2"));
root.appendChild(tag);
t=doc.createTextNode(QString::number(bSSE2 ? 1 : 0));
tag.appendChild(t);
}
|