forked from w3dhub/OpenW3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnicenum.cpp
More file actions
488 lines (393 loc) · 10.3 KB
/
nicenum.cpp
File metadata and controls
488 lines (393 loc) · 10.3 KB
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
/*
** Command & Conquer Renegade(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//
// Filename: nicenum.cpp
// Author: Tom Spencer-Smith
// Date: Dec 1999
// Description:
//
#include "nicenum.h"
#include "network-typedefs.h"
#include <stdio.h>
#include "wwdebug.h"
#include "netutil.h"
#include "useroptions.h"
#include "GameSpy_QnR.h"
//
// Class statics
//
ULONG cNicEnum::NicList[];
USHORT cNicEnum::NumNics = 0;
ULONG cNicEnum::GSNicList[];
USHORT cNicEnum::NumGSNics = 0;
//----------------------------------------------------------------------------------
void
cNicEnum::Init
(
void
)
{
WWDEBUG_SAY(("cNicEnum::Init\n"));
WSADATA wsa_data;
int startup_rc = ::WSAStartup(MAKEWORD(1, 1), &wsa_data);
if (startup_rc != 0)
{
WWDEBUG_SAY((" WSAStartup failed!\n"));
return;
}
//
// Retrieve list of nic's
//
ULONG local_addresses[MAX_NICS];
ULONG num_addresses = Enumerate_Nics(local_addresses, MAX_NICS);
WWASSERT(num_addresses <= MAX_NICS);
WWDEBUG_SAY((" Found %d NIC(s)\n", num_addresses));
USHORT index = 0;
USHORT class_1 = 0;
USHORT class_2 = 0;
//
// First, extract the non-internet addressable nicks, ordered on general usage.
//
ULONG lan_addresses[MAX_NICS];
ULONG num_lan_addresses = 0;
//
// First, scan for 10.*.*.* addresses
//
for (index = 0; index < num_addresses; index++)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
if (class_1 == 10)
{
lan_addresses[num_lan_addresses++] = local_addresses[index];
local_addresses[index] = 0;
}
}
//
// Next, scan for 192.168.*.* addresses
//
for (index = 0; index < num_addresses; index++)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
if (class_1 == 192 && class_2 == 168)
{
lan_addresses[num_lan_addresses++] = local_addresses[index];
local_addresses[index] = 0;
}
}
//
// Next, scan for 172.16-31.*.* addresses
//
for (index = 0; index < num_addresses; index++)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
if (class_1 == 172 && class_2 >= 16 && class_2 <= 31)
{
lan_addresses[num_lan_addresses++] = local_addresses[index];
local_addresses[index] = 0;
}
}
//
// Finally, scan for 169.254.*.* addresses (IP autoconfiguration)
//
for (index = 0; index < num_addresses; index++)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
if (class_1 == 169 && class_2 == 254)
{
lan_addresses[num_lan_addresses++] = local_addresses[index];
local_addresses[index] = 0;
}
}
WWDEBUG_SAY((" Of which %d are non-internet addressable.\n", num_lan_addresses));
//
// Next, copy the Internet addressable addresses. Weed out localhost and multicast
// addresses.
//
ULONG internet_addresses[MAX_NICS];
ULONG num_internet_addresses = 0;
for (index = 0; index < num_addresses; index++)
{
if (local_addresses[index] != 0)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
if (class_1 != 127 && class_1 != 224)
{
internet_addresses[num_internet_addresses++] = local_addresses[index];
local_addresses[index] = 0;
}
}
}
//
// Now build the LAN and GameSpy NIC lists. They contain the same entries.
// The only difference is that the LAN list puts the non-internet addressable
// NIC's first, the GameSpy list puts them last.
//
NumNics = 0;
NumGSNics = 0;
for (index = 0; index < num_lan_addresses; index++)
{
NicList[NumNics++] = lan_addresses[index];
}
for (index = 0; index < num_internet_addresses; index++)
{
NicList[NumNics++] = internet_addresses[index];
}
for (index = 0; index < num_internet_addresses; index++)
{
GSNicList[NumGSNics++] = internet_addresses[index];
}
for (index = 0; index < num_lan_addresses; index++)
{
GSNicList[NumGSNics++] = lan_addresses[index];
}
WWASSERT(NumNics == NumGSNics);
//
// Initialize or update PreferredLanNic if required.
//
bool is_nic_valid = false;
for (index = 0; index < NumNics; index++)
{
if ((ULONG) cUserOptions::PreferredLanNic.Get() == NicList[index])
{
is_nic_valid = true;
break;
}
}
if (!is_nic_valid)
{
if (NumNics > 0)
{
cUserOptions::PreferredLanNic.Set(NicList[0]);
}
else
{
cUserOptions::PreferredLanNic.Set(0);
}
}
//
// Initialize or update PreferredGameSpyNic if required.
//
is_nic_valid = false;
for (index = 0; index < NumGSNics; index++)
{
if ((ULONG) cUserOptions::PreferredGameSpyNic.Get() == GSNicList[index])
{
is_nic_valid = true;
break;
}
}
if (!is_nic_valid)
{
if (NumGSNics > 0)
{
cUserOptions::PreferredGameSpyNic.Set(GSNicList[0]);
}
else
{
cUserOptions::PreferredGameSpyNic.Set(0);
}
}
WWDEBUG_SAY((" PreferredLanNic is %u (%s)\n",
(ULONG) cUserOptions::PreferredLanNic.Get(),
cNetUtil::Address_To_String(cUserOptions::PreferredLanNic.Get())));
WWDEBUG_SAY((" PreferredGameSpyNic is %u (%s)\n",
(ULONG) cUserOptions::PreferredGameSpyNic.Get(),
cNetUtil::Address_To_String(cUserOptions::PreferredGameSpyNic.Get())));
int cleanup_rc = ::WSACleanup();
WWASSERT(cleanup_rc != SOCKET_ERROR);
}
//---------------------------------------------------------------------------
ULONG
cNicEnum::Enumerate_Nics
(
ULONG * addresses,
ULONG max_nics
)
{
WWASSERT(addresses != NULL);
WWASSERT(max_nics > 0);
WWDEBUG_SAY(("cNicEnum::Enumerate_Nics\n"));
//
// Get the local hostname
//
char local_host_name[300];
int gethostname_rc = ::gethostname(local_host_name, sizeof(local_host_name));
WWASSERT(gethostname_rc != SOCKET_ERROR);
//
// Resolve hostname for local adapter addresses.
// This does a DNS lookup (name resolution)
//
LPHOSTENT p_hostent = ::gethostbyname(local_host_name);
if (p_hostent == NULL)
{
DIE;
}
ULONG num_addresses = 0;
while (num_addresses < max_nics && p_hostent->h_addr_list[num_addresses] != NULL)
{
IN_ADDR in_addr;
::memcpy(&in_addr, p_hostent->h_addr_list[num_addresses], sizeof(in_addr));
addresses[num_addresses] = in_addr.s_addr;
WWDEBUG_SAY((" NIC: %s\n", cNetUtil::Address_To_String(addresses[num_addresses])));
num_addresses++;
}
return num_addresses;
}
/*
void
cNicEnum::Init
(
void
)
{
WWDEBUG_SAY(("cNicEnum::Init\n"));
WSADATA wsa_data;
int startup_rc = ::WSAStartup(MAKEWORD(1, 1), &wsa_data);
if (startup_rc != 0)
{
WWDEBUG_SAY((" WSAStartup failed!\n"));
return;
}
//
// Retrieve list of nic's
//
ULONG local_addresses[MAX_NICS];
ULONG num_addresses = Enumerate_Nics(local_addresses, MAX_NICS);
WWASSERT(num_addresses <= MAX_NICS);
WWDEBUG_SAY((" Found %d NIC(s)\n", num_addresses));
//
// We are going to discard anything that is internet addressable.
// The non-internet-addressable NIC's are:
// 10.*.*.*
// 192.168.*.*
// 172.16-31.*.*
// We will order these as above to promote the more common choice.
//
USHORT index = 0;
USHORT class_1 = 0;
USHORT class_2 = 0;
NumNics = 0;
NumGSNics = 0;
//
// Create a seperate list of GameSpy compatible NIC's that includes
// local and internet addressable
//
// Don't add the following interfaces to the list.
// 127.* (localhost)
// 224.* (multicast)
//
for (index = 0; index < num_addresses; index++)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
if (class_1 != 127 && class_1 != 224)
{
GSNicList[NumGSNics++] = local_addresses[index];
}
}
//
// First, scan for 10.*.*.* addresses
//
for (index = 0; index < num_addresses; index++)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
if (class_1 == 10)
{
NicList[NumNics++] = local_addresses[index];
}
}
//
// Next, scan for 192.168.*.* addresses
//
for (index = 0; index < num_addresses; index++)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
if (class_1 == 192 && class_2 == 168)
{
NicList[NumNics++] = local_addresses[index];
}
}
//
// Finally, scan for 172.16-31.*.* addresses
//
for (index = 0; index < num_addresses; index++)
{
class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
if (class_1 == 172 && class_2 >= 16 && class_2 <= 31)
{
NicList[NumNics++] = local_addresses[index];
}
}
WWDEBUG_SAY((" Of which %d are non-internet addressable.\n", NumNics));
//
// Initialize or update PreferredLanNic if required.
//
bool is_nic_valid = false;
for (index = 0; index < NumNics; index++)
{
if ((ULONG) cUserOptions::PreferredLanNic.Get() == NicList[index])
{
is_nic_valid = true;
break;
}
}
if (!is_nic_valid)
{
if (NumNics > 0)
{
cUserOptions::PreferredLanNic.Set(NicList[0]);
}
else
{
cUserOptions::PreferredLanNic.Set(0);
}
}
is_nic_valid = false;
for (index = 0; index < NumGSNics; index++)
{
if ((ULONG) cUserOptions::PreferredGameSpyNic.Get() == GSNicList[index])
{
is_nic_valid = true;
break;
}
}
if (!is_nic_valid)
{
if (NumGSNics > 0)
{
cUserOptions::PreferredGameSpyNic.Set(GSNicList[0]);
}
else
{
cUserOptions::PreferredGameSpyNic.Set(0);
}
}
WWDEBUG_SAY((" PreferredLanNic is %u (%s)\n",
(ULONG) cUserOptions::PreferredLanNic.Get(),
cNetUtil::Address_To_String(cUserOptions::PreferredLanNic.Get())));
WWDEBUG_SAY((" PreferredGameSpyNic is %u (%s)\n",
(ULONG) cUserOptions::PreferredGameSpyNic.Get(),
cNetUtil::Address_To_String(cUserOptions::PreferredGameSpyNic.Get())));
int cleanup_rc = ::WSACleanup();
WWASSERT(cleanup_rc != SOCKET_ERROR);
}
*/