replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[metze/wireshark/wip.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  * SPDX-License-Identifier: GPL-2.0-or-later*/
8
9 /* Dump our collected IPv4- and IPv6-to-hostname mappings */
10
11 #include "config.h"
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "globals.h"
17
18 #include <epan/packet.h>
19 #include <epan/tap.h>
20 #include <epan/stat_tap_ui.h>
21 #include <epan/addr_resolv.h>
22
23 void register_tap_listener_hosts(void);
24
25 gboolean dump_v4 = FALSE;
26 gboolean dump_v6 = FALSE;
27
28 #define TAP_NAME "hosts"
29
30 static void
31 ipv4_hash_table_print_resolved(gpointer key _U_, gpointer value, gpointer user_data _U_)
32 {
33         hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
34
35         if ((ipv4_hash_table_entry->flags & NAME_RESOLVED)) {
36                 printf("%s\t%s\n",
37                        ipv4_hash_table_entry->ip,
38                        ipv4_hash_table_entry->name);
39         }
40 }
41
42 static void
43 ipv6_hash_table_print_resolved(gpointer key _U_, gpointer value, gpointer user_data _U_)
44 {
45         hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
46
47         if ((ipv6_hash_table_entry->flags & NAME_RESOLVED)) {
48                 printf("%s\t%s\n",
49                        ipv6_hash_table_entry->ip6,
50                        ipv6_hash_table_entry->name);
51         }
52 }
53
54 static void
55 hosts_draw(void *dummy _U_)
56 {
57
58         wmem_map_t *ipv4_hash_table;
59         wmem_map_t *ipv6_hash_table;
60
61         printf("# TShark hosts output\n");
62         printf("#\n");
63         printf("# Host data gathered from %s\n", cfile.filename);
64         printf("\n");
65
66         ipv4_hash_table = get_ipv4_hash_table();
67         if (ipv4_hash_table) {
68                 wmem_map_foreach( ipv4_hash_table, ipv4_hash_table_print_resolved, NULL);
69         }
70
71         ipv6_hash_table = get_ipv6_hash_table();
72         if (ipv6_hash_table) {
73                 wmem_map_foreach( ipv6_hash_table, ipv6_hash_table_print_resolved, NULL);
74         }
75
76 }
77
78
79 static void
80 hosts_init(const char *opt_arg, void *userdata _U_)
81 {
82         GString *error_string;
83         gchar **tokens;
84         gint opt_count;
85
86         dump_v4 = FALSE;
87         dump_v6 = FALSE;
88
89         if (strcmp(TAP_NAME, opt_arg) == 0) {
90                 /* No arguments; dump everything */
91                 dump_v4 = TRUE;
92                 dump_v6 = TRUE;
93         } else {
94                 tokens = g_strsplit(opt_arg, ",", 0);
95                 opt_count = 0;
96                 while (tokens[opt_count]) {
97                         if (strcmp("ipv4", tokens[opt_count]) == 0) {
98                                 dump_v4 = TRUE;
99                         } else if (strcmp("ipv6", tokens[opt_count]) == 0) {
100                                 dump_v6 = TRUE;
101                         } else if (opt_count > 0) {
102                                 fprintf(stderr, "tshark: invalid \"-z " TAP_NAME "[,ipv4|ipv6]\" argument\n");
103                                 exit(1);
104                         }
105                         opt_count++;
106                 }
107                 g_strfreev(tokens);
108         }
109
110         error_string = register_tap_listener("frame", NULL, NULL, TL_REQUIRES_PROTO_TREE,
111                                            NULL, NULL, hosts_draw);
112         if (error_string) {
113                 /* error, we failed to attach to the tap. clean up */
114                 fprintf(stderr, "tshark: Couldn't register " TAP_NAME " tap: %s\n",
115                         error_string->str);
116                 g_string_free(error_string, TRUE);
117                 exit(1);
118         }
119 }
120
121 static stat_tap_ui hosts_ui = {
122         REGISTER_STAT_GROUP_GENERIC,
123         NULL,
124         TAP_NAME,
125         hosts_init,
126         0,
127         NULL
128 };
129
130 void
131 register_tap_listener_hosts(void)
132 {
133         register_stat_tap_ui(&hosts_ui, NULL);
134 }
135
136
137 /*
138  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
139  *
140  * Local variables:
141  * c-basic-offset: 8
142  * tab-width: 8
143  * indent-tabs-mode: t
144  * End:
145  *
146  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
147  * :indentSize=8:tabSize=8:noTabs=false:
148  */