nataddr.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/nataddr.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 9/17/01 3:55p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * IPAddressClass::IPAddressClass -- Class constructor *
  35. * IPAddressClass::IPAddressClass -- Class constructor *
  36. * IPAddressClass::IPAddressClass -- Class constructor *
  37. * IPAddressClass::Set_Address -- Sets a valid IP address into the class *
  38. * IPAddressClass::Set_Address -- Sets a valid IP address into the class *
  39. * IPAddressClass::Get_Address -- Get the address value of this class *
  40. * IPAddressClass::Get_Address -- Get the address value of this class *
  41. * IPAddressClass::Is_Broadcast -- Does this class represent a broadcast address? *
  42. * IPAddressClass::As_String -- Get address in human readable form *
  43. * IPAddressClass::operator == - Equality operator for the class *
  44. * IPAddressClass::operator != - Non-equality operator for the class *
  45. * IPAddressClass::operator = Assignment operator *
  46. * IPXAddressClass::As_String -- Get address in human readable form *
  47. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  48. #include "always.h"
  49. #include "assert.h"
  50. #include "nataddr.h"
  51. #include <stdio.h>
  52. #include <string.h>
  53. /***********************************************************************************************
  54. * IPAddressClass::IPAddressClass -- Class constructor *
  55. * *
  56. * *
  57. * *
  58. * INPUT: Nothing *
  59. * *
  60. * OUTPUT: Nothing *
  61. * *
  62. * WARNINGS: None *
  63. * *
  64. * HISTORY: *
  65. * 3/9/00 12:58PM ST : Created *
  66. *=============================================================================================*/
  67. IPAddressClass::IPAddressClass(void)
  68. {
  69. WholeAddress = 0x00000000;
  70. Port = 0;
  71. IsValid = false;
  72. }
  73. /***********************************************************************************************
  74. * IPAddressClass::IPAddressClass -- Class constructor *
  75. * *
  76. * *
  77. * *
  78. * INPUT: Ptr to valid IP address *
  79. * *
  80. * OUTPUT: Nothing *
  81. * *
  82. * WARNINGS: None *
  83. * *
  84. * HISTORY: *
  85. * 3/9/00 12:59PM ST : Created *
  86. *=============================================================================================*/
  87. IPAddressClass::IPAddressClass(unsigned char *address, unsigned short port)
  88. {
  89. WholeAddress = *((unsigned long*)address);
  90. Port = port;
  91. IsValid = true;
  92. }
  93. /***********************************************************************************************
  94. * IPAddressClass::IPAddressClass -- Class constructor *
  95. * *
  96. * *
  97. * *
  98. * INPUT: 32 bit IP address *
  99. * *
  100. * OUTPUT: Nothing *
  101. * *
  102. * WARNINGS: None *
  103. * *
  104. * HISTORY: *
  105. * 3/9/00 1:00PM ST : Created *
  106. *=============================================================================================*/
  107. IPAddressClass::IPAddressClass(unsigned long address, unsigned short port)
  108. {
  109. WholeAddress = address;
  110. Port = port;
  111. IsValid = true;
  112. }
  113. /***********************************************************************************************
  114. * IPAddressClass::Set_Address -- Sets a valid IP address into the class *
  115. * *
  116. * *
  117. * *
  118. * INPUT: Ptr to IP address *
  119. * *
  120. * OUTPUT: Nothing *
  121. * *
  122. * WARNINGS: None *
  123. * *
  124. * HISTORY: *
  125. * 3/9/00 1:01PM ST : Created *
  126. *=============================================================================================*/
  127. void IPAddressClass::Set_Address(unsigned char *address, unsigned short port)
  128. {
  129. WholeAddress = *((unsigned long*)address);
  130. Port = port;
  131. IsValid = true;
  132. }
  133. /***********************************************************************************************
  134. * IPAddressClass::Set_Address -- Sets a valid IP address into the class *
  135. * *
  136. * *
  137. * *
  138. * INPUT: 32 bit IP address *
  139. * *
  140. * OUTPUT: Nothing *
  141. * *
  142. * WARNINGS: None *
  143. * *
  144. * HISTORY: *
  145. * 3/9/00 1:01PM ST : Created *
  146. *=============================================================================================*/
  147. void IPAddressClass::Set_Address(unsigned long address, unsigned short port)
  148. {
  149. WholeAddress = address;
  150. Port = port;
  151. IsValid = true;
  152. }
  153. /***********************************************************************************************
  154. * IPAddressClass::Get_Address -- Get the address value of this class *
  155. * *
  156. * *
  157. * *
  158. * INPUT: Ptr to area to store the address into *
  159. * *
  160. * OUTPUT: Nothing *
  161. * *
  162. * WARNINGS: None *
  163. * *
  164. * HISTORY: *
  165. * 3/9/00 1:03PM ST : Created *
  166. *=============================================================================================*/
  167. void IPAddressClass::Get_Address(unsigned char *address, unsigned short *port)
  168. {
  169. fw_assert(IsValid);
  170. *((unsigned long*)address) = WholeAddress;
  171. if (port) {
  172. *port = Port;
  173. }
  174. }
  175. /***********************************************************************************************
  176. * IPAddressClass::Get_Address -- Get the address value of this class *
  177. * *
  178. * *
  179. * *
  180. * INPUT: Nothing *
  181. * *
  182. * OUTPUT: 32 bit IP address *
  183. * *
  184. * WARNINGS: None *
  185. * *
  186. * HISTORY: *
  187. * 3/9/00 1:03PM ST : Created *
  188. *=============================================================================================*/
  189. unsigned long IPAddressClass::Get_Address(void)
  190. {
  191. fw_assert(IsValid);
  192. return (WholeAddress);
  193. }
  194. /***********************************************************************************************
  195. * IPAddressClass::Get_Port -- Get the port number associated with this address *
  196. * *
  197. * *
  198. * *
  199. * INPUT: Nothing *
  200. * *
  201. * OUTPUT: Port number *
  202. * *
  203. * WARNINGS: None *
  204. * *
  205. * HISTORY: *
  206. * 10/24/00 11:51AM ST : Created *
  207. *=============================================================================================*/
  208. unsigned short IPAddressClass::Get_Port(void)
  209. {
  210. fw_assert(IsValid);
  211. return(Port);
  212. }
  213. /***********************************************************************************************
  214. * IPAddressClass::Is_Broadcast -- Does this class represent a broadcast address? *
  215. * *
  216. * *
  217. * *
  218. * INPUT: Nothing *
  219. * *
  220. * OUTPUT: True if broadcast address *
  221. * *
  222. * WARNINGS: None *
  223. * *
  224. * HISTORY: *
  225. * 3/9/00 1:06PM ST : Created *
  226. *=============================================================================================*/
  227. bool IPAddressClass::Is_Broadcast(void)
  228. {
  229. if (IsValid && WholeAddress == 0xffffffff) {
  230. return(true);
  231. }
  232. return(false);
  233. }
  234. /***********************************************************************************************
  235. * IPAddressClass::As_String -- Get address in human readable form *
  236. * *
  237. * *
  238. * *
  239. * INPUT: ptr to address *
  240. * *
  241. * OUTPUT: ptr to string *
  242. * *
  243. * WARNINGS: None *
  244. * *
  245. * HISTORY: *
  246. * 3/9/00 1:07PM ST : Created *
  247. *=============================================================================================*/
  248. char *IPAddressClass::As_String(void)
  249. {
  250. static char _addr_str[128];
  251. sprintf (_addr_str, "%d.%d.%d.%d ; %d", Address[0], Address[1], Address[2], Address[3], (unsigned int)Port);
  252. return (_addr_str);
  253. }
  254. /***********************************************************************************************
  255. * IPAddressClass::Is_IP_Equal -- Compare the IP portion of the address only, ignore the port *
  256. * *
  257. * *
  258. * *
  259. * INPUT: Address to compare to *
  260. * *
  261. * OUTPUT: True if equal *
  262. * *
  263. * WARNINGS: None *
  264. * *
  265. * HISTORY: *
  266. * 10/25/00 2:23PM ST : Created *
  267. *=============================================================================================*/
  268. bool IPAddressClass::Is_IP_Equal(IPAddressClass &address)
  269. {
  270. if (IsValid && address.Is_Valid() && address.Get_Address() == WholeAddress) {
  271. return(true);
  272. }
  273. return(false);
  274. }
  275. /***********************************************************************************************
  276. * IPAddressClass::operator == - Equality operator for the class *
  277. * *
  278. * *
  279. * *
  280. * INPUT: Class to compare to *
  281. * *
  282. * OUTPUT: True if the same *
  283. * *
  284. * WARNINGS: None *
  285. * *
  286. * HISTORY: *
  287. * 3/9/00 1:10PM ST : Created *
  288. *=============================================================================================*/
  289. bool IPAddressClass::operator == (IPAddressClass &address)
  290. {
  291. if (IsValid && address.Is_Valid() && address.Get_Address() == WholeAddress && address.Get_Port() == Port) {
  292. return(true);
  293. }
  294. return(false);
  295. }
  296. /***********************************************************************************************
  297. * IPAddressClass::operator != - Non-equality operator for the class *
  298. * *
  299. * *
  300. * *
  301. * INPUT: Class to compare to *
  302. * *
  303. * OUTPUT: True if not the same *
  304. * *
  305. * WARNINGS: None *
  306. * *
  307. * HISTORY: *
  308. * 3/9/00 1:10PM ST : Created *
  309. *=============================================================================================*/
  310. bool IPAddressClass::operator != (IPAddressClass &address)
  311. {
  312. if (!IsValid || !address.Is_Valid() || address.Get_Address() != WholeAddress || address.Get_Port() != Port) {
  313. return(true);
  314. }
  315. return(false);
  316. }
  317. /***********************************************************************************************
  318. * IPAddressClass::operator == - Equality operator for the class *
  319. * *
  320. * *
  321. * *
  322. * INPUT: Class to compare to *
  323. * *
  324. * OUTPUT: True if the same *
  325. * *
  326. * WARNINGS: None *
  327. * *
  328. * HISTORY: *
  329. * 3/9/00 1:10PM ST : Created *
  330. *=============================================================================================*/
  331. bool IPAddressClass::operator == (IPAddressClass address)
  332. {
  333. if (IsValid && address.Is_Valid() && address.Get_Address() == WholeAddress && address.Get_Port() == Port) {
  334. return(true);
  335. }
  336. return(false);
  337. }
  338. /***********************************************************************************************
  339. * IPAddressClass::operator != - Non-equality operator for the class *
  340. * *
  341. * *
  342. * *
  343. * INPUT: Class to compare to *
  344. * *
  345. * OUTPUT: True if not the same *
  346. * *
  347. * WARNINGS: None *
  348. * *
  349. * HISTORY: *
  350. * 3/9/00 1:10PM ST : Created *
  351. *=============================================================================================*/
  352. bool IPAddressClass::operator != (IPAddressClass address)
  353. {
  354. if (!IsValid || !address.Is_Valid() || address.Get_Address() != WholeAddress || address.Get_Port() != Port) {
  355. return(true);
  356. }
  357. return(false);
  358. }
  359. /***********************************************************************************************
  360. * IPAddressClass::operator = Assignment operator *
  361. * *
  362. * *
  363. * *
  364. * INPUT: Class to store into this class *
  365. * *
  366. * OUTPUT: Nothing *
  367. * *
  368. * WARNINGS: None *
  369. * *
  370. * HISTORY: *
  371. * 3/9/00 1:10PM ST : Created *
  372. *=============================================================================================*/
  373. bool IPAddressClass::operator = (const IPAddressClass &address)
  374. {
  375. IsValid = ((IPAddressClass &)address).Is_Valid();
  376. /*
  377. ** Can't read an invalid address without causing an assert.
  378. */
  379. if (IsValid) {
  380. WholeAddress = ((IPAddressClass &)address).Get_Address();
  381. Port = ((IPAddressClass &)address).Get_Port();
  382. } else {
  383. WholeAddress = 0xffffffff;
  384. Port = 0;
  385. }
  386. return(IsValid);
  387. }