cl_net_chan.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "../game/q_shared.h"
  19. #include "../qcommon/qcommon.h"
  20. #include "client.h"
  21. /*
  22. ==============
  23. CL_Netchan_Encode
  24. // first 12 bytes of the data are always:
  25. long serverId;
  26. long messageAcknowledge;
  27. long reliableAcknowledge;
  28. ==============
  29. */
  30. static void CL_Netchan_Encode( msg_t *msg ) {
  31. int serverId, messageAcknowledge, reliableAcknowledge;
  32. int i, index, srdc, sbit, soob;
  33. byte key, *string;
  34. if ( msg->cursize <= CL_ENCODE_START ) {
  35. return;
  36. }
  37. srdc = msg->readcount;
  38. sbit = msg->bit;
  39. soob = msg->oob;
  40. msg->bit = 0;
  41. msg->readcount = 0;
  42. msg->oob = 0;
  43. serverId = MSG_ReadLong(msg);
  44. messageAcknowledge = MSG_ReadLong(msg);
  45. reliableAcknowledge = MSG_ReadLong(msg);
  46. msg->oob = soob;
  47. msg->bit = sbit;
  48. msg->readcount = srdc;
  49. string = (byte *)clc.serverCommands[ reliableAcknowledge & (MAX_RELIABLE_COMMANDS-1) ];
  50. index = 0;
  51. //
  52. key = clc.challenge ^ serverId ^ messageAcknowledge;
  53. for (i = CL_ENCODE_START; i < msg->cursize; i++) {
  54. // modify the key with the last received now acknowledged server command
  55. if (!string[index])
  56. index = 0;
  57. if (string[index] > 127 || string[index] == '%') {
  58. key ^= '.' << (i & 1);
  59. }
  60. else {
  61. key ^= string[index] << (i & 1);
  62. }
  63. index++;
  64. // encode the data with this key
  65. *(msg->data + i) = (*(msg->data + i)) ^ key;
  66. }
  67. }
  68. /*
  69. ==============
  70. CL_Netchan_Decode
  71. // first four bytes of the data are always:
  72. long reliableAcknowledge;
  73. ==============
  74. */
  75. static void CL_Netchan_Decode( msg_t *msg ) {
  76. long reliableAcknowledge, i, index;
  77. byte key, *string;
  78. int srdc, sbit, soob;
  79. srdc = msg->readcount;
  80. sbit = msg->bit;
  81. soob = msg->oob;
  82. msg->oob = 0;
  83. reliableAcknowledge = MSG_ReadLong(msg);
  84. msg->oob = soob;
  85. msg->bit = sbit;
  86. msg->readcount = srdc;
  87. string = clc.reliableCommands[ reliableAcknowledge & (MAX_RELIABLE_COMMANDS-1) ];
  88. index = 0;
  89. // xor the client challenge with the netchan sequence number (need something that changes every message)
  90. key = clc.challenge ^ LittleLong( *(unsigned *)msg->data );
  91. for (i = msg->readcount + CL_DECODE_START; i < msg->cursize; i++) {
  92. // modify the key with the last sent and with this message acknowledged client command
  93. if (!string[index])
  94. index = 0;
  95. if (string[index] > 127 || string[index] == '%') {
  96. key ^= '.' << (i & 1);
  97. }
  98. else {
  99. key ^= string[index] << (i & 1);
  100. }
  101. index++;
  102. // decode the data with this key
  103. *(msg->data + i) = *(msg->data + i) ^ key;
  104. }
  105. }
  106. /*
  107. =================
  108. CL_Netchan_TransmitNextFragment
  109. =================
  110. */
  111. void CL_Netchan_TransmitNextFragment( netchan_t *chan ) {
  112. Netchan_TransmitNextFragment( chan );
  113. }
  114. /*
  115. ===============
  116. CL_Netchan_Transmit
  117. ================
  118. */
  119. void CL_Netchan_Transmit( netchan_t *chan, msg_t* msg ) {
  120. MSG_WriteByte( msg, clc_EOF );
  121. CL_Netchan_Encode( msg );
  122. Netchan_Transmit( chan, msg->cursize, msg->data );
  123. }
  124. extern int oldsize;
  125. int newsize = 0;
  126. /*
  127. =================
  128. CL_Netchan_Process
  129. =================
  130. */
  131. qboolean CL_Netchan_Process( netchan_t *chan, msg_t *msg ) {
  132. int ret;
  133. ret = Netchan_Process( chan, msg );
  134. if (!ret)
  135. return qfalse;
  136. CL_Netchan_Decode( msg );
  137. newsize += msg->cursize;
  138. return qtrue;
  139. }