1 /* addr_resolution_dlg.c
2 * Show current address resolution as a hosts file
4 * Copyright 2012 Anders Broman <anders.broman@ericsson.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <epan/epan.h>
32 #include <wsutil/filesystem.h>
33 #include <epan/addr_resolv.h>
38 #include "ui/main_statusbar.h"
40 #include "ui/gtk/dlg_utils.h"
41 #include "ui/gtk/expert_comp_dlg.h"
42 #include "ui/gtk/font_utils.h"
43 #include "ui/gtk/gui_utils.h"
44 #include "ui/gtk/help_dlg.h"
45 #include "ui/gtk/main.h"
46 #include "ui/gtk/packet_list.h"
47 #include "ui/gtk/addr_resolution_dlg.h"
48 #include "ui/gtk/old-gtk-compat.h"
50 static GtkWidget *addr_resolution_dlg_w = NULL;
53 #define HOSTNAME_POS 48
54 #define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
55 #define ADDRESS_STR_MAX 1024
58 eth_hash_to_texbuff(gpointer key, gpointer value, gpointer user_data)
60 gchar string_buff[ADDRESS_STR_MAX];
61 GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
62 gint64 eth_as_gint64 = *(gint64*)key;
63 hashether_t* tp = (hashether_t*)value;
65 g_snprintf(string_buff, ADDRESS_STR_MAX, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X Status: %u %s %s\n",
66 (guint8)(eth_as_gint64>>40&0xff),
67 (guint8)(eth_as_gint64>>32&0xff),
68 (guint8)((eth_as_gint64>>24)&0xff),
69 (guint8)((eth_as_gint64>>16)&0xff),
70 (guint8)((eth_as_gint64>>8)&0xff),
71 (guint8)(eth_as_gint64&0xff),
75 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
79 manuf_hash_to_texbuff(gpointer key, gpointer value, gpointer user_data)
81 gchar string_buff[ADDRESS_STR_MAX];
82 GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
83 gchar *name = (gchar *)value;
84 int eth_as_gint = *(int*)key;
86 g_snprintf(string_buff, ADDRESS_STR_MAX, "%.2X:%.2X:%.2X %s\n",eth_as_gint>>16, (eth_as_gint>>8)&0xff, eth_as_gint&0xff,name);
87 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
92 wka_hash_to_texbuff(gpointer key, gpointer value, gpointer user_data)
94 gchar string_buff[ADDRESS_STR_MAX];
95 GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
96 gchar *name = (gchar *)value;
97 gint64 eth_as_gint64 = *(gint64*)key;
99 g_snprintf(string_buff, ADDRESS_STR_MAX, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X %s\n",
100 (guint8)(eth_as_gint64>>40&0xff),
101 (guint8)(eth_as_gint64>>32&0xff),
102 (guint8)((eth_as_gint64>>24)&0xff),
103 (guint8)((eth_as_gint64>>16)&0xff),
104 (guint8)((eth_as_gint64>>8)&0xff),
105 (guint8)(eth_as_gint64&0xff),
107 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
112 serv_port_hash_to_texbuff(gpointer key, gpointer value, gpointer user_data)
114 gchar string_buff[ADDRESS_STR_MAX];
115 GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
116 serv_port_t *serv_port_table = (serv_port_t *)value;
117 int port = *(int*)key;
119 g_snprintf(string_buff, ADDRESS_STR_MAX, "Port %u \n"" TCP %s\n"" UDP %s\n"" SCTP %s\n"" DCCP %s\n",
121 serv_port_table->tcp_name,
122 serv_port_table->udp_name,
123 serv_port_table->sctp_name,
124 serv_port_table->dccp_name);
126 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
131 ipv4_hash_table_to_texbuff(gpointer key, gpointer value, gpointer user_data)
133 gchar string_buff[ADDRESS_STR_MAX];
134 GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
135 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
136 int addr = GPOINTER_TO_UINT(key);
138 g_snprintf(string_buff, ADDRESS_STR_MAX, "Key:0x%x IP: %s, Name: %s\n",
140 ipv4_hash_table_entry->ip,
141 ipv4_hash_table_entry->name);
143 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
148 ipv4_hash_table_resolved_to_texbuff(gpointer key _U_, gpointer value, gpointer user_data)
150 gchar string_buff[ADDRESS_STR_MAX];
151 GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
152 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
154 if((ipv4_hash_table_entry->flags & DUMMY_ADDRESS_ENTRY)== 0){
155 g_snprintf(string_buff, ADDRESS_STR_MAX, "%s\t%s\n",
156 ipv4_hash_table_entry->ip,
157 ipv4_hash_table_entry->name);
159 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
167 ipv6_hash_table_to_texbuff(gpointer key _U_, gpointer value, gpointer user_data)
169 gchar string_buff[ADDRESS_STR_MAX];
170 GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
171 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
173 g_snprintf(string_buff, ADDRESS_STR_MAX, "IP: %s, Name: %s\n",
174 ipv6_hash_table_entry->ip6,
175 ipv6_hash_table_entry->name);
177 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
182 ipv6_hash_table_resolved_to_texbuff(gpointer key _U_, gpointer value, gpointer user_data)
184 gchar string_buff[ADDRESS_STR_MAX];
185 GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
186 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
188 if((ipv6_hash_table_entry->flags & DUMMY_ADDRESS_ENTRY)== 0){
189 g_snprintf(string_buff, ADDRESS_STR_MAX, "%s\t%s\n",
190 ipv6_hash_table_entry->ip6,
191 ipv6_hash_table_entry->name);
193 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
199 addres_resolution_to_texbuff(GtkTextBuffer *buffer)
201 gchar string_buff[ADDRESS_STR_MAX];
202 GHashTable *manuf_hashtable;
203 GHashTable *wka_hashtable;
204 GHashTable *eth_hashtable;
205 GHashTable *serv_port_hashtable;
206 GHashTable *ipv4_hash_table;
207 GHashTable *ipv6_hash_table;
209 g_snprintf(string_buff, ADDRESS_STR_MAX, "# Hosts information in Wireshark \n#\n");
210 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
211 g_snprintf(string_buff, ADDRESS_STR_MAX, "# Host data gathered from %s\n\n", cfile.filename);
212 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
214 /* Dump the v4 addresses first, then v6 */
215 ipv4_hash_table = get_ipv4_hash_table();
217 g_hash_table_foreach( ipv4_hash_table, ipv4_hash_table_resolved_to_texbuff, buffer);
220 ipv6_hash_table = get_ipv6_hash_table();
222 g_hash_table_foreach( ipv6_hash_table, ipv6_hash_table_resolved_to_texbuff, buffer);
225 g_snprintf(string_buff, ADDRESS_STR_MAX, "\n\n# Address resolution IPv4 Hash table \n#\n");
226 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
229 g_snprintf(string_buff, ADDRESS_STR_MAX, "# With %i entries\n#\n", g_hash_table_size(ipv4_hash_table));
230 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
231 g_hash_table_foreach( ipv4_hash_table, ipv4_hash_table_to_texbuff, buffer);
234 g_snprintf(string_buff, ADDRESS_STR_MAX, "\n\n# Address resolution IPv6 Hash table \n#\n");
235 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
238 g_snprintf(string_buff, ADDRESS_STR_MAX, "# With %i entries\n#\n", g_hash_table_size(ipv6_hash_table));
239 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
240 g_hash_table_foreach( ipv6_hash_table, ipv6_hash_table_to_texbuff, buffer);
244 g_snprintf(string_buff, ADDRESS_STR_MAX, "\n\n# Port names information in Wireshark \n#\n");
245 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
247 serv_port_hashtable = get_serv_port_hashtable();
248 if(serv_port_hashtable){
249 g_snprintf(string_buff, ADDRESS_STR_MAX, "# With %i entries\n#\n", g_hash_table_size(serv_port_hashtable));
250 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
251 g_hash_table_foreach( serv_port_hashtable, serv_port_hash_to_texbuff, buffer);
254 g_snprintf(string_buff, ADDRESS_STR_MAX, "\n\n# Eth names information in Wireshark \n#\n");
255 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
257 eth_hashtable = get_eth_hashtable();
259 g_snprintf(string_buff, ADDRESS_STR_MAX, "# With %i entries\n#\n", g_hash_table_size(eth_hashtable));
260 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
261 g_hash_table_foreach( eth_hashtable, eth_hash_to_texbuff, buffer);
264 g_snprintf(string_buff, ADDRESS_STR_MAX, "\n\n# Manuf information in Wireshark \n#\n");
265 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
267 manuf_hashtable = get_manuf_hashtable();
269 g_snprintf(string_buff, ADDRESS_STR_MAX, "# With %i entries\n#\n", g_hash_table_size(manuf_hashtable));
270 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
271 g_hash_table_foreach( manuf_hashtable, manuf_hash_to_texbuff, buffer);
274 g_snprintf(string_buff, ADDRESS_STR_MAX, "\n\n# wka information in Wireshark \n#\n");
275 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
277 wka_hashtable = get_wka_hashtable();
279 g_snprintf(string_buff, ADDRESS_STR_MAX, "# With %i entries\n#\n", g_hash_table_size(wka_hashtable));
280 gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
281 g_hash_table_foreach( wka_hashtable, wka_hash_to_texbuff, buffer);
288 addr_resolution_dlg (GtkAction *action _U_, gpointer data _U_)
295 GtkWidget *ok_bt, *cancel_bt, *help_bt;
296 GtkTextBuffer *buffer;
298 addr_resolution_dlg_w = dlg_window_new ("Address Resolution");
299 gtk_widget_set_size_request (addr_resolution_dlg_w, 750, 350);
300 gtk_window_set_resizable (GTK_WINDOW (addr_resolution_dlg_w), TRUE);
301 gtk_container_set_border_width (GTK_CONTAINER (addr_resolution_dlg_w), DLG_OUTER_MARGIN);
303 vbox = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, DLG_UNRELATED_SPACING, FALSE);
304 gtk_container_add (GTK_CONTAINER (addr_resolution_dlg_w), vbox);
305 gtk_widget_show (vbox);
307 view = gtk_text_view_new ();
308 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
309 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
310 #if GTK_CHECK_VERSION(3, 0, 0)
311 gtk_widget_override_font(view, user_font_get_regular());
313 gtk_widget_modify_font(view, user_font_get_regular());
315 gtk_widget_show (view);
317 scroll = gtk_scrolled_window_new(NULL, NULL);
318 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
319 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
320 gtk_container_add(GTK_CONTAINER(scroll), view);
321 gtk_widget_show(scroll);
322 gtk_box_pack_start(GTK_BOX (vbox), scroll, TRUE, TRUE, 0);
324 /* Get the address list */
325 addres_resolution_to_texbuff(buffer);
328 bbox = dlg_button_row_new (GTK_STOCK_OK, GTK_STOCK_CANCEL, GTK_STOCK_HELP, NULL);
329 gtk_box_pack_end (GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
331 ok_bt = (GtkWidget *)g_object_get_data (G_OBJECT(bbox), GTK_STOCK_OK);
332 g_signal_connect(ok_bt, "clicked", G_CALLBACK(window_cancel_button_cb), addr_resolution_dlg_w); /* act as cxl */
333 gtk_widget_set_sensitive (ok_bt, TRUE);
335 cancel_bt = (GtkWidget *)g_object_get_data (G_OBJECT(bbox), GTK_STOCK_CANCEL);
336 window_set_cancel_button (addr_resolution_dlg_w, cancel_bt, window_cancel_button_cb);
338 help_bt = (GtkWidget *)g_object_get_data (G_OBJECT(bbox), GTK_STOCK_HELP);
340 g_signal_connect (help_bt, "clicked",/* G_CALLBACK(topic_cb)*/NULL, /*(gpointer)HELP_MANUAL_ADDR_RESOLVE_DIALOG*/NULL);
342 gtk_widget_set_sensitive (help_bt, FALSE);
344 gtk_widget_grab_default (ok_bt);
345 /*g_signal_connect (addr_resolution_dlg_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);*/
348 gtk_widget_show (addr_resolution_dlg_w);
357 * indent-tabs-mode: nil
360 * ex: set shiftwidth=4 tabstop=8 expandtab:
361 * :indentSize=4:tabSize=8:noTabs=true: