Have GTPv2 pass its "instance ID" to "private extension" subdissectors rather than...
[metze/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  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <stdio.h>
28
29 #include <string.h>
30 #include <glib.h>
31 #include "packet.h"
32 #include "emem.h"
33 #include "conversation.h"
34
35 /*
36  * Hash table for conversations with no wildcards.
37  */
38 static GHashTable *conversation_hashtable_exact = NULL;
39
40 /*
41  * Hash table for conversations with one wildcard address.
42  */
43 static GHashTable *conversation_hashtable_no_addr2 = NULL;
44
45 /*
46  * Hash table for conversations with one wildcard port.
47  */
48 static GHashTable *conversation_hashtable_no_port2 = NULL;
49
50 /*
51  * Hash table for conversations with one wildcard address and port.
52  */
53 static GHashTable *conversation_hashtable_no_addr2_or_port2 = NULL;
54
55
56 #ifdef __NOT_USED__
57 typedef struct conversation_key {
58         struct conversation_key *next;
59         address addr1;
60         address addr2;
61         port_type ptype;
62         guint32 port1;
63         guint32 port2;
64 } conversation_key;
65 #endif
66 /*
67  * Linked list of conversation keys, so we can, before freeing them all,
68  * free the address data allocations associated with them.
69  */
70 static conversation_key *conversation_keys;
71
72 static guint32 new_index;
73
74 /*
75  * Protocol-specific data attached to a conversation_t structure - protocol
76  * index and opaque pointer.
77  */
78 typedef struct _conv_proto_data {
79         int     proto;
80         void    *proto_data;
81 } conv_proto_data;
82
83 /*
84  * Creates a new conversation with known endpoints based on a conversation
85  * created with the CONVERSATION_TEMPLATE option while keeping the
86  * conversation created with the CONVERSATION_TEMPLATE option so it can still
87  * match future connections.
88  *
89  * Passing a pointer to a conversation whose options mask does not include
90  * CONVERSATION_TEMPLATE or where the conversation's protocol type (ptype)
91  * indicates a non-connnection oriented protocol will return the conversation
92  * without changes.
93  *
94  * addr2 and port2 are used in the function if their respective conversation
95  * options bits are set (NO_ADDR2 and NO_PORT2).
96  */
97 static conversation_t *
98 conversation_create_from_template(conversation_t *conversation, const address *addr2, const guint32 port2)
99 {
100    /*
101     * Add a new conversation and keep the conversation template only if the
102     * CONVERSATION_TEMPLATE bit is set for a connection oriented protocol.
103     */
104    if(conversation->options & CONVERSATION_TEMPLATE &&
105       conversation->key_ptr->ptype != PT_UDP)
106    {
107       /*
108        * Set up a new options mask where the conversation template bit and the
109        * bits for absence of a second address and port pair have been removed.
110        */
111       conversation_t *new_conversation_from_template;
112       guint options = conversation->options & ~(CONVERSATION_TEMPLATE | NO_ADDR2 | NO_PORT2);
113
114       /*
115        * Are both the NO_ADDR2 and NO_PORT2 wildcards set in the options mask?
116        */
117       if(conversation->options & NO_ADDR2 &&
118          conversation->options & NO_PORT2)
119       {
120          /*
121           * The conversation template was created without knowledge of both
122           * the second address as well as the second port. Create a new
123           * conversation with new 2nd address and 2nd port.
124           */
125          new_conversation_from_template =
126             conversation_new(conversation->setup_frame,
127                              &conversation->key_ptr->addr1, addr2,
128                              conversation->key_ptr->ptype, conversation->key_ptr->port1,
129                              port2, options);
130       }
131       else if(conversation->options & NO_PORT2)
132       {
133          /*
134           * The conversation template was created without knowledge of port 2
135           * only. Create a new conversation with new 2nd port.
136           */
137          new_conversation_from_template =
138             conversation_new(conversation->setup_frame,
139                              &conversation->key_ptr->addr1, &conversation->key_ptr->addr2,
140                              conversation->key_ptr->ptype, conversation->key_ptr->port1,
141                              port2, options);
142       }
143       else if(conversation->options & NO_ADDR2)
144       {
145          /*
146           * The conversation template was created without knowledge of address
147           * 2. Create a new conversation with new 2nd address.
148           */
149          new_conversation_from_template =
150             conversation_new(conversation->setup_frame,
151                              &conversation->key_ptr->addr1, addr2,
152                              conversation->key_ptr->ptype, conversation->key_ptr->port1,
153                              conversation->key_ptr->port2, options);
154       }
155       else
156       {
157          /*
158           * The CONVERSATION_TEMPLATE bit was set, but no other bit that the
159           * CONVERSATION_TEMPLATE bit controls is active. Just return the old
160           * conversation.
161           */
162          return conversation;
163       }
164
165       /*
166        * Set the protocol dissector used for the template conversation as
167        * the handler of the new conversation as well.
168        */
169       new_conversation_from_template->dissector_handle = conversation->dissector_handle;
170
171       return new_conversation_from_template;
172    }
173    else
174    {
175       return conversation;
176    }
177 }
178
179 /*
180  * Compute the hash value for two given address/port pairs if the match
181  * is to be exact.
182  */
183 /* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing 
184  * One-at-a-Time hash
185  */
186 static guint
187 conversation_hash_exact(gconstpointer v)
188 {
189         const conversation_key *key = (const conversation_key *)v;
190         guint hash_val;
191         address tmp_addr;
192
193         hash_val = 0;
194         tmp_addr.len  = 4;
195
196         ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
197
198         tmp_addr.data = &key->port1;
199         ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
200
201         ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
202
203         tmp_addr.data = &key->port2;
204         ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
205
206         hash_val += ( hash_val << 3 );
207         hash_val ^= ( hash_val >> 11 );
208         hash_val += ( hash_val << 15 );
209
210         return hash_val;
211 }
212
213 /*
214  * Compare two conversation keys for an exact match.
215  */
216 static gint
217 conversation_match_exact(gconstpointer v, gconstpointer w)
218 {
219         const conversation_key *v1 = (const conversation_key *)v;
220         const conversation_key *v2 = (const conversation_key *)w;
221
222         if (v1->ptype != v2->ptype)
223                 return 0;       /* different types of port */
224
225         /*
226          * Are the first and second port 1 values the same, the first and
227          * second port 2 values the same, the first and second address
228          * 1 values the same, and the first and second address 2 values
229          * the same?
230          */
231         if (v1->port1 == v2->port1 &&
232             v1->port2 == v2->port2 &&
233             ADDRESSES_EQUAL(&v1->addr1, &v2->addr1) &&
234             ADDRESSES_EQUAL(&v1->addr2, &v2->addr2)) {
235                 /*
236                  * Yes.  It's the same conversation, and the two
237                  * address/port pairs are going in the same direction.
238                  */
239                 return 1;
240         }
241
242         /*
243          * Is the first port 2 the same as the second port 1, the first
244          * port 1 the same as the second port 2, the first address 2
245          * the same as the second address 1, and the first address 1
246          * the same as the second address 2?
247          */
248         if (v1->port2 == v2->port1 &&
249             v1->port1 == v2->port2 &&
250             ADDRESSES_EQUAL(&v1->addr2, &v2->addr1) &&
251             ADDRESSES_EQUAL(&v1->addr1, &v2->addr2)) {
252                 /*
253                  * Yes.  It's the same conversation, and the two
254                  * address/port pairs are going in opposite directions.
255                  */
256                 return 1;
257         }
258
259         /*
260          * The addresses or the ports don't match.
261          */
262         return 0;
263 }
264
265 /*
266  * Compute the hash value for two given address/port pairs if the match
267  * has a wildcard address 2.
268  */
269 static guint
270 conversation_hash_no_addr2(gconstpointer v)
271 {
272         const conversation_key *key = (const conversation_key *)v;
273         guint hash_val;
274         address tmp_addr;
275
276         hash_val = 0;
277         tmp_addr.len  = 4;
278
279         ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
280
281         tmp_addr.data = &key->port1;
282         ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
283
284         tmp_addr.data = &key->port2;
285         ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
286
287         hash_val += ( hash_val << 3 );
288         hash_val ^= ( hash_val >> 11 );
289         hash_val += ( hash_val << 15 );
290
291         return hash_val;
292 }
293
294 /*
295  * Compare two conversation keys, except for the address 2 value.
296  * We don't check both directions of the conversation - the routine
297  * doing the hash lookup has to do two searches, as the hash key
298  * will be different for the two directions.
299  */
300 static gint
301 conversation_match_no_addr2(gconstpointer v, gconstpointer w)
302 {
303         const conversation_key *v1 = (const conversation_key *)v;
304         const conversation_key *v2 = (const conversation_key *)w;
305
306         if (v1->ptype != v2->ptype)
307                 return 0;       /* different types of port */
308
309         /*
310          * Are the first and second port 1 values the same, the first and
311          * second port 2 valuess the same, and the first and second
312          * address 1 values the same?
313          */
314         if (v1->port1 == v2->port1 &&
315             v1->port2 == v2->port2 &&
316             ADDRESSES_EQUAL(&v1->addr1, &v2->addr1)) {
317                 /*
318                  * Yes.  It's the same conversation, and the two
319                  * address/port pairs are going in the same direction.
320                  */
321                 return 1;
322         }
323
324         /*
325          * The addresses or the ports don't match.
326          */
327         return 0;
328 }
329
330 /*
331  * Compute the hash value for two given address/port pairs if the match
332  * has a wildcard port 2.
333  */
334 static guint
335 conversation_hash_no_port2(gconstpointer v)
336 {
337         const conversation_key *key = (const conversation_key *)v;
338         guint hash_val;
339         address tmp_addr;
340
341         hash_val = 0;
342         tmp_addr.len  = 4;
343
344         ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
345
346         tmp_addr.data = &key->port1;
347         ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
348
349         ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
350
351         hash_val += ( hash_val << 3 );
352         hash_val ^= ( hash_val >> 11 );
353         hash_val += ( hash_val << 15 );
354
355         return hash_val;
356 }
357
358 /*
359  * Compare two conversation keys, except for the port 2 value.
360  * We don't check both directions of the conversation - the routine
361  * doing the hash lookup has to do two searches, as the hash key
362  * will be different for the two directions.
363  */
364 static gint
365 conversation_match_no_port2(gconstpointer v, gconstpointer w)
366 {
367         const conversation_key *v1 = (const conversation_key *)v;
368         const conversation_key *v2 = (const conversation_key *)w;
369
370         if (v1->ptype != v2->ptype)
371                 return 0;       /* different types of port */
372
373         /*
374          * Are the first and second port 1 values the same, the first and
375          * second address 1 values the same, and the first and second
376          * address 2 values the same?
377          */
378         if (v1->port1 == v2->port1 &&
379             ADDRESSES_EQUAL(&v1->addr1, &v2->addr1) &&
380             ADDRESSES_EQUAL(&v1->addr2, &v2->addr2)) {
381                 /*
382                  * Yes.  It's the same conversation, and the two
383                  * address/port pairs are going in the same direction.
384                  */
385                 return 1;
386         }
387
388         /*
389          * The addresses or the ports don't match.
390          */
391         return 0;
392 }
393
394 /*
395  * Compute the hash value for two given address/port pairs if the match
396  * has a wildcard address 2 and port 2.
397  */
398 static guint
399 conversation_hash_no_addr2_or_port2(gconstpointer v)
400 {
401         const conversation_key *key = (const conversation_key *)v;
402         guint hash_val;
403         address tmp_addr;
404
405         hash_val = 0;
406         tmp_addr.len  = 4;
407
408         ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
409
410         tmp_addr.data = &key->port1;
411         ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
412
413         hash_val += ( hash_val << 3 );
414         hash_val ^= ( hash_val >> 11 );
415         hash_val += ( hash_val << 15 );
416
417         return hash_val;
418 }
419
420 /*
421  * Compare the address 1 and port 1 in the two conversation keys.
422  * We don't check both directions of the conversation - the routine
423  * doing the hash lookup has to do two searches, as the hash key
424  * will be different for the two directions.
425  */
426 static gint
427 conversation_match_no_addr2_or_port2(gconstpointer v, gconstpointer w)
428 {
429         const conversation_key *v1 = (const conversation_key *)v;
430         const conversation_key *v2 = (const conversation_key *)w;
431
432         if (v1->ptype != v2->ptype)
433                 return 0;       /* different types of port */
434
435         /*
436          * Are the first and second port 1 values the same and the first
437          * and second address 1 values the same?
438          */
439         if (v1->port1 == v2->port1 &&
440             ADDRESSES_EQUAL(&v1->addr1, &v2->addr1)) {
441                 /*
442                  * Yes.  It's the same conversation, and the two
443                  * address/port pairs are going in the same direction.
444                  */
445                 return 1;
446         }
447
448         /*
449          * The addresses or the ports don't match.
450          */
451         return 0;
452 }
453
454 /*
455  * Free the proto_data.  The conversation itself is se_allocated.
456  */
457 static void
458 free_data_list(gpointer key _U_, gpointer value, gpointer user_data _U_)
459 {
460         conversation_t *conv = (conversation_t *)value;
461
462         /* TODO: se_slist? */
463         g_slist_free(conv->data_list);
464
465         /* Not really necessary, but... */
466         conv->data_list = NULL;
467
468 }
469
470 /*
471  * Destroy all existing conversations
472  */
473 void
474 conversation_cleanup(void)
475 {
476         /*  Clean up the hash tables, but only after freeing any proto_data
477          *  that may be hanging off the conversations.
478          *  The conversation keys are se_ allocated so we don't have to clean them up.
479          */
480         conversation_keys = NULL;
481         if (conversation_hashtable_exact != NULL) {
482                 g_hash_table_foreach(conversation_hashtable_exact, free_data_list, NULL);
483                 g_hash_table_destroy(conversation_hashtable_exact);
484         }
485         if (conversation_hashtable_no_addr2 != NULL) {
486                 g_hash_table_foreach(conversation_hashtable_no_addr2, free_data_list, NULL);
487                 g_hash_table_destroy(conversation_hashtable_no_addr2);
488         }
489         if (conversation_hashtable_no_port2 != NULL) {
490                 g_hash_table_foreach(conversation_hashtable_no_port2, free_data_list, NULL);
491                 g_hash_table_destroy(conversation_hashtable_no_port2);
492         }
493         if (conversation_hashtable_no_addr2_or_port2 != NULL) {
494                 g_hash_table_foreach(conversation_hashtable_no_addr2_or_port2, free_data_list, NULL);
495                 g_hash_table_destroy(conversation_hashtable_no_addr2_or_port2);
496         }
497
498         conversation_hashtable_exact = NULL;
499         conversation_hashtable_no_addr2 = NULL;
500         conversation_hashtable_no_port2 = NULL;
501         conversation_hashtable_no_addr2_or_port2 = NULL;
502 }
503
504 /*
505  * Initialize some variables every time a file is loaded or re-loaded.
506  * Create a new hash table for the conversations in the new file.
507  */
508 void
509 conversation_init(void)
510 {
511         /*
512          * Free up any space allocated for conversation protocol data
513          * areas.
514          *
515          * We can free the space, as the structures it contains are
516          * pointed to by conversation data structures that were freed
517          * above.
518          */
519         conversation_hashtable_exact =
520             g_hash_table_new(conversation_hash_exact,
521               conversation_match_exact);
522         conversation_hashtable_no_addr2 =
523             g_hash_table_new(conversation_hash_no_addr2,
524               conversation_match_no_addr2);
525         conversation_hashtable_no_port2 =
526             g_hash_table_new(conversation_hash_no_port2,
527               conversation_match_no_port2);
528         conversation_hashtable_no_addr2_or_port2 =
529             g_hash_table_new(conversation_hash_no_addr2_or_port2,
530               conversation_match_no_addr2_or_port2);
531
532         /*
533          * Start the conversation indices over at 0.
534          */
535         new_index = 0;
536 }
537
538 /*
539  * Does the right thing when inserting into one of the conversation hash tables,
540  * taking into account ordering and hash chains and all that good stuff.
541  *
542  * Mostly adapted from the old conversation_new().
543  */
544 void
545 conversation_insert_into_hashtable(GHashTable *hashtable, conversation_t *conv)
546 {
547         conversation_t *chain_head, *chain_tail, *cur, *prev;
548
549         chain_head = (conversation_t *)g_hash_table_lookup(hashtable, conv->key_ptr);
550
551         if (NULL==chain_head) {
552                 /* New entry */
553                 conv->next = NULL;
554                 conv->last = conv;
555                 g_hash_table_insert(hashtable, conv->key_ptr, conv);
556         }
557         else {
558                 /* There's an existing chain for this key */
559
560                 chain_tail = chain_head->last;
561
562                 if(conv->setup_frame >= chain_tail->setup_frame) {
563                         /* This convo belongs at the end of the chain */
564                         conv->next = NULL;
565                         conv->last = NULL;
566                         chain_tail->next = conv;
567                         chain_head->last = conv;
568                 }
569                 else {
570                         /* Loop through the chain to find the right spot */
571                         cur = chain_head;
572                         prev = NULL;
573
574                         for (; (conv->setup_frame > cur->setup_frame) && cur->next; prev=cur, cur=cur->next)
575                                 ;
576
577                         if (NULL==prev) {
578                                 /* Changing the head of the chain */
579                                 conv->next = chain_head;
580                                 conv->last = chain_tail;
581                                 chain_head->last = NULL;
582                                 g_hash_table_insert(hashtable, conv->key_ptr, conv);
583                         }
584                         else {
585                                 /* Inserting into the middle of the chain */
586                                 conv->next = cur;
587                                 conv->last = NULL;
588                                 prev->next = conv;
589                         }
590                 }
591         }
592 }
593
594 /*
595  * Does the right thing when removing from one of the conversation hash tables,
596  * taking into account ordering and hash chains and all that good stuff.
597  */
598 void
599 conversation_remove_from_hashtable(GHashTable *hashtable, conversation_t *conv)
600 {
601         conversation_t *chain_head, *cur, *prev;
602
603         chain_head = (conversation_t *)g_hash_table_lookup(hashtable, conv->key_ptr);
604
605         if (conv == chain_head) {
606                 /* We are currently the front of the chain */
607                 if (NULL == conv->next) {
608                         /* We are the only conversation in the chain */
609                         g_hash_table_remove(hashtable, conv->key_ptr);
610                 }
611                 else {
612                         /* Update the head of the chain */
613                         chain_head = conv->next;
614                         chain_head->last = conv->last;
615
616                         if (conv->latest_found == conv)
617                                 chain_head->latest_found = NULL;
618                         else
619                                 chain_head->latest_found = conv->latest_found;
620
621                         g_hash_table_insert(hashtable, chain_head->key_ptr, chain_head);
622                 }
623         }
624         else {
625                 /* We are not the front of the chain. Loop through to find us.
626                  * Start loop at chain_head->next rather than chain_head because
627                  * we already know we're not at the head. */
628                 cur = chain_head->next;
629                 prev = chain_head;
630
631                 for (; (cur != conv) && cur->next; prev=cur, cur=cur->next)
632                         ;
633
634                 if (cur != conv) {
635                         /* XXX: Conversation not found. Wrong hashtable? */
636                         return;
637                 }
638
639                 prev->next = conv->next;
640
641                 if (NULL == conv->next) {
642                         /* We're at the very end of the list. */
643                         chain_head->last = prev;
644                 }
645
646                 if (chain_head->latest_found == conv)
647                         chain_head->latest_found = prev;
648         }
649 }
650
651 /*
652  * Given two address/port pairs for a packet, create a new conversation
653  * to contain packets between those address/port pairs.
654  *
655  * The options field is used to specify whether the address 2 value
656  * and/or port 2 value are not given and any value is acceptable
657  * when searching for this conversation.
658  */
659 conversation_t *
660 conversation_new(const guint32 setup_frame, const address *addr1, const address *addr2, const port_type ptype,
661     const guint32 port1, const guint32 port2, const guint options)
662 {
663 /*
664         DISSECTOR_ASSERT(!(options | CONVERSATION_TEMPLATE) || ((options | (NO_ADDR2 | NO_PORT2 | NO_PORT2_FORCE))) &&
665                                 "A conversation template may not be constructed without wildcard options");
666 */
667         GHashTable* hashtable;
668         conversation_t *conversation=NULL;
669         conversation_key *new_key;
670
671         if (options & NO_ADDR2) {
672                 if (options & (NO_PORT2|NO_PORT2_FORCE)) {
673                         hashtable = conversation_hashtable_no_addr2_or_port2;
674                 } else {
675                         hashtable = conversation_hashtable_no_addr2;
676                 }
677         } else {
678                 if (options & (NO_PORT2|NO_PORT2_FORCE)) {
679                         hashtable = conversation_hashtable_no_port2;
680                 } else {
681                         hashtable = conversation_hashtable_exact;
682                 }
683         }
684
685         new_key = se_new(struct conversation_key);
686         new_key->next = conversation_keys;
687         conversation_keys = new_key;
688         SE_COPY_ADDRESS(&new_key->addr1, addr1);
689         SE_COPY_ADDRESS(&new_key->addr2, addr2);
690         new_key->ptype = ptype;
691         new_key->port1 = port1;
692         new_key->port2 = port2;
693
694         conversation = se_new(conversation_t); 
695         memset(conversation, 0, sizeof(conversation_t));
696
697         conversation->index = new_index;
698         conversation->setup_frame = conversation->last_frame = setup_frame;
699         conversation->data_list = NULL;
700
701         /* clear dissector handle */
702         conversation->dissector_handle = NULL;
703
704         /* set the options and key pointer */
705         conversation->options = options;
706         conversation->key_ptr = new_key;
707
708         new_index++;
709
710         conversation_insert_into_hashtable(hashtable, conversation);
711
712         return conversation;
713 }
714
715 /*
716  * Set the port 2 value in a key.  Remove the original from table,
717  * update the options and port values, insert the updated key.
718  */
719 void
720 conversation_set_port2(conversation_t *conv, const guint32 port)
721 {
722    DISSECTOR_ASSERT_HINT(!(conv->options & CONVERSATION_TEMPLATE),
723             "Use the conversation_create_from_template function when the CONVERSATION_TEMPLATE bit is set in the options mask");
724
725         /*
726          * If the port 2 value is not wildcarded, don't set it.
727          */
728         if ((!(conv->options & NO_PORT2)) || (conv->options & NO_PORT2_FORCE))
729                 return;
730
731         if (conv->options & NO_ADDR2) {
732                 conversation_remove_from_hashtable(conversation_hashtable_no_addr2_or_port2, conv);
733         } else {
734                 conversation_remove_from_hashtable(conversation_hashtable_no_port2, conv);
735         }
736         conv->options &= ~NO_PORT2;
737         conv->key_ptr->port2  = port;
738         if (conv->options & NO_ADDR2) {
739                 conversation_insert_into_hashtable(conversation_hashtable_no_addr2, conv);
740         } else {
741                 conversation_insert_into_hashtable(conversation_hashtable_exact, conv);
742         }
743 }
744
745 /*
746  * Set the address 2 value in a key.  Remove the original from
747  * table, update the options and port values, insert the updated key.
748  */
749 void
750 conversation_set_addr2(conversation_t *conv, const address *addr)
751 {
752    DISSECTOR_ASSERT_HINT(!(conv->options & CONVERSATION_TEMPLATE),
753             "Use the conversation_create_from_template function when the CONVERSATION_TEMPLATE bit is set in the options mask");
754
755         /*
756          * If the address 2 value is not wildcarded, don't set it.
757          */
758         if (!(conv->options & NO_ADDR2))
759                 return;
760
761         if (conv->options & NO_PORT2) {
762                 conversation_remove_from_hashtable(conversation_hashtable_no_addr2_or_port2, conv);
763         } else {
764                 conversation_remove_from_hashtable(conversation_hashtable_no_port2, conv);
765         }
766         conv->options &= ~NO_ADDR2;
767         SE_COPY_ADDRESS(&conv->key_ptr->addr2, addr);
768         if (conv->options & NO_PORT2) {
769                 conversation_insert_into_hashtable(conversation_hashtable_no_port2, conv);
770         } else {
771                 conversation_insert_into_hashtable(conversation_hashtable_exact, conv);
772         }
773 }
774
775 /*
776  * Search a particular hash table for a conversation with the specified
777  * {addr1, port1, addr2, port2} and set up before frame_num.
778  */
779 static conversation_t *
780 conversation_lookup_hashtable(GHashTable *hashtable, const guint32 frame_num, const address *addr1, const address *addr2,
781     const port_type ptype, const guint32 port1, const guint32 port2)
782 {
783         conversation_t* convo=NULL;
784         conversation_t* match=NULL;
785         conversation_t* chain_head=NULL;
786         conversation_key key;
787
788         /*
789          * We don't make a copy of the address data, we just copy the
790          * pointer to it, as "key" disappears when we return.
791          */
792         key.addr1 = *addr1;
793         key.addr2 = *addr2;
794         key.ptype = ptype;
795         key.port1 = port1;
796         key.port2 = port2;
797
798         chain_head = (conversation_t *)g_hash_table_lookup(hashtable, &key);
799
800         if (chain_head && (chain_head->setup_frame <= frame_num)) {
801                 match = chain_head;
802
803                 if((chain_head->last)&&(chain_head->last->setup_frame<=frame_num))
804                         return chain_head->last;
805
806                 if((chain_head->latest_found)&&(chain_head->latest_found->setup_frame<=frame_num))
807                         match = chain_head->latest_found;
808
809                 for (convo = match; convo && convo->setup_frame <= frame_num; convo = convo->next) {
810                         if (convo->setup_frame > match->setup_frame) {
811                                 match = convo;
812                         }
813                 }
814         }
815
816     if (match)
817         chain_head->latest_found = match;
818
819         return match;
820 }
821
822
823 /*
824  * Given two address/port pairs for a packet, search for a conversation
825  * containing packets between those address/port pairs.  Returns NULL if
826  * not found.
827  *
828  * We try to find the most exact match that we can, and then proceed to
829  * try wildcard matches on the "addr_b" and/or "port_b" argument if a more
830  * exact match failed.
831  *
832  * Either or both of the "addr_b" and "port_b" arguments may be specified as
833  * a wildcard by setting the NO_ADDR_B or NO_PORT_B flags in the "options"
834  * argument.  We do only wildcard matches on addresses and ports specified
835  * as wildcards.
836  *
837  * I.e.:
838  *
839  *      if neither "addr_b" nor "port_b" were specified as wildcards, we
840  *      do an exact match (addr_a/port_a and addr_b/port_b) and, if that
841  *      succeeds, we return a pointer to the matched conversation;
842  *
843  *      otherwise, if "port_b" wasn't specified as a wildcard, we try to
844  *      match any address 2 with the specified port 2 (addr_a/port_a and
845  *      {any}/addr_b) and, if that succeeds, we return a pointer to the
846  *      matched conversation;
847  *
848  *      otherwise, if "addr_b" wasn't specified as a wildcard, we try to
849  *      match any port 2 with the specified address 2 (addr_a/port_a and
850  *      addr_b/{any}) and, if that succeeds, we return a pointer to the
851  *      matched conversation;
852  *
853  *      otherwise, we try to match any address 2 and any port 2
854  *      (addr_a/port_a and {any}/{any}) and, if that succeeds, we return
855  *      a pointer to the matched conversation;
856  *
857  *      otherwise, we found no matching conversation, and return NULL.
858  */
859 conversation_t *
860 find_conversation(const guint32 frame_num, const address *addr_a, const address *addr_b, const port_type ptype,
861     const guint32 port_a, const guint32 port_b, const guint options)
862 {
863    conversation_t *conversation;
864
865    /*
866     * First try an exact match, if we have two addresses and ports.
867     */
868    if (!(options & (NO_ADDR_B|NO_PORT_B))) {
869       /*
870        * Neither search address B nor search port B are wildcarded,
871        * start out with an exact match.
872        */
873       conversation =
874          conversation_lookup_hashtable(conversation_hashtable_exact,
875          frame_num, addr_a, addr_b, ptype,
876          port_a, port_b);
877       /* Didn't work, try the other direction */
878       if (conversation == NULL) {
879               conversation =
880                  conversation_lookup_hashtable(conversation_hashtable_exact,
881                  frame_num, addr_b, addr_a, ptype,
882                  port_b, port_a);
883       }
884       if ((conversation == NULL) && (addr_a->type == AT_FC)) {
885          /* In Fibre channel, OXID & RXID are never swapped as
886           * TCP/UDP ports are in TCP/IP.
887           */
888          conversation =
889             conversation_lookup_hashtable(conversation_hashtable_exact,
890             frame_num, addr_b, addr_a, ptype,
891             port_a, port_b);
892       }
893       if (conversation != NULL)
894          return conversation;
895    }
896
897    /*
898     * Well, that didn't find anything.  Try matches that wildcard
899     * one of the addresses, if we have two ports.
900     */
901    if (!(options & NO_PORT_B)) {
902       /*
903        * Search port B isn't wildcarded.
904        *
905        * First try looking for a conversation with the specified
906        * address A and port A as the first address and port, and
907        * with any address and the specified port B as the second
908        * address and port.
909        * ("addr_b" doesn't take part in this lookup.)
910        */
911       conversation =
912          conversation_lookup_hashtable(conversation_hashtable_no_addr2,
913          frame_num, addr_a, addr_b, ptype, port_a, port_b);
914       if ((conversation == NULL) && (addr_a->type == AT_FC)) {
915          /* In Fibre channel, OXID & RXID are never swapped as
916           * TCP/UDP ports are in TCP/IP.
917           */
918          conversation =
919             conversation_lookup_hashtable(conversation_hashtable_no_addr2,
920             frame_num, addr_b, addr_a, ptype,
921             port_a, port_b);
922       }
923       if (conversation != NULL) {
924          /*
925           * If search address B isn't wildcarded, and this is for a
926           * connection-oriented protocol, set the second address for this
927           * conversation to address B, as that's the address that matched the
928           * wildcarded second address for this conversation.
929           *
930           * (This assumes that, for all connection oriented protocols, the
931           * endpoints of a connection have only one address each, i.e. you
932           * don't get packets in a given direction coming from more than one
933           * address, unless the CONVERSATION_TEMPLATE option is set.)
934           */
935          if (!(conversation->options & NO_ADDR_B) && ptype != PT_UDP)
936          {
937             if(!(conversation->options & CONVERSATION_TEMPLATE))
938             {
939                conversation_set_addr2(conversation, addr_b);
940             }
941             else
942             {
943                conversation =
944                   conversation_create_from_template(conversation, addr_b, 0);
945             }
946          }
947          return conversation;
948       }
949
950       /*
951        * Well, that didn't find anything.
952        * If search address B was specified, try looking for a
953        * conversation with the specified address B and port B as
954        * the first address and port, and with any address and the
955        * specified port A as the second address and port (this
956        * packet may be going in the opposite direction from the
957        * first packet in the conversation).
958        * ("addr_a" doesn't take part in this lookup.)
959        */
960       if (!(options & NO_ADDR_B)) {
961          conversation =
962             conversation_lookup_hashtable(conversation_hashtable_no_addr2,
963             frame_num, addr_b, addr_a, ptype, port_b, port_a);
964          if (conversation != NULL) {
965             /*
966              * If this is for a connection-oriented
967              * protocol, set the second address for
968              * this conversation to address A, as
969              * that's the address that matched the
970              * wildcarded second address for this
971              * conversation.
972              */
973             if (ptype != PT_UDP) {
974                if(!(conversation->options & CONVERSATION_TEMPLATE))
975                {
976                   conversation_set_addr2(conversation, addr_a);
977                }
978                else
979                {
980                   conversation =
981                      conversation_create_from_template(conversation, addr_a, 0);
982                }
983             }
984             return conversation;
985          }
986       }
987    }
988
989    /*
990     * Well, that didn't find anything.  Try matches that wildcard
991     * one of the ports, if we have two addresses.
992    */
993    if (!(options & NO_ADDR_B)) {
994       /*
995        * Search address B isn't wildcarded.
996        *
997        * First try looking for a conversation with the specified
998        * address A and port A as the first address and port, and
999        * with the specified address B and any port as the second
1000        * address and port.
1001        * ("port_b" doesn't take part in this lookup.)
1002        */
1003       conversation =
1004          conversation_lookup_hashtable(conversation_hashtable_no_port2,
1005          frame_num, addr_a, addr_b, ptype, port_a, port_b);
1006       if ((conversation == NULL) && (addr_a->type == AT_FC)) {
1007          /* In Fibre channel, OXID & RXID are never swapped as
1008           * TCP/UDP ports are in TCP/IP
1009           */
1010          conversation =
1011             conversation_lookup_hashtable(conversation_hashtable_no_port2,
1012             frame_num, addr_b, addr_a, ptype, port_a, port_b);
1013       }
1014       if (conversation != NULL) {
1015          /*
1016           * If search port B isn't wildcarded, and this is for a connection-
1017           * oriented protocol, set the second port for this conversation to
1018           * port B, as that's the port that matched the wildcarded second port
1019           * for this conversation.
1020           *
1021           * (This assumes that, for all connection oriented protocols, the
1022           * endpoints of a connection have only one port each, i.e. you don't
1023           * get packets in a given direction coming from more than one port,
1024           * unless the CONVERSATION_TEMPLATE option is set.)
1025           */
1026          if (!(conversation->options & NO_PORT_B) && ptype != PT_UDP)
1027          {
1028             if(!(conversation->options & CONVERSATION_TEMPLATE))
1029             {
1030                conversation_set_port2(conversation, port_b);
1031             }
1032             else
1033             {
1034                conversation =
1035                   conversation_create_from_template(conversation, 0, port_b);
1036             }
1037          }
1038          return conversation;
1039       }
1040
1041       /*
1042        * Well, that didn't find anything.
1043        * If search port B was specified, try looking for a
1044        * conversation with the specified address B and port B
1045        * as the first address and port, and with the specified
1046        * address A and any port as the second address and port
1047        * (this packet may be going in the opposite direction
1048        * from the first packet in the conversation).
1049        * ("port_a" doesn't take part in this lookup.)
1050        */
1051       if (!(options & NO_PORT_B)) {
1052          conversation =
1053             conversation_lookup_hashtable(conversation_hashtable_no_port2,
1054             frame_num, addr_b, addr_a, ptype, port_b, port_a);
1055          if (conversation != NULL) {
1056             /*
1057              * If this is for a connection-oriented
1058              * protocol, set the second port for
1059              * this conversation to port A, as
1060              * that's the address that matched the
1061              * wildcarded second address for this
1062              * conversation.
1063              */
1064             if (ptype != PT_UDP)
1065             {
1066                if(!(conversation->options & CONVERSATION_TEMPLATE))
1067                {
1068                   conversation_set_port2(conversation, port_a);
1069                }
1070                else
1071                {
1072                   conversation =
1073                      conversation_create_from_template(conversation, 0, port_a);
1074                }
1075             }
1076             return conversation;
1077          }
1078       }
1079    }
1080
1081    /*
1082     * Well, that didn't find anything.  Try matches that wildcard
1083     * one address/port pair.
1084     *
1085     * First try looking for a conversation with the specified address A
1086     * and port A as the first address and port.
1087     * (Neither "addr_b" nor "port_b" take part in this lookup.)
1088     */
1089    conversation =
1090       conversation_lookup_hashtable(conversation_hashtable_no_addr2_or_port2,
1091       frame_num, addr_a, addr_b, ptype, port_a, port_b);
1092    if (conversation != NULL) {
1093       /*
1094        * If this is for a connection-oriented protocol:
1095        *
1096        * if search address B isn't wildcarded, set the
1097        * second address for this conversation to address
1098        * B, as that's the address that matched the
1099        * wildcarded second address for this conversation;
1100        *
1101        * if search port B isn't wildcarded, set the
1102        * second port for this conversation to port B,
1103        * as that's the port that matched the wildcarded
1104        * second port for this conversation.
1105        */
1106       if (ptype != PT_UDP)
1107       {
1108          if(!(conversation->options & CONVERSATION_TEMPLATE))
1109          {
1110             if (!(conversation->options & NO_ADDR_B))
1111                conversation_set_addr2(conversation, addr_b);
1112             if (!(conversation->options & NO_PORT_B))
1113                conversation_set_port2(conversation, port_b);
1114          }
1115          else
1116          {
1117             conversation =
1118                conversation_create_from_template(conversation, addr_b, port_b);
1119          }
1120       }
1121       return conversation;
1122    }
1123
1124    /*
1125     * Well, that didn't find anything.
1126     * If search address and port B were specified, try looking for a
1127     * conversation with the specified address B and port B as the
1128     * first address and port, and with any second address and port
1129     * (this packet may be going in the opposite direction from the
1130     * first packet in the conversation).
1131     * (Neither "addr_a" nor "port_a" take part in this lookup.)
1132     */
1133    if (addr_a->type == AT_FC)
1134       conversation =
1135       conversation_lookup_hashtable(conversation_hashtable_no_addr2_or_port2,
1136       frame_num, addr_b, addr_a, ptype, port_a, port_b);
1137    else
1138       conversation =
1139       conversation_lookup_hashtable(conversation_hashtable_no_addr2_or_port2,
1140       frame_num, addr_b, addr_a, ptype, port_b, port_a);
1141    if (conversation != NULL) {
1142       /*
1143        * If this is for a connection-oriented protocol, set the
1144        * second address for this conversation to address A, as
1145        * that's the address that matched the wildcarded second
1146        * address for this conversation, and set the second port
1147        * for this conversation to port A, as that's the port
1148        * that matched the wildcarded second port for this
1149        * conversation.
1150        */
1151       if (ptype != PT_UDP)
1152       {
1153          if(!(conversation->options & CONVERSATION_TEMPLATE))
1154          {
1155             conversation_set_addr2(conversation, addr_a);
1156             conversation_set_port2(conversation, port_a);
1157          }
1158          else
1159          {
1160             conversation = conversation_create_from_template(conversation, addr_a, port_a);
1161          }
1162       }
1163       return conversation;
1164    }
1165
1166    /*
1167     * We found no conversation.
1168     */
1169    return NULL;
1170 }
1171
1172 static gint
1173 p_compare(gconstpointer a, gconstpointer b)
1174 {
1175         const conv_proto_data *ap = (const conv_proto_data *)a;
1176         const conv_proto_data *bp = (const conv_proto_data *)b;
1177
1178         if (ap->proto > bp->proto)
1179                 return 1;
1180         else if (ap->proto == bp->proto)
1181                 return 0;
1182         else
1183                 return -1;
1184 }
1185
1186 void
1187 conversation_add_proto_data(conversation_t *conv, const int proto, void *proto_data)
1188 {
1189         conv_proto_data *p1 = se_new(conv_proto_data);
1190
1191         p1->proto = proto;
1192         p1->proto_data = proto_data;
1193
1194         /* Add it to the list of items for this conversation. */
1195
1196         conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
1197             p_compare);
1198 }
1199
1200 void *
1201 conversation_get_proto_data(const conversation_t *conv, const int proto)
1202 {
1203         conv_proto_data temp, *p1;
1204         GSList *item;
1205
1206         temp.proto = proto;
1207         temp.proto_data = NULL;
1208
1209         item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
1210             p_compare);
1211
1212         if (item != NULL) {
1213                 p1 = (conv_proto_data *)item->data;
1214                 return p1->proto_data;
1215         }
1216
1217         return NULL;
1218 }
1219
1220 void
1221 conversation_delete_proto_data(conversation_t *conv, const int proto)
1222 {
1223         conv_proto_data temp;
1224         GSList *item;
1225
1226         temp.proto = proto;
1227         temp.proto_data = NULL;
1228
1229         item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
1230             p_compare);
1231
1232         while(item){
1233                 conv->data_list = g_slist_remove(conv->data_list, item->data);
1234                 item=item->next;
1235         }
1236 }
1237
1238 void
1239 conversation_set_dissector(conversation_t *conversation, const dissector_handle_t handle)
1240 {
1241         conversation->dissector_handle = handle;
1242 }
1243
1244 /*
1245  * Given two address/port pairs for a packet, search for a matching
1246  * conversation and, if found and it has a conversation dissector,
1247  * call that dissector and return TRUE, otherwise return FALSE.
1248  *
1249  * This helper uses call_dissector_only which will NOT call the default
1250  * "data" dissector if the packet was rejected.
1251  * Our caller is responsible to call the data dissector explicitely in case
1252  * this function returns FALSE.
1253  */
1254 gboolean
1255 try_conversation_dissector(const address *addr_a, const address *addr_b, const port_type ptype,
1256     const guint32 port_a, const guint32 port_b, tvbuff_t *tvb, packet_info *pinfo,
1257     proto_tree *tree)
1258 {
1259         conversation_t *conversation;
1260
1261         conversation = find_conversation(pinfo->fd->num, addr_a, addr_b, ptype, port_a,
1262             port_b, 0);
1263
1264         if (conversation != NULL) {
1265                 int ret;
1266                 if (conversation->dissector_handle == NULL)
1267                         return FALSE;
1268                 ret=call_dissector_only(conversation->dissector_handle, tvb, pinfo,
1269                     tree, NULL);
1270                 if(!ret) {
1271                         /* this packet was rejected by the dissector
1272                          * so return FALSE in case our caller wants
1273                          * to do some cleaning up.
1274                          */
1275                         return FALSE;
1276                 }
1277                 return TRUE;
1278         }
1279         return FALSE;
1280 }
1281
1282 /*  A helper function that calls find_conversation() and, if a conversation is
1283  *  not found, calls conversation_new().
1284  *  The frame number and addresses are taken from pinfo.
1285  *  No options are used, though we could extend this API to include an options
1286  *  parameter.
1287  */
1288 conversation_t *
1289 find_or_create_conversation(packet_info *pinfo)
1290 {
1291         conversation_t *conv=NULL;
1292
1293         /* Have we seen this conversation before? */
1294         if((conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
1295                                      pinfo->ptype, pinfo->srcport,
1296                                      pinfo->destport, 0)) != NULL) {
1297                 if (pinfo->fd->num > conv->last_frame) {
1298                         conv->last_frame = pinfo->fd->num;
1299                 }
1300         } else {
1301                 /* No, this is a new conversation. */
1302                 conv = conversation_new(pinfo->fd->num, &pinfo->src,
1303                                         &pinfo->dst, pinfo->ptype,
1304                                         pinfo->srcport, pinfo->destport, 0);
1305         }
1306
1307         return conv;
1308 }
1309
1310 GHashTable *
1311 get_conversation_hashtable_exact(void)
1312 {
1313         return conversation_hashtable_exact;
1314 }
1315
1316 GHashTable *
1317 get_conversation_hashtable_no_addr2(void)
1318 {
1319         return conversation_hashtable_no_addr2;
1320 }
1321
1322 GHashTable *
1323 get_conversation_hashtable_no_port2(void)
1324 {
1325         return conversation_hashtable_no_port2;
1326 }
1327
1328 GHashTable *
1329 get_conversation_hashtable_no_addr2_or_port2(void)
1330 {
1331         return conversation_hashtable_no_addr2_or_port2;
1332 }
1333
1334 /*
1335  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1336  *
1337  * Local variables:
1338  * c-basic-offset: 8
1339  * tab-width: 8
1340  * indent-tabs-mode: t
1341  * End:
1342  *
1343  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1344  * :indentSize=8:tabSize=8:noTabs=false:
1345  */