1 /* conversations_table.c
2 * conversations_table 2003 Ronnie Sahlberg
3 * Helper routines common to all endpoint conversations tap.
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "packet_info.h"
30 #include "conversation_table.h"
31 #include "addr_resolv.h"
34 #include "stat_cmd_args.h"
36 GList *cmd_string_list_ = NULL;
39 gboolean hide_ports; /* hide TCP / UDP port columns */
40 int proto_id; /* protocol id (0-indexed) */
41 tap_packet_cb conv_func; /* function to be called for new incoming packets for conversation*/
42 tap_packet_cb host_func; /* function to be called for new incoming packets for hostlist */
43 host_tap_prefix prefix_func; /* function to provide prefix if different than default (host) */
44 conv_gui_init_cb conv_gui_init; /* GUI specific function to initialize conversation */
45 host_gui_init_cb host_gui_init; /* GUI specific function to initialize hostlist */
48 gboolean get_conversation_hide_ports(register_ct_t* ct)
50 return ct->hide_ports;
53 int get_conversation_proto_id(register_ct_t* ct)
61 tap_packet_cb get_conversation_packet_func(register_ct_t* ct)
66 tap_packet_cb get_hostlist_packet_func(register_ct_t* ct)
71 host_tap_prefix get_hostlist_prefix_func(register_ct_t* ct)
73 return ct->prefix_func;
77 static GSList *registered_ct_tables = NULL;
80 dissector_conversation_init(const char *opt_arg, void* userdata)
82 register_ct_t *table = (register_ct_t*)userdata;
83 GString *cmd_str = g_string_new("conv,");
84 const char *filter=NULL;
86 g_string_append(cmd_str, proto_get_protocol_filter_name(table->proto_id));
87 if(!strncmp(opt_arg, cmd_str->str, cmd_str->len)){
88 filter = opt_arg + cmd_str->len;
92 g_string_free(cmd_str, TRUE);
94 if (table->conv_gui_init)
95 table->conv_gui_init(table, filter);
99 dissector_hostlist_init(const char *opt_arg, void* userdata)
101 register_ct_t *table = (register_ct_t*)userdata;
102 GString *cmd_str = g_string_new("");
103 const char *filter=NULL;
105 g_string_printf(cmd_str, "%s,%s,", (table->prefix_func != NULL) ? table->prefix_func() : "host", proto_get_protocol_filter_name(table->proto_id));
106 if(!strncmp(opt_arg, cmd_str->str, cmd_str->len)){
107 filter=opt_arg+cmd_str->len;
112 g_string_free(cmd_str, TRUE);
114 if (table->host_gui_init)
115 table->host_gui_init(table, filter);
117 /** get conversation from protocol ID
119 * @param proto_id protocol ID
120 * @return tap function handler of conversation
122 register_ct_t* get_conversation_by_proto_id(int proto_id)
125 register_ct_t *table;
127 for(ct = registered_ct_tables; ct != NULL; ct = g_slist_next(ct)){
128 table = (register_ct_t*)ct->data;
129 if ((table) && (table->proto_id == proto_id))
137 insert_sorted_by_table_name(gconstpointer aparam, gconstpointer bparam)
139 const register_ct_t *a = (register_ct_t *)aparam;
140 const register_ct_t *b = (register_ct_t *)bparam;
142 return g_ascii_strcasecmp(proto_get_protocol_short_name(find_protocol_by_id(a->proto_id)), proto_get_protocol_short_name(find_protocol_by_id(b->proto_id)));
146 register_conversation_table(const int proto_id, gboolean hide_ports, tap_packet_cb conv_packet_func, tap_packet_cb hostlist_func, host_tap_prefix prefix_func)
148 register_ct_t *table;
149 GString *conv_cmd_str = g_string_new("conv,");
150 GString *host_cmd_str = g_string_new("");
153 table = g_new(register_ct_t,1);
155 table->hide_ports = hide_ports;
156 table->proto_id = proto_id;
157 table->conv_func = conv_packet_func;
158 table->host_func = hostlist_func;
159 table->conv_gui_init = NULL;
160 table->host_gui_init = NULL;
161 table->prefix_func = prefix_func;
163 registered_ct_tables = g_slist_insert_sorted(registered_ct_tables, table, insert_sorted_by_table_name);
165 g_string_append(conv_cmd_str, proto_get_protocol_filter_name(table->proto_id));
166 cmd_string_list_ = g_list_append(cmd_string_list_, conv_cmd_str->str);
167 ui_info.group = REGISTER_STAT_GROUP_CONVERSATION_LIST;
168 ui_info.title = NULL; /* construct this from the protocol info? */
169 ui_info.cli_string = conv_cmd_str->str;
170 ui_info.tap_init_cb = dissector_conversation_init;
173 ui_info.params = NULL;
174 register_stat_tap_ui(&ui_info, table);
175 g_string_free(conv_cmd_str, FALSE);
177 g_string_printf(host_cmd_str, "%s,%s", (get_hostlist_prefix_func(table) != NULL) ? get_hostlist_prefix_func(table)() : "host",
178 proto_get_protocol_filter_name(table->proto_id));
179 ui_info.group = REGISTER_STAT_GROUP_ENDPOINT_LIST;
180 ui_info.title = NULL; /* construct this from the protocol info? */
181 ui_info.cli_string = host_cmd_str->str;
182 ui_info.tap_init_cb = dissector_hostlist_init;
185 ui_info.params = NULL;
186 register_stat_tap_ui(&ui_info, table);
187 g_string_free(host_cmd_str, FALSE);
190 /* Set GUI fields for register_ct list */
192 set_conv_gui_data(gpointer data, gpointer user_data)
194 register_ct_t *table = (register_ct_t*)data;
195 table->conv_gui_init = (conv_gui_init_cb)user_data;
198 void conversation_table_set_gui_info(conv_gui_init_cb init_cb)
200 g_slist_foreach(registered_ct_tables, set_conv_gui_data, init_cb);
204 set_host_gui_data(gpointer data, gpointer user_data)
206 register_ct_t *table = (register_ct_t*)data;
207 table->host_gui_init = (host_gui_init_cb)user_data;
210 void hostlist_table_set_gui_info(host_gui_init_cb init_cb)
212 g_slist_foreach(registered_ct_tables, set_host_gui_data, init_cb);
215 void conversation_table_iterate_tables(GFunc func, gpointer user_data)
217 g_slist_foreach(registered_ct_tables, func, user_data);
220 guint conversation_table_get_num(void)
222 return g_slist_length(registered_ct_tables);
226 register_ct_t *get_conversation_table_by_num(guint table_num)
228 return (register_ct_t *) g_slist_nth_data(registered_ct_tables, table_num);
231 /** Compute the hash value for two given address/port pairs.
232 * (Parameter type is gconstpointer for GHashTable compatibility.)
234 * @param v Conversation Key. MUST point to a conv_key_t struct.
235 * @return Computed key hash.
238 conversation_hash(gconstpointer v)
240 const conv_key_t *key = (const conv_key_t *)v;
244 ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
245 hash_val += key->port1;
246 ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
247 hash_val += key->port2;
248 hash_val ^= key->conv_id;
253 /** Compare two conversation keys for an exact match.
254 * (Parameter types are gconstpointer for GHashTable compatibility.)
256 * @param key1 First conversation. MUST point to a conv_key_t struct.
257 * @param key2 Second conversation. MUST point to a conv_key_t struct.
258 * @return TRUE if conversations are equal, FALSE otherwise.
261 conversation_equal(gconstpointer key1, gconstpointer key2)
263 const conv_key_t *ck1 = (const conv_key_t *)key1;
264 const conv_key_t *ck2 = (const conv_key_t *)key2;
266 if (ck1->conv_id == ck2->conv_id)
268 if (ck1->port1 == ck2->port1 &&
269 ck1->port2 == ck2->port2 &&
270 ADDRESSES_EQUAL(&ck1->addr1, &ck2->addr1) &&
271 ADDRESSES_EQUAL(&ck1->addr2, &ck2->addr2)) {
275 if (ck1->port2 == ck2->port1 &&
276 ck1->port1 == ck2->port2 &&
277 ADDRESSES_EQUAL(&ck1->addr2, &ck2->addr1) &&
278 ADDRESSES_EQUAL(&ck1->addr1, &ck2->addr2)) {
284 * The addresses, ports, or conversation IDs don't match.
290 reset_conversation_table_data(conv_hash_t *ch)
296 if (ch->conv_array != NULL) {
298 for(i = 0; i < ch->conv_array->len; i++){
299 conv_item_t *conv = &g_array_index(ch->conv_array, conv_item_t, i);
300 g_free((gpointer)conv->src_address.data);
301 g_free((gpointer)conv->dst_address.data);
304 g_array_free(ch->conv_array, TRUE);
307 if (ch->hashtable != NULL) {
308 g_hash_table_destroy(ch->hashtable);
315 void reset_hostlist_table_data(conv_hash_t *ch)
321 if (ch->conv_array != NULL) {
323 for(i = 0; i < ch->conv_array->len; i++){
324 hostlist_talker_t *host = &g_array_index(ch->conv_array, hostlist_talker_t, i);
325 g_free((gpointer)host->myaddress.data);
328 g_array_free(ch->conv_array, TRUE);
331 if (ch->hashtable != NULL) {
332 g_hash_table_destroy(ch->hashtable);
339 const char *get_conversation_address(address *addr, gboolean resolve_names)
342 return ep_address_to_display(addr);
344 return ep_address_to_str(addr);
348 const char *get_conversation_port(guint32 port, port_type ptype, gboolean resolve_names)
351 if(!resolve_names) ptype = PT_NONE;
355 return ep_tcp_port_to_display(port);
357 return ep_udp_port_to_display(port);
359 return ep_sctp_port_to_display(port);
361 return ep_strdup_printf("%d", port);
365 /* given an address (to distinguish between ipv4 and ipv6 for tcp/udp),
366 a port_type and a name_type (FN_...)
367 return a string for the filter name.
369 Some addresses, like AT_ETHER may actually be any of multiple types
370 of protocols, either ethernet, tokenring, fddi, wlan etc so we must be
371 more specific there; that's why we need specific_addr_type.
374 conversation_get_filter_name(conv_item_t *conv_item, conv_filter_type_e filter_type)
377 if ((conv_item == NULL) || (conv_item->dissector_info == NULL) || (conv_item->dissector_info->get_filter_type == NULL)) {
378 return CONV_FILTER_INVALID;
381 return conv_item->dissector_info->get_filter_type(conv_item, filter_type);
385 hostlist_get_filter_name(hostlist_talker_t *host, conv_filter_type_e filter_type)
388 if ((host == NULL) || (host->dissector_info == NULL) || (host->dissector_info->get_filter_type == NULL)) {
389 return CONV_FILTER_INVALID;
392 return host->dissector_info->get_filter_type(host, filter_type);
395 /* Convert a port number into a string or NULL */
397 ct_port_to_str(port_type ptype, guint32 port)
404 return g_strdup_printf("%d", port);
411 const char *get_conversation_filter(conv_item_t *conv_item, conv_direction_e direction)
414 const char *str = "INVALID";
416 sport = ct_port_to_str(conv_item->ptype, conv_item->src_port);
417 dport = ct_port_to_str(conv_item->ptype, conv_item->dst_port);
420 case CONV_DIR_A_TO_FROM_B:
422 str = ep_strdup_printf("%s==%s%s%s%s%s && %s==%s%s%s%s%s",
423 conversation_get_filter_name(conv_item, CONV_FT_ANY_ADDRESS),
424 ep_address_to_str(&conv_item->src_address),
426 sport?conversation_get_filter_name(conv_item, CONV_FT_ANY_PORT):"",
429 conversation_get_filter_name(conv_item, CONV_FT_ANY_ADDRESS),
430 ep_address_to_str(&conv_item->dst_address),
432 dport?conversation_get_filter_name(conv_item, CONV_FT_ANY_PORT):"",
437 case CONV_DIR_A_TO_B:
439 str = ep_strdup_printf("%s==%s%s%s%s%s && %s==%s%s%s%s%s",
440 conversation_get_filter_name(conv_item, CONV_FT_SRC_ADDRESS),
441 ep_address_to_str(&conv_item->src_address),
443 sport?conversation_get_filter_name(conv_item, CONV_FT_SRC_PORT):"",
446 conversation_get_filter_name(conv_item, CONV_FT_DST_ADDRESS),
447 ep_address_to_str(&conv_item->dst_address),
449 dport?conversation_get_filter_name(conv_item, CONV_FT_DST_PORT):"",
454 case CONV_DIR_A_FROM_B:
456 str = ep_strdup_printf("%s==%s%s%s%s%s && %s==%s%s%s%s%s",
457 conversation_get_filter_name(conv_item, CONV_FT_DST_ADDRESS),
458 ep_address_to_str(&conv_item->src_address),
460 sport?conversation_get_filter_name(conv_item, CONV_FT_DST_PORT):"",
463 conversation_get_filter_name(conv_item, CONV_FT_SRC_ADDRESS),
464 ep_address_to_str(&conv_item->dst_address),
466 dport?conversation_get_filter_name(conv_item, CONV_FT_SRC_PORT):"",
471 case CONV_DIR_A_TO_FROM_ANY:
473 str = ep_strdup_printf("%s==%s%s%s%s%s",
474 conversation_get_filter_name(conv_item, CONV_FT_ANY_ADDRESS),
475 ep_address_to_str(&conv_item->src_address),
477 sport?conversation_get_filter_name(conv_item, CONV_FT_ANY_PORT):"",
482 case CONV_DIR_A_TO_ANY:
484 str = ep_strdup_printf("%s==%s%s%s%s%s",
485 conversation_get_filter_name(conv_item, CONV_FT_SRC_ADDRESS),
486 ep_address_to_str(&conv_item->src_address),
488 sport?conversation_get_filter_name(conv_item, CONV_FT_SRC_PORT):"",
493 case CONV_DIR_A_FROM_ANY:
495 str = ep_strdup_printf("%s==%s%s%s%s%s",
496 conversation_get_filter_name(conv_item, CONV_FT_DST_ADDRESS),
497 ep_address_to_str(&conv_item->src_address),
499 sport?conversation_get_filter_name(conv_item, CONV_FT_DST_PORT):"",
504 case CONV_DIR_ANY_TO_FROM_B:
506 str = ep_strdup_printf("%s==%s%s%s%s%s",
507 conversation_get_filter_name(conv_item, CONV_FT_ANY_ADDRESS),
508 ep_address_to_str(&conv_item->dst_address),
510 dport?conversation_get_filter_name(conv_item, CONV_FT_ANY_PORT):"",
515 case CONV_DIR_ANY_FROM_B:
517 str = ep_strdup_printf("%s==%s%s%s%s%s",
518 conversation_get_filter_name(conv_item, CONV_FT_SRC_ADDRESS),
519 ep_address_to_str(&conv_item->dst_address),
521 dport?conversation_get_filter_name(conv_item, CONV_FT_SRC_PORT):"",
526 case CONV_DIR_ANY_TO_B:
528 str = ep_strdup_printf("%s==%s%s%s%s%s",
529 conversation_get_filter_name(conv_item, CONV_FT_DST_ADDRESS),
530 ep_address_to_str(&conv_item->dst_address),
532 dport?conversation_get_filter_name(conv_item, CONV_FT_DST_PORT):"",
545 const char *get_hostlist_filter(hostlist_talker_t *host)
550 sport=ct_port_to_str(host->ptype, host->port);
552 str = g_strdup_printf("%s==%s%s%s%s%s",
553 hostlist_get_filter_name(host, CONV_FT_ANY_ADDRESS),
554 ep_address_to_str(&host->myaddress),
556 sport?hostlist_get_filter_name(host, CONV_FT_ANY_PORT):"",
564 add_conversation_table_data(conv_hash_t *ch, const address *src, const address *dst, guint32 src_port, guint32 dst_port, int num_frames, int num_bytes,
565 nstime_t *ts, nstime_t *abs_ts, ct_dissector_info_t *ct_info, port_type ptype)
567 add_conversation_table_data_with_conv_id(ch, src, dst, src_port, dst_port, CONV_ID_UNSET, num_frames, num_bytes, ts, abs_ts, ct_info, ptype);
571 add_conversation_table_data_with_conv_id(
582 ct_dissector_info_t *ct_info,
585 const address *addr1, *addr2;
586 guint32 port1, port2;
587 conv_item_t *conv_item = NULL;
588 unsigned int conversation_idx = 0;
590 if (src_port > dst_port) {
595 } else if (src_port < dst_port) {
600 } else if (CMP_ADDRESS(src, dst) < 0) {
612 /* if we don't have any entries at all yet */
613 if (ch->conv_array == NULL) {
614 ch->conv_array = g_array_sized_new(FALSE, FALSE, sizeof(conv_item_t), 10000);
616 ch->hashtable = g_hash_table_new_full(conversation_hash,
617 conversation_equal, /* key_equal_func */
618 g_free, /* key_destroy_func */
619 NULL); /* value_destroy_func */
622 /* try to find it among the existing known conversations */
623 conv_key_t existing_key;
625 existing_key.addr1 = *addr1;
626 existing_key.addr2 = *addr2;
627 existing_key.port1 = port1;
628 existing_key.port2 = port2;
629 existing_key.conv_id = conv_id;
630 if (g_hash_table_lookup_extended(ch->hashtable, &existing_key, NULL, (gpointer *) &conversation_idx)) {
631 conv_item = &g_array_index(ch->conv_array, conv_item_t, conversation_idx);
635 /* if we still don't know what conversation this is it has to be a new one
636 and we have to allocate it and append it to the end of the list */
637 if (conv_item == NULL) {
639 conv_item_t new_conv_item;
641 COPY_ADDRESS(&new_conv_item.src_address, addr1);
642 COPY_ADDRESS(&new_conv_item.dst_address, addr2);
643 new_conv_item.dissector_info = ct_info;
644 new_conv_item.ptype = ptype;
645 new_conv_item.src_port = port1;
646 new_conv_item.dst_port = port2;
647 new_conv_item.conv_id = conv_id;
648 new_conv_item.rx_frames = 0;
649 new_conv_item.tx_frames = 0;
650 new_conv_item.rx_bytes = 0;
651 new_conv_item.tx_bytes = 0;
652 new_conv_item.modified = TRUE;
655 memcpy(&new_conv_item.start_time, ts, sizeof(new_conv_item.start_time));
656 memcpy(&new_conv_item.stop_time, ts, sizeof(new_conv_item.stop_time));
657 memcpy(&new_conv_item.start_abs_time, abs_ts, sizeof(new_conv_item.start_abs_time));
659 nstime_set_unset(&new_conv_item.start_abs_time);
660 nstime_set_unset(&new_conv_item.start_time);
661 nstime_set_unset(&new_conv_item.stop_time);
663 g_array_append_val(ch->conv_array, new_conv_item);
664 conversation_idx = ch->conv_array->len - 1;
665 conv_item = &g_array_index(ch->conv_array, conv_item_t, conversation_idx);
667 /* ct->conversations address is not a constant but src/dst_address.data are */
668 new_key = g_new(conv_key_t, 1);
669 SET_ADDRESS(&new_key->addr1, conv_item->src_address.type, conv_item->src_address.len, conv_item->src_address.data);
670 SET_ADDRESS(&new_key->addr2, conv_item->dst_address.type, conv_item->dst_address.len, conv_item->dst_address.data);
671 new_key->port1 = port1;
672 new_key->port2 = port2;
673 new_key->conv_id = conv_id;
674 g_hash_table_insert(ch->hashtable, new_key, GUINT_TO_POINTER(conversation_idx));
677 /* update the conversation struct */
678 conv_item->modified = TRUE;
679 if ( (!CMP_ADDRESS(src, addr1)) && (!CMP_ADDRESS(dst, addr2)) && (src_port==port1) && (dst_port==port2) ) {
680 conv_item->tx_frames += num_frames;
681 conv_item->tx_bytes += num_bytes;
683 conv_item->rx_frames += num_frames;
684 conv_item->rx_bytes += num_bytes;
688 if (nstime_cmp(ts, &conv_item->stop_time) > 0) {
689 memcpy(&conv_item->stop_time, ts, sizeof(conv_item->stop_time));
690 } else if (nstime_cmp(ts, &conv_item->start_time) < 0) {
691 memcpy(&conv_item->start_time, ts, sizeof(conv_item->start_time));
692 memcpy(&conv_item->start_abs_time, abs_ts, sizeof(conv_item->start_abs_time));
698 * Compute the hash value for a given address/port pairs if the match
702 host_hash(gconstpointer v)
704 const host_key_t *key = (const host_key_t *)v;
708 ADD_ADDRESS_TO_HASH(hash_val, &key->myaddress);
709 hash_val += key->port;
714 * Compare two host keys for an exact match.
717 host_match(gconstpointer v, gconstpointer w)
719 const host_key_t *v1 = (const host_key_t *)v;
720 const host_key_t *v2 = (const host_key_t *)w;
722 if (v1->port == v2->port &&
723 ADDRESSES_EQUAL(&v1->myaddress, &v2->myaddress)) {
727 * The addresses or the ports don't match.
733 add_hostlist_table_data(conv_hash_t *ch, const address *addr, guint32 port, gboolean sender, int num_frames, int num_bytes, hostlist_dissector_info_t *host_info, port_type port_type_val)
735 hostlist_talker_t *talker=NULL;
738 /* XXX should be optimized to allocate n extra entries at a time
739 instead of just one */
740 /* if we don't have any entries at all yet */
741 if(ch->conv_array==NULL){
742 ch->conv_array=g_array_sized_new(FALSE, FALSE, sizeof(hostlist_talker_t), 10000);
743 ch->hashtable = g_hash_table_new_full(host_hash,
744 host_match, /* key_equal_func */
745 g_free, /* key_destroy_func */
746 NULL); /* value_destroy_func */
749 /* try to find it among the existing known conversations */
750 host_key_t existing_key;
752 existing_key.myaddress = *addr;
753 existing_key.port = port;
755 if (g_hash_table_lookup_extended(ch->hashtable, &existing_key, NULL, (gpointer *) &talker_idx)) {
756 talker = &g_array_index(ch->conv_array, hostlist_talker_t, talker_idx);
760 /* if we still don't know what talker this is it has to be a new one
761 and we have to allocate it and append it to the end of the list */
764 hostlist_talker_t host;
766 COPY_ADDRESS(&host.myaddress, addr);
767 host.dissector_info = host_info;
768 host.ptype=port_type_val;
774 host.modified = TRUE;
776 g_array_append_val(ch->conv_array, host);
777 talker_idx= ch->conv_array->len - 1;
778 talker=&g_array_index(ch->conv_array, hostlist_talker_t, talker_idx);
780 /* hl->hosts address is not a constant but address.data is */
781 new_key = g_new(host_key_t,1);
782 SET_ADDRESS(&new_key->myaddress, talker->myaddress.type, talker->myaddress.len, talker->myaddress.data);
783 new_key->port = port;
784 g_hash_table_insert(ch->hashtable, new_key, GUINT_TO_POINTER(talker_idx));
787 /* if this is a new talker we need to initialize the struct */
788 talker->modified = TRUE;
790 /* update the talker struct */
792 talker->tx_frames+=num_frames;
793 talker->tx_bytes+=num_bytes;
795 talker->rx_frames+=num_frames;
796 talker->rx_bytes+=num_bytes;
806 * indent-tabs-mode: nil
809 * ex: set shiftwidth=4 tabstop=8 expandtab:
810 * :indentSize=4:tabSize=8:noTabs=true: