IPEnumeration.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  24. #include "GameNetwork/IPEnumeration.h"
  25. IPEnumeration::IPEnumeration( void )
  26. {
  27. m_IPlist = NULL;
  28. m_isWinsockInitialized = false;
  29. }
  30. IPEnumeration::~IPEnumeration( void )
  31. {
  32. if (m_isWinsockInitialized)
  33. {
  34. WSACleanup();
  35. m_isWinsockInitialized = false;
  36. }
  37. EnumeratedIP *ip = m_IPlist;
  38. while (ip)
  39. {
  40. ip = ip->getNext();
  41. m_IPlist->deleteInstance();
  42. m_IPlist = ip;
  43. }
  44. }
  45. EnumeratedIP * IPEnumeration::getAddresses( void )
  46. {
  47. if (m_IPlist)
  48. return m_IPlist;
  49. if (!m_isWinsockInitialized)
  50. {
  51. WORD verReq = MAKEWORD(2, 2);
  52. WSADATA wsadata;
  53. int err = WSAStartup(verReq, &wsadata);
  54. if (err != 0) {
  55. return NULL;
  56. }
  57. if ((LOBYTE(wsadata.wVersion) != 2) || (HIBYTE(wsadata.wVersion) !=2)) {
  58. WSACleanup();
  59. return NULL;
  60. }
  61. m_isWinsockInitialized = true;
  62. }
  63. // get the local machine's host name
  64. char hostname[256];
  65. if (gethostname(hostname, sizeof(hostname)))
  66. {
  67. DEBUG_LOG(("Failed call to gethostname; WSAGetLastError returned %d\n", WSAGetLastError()));
  68. return NULL;
  69. }
  70. DEBUG_LOG(("Hostname is '%s'\n", hostname));
  71. // get host information from the host name
  72. HOSTENT* hostEnt = gethostbyname(hostname);
  73. if (hostEnt == NULL)
  74. {
  75. DEBUG_LOG(("Failed call to gethostnyname; WSAGetLastError returned %d\n", WSAGetLastError()));
  76. return NULL;
  77. }
  78. // sanity-check the length of the IP adress
  79. if (hostEnt->h_length != 4)
  80. {
  81. DEBUG_LOG(("gethostbyname returns oddly-sized IP addresses!\n"));
  82. return NULL;
  83. }
  84. // construct a list of addresses
  85. int numAddresses = 0;
  86. char *entry;
  87. while ( (entry = hostEnt->h_addr_list[numAddresses++]) != 0 )
  88. {
  89. EnumeratedIP *newIP = newInstance(EnumeratedIP);
  90. AsciiString str;
  91. str.format("%d.%d.%d.%d", (unsigned char)entry[0], (unsigned char)entry[1], (unsigned char)entry[2], (unsigned char)entry[3]);
  92. UnsignedInt testIP = *((UnsignedInt *)entry);
  93. UnsignedInt ip = ntohl(testIP);
  94. /*
  95. ip = *entry++;
  96. ip <<= 8;
  97. ip += *entry++;
  98. ip <<= 8;
  99. ip += *entry++;
  100. ip <<= 8;
  101. ip += *entry++;
  102. */
  103. newIP->setIPstring(str);
  104. newIP->setIP(ip);
  105. DEBUG_LOG(("IP: 0x%8.8X / 0x%8.8X (%s)\n", testIP, ip, str.str()));
  106. // Add the IP to the list in ascending order
  107. if (!m_IPlist)
  108. {
  109. m_IPlist = newIP;
  110. newIP->setNext(NULL);
  111. }
  112. else
  113. {
  114. if (newIP->getIP() < m_IPlist->getIP())
  115. {
  116. newIP->setNext(m_IPlist);
  117. m_IPlist = newIP;
  118. }
  119. else
  120. {
  121. EnumeratedIP *p = m_IPlist;
  122. while (p->getNext() && p->getNext()->getIP() < newIP->getIP())
  123. {
  124. p = p->getNext();
  125. }
  126. newIP->setNext(p->getNext());
  127. p->setNext(newIP);
  128. }
  129. }
  130. }
  131. return m_IPlist;
  132. }
  133. AsciiString IPEnumeration::getMachineName( void )
  134. {
  135. if (!m_isWinsockInitialized)
  136. {
  137. WORD verReq = MAKEWORD(2, 2);
  138. WSADATA wsadata;
  139. int err = WSAStartup(verReq, &wsadata);
  140. if (err != 0) {
  141. return NULL;
  142. }
  143. if ((LOBYTE(wsadata.wVersion) != 2) || (HIBYTE(wsadata.wVersion) !=2)) {
  144. WSACleanup();
  145. return NULL;
  146. }
  147. m_isWinsockInitialized = true;
  148. }
  149. // get the local machine's host name
  150. char hostname[256];
  151. if (gethostname(hostname, sizeof(hostname)))
  152. {
  153. DEBUG_LOG(("Failed call to gethostname; WSAGetLastError returned %d\n", WSAGetLastError()));
  154. return NULL;
  155. }
  156. return AsciiString(hostname);
  157. }