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