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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 /* Dump our collected IPv4- and IPv6-to-hostname mappings */
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
40 #include <epan/packet.h>
42 #include <epan/proto.h>
44 #include <epan/stat_cmd_args.h>
45 #include <epan/addr_resolv.h>
47 /* Needed for addrinfo */
48 #ifdef HAVE_SYS_TYPES_H
49 # include <sys/types.h>
52 #ifdef HAVE_SYS_SOCKET_H
53 #include <sys/socket.h>
56 #ifdef HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
60 #ifdef HAVE_NETINET_IN_H
61 # include <netinet/in.h>
68 #ifdef HAVE_WINSOCK2_H
69 # include <winsock2.h>
72 #if defined(_WIN32) && defined(INET6)
73 # include <ws2tcpip.h>
76 #ifdef NEED_INET_V6DEFS_H
77 # include "wsutil/inet_v6defs.h"
81 gboolean dump_v4 = FALSE;
82 gboolean dump_v6 = FALSE;
84 #define TAP_NAME "hosts"
86 #define HOSTNAME_POS 48
87 #define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
89 hosts_draw(void *dummy _U_)
92 struct sockaddr_in *sa4;
93 struct sockaddr_in6 *sa6;
94 char addr_str[ADDRSTRLEN];
97 printf("# TShark hosts output\n");
99 printf("# Host data gathered from %s\n", cfile.filename);
102 /* Dump the v4 addresses first, then v6 */
103 for (ai = get_addrinfo_list(); ai; ai = ai->ai_next) {
104 if (!dump_v4 || ai->ai_family != AF_INET) {
108 sa4 = (struct sockaddr_in *)(void *)ai->ai_addr;
109 if (inet_ntop(AF_INET, &(sa4->sin_addr.s_addr), addr_str, ADDRSTRLEN)) {
110 tab_count = (HOSTNAME_POS - (int)strlen(addr_str)) / 8;
111 printf("%s", addr_str);
112 for (i = 0; i < tab_count; i++)
114 printf("%s\n", ai->ai_canonname);
119 for (ai = get_addrinfo_list(); ai; ai = ai->ai_next) {
120 if (!dump_v6 || ai->ai_family != AF_INET6) {
124 sa6 = (struct sockaddr_in6 *)(void *)ai->ai_addr;
125 if (inet_ntop(AF_INET6, sa6->sin6_addr.s6_addr, addr_str, ADDRSTRLEN)) {
126 tab_count = (HOSTNAME_POS - (int)strlen(addr_str)) / 8;
127 printf("%s", addr_str);
128 for (i = 0; i < tab_count; i++)
130 printf("%s\n", ai->ai_canonname);
137 hosts_init(const char *optarg, void* userdata _U_)
139 GString *error_string;
146 if(strcmp(TAP_NAME, optarg)==0) {
147 /* No arguments; dump everything */
151 tokens = g_strsplit(optarg,",", 0);
153 while (tokens[opt_count]) {
154 if (strcmp("ipv4", tokens[opt_count]) == 0) {
156 } else if (strcmp("ipv6", tokens[opt_count]) == 0) {
158 } else if (opt_count > 0) {
159 fprintf(stderr, "tshark: invalid \"-z " TAP_NAME "[,ipv4|ipv6]\" argument\n");
167 error_string=register_tap_listener("frame", NULL, NULL, TL_REQUIRES_PROTO_TREE,
168 NULL, NULL, hosts_draw);
170 /* error, we failed to attach to the tap. clean up */
171 fprintf(stderr, "tshark: Couldn't register " TAP_NAME " tap: %s\n",
173 g_string_free(error_string, TRUE);
179 register_tap_listener_hosts(void)
181 register_stat_cmd_arg(TAP_NAME, hosts_init, NULL);