IPXGCONN.CPP 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. ** Command & Conquer(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. /* $Header: F:\projects\c&c\vcs\code\ipxgconn.cpv 1.9 16 Oct 1995 16:51:00 JOE_BOSTIC $ */
  19. /***************************************************************************
  20. ** 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 **
  21. ***************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : IPXGCONN.CPP *
  26. * *
  27. * Programmer : Bill Randolph *
  28. * *
  29. * Start Date : December 20, 1994 *
  30. * *
  31. * Last Update : July 6, 1995 [BRR] *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * IPXGlobalConnClass::IPXGlobalConnClass -- class constructor *
  35. * IPXGlobalConnClass::~IPXGlobalConnClass -- class destructor *
  36. * IPXGlobalConnClass::Send_Packet -- adds a packet to the send queue *
  37. * IPXGlobalConnClass::Receive_Packet -- adds packet to the receive queue*
  38. * IPXGlobalConnClass::Get_Packet -- gets a packet from the receive queue*
  39. * IPXGlobalConnClass::Send -- sends a packet *
  40. * IPXGlobalConnClass::Service_Receive_Queue -- services recieve queue *
  41. * IPXGlobalConnClass::Set_Bridge -- Sets up connection to cross a bridge*
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. #include "function.h"
  44. /***************************************************************************
  45. * IPXGlobalConnClass::IPXGlobalConnClass -- class constructor *
  46. * *
  47. * This routine chains to the parent constructor, but it adjusts the size *
  48. * of the packet by the added bytes in the GlobalHeaderType structure. *
  49. * This forces the parent classes to allocate the proper sized PacketBuf *
  50. * for outgoing packets, and to set MaxPacketLen to the proper value. *
  51. * *
  52. * INPUT: *
  53. * numsend desired # of entries for the send queue *
  54. * numreceive desired # of entries for the recieve queue *
  55. * maxlen max length of an application packet *
  56. * product_id unique ID for this product *
  57. * *
  58. * OUTPUT: *
  59. * none. *
  60. * *
  61. * WARNINGS: *
  62. * none. *
  63. * *
  64. * HISTORY: *
  65. * 12/20/1994 BR : Created. *
  66. *=========================================================================*/
  67. IPXGlobalConnClass::IPXGlobalConnClass (int numsend, int numreceive, int maxlen,
  68. unsigned short product_id) :
  69. IPXConnClass (numsend, numreceive,
  70. maxlen + sizeof(GlobalHeaderType) - sizeof(CommHeaderType),
  71. GLOBAL_MAGICNUM, // magic number for this connection
  72. NULL, // IPX Address (none)
  73. 0, // Connection ID
  74. "") // Connection Name
  75. {
  76. ProductID = product_id;
  77. IsBridge = 0;
  78. } /* end of IPXGlobalConnClass */
  79. /***************************************************************************
  80. * IPXGlobalConnClass::Send_Packet -- adds a packet to the send queue *
  81. * *
  82. * This routine prefixes the given buffer with a GlobalHeaderType and *
  83. * queues the resulting packet into the Send Queue. The packet's *
  84. * MagicNumber, Code, PacketID, destination Address and ProductID are set *
  85. * here. *
  86. * *
  87. * INPUT: *
  88. * buf buffer to send *
  89. * buflen length of buffer *
  90. * address address to send the packet to (NULL = Broadcast) *
  91. * ack_req true = ACK is required for this packet; false = isn't *
  92. * *
  93. * OUTPUT: *
  94. * 1 = OK, 0 = error *
  95. * *
  96. * WARNINGS: *
  97. * none. *
  98. * *
  99. * HISTORY: *
  100. * 12/20/1994 BR : Created. *
  101. *=========================================================================*/
  102. int IPXGlobalConnClass::Send_Packet (void * buf, int buflen,
  103. IPXAddressClass *address, int ack_req)
  104. {
  105. /*------------------------------------------------------------------------
  106. Store the packet's Magic Number
  107. ------------------------------------------------------------------------*/
  108. ((GlobalHeaderType *)PacketBuf)->Header.MagicNumber = MagicNum;
  109. /*------------------------------------------------------------------------
  110. If this is a ACK-required packet, sent to a specific system, mark it as
  111. ACK-required; otherwise, mark as no-ACK-required.
  112. ------------------------------------------------------------------------*/
  113. if (ack_req && address != NULL) {
  114. ((GlobalHeaderType *)PacketBuf)->Header.Code = PACKET_DATA_ACK;
  115. } else {
  116. ((GlobalHeaderType *)PacketBuf)->Header.Code = PACKET_DATA_NOACK;
  117. }
  118. /*------------------------------------------------------------------------
  119. Fill in the packet ID. This will have very limited meaning; it only
  120. allows us to determine if an ACK packet we receive later goes with this
  121. packet; it doesn't let us detect re-sends of other systems' packets.
  122. ------------------------------------------------------------------------*/
  123. ((GlobalHeaderType *)PacketBuf)->Header.PacketID = Queue->Send_Total();
  124. /*------------------------------------------------------------------------
  125. Set the product ID for this packet.
  126. ------------------------------------------------------------------------*/
  127. ((GlobalHeaderType *)PacketBuf)->ProductID = ProductID;
  128. /*------------------------------------------------------------------------
  129. Set this packet's destination address. If no address is specified, use
  130. a Broadcast address (which IPXAddressClass's default constructor creates).
  131. ------------------------------------------------------------------------*/
  132. if (address != NULL) {
  133. ((GlobalHeaderType *)PacketBuf)->Address = (*address);
  134. } else {
  135. ((GlobalHeaderType *)PacketBuf)->Address = IPXAddressClass();
  136. }
  137. /*------------------------------------------------------------------------
  138. Copy the application's data
  139. ------------------------------------------------------------------------*/
  140. memcpy(PacketBuf + sizeof(GlobalHeaderType), buf, buflen);
  141. /*------------------------------------------------------------------------
  142. Queue it
  143. ------------------------------------------------------------------------*/
  144. return(Queue->Queue_Send(PacketBuf,buflen + sizeof(GlobalHeaderType)));
  145. } /* end of Send_Packet */
  146. /***************************************************************************
  147. * IPXGlobalConnClass::Receive_Packet -- adds packet to the receive queue *
  148. * *
  149. * INPUT: *
  150. * buf buffer to process (already includes GlobalHeaderType) *
  151. * buflen length of buffer to process *
  152. * address the address of the sender (the IPX Manager class must *
  153. * extract this from the IPX Header of the received packet.) *
  154. * *
  155. * OUTPUT: *
  156. * 1 = OK, 0 = error *
  157. * *
  158. * WARNINGS: *
  159. * none. *
  160. * *
  161. * HISTORY: *
  162. * 12/20/1994 BR : Created. *
  163. *=========================================================================*/
  164. int IPXGlobalConnClass::Receive_Packet (void * buf, int buflen,
  165. IPXAddressClass *address)
  166. {
  167. GlobalHeaderType *packet; // ptr to this packet
  168. SendQueueType *send_entry; // ptr to send entry header
  169. GlobalHeaderType *entry_data; // ptr to queue entry data
  170. int i;
  171. /*
  172. --------------------------- Check the magic # ----------------------------
  173. */
  174. packet = (GlobalHeaderType *)buf;
  175. if (packet->Header.MagicNumber!=MagicNum) {
  176. return(false);
  177. }
  178. /*------------------------------------------------------------------------
  179. Process the packet based on its Code
  180. ------------------------------------------------------------------------*/
  181. switch (packet->Header.Code) {
  182. /*.....................................................................
  183. DATA: Save the given address in the message buffer (so Get_Message()
  184. can extract it later), and queue this message.
  185. Don't bother checking for a Re-Send; since this queue is receiving data
  186. from multiple systems, the Total_Receive() value for this queue will
  187. have nothing to do with the packet's ID. The application must deal
  188. with this by being able to handle multiple receipts of the same packet.
  189. .....................................................................*/
  190. case PACKET_DATA_ACK:
  191. case PACKET_DATA_NOACK:
  192. packet->Address = (*address);
  193. Queue->Queue_Receive (buf, buflen);
  194. break;
  195. /*.....................................................................
  196. ACK: If this ACK is for any of my packets, mark that packet as
  197. acknowledged, then throw this packet away. Otherwise, ignore the ACK
  198. (if we re-sent before we received the other system's first ACK, this
  199. ACK will be a leftover)
  200. .....................................................................*/
  201. case PACKET_ACK:
  202. for (i = 0; i < Queue->Num_Send(); i++) {
  203. /*
  204. ..................... Get queue entry ptr .......................
  205. */
  206. send_entry = Queue->Get_Send(i);
  207. /*
  208. ............. If ptr is valid, get ptr to its data ..............
  209. */
  210. entry_data = (GlobalHeaderType *)(send_entry->Buffer);
  211. /*
  212. .............. If ACK is for this entry, mark it ................
  213. */
  214. if (packet->Header.PacketID==entry_data->Header.PacketID &&
  215. entry_data->Header.Code == PACKET_DATA_ACK) {
  216. send_entry->IsACK = 1;
  217. break;
  218. }
  219. }
  220. break;
  221. /*.....................................................................
  222. Default: ignore the packet
  223. .....................................................................*/
  224. default:
  225. break;
  226. } /* end of switch */
  227. return(true);
  228. }
  229. /***************************************************************************
  230. * IPXGlobalConnClass::Get_Packet -- gets a packet from the receive queue *
  231. * *
  232. * INPUT: *
  233. * buf location to store buffer *
  234. * buflen filled in with length of 'buf' *
  235. * address filled in with sender's address *
  236. * product_id filled in with sender's ProductID *
  237. * *
  238. * OUTPUT: *
  239. * 1 = OK, 0 = error *
  240. * *
  241. * WARNINGS: *
  242. * none. *
  243. * *
  244. * HISTORY: *
  245. * 12/20/1994 BR : Created. *
  246. *=========================================================================*/
  247. int IPXGlobalConnClass::Get_Packet (void * buf, int *buflen,
  248. IPXAddressClass *address, unsigned short *product_id)
  249. {
  250. ReceiveQueueType *rec_entry; // ptr to receive entry header
  251. GlobalHeaderType *packet;
  252. int packetlen; // size of received packet
  253. /*
  254. ------------------------ Return if nothing to do -------------------------
  255. */
  256. if (Queue->Num_Receive() == 0) {
  257. return(false);
  258. }
  259. /*
  260. ------------------ Get ptr to the next available entry -------------------
  261. */
  262. rec_entry = Queue->Get_Receive(0);
  263. /*
  264. ------------------------ Read it if it's un-read -------------------------
  265. */
  266. if (rec_entry!=NULL && rec_entry->IsRead==0) {
  267. /*
  268. ........................... Mark as read ..............................
  269. */
  270. rec_entry->IsRead = 1;
  271. /*
  272. .......................... Copy data packet ...........................
  273. */
  274. packet = (GlobalHeaderType *)(rec_entry->Buffer);
  275. packetlen = rec_entry->BufLen - sizeof(GlobalHeaderType);
  276. if (packetlen > 0)
  277. memcpy(buf, rec_entry->Buffer + sizeof(GlobalHeaderType), packetlen);
  278. (*buflen) = packetlen;
  279. (*address) = packet->Address;
  280. (*product_id) = packet->ProductID;
  281. return(true);
  282. }
  283. return(false);
  284. }
  285. /***************************************************************************
  286. * IPXGlobalConnClass::Send -- sends a packet *
  287. * *
  288. * This routine gets invoked by NonSequencedConn, when it's processing *
  289. * the Send & Receive Queues. The buffer provided will already have the *
  290. * GlobalHeaderType header embedded in it. *
  291. * *
  292. * INPUT: *
  293. * buf buffer to send *
  294. * buflen length of buffer *
  295. * *
  296. * OUTPUT: *
  297. * 1 = OK, 0 = error *
  298. * *
  299. * WARNINGS: *
  300. * none. *
  301. * *
  302. * HISTORY: *
  303. * 12/20/1994 BR : Created. *
  304. *=========================================================================*/
  305. int IPXGlobalConnClass::Send(char *buf, int buflen)
  306. {
  307. IPXAddressClass *addr;
  308. int rc;
  309. /*------------------------------------------------------------------------
  310. Extract the packet's embedded IPX address
  311. ------------------------------------------------------------------------*/
  312. addr = &(((GlobalHeaderType *)buf)->Address);
  313. /*------------------------------------------------------------------------
  314. If it's a broadcast address, broadcast it
  315. ------------------------------------------------------------------------*/
  316. if (addr->Is_Broadcast()) {
  317. return(Broadcast (buf, buflen));
  318. } else {
  319. /*------------------------------------------------------------------------
  320. Otherwise, send it
  321. ------------------------------------------------------------------------*/
  322. if (IsBridge && !memcmp (addr, BridgeNet, 4)) {
  323. rc = Send_To (buf, buflen, &(((GlobalHeaderType *)buf)->Address),
  324. BridgeNode);
  325. } else {
  326. rc = Send_To (buf, buflen, &(((GlobalHeaderType *)buf)->Address), NULL);
  327. }
  328. return (rc);
  329. }
  330. } /* end of Send */
  331. /***************************************************************************
  332. * IPXGlobalConnClass::Service_Receive_Queue -- services the recieve queue *
  333. * *
  334. * This routine is necessary because the Global Connection has to ACK *
  335. * a packet differently from other types of connections; its Send routine *
  336. * assumes that the destination address is embedded within the outgoing *
  337. * packet, so we have to create our ACK Packet using the GlobalHeaderType, *
  338. * not the CommHeaderType. *
  339. * *
  340. * INPUT: *
  341. * none. *
  342. * *
  343. * OUTPUT: *
  344. * 1 = OK, 0 = error *
  345. * *
  346. * WARNINGS: *
  347. * none. *
  348. * *
  349. * HISTORY: *
  350. * 12/20/1994 BR : Created. *
  351. *=========================================================================*/
  352. int IPXGlobalConnClass::Service_Receive_Queue (void)
  353. {
  354. GlobalHeaderType ackpacket; // ACK packet to send
  355. ReceiveQueueType *rec_entry; // ptr to receive entry header
  356. GlobalHeaderType *packet_hdr; // packet header
  357. /*------------------------------------------------------------------------
  358. Get a pointer to the next received entry
  359. ------------------------------------------------------------------------*/
  360. rec_entry = Queue->Get_Receive(0);
  361. if (rec_entry==NULL)
  362. return(1);
  363. /*------------------------------------------------------------------------
  364. If this packet doesn't require an ACK, mark it as ACK'd.
  365. ------------------------------------------------------------------------*/
  366. packet_hdr = (GlobalHeaderType *)(rec_entry->Buffer);
  367. if (packet_hdr->Header.Code==PACKET_DATA_NOACK)
  368. rec_entry->IsACK = 1;
  369. /*------------------------------------------------------------------------
  370. If this packet hasn't been ACK'd, send an ACK:
  371. - Fill in the MagicNum & the Code
  372. - Set the PacketID to the same ID that the sending system used, so the
  373. sending system knows which packet the ACK is for
  374. ------------------------------------------------------------------------*/
  375. if (rec_entry->IsACK==0) {
  376. ackpacket.Header.MagicNumber = MagicNum;
  377. ackpacket.Header.Code = PACKET_ACK;
  378. ackpacket.Header.PacketID = packet_hdr->Header.PacketID;
  379. ackpacket.Address = packet_hdr->Address;
  380. ackpacket.ProductID = ProductID;
  381. Send ((char *)&ackpacket, sizeof(GlobalHeaderType));
  382. rec_entry->IsACK = 1;
  383. }
  384. /*------------------------------------------------------------------------
  385. If this packet has been read by the application, and has been ACK'd, and
  386. there is another packet in the queue behind this one, it means the other
  387. system got the ACK we sent for this packet; remove this packet from the
  388. queue.
  389. ------------------------------------------------------------------------*/
  390. if (rec_entry!=NULL && rec_entry->IsRead && rec_entry->IsACK &&
  391. Queue->Num_Receive() > 1)
  392. Queue->UnQueue_Receive(NULL,NULL,0);
  393. return(1);
  394. } /* end of Service_Receive_Queue */
  395. /***************************************************************************
  396. * Set_Bridge -- Sets up connection to cross a bridge *
  397. * *
  398. * This routine is designed to prevent the connection from having to *
  399. * call Get_Local_Target, except the minimum number of times, since that *
  400. * routine is buggy & goes away for long periods sometimes. *
  401. * *
  402. * INPUT: *
  403. * bridge network number of the destination bridge *
  404. * *
  405. * OUTPUT: *
  406. * none *
  407. * *
  408. * WARNINGS: *
  409. * none *
  410. * *
  411. * HISTORY: *
  412. * 07/06/1995 BRR : Created. *
  413. *=========================================================================*/
  414. void IPXGlobalConnClass::Set_Bridge(NetNumType bridge)
  415. {
  416. if (Configured) {
  417. memcpy (BridgeNet, bridge, 4);
  418. memset (BridgeNode, 0xff, 6);
  419. if (IPX_Get_Local_Target (BridgeNet, BridgeNode, Socket, BridgeNode)==0) {
  420. IsBridge = 1;
  421. } else {
  422. IsBridge = 0;
  423. }
  424. }
  425. }