1837738b09dba939f49bfe96b01cd2032619b787
[jlayton/wireshark.git] / ui / cli / tap-hosts.c
1 /* tap-hosts.c
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 /* Dump our collected IPv4- and IPv6-to-hostname mappings */
23
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "globals.h"
30
31 #include <epan/packet.h>
32 #include <epan/tap.h>
33 #include <epan/stat_tap_ui.h>
34 #include <epan/addr_resolv.h>
35
36 void register_tap_listener_hosts(void);
37
38 gboolean dump_v4 = FALSE;
39 gboolean dump_v6 = FALSE;
40
41 #define TAP_NAME "hosts"
42
43 #define HOSTNAME_POS 48
44 #define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
45
46 static void
47 ipv4_hash_table_print_resolved(gpointer key _U_, gpointer value, gpointer user_data _U_)
48 {
49         hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
50
51         if (!(ipv4_hash_table_entry->flags & DUMMY_ADDRESS_ENTRY)) {
52                 printf("%s\t%s\n",
53                        ipv4_hash_table_entry->ip,
54                        ipv4_hash_table_entry->name);
55         }
56 }
57
58 static void
59 ipv6_hash_table_print_resolved(gpointer key _U_, gpointer value, gpointer user_data _U_)
60 {
61         hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
62
63         if (!(ipv6_hash_table_entry->flags & DUMMY_ADDRESS_ENTRY)) {
64                 printf("%s\t%s\n",
65                        ipv6_hash_table_entry->ip6,
66                        ipv6_hash_table_entry->name);
67         }
68 }
69
70 static void
71 hosts_draw(void *dummy _U_)
72 {
73
74         wmem_map_t *ipv4_hash_table;
75         wmem_map_t *ipv6_hash_table;
76
77         printf("# TShark hosts output\n");
78         printf("#\n");
79         printf("# Host data gathered from %s\n", cfile.filename);
80         printf("\n");
81
82         ipv4_hash_table = get_ipv4_hash_table();
83         if (ipv4_hash_table) {
84                 wmem_map_foreach( ipv4_hash_table, ipv4_hash_table_print_resolved, NULL);
85         }
86
87         ipv6_hash_table = get_ipv6_hash_table();
88         if (ipv6_hash_table) {
89                 wmem_map_foreach( ipv6_hash_table, ipv6_hash_table_print_resolved, NULL);
90         }
91
92 }
93
94
95 static void
96 hosts_init(const char *opt_arg, void *userdata _U_)
97 {
98         gchar *error_string;
99         gchar **tokens;
100         gint opt_count;
101
102         dump_v4 = FALSE;
103         dump_v6 = FALSE;
104
105         if (strcmp(TAP_NAME, opt_arg) == 0) {
106                 /* No arguments; dump everything */
107                 dump_v4 = TRUE;
108                 dump_v6 = TRUE;
109         } else {
110                 tokens = g_strsplit(opt_arg, ",", 0);
111                 opt_count = 0;
112                 while (tokens[opt_count]) {
113                         if (strcmp("ipv4", tokens[opt_count]) == 0) {
114                                 dump_v4 = TRUE;
115                         } else if (strcmp("ipv6", tokens[opt_count]) == 0) {
116                                 dump_v6 = TRUE;
117                         } else if (opt_count > 0) {
118                                 fprintf(stderr, "tshark: invalid \"-z " TAP_NAME "[,ipv4|ipv6]\" argument\n");
119                                 exit(1);
120                         }
121                         opt_count++;
122                 }
123                 g_strfreev(tokens);
124         }
125
126         error_string = register_tap_listener("frame", NULL, NULL, TL_REQUIRES_PROTO_TREE,
127                                            NULL, NULL, hosts_draw);
128         if (error_string) {
129                 /* error, we failed to attach to the tap. clean up */
130                 fprintf(stderr, "tshark: Couldn't register " TAP_NAME " tap: %s\n",
131                         error_string);
132                 wmem_free(NULL, error_string);
133                 exit(1);
134         }
135 }
136
137 static stat_tap_ui hosts_ui = {
138         REGISTER_STAT_GROUP_GENERIC,
139         NULL,
140         TAP_NAME,
141         hosts_init,
142         0,
143         NULL
144 };
145
146 void
147 register_tap_listener_hosts(void)
148 {
149         register_stat_tap_ui(&hosts_ui, NULL);
150 }
151
152
153 /*
154  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
155  *
156  * Local variables:
157  * c-basic-offset: 8
158  * tab-width: 8
159  * indent-tabs-mode: t
160  * End:
161  *
162  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
163  * :indentSize=8:tabSize=8:noTabs=false:
164  */