Fix whitespace/indentation to match editor modelines.
[metze/wireshark/wip.git] / ui / gtk / addr_resolution_dlg.c
1 /* addr_resolution_dlg.c
2  * Show current address resolution as a hosts file
3  *
4  * Copyright 2012 Anders Broman <anders.broman@ericsson.com>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28
29 #include <gtk/gtk.h>
30
31 #include <epan/epan.h>
32 #include <wsutil/filesystem.h>
33 #include <epan/addr_resolv.h>
34
35 #include "../cfile.h"
36 #include "../file.h"
37
38 #include "ui/main_statusbar.h"
39
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"
49
50 static GtkWidget *addr_resolution_dlg_w = NULL;
51
52
53 #define HOSTNAME_POS 48
54 #define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
55 #define ADDRESS_STR_MAX     1024
56
57 static void
58 eth_hash_to_texbuff(gpointer key, gpointer value, gpointer user_data)
59 {
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;
64
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),
72                tp->status,
73                tp->hexaddr,
74                tp->resolved_name);
75     gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
76
77 }
78 static void
79 manuf_hash_to_texbuff(gpointer key, gpointer value, gpointer user_data)
80 {
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;
85
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);
88
89 }
90
91 static void
92 wka_hash_to_texbuff(gpointer key, gpointer value, gpointer user_data)
93 {
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;
98
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),
106                name);
107     gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
108
109 }
110
111 static void
112 serv_port_hash_to_texbuff(gpointer key, gpointer value, gpointer user_data)
113 {
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;
118
119     g_snprintf(string_buff, ADDRESS_STR_MAX, "Port %u \n""     TCP  %s\n""     UDP  %s\n""     SCTP %s\n""     DCCP %s\n",
120                port,
121                serv_port_table->tcp_name,
122                serv_port_table->udp_name,
123                serv_port_table->sctp_name,
124                serv_port_table->dccp_name);
125
126     gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
127
128 }
129
130 static void
131 ipv4_hash_table_to_texbuff(gpointer key, gpointer value, gpointer user_data)
132 {
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);
137
138     g_snprintf(string_buff, ADDRESS_STR_MAX, "Key:0x%x IP: %s, Name: %s\n",
139                addr,
140                ipv4_hash_table_entry->ip,
141                ipv4_hash_table_entry->name);
142
143     gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
144
145 }
146
147 static void
148 ipv4_hash_table_resolved_to_texbuff(gpointer key _U_, gpointer value, gpointer user_data)
149 {
150     gchar string_buff[ADDRESS_STR_MAX];
151     GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
152     hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
153
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);
158
159         gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
160     }
161
162 }
163
164
165
166 static void
167 ipv6_hash_table_to_texbuff(gpointer key _U_, gpointer value, gpointer user_data)
168 {
169     gchar string_buff[ADDRESS_STR_MAX];
170     GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
171     hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
172
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);
176
177     gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
178
179 }
180
181 static void
182 ipv6_hash_table_resolved_to_texbuff(gpointer key _U_, gpointer value, gpointer user_data)
183 {
184     gchar string_buff[ADDRESS_STR_MAX];
185     GtkTextBuffer *buffer = (GtkTextBuffer*)user_data;
186     hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
187
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);
192
193         gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
194     }
195
196 }
197
198 static void
199 addres_resolution_to_texbuff(GtkTextBuffer *buffer)
200 {
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;
208
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);
213
214     /* Dump the v4 addresses first, then v6 */
215     ipv4_hash_table = get_ipv4_hash_table();
216     if(ipv4_hash_table){
217         g_hash_table_foreach( ipv4_hash_table, ipv4_hash_table_resolved_to_texbuff, buffer);
218     }
219
220     ipv6_hash_table = get_ipv6_hash_table();
221     if(ipv6_hash_table){
222         g_hash_table_foreach( ipv6_hash_table, ipv6_hash_table_resolved_to_texbuff, buffer);
223     }
224
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);
227
228     if(ipv4_hash_table){
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);
232     }
233
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);
236
237     if(ipv6_hash_table){
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);
241     }
242
243
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);
246
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);
252     }
253
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);
256
257     eth_hashtable = get_eth_hashtable();
258     if(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);
262     }
263
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);
266
267     manuf_hashtable = get_manuf_hashtable();
268     if(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);
272     }
273
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);
276
277     wka_hashtable = get_wka_hashtable();
278     if(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);
282     }
283
284
285 }
286
287 void
288 addr_resolution_dlg (GtkAction *action _U_, gpointer data _U_)
289 {
290
291     GtkWidget *vbox;
292     GtkWidget *view;
293     GtkWidget *scroll;
294     GtkWidget *bbox;
295     GtkWidget *ok_bt, *cancel_bt, *help_bt;
296     GtkTextBuffer *buffer;
297
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);
302
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);
306
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());
312 #else
313     gtk_widget_modify_font(view, user_font_get_regular());
314 #endif
315     gtk_widget_show (view);
316
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);
323
324     /* Get the address list */
325     addres_resolution_to_texbuff(buffer);
326
327     /* Button row. */
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);
330
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);
334
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);
337
338     help_bt = (GtkWidget *)g_object_get_data (G_OBJECT(bbox), GTK_STOCK_HELP);
339 #if 0
340     g_signal_connect (help_bt, "clicked",/* G_CALLBACK(topic_cb)*/NULL, /*(gpointer)HELP_MANUAL_ADDR_RESOLVE_DIALOG*/NULL);
341 #endif
342     gtk_widget_set_sensitive (help_bt, FALSE);
343
344     gtk_widget_grab_default (ok_bt);
345     /*g_signal_connect (addr_resolution_dlg_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);*/
346
347
348     gtk_widget_show (addr_resolution_dlg_w);
349 }
350
351 /*
352  * Editor modelines
353  *
354  * Local Variables:
355  * c-basic-offset: 4
356  * tab-width: 8
357  * indent-tabs-mode: nil
358  * End:
359  *
360  * ex: set shiftwidth=4 tabstop=8 expandtab:
361  * :indentSize=4:tabSize=8:noTabs=true:
362  */