nicenum.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //
  19. // Filename: nicenum.cpp
  20. // Author: Tom Spencer-Smith
  21. // Date: Dec 1999
  22. // Description:
  23. //
  24. #include "nicenum.h"
  25. #include <winsock.h>
  26. #include <stdio.h>
  27. #include "wwdebug.h"
  28. #include "netutil.h"
  29. #include "useroptions.h"
  30. #include "GameSpy_QnR.h"
  31. //
  32. // Class statics
  33. //
  34. ULONG cNicEnum::NicList[];
  35. USHORT cNicEnum::NumNics = 0;
  36. ULONG cNicEnum::GSNicList[];
  37. USHORT cNicEnum::NumGSNics = 0;
  38. //----------------------------------------------------------------------------------
  39. void
  40. cNicEnum::Init
  41. (
  42. void
  43. )
  44. {
  45. WWDEBUG_SAY(("cNicEnum::Init\n"));
  46. WSADATA wsa_data;
  47. int startup_rc = ::WSAStartup(MAKEWORD(1, 1), &wsa_data);
  48. if (startup_rc != 0)
  49. {
  50. WWDEBUG_SAY((" WSAStartup failed!\n"));
  51. return;
  52. }
  53. //
  54. // Retrieve list of nic's
  55. //
  56. ULONG local_addresses[MAX_NICS];
  57. ULONG num_addresses = Enumerate_Nics(local_addresses, MAX_NICS);
  58. WWASSERT(num_addresses <= MAX_NICS);
  59. WWDEBUG_SAY((" Found %d NIC(s)\n", num_addresses));
  60. USHORT index = 0;
  61. USHORT class_1 = 0;
  62. USHORT class_2 = 0;
  63. //
  64. // First, extract the non-internet addressable nicks, ordered on general usage.
  65. //
  66. ULONG lan_addresses[MAX_NICS];
  67. ULONG num_lan_addresses = 0;
  68. //
  69. // First, scan for 10.*.*.* addresses
  70. //
  71. for (index = 0; index < num_addresses; index++)
  72. {
  73. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  74. if (class_1 == 10)
  75. {
  76. lan_addresses[num_lan_addresses++] = local_addresses[index];
  77. local_addresses[index] = 0;
  78. }
  79. }
  80. //
  81. // Next, scan for 192.168.*.* addresses
  82. //
  83. for (index = 0; index < num_addresses; index++)
  84. {
  85. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  86. class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
  87. if (class_1 == 192 && class_2 == 168)
  88. {
  89. lan_addresses[num_lan_addresses++] = local_addresses[index];
  90. local_addresses[index] = 0;
  91. }
  92. }
  93. //
  94. // Next, scan for 172.16-31.*.* addresses
  95. //
  96. for (index = 0; index < num_addresses; index++)
  97. {
  98. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  99. class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
  100. if (class_1 == 172 && class_2 >= 16 && class_2 <= 31)
  101. {
  102. lan_addresses[num_lan_addresses++] = local_addresses[index];
  103. local_addresses[index] = 0;
  104. }
  105. }
  106. //
  107. // Finally, scan for 169.254.*.* addresses (IP autoconfiguration)
  108. //
  109. for (index = 0; index < num_addresses; index++)
  110. {
  111. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  112. class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
  113. if (class_1 == 169 && class_2 == 254)
  114. {
  115. lan_addresses[num_lan_addresses++] = local_addresses[index];
  116. local_addresses[index] = 0;
  117. }
  118. }
  119. WWDEBUG_SAY((" Of which %d are non-internet addressable.\n", num_lan_addresses));
  120. //
  121. // Next, copy the Internet addressable addresses. Weed out localhost and multicast
  122. // addresses.
  123. //
  124. ULONG internet_addresses[MAX_NICS];
  125. ULONG num_internet_addresses = 0;
  126. for (index = 0; index < num_addresses; index++)
  127. {
  128. if (local_addresses[index] != 0)
  129. {
  130. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  131. if (class_1 != 127 && class_1 != 224)
  132. {
  133. internet_addresses[num_internet_addresses++] = local_addresses[index];
  134. local_addresses[index] = 0;
  135. }
  136. }
  137. }
  138. //
  139. // Now build the LAN and GameSpy NIC lists. They contain the same entries.
  140. // The only difference is that the LAN list puts the non-internet addressable
  141. // NIC's first, the GameSpy list puts them last.
  142. //
  143. NumNics = 0;
  144. NumGSNics = 0;
  145. for (index = 0; index < num_lan_addresses; index++)
  146. {
  147. NicList[NumNics++] = lan_addresses[index];
  148. }
  149. for (index = 0; index < num_internet_addresses; index++)
  150. {
  151. NicList[NumNics++] = internet_addresses[index];
  152. }
  153. for (index = 0; index < num_internet_addresses; index++)
  154. {
  155. GSNicList[NumGSNics++] = internet_addresses[index];
  156. }
  157. for (index = 0; index < num_lan_addresses; index++)
  158. {
  159. GSNicList[NumGSNics++] = lan_addresses[index];
  160. }
  161. WWASSERT(NumNics == NumGSNics);
  162. //
  163. // Initialize or update PreferredLanNic if required.
  164. //
  165. bool is_nic_valid = false;
  166. for (index = 0; index < NumNics; index++)
  167. {
  168. if ((ULONG) cUserOptions::PreferredLanNic.Get() == NicList[index])
  169. {
  170. is_nic_valid = true;
  171. break;
  172. }
  173. }
  174. if (!is_nic_valid)
  175. {
  176. if (NumNics > 0)
  177. {
  178. cUserOptions::PreferredLanNic.Set(NicList[0]);
  179. }
  180. else
  181. {
  182. cUserOptions::PreferredLanNic.Set(0);
  183. }
  184. }
  185. //
  186. // Initialize or update PreferredGameSpyNic if required.
  187. //
  188. is_nic_valid = false;
  189. for (index = 0; index < NumGSNics; index++)
  190. {
  191. if ((ULONG) cUserOptions::PreferredGameSpyNic.Get() == GSNicList[index])
  192. {
  193. is_nic_valid = true;
  194. break;
  195. }
  196. }
  197. if (!is_nic_valid)
  198. {
  199. if (NumGSNics > 0)
  200. {
  201. cUserOptions::PreferredGameSpyNic.Set(GSNicList[0]);
  202. }
  203. else
  204. {
  205. cUserOptions::PreferredGameSpyNic.Set(0);
  206. }
  207. }
  208. WWDEBUG_SAY((" PreferredLanNic is %u (%s)\n",
  209. (ULONG) cUserOptions::PreferredLanNic.Get(),
  210. cNetUtil::Address_To_String(cUserOptions::PreferredLanNic.Get())));
  211. WWDEBUG_SAY((" PreferredGameSpyNic is %u (%s)\n",
  212. (ULONG) cUserOptions::PreferredGameSpyNic.Get(),
  213. cNetUtil::Address_To_String(cUserOptions::PreferredGameSpyNic.Get())));
  214. int cleanup_rc = ::WSACleanup();
  215. WWASSERT(cleanup_rc != SOCKET_ERROR);
  216. }
  217. //---------------------------------------------------------------------------
  218. ULONG
  219. cNicEnum::Enumerate_Nics
  220. (
  221. ULONG * addresses,
  222. ULONG max_nics
  223. )
  224. {
  225. WWASSERT(addresses != NULL);
  226. WWASSERT(max_nics > 0);
  227. WWDEBUG_SAY(("cNicEnum::Enumerate_Nics\n"));
  228. //
  229. // Get the local hostname
  230. //
  231. char local_host_name[300];
  232. int gethostname_rc = ::gethostname(local_host_name, sizeof(local_host_name));
  233. WWASSERT(gethostname_rc != SOCKET_ERROR);
  234. //
  235. // Resolve hostname for local adapter addresses.
  236. // This does a DNS lookup (name resolution)
  237. //
  238. LPHOSTENT p_hostent = ::gethostbyname(local_host_name);
  239. if (p_hostent == NULL)
  240. {
  241. DIE;
  242. }
  243. ULONG num_addresses = 0;
  244. while (num_addresses < max_nics && p_hostent->h_addr_list[num_addresses] != NULL)
  245. {
  246. IN_ADDR in_addr;
  247. ::memcpy(&in_addr, p_hostent->h_addr_list[num_addresses], sizeof(in_addr));
  248. addresses[num_addresses] = in_addr.s_addr;
  249. WWDEBUG_SAY((" NIC: %s\n", cNetUtil::Address_To_String(addresses[num_addresses])));
  250. num_addresses++;
  251. }
  252. return num_addresses;
  253. }
  254. /*
  255. void
  256. cNicEnum::Init
  257. (
  258. void
  259. )
  260. {
  261. WWDEBUG_SAY(("cNicEnum::Init\n"));
  262. WSADATA wsa_data;
  263. int startup_rc = ::WSAStartup(MAKEWORD(1, 1), &wsa_data);
  264. if (startup_rc != 0)
  265. {
  266. WWDEBUG_SAY((" WSAStartup failed!\n"));
  267. return;
  268. }
  269. //
  270. // Retrieve list of nic's
  271. //
  272. ULONG local_addresses[MAX_NICS];
  273. ULONG num_addresses = Enumerate_Nics(local_addresses, MAX_NICS);
  274. WWASSERT(num_addresses <= MAX_NICS);
  275. WWDEBUG_SAY((" Found %d NIC(s)\n", num_addresses));
  276. //
  277. // We are going to discard anything that is internet addressable.
  278. // The non-internet-addressable NIC's are:
  279. // 10.*.*.*
  280. // 192.168.*.*
  281. // 172.16-31.*.*
  282. // We will order these as above to promote the more common choice.
  283. //
  284. USHORT index = 0;
  285. USHORT class_1 = 0;
  286. USHORT class_2 = 0;
  287. NumNics = 0;
  288. NumGSNics = 0;
  289. //
  290. // Create a seperate list of GameSpy compatible NIC's that includes
  291. // local and internet addressable
  292. //
  293. // Don't add the following interfaces to the list.
  294. // 127.* (localhost)
  295. // 224.* (multicast)
  296. //
  297. for (index = 0; index < num_addresses; index++)
  298. {
  299. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  300. if (class_1 != 127 && class_1 != 224)
  301. {
  302. GSNicList[NumGSNics++] = local_addresses[index];
  303. }
  304. }
  305. //
  306. // First, scan for 10.*.*.* addresses
  307. //
  308. for (index = 0; index < num_addresses; index++)
  309. {
  310. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  311. if (class_1 == 10)
  312. {
  313. NicList[NumNics++] = local_addresses[index];
  314. }
  315. }
  316. //
  317. // Next, scan for 192.168.*.* addresses
  318. //
  319. for (index = 0; index < num_addresses; index++)
  320. {
  321. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  322. class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
  323. if (class_1 == 192 && class_2 == 168)
  324. {
  325. NicList[NumNics++] = local_addresses[index];
  326. }
  327. }
  328. //
  329. // Finally, scan for 172.16-31.*.* addresses
  330. //
  331. for (index = 0; index < num_addresses; index++)
  332. {
  333. class_1 = (::ntohl(local_addresses[index]) & 0xff000000) >> 24;
  334. class_2 = (::ntohl(local_addresses[index]) & 0x00ff0000) >> 16;
  335. if (class_1 == 172 && class_2 >= 16 && class_2 <= 31)
  336. {
  337. NicList[NumNics++] = local_addresses[index];
  338. }
  339. }
  340. WWDEBUG_SAY((" Of which %d are non-internet addressable.\n", NumNics));
  341. //
  342. // Initialize or update PreferredLanNic if required.
  343. //
  344. bool is_nic_valid = false;
  345. for (index = 0; index < NumNics; index++)
  346. {
  347. if ((ULONG) cUserOptions::PreferredLanNic.Get() == NicList[index])
  348. {
  349. is_nic_valid = true;
  350. break;
  351. }
  352. }
  353. if (!is_nic_valid)
  354. {
  355. if (NumNics > 0)
  356. {
  357. cUserOptions::PreferredLanNic.Set(NicList[0]);
  358. }
  359. else
  360. {
  361. cUserOptions::PreferredLanNic.Set(0);
  362. }
  363. }
  364. is_nic_valid = false;
  365. for (index = 0; index < NumGSNics; index++)
  366. {
  367. if ((ULONG) cUserOptions::PreferredGameSpyNic.Get() == GSNicList[index])
  368. {
  369. is_nic_valid = true;
  370. break;
  371. }
  372. }
  373. if (!is_nic_valid)
  374. {
  375. if (NumGSNics > 0)
  376. {
  377. cUserOptions::PreferredGameSpyNic.Set(GSNicList[0]);
  378. }
  379. else
  380. {
  381. cUserOptions::PreferredGameSpyNic.Set(0);
  382. }
  383. }
  384. WWDEBUG_SAY((" PreferredLanNic is %u (%s)\n",
  385. (ULONG) cUserOptions::PreferredLanNic.Get(),
  386. cNetUtil::Address_To_String(cUserOptions::PreferredLanNic.Get())));
  387. WWDEBUG_SAY((" PreferredGameSpyNic is %u (%s)\n",
  388. (ULONG) cUserOptions::PreferredGameSpyNic.Get(),
  389. cNetUtil::Address_To_String(cUserOptions::PreferredGameSpyNic.Get())));
  390. int cleanup_rc = ::WSACleanup();
  391. WWASSERT(cleanup_rc != SOCKET_ERROR);
  392. }
  393. */