net_chan.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "qcommon.h"
  16. /*
  17. packet header
  18. -------------
  19. 31 sequence
  20. 1 does this message contain a reliable payload
  21. 31 acknowledge sequence
  22. 1 acknowledge receipt of even/odd message
  23. 16 qport
  24. The remote connection never knows if it missed a reliable message, the
  25. local side detects that it has been dropped by seeing a sequence acknowledge
  26. higher thatn the last reliable sequence, but without the correct evon/odd
  27. bit for the reliable set.
  28. If the sender notices that a reliable message has been dropped, it will be
  29. retransmitted. It will not be retransmitted again until a message after
  30. the retransmit has been acknowledged and the reliable still failed to get there.
  31. if the sequence number is -1, the packet should be handled without a netcon
  32. The reliable message can be added to at any time by doing
  33. MSG_Write* (&netchan->message, <data>).
  34. If the message buffer is overflowed, either by a single message, or by
  35. multiple frames worth piling up while the last reliable transmit goes
  36. unacknowledged, the netchan signals a fatal error.
  37. Reliable messages are always placed first in a packet, then the unreliable
  38. message is included if there is sufficient room.
  39. To the receiver, there is no distinction between the reliable and unreliable
  40. parts of the message, they are just processed out as a single larger message.
  41. Illogical packet sequence numbers cause the packet to be dropped, but do
  42. not kill the connection. This, combined with the tight window of valid
  43. reliable acknowledgement numbers provides protection against malicious
  44. address spoofing.
  45. The qport field is a workaround for bad address translating routers that
  46. sometimes remap the client's source port on a packet during gameplay.
  47. If the base part of the net address matches and the qport matches, then the
  48. channel matches even if the IP port differs. The IP port should be updated
  49. to the new value before sending out any replies.
  50. If there is no information that needs to be transfered on a given frame,
  51. such as during the connection stage while waiting for the client to load,
  52. then a packet only needs to be delivered if there is something in the
  53. unacknowledged reliable
  54. */
  55. cvar_t *showpackets;
  56. cvar_t *showdrop;
  57. cvar_t *qport;
  58. netadr_t net_from;
  59. sizebuf_t net_message;
  60. byte net_message_buffer[MAX_MSGLEN];
  61. /*
  62. ===============
  63. Netchan_Init
  64. ===============
  65. */
  66. void Netchan_Init (void)
  67. {
  68. int port;
  69. // pick a port value that should be nice and random
  70. port = Sys_Milliseconds() & 0xffff;
  71. showpackets = Cvar_Get ("showpackets", "0", 0);
  72. showdrop = Cvar_Get ("showdrop", "0", 0);
  73. qport = Cvar_Get ("qport", va("%i", port), CVAR_NOSET);
  74. }
  75. /*
  76. ===============
  77. Netchan_OutOfBand
  78. Sends an out-of-band datagram
  79. ================
  80. */
  81. void Netchan_OutOfBand (int net_socket, netadr_t adr, int length, byte *data)
  82. {
  83. sizebuf_t send;
  84. byte send_buf[MAX_MSGLEN];
  85. // write the packet header
  86. SZ_Init (&send, send_buf, sizeof(send_buf));
  87. MSG_WriteLong (&send, -1); // -1 sequence means out of band
  88. SZ_Write (&send, data, length);
  89. // send the datagram
  90. NET_SendPacket (net_socket, send.cursize, send.data, adr);
  91. }
  92. /*
  93. ===============
  94. Netchan_OutOfBandPrint
  95. Sends a text message in an out-of-band datagram
  96. ================
  97. */
  98. void Netchan_OutOfBandPrint (int net_socket, netadr_t adr, char *format, ...)
  99. {
  100. va_list argptr;
  101. static char string[MAX_MSGLEN - 4];
  102. va_start (argptr, format);
  103. vsprintf (string, format,argptr);
  104. va_end (argptr);
  105. Netchan_OutOfBand (net_socket, adr, strlen(string), (byte *)string);
  106. }
  107. /*
  108. ==============
  109. Netchan_Setup
  110. called to open a channel to a remote system
  111. ==============
  112. */
  113. void Netchan_Setup (netsrc_t sock, netchan_t *chan, netadr_t adr, int qport)
  114. {
  115. memset (chan, 0, sizeof(*chan));
  116. chan->sock = sock;
  117. chan->remote_address = adr;
  118. chan->qport = qport;
  119. chan->last_received = curtime;
  120. chan->incoming_sequence = 0;
  121. chan->outgoing_sequence = 1;
  122. SZ_Init (&chan->message, chan->message_buf, sizeof(chan->message_buf));
  123. chan->message.allowoverflow = true;
  124. }
  125. /*
  126. ===============
  127. Netchan_CanReliable
  128. Returns true if the last reliable message has acked
  129. ================
  130. */
  131. qboolean Netchan_CanReliable (netchan_t *chan)
  132. {
  133. if (chan->reliable_length)
  134. return false; // waiting for ack
  135. return true;
  136. }
  137. qboolean Netchan_NeedReliable (netchan_t *chan)
  138. {
  139. qboolean send_reliable;
  140. // if the remote side dropped the last reliable message, resend it
  141. send_reliable = false;
  142. if (chan->incoming_acknowledged > chan->last_reliable_sequence
  143. && chan->incoming_reliable_acknowledged != chan->reliable_sequence)
  144. send_reliable = true;
  145. // if the reliable transmit buffer is empty, copy the current message out
  146. if (!chan->reliable_length && chan->message.cursize)
  147. {
  148. send_reliable = true;
  149. }
  150. return send_reliable;
  151. }
  152. /*
  153. ===============
  154. Netchan_Transmit
  155. tries to send an unreliable message to a connection, and handles the
  156. transmition / retransmition of the reliable messages.
  157. A 0 length will still generate a packet and deal with the reliable messages.
  158. ================
  159. */
  160. void Netchan_Transmit (netchan_t *chan, int length, byte *data)
  161. {
  162. sizebuf_t send;
  163. byte send_buf[MAX_MSGLEN];
  164. qboolean send_reliable;
  165. unsigned w1, w2;
  166. // check for message overflow
  167. if (chan->message.overflowed)
  168. {
  169. chan->fatal_error = true;
  170. Com_Printf ("%s:Outgoing message overflow\n"
  171. , NET_AdrToString (chan->remote_address));
  172. return;
  173. }
  174. send_reliable = Netchan_NeedReliable (chan);
  175. if (!chan->reliable_length && chan->message.cursize)
  176. {
  177. memcpy (chan->reliable_buf, chan->message_buf, chan->message.cursize);
  178. chan->reliable_length = chan->message.cursize;
  179. chan->message.cursize = 0;
  180. chan->reliable_sequence ^= 1;
  181. }
  182. // write the packet header
  183. SZ_Init (&send, send_buf, sizeof(send_buf));
  184. w1 = ( chan->outgoing_sequence & ~(1<<31) ) | (send_reliable<<31);
  185. w2 = ( chan->incoming_sequence & ~(1<<31) ) | (chan->incoming_reliable_sequence<<31);
  186. chan->outgoing_sequence++;
  187. chan->last_sent = curtime;
  188. MSG_WriteLong (&send, w1);
  189. MSG_WriteLong (&send, w2);
  190. // send the qport if we are a client
  191. if (chan->sock == NS_CLIENT)
  192. MSG_WriteShort (&send, qport->value);
  193. // copy the reliable message to the packet first
  194. if (send_reliable)
  195. {
  196. SZ_Write (&send, chan->reliable_buf, chan->reliable_length);
  197. chan->last_reliable_sequence = chan->outgoing_sequence;
  198. }
  199. // add the unreliable part if space is available
  200. if (send.maxsize - send.cursize >= length)
  201. SZ_Write (&send, data, length);
  202. else
  203. Com_Printf ("Netchan_Transmit: dumped unreliable\n");
  204. // send the datagram
  205. NET_SendPacket (chan->sock, send.cursize, send.data, chan->remote_address);
  206. if (showpackets->value)
  207. {
  208. if (send_reliable)
  209. Com_Printf ("send %4i : s=%i reliable=%i ack=%i rack=%i\n"
  210. , send.cursize
  211. , chan->outgoing_sequence - 1
  212. , chan->reliable_sequence
  213. , chan->incoming_sequence
  214. , chan->incoming_reliable_sequence);
  215. else
  216. Com_Printf ("send %4i : s=%i ack=%i rack=%i\n"
  217. , send.cursize
  218. , chan->outgoing_sequence - 1
  219. , chan->incoming_sequence
  220. , chan->incoming_reliable_sequence);
  221. }
  222. }
  223. /*
  224. =================
  225. Netchan_Process
  226. called when the current net_message is from remote_address
  227. modifies net_message so that it points to the packet payload
  228. =================
  229. */
  230. qboolean Netchan_Process (netchan_t *chan, sizebuf_t *msg)
  231. {
  232. unsigned sequence, sequence_ack;
  233. unsigned reliable_ack, reliable_message;
  234. int qport;
  235. // get sequence numbers
  236. MSG_BeginReading (msg);
  237. sequence = MSG_ReadLong (msg);
  238. sequence_ack = MSG_ReadLong (msg);
  239. // read the qport if we are a server
  240. if (chan->sock == NS_SERVER)
  241. qport = MSG_ReadShort (msg);
  242. reliable_message = sequence >> 31;
  243. reliable_ack = sequence_ack >> 31;
  244. sequence &= ~(1<<31);
  245. sequence_ack &= ~(1<<31);
  246. if (showpackets->value)
  247. {
  248. if (reliable_message)
  249. Com_Printf ("recv %4i : s=%i reliable=%i ack=%i rack=%i\n"
  250. , msg->cursize
  251. , sequence
  252. , chan->incoming_reliable_sequence ^ 1
  253. , sequence_ack
  254. , reliable_ack);
  255. else
  256. Com_Printf ("recv %4i : s=%i ack=%i rack=%i\n"
  257. , msg->cursize
  258. , sequence
  259. , sequence_ack
  260. , reliable_ack);
  261. }
  262. //
  263. // discard stale or duplicated packets
  264. //
  265. if (sequence <= chan->incoming_sequence)
  266. {
  267. if (showdrop->value)
  268. Com_Printf ("%s:Out of order packet %i at %i\n"
  269. , NET_AdrToString (chan->remote_address)
  270. , sequence
  271. , chan->incoming_sequence);
  272. return false;
  273. }
  274. //
  275. // dropped packets don't keep the message from being used
  276. //
  277. chan->dropped = sequence - (chan->incoming_sequence+1);
  278. if (chan->dropped > 0)
  279. {
  280. if (showdrop->value)
  281. Com_Printf ("%s:Dropped %i packets at %i\n"
  282. , NET_AdrToString (chan->remote_address)
  283. , chan->dropped
  284. , sequence);
  285. }
  286. //
  287. // if the current outgoing reliable message has been acknowledged
  288. // clear the buffer to make way for the next
  289. //
  290. if (reliable_ack == chan->reliable_sequence)
  291. chan->reliable_length = 0; // it has been received
  292. //
  293. // if this message contains a reliable message, bump incoming_reliable_sequence
  294. //
  295. chan->incoming_sequence = sequence;
  296. chan->incoming_acknowledged = sequence_ack;
  297. chan->incoming_reliable_acknowledged = reliable_ack;
  298. if (reliable_message)
  299. {
  300. chan->incoming_reliable_sequence ^= 1;
  301. }
  302. //
  303. // the message can now be read from the current message pointer
  304. //
  305. chan->last_received = curtime;
  306. return true;
  307. }