Fix Dead Store (Dead nested assignment) Warning found by Clang
[obnox/wireshark/wip.git] / tap-hosts.c
1 /* tap-hosts.c
2  *
3  * $Id$
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 /* Dump our collected IPv4- and IPv6-to-hostname mappings */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <string.h>
37
38 #include "globals.h"
39
40 #include <epan/packet.h>
41 #include <cfile.h>
42 #include <epan/proto.h>
43 #include <epan/tap.h>
44 #include <epan/stat_cmd_args.h>
45 #include <epan/addr_resolv.h>
46
47 /* Needed for addrinfo */
48 #ifdef HAVE_SYS_TYPES_H
49 # include <sys/types.h>
50 #endif
51
52 #ifdef HAVE_SYS_SOCKET_H
53 #include <sys/socket.h>
54 #endif
55
56 #ifdef HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
58 #endif
59
60 #ifdef HAVE_NETINET_IN_H
61 # include <netinet/in.h>
62 #endif
63
64 #ifdef HAVE_NETDB_H
65 # include <netdb.h>
66 #endif
67
68 #ifdef HAVE_WINSOCK2_H
69 # include <winsock2.h>
70 #endif
71
72 #if defined(_WIN32) && defined(INET6)
73 # include <ws2tcpip.h>
74 #endif
75
76 #ifdef NEED_INET_V6DEFS_H
77 # include "wsutil/inet_v6defs.h"
78 #endif
79
80
81 gboolean dump_v4 = FALSE;
82 gboolean dump_v6 = FALSE;
83
84 #define TAP_NAME "hosts"
85
86 #define HOSTNAME_POS 48
87 #define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
88 static void
89 hosts_draw(void *dummy _U_)
90 {
91         struct addrinfo *ai;
92         struct sockaddr_in *sa4;
93         struct sockaddr_in6 *sa6;
94         char   addr_str[ADDRSTRLEN];
95         int i, tab_count;
96
97         printf("# TShark hosts output\n");
98         printf("#\n");
99         printf("# Host data gathered from %s\n", cfile.filename);
100         printf("\n");
101
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) {
105                         continue;
106                 }
107
108                 sa4 = (struct sockaddr_in *) 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++)
113                                 printf("\t");
114                         printf("%s\n", ai->ai_canonname);
115                 }
116         }
117
118
119         for (ai = get_addrinfo_list(); ai; ai = ai->ai_next) {
120                 if (!dump_v6 || ai->ai_family != AF_INET6) {
121                         continue;
122                 }
123
124                 sa6 = (struct sockaddr_in6 *) 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++)
129                                 printf("\t");
130                         printf("%s\n", ai->ai_canonname);
131                 }
132         }
133 }
134
135
136 static void
137 hosts_init(const char *optarg, void* userdata _U_)
138 {
139         GString *error_string;
140         gchar **tokens;
141         gint opt_count;
142
143         dump_v4 = FALSE;
144         dump_v6 = FALSE;
145
146         if(strcmp(TAP_NAME, optarg)==0) {
147                 /* No arguments; dump everything */
148                 dump_v4 = TRUE;
149                 dump_v6 = TRUE;
150         } else {
151                 tokens = g_strsplit(optarg,",", 0);
152                 opt_count=0;
153                 while (tokens[opt_count]) {
154                         if (strcmp("ipv4", tokens[opt_count]) == 0) {
155                                 dump_v4 = TRUE;
156                         } else if (strcmp("ipv6", tokens[opt_count]) == 0) {
157                                 dump_v6 = TRUE;
158                         } else if (opt_count > 0) {
159                                 fprintf(stderr, "tshark: invalid \"-z " TAP_NAME "[,ipv4|ipv6]\" argument\n");
160                                 exit(1);
161                         }
162                         opt_count++;
163                 }
164                 g_strfreev(tokens);
165         }
166
167         error_string=register_tap_listener("frame", NULL, NULL, TL_REQUIRES_PROTO_TREE,
168                                            NULL, NULL, hosts_draw);
169         if(error_string){
170                 /* error, we failed to attach to the tap. clean up */
171                 fprintf(stderr, "tshark: Couldn't register " TAP_NAME " tap: %s\n",
172                     error_string->str);
173                 g_string_free(error_string, TRUE);
174                 exit(1);
175         }
176 }
177
178 void
179 register_tap_listener_hosts(void)
180 {
181         register_stat_cmd_arg(TAP_NAME, hosts_init, NULL);
182 }
183