HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / conversation.c
blob848775b1a3f35e9a4f5a62858435720bd1c77395
1 /* conversation.c
2 * Routines for building lists of packets that are part of a "conversation"
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
25 #include "config.h"
27 #include <stdio.h>
29 #include <string.h>
30 #include <glib.h>
31 #include "packet.h"
32 #include "emem.h"
33 #include "conversation.h"
36 * Hash table for conversations with no wildcards.
38 static GHashTable *conversation_hashtable_exact = NULL;
41 * Hash table for conversations with one wildcard address.
43 static GHashTable *conversation_hashtable_no_addr2 = NULL;
46 * Hash table for conversations with one wildcard port.
48 static GHashTable *conversation_hashtable_no_port2 = NULL;
51 * Hash table for conversations with one wildcard address and port.
53 static GHashTable *conversation_hashtable_no_addr2_or_port2 = NULL;
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
67 * Linked list of conversation keys, so we can, before freeing them all,
68 * free the address data allocations associated with them.
70 static conversation_key *conversation_keys;
72 static guint32 new_index;
75 * Protocol-specific data attached to a conversation_t structure - protocol
76 * index and opaque pointer.
78 typedef struct _conv_proto_data {
79 int proto;
80 void *proto_data;
81 } conv_proto_data;
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.
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.
94 * addr2 and port2 are used in the function if their respective conversation
95 * options bits are set (NO_ADDR2 and NO_PORT2).
97 static conversation_t *
98 conversation_create_from_template(conversation_t *conversation, const address *addr2, const guint32 port2)
101 * Add a new conversation and keep the conversation template only if the
102 * CONVERSATION_TEMPLATE bit is set for a connection oriented protocol.
104 if(conversation->options & CONVERSATION_TEMPLATE &&
105 conversation->key_ptr->ptype != PT_UDP)
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.
111 conversation_t *new_conversation_from_template;
112 guint options = conversation->options & ~(CONVERSATION_TEMPLATE | NO_ADDR2 | NO_PORT2);
115 * Are both the NO_ADDR2 and NO_PORT2 wildcards set in the options mask?
117 if(conversation->options & NO_ADDR2 &&
118 conversation->options & NO_PORT2)
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.
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);
131 else if(conversation->options & NO_PORT2)
134 * The conversation template was created without knowledge of port 2
135 * only. Create a new conversation with new 2nd port.
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);
143 else if(conversation->options & NO_ADDR2)
146 * The conversation template was created without knowledge of address
147 * 2. Create a new conversation with new 2nd address.
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);
155 else
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.
162 return conversation;
166 * Set the protocol dissector used for the template conversation as
167 * the handler of the new conversation as well.
169 new_conversation_from_template->dissector_handle = conversation->dissector_handle;
171 return new_conversation_from_template;
173 else
175 return conversation;
180 * Compute the hash value for two given address/port pairs if the match
181 * is to be exact.
183 /* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
184 * One-at-a-Time hash
186 static guint
187 conversation_hash_exact(gconstpointer v)
189 const conversation_key *key = (const conversation_key *)v;
190 guint hash_val;
191 address tmp_addr;
193 hash_val = 0;
194 tmp_addr.len = 4;
196 ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
198 tmp_addr.data = &key->port1;
199 ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
201 ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
203 tmp_addr.data = &key->port2;
204 ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
206 hash_val += ( hash_val << 3 );
207 hash_val ^= ( hash_val >> 11 );
208 hash_val += ( hash_val << 15 );
210 return hash_val;
214 * Compare two conversation keys for an exact match.
216 static gint
217 conversation_match_exact(gconstpointer v, gconstpointer w)
219 const conversation_key *v1 = (const conversation_key *)v;
220 const conversation_key *v2 = (const conversation_key *)w;
222 if (v1->ptype != v2->ptype)
223 return 0; /* different types of port */
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?
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)) {
236 * Yes. It's the same conversation, and the two
237 * address/port pairs are going in the same direction.
239 return 1;
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?
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)) {
253 * Yes. It's the same conversation, and the two
254 * address/port pairs are going in opposite directions.
256 return 1;
260 * The addresses or the ports don't match.
262 return 0;
266 * Compute the hash value for two given address/port pairs if the match
267 * has a wildcard address 2.
269 static guint
270 conversation_hash_no_addr2(gconstpointer v)
272 const conversation_key *key = (const conversation_key *)v;
273 guint hash_val;
274 address tmp_addr;
276 hash_val = 0;
277 tmp_addr.len = 4;
279 ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
281 tmp_addr.data = &key->port1;
282 ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
284 tmp_addr.data = &key->port2;
285 ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
287 hash_val += ( hash_val << 3 );
288 hash_val ^= ( hash_val >> 11 );
289 hash_val += ( hash_val << 15 );
291 return hash_val;
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.
300 static gint
301 conversation_match_no_addr2(gconstpointer v, gconstpointer w)
303 const conversation_key *v1 = (const conversation_key *)v;
304 const conversation_key *v2 = (const conversation_key *)w;
306 if (v1->ptype != v2->ptype)
307 return 0; /* different types of port */
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?
314 if (v1->port1 == v2->port1 &&
315 v1->port2 == v2->port2 &&
316 ADDRESSES_EQUAL(&v1->addr1, &v2->addr1)) {
318 * Yes. It's the same conversation, and the two
319 * address/port pairs are going in the same direction.
321 return 1;
325 * The addresses or the ports don't match.
327 return 0;
331 * Compute the hash value for two given address/port pairs if the match
332 * has a wildcard port 2.
334 static guint
335 conversation_hash_no_port2(gconstpointer v)
337 const conversation_key *key = (const conversation_key *)v;
338 guint hash_val;
339 address tmp_addr;
341 hash_val = 0;
342 tmp_addr.len = 4;
344 ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
346 tmp_addr.data = &key->port1;
347 ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
349 ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
351 hash_val += ( hash_val << 3 );
352 hash_val ^= ( hash_val >> 11 );
353 hash_val += ( hash_val << 15 );
355 return hash_val;
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.
364 static gint
365 conversation_match_no_port2(gconstpointer v, gconstpointer w)
367 const conversation_key *v1 = (const conversation_key *)v;
368 const conversation_key *v2 = (const conversation_key *)w;
370 if (v1->ptype != v2->ptype)
371 return 0; /* different types of port */
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?
378 if (v1->port1 == v2->port1 &&
379 ADDRESSES_EQUAL(&v1->addr1, &v2->addr1) &&
380 ADDRESSES_EQUAL(&v1->addr2, &v2->addr2)) {
382 * Yes. It's the same conversation, and the two
383 * address/port pairs are going in the same direction.
385 return 1;
389 * The addresses or the ports don't match.
391 return 0;
395 * Compute the hash value for two given address/port pairs if the match
396 * has a wildcard address 2 and port 2.
398 static guint
399 conversation_hash_no_addr2_or_port2(gconstpointer v)
401 const conversation_key *key = (const conversation_key *)v;
402 guint hash_val;
403 address tmp_addr;
405 hash_val = 0;
406 tmp_addr.len = 4;
408 ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
410 tmp_addr.data = &key->port1;
411 ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
413 hash_val += ( hash_val << 3 );
414 hash_val ^= ( hash_val >> 11 );
415 hash_val += ( hash_val << 15 );
417 return hash_val;
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.
426 static gint
427 conversation_match_no_addr2_or_port2(gconstpointer v, gconstpointer w)
429 const conversation_key *v1 = (const conversation_key *)v;
430 const conversation_key *v2 = (const conversation_key *)w;
432 if (v1->ptype != v2->ptype)
433 return 0; /* different types of port */
436 * Are the first and second port 1 values the same and the first
437 * and second address 1 values the same?
439 if (v1->port1 == v2->port1 &&
440 ADDRESSES_EQUAL(&v1->addr1, &v2->addr1)) {
442 * Yes. It's the same conversation, and the two
443 * address/port pairs are going in the same direction.
445 return 1;
449 * The addresses or the ports don't match.
451 return 0;
455 * Free the proto_data. The conversation itself is se_allocated.
457 static void
458 free_data_list(gpointer key _U_, gpointer value, gpointer user_data _U_)
460 conversation_t *conv = (conversation_t *)value;
462 /* TODO: se_slist? */
463 g_slist_free(conv->data_list);
465 /* Not really necessary, but... */
466 conv->data_list = NULL;
471 * Destroy all existing conversations
473 void
474 conversation_cleanup(void)
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.
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);
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);
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);
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);
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;
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.
508 void
509 conversation_init(void)
512 * Free up any space allocated for conversation protocol data
513 * areas.
515 * We can free the space, as the structures it contains are
516 * pointed to by conversation data structures that were freed
517 * above.
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);
533 * Start the conversation indices over at 0.
535 new_index = 0;
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.
542 * Mostly adapted from the old conversation_new().
544 void
545 conversation_insert_into_hashtable(GHashTable *hashtable, conversation_t *conv)
547 conversation_t *chain_head, *chain_tail, *cur, *prev;
549 chain_head = (conversation_t *)g_hash_table_lookup(hashtable, conv->key_ptr);
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);
557 else {
558 /* There's an existing chain for this key */
560 chain_tail = chain_head->last;
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;
569 else {
570 /* Loop through the chain to find the right spot */
571 cur = chain_head;
572 prev = NULL;
574 for (; (conv->setup_frame > cur->setup_frame) && cur->next; prev=cur, cur=cur->next)
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);
584 else {
585 /* Inserting into the middle of the chain */
586 conv->next = cur;
587 conv->last = NULL;
588 prev->next = conv;
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.
598 void
599 conversation_remove_from_hashtable(GHashTable *hashtable, conversation_t *conv)
601 conversation_t *chain_head, *cur, *prev;
603 chain_head = (conversation_t *)g_hash_table_lookup(hashtable, conv->key_ptr);
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);
611 else {
612 /* Update the head of the chain */
613 chain_head = conv->next;
614 chain_head->last = conv->last;
616 if (conv->latest_found == conv)
617 chain_head->latest_found = NULL;
618 else
619 chain_head->latest_found = conv->latest_found;
621 g_hash_table_insert(hashtable, chain_head->key_ptr, chain_head);
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;
631 for (; (cur != conv) && cur->next; prev=cur, cur=cur->next)
634 if (cur != conv) {
635 /* XXX: Conversation not found. Wrong hashtable? */
636 return;
639 prev->next = conv->next;
641 if (NULL == conv->next) {
642 /* We're at the very end of the list. */
643 chain_head->last = prev;
646 if (chain_head->latest_found == conv)
647 chain_head->latest_found = prev;
652 * Given two address/port pairs for a packet, create a new conversation
653 * to contain packets between those address/port pairs.
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.
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)
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");
667 GHashTable* hashtable;
668 conversation_t *conversation=NULL;
669 conversation_key *new_key;
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;
677 } else {
678 if (options & (NO_PORT2|NO_PORT2_FORCE)) {
679 hashtable = conversation_hashtable_no_port2;
680 } else {
681 hashtable = conversation_hashtable_exact;
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;
694 conversation = se_new(conversation_t);
695 memset(conversation, 0, sizeof(conversation_t));
697 conversation->index = new_index;
698 conversation->setup_frame = conversation->last_frame = setup_frame;
699 conversation->data_list = NULL;
701 /* clear dissector handle */
702 conversation->dissector_handle = NULL;
704 /* set the options and key pointer */
705 conversation->options = options;
706 conversation->key_ptr = new_key;
708 new_index++;
710 conversation_insert_into_hashtable(hashtable, conversation);
712 return conversation;
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.
719 void
720 conversation_set_port2(conversation_t *conv, const guint32 port)
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");
726 * If the port 2 value is not wildcarded, don't set it.
728 if ((!(conv->options & NO_PORT2)) || (conv->options & NO_PORT2_FORCE))
729 return;
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);
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);
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.
749 void
750 conversation_set_addr2(conversation_t *conv, const address *addr)
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");
756 * If the address 2 value is not wildcarded, don't set it.
758 if (!(conv->options & NO_ADDR2))
759 return;
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);
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);
776 * Search a particular hash table for a conversation with the specified
777 * {addr1, port1, addr2, port2} and set up before frame_num.
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)
783 conversation_t* convo=NULL;
784 conversation_t* match=NULL;
785 conversation_t* chain_head=NULL;
786 conversation_key key;
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.
792 key.addr1 = *addr1;
793 key.addr2 = *addr2;
794 key.ptype = ptype;
795 key.port1 = port1;
796 key.port2 = port2;
798 chain_head = (conversation_t *)g_hash_table_lookup(hashtable, &key);
800 if (chain_head && (chain_head->setup_frame <= frame_num)) {
801 match = chain_head;
803 if((chain_head->last)&&(chain_head->last->setup_frame<=frame_num))
804 return chain_head->last;
806 if((chain_head->latest_found)&&(chain_head->latest_found->setup_frame<=frame_num))
807 match = chain_head->latest_found;
809 for (convo = match; convo && convo->setup_frame <= frame_num; convo = convo->next) {
810 if (convo->setup_frame > match->setup_frame) {
811 match = convo;
816 if (match)
817 chain_head->latest_found = match;
819 return match;
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.
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.
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.
837 * I.e.:
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;
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;
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;
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;
857 * otherwise, we found no matching conversation, and return NULL.
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)
863 conversation_t *conversation;
866 * First try an exact match, if we have two addresses and ports.
868 if (!(options & (NO_ADDR_B|NO_PORT_B))) {
870 * Neither search address B nor search port B are wildcarded,
871 * start out with an exact match.
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);
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.
888 conversation =
889 conversation_lookup_hashtable(conversation_hashtable_exact,
890 frame_num, addr_b, addr_a, ptype,
891 port_a, port_b);
893 if (conversation != NULL)
894 return conversation;
898 * Well, that didn't find anything. Try matches that wildcard
899 * one of the addresses, if we have two ports.
901 if (!(options & NO_PORT_B)) {
903 * Search port B isn't wildcarded.
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.)
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.
918 conversation =
919 conversation_lookup_hashtable(conversation_hashtable_no_addr2,
920 frame_num, addr_b, addr_a, ptype,
921 port_a, port_b);
923 if (conversation != NULL) {
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.
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.)
935 if (!(conversation->options & NO_ADDR_B) && ptype != PT_UDP)
937 if(!(conversation->options & CONVERSATION_TEMPLATE))
939 conversation_set_addr2(conversation, addr_b);
941 else
943 conversation =
944 conversation_create_from_template(conversation, addr_b, 0);
947 return conversation;
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.)
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) {
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.
973 if (ptype != PT_UDP) {
974 if(!(conversation->options & CONVERSATION_TEMPLATE))
976 conversation_set_addr2(conversation, addr_a);
978 else
980 conversation =
981 conversation_create_from_template(conversation, addr_a, 0);
984 return conversation;
990 * Well, that didn't find anything. Try matches that wildcard
991 * one of the ports, if we have two addresses.
993 if (!(options & NO_ADDR_B)) {
995 * Search address B isn't wildcarded.
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.)
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
1010 conversation =
1011 conversation_lookup_hashtable(conversation_hashtable_no_port2,
1012 frame_num, addr_b, addr_a, ptype, port_a, port_b);
1014 if (conversation != NULL) {
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.
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.)
1026 if (!(conversation->options & NO_PORT_B) && ptype != PT_UDP)
1028 if(!(conversation->options & CONVERSATION_TEMPLATE))
1030 conversation_set_port2(conversation, port_b);
1032 else
1034 conversation =
1035 conversation_create_from_template(conversation, 0, port_b);
1038 return conversation;
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.)
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) {
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.
1064 if (ptype != PT_UDP)
1066 if(!(conversation->options & CONVERSATION_TEMPLATE))
1068 conversation_set_port2(conversation, port_a);
1070 else
1072 conversation =
1073 conversation_create_from_template(conversation, 0, port_a);
1076 return conversation;
1082 * Well, that didn't find anything. Try matches that wildcard
1083 * one address/port pair.
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.)
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) {
1094 * If this is for a connection-oriented protocol:
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;
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.
1106 if (ptype != PT_UDP)
1108 if(!(conversation->options & CONVERSATION_TEMPLATE))
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);
1115 else
1117 conversation =
1118 conversation_create_from_template(conversation, addr_b, port_b);
1121 return conversation;
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.)
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) {
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.
1151 if (ptype != PT_UDP)
1153 if(!(conversation->options & CONVERSATION_TEMPLATE))
1155 conversation_set_addr2(conversation, addr_a);
1156 conversation_set_port2(conversation, port_a);
1158 else
1160 conversation = conversation_create_from_template(conversation, addr_a, port_a);
1163 return conversation;
1167 * We found no conversation.
1169 return NULL;
1172 static gint
1173 p_compare(gconstpointer a, gconstpointer b)
1175 const conv_proto_data *ap = (const conv_proto_data *)a;
1176 const conv_proto_data *bp = (const conv_proto_data *)b;
1178 if (ap->proto > bp->proto)
1179 return 1;
1180 else if (ap->proto == bp->proto)
1181 return 0;
1182 else
1183 return -1;
1186 void
1187 conversation_add_proto_data(conversation_t *conv, const int proto, void *proto_data)
1189 conv_proto_data *p1 = se_new(conv_proto_data);
1191 p1->proto = proto;
1192 p1->proto_data = proto_data;
1194 /* Add it to the list of items for this conversation. */
1196 conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
1197 p_compare);
1200 void *
1201 conversation_get_proto_data(const conversation_t *conv, const int proto)
1203 conv_proto_data temp, *p1;
1204 GSList *item;
1206 temp.proto = proto;
1207 temp.proto_data = NULL;
1209 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
1210 p_compare);
1212 if (item != NULL) {
1213 p1 = (conv_proto_data *)item->data;
1214 return p1->proto_data;
1217 return NULL;
1220 void
1221 conversation_delete_proto_data(conversation_t *conv, const int proto)
1223 conv_proto_data temp;
1224 GSList *item;
1226 temp.proto = proto;
1227 temp.proto_data = NULL;
1229 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
1230 p_compare);
1232 while(item){
1233 conv->data_list = g_slist_remove(conv->data_list, item->data);
1234 item=item->next;
1238 void
1239 conversation_set_dissector(conversation_t *conversation, const dissector_handle_t handle)
1241 conversation->dissector_handle = handle;
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.
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.
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, void* data)
1259 conversation_t *conversation;
1261 conversation = find_conversation(pinfo->fd->num, addr_a, addr_b, ptype, port_a,
1262 port_b, 0);
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, data);
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.
1275 return FALSE;
1277 return TRUE;
1279 return FALSE;
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.
1288 conversation_t *
1289 find_or_create_conversation(packet_info *pinfo)
1291 conversation_t *conv=NULL;
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;
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);
1307 return conv;
1310 GHashTable *
1311 get_conversation_hashtable_exact(void)
1313 return conversation_hashtable_exact;
1316 GHashTable *
1317 get_conversation_hashtable_no_addr2(void)
1319 return conversation_hashtable_no_addr2;
1322 GHashTable *
1323 get_conversation_hashtable_no_port2(void)
1325 return conversation_hashtable_no_port2;
1328 GHashTable *
1329 get_conversation_hashtable_no_addr2_or_port2(void)
1331 return conversation_hashtable_no_addr2_or_port2;
1335 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1337 * Local variables:
1338 * c-basic-offset: 8
1339 * tab-width: 8
1340 * indent-tabs-mode: t
1341 * End:
1343 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1344 * :indentSize=8:tabSize=8:noTabs=false: