IPXADDR.CPP 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: F:\projects\c&c\vcs\code\ipxaddr.cpv 2.17 16 Oct 1995 16:50:58 JOE_BOSTIC $ */
  15. /***************************************************************************
  16. ** 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 **
  17. ***************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : IPXADDR.CPP *
  22. * *
  23. * Programmer : Bill Randolph *
  24. * *
  25. * Start Date : December 19, 1994 *
  26. * *
  27. * Last Update : December 19, 1994 [BR] *
  28. * *
  29. *-------------------------------------------------------------------------*
  30. * Functions: *
  31. * IPXAddressClass::IPXAddressClass -- class constructor *
  32. * IPXAddressClass::IPXAddressClass -- class constructor form 2 *
  33. * IPXAddressClass::IPXAddressClass -- class constructor form 3 *
  34. * IPXAddressClass::Set_Address -- sets the IPX address values *
  35. * IPXAddressClass::Set_Address -- sets the IPX values from a header *
  36. * IPXAddressClass::Get_Address -- retrieves the IPX address values *
  37. * IPXAddressClass::Get_Address -- copies address into an IPX header *
  38. * IPXAddressClass::Is_Broadcast -- tells if this is a broadcast address *
  39. * IPXAddressClass::operator== -- overloaded comparison operator *
  40. * IPXAddressClass::operator!= -- overloaded comparison operator *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include "function.h"
  43. #include "tcpip.h"
  44. /***************************************************************************
  45. * IPXAddressClass::IPXAddressClass -- class constructor *
  46. * *
  47. * This default constructor generates a broadcast address. *
  48. * *
  49. * INPUT: *
  50. * net Network Number for this address *
  51. * node Node Address for this address *
  52. * *
  53. * OUTPUT: *
  54. * none. *
  55. * *
  56. * WARNINGS: *
  57. * none. *
  58. * *
  59. * HISTORY: *
  60. * 12/19/1994 BR : Created. *
  61. *=========================================================================*/
  62. IPXAddressClass::IPXAddressClass(void)
  63. {
  64. NetworkNumber[0] = 0xff;
  65. NetworkNumber[1] = 0xff;
  66. NetworkNumber[2] = 0xff;
  67. NetworkNumber[3] = 0xff;
  68. NodeAddress[0] = 0xff;
  69. NodeAddress[1] = 0xff;
  70. NodeAddress[2] = 0xff;
  71. NodeAddress[3] = 0xff;
  72. NodeAddress[4] = 0xff;
  73. NodeAddress[5] = 0xff;
  74. } /* end of IPXAddressClass */
  75. /***************************************************************************
  76. * IPXAddressClass::IPXAddressClass -- class constructor form 2 *
  77. * *
  78. * INPUT: *
  79. * net Network Number for this address *
  80. * node Node Address for this address *
  81. * *
  82. * OUTPUT: *
  83. * none. *
  84. * *
  85. * WARNINGS: *
  86. * none. *
  87. * *
  88. * HISTORY: *
  89. * 12/19/1994 BR : Created. *
  90. *=========================================================================*/
  91. IPXAddressClass::IPXAddressClass(NetNumType net, NetNodeType node)
  92. {
  93. memcpy(NetworkNumber,net,4);
  94. memcpy(NodeAddress,node,6);
  95. } /* end of IPXAddressClass */
  96. /***************************************************************************
  97. * IPXAddressClass::IPXAddressClass -- class constructor form 3 *
  98. * *
  99. * This form of the constructor takes an IPX header as an argument. It *
  100. * extracts the address from the Source address fields in the header. *
  101. * *
  102. * INPUT: *
  103. * header Header from which to extract the address *
  104. * *
  105. * OUTPUT: *
  106. * none. *
  107. * *
  108. * WARNINGS: *
  109. * none. *
  110. * *
  111. * HISTORY: *
  112. * 12/19/1994 BR : Created. *
  113. *=========================================================================*/
  114. IPXAddressClass::IPXAddressClass(IPXHeaderType *header)
  115. {
  116. memcpy(NetworkNumber,header->SourceNetworkNumber,4);
  117. memcpy(NodeAddress,header->SourceNetworkNode,6);
  118. } /* end of IPXAddressClass */
  119. /***************************************************************************
  120. * IPXAddressClass::Set_Address -- sets the IPX address values *
  121. * *
  122. * INPUT: *
  123. * net Network Number for this address *
  124. * node Node Address for this address *
  125. * *
  126. * OUTPUT: *
  127. * none. *
  128. * *
  129. * WARNINGS: *
  130. * none. *
  131. * *
  132. * HISTORY: *
  133. * 12/19/1994 BR : Created. *
  134. *=========================================================================*/
  135. void IPXAddressClass::Set_Address(NetNumType net, NetNodeType node)
  136. {
  137. memcpy(NetworkNumber,net,4);
  138. memcpy(NodeAddress,node,6);
  139. } /* end of Set_Address */
  140. /***************************************************************************
  141. * IPXAddressClass::Set_Address -- sets the IPX values from a header *
  142. * *
  143. * This routine extracts the source addresses from the given IPX header. *
  144. * *
  145. * INPUT: *
  146. * net Network Number for this address *
  147. * node Node Address for this address *
  148. * *
  149. * OUTPUT: *
  150. * none. *
  151. * *
  152. * WARNINGS: *
  153. * none. *
  154. * *
  155. * HISTORY: *
  156. * 12/19/1994 BR : Created. *
  157. *=========================================================================*/
  158. void IPXAddressClass::Set_Address(IPXHeaderType *header)
  159. {
  160. #ifdef VIRTUAL_SUBNET_SERVER
  161. if (Winsock.Get_Connected()){
  162. memset(NetworkNumber, 1 ,4);
  163. memset(NodeAddress, 0, 6);
  164. unsigned short target_mask = *(unsigned short*)header;
  165. /*
  166. ** If this is a head to head game (no VSS) --
  167. ** If mask is 0 then this packet was broadcast from the other player
  168. ** Otherwise exclusive or with 3 to get other players mask
  169. */
  170. if (!UseVirtualSubnetServer){
  171. if (target_mask == 0){
  172. target_mask = 1 << PlanetWestwoodIsHost;
  173. }
  174. target_mask ^= 3;
  175. }
  176. *(unsigned short*) &NodeAddress[0] = target_mask;
  177. }else{
  178. memcpy(NetworkNumber,header->SourceNetworkNumber,4);
  179. memcpy(NodeAddress,header->SourceNetworkNode,6);
  180. }
  181. #else //VIRTUAL_SUBNET_SERVER
  182. if (header){
  183. memcpy(NetworkNumber,header->SourceNetworkNumber,4);
  184. memcpy(NodeAddress,header->SourceNetworkNode,6);
  185. }else{
  186. /*
  187. ** Address is meaningless when using winsock
  188. */
  189. memset(NetworkNumber, 1 ,4);
  190. memset(NodeAddress, 1, 6);
  191. }
  192. #endif //VIRTUAL_SUBNET_SERVER
  193. } /* end of Set_Address */
  194. /***************************************************************************
  195. * IPXAddressClass::Get_Address -- retrieves the IPX address values *
  196. * *
  197. * INPUT: *
  198. * net Network Number for this address *
  199. * node Node Address for this address *
  200. * *
  201. * OUTPUT: *
  202. * none. *
  203. * *
  204. * WARNINGS: *
  205. * none. *
  206. * *
  207. * HISTORY: *
  208. * 12/19/1994 BR : Created. *
  209. *=========================================================================*/
  210. void IPXAddressClass::Get_Address(NetNumType net, NetNodeType node)
  211. {
  212. memcpy(net,NetworkNumber,4);
  213. memcpy(node,NodeAddress,6);
  214. } /* end of Get_Address */
  215. /***************************************************************************
  216. * IPXAddressClass::Get_Address -- copies address into an IPX header *
  217. * *
  218. * INPUT: *
  219. * net Network Number for this address *
  220. * node Node Address for this address *
  221. * *
  222. * OUTPUT: *
  223. * none. *
  224. * *
  225. * WARNINGS: *
  226. * none. *
  227. * *
  228. * HISTORY: *
  229. * 12/19/1994 BR : Created. *
  230. *=========================================================================*/
  231. void IPXAddressClass::Get_Address(IPXHeaderType *header)
  232. {
  233. memcpy(header->DestNetworkNumber,NetworkNumber,4);
  234. memcpy(header->DestNetworkNode,NodeAddress,6);
  235. } /* end of Get_Address */
  236. /***************************************************************************
  237. * IPXAddressClass::Is_Broadcast -- tells if this is a broadcast address *
  238. * *
  239. * INPUT: *
  240. * none. *
  241. * *
  242. * OUTPUT: *
  243. * none. *
  244. * *
  245. * WARNINGS: *
  246. * none. *
  247. * *
  248. * HISTORY: *
  249. * 12/19/1994 BR : Created. *
  250. *=========================================================================*/
  251. bool IPXAddressClass::Is_Broadcast(void)
  252. {
  253. if ( NetworkNumber[0] == 0xff &&
  254. NetworkNumber[1] == 0xff &&
  255. NetworkNumber[2] == 0xff &&
  256. NetworkNumber[3] == 0xff &&
  257. NodeAddress[0] == 0xff &&
  258. NodeAddress[1] == 0xff &&
  259. NodeAddress[2] == 0xff &&
  260. NodeAddress[3] == 0xff &&
  261. NodeAddress[4] == 0xff &&
  262. NodeAddress[5] == 0xff) {
  263. return(true);
  264. } else {
  265. return(false);
  266. }
  267. }
  268. /***************************************************************************
  269. * IPXAddressClass::operator== -- overloaded comparison operator *
  270. * *
  271. * Since, if NETX isn't running, the network number on a received packet *
  272. * can be bogus (all 0's), only the node address is used for comparison *
  273. * purposes here. *
  274. * *
  275. * INPUT: *
  276. * addr address to compare to *
  277. * *
  278. * OUTPUT: *
  279. * 0 = not equal, 1 = equal *
  280. * *
  281. * WARNINGS: *
  282. * none. *
  283. * *
  284. * HISTORY: *
  285. * 12/19/1994 BR : Created. *
  286. *=========================================================================*/
  287. int IPXAddressClass::operator == (IPXAddressClass & addr)
  288. {
  289. /*------------------------------------------------------------------------
  290. If either Network Number is all 0's (which can happen if the system is
  291. not running NETX), compare only the Node Addresses.
  292. ------------------------------------------------------------------------*/
  293. if ( (NetworkNumber[0]==0 &&
  294. NetworkNumber[1]==0 &&
  295. NetworkNumber[2]==0 &&
  296. NetworkNumber[3]==0) ||
  297. (addr.NetworkNumber[0]==0 &&
  298. addr.NetworkNumber[1]==0 &&
  299. addr.NetworkNumber[2]==0 &&
  300. addr.NetworkNumber[3]==0) ) {
  301. if (memcmp(NodeAddress,addr.NodeAddress,6)==0) {
  302. return(true);
  303. } else {
  304. return(false);
  305. }
  306. } else {
  307. /*------------------------------------------------------------------------
  308. Otherwise, compare both the Network Numbers and Node Addresses
  309. ------------------------------------------------------------------------*/
  310. if (memcmp(NodeAddress,addr.NodeAddress,6)==0 && memcmp(NetworkNumber,addr.NetworkNumber,4)==0) {
  311. return(true);
  312. } else {
  313. return(false);
  314. }
  315. }
  316. }
  317. /***************************************************************************
  318. * IPXAddressClass::operator!= -- overloaded comparison operator *
  319. * *
  320. * Since, if NETX isn't running, the network number on a received packet *
  321. * can be bogus (all 0's), only the node address is used for comparison *
  322. * purposes here. *
  323. * *
  324. * INPUT: *
  325. * addr address to compare to *
  326. * *
  327. * OUTPUT: *
  328. * 0 = equal, 1 = not equal *
  329. * *
  330. * WARNINGS: *
  331. * none. *
  332. * *
  333. * HISTORY: *
  334. * 12/19/1994 BR : Created. *
  335. *=========================================================================*/
  336. int IPXAddressClass::operator != (IPXAddressClass & addr)
  337. {
  338. /*------------------------------------------------------------------------
  339. If either Network Number is all 0's (which can happen if the system is
  340. not running NETX), compare only the Node Addresses.
  341. ------------------------------------------------------------------------*/
  342. if ( (NetworkNumber[0]==0 &&
  343. NetworkNumber[1]==0 &&
  344. NetworkNumber[2]==0 &&
  345. NetworkNumber[3]==0) ||
  346. (addr.NetworkNumber[0]==0 &&
  347. addr.NetworkNumber[1]==0 &&
  348. addr.NetworkNumber[2]==0 &&
  349. addr.NetworkNumber[3]==0) ) {
  350. if (memcmp(NodeAddress,addr.NodeAddress,6)==0) {
  351. return(false);
  352. } else {
  353. return(true);
  354. }
  355. } else {
  356. /*------------------------------------------------------------------------
  357. Otherwise, compare both the Network Numbers and Node Addresses
  358. ------------------------------------------------------------------------*/
  359. if (memcmp(NodeAddress,addr.NodeAddress,6)==0 && memcmp(NetworkNumber,addr.NetworkNumber,4)==0) {
  360. return(false);
  361. } else {
  362. return(true);
  363. }
  364. }
  365. }
  366. /***************************************************************************
  367. * IPXAddressClass::operator > -- overloaded comparison operator *
  368. * *
  369. * INPUT: *
  370. * addr address to compare to *
  371. * *
  372. * OUTPUT: *
  373. * TRUE = greater, FALSE = not *
  374. * *
  375. * WARNINGS: *
  376. * none. *
  377. * *
  378. * HISTORY: *
  379. * 12/19/1994 BR : Created. *
  380. *=========================================================================*/
  381. int IPXAddressClass::operator > (IPXAddressClass & addr)
  382. {
  383. return(memcmp(this, &addr, 10) > 0);
  384. } /* end of operator != */
  385. /***************************************************************************
  386. * IPXAddressClass::operator < -- overloaded comparison operator *
  387. * *
  388. * INPUT: *
  389. * addr address to compare to *
  390. * *
  391. * OUTPUT: *
  392. * TRUE = less, FALSE = not *
  393. * *
  394. * WARNINGS: *
  395. * none. *
  396. * *
  397. * HISTORY: *
  398. * 12/19/1994 BR : Created. *
  399. *=========================================================================*/
  400. int IPXAddressClass::operator < (IPXAddressClass & addr)
  401. {
  402. return(memcmp(this, &addr, 10) < 0);
  403. } /* end of operator != */
  404. /***************************************************************************
  405. * IPXAddressClass::operator >= -- overloaded comparison operator *
  406. * *
  407. * INPUT: *
  408. * addr address to compare to *
  409. * *
  410. * OUTPUT: *
  411. * TRUE = greater or equal, FALSE = not *
  412. * *
  413. * WARNINGS: *
  414. * none. *
  415. * *
  416. * HISTORY: *
  417. * 12/19/1994 BR : Created. *
  418. *=========================================================================*/
  419. int IPXAddressClass::operator >= (IPXAddressClass & addr)
  420. {
  421. return(memcmp(this, &addr, 10) >= 0);
  422. } /* end of operator != */
  423. /***************************************************************************
  424. * IPXAddressClass::operator <= -- overloaded comparison operator *
  425. * *
  426. * INPUT: *
  427. * addr address to compare to *
  428. * *
  429. * OUTPUT: *
  430. * TRUE = less or equal, FALSE = not *
  431. * *
  432. * WARNINGS: *
  433. * none. *
  434. * *
  435. * HISTORY: *
  436. * 12/19/1994 BR : Created. *
  437. *=========================================================================*/
  438. int IPXAddressClass::operator <= (IPXAddressClass & addr)
  439. {
  440. return(memcmp(this, &addr, 10) <= 0);
  441. } /* end of operator != */