make the SEQUENCE dissection helper understand and handle Indefinite Length
[obnox/wireshark/wip.git] / epan / conversation.c
1 /* conversation.c
2  * Routines for building lists of packets that are part of a "conversation"
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #include <string.h>
32 #include <glib.h>
33 #include "packet.h"
34 #include "conversation.h"
35
36 /*
37  * Hash table for conversations with no wildcards.
38  */
39 static GHashTable *conversation_hashtable_exact = NULL;
40
41 /*
42  * Hash table for conversations with one wildcard address.
43  */
44 static GHashTable *conversation_hashtable_no_addr2 = NULL;
45
46 /*
47  * Hash table for conversations with one wildcard port.
48  */
49 static GHashTable *conversation_hashtable_no_port2 = NULL;
50
51 /*
52  * Hash table for conversations with one wildcard address and port.
53  */
54 static GHashTable *conversation_hashtable_no_addr2_or_port2 = NULL;
55
56 static GMemChunk *conversation_key_chunk = NULL;
57 static GMemChunk *conversation_chunk = NULL;
58
59 #ifdef __NOT_USED__
60 typedef struct conversation_key {
61         struct conversation_key *next;
62         address addr1;
63         address addr2;
64         port_type ptype;
65         guint32 port1;
66         guint32 port2;
67 } conversation_key;
68 #endif
69 /*
70  * Linked list of conversation keys, so we can, before freeing them all,
71  * free the address data allocations associated with them.
72  */
73 static conversation_key *conversation_keys;
74
75 static guint32 new_index;
76
77 static int conversation_init_count = 200;
78
79 /*
80  * Protocol-specific data attached to a conversation_t structure - protocol
81  * index and opaque pointer.
82  */
83 typedef struct _conv_proto_data {
84         int     proto;
85         void    *proto_data;
86 } conv_proto_data;
87
88 static GMemChunk *conv_proto_data_area = NULL;
89
90 /*
91  * Compute the hash value for two given address/port pairs if the match
92  * is to be exact.
93  */
94 static guint
95 conversation_hash_exact(gconstpointer v)
96 {
97         const conversation_key *key = (const conversation_key *)v;
98         guint hash_val;
99         int i;
100
101         hash_val = 0;
102         for (i = 0; i < key->addr1.len; i++)
103                 hash_val += key->addr1.data[i];
104
105         hash_val += key->port1;
106
107         for (i = 0; i < key->addr2.len; i++)
108                 hash_val += key->addr2.data[i];
109
110         hash_val += key->port2;
111
112         return hash_val;
113 }
114
115 /*
116  * Compare two conversation keys for an exact match.
117  */
118 static gint
119 conversation_match_exact(gconstpointer v, gconstpointer w)
120 {
121         const conversation_key *v1 = (const conversation_key *)v;
122         const conversation_key *v2 = (const conversation_key *)w;
123
124         if (v1->ptype != v2->ptype)
125                 return 0;       /* different types of port */
126
127         /*
128          * Are the first and second port 1 values the same, the first and
129          * second port 2 values the same, the first and second address
130          * 1 values the same, and the first and second address 2 values
131          * the same?
132          */
133         if (v1->port1 == v2->port1 &&
134             v1->port2 == v2->port2 &&
135             ADDRESSES_EQUAL(&v1->addr1, &v2->addr1) &&
136             ADDRESSES_EQUAL(&v1->addr2, &v2->addr2)) {
137                 /*
138                  * Yes.  It's the same conversation, and the two
139                  * address/port pairs are going in the same direction.
140                  */
141                 return 1;
142         }
143
144         /*
145          * Is the first port 2 the same as the second port 1, the first
146          * port 1 the same as the second port 2, the first address 2
147          * the same as the second address 1, and the first address 1
148          * the same as the second address 2?
149          */
150         if (v1->port2 == v2->port1 &&
151             v1->port1 == v2->port2 &&
152             ADDRESSES_EQUAL(&v1->addr2, &v2->addr1) &&
153             ADDRESSES_EQUAL(&v1->addr1, &v2->addr2)) {
154                 /*
155                  * Yes.  It's the same conversation, and the two
156                  * address/port pairs are going in opposite directions.
157                  */
158                 return 1;
159         }
160
161         /*
162          * The addresses or the ports don't match.
163          */
164         return 0;
165 }
166
167 /*
168  * Compute the hash value for two given address/port pairs if the match
169  * has a wildcard address 2.
170  */
171 static guint
172 conversation_hash_no_addr2(gconstpointer v)
173 {
174         const conversation_key *key = (const conversation_key *)v;
175         guint hash_val;
176         int i;
177
178         hash_val = 0;
179         for (i = 0; i < key->addr1.len; i++)
180                 hash_val += key->addr1.data[i];
181
182         hash_val += key->port1;
183
184         hash_val += key->port2;
185
186         return hash_val;
187 }
188
189 /*
190  * Compare two conversation keys, except for the address 2 value.
191  * We don't check both directions of the conversation - the routine
192  * doing the hash lookup has to do two searches, as the hash key
193  * will be different for the two directions.
194  */
195 static gint
196 conversation_match_no_addr2(gconstpointer v, gconstpointer w)
197 {
198         const conversation_key *v1 = (const conversation_key *)v;
199         const conversation_key *v2 = (const conversation_key *)w;
200
201         if (v1->ptype != v2->ptype)
202                 return 0;       /* different types of port */
203
204         /*
205          * Are the first and second port 1 values the same, the first and
206          * second port 2 valuess the same, and the first and second
207          * address 1 values the same?
208          */
209         if (v1->port1 == v2->port1 &&
210             v1->port2 == v2->port2 &&
211             ADDRESSES_EQUAL(&v1->addr1, &v2->addr1)) {
212                 /*
213                  * Yes.  It's the same conversation, and the two
214                  * address/port pairs are going in the same direction.
215                  */
216                 return 1;
217         }
218
219         /*
220          * The addresses or the ports don't match.
221          */
222         return 0;
223 }
224
225 /*
226  * Compute the hash value for two given address/port pairs if the match
227  * has a wildcard port 2.
228  */
229 static guint
230 conversation_hash_no_port2(gconstpointer v)
231 {
232         const conversation_key *key = (const conversation_key *)v;
233         guint hash_val;
234         int i;
235
236         hash_val = 0;
237         for (i = 0; i < key->addr1.len; i++)
238                 hash_val += key->addr1.data[i];
239
240         hash_val += key->port1;
241
242         for (i = 0; i < key->addr2.len; i++)
243                 hash_val += key->addr2.data[i];
244
245         return hash_val;
246 }
247
248 /*
249  * Compare two conversation keys, except for the port 2 value.
250  * We don't check both directions of the conversation - the routine
251  * doing the hash lookup has to do two searches, as the hash key
252  * will be different for the two directions.
253  */
254 static gint
255 conversation_match_no_port2(gconstpointer v, gconstpointer w)
256 {
257         const conversation_key *v1 = (const conversation_key *)v;
258         const conversation_key *v2 = (const conversation_key *)w;
259
260         if (v1->ptype != v2->ptype)
261                 return 0;       /* different types of port */
262
263         /*
264          * Are the first and second port 1 values the same, the first and
265          * second address 1 values the same, and the first and second
266          * address 2 values the same?
267          */
268         if (v1->port1 == v2->port1 &&
269             ADDRESSES_EQUAL(&v1->addr1, &v2->addr1) &&
270             ADDRESSES_EQUAL(&v1->addr2, &v2->addr2)) {
271                 /*
272                  * Yes.  It's the same conversation, and the two
273                  * address/port pairs are going in the same direction.
274                  */
275                 return 1;
276         }
277
278         /*
279          * The addresses or the ports don't match.
280          */
281         return 0;
282 }
283
284 /*
285  * Compute the hash value for two given address/port pairs if the match
286  * has a wildcard address 2 and port 2.
287  */
288 static guint
289 conversation_hash_no_addr2_or_port2(gconstpointer v)
290 {
291         const conversation_key *key = (const conversation_key *)v;
292         guint hash_val;
293         int i;
294
295         hash_val = 0;
296         for (i = 0; i < key->addr1.len; i++)
297                 hash_val += key->addr1.data[i];
298
299         hash_val += key->port1;
300
301         return hash_val;
302 }
303
304 /*
305  * Compare the address 1 and port 1 in the two conversation keys.
306  * We don't check both directions of the conversation - the routine
307  * doing the hash lookup has to do two searches, as the hash key
308  * will be different for the two directions.
309  */
310 static gint
311 conversation_match_no_addr2_or_port2(gconstpointer v, gconstpointer w)
312 {
313         const conversation_key *v1 = (const conversation_key *)v;
314         const conversation_key *v2 = (const conversation_key *)w;
315
316         if (v1->ptype != v2->ptype)
317                 return 0;       /* different types of port */
318
319         /*
320          * Are the first and second port 1 values the same and the first
321          * and second address 1 values the same?
322          */
323         if (v1->port1 == v2->port1 &&
324             ADDRESSES_EQUAL(&v1->addr1, &v2->addr1)) {
325                 /*
326                  * Yes.  It's the same conversation, and the two
327                  * address/port pairs are going in the same direction.
328                  */
329                 return 1;
330         }
331
332         /*
333          * The addresses or the ports don't match.
334          */
335         return 0;
336 }
337
338 /*
339  * Initialize some variables every time a file is loaded or re-loaded.
340  * Destroy all existing conversations, and create a new hash table
341  * for the conversations in the new file.
342  */
343 void
344 conversation_init(void)
345 {
346         conversation_key *key;
347
348         /*
349          * Free the addresses associated with the conversation keys.
350          */
351         for (key = conversation_keys; key != NULL; key = key->next) {
352                 /*
353                  * Grr.  I guess the theory here is that freeing
354                  * something sure as heck modifies it, so you
355                  * want to ban attempts to free it, but, alas,
356                  * if we make the "data" field of an "address"
357                  * structure not a "const", the compiler whines if
358                  * we try to make it point into the data for a packet,
359                  * as that's a "const" array (and should be, as dissectors
360                  * shouldn't trash it).
361                  *
362                  * So we cast the complaint into oblivion, and rely on
363                  * the fact that these addresses are known to have had
364                  * their data mallocated, i.e. they don't point into,
365                  * say, the middle of the data for a packet.
366                  */
367                 g_free((gpointer)key->addr1.data);
368                 g_free((gpointer)key->addr2.data);
369         }
370         conversation_keys = NULL;
371         if (conversation_hashtable_exact != NULL)
372                 g_hash_table_destroy(conversation_hashtable_exact);
373         if (conversation_hashtable_no_addr2 != NULL)
374                 g_hash_table_destroy(conversation_hashtable_no_addr2);
375         if (conversation_hashtable_no_port2 != NULL)
376                 g_hash_table_destroy(conversation_hashtable_no_port2);
377         if (conversation_hashtable_no_addr2_or_port2 != NULL)
378                 g_hash_table_destroy(conversation_hashtable_no_addr2_or_port2);
379         if (conversation_key_chunk != NULL)
380                 g_mem_chunk_destroy(conversation_key_chunk);
381         if (conversation_chunk != NULL)
382                 g_mem_chunk_destroy(conversation_chunk);
383
384         /*
385          * Free up any space allocated for conversation protocol data
386          * areas.
387          *
388          * We can free the space, as the structures it contains are
389          * pointed to by conversation data structures that were freed
390          * above.
391          */
392         if (conv_proto_data_area != NULL)
393                 g_mem_chunk_destroy(conv_proto_data_area);
394
395         conversation_hashtable_exact =
396             g_hash_table_new(conversation_hash_exact,
397               conversation_match_exact);
398         conversation_hashtable_no_addr2 =
399             g_hash_table_new(conversation_hash_no_addr2,
400               conversation_match_no_addr2);
401         conversation_hashtable_no_port2 =
402             g_hash_table_new(conversation_hash_no_port2,
403               conversation_match_no_port2);
404         conversation_hashtable_no_addr2_or_port2 =
405             g_hash_table_new(conversation_hash_no_addr2_or_port2,
406               conversation_match_no_addr2_or_port2);
407         conversation_key_chunk = g_mem_chunk_new("conversation_key_chunk",
408             sizeof(conversation_key),
409             conversation_init_count * sizeof(struct conversation_key),
410             G_ALLOC_AND_FREE);
411         conversation_chunk = g_mem_chunk_new("conversation_chunk",
412             sizeof(conversation_t),
413             conversation_init_count * sizeof(conversation_t),
414             G_ALLOC_AND_FREE);
415
416         /*
417          * Allocate a new area for conversation protocol data items.
418          */
419         conv_proto_data_area = g_mem_chunk_new("conv_proto_data_area",
420             sizeof(conv_proto_data), 20 * sizeof(conv_proto_data), /* FIXME*/
421             G_ALLOC_ONLY);
422
423         /*
424          * Start the conversation indices over at 0.
425          */
426         new_index = 0;
427 }
428
429 /*
430  * Given two address/port pairs for a packet, create a new conversation
431  * to contain packets between those address/port pairs.
432  *
433  * The options field is used to specify whether the address 2 value
434  * and/or port 2 value are not given and any value is acceptable
435  * when searching for this conversation.
436  */
437 conversation_t *
438 conversation_new(address *addr1, address *addr2, port_type ptype,
439     guint32 port1, guint32 port2, guint options)
440 {
441         conversation_t *conversation;
442         conversation_key *new_key;
443
444         new_key = g_mem_chunk_alloc(conversation_key_chunk);
445         new_key->next = conversation_keys;
446         conversation_keys = new_key;
447         COPY_ADDRESS(&new_key->addr1, addr1);
448         COPY_ADDRESS(&new_key->addr2, addr2);
449         new_key->ptype = ptype;
450         new_key->port1 = port1;
451         new_key->port2 = port2;
452
453         conversation = g_mem_chunk_alloc(conversation_chunk);
454         conversation->index = new_index;
455         conversation->data_list = NULL;
456
457 /* clear dissector handle */
458         conversation->dissector_handle = NULL;
459
460 /* set the options and key pointer */
461         conversation->options = options;
462         conversation->key_ptr = new_key;
463
464         new_index++;
465
466         if (options & NO_ADDR2) {
467                 if (options & (NO_PORT2|NO_PORT2_FORCE)) {
468                         g_hash_table_insert(conversation_hashtable_no_addr2_or_port2,
469                             new_key, conversation);
470                 } else {
471                         g_hash_table_insert(conversation_hashtable_no_addr2,
472                             new_key, conversation);
473                 }
474         } else {
475                 if (options & (NO_PORT2|NO_PORT2_FORCE)) {
476                         g_hash_table_insert(conversation_hashtable_no_port2,
477                             new_key, conversation);
478                 } else {
479                         g_hash_table_insert(conversation_hashtable_exact,
480                             new_key, conversation);
481                 }
482         }
483         return conversation;
484 }
485
486 /*
487  * Set the port 2 value in a key.  Remove the original from table,
488  * update the options and port values, insert the updated key.
489  */
490 void
491 conversation_set_port2(conversation_t *conv, guint32 port)
492 {
493         /*
494          * If the port 2 value is not wildcarded, don't set it.
495          */
496         if ((!(conv->options & NO_PORT2)) || (conv->options & NO_PORT2_FORCE))
497                 return;
498
499         if (conv->options & NO_ADDR2) {
500                 g_hash_table_remove(conversation_hashtable_no_addr2_or_port2,
501                     conv->key_ptr);
502         } else {
503                 g_hash_table_remove(conversation_hashtable_no_port2,
504                     conv->key_ptr);
505         }
506         conv->options &= ~NO_PORT2;
507         conv->key_ptr->port2  = port;
508         if (conv->options & NO_ADDR2) {
509                 g_hash_table_insert(conversation_hashtable_no_addr2,
510                     conv->key_ptr, conv);
511         } else {
512                 g_hash_table_insert(conversation_hashtable_exact,
513                     conv->key_ptr, conv);
514         }
515 }
516
517 /*
518  * Set the address 2 value in a key.  Remove the original from
519  * table, update the options and port values, insert the updated key.
520  */
521 void
522 conversation_set_addr2(conversation_t *conv, address *addr)
523 {
524         /*
525          * If the address 2 value is not wildcarded, don't set it.
526          */
527         if (!(conv->options & NO_ADDR2))
528                 return;
529
530         if (conv->options & NO_PORT2) {
531                 g_hash_table_remove(conversation_hashtable_no_addr2_or_port2,
532                     conv->key_ptr);
533         } else {
534                 g_hash_table_remove(conversation_hashtable_no_addr2,
535                     conv->key_ptr);
536         }
537         conv->options &= ~NO_ADDR2;
538         COPY_ADDRESS(&conv->key_ptr->addr2, addr);
539         if (conv->options & NO_PORT2) {
540                 g_hash_table_insert(conversation_hashtable_no_port2,
541                     conv->key_ptr, conv);
542         } else {
543                 g_hash_table_insert(conversation_hashtable_exact,
544                     conv->key_ptr, conv);
545         }
546 }
547
548 /*
549  * Search a particular hash table for a conversaton with the specified
550  * addr1, port1, addr2, and port2.
551  */
552 static conversation_t *
553 conversation_lookup_hashtable(GHashTable *hashtable, address *addr1, address *addr2,
554     port_type ptype, guint32 port1, guint32 port2)
555 {
556         conversation_key key;
557
558         /*
559          * We don't make a copy of the address data, we just copy the
560          * pointer to it, as "key" disappears when we return.
561          */
562         key.addr1 = *addr1;
563         key.addr2 = *addr2;
564         key.ptype = ptype;
565         key.port1 = port1;
566         key.port2 = port2;
567         return g_hash_table_lookup(hashtable, &key);
568 }
569
570
571 /*
572  * Given two address/port pairs for a packet, search for a conversation
573  * containing packets between those address/port pairs.  Returns NULL if
574  * not found.
575  *
576  * We try to find the most exact match that we can, and then proceed to
577  * try wildcard matches on the "addr_b" and/or "port_b" argument if a more
578  * exact match failed.
579  *
580  * Either or both of the "addr_b" and "port_b" arguments may be specified as
581  * a wildcard by setting the NO_ADDR_B or NO_PORT_B flags in the "options"
582  * argument.  We do only wildcard matches on addresses and ports specified
583  * as wildcards.
584  *
585  * I.e.:
586  *
587  *      if neither "addr_b" nor "port_b" were specified as wildcards, we
588  *      do an exact match (addr_a/port_a and addr_b/port_b) and, if that
589  *      succeeds, we return a pointer to the matched conversation;
590  *
591  *      otherwise, if "port_b" wasn't specified as a wildcard, we try to
592  *      match any address 2 with the specified port 2 (addr_a/port_a and
593  *      {any}/addr_b) and, if that succeeds, we return a pointer to the
594  *      matched conversation;
595  *
596  *      otherwise, if "addr_b" wasn't specified as a wildcard, we try to
597  *      match any port 2 with the specified address 2 (addr_a/port_a and
598  *      addr_b/{any}) and, if that succeeds, we return a pointer to the
599  *      matched conversation;
600  *
601  *      otherwise, we try to match any address 2 and any port 2
602  *      (addr_a/port_a and {any}/{any}) and, if that succeeds, we return
603  *      a pointer to the matched conversation;
604  *
605  *      otherwise, we found no matching conversation, and return NULL.
606  */
607 conversation_t *
608 find_conversation(address *addr_a, address *addr_b, port_type ptype,
609     guint32 port_a, guint32 port_b, guint options)
610 {
611         conversation_t *conversation;
612
613         /*
614          * First try an exact match, if we have two addresses and ports.
615          */
616         if (!(options & (NO_ADDR_B|NO_PORT_B))) {
617                 /*
618                  * Neither search address B nor search port B are wildcarded,
619                  * start out with an exact match.
620                  * Exact matches check both directions.
621                  */
622                 conversation =
623                     conversation_lookup_hashtable(conversation_hashtable_exact,
624                                                   addr_a, addr_b, ptype,
625                                                   port_a, port_b);
626                 if ((conversation == NULL) && (addr_a->type == AT_FC)) {
627                     /* In Fibre channel, OXID & RXID are never swapped as
628                      * TCP/UDP ports are in TCP/IP.
629                      */
630                     conversation =
631                         conversation_lookup_hashtable(conversation_hashtable_exact,
632                                                       addr_b, addr_a, ptype,
633                                                       port_a, port_b);
634                 }
635                 if (conversation != NULL)
636                         return conversation;
637         }
638
639         /*
640          * Well, that didn't find anything.  Try matches that wildcard
641          * one of the addresses, if we have two ports.
642          */
643         if (!(options & NO_PORT_B)) {
644                 /*
645                  * Search port B isn't wildcarded.
646                  *
647                  * First try looking for a conversation with the specified
648                  * address A and port A as the first address and port, and
649                  * with any address and the specified port B as the second
650                  * address and port.
651                  * ("addr_b" doesn't take part in this lookup.)
652                  */
653                 conversation =
654                     conversation_lookup_hashtable(conversation_hashtable_no_addr2,
655                         addr_a, addr_b, ptype, port_a, port_b);
656                 if ((conversation == NULL) && (addr_a->type == AT_FC)) {
657                     /* In Fibre channel, OXID & RXID are never swapped as
658                      * TCP/UDP ports are in TCP/IP.
659                      */
660                     conversation =
661                         conversation_lookup_hashtable(conversation_hashtable_no_addr2,
662                                                       addr_b, addr_a, ptype,
663                                                       port_a, port_b);
664                 }
665                 if (conversation != NULL) {
666                         /*
667                          * If search address B isn't wildcarded, and this
668                          * is for a connection-oriented protocol, set the
669                          * second address for this conversation to address
670                          * B, as that's the address that matched the
671                          * wildcarded second address for this conversation.
672                          *
673                          * (XXX - this assumes that, for all connection-
674                          * oriented protocols, the endpoints of a connection
675                          * have only one address each, i.e. you don't get
676                          * packets in a given direction coming from more than
677                          * one address.)
678                          */
679                         if (!(options & NO_ADDR_B) && ptype != PT_UDP)
680                                 conversation_set_addr2(conversation, addr_b);
681                         return conversation;
682                 }
683
684                 /*
685                  * Well, that didn't find anything.
686                  * If search address B was specified, try looking for a
687                  * conversation with the specified address B and port B as
688                  * the first address and port, and with any address and the
689                  * specified port A as the second address and port (this
690                  * packet may be going in the opposite direction from the
691                  * first packet in the conversation).
692                  * ("addr_a" doesn't take part in this lookup.)
693                  */
694                 if (!(options & NO_ADDR_B)) {
695                     conversation =
696                         conversation_lookup_hashtable(conversation_hashtable_no_addr2,
697                                                       addr_b, addr_a, ptype, port_b, port_a);
698                         if (conversation != NULL) {
699                                 /*
700                                  * If this is for a connection-oriented
701                                  * protocol, set the second address for
702                                  * this conversation to address A, as
703                                  * that's the address that matched the
704                                  * wildcarded second address for this
705                                  * conversation.
706                                  */
707                                 if (ptype != PT_UDP) {
708                                         conversation_set_addr2(conversation,
709                                             addr_a);
710                                 }
711                                 return conversation;
712                         }
713                 }
714         }
715
716         /*
717          * Well, that didn't find anything.  Try matches that wildcard
718          * one of the ports, if we have two addresses.
719          */
720         if (!(options & NO_ADDR_B)) {
721                 /*
722                  * Search address B isn't wildcarded.
723                  *
724                  * First try looking for a conversation with the specified
725                  * address A and port A as the first address and port, and
726                  * with the specified address B and any port as the second
727                  * address and port.
728                  * ("port_b" doesn't take part in this lookup.)
729                  */
730                 conversation =
731                     conversation_lookup_hashtable(conversation_hashtable_no_port2,
732                       addr_a, addr_b, ptype, port_a, port_b);
733                 if ((conversation == NULL) && (addr_a->type == AT_FC)) {
734                     /* In Fibre channel, OXID & RXID are never swapped as
735                      * TCP/UDP ports are in TCP/IP
736                      */
737                     conversation =
738                     conversation_lookup_hashtable(conversation_hashtable_no_port2,
739                       addr_b, addr_a, ptype, port_a, port_b);
740                 }
741                 if (conversation != NULL) {
742                         /*
743                          * If search port B isn't wildcarded, and this is
744                          * for a connection-oriented protocol, set the
745                          * second port for this conversation to port B,
746                          * as that's the port that matched the wildcarded
747                          * second port for this conversation.
748                          *
749                          * (XXX - this assumes that, for all connection-
750                          * oriented protocols, the endpoints of a connection
751                          * have only one port each, i.e. you don't get
752                          * packets in a given direction coming from more than
753                          * one port.)
754                          */
755                         if (!(options & NO_PORT_B) && ptype != PT_UDP)
756                                 conversation_set_port2(conversation, port_b);
757                         return conversation;
758                 }
759
760                 /*
761                  * Well, that didn't find anything.
762                  * If search port B was specified, try looking for a
763                  * conversation with the specified address B and port B
764                  * as the first address and port, and with the specified
765                  * address A and any port as the second address and port
766                  * (this packet may be going in the opposite direction
767                  * from the first packet in the conversation).
768                  * ("port_a" doesn't take part in this lookup.)
769                  */
770                 if (!(options & NO_PORT_B)) {
771                         conversation =
772                             conversation_lookup_hashtable(conversation_hashtable_no_port2,
773                               addr_b, addr_a, ptype, port_b, port_a);
774                         if (conversation != NULL) {
775                                 /*
776                                  * If this is for a connection-oriented
777                                  * protocol, set the second port for
778                                  * this conversation to port A, as
779                                  * that's the address that matched the
780                                  * wildcarded second address for this
781                                  * conversation.
782                                  */
783                                 if (ptype != PT_UDP) {
784                                         conversation_set_port2(conversation,
785                                             port_a);
786                                 }
787                                 return conversation;
788                         }
789                 }
790         }
791
792         /*
793          * Well, that didn't find anything.  Try matches that wildcard
794          * one address/port pair.
795          *
796          * First try looking for a conversation with the specified address A
797          * and port A as the first address and port.
798          * (Neither "addr_b" nor "port_b" take part in this lookup.)
799          */
800         conversation =
801             conversation_lookup_hashtable(conversation_hashtable_no_addr2_or_port2,
802               addr_a, addr_b, ptype, port_a, port_b);
803         if (conversation != NULL) {
804                 /*
805                  * If this is for a connection-oriented protocol:
806                  *
807                  *      if search address B isn't wildcarded, set the
808                  *      second address for this conversation to address
809                  *      B, as that's the address that matched the
810                  *      wildcarded second address for this conversation;
811                  *
812                  *      if search port B isn't wildcarded, set the
813                  *      second port for this conversation to port B,
814                  *      as that's the port that matched the wildcarded
815                  *      second port for this conversation.
816                  */
817                 if (ptype != PT_UDP) {
818                         if (!(options & NO_ADDR_B))
819                                 conversation_set_addr2(conversation, addr_b);
820                         if (!(options & NO_PORT_B))
821                                 conversation_set_port2(conversation, port_b);
822                 }
823                 return conversation;
824         }
825
826         /*
827          * Well, that didn't find anything.
828          * If search address and port B were specified, try looking for a
829          * conversation with the specified address B and port B as the
830          * first address and port, and with any second address and port
831          * (this packet may be going in the opposite direction from the
832          * first packet in the conversation).
833          * (Neither "addr_a" nor "port_a" take part in this lookup.)
834          */
835         if (addr_a->type == AT_FC)
836             conversation =
837                 conversation_lookup_hashtable(conversation_hashtable_no_addr2_or_port2,
838                                               addr_b, addr_a, ptype, port_a,
839                                               port_b);
840         else 
841             conversation =
842                 conversation_lookup_hashtable(conversation_hashtable_no_addr2_or_port2,
843                                               addr_b, addr_a, ptype, port_b,
844                                               port_a);
845         if (conversation != NULL) {
846                 /*
847                  * If this is for a connection-oriented protocol, set the
848                  * second address for this conversation to address A, as
849                  * that's the address that matched the wildcarded second
850                  * address for this conversation, and set the second port
851                  * for this conversation to port A, as that's the port
852                  * that matched the wildcarded second port for this
853                  * conversation.
854                  */
855                 if (ptype != PT_UDP) {
856                         conversation_set_addr2(conversation, addr_a);
857                         conversation_set_port2(conversation, port_a);
858                 }
859                 return conversation;
860         }
861
862         /*
863          * We found no conversation.
864          */
865         return NULL;
866 }
867
868 static gint
869 p_compare(gconstpointer a, gconstpointer b)
870 {
871         const conv_proto_data *ap = (const conv_proto_data *)a;
872         const conv_proto_data *bp = (const conv_proto_data *)b;
873
874         if (ap->proto > bp->proto)
875                 return 1;
876         else if (ap->proto == bp->proto)
877                 return 0;
878         else
879                 return -1;
880 }
881
882 void
883 conversation_add_proto_data(conversation_t *conv, int proto, void *proto_data)
884 {
885         conv_proto_data *p1 = g_mem_chunk_alloc(conv_proto_data_area);
886
887         p1->proto = proto;
888         p1->proto_data = proto_data;
889
890         /* Add it to the list of items for this conversation. */
891
892         conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
893             p_compare);
894 }
895
896 void *
897 conversation_get_proto_data(conversation_t *conv, int proto)
898 {
899         conv_proto_data temp, *p1;
900         GSList *item;
901
902         temp.proto = proto;
903         temp.proto_data = NULL;
904
905         item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
906             p_compare);
907
908         if (item != NULL) {
909                 p1 = (conv_proto_data *)item->data;
910                 return p1->proto_data;
911         }
912
913         return NULL;
914 }
915
916 void
917 conversation_delete_proto_data(conversation_t *conv, int proto)
918 {
919         conv_proto_data temp;
920         GSList *item;
921
922         temp.proto = proto;
923         temp.proto_data = NULL;
924
925         item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
926             p_compare);
927
928         if (item != NULL)
929                 conv->data_list = g_slist_remove(conv->data_list, item);
930 }
931
932 void
933 conversation_set_dissector(conversation_t *conversation,
934     dissector_handle_t handle)
935 {
936         conversation->dissector_handle = handle;
937 }
938
939 /*
940  * Given two address/port pairs for a packet, search for a matching
941  * conversation and, if found and it has a conversation dissector,
942  * call that dissector and return TRUE, otherwise return FALSE.
943  */
944 gboolean
945 try_conversation_dissector(address *addr_a, address *addr_b, port_type ptype,
946     guint32 port_a, guint32 port_b, tvbuff_t *tvb, packet_info *pinfo,
947     proto_tree *tree)
948 {
949         conversation_t *conversation;
950
951         conversation = find_conversation(addr_a, addr_b, ptype, port_a,
952             port_b, 0);
953
954         if (conversation != NULL) {
955                 if (conversation->dissector_handle == NULL)
956                         return FALSE;
957                 call_dissector(conversation->dissector_handle, tvb, pinfo,
958                     tree);
959                 return TRUE;
960         }
961         return FALSE;
962 }