| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- /*
- ** 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 <winsock.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);
- }
- */
|