File: netmess.c

package info (click to toggle)
liquidwar 5.6.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,912 kB
  • ctags: 2,624
  • sloc: ansic: 25,422; xml: 3,985; sh: 3,355; makefile: 1,378; asm: 1,344; python: 537; php: 486; sql: 22
file content (472 lines) | stat: -rw-r--r-- 13,087 bytes parent folder | download | duplicates (4)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/********************************************************************/
/*                                                                  */
/*            L   I  QQ  U U I DD    W   W  A  RR    555            */
/*            L   I Q  Q U U I D D   W   W A A R R   5              */
/*            L   I Q  Q U U I D D   W W W AAA RR    55             */
/*            L   I Q Q  U U I D D   WW WW A A R R     5            */
/*            LLL I  Q Q  U  I DD    W   W A A R R   55             */
/*                                                                  */
/*                             b                                    */
/*                             bb  y y                              */
/*                             b b yyy                              */
/*                             bb    y                              */
/*                                 yy                               */
/*                                                                  */
/*                     U U       FFF  O   O  TTT                    */
/*                     U U       F   O O O O  T                     */
/*                     U U TIRET FF  O O O O  T                     */
/*                     U U       F   O O O O  T                     */
/*                      U        F    O   O   T                     */
/*                                                                  */
/********************************************************************/

/*****************************************************************************/
/* Liquid War is a multiplayer wargame                                       */
/* Copyright (C) 1998-2007 Christian Mauduit                                 */
/*                                                                           */
/* 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
/*                                                                           */
/* Liquid War homepage : http://www.ufoot.org/liquidwar/v5                   */
/* Contact author      : ufoot@ufoot.org                                     */
/*****************************************************************************/

/********************************************************************/
/* name          : netmess.c                                        */
/* content       : functions to create and parse network messages   */
/* last update   : April 14th 2001                                  */
/********************************************************************/

/*==================================================================*/
/* includes                                                         */
/*==================================================================*/

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "netmess.h"

/*==================================================================*/
/* static functions declaration                                     */
/*==================================================================*/

static int eat_space (char **message);
static int read_separator (char **message);
static char *read_word (char **message);
static int get_code (char **message);
static int get_argc (char *message);
static char **get_argv (char *message, int argc);

/*==================================================================*/
/* functions                                                        */
/*==================================================================*/

/*------------------------------------------------------------------*/
/*
 * strcasecmp does not exists on win32, so we make it default to strcmp.
 * It's not a big problem since case insensitive tests are usefull in
 * developpement and network stuff is easier to test under UNIX anyway...
 */
#ifndef strcasecmp
#define strcasecmp(A,B) strcmp((A),(B))
#endif

/*------------------------------------------------------------------*/
/*
 * Skip spaces, tabs and the likes.
 */
static int
eat_space (char **message)
{
  int result = 0;

  while (isspace (**message))
    {
      result++;
      ++(*message);
    }

  return result;
}

/*------------------------------------------------------------------*/
/*
 * Reads a separator token. The separator is ',' 
 */
static int
read_separator (char **message)
{
  int result = 0;

  eat_space (message);
  if ((**message) == ',')
    {
      result++;
      ++(*message);
    }
  eat_space (message);

  return result;
}

/*------------------------------------------------------------------*/
/*
 * Reads a word, a number, basically what's between separators.
 * The returned pointer must be disallocated with a "free".
 */
static char *
read_word (char **message)
{
  char *begin = NULL;
  char *result = NULL;
  int len;
  char quote_char;

  eat_space (message);

  quote_char = **message;

  if (quote_char == '\"' || quote_char == '\'')
    {
      /*
       * We handle strings quoted with ' or ".
       * No escape character is handled yet...
       */
      ++(*message);

      begin = *message;

      while ((**message) != quote_char && (**message) != '\0')
	{
	  ++(*message);
	}

      len = (*message) - begin;

      ++(*message);
    }
  else
    {
      begin = *message;

      while ((!isspace (**message)) && (**message) != ','
	     && (**message) != '\0')
	{
	  ++(*message);
	}

      len = (*message) - begin;
    }

  if (len >= 0)
    {
      result = (char *) malloc (len + 1);
      if (result != NULL)
	{
	  if (len > 0)
	    {
	      strncpy (result, begin, len);
	    }
	  result[len] = 0;

	  lw_netmess_cleanup_arg (result);
	}
    }
  eat_space (message);

  return result;
}

/*------------------------------------------------------------------*/
/*
 * Returns the code of the command.
 */
static int
get_code (char **message)
{
  char *command;
  int result = LW_NETMESS_CODE_UNKNOWN;

  if (read_separator (message) == 0)
    {
      if ((command = read_word (message)) != NULL)
	{
	  if (strcasecmp (command, LW_NETMESS_TEXT_OK) == 0)
	    {
	      result = LW_NETMESS_CODE_OK;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_ERR) == 0)
	    {
	      result = LW_NETMESS_CODE_ERR;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_PING) == 0)
	    {
	      result = LW_NETMESS_CODE_PING;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_ECHO) == 0)
	    {
	      result = LW_NETMESS_CODE_ECHO;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_PROGRAM) == 0)
	    {
	      result = LW_NETMESS_CODE_PROGRAM;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_VERSION) == 0)
	    {
	      result = LW_NETMESS_CODE_VERSION;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_PASSWORD) == 0)
	    {
	      result = LW_NETMESS_CODE_PASSWORD;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_FREE) == 0)
	    {
	      result = LW_NETMESS_CODE_FREE;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_TEAM) == 0)
	    {
	      result = LW_NETMESS_CODE_TEAM;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_READY) == 0)
	    {
	      result = LW_NETMESS_CODE_READY;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_START) == 0)
	    {
	      result = LW_NETMESS_CODE_START;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_WAITING) == 0)
	    {
	      result = LW_NETMESS_CODE_WAITING;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_NEXT) == 0)
	    {
	      result = LW_NETMESS_CODE_NEXT;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_TEAMSTARTINFO) == 0)
	    {
	      result = LW_NETMESS_CODE_TEAMSTARTINFO;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_WHO) == 0)
	    {
	      result = LW_NETMESS_CODE_WHO;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_LOCAL) == 0)
	    {
	      result = LW_NETMESS_CODE_LOCAL;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_NETWORK) == 0)
	    {
	      result = LW_NETMESS_CODE_NETWORK;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_NOBODY) == 0)
	    {
	      result = LW_NETMESS_CODE_NOBODY;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_SENDMAP) == 0)
	    {
	      result = LW_NETMESS_CODE_SENDMAP;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_RECVMAP) == 0)
	    {
	      result = LW_NETMESS_CODE_RECVMAP;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_SENDCONFIG) == 0)
	    {
	      result = LW_NETMESS_CODE_SENDCONFIG;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_RECVCONFIG) == 0)
	    {
	      result = LW_NETMESS_CODE_RECVCONFIG;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_CHATTALK) == 0)
	    {
	      result = LW_NETMESS_CODE_CHATTALK;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_CHATLISTEN) == 0)
	    {
	      result = LW_NETMESS_CODE_CHATLISTEN;
	    }
	  else if (strcasecmp (command, LW_NETMESS_TEXT_QUIT) == 0)
	    {
	      result = LW_NETMESS_CODE_QUIT;
	    }

	  free (command);
	}
    }

  return result;
}

/*------------------------------------------------------------------*/
/* 
 * Gets the number of arguments in the message.
 * It's a little different from the C convention used in main(),
 * since 0 means "0 args" (whereas in main() 0 args corresponds to argc==1).
 */
static int
get_argc (char *message)
{
  int result = 0;
  char *word = NULL;
  int null_arg = 0;

  if (read_separator (&message) == 0)
    {
      if (message[0] == '\0')
	{
	  null_arg = 1;
	}

      word = read_word (&message);
      if (word != NULL)
	{
	  free (word);
	  result = 1;
	  while (read_separator (&message) == 1)
	    {
	      word = read_word (&message);
	      if (word != NULL)
		{
		  free (word);
		  result++;
		}
	    }
	}
    }

  if (result == 1 && null_arg)
    {
      result = 0;
    }

  return result;
}

/*------------------------------------------------------------------*/
/*
 * Parses all the arguments of the message.
 */
static char **
get_argv (char *message, int argc)
{
  char **result = NULL;
  char *word;
  int i = 0;

  if (argc > 0 && read_separator (&message) == 0)
    {
      result = (char **) malloc (argc * sizeof (char *));
      if (result != NULL)
	{
	  word = read_word (&message);
	  if (word != NULL)
	    {
	      result[i] = word;
	      i = 1;
	      while (i < argc && read_separator (&message) == 1)
		{
		  word = read_word (&message);
		  if (word != NULL)
		    {
		      result[i] = word;
		      i++;
		    }
		}
	    }
	}
      if (i != argc)
	{
	  free (result);
	  result = NULL;
	}
    }

  return result;
}

/*------------------------------------------------------------------*/
/*
 * Parses and interprets a network message.
 * The syntaxe must be "COMMAND arg1,arg2,arg3,...".
 * This function should never fail, unless there are some problems
 * in memory allocation. Parsing an invalid message should simply
 * return a LW_NETMESS_CODE_UNKNOWN type with no args at all.
 * It's important to free the obtained pointer with lw_netmess_free.
 */
LW_NETMESS *
lw_netmess_read (char *message)
{
  LW_NETMESS *result = NULL;

  result = (LW_NETMESS *) malloc (sizeof (LW_NETMESS));
  if (result != NULL)
    {
      result->code = get_code (&message);
      result->argc = get_argc (message);
      result->argv = get_argv (message, result->argc);
    }

  return result;
}

/*------------------------------------------------------------------*/
/*
 * To be called after each successfull call to lw_netmess_read
 * This function calls free() internally.
 */
void
lw_netmess_free (LW_NETMESS * ptr)
{
  int i;

  if (ptr != NULL)
    {
      if (ptr->argv != NULL)
	{
	  for (i = 0; i < ptr->argc; ++i)
	    {
	      free (ptr->argv[i]);
	    }
	  if (ptr->argc > 0)
	    {
	      free (ptr->argv);
	    }
	}
    }
}

/*------------------------------------------------------------------*/
/*
 * Cleanups strings so that they can be used as arguments in
 * network messages
 */
void
lw_netmess_cleanup_arg (char *string)
{
  char *pos;
  unsigned char c;

  pos = string;
  while ((c = *pos) != '\0')
    {
      if (c < 32 || c > 127 || strchr ("'\",", c))
	{
	  (*pos) = ' ';
	}
      pos++;
    }
}