Add a routine to "epan/filesystem.c" to test whether a file is a FIFO.
[obnox/wireshark/wip.git] / packet-quakeworld.c
1 /* packet-quakeworld.c
2  * Routines for QuakeWorld packet dissection
3  *
4  * Uwe Girlich <uwe@planetquake.com>
5  *      http://www.idsoftware.com/q1source/q1source.zip
6  *
7  * $Id: packet-quakeworld.c,v 1.14 2002/06/09 21:25:47 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * Copied from packet-quake.c
14  * 
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  * 
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #include <glib.h>
39 #include <string.h>
40 #include <epan/packet.h>
41 #include "prefs.h"
42
43 static int proto_quakeworld = -1;
44
45 static int hf_quakeworld_s2c = -1;
46 static int hf_quakeworld_c2s = -1;
47 static int hf_quakeworld_connectionless = -1;
48 static int hf_quakeworld_game = -1;
49 static int hf_quakeworld_connectionless_marker = -1;
50 static int hf_quakeworld_connectionless_text = -1;
51 static int hf_quakeworld_connectionless_command = -1;
52 static int hf_quakeworld_connectionless_arguments = -1;
53 static int hf_quakeworld_connectionless_connect_version = -1;
54 static int hf_quakeworld_connectionless_connect_qport = -1;
55 static int hf_quakeworld_connectionless_connect_challenge = -1;
56 static int hf_quakeworld_connectionless_connect_infostring = -1;
57 static int hf_quakeworld_connectionless_connect_infostring_key_value = -1;
58 static int hf_quakeworld_connectionless_connect_infostring_key = -1;
59 static int hf_quakeworld_connectionless_connect_infostring_value = -1;
60 static int hf_quakeworld_connectionless_rcon_password = -1;
61 static int hf_quakeworld_connectionless_rcon_command = -1;
62 static int hf_quakeworld_game_seq1 = -1;
63 static int hf_quakeworld_game_rel1 = -1;
64 static int hf_quakeworld_game_seq2 = -1;
65 static int hf_quakeworld_game_rel2 = -1;
66 static int hf_quakeworld_game_qport = -1;
67
68 static gint ett_quakeworld = -1;
69 static gint ett_quakeworld_connectionless = -1;
70 static gint ett_quakeworld_connectionless_text = -1;
71 static gint ett_quakeworld_connectionless_arguments = -1;
72 static gint ett_quakeworld_connectionless_connect_infostring = -1;
73 static gint ett_quakeworld_connectionless_connect_infostring_key_value = -1;
74 static gint ett_quakeworld_game = -1;
75 static gint ett_quakeworld_game_seq1 = -1;
76 static gint ett_quakeworld_game_seq2 = -1;
77 static gint ett_quakeworld_game_clc = -1;
78 static gint ett_quakeworld_game_svc = -1;
79
80 static dissector_handle_t data_handle;
81
82 /*
83    helper functions, they may ave to go somewhere else
84    they are mostly copied without change from
85         quakeworldsource/client/cmd.c
86         quakeworldsource/client/common.c
87 */
88
89 #define MAX_TEXT_SIZE   2048
90
91 static  char            com_token[MAX_TEXT_SIZE+1];
92 static  int             com_token_start;
93 static  int             com_token_length;
94
95 static char *
96 COM_Parse (char *data)
97 {
98         int     c;
99         int     len;
100
101         len = 0;
102         com_token[0] = 0;
103         com_token_start = 0;
104         com_token_length = 0;
105
106         if (data == NULL)
107                 return NULL;
108
109         /* skip whitespace */
110 skipwhite:
111         while ( (c = *data) <= ' ') {
112                 if (c == 0)
113                         return NULL;    /* end of file; */
114                 data++;
115                 com_token_start++;
116         }
117
118         /* skip // comments */
119         if (c=='/' && data[1] == '/') {
120                 while (*data && *data != '\n')
121                         data++;
122                         com_token_start++;
123                 goto skipwhite;
124         }
125
126         /* handle quoted strings specially */
127         if (c == '\"') {
128                 data++;
129                 com_token_start++;
130                 while (1) {
131                         c = *data++;
132                         if (c=='\"' || c==0) {
133                                 com_token[len] = 0;
134                                 return data;
135                         }
136                         com_token[len] = c;
137                         len++;
138                         com_token_length++;
139                 }
140         }
141
142         /* parse a regular word */
143         do {
144                 com_token[len] = c;
145                 data++;
146                 len++;
147                 com_token_length++;
148                 c = *data;
149         } while (c>32);
150
151         com_token[len] = 0;
152         return data;
153 }
154
155
156 #define                 MAX_ARGS 80
157 static  int             cmd_argc = 0;
158 static  char            *cmd_argv[MAX_ARGS];
159 static  char            *cmd_null_string = "";
160 static  int             cmd_argv_start[MAX_ARGS];
161 static  int             cmd_argv_length[MAX_ARGS];
162
163
164
165 static int
166 Cmd_Argc(void)
167 {
168         return cmd_argc;
169 }
170
171
172 static char*
173 Cmd_Argv(int arg)
174 {
175         if ( arg >= cmd_argc )
176                 return cmd_null_string;
177         return cmd_argv[arg];
178 }
179
180
181 static int
182 Cmd_Argv_start(int arg)
183 {
184         if ( arg >= cmd_argc )
185                 return 0;
186         return cmd_argv_start[arg];
187 }
188
189
190 static int
191 Cmd_Argv_length(int arg)
192 {
193         if ( arg >= cmd_argc )
194                 return 0;
195         return cmd_argv_length[arg];
196 }
197
198
199 static void
200 Cmd_TokenizeString(char* text)
201 {
202         int i;
203         int start;
204         int length;
205         
206
207         /* clear the args from the last string */
208         for (i=0 ; i<cmd_argc ; i++)
209                 g_free(cmd_argv[i]);
210
211         cmd_argc = 0;
212
213         start = 0;
214         while (TRUE) {
215
216                 /* skip whitespace up to a \n */
217                 while (*text && *text <= ' ' && *text != '\n') {
218                         text++;
219                         start++;
220                 }
221
222                 length = 0;
223
224                 if (*text == '\n') {
225                         /* a newline seperates commands in the buffer */
226                         text++;
227                         break;
228                 }
229
230                 if (!*text)
231                         return;
232                         
233                 text = COM_Parse (text);
234                 if (!text)
235                         return;
236
237                 if (cmd_argc < MAX_ARGS) {
238                         cmd_argv[cmd_argc] = g_strdup(com_token);
239                         cmd_argv_start[cmd_argc] = start + com_token_start;
240                         cmd_argv_length[cmd_argc] = com_token_length;
241                         cmd_argc++;
242                 }
243
244                 start += com_token_start + com_token_length;
245         }
246 }
247
248                         
249 static void
250 dissect_id_infostring(tvbuff_t *tvb, proto_tree* tree, 
251         int offset, char* infostring,
252         gint ett_key_value, int hf_key_value, int hf_key, int hf_value)
253 {
254         char * newpos = infostring;
255         int end_of_info = FALSE;
256
257         /* to look at all the key/value pairs, we destroy infostring */
258         while(!end_of_info) {
259                 char* keypos;
260                 char* valuepos;
261                 int keylength;
262                 char* keyvaluesep;
263                 int valuelength;
264                 char* valueend;
265
266                 keypos = newpos;
267                 if (*keypos == 0) break;
268                 if (*keypos == '\\') keypos++;
269
270                 for (keylength = 0
271                         ;
272                         *(keypos + keylength) != '\\' && 
273                         *(keypos + keylength) != 0
274                         ;
275                         keylength++) ;
276                 keyvaluesep = keypos + keylength;
277                 if (*keyvaluesep == 0) break;
278                 valuepos = keyvaluesep+1;
279                 for (valuelength = 0
280                         ;
281                         *(valuepos + valuelength) != '\\' && 
282                         *(valuepos + valuelength) != 0
283                         ;
284                         valuelength++) ;
285                 valueend = valuepos + valuelength;
286                 if (*valueend == 0) {
287                         end_of_info = TRUE;
288                 }
289                 *(keyvaluesep) = '=';
290                 *(valueend) = 0;
291                 
292                 if (tree) {
293                         proto_item* sub_item = NULL;
294                         proto_tree* sub_tree = NULL;
295
296                         sub_item = proto_tree_add_string(tree,
297                                 hf_key_value,
298                                 tvb,
299                                 offset + (keypos-infostring),
300                                 keylength + 1 + valuelength, keypos);
301                         if (sub_item) 
302                                 sub_tree =
303                                         proto_item_add_subtree(
304                                         sub_item,
305                                         ett_key_value);
306                         *(keyvaluesep) = 0;
307                         if (sub_tree) {
308                                 proto_tree_add_string(sub_tree,
309                                         hf_key,
310                                         tvb,
311                                         offset + (keypos-infostring),
312                                         keylength, keypos);
313                                 proto_tree_add_string(sub_tree,
314                                         hf_value,
315                                         tvb,
316                                         offset + (valuepos-infostring),
317                                         valuelength, valuepos);
318                         }
319                 }
320                 newpos = valueend + 1;
321         }
322 }
323
324
325 static const value_string names_direction[] = {
326 #define DIR_C2S 0
327         { DIR_C2S, "Client to Server" },
328 #define DIR_S2C 1
329         { DIR_S2C, "Server to Client" },
330         { 0, NULL }
331 };
332
333
334 /* I took this name and value directly out of the QW source. */
335 #define PORT_MASTER 27500
336 static unsigned int gbl_quakeworldServerPort=PORT_MASTER;
337
338
339 /* out of band message id bytes (taken out of quakeworldsource/client/protocol.h */
340  
341 /* M = master, S = server, C = client, A = any */
342 /* the second character will allways be \n if the message isn't a single */
343 /* byte long (?? not true anymore?) */
344  
345 #define S2C_CHALLENGE           'c'
346 #define S2C_CONNECTION          'j'
347 #define A2A_PING                'k'     /* respond with an A2A_ACK */
348 #define A2A_ACK                 'l'     /* general acknowledgement without info */
349 #define A2A_NACK                'm'     /* [+ comment] general failure */
350 #define A2A_ECHO                'e'     /* for echoing */
351 #define A2C_PRINT               'n'     /* print a message on client */
352  
353 #define S2M_HEARTBEAT           'a'     /* + serverinfo + userlist + fraglist */
354 #define A2C_CLIENT_COMMAND      'B'     /* + command line */
355 #define S2M_SHUTDOWN            'C'
356
357
358 static void
359 dissect_quakeworld_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo,
360         proto_tree *tree, int direction)
361 {
362         proto_tree      *cl_tree = NULL;
363         proto_item      *cl_item = NULL;
364         proto_item      *text_item = NULL;
365         proto_tree      *text_tree = NULL;
366         guint8          text[MAX_TEXT_SIZE+1];
367         int             maxbufsize = 0;
368         int             len;
369         int             offset;
370         guint32 marker;
371         int command_len;
372         char command[MAX_TEXT_SIZE+1];
373         int command_finished = FALSE;
374
375         marker = tvb_get_ntohl(tvb, 0);
376         if (tree) {
377                 cl_item = proto_tree_add_text(tree, tvb,
378                                 0, -1, "Connectionless");
379                 if (cl_item)
380                         cl_tree = proto_item_add_subtree(
381                                 cl_item, ett_quakeworld_connectionless);
382         }
383
384         if (cl_tree) {
385                 proto_tree_add_uint(cl_tree, hf_quakeworld_connectionless_marker,
386                                 tvb, 0, 4, marker);
387         }
388
389         /* all the rest of the packet is just text */
390         offset = 4;
391
392         maxbufsize = MIN((gint)MAX_TEXT_SIZE, tvb_length_remaining(tvb, offset));
393         len = tvb_get_nstringz0(tvb, offset, maxbufsize, text);
394         /* actually, we should look for a eol char and stop already there */
395
396         if (cl_tree) {
397                 text_item = proto_tree_add_string(cl_tree, hf_quakeworld_connectionless_text,
398                         tvb, offset, len + 1, text);
399                 if (text_item) {
400                         text_tree = proto_item_add_subtree(
401                                 text_item, ett_quakeworld_connectionless_text);
402                 }
403         }
404
405         if (direction == DIR_C2S) {
406                 /* client to sever commands */
407                 char *c;
408
409                 Cmd_TokenizeString(text);
410                 c = Cmd_Argv(0);
411                 
412                 /* client to sever commands */
413                 if (strcmp(c,"ping") == 0) {
414                         strcpy(command, "Ping");
415                         command_len = 4;
416                 } else if (strcmp(c,"status") == 0) {
417                         strcpy(command, "Status");
418                         command_len = 6;
419                 } else if (strcmp(c,"log") == 0) {
420                         strcpy(command, "Status");
421                         command_len = 3;
422                 } else if (strcmp(c,"connect") == 0) {
423                         int version;
424                         int qport;
425                         int challenge;
426                         char *infostring;
427                         proto_item *argument_item = NULL;
428                         proto_tree *argument_tree = NULL;
429                         proto_item *info_item = NULL;
430                         proto_tree *info_tree = NULL;
431                         strcpy(command, "Connect");
432                         command_len = Cmd_Argv_length(0);
433                         if (text_tree) {
434                                 proto_tree_add_string(text_tree, hf_quakeworld_connectionless_command,
435                                         tvb, offset, command_len, command);
436                                 argument_item = proto_tree_add_string(text_tree,
437                                         hf_quakeworld_connectionless_arguments,
438                                         tvb, offset + Cmd_Argv_start(1), len + 1 - Cmd_Argv_start(1), 
439                                         text + Cmd_Argv_start(1));
440                                 if (argument_item) {
441                                         argument_tree =
442                                                 proto_item_add_subtree(argument_item,   
443                                                         ett_quakeworld_connectionless_arguments);
444                                 }
445                                 command_finished=TRUE;
446                         }
447                         version = atoi(Cmd_Argv(1));
448                         qport = atoi(Cmd_Argv(2));
449                         challenge = atoi(Cmd_Argv(3));
450                         infostring = Cmd_Argv(4);
451                         if (argument_tree) {
452                                 proto_tree_add_uint(argument_tree,
453                                         hf_quakeworld_connectionless_connect_version,
454                                         tvb,
455                                         offset + Cmd_Argv_start(1),
456                                         Cmd_Argv_length(1), version);
457                                 proto_tree_add_uint(argument_tree,
458                                         hf_quakeworld_connectionless_connect_qport,
459                                         tvb,
460                                         offset + Cmd_Argv_start(2),
461                                         Cmd_Argv_length(2), qport);
462                                 proto_tree_add_int(argument_tree,
463                                         hf_quakeworld_connectionless_connect_challenge,
464                                         tvb,
465                                         offset + Cmd_Argv_start(3),
466                                         Cmd_Argv_length(3), challenge);
467                                 info_item = proto_tree_add_string(argument_tree,
468                                         hf_quakeworld_connectionless_connect_infostring,
469                                         tvb,
470                                         offset + Cmd_Argv_start(4),
471                                         Cmd_Argv_length(4), infostring);
472                                 if (info_item)
473                                         info_tree = proto_item_add_subtree(
474                                                 info_item, ett_quakeworld_connectionless_connect_infostring);
475                                 dissect_id_infostring(tvb, info_tree, offset + Cmd_Argv_start(4),
476                                         infostring,
477                                         ett_quakeworld_connectionless_connect_infostring_key_value,
478                                         hf_quakeworld_connectionless_connect_infostring_key_value,
479                                         hf_quakeworld_connectionless_connect_infostring_key,
480                                         hf_quakeworld_connectionless_connect_infostring_value);
481                         }
482                 } else if (strcmp(c,"getchallenge") == 0) {
483                         strcpy(command, "Get Challenge");
484                         command_len = Cmd_Argv_length(0);
485                 } else if (strcmp(c,"rcon") == 0) {
486                         char* password;
487                         int i;
488                         char remaining[MAX_TEXT_SIZE+1];
489                         proto_item *argument_item = NULL;
490                         proto_tree *argument_tree = NULL;
491                         strcpy(command, "Remote Command");
492                         command_len = Cmd_Argv_length(0);
493                         if (text_tree) {
494                                 proto_tree_add_string(text_tree, hf_quakeworld_connectionless_command,
495                                         tvb, offset, command_len, command);
496                                 argument_item = proto_tree_add_string(text_tree,
497                                         hf_quakeworld_connectionless_arguments,
498                                         tvb, offset + Cmd_Argv_start(1), len + 1 - Cmd_Argv_start(1), 
499                                         text + Cmd_Argv_start(1));
500                                 if (argument_item) {
501                                         argument_tree =
502                                                 proto_item_add_subtree(argument_item,   
503                                                         ett_quakeworld_connectionless_arguments);
504                                 }
505                                 command_finished=TRUE;
506                         }
507                         password = Cmd_Argv(1);
508                         if (argument_tree) {
509                                 proto_tree_add_string(argument_tree,
510                                         hf_quakeworld_connectionless_rcon_password,
511                                         tvb,
512                                         offset + Cmd_Argv_start(1),
513                                         Cmd_Argv_length(1), password);
514                         }
515                         for (i=2; i<Cmd_Argc() ; i++) {
516                                 remaining[0] = 0;
517                                 strcat (remaining, Cmd_Argv(i) );
518                                 strcat (remaining, " ");
519                         }
520                         if (text_tree) {
521                                 proto_tree_add_string(argument_tree,
522                                         hf_quakeworld_connectionless_rcon_command,
523                                         tvb, offset + Cmd_Argv_start(2),
524                                         Cmd_Argv_start(Cmd_Argc()-1) + Cmd_Argv_length(Cmd_Argc()-1) -
525                                         Cmd_Argv_start(2),
526                                         remaining);
527                         }
528                 } else if (c[0]==A2A_PING && ( c[1]==0 || c[1]=='\n')) {
529                         strcpy(command, "Ping");
530                         command_len = 1;
531                 } else if (c[0]==A2A_ACK && ( c[1]==0 || c[1]=='\n')) {
532                         strcpy(command, "Ack");
533                         command_len = 1;
534                 } else {
535                         strcpy(command, "Unknown");
536                         command_len = len;
537                 }
538         }
539         else {
540                 /* server to client commands */
541                 if (text[0] == S2C_CONNECTION) {
542                         strcpy(command, "Connected");
543                         command_len = 1;
544                 } else if (text[0] == A2C_CLIENT_COMMAND) {
545                         strcpy(command, "Client Command");
546                         command_len = 1;
547                         /* stringz (command), stringz (localid) */
548                 } else if (text[0] == A2C_PRINT) {
549                         strcpy(command, "Print");
550                         command_len = 1;
551                         /* string */
552                 } else if (text[0] == A2A_PING) {
553                         strcpy(command, "Ping");
554                         command_len = 1;
555                 } else if (text[0] == S2C_CHALLENGE) {
556                         strcpy(command, "Challenge");
557                         command_len = 1;
558                         /* string, atoi */
559                 } else {
560                         strcpy(command, "Unknown");
561                         command_len = len;
562                 }
563         }
564                 
565         if (check_col(pinfo->cinfo, COL_INFO)) {
566                 col_append_fstr(pinfo->cinfo, COL_INFO, " %s", command);
567         }
568                 
569         if (text_tree && !command_finished) {
570                 proto_tree_add_string(text_tree, hf_quakeworld_connectionless_command,
571                         tvb, offset, command_len, command);
572         }
573         offset += len + 1;
574 }
575
576
577 static void
578 dissect_quakeworld_client_commands(tvbuff_t *tvb, packet_info *pinfo,
579         proto_tree *tree)
580 {
581         /* If I have too much time at hand, I'll fill it with all
582            the information from my QWD specs:
583                 http://www.planetquake.com/demospecs/qwd/
584         */
585         call_dissector(data_handle,tvb, pinfo, tree);
586 }
587
588
589 static void
590 dissect_quakeworld_server_commands(tvbuff_t *tvb, packet_info *pinfo,
591         proto_tree *tree)
592 {
593         /* If I have too much time at hand, I'll fill it with all
594            the information from my QWD specs:
595                 http://www.planetquake.com/demospecs/qwd/
596         */
597         call_dissector(data_handle,tvb, pinfo, tree);
598 }
599
600
601 static const value_string names_reliable[] = {
602         { 0, "Non Reliable" },
603         { 1, "Reliable" },
604         { 0, NULL }
605 };
606
607
608 static void
609 dissect_quakeworld_GamePacket(tvbuff_t *tvb, packet_info *pinfo,
610         proto_tree *tree, int direction)
611 {
612         proto_tree      *game_tree = NULL;
613         proto_item      *game_item = NULL;
614         guint32 seq1;
615         guint32 seq2;
616         int rel1;
617         int rel2;
618         int offset;
619         guint           rest_length;
620
621         direction = (pinfo->destport == gbl_quakeworldServerPort) ?
622                         DIR_C2S : DIR_S2C;
623
624         if (tree) {
625                 game_item = proto_tree_add_text(tree, tvb,
626                                 0, -1, "Game");
627                 if (game_item)
628                         game_tree = proto_item_add_subtree(
629                                 game_item, ett_quakeworld_game);
630         }
631
632         offset = 0;
633
634         seq1 = tvb_get_letohl(tvb, offset);
635         rel1 = seq1 & 0x80000000 ? 1 : 0;
636         seq1 &= ~0x80000000;
637         if (game_tree) {
638                 proto_item *seq1_item = proto_tree_add_text(game_tree,
639                         tvb, offset, 4, "Current Sequence: %u (%s)",
640                         seq1, val_to_str(rel1,names_reliable,"%u"));
641                 if (seq1_item) {
642                         proto_tree *seq1_tree = proto_item_add_subtree(
643                                 seq1_item, ett_quakeworld_game_seq1);
644                         proto_tree_add_uint(seq1_tree, hf_quakeworld_game_seq1,
645                                         tvb, offset, 4, seq1);
646                         proto_tree_add_boolean(seq1_tree, hf_quakeworld_game_rel1,
647                                         tvb, offset+3, 1, rel1);
648                 }
649         }
650         offset += 4;
651
652         seq2 = tvb_get_letohl(tvb, offset);
653         rel2 = seq2 & 0x80000000 ? 1 : 0;
654         seq2 &= ~0x80000000;
655         if (game_tree) {
656                 proto_item *seq2_item = proto_tree_add_text(game_tree,
657                         tvb, offset, 4, "Acknowledge Sequence: %u (%s)",
658                         seq2, val_to_str(rel2,names_reliable,"%u"));;
659                 if (seq2_item) {
660                         proto_tree *seq2_tree = proto_item_add_subtree(
661                                 seq2_item, ett_quakeworld_game_seq2);
662                         proto_tree_add_uint(seq2_tree, hf_quakeworld_game_seq2,
663                                         tvb, offset, 4, seq2);
664                         proto_tree_add_boolean(seq2_tree, hf_quakeworld_game_rel2,
665                                         tvb, offset+3, 1, rel2);
666                 }
667         }
668         offset += 4;
669
670         if (direction == DIR_C2S) {
671                 /* client to server */
672                 guint16 qport = tvb_get_letohs(tvb, offset);
673                 if (game_tree) {
674                         proto_tree_add_uint(game_tree, hf_quakeworld_game_qport, 
675                                 tvb, offset, 2, qport);
676                 }
677                 offset +=2;
678         }
679
680         /* all the rest is pure game data */
681         rest_length = tvb_reported_length(tvb) - offset;
682         if (rest_length) {
683                 tvbuff_t *next_tvb =
684                 tvb_new_subset(tvb, offset, rest_length , rest_length);
685
686                 if (direction == DIR_C2S) {
687                         proto_item *c_item = NULL;
688                         proto_tree *c_tree = NULL;
689                         if (tree) {
690                                 c_item = proto_tree_add_text(game_tree, next_tvb,
691                                 0, -1, "Client Commands");
692                                 if (c_item) {
693                                         c_tree = proto_item_add_subtree(
694                                                 c_item, ett_quakeworld_game_clc);
695                                 }
696                         }
697                         dissect_quakeworld_client_commands(next_tvb, pinfo, c_tree);
698                 }
699                 else {
700                         proto_item *c_item = NULL;
701                         proto_tree *c_tree = NULL;
702                         if (tree) {
703                                 c_item = proto_tree_add_text(game_tree, next_tvb,
704                                 0, -1, "Server Commands");
705                                 if (c_item) {
706                                         c_tree = proto_item_add_subtree(
707                                         c_item, ett_quakeworld_game_svc);
708                                 }
709                         }
710                         dissect_quakeworld_server_commands(next_tvb, pinfo, c_tree);
711                 }
712         }
713 }
714
715
716 static void
717 dissect_quakeworld(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
718 {
719         proto_tree      *quakeworld_tree = NULL;
720         proto_item      *quakeworld_item = NULL;
721         int             direction;
722
723         direction = (pinfo->destport == gbl_quakeworldServerPort) ?
724                         DIR_C2S : DIR_S2C;
725
726         if (check_col(pinfo->cinfo, COL_PROTOCOL))
727                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUAKEWORLD");
728         if (check_col(pinfo->cinfo, COL_INFO))
729                 col_set_str(pinfo->cinfo, COL_INFO, val_to_str(direction,
730                         names_direction, "%u"));
731
732         if (tree) {
733                 quakeworld_item = proto_tree_add_item(tree, proto_quakeworld,
734                                 tvb, 0, -1, FALSE);
735                 if (quakeworld_item)
736                         quakeworld_tree = proto_item_add_subtree(
737                                 quakeworld_item, ett_quakeworld);
738                         if (quakeworld_tree) {
739                                 proto_tree_add_uint_format(quakeworld_tree,
740                                         direction == DIR_S2C ?
741                                         hf_quakeworld_s2c :
742                                         hf_quakeworld_c2s,
743                                         tvb, 0, 0, 1,
744                                         "Direction: %s", val_to_str(direction, names_direction, "%u"));
745                         }
746         }
747
748         if (tvb_get_ntohl(tvb, 0) == 0xffffffff) {
749                 if (check_col(pinfo->cinfo, COL_INFO)) {
750                         col_append_str(pinfo->cinfo, COL_INFO, " Connectionless");
751                 }
752                 if (quakeworld_tree)
753                         proto_tree_add_uint_format(quakeworld_tree,
754                                 hf_quakeworld_connectionless,
755                                 tvb, 0, 0, 1,
756                                 "Type: Connectionless");
757                 dissect_quakeworld_ConnectionlessPacket(
758                         tvb, pinfo, quakeworld_tree, direction);
759         }
760         else {
761                 if (check_col(pinfo->cinfo, COL_INFO)) {
762                         col_append_str(pinfo->cinfo, COL_INFO, " Game");
763                 }
764                 if (quakeworld_tree)
765                         proto_tree_add_uint_format(quakeworld_tree,
766                                 hf_quakeworld_game,
767                                 tvb, 0, 0, 1,
768                                 "Type: Game");
769                 dissect_quakeworld_GamePacket(
770                         tvb, pinfo, quakeworld_tree, direction);
771         }
772 }
773
774
775 void
776 proto_reg_handoff_quakeworld(void)
777 {
778         static int Initialized=FALSE;
779         static dissector_handle_t quakeworld_handle;
780         static int ServerPort=0;
781  
782         if (!Initialized) {
783                 quakeworld_handle = create_dissector_handle(dissect_quakeworld,
784                                 proto_quakeworld);
785                 Initialized=TRUE;
786         } else {
787                 dissector_delete("udp.port", ServerPort, quakeworld_handle);
788         }
789  
790         /* set port for future deletes */
791         ServerPort=gbl_quakeworldServerPort;
792  
793         dissector_add("udp.port", gbl_quakeworldServerPort, quakeworld_handle);
794         data_handle = find_dissector("data");
795 }
796
797
798 void
799 proto_register_quakeworld(void)
800 {
801         static hf_register_info hf[] = {
802                 { &hf_quakeworld_c2s,
803                         { "Client to Server", "quakeworld.c2s",
804                         FT_UINT32, BASE_DEC, NULL, 0x0,
805                         "Client to Server", HFILL }},
806                 { &hf_quakeworld_s2c,
807                         { "Server to Client", "quakeworld.s2c",
808                         FT_UINT32, BASE_DEC, NULL, 0x0,
809                         "Server to Client", HFILL }},
810                 { &hf_quakeworld_connectionless,
811                         { "Connectionless", "quakeworld.connectionless",
812                         FT_UINT32, BASE_DEC, NULL, 0x0,
813                         "Connectionless", HFILL }},
814                 { &hf_quakeworld_game,
815                         { "Game", "quakeworld.game",
816                         FT_UINT32, BASE_DEC, NULL, 0x0,
817                         "Game", HFILL }},
818                 { &hf_quakeworld_connectionless_marker,
819                         { "Marker", "quakeworld.connectionless.marker",
820                         FT_UINT32, BASE_HEX, NULL, 0x0,
821                         "Marker", HFILL }},
822                 { &hf_quakeworld_connectionless_text,
823                         { "Text", "quakeworld.connectionless.text",
824                         FT_STRING, BASE_DEC, NULL, 0x0,
825                         "Text", HFILL }},
826                 { &hf_quakeworld_connectionless_command,
827                         { "Command", "quakeworld.connectionless.command",
828                         FT_STRING, BASE_DEC, NULL, 0x0,
829                         "Command", HFILL }},
830                 { &hf_quakeworld_connectionless_arguments,
831                         { "Arguments", "quakeworld.connectionless.arguments",
832                         FT_STRING, BASE_DEC, NULL, 0x0,
833                         "Arguments", HFILL }},
834                 { &hf_quakeworld_connectionless_connect_version,
835                         { "Version", "quakeworld.connectionless.connect.version",
836                         FT_UINT32, BASE_DEC, NULL, 0x0,
837                         "Protocol Version", HFILL }},
838                 { &hf_quakeworld_connectionless_connect_qport,
839                         { "QPort", "quakeworld.connectionless.connect.qport",
840                         FT_UINT32, BASE_DEC, NULL, 0x0,
841                         "QPort of the client", HFILL }},
842                 { &hf_quakeworld_connectionless_connect_challenge,
843                         { "Challenge", "quakeworld.connectionless.connect.challenge",
844                         FT_INT32, BASE_DEC, NULL, 0x0,
845                         "Challenge from the server", HFILL }},
846                 { &hf_quakeworld_connectionless_connect_infostring,
847                         { "Infostring", "quakeworld.connectionless.connect.infostring",
848                         FT_STRING, BASE_DEC, NULL, 0x0,
849                         "Infostring with additional variables", HFILL }},
850                 { &hf_quakeworld_connectionless_connect_infostring_key_value,
851                         { "Key/Value", "quakeworld.connectionless.connect.infostring.key_value",
852                         FT_STRING, BASE_DEC, NULL, 0x0,
853                         "Key and Value", HFILL }},
854                 { &hf_quakeworld_connectionless_connect_infostring_key,
855                         { "Key", "quakeworld.connectionless.connect.infostring.key",
856                         FT_STRING, BASE_DEC, NULL, 0x0,
857                         "Infostring Key", HFILL }},
858                 { &hf_quakeworld_connectionless_connect_infostring_value,
859                         { "Value", "quakeworld.connectionless.connect.infostring.value",
860                         FT_STRING, BASE_DEC, NULL, 0x0,
861                         "Infostring Value", HFILL }},
862                 { &hf_quakeworld_connectionless_rcon_password,
863                         { "Password", "quakeworld.connectionless.rcon.password",
864                         FT_STRING, BASE_DEC, NULL, 0x0,
865                         "Rcon Password", HFILL }},
866                 { &hf_quakeworld_connectionless_rcon_command,
867                         { "Command", "quakeworld.connectionless.rcon.command",
868                         FT_STRING, BASE_DEC, NULL, 0x0,
869                         "Command", HFILL }},
870                 { &hf_quakeworld_game_seq1,
871                         { "Sequence Number", "quakeworld.game.seq1",
872                         FT_UINT32, BASE_DEC, NULL, 0x0,
873                         "Sequence number of the current packet", HFILL }},
874                 { &hf_quakeworld_game_rel1,
875                         { "Reliable", "quakeworld.game.rel1",
876                         FT_BOOLEAN, BASE_DEC, NULL, 0x0,
877                         "Packet is reliable and may be retransmitted", HFILL }},
878                 { &hf_quakeworld_game_seq2,
879                         { "Sequence Number", "quakeworld.game.seq2",
880                         FT_UINT32, BASE_DEC, NULL, 0x0,
881                         "Sequence number of the last received packet", HFILL }},
882                 { &hf_quakeworld_game_rel2,
883                         { "Reliable", "quakeworld.game.rel2",
884                         FT_BOOLEAN, BASE_DEC, NULL, 0x0,
885                         "Packet was reliable and may be retransmitted", HFILL }},
886                 { &hf_quakeworld_game_qport,
887                         { "QPort", "quakeworld.game.qport",
888                         FT_UINT32, BASE_DEC, NULL, 0x0,
889                         "QuakeWorld Client Port", HFILL }}
890         };
891         static gint *ett[] = {
892                 &ett_quakeworld,
893                 &ett_quakeworld_connectionless,
894                 &ett_quakeworld_connectionless_text,
895                 &ett_quakeworld_connectionless_arguments,
896                 &ett_quakeworld_connectionless_connect_infostring,
897                 &ett_quakeworld_connectionless_connect_infostring_key_value,
898                 &ett_quakeworld_game,
899                 &ett_quakeworld_game_seq1,
900                 &ett_quakeworld_game_seq2,
901                 &ett_quakeworld_game_clc,
902                 &ett_quakeworld_game_svc
903         };
904         module_t *quakeworld_module;
905
906         proto_quakeworld = proto_register_protocol("QuakeWorld Network Protocol",
907                                                 "QUAKEWORLD", "quakeworld");
908         proto_register_field_array(proto_quakeworld, hf, array_length(hf));
909         proto_register_subtree_array(ett, array_length(ett));
910
911         /* Register a configuration option for port */
912         quakeworld_module = prefs_register_protocol(proto_quakeworld,
913                 proto_reg_handoff_quakeworld);
914         prefs_register_uint_preference(quakeworld_module, "udp.port",
915                                         "QuakeWorld Server UDP Port",
916                                         "Set the UDP port for the QuakeWorld Server",
917                                         10, &gbl_quakeworldServerPort);
918 }
919