… | |
… | |
40 | |
40 | |
41 | #ifdef __linux__ |
41 | #ifdef __linux__ |
42 | # include <sys/types.h> |
42 | # include <sys/types.h> |
43 | # include <sys/socket.h> |
43 | # include <sys/socket.h> |
44 | # include <netinet/in.h> |
44 | # include <netinet/in.h> |
45 | # define TCP_HZ 1000 // sorry... |
|
|
46 | # include <netinet/tcp.h> |
45 | # include <netinet/tcp.h> |
47 | #endif |
46 | #endif |
48 | |
47 | |
49 | // use a really low timeout, as it doesn't cost any bandwidth, and you can |
48 | // use a really low timeout, as it doesn't cost any bandwidth, and you can |
50 | // easily die in 20 seconds... |
49 | // easily die in 20 seconds... |
51 | #define SOCKET_TIMEOUT1 10 |
50 | #define SOCKET_TIMEOUT1 10 * 1000 |
52 | #define SOCKET_TIMEOUT2 20 |
51 | #define SOCKET_TIMEOUT2 20 * 1000 |
53 | |
52 | |
54 | void |
53 | void |
55 | client::flush () |
54 | client::flush () |
56 | { |
55 | { |
57 | #ifdef __linux__ |
56 | #ifdef __linux__ |
… | |
… | |
64 | unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
63 | unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
65 | |
64 | |
66 | rtt = tcpi.tcpi_rtt; |
65 | rtt = tcpi.tcpi_rtt; |
67 | rttvar = tcpi.tcpi_rttvar; |
66 | rttvar = tcpi.tcpi_rttvar; |
68 | |
67 | |
69 | if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s |
68 | if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 < diff && diff < 0x80000000UL // ack delayed for 20s |
70 | && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s |
69 | && SOCKET_TIMEOUT2 < tcpi.tcpi_last_data_sent) // no data sent for 10s |
71 | { |
70 | { |
72 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
71 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
73 | (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); |
72 | (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); |
74 | status = Ns_Dead; |
73 | destroy (); |
75 | } |
74 | } |
76 | } |
75 | } |
77 | #endif |
76 | #endif |
78 | |
77 | |
79 | /** |
78 | /** |
… | |
… | |
111 | #endif |
110 | #endif |
112 | } |
111 | } |
113 | else if (res == 0) |
112 | else if (res == 0) |
114 | { |
113 | { |
115 | LOG (llevError, "socket write failed, connection closed.\n"); |
114 | LOG (llevError, "socket write failed, connection closed.\n"); |
116 | status = Ns_Dead; |
115 | destroy (); |
117 | return; |
116 | return; |
118 | } |
117 | } |
119 | else if (errno == EINTR) |
118 | else if (errno == EINTR) |
120 | { |
119 | { |
121 | // just retry |
120 | // just retry |
… | |
… | |
128 | return; |
127 | return; |
129 | } |
128 | } |
130 | else |
129 | else |
131 | { |
130 | { |
132 | LOG (llevError, "socket write failed: %s\n", strerror (errno)); |
131 | LOG (llevError, "socket write failed: %s\n", strerror (errno)); |
133 | status = Ns_Dead; |
132 | destroy (); |
134 | return; |
133 | return; |
135 | } |
134 | } |
136 | } |
135 | } |
137 | |
136 | |
138 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
137 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
139 | } |
138 | } |
140 | |
139 | |
|
|
140 | /****************************************************************************** |
|
|
141 | * |
|
|
142 | * Start of read routines. |
|
|
143 | * |
|
|
144 | ******************************************************************************/ |
|
|
145 | |
|
|
146 | int |
|
|
147 | client::next_packet () |
|
|
148 | { |
|
|
149 | if (inbuf_len >= 2) |
|
|
150 | { |
|
|
151 | int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
|
|
152 | |
|
|
153 | if (inbuf_len >= 2 + pkt_len) |
|
|
154 | return 2 + pkt_len; |
|
|
155 | |
|
|
156 | if (inbuf_len == sizeof (inbuf)) |
|
|
157 | { |
|
|
158 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED); |
|
|
159 | destroy (); |
|
|
160 | return -1; |
|
|
161 | } |
|
|
162 | } |
|
|
163 | |
|
|
164 | return 0; |
|
|
165 | } |
|
|
166 | |
|
|
167 | void |
|
|
168 | client::skip_packet (int len) |
|
|
169 | { |
|
|
170 | inbuf_len -= len; |
|
|
171 | memmove (inbuf, inbuf + len, inbuf_len); |
|
|
172 | } |
|
|
173 | |
141 | /*********************************************************************** |
174 | /***************************************************************************** |
142 | * |
175 | * Start of command dispatch area. |
143 | * packet functions/utilities |
176 | * The commands here are protocol commands. |
144 | * |
|
|
145 | **********************************************************************/ |
177 | ****************************************************************************/ |
146 | |
178 | |
147 | packet &packet::operator <<(const data &v) |
179 | // SocketCommand, PlayingCommand, should not exist with those ugly casts |
|
|
180 | #define SC(cb) (void *)static_cast<void (*)(char *, int, client *)>(cb), |
|
|
181 | #define PC(cb) (void *)static_cast<void (*)(char *, int, player *)>(cb), PF_PLAYER | |
|
|
182 | |
|
|
183 | /** |
|
|
184 | * Dispatch table for the server. |
|
|
185 | */ |
|
|
186 | static struct packet_type packets[] = { |
|
|
187 | {"ncom", PC(NewPlayerCmd) PF_PLAYING }, |
|
|
188 | {"command", PC(PlayerCmd) PF_PLAYING }, |
|
|
189 | |
|
|
190 | {"examine", PC(ExamineCmd) PF_PLAYING }, |
|
|
191 | {"apply", PC(ApplyCmd) PF_PLAYING }, |
|
|
192 | {"reply", PC(ReplyCmd) 0 }, |
|
|
193 | {"lookat", PC(LookAt) PF_PLAYING }, |
|
|
194 | {"lock", PC(LockItem) PF_PLAYING }, |
|
|
195 | {"mark", PC(MarkItem) PF_PLAYING }, |
|
|
196 | {"move", PC(MoveCmd) PF_PLAYING }, |
|
|
197 | {"ext", PC(ExtCmd) 0 }, /* CF+ */ |
|
|
198 | {"mapredraw", PC(MapRedrawCmd) 0 }, /* Added: phil */ |
|
|
199 | {"mapinfo", PC(MapInfoCmd) 0 }, /* CF+ */ |
|
|
200 | |
|
|
201 | {"exti", SC(ExtiCmd) 0 }, /* CF+ */ |
|
|
202 | {"addme", SC(AddMeCmd) 0 }, |
|
|
203 | {"askface", SC(SendFaceCmd) 0 }, /* Added: phil */ |
|
|
204 | {"requestinfo", SC(RequestInfo) 0 }, |
|
|
205 | {"setfacemode", SC(SetFaceMode) 0 }, |
|
|
206 | {"setsound", SC(SetSound) 0 }, |
|
|
207 | {"setup", SC(SetUp) 0 }, |
|
|
208 | {"version", SC(VersionCmd) 0 }, |
|
|
209 | {"toggleextendedinfos", SC(ToggleExtendedInfos) 0 }, /*Added: tchize */ |
|
|
210 | {"toggleextendedtext", SC(ToggleExtendedText) 0 }, /*Added: tchize */ |
|
|
211 | {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */ |
|
|
212 | }; |
|
|
213 | |
|
|
214 | bool |
|
|
215 | client::may_execute (const packet_type *pkt) const |
148 | { |
216 | { |
149 | if (room () < v.len) |
217 | return (!(pkt->flags & PF_PLAYER) || pl) |
150 | reset (); |
218 | && (!(pkt->flags & PF_PLAYING) || (pl && pl->state == ST_PLAYING)); |
|
|
219 | } |
|
|
220 | |
|
|
221 | void |
|
|
222 | client::execute (const packet_type *pkt, char *data, int datalen) |
|
|
223 | { |
|
|
224 | if (may_execute (pkt)) |
|
|
225 | { |
|
|
226 | //TODO: only one format |
|
|
227 | if (pkt->flags & PF_PLAYER) |
|
|
228 | ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); |
|
|
229 | else |
|
|
230 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
|
|
231 | } |
151 | else |
232 | else |
|
|
233 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED, pkt->name); |
|
|
234 | } |
|
|
235 | |
|
|
236 | bool |
|
|
237 | client::handle_packet () |
|
|
238 | { |
|
|
239 | int pkt_len = next_packet (); |
|
|
240 | |
|
|
241 | if (!pkt_len) |
|
|
242 | return false; |
|
|
243 | else if (pkt_len < 0) |
|
|
244 | { |
|
|
245 | LOG (llevError, "read error on player %s\n", |
|
|
246 | pl && pl->ob ? &pl->ob->name : "[anonymous]"); |
|
|
247 | destroy (); |
|
|
248 | return false; |
152 | { |
249 | } |
153 | if (v.len) |
250 | |
|
|
251 | inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */ |
|
|
252 | |
|
|
253 | /* First, break out beginning word. There are at least |
|
|
254 | * a few commands that do not have any paremeters. If |
|
|
255 | * we get such a command, don't worry about trying |
|
|
256 | * to break it up. |
|
|
257 | */ |
|
|
258 | int datalen; |
|
|
259 | char *data = strchr ((char *)inbuf + 2, ' '); |
|
|
260 | |
|
|
261 | if (data) |
|
|
262 | { |
|
|
263 | *data++ = 0; |
|
|
264 | datalen = pkt_len - (data - (char *)inbuf); |
|
|
265 | } |
|
|
266 | else |
|
|
267 | { |
|
|
268 | data = (char *)inbuf + 2; // better read garbage than segfault |
|
|
269 | datalen = 0; |
|
|
270 | } |
|
|
271 | |
|
|
272 | for (packet_type *pkt = packets; pkt < packets + (sizeof (packets) / sizeof (packets[0])); ++pkt) |
|
|
273 | if (!strcmp ((char *)inbuf + 2, pkt->name)) |
154 | { |
274 | { |
155 | memcpy (cur, v.ptr, v.len); |
275 | if (pkt->flags & PF_PLAYER) |
156 | cur += v.len; |
276 | queue_command (pkt, data, datalen); |
|
|
277 | else |
|
|
278 | execute (pkt, data, datalen); |
|
|
279 | |
|
|
280 | goto next_packet; |
157 | } |
281 | } |
|
|
282 | |
|
|
283 | // If we get here, we didn't find a valid command. |
|
|
284 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED, (char *)inbuf + 2); |
|
|
285 | next_packet: |
|
|
286 | skip_packet (pkt_len); |
|
|
287 | |
|
|
288 | // input buffer has space again |
|
|
289 | socket_ev.poll (socket_ev.poll () | PE_R); |
|
|
290 | |
|
|
291 | return true; |
|
|
292 | } |
|
|
293 | |
|
|
294 | // callback called when socket is either readable or writable |
|
|
295 | void |
|
|
296 | client::socket_cb (iow &w, int got) |
|
|
297 | { |
|
|
298 | //TODO remove when we have better socket cleanup logic |
|
|
299 | if (status == Ns_Dead) |
158 | } |
300 | { |
159 | |
301 | socket_ev.poll (0); |
160 | return *this; |
302 | return; |
161 | } |
|
|
162 | |
|
|
163 | packet &packet::operator <<(const data8 &v) |
|
|
164 | { |
|
|
165 | unsigned int len = min (v.len, 0x00FF); |
|
|
166 | return *this << uint8 (len) << data (v.ptr, len); |
|
|
167 | } |
|
|
168 | |
|
|
169 | packet &packet::operator <<(const data16 &v) |
|
|
170 | { |
|
|
171 | unsigned int len = min (v.len, 0xFFFF); |
|
|
172 | return *this << uint16 (len) << data (v.ptr, len); |
|
|
173 | } |
|
|
174 | |
|
|
175 | packet &packet::operator <<(const char *v) |
|
|
176 | { |
|
|
177 | return *this << data (v, strlen (v ? v : 0)); |
|
|
178 | } |
|
|
179 | |
|
|
180 | void |
|
|
181 | packet::printf (const char *format, ...) |
|
|
182 | { |
|
|
183 | int size = room (); |
|
|
184 | |
|
|
185 | va_list ap; |
|
|
186 | va_start (ap, format); |
|
|
187 | int len = vsnprintf ((char *)cur, size, format, ap); |
|
|
188 | va_end (ap); |
|
|
189 | |
|
|
190 | if (len >= size) |
|
|
191 | return reset (); |
|
|
192 | |
|
|
193 | cur += len; |
|
|
194 | } |
|
|
195 | |
|
|
196 | /****************************************************************************** |
|
|
197 | * |
|
|
198 | * Start of read routines. |
|
|
199 | * |
|
|
200 | ******************************************************************************/ |
|
|
201 | |
|
|
202 | int |
|
|
203 | client::read_packet () |
|
|
204 | { |
|
|
205 | for (;;) |
|
|
206 | { |
303 | } |
207 | if (inbuf_len >= 2) |
|
|
208 | { |
|
|
209 | unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
|
|
210 | |
304 | |
211 | if (inbuf_len >= 2 + pkt_len) |
305 | if (got & PE_W) |
212 | return pkt_len + 2; |
306 | { |
213 | } |
307 | write_outputbuffer (); |
|
|
308 | |
|
|
309 | if (!outputbuffer.len) |
|
|
310 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
|
|
311 | } |
|
|
312 | |
|
|
313 | if (got & PE_R) |
|
|
314 | { |
|
|
315 | //TODO: rate-limit tcp connection in better ways, important |
214 | |
316 | |
215 | int amount = sizeof (inbuf) - inbuf_len; |
317 | int amount = sizeof (inbuf) - inbuf_len; |
216 | |
318 | |
217 | if (amount <= 0) |
319 | if (!amount) |
218 | { |
320 | { |
219 | LOG (llevError, "packet too large");//TODO |
321 | // input buffer full |
|
|
322 | socket_ev.poll (socket_ev.poll () & ~PE_R); |
220 | return -1; |
323 | return; |
221 | } |
324 | } |
222 | |
325 | |
223 | amount = read (fd, inbuf + inbuf_len, amount); |
326 | amount = read (fd, inbuf + inbuf_len, amount); |
224 | |
327 | |
225 | if (!amount) |
328 | if (!amount) |
226 | { |
329 | { |
227 | status = Ns_Dead; |
330 | destroy (); |
228 | return -1; |
331 | return; |
229 | } |
332 | } |
230 | else if (amount < 0) |
333 | else if (amount < 0) |
231 | { |
334 | { |
232 | if (errno != EAGAIN && errno != EINTR) |
335 | if (errno != EAGAIN && errno != EINTR) |
233 | { |
336 | { |
234 | LOG (llevError, "read error: %s\n", strerror (errno)); |
337 | LOG (llevError, "read error: %s\n", strerror (errno)); |
|
|
338 | destroy (); |
235 | return -1; |
339 | return; |
236 | } |
340 | } |
237 | |
341 | |
238 | return 0; |
342 | // should not be here, normally |
|
|
343 | } |
|
|
344 | else |
239 | } |
345 | { |
240 | |
|
|
241 | inbuf_len += amount; |
346 | inbuf_len += amount; |
242 | |
347 | |
243 | cst_tot.ibytes += amount; |
348 | cst_tot.ibytes += amount; |
244 | cst_lst.ibytes += amount; |
349 | cst_lst.ibytes += amount; |
245 | } |
|
|
246 | } |
|
|
247 | |
350 | |
|
|
351 | cmd_ev.start (); |
|
|
352 | } |
|
|
353 | } |
|
|
354 | } |
|
|
355 | |
|
|
356 | // called whenever we have additional commands to process |
248 | void |
357 | void |
249 | client::skip_packet (int len) |
358 | client::cmd_cb (iw &w) |
250 | { |
359 | { |
251 | inbuf_len -= len; |
360 | if (handle_packet ()) |
252 | memmove (inbuf, inbuf + len, inbuf_len); |
361 | w.start (); |
|
|
362 | else |
|
|
363 | flush (); |
253 | } |
364 | } |
254 | |
365 | |
255 | /******************************************************************************* |
366 | /******************************************************************************* |
256 | * |
367 | * |
257 | * Start of write related routines. |
368 | * Start of write related routines. |
… | |
… | |
270 | char *buf = (char *)buf_; |
381 | char *buf = (char *)buf_; |
271 | char *pos = buf; |
382 | char *pos = buf; |
272 | int amt = 0; |
383 | int amt = 0; |
273 | |
384 | |
274 | if (status == Ns_Dead || !buf) |
385 | if (status == Ns_Dead || !buf) |
275 | { |
|
|
276 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
|
|
277 | return; |
386 | return; |
278 | } |
|
|
279 | |
387 | |
280 | if ((len + outputbuffer.len) > SOCKETBUFSIZE) |
388 | if ((len + outputbuffer.len) > SOCKETBUFSIZE) |
281 | { |
389 | { |
282 | LOG (llevDebug, "Socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
390 | LOG (llevDebug, "socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
283 | status = Ns_Dead; |
391 | destroy (); |
284 | return; |
392 | return; |
285 | } |
393 | } |
286 | |
394 | |
287 | int avail, end; |
395 | int avail, end; |
288 | |
396 | |
… | |
… | |
306 | } |
414 | } |
307 | |
415 | |
308 | outputbuffer.len += len; |
416 | outputbuffer.len += len; |
309 | } |
417 | } |
310 | |
418 | |
311 | void |
|
|
312 | client::socket_cb (iow &w, int got) |
|
|
313 | { |
|
|
314 | write_outputbuffer (); |
|
|
315 | |
|
|
316 | if (!outputbuffer.len) |
|
|
317 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
|
|
318 | } |
|
|
319 | |
|
|
320 | /** |
419 | /** |
321 | * Takes a string of data, and writes it out to the socket. A very handy |
420 | * Takes a string of data, and writes it out to the socket. A very handy |
322 | * shortcut function. |
421 | * shortcut function. |
323 | */ |
422 | */ |
324 | void |
423 | void |
… | |
… | |
358 | |
457 | |
359 | void |
458 | void |
360 | client::send_packet (const char *buf) |
459 | client::send_packet (const char *buf) |
361 | { |
460 | { |
362 | send_packet (buf, strlen (buf)); |
461 | send_packet (buf, strlen (buf)); |
|
|
462 | } |
|
|
463 | |
|
|
464 | void |
|
|
465 | client::send_packet_printf (const char *format, ...) |
|
|
466 | { |
|
|
467 | packet sl; |
|
|
468 | |
|
|
469 | va_list ap; |
|
|
470 | va_start (ap, format); |
|
|
471 | sl.vprintf (format, ap); |
|
|
472 | va_end (ap); |
|
|
473 | |
|
|
474 | send_packet (sl); |
|
|
475 | } |
|
|
476 | |
|
|
477 | /*********************************************************************** |
|
|
478 | * |
|
|
479 | * packet functions/utilities |
|
|
480 | * |
|
|
481 | **********************************************************************/ |
|
|
482 | |
|
|
483 | packet &packet::operator <<(const data &v) |
|
|
484 | { |
|
|
485 | if (room () < v.len) |
|
|
486 | reset (); |
|
|
487 | else |
|
|
488 | { |
|
|
489 | if (v.len) |
|
|
490 | { |
|
|
491 | memcpy (cur, v.ptr, v.len); |
|
|
492 | cur += v.len; |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | return *this; |
|
|
497 | } |
|
|
498 | |
|
|
499 | packet &packet::operator <<(const data8 &v) |
|
|
500 | { |
|
|
501 | unsigned int len = min (v.len, 0x00FF); |
|
|
502 | return *this << uint8 (len) << data (v.ptr, len); |
|
|
503 | } |
|
|
504 | |
|
|
505 | packet &packet::operator <<(const data16 &v) |
|
|
506 | { |
|
|
507 | unsigned int len = min (v.len, 0xFFFF); |
|
|
508 | return *this << uint16 (len) << data (v.ptr, len); |
|
|
509 | } |
|
|
510 | |
|
|
511 | packet &packet::operator <<(const char *v) |
|
|
512 | { |
|
|
513 | return *this << data (v, strlen (v ? v : 0)); |
|
|
514 | } |
|
|
515 | |
|
|
516 | void |
|
|
517 | packet::vprintf (const char *format, va_list ap) |
|
|
518 | { |
|
|
519 | int size = room (); |
|
|
520 | |
|
|
521 | int len = vsnprintf ((char *)cur, size, format, ap); |
|
|
522 | |
|
|
523 | if (len >= size) |
|
|
524 | return reset (); |
|
|
525 | |
|
|
526 | cur += len; |
363 | } |
527 | } |
364 | |
528 | |
365 | /****************************************************************************** |
529 | /****************************************************************************** |
366 | * |
530 | * |
367 | * statistics logging functions. |
531 | * statistics logging functions. |