Merging upstream version 0.5.9.
[debian/lxpanel.git] / src / plugins / netstat / netstat.c
1 /**
2 * Copyright (c) 2008 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #if HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <sys/types.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <pthread.h>
29 #include <iwlib.h>
30 #include "nsconfig.h"
31 #include "netstat.h"
32 #include "lxnm_client.h"
33 #include "statusicon.h"
34 #include "passwd_gui.h"
35 #include "devproc.h"
36 #include "wireless.h"
37 #include "panel.h"
38 #include "misc.h"
39 #include "plugin.h"
40 #include "dbg.h"
41
42 /* 1 second */
43 #define NETSTAT_IFACE_POLL_DELAY 3000
44
45 static void* actionProcess(void *arg)
46 {
47 ENTER;
48 RET(GINT_TO_POINTER(system((char *)arg)));
49 }
50
51 /* menu handlers */
52 static void ethernet_repair(GtkWidget *widget, netdev_info *ni)
53 {
54 if (ni->ns->fixcmd) {
55 pthread_t actionThread;
56 pthread_attr_t attr;
57 char *fixcmd;
58
59 fixcmd = g_strdup_printf(ni->ns->fixcmd, ni->netdev_list->info.ifname);
60
61 /* Note: Better would be to join the thread, and thus get a status
62 * message. But detaching is currently simpler to implement. */
63 pthread_attr_init(&attr);
64 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
65 pthread_create(&actionThread, &attr, actionProcess, fixcmd);
66 pthread_attr_destroy(&attr);
67 } else {
68 lxnm_send_command(ni->ns->fnetd->lxnmchannel, LXNM_ETHERNET_REPAIR, ni->netdev_list->info.ifname);
69 }
70 }
71
72 static void ethernet_down(GtkWidget *widget, netdev_info *ni)
73 {
74 lxnm_send_command(ni->ns->fnetd->lxnmchannel, LXNM_ETHERNET_DOWN, ni->netdev_list->info.ifname);
75 }
76
77 static void wireless_connect(GtkWidget *widget, ap_setting *aps)
78 {
79 char *cmdargs;
80
81 /* without encryption */
82 if (!aps->apinfo->haskey) {
83 cmdargs = lxnm_wireless_command_make(aps->ifname, aps->apinfo->essid, aps->apinfo->apaddr, "",
84 aps->apinfo->en_method, aps->apinfo->key_mgmt,
85 aps->apinfo->group, aps->apinfo->pairwise);
86 lxnm_send_command(aps->gio, LXNM_WIRELESS_CONNECT, cmdargs);
87 g_free(cmdargs);
88 } else {
89 /* with encryption */
90 if (aps->ni->netdev_list->info.pg!=NULL)
91 passwd_gui_destroy(aps->ni->netdev_list->info.pg);
92
93 /* record information for password dialog */
94 ap_setting *aps_new;
95 ap_info *apinfo;
96 apinfo = malloc(sizeof(ap_info));
97 if (aps->apinfo->essid==NULL)
98 apinfo->essid = NULL;
99 else
100 apinfo->essid = g_strdup(aps->apinfo->essid);
101
102 apinfo->apaddr = g_strdup(aps->apinfo->apaddr);
103 apinfo->quality = aps->apinfo->quality;
104 apinfo->en_method = aps->apinfo->en_method;
105 apinfo->pairwise = aps->apinfo->pairwise;
106 apinfo->group = aps->apinfo->group;
107 apinfo->key_mgmt = aps->apinfo->key_mgmt;
108 apinfo->haskey = aps->apinfo->haskey;
109
110 aps_new = g_new0(ap_setting, 1);
111 aps_new->ni = aps->ni;
112 aps_new->gio = aps->gio;
113 aps_new->ifname = g_strdup(aps->ifname);
114 aps_new->apinfo = apinfo;
115 /* create dialog window for typing password */
116 aps->ni->netdev_list->info.pg = passwd_gui_new(aps_new);
117 //passwd_gui_set_style(aps->ni->netdev_list->info.pg, gtk_style_new());
118 }
119 }
120
121 static void
122 g_free_weaknotify(void *ptr, GObject *dummy)
123 {
124 g_free(ptr);
125 }
126
127 static GtkWidget *
128 wireless_menu(netdev_info *ni)
129 {
130 APLIST *aplist;
131 APLIST *ptr;
132 GtkWidget *menu;
133 GtkWidget *menu_item;
134 GtkWidget *wireless_label;
135
136 /* AP status widget */
137 GtkWidget *item_box;
138 GtkWidget *essid_label;
139 GtkWidget *lockicon;
140 GtkWidget *signal_quality;
141 gdouble quality_per;
142 ap_setting *aps;
143
144 /* create menu */
145 menu = gtk_menu_new();
146 g_signal_connect(menu, "selection-done", G_CALLBACK(gtk_widget_destroy), NULL);
147
148 /* Scanning AP */
149 aplist = wireless_scanning(ni->ns->fnetd->iwsockfd, ni->netdev_list->info.ifname);
150 if (aplist!=NULL) {
151 /* release AP list after menu */
152 g_object_weak_ref(G_OBJECT(menu), wireless_aplist_free, aplist);
153 ptr = aplist;
154 do {
155 /* skip hidden AP with Encryption */
156 if (ptr->info->haskey&&ptr->info->essid==NULL) {
157 /* handle next AP */
158 ptr = ptr->next;
159 continue;
160 }
161
162 aps = g_new0(ap_setting, 1);
163 aps->ni = ni;
164 aps->gio = ni->ns->fnetd->lxnmchannel;
165 aps->ifname = ni->netdev_list->info.ifname;
166 aps->apinfo = ptr->info;
167
168 /* create a new item */
169 menu_item = gtk_menu_item_new();
170 item_box = gtk_hbox_new(FALSE, 0);
171
172 /* Encryption */
173 if (aps->apinfo->haskey) {
174 lockicon = gtk_image_new_from_file(ICONS_WL_LOCK);
175 gtk_box_pack_start(GTK_BOX(item_box), lockicon, FALSE, FALSE, 0);
176 }
177
178 /* ESSID */
179 if (aps->apinfo->essid==NULL)
180 essid_label = gtk_label_new(_("<Hidden Access Point>"));
181 else
182 essid_label = gtk_label_new(aps->apinfo->essid);
183
184 gtk_label_set_justify(GTK_LABEL(essid_label), GTK_JUSTIFY_LEFT);
185 gtk_misc_set_padding(GTK_MISC(essid_label), 2, 0);
186 gtk_box_pack_start(GTK_BOX(item_box), essid_label, TRUE, FALSE, 0);
187
188 /* Quality */
189 quality_per = (gdouble)((double)aps->apinfo->quality/100);
190 if (quality_per>1)
191 quality_per = 1;
192 else if (quality_per<0)
193 quality_per = 0;
194
195 signal_quality = gtk_progress_bar_new();
196 gtk_widget_set_size_request(signal_quality, 100, -1);
197 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(signal_quality), GTK_PROGRESS_LEFT_TO_RIGHT);
198 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(signal_quality), quality_per);
199 gtk_box_pack_start(GTK_BOX(item_box), signal_quality, FALSE, FALSE, 0);
200
201 /* add this item to menu */
202 gtk_container_add(GTK_CONTAINER(menu_item), item_box);
203 gtk_menu_append(GTK_MENU(menu), menu_item);
204 g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(wireless_connect), aps);
205 g_object_weak_ref(G_OBJECT(menu_item), g_free_weaknotify, aps);
206
207 /* handle next AP */
208 ptr = ptr->next;
209 } while(ptr!=NULL);
210 } else {
211 /* we do not found any wireless networks */
212 menu_item = gtk_menu_item_new();
213 wireless_label = gtk_label_new(_("Wireless Networks not found in range"));
214 gtk_label_set_justify(GTK_LABEL(wireless_label), GTK_JUSTIFY_LEFT);
215 gtk_widget_set_sensitive(GTK_WIDGET(wireless_label), FALSE);
216 gtk_container_add(GTK_CONTAINER(menu_item), wireless_label);
217 gtk_menu_append(GTK_MENU(menu), menu_item);
218 }
219
220 gtk_widget_show_all(menu);
221
222 return menu;
223 }
224
225 static gint menupopup(GtkWidget *widget, GdkEvent *event, netdev_info *ni)
226 {
227 GdkEventButton *event_button;
228
229 g_return_val_if_fail (event != NULL, FALSE);
230
231 if (event->type == GDK_BUTTON_PRESS) {
232 event_button = (GdkEventButton *) event;
233 if (event_button->button == 1) {
234 /* wireless device */
235 if (ni->netdev_list->info.wireless) {
236 gtk_menu_popup(GTK_MENU(wireless_menu(ni)), NULL, NULL, NULL, NULL, event_button->button, event_button->time);
237 }
238 return TRUE;
239 } else if (event_button->button == 3) {
240 GtkWidget *menu;
241 GtkWidget *menu_item;
242
243 /* create menu */
244 menu = gtk_menu_new();
245
246 /* Repair */
247 menu_item = gtk_menu_item_new_with_label(_("Repair"));
248 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
249 g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(ethernet_repair), ni);
250
251 /* interface down */
252 menu_item = gtk_menu_item_new_with_label(_("Disable"));
253 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
254 g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(ethernet_down), ni);
255
256 gtk_widget_show_all(menu);
257
258 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event_button->button, event_button->time);
259 return TRUE;
260 }
261 }
262
263 return FALSE;
264 }
265
266 static char *select_icon(gboolean plug, gboolean connected, int stat)
267 {
268 if (!plug)
269 return ICONS_DISCONNECT;
270
271 switch(stat) {
272 case NETDEV_STAT_NORMAL:
273 return ICONS_CONNECTED;
274 break;
275 case NETDEV_STAT_PROBLEM:
276 return ICONS_PROBLEM;
277 break;
278 case NETDEV_STAT_RENEW:
279 return ICONS_RENEW;
280 break;
281 case NETDEV_STAT_BOTHRS:
282 return ICONS_BOTHRS;
283 break;
284 case NETDEV_STAT_SENDDATA:
285 return ICONS_SENDDATA;
286 break;
287 case NETDEV_STAT_RECVDATA:
288 return ICONS_RECVDATA;
289 break;
290 default:
291 return NULL;
292 }
293 }
294
295 static void refresh_systray(netstat *ns, NETDEVLIST_PTR netdev_list)
296 {
297 NETDEVLIST_PTR ptr;
298 char *tooltip;
299
300 if (netdev_list==NULL) {
301 return;
302 }
303
304 ptr = netdev_list;
305 do {
306 if (!ptr->info.enable) {
307 if (ptr->info.status_icon!=NULL) {
308 set_statusicon_visible(ptr->info.status_icon, FALSE);
309 }
310 } else if (ptr->info.updated) {
311 if (!ptr->info.plug) {
312 if (ptr->info.wireless)
313 tooltip = g_strdup_printf("%s\n %s", ptr->info.ifname, _("Wireless Connection has no connectivity"));
314 else
315 tooltip = g_strdup_printf("%s\n %s", ptr->info.ifname, _("Network cable is plugged out"));
316 } else if (!ptr->info.connected)
317 tooltip = g_strdup_printf("%s\n %s", ptr->info.ifname, _("Connection has limited or no connectivity"));
318 else if (ptr->info.flags & IFF_POINTOPOINT)
319 tooltip = g_strdup_printf("%s\n %s\t%s\n %s\t%s\n %s\t%s\n\n %s(%s/%s)\n %lu/%lu %s\n %lu/%lu %s",
320 ptr->info.ifname,
321 _("IP Address:"), ptr->info.ipaddr,
322 _("Remote IP:"), ptr->info.dest,
323 _("Netmask:"), ptr->info.mask,
324 _("Activity"), _("Sent"),_("Received"),
325 ptr->info.trans_bytes, ptr->info.recv_bytes, _("bytes"),
326 ptr->info.trans_packets, ptr->info.recv_packets, _("packets"));
327 else if (ptr->info.wireless)
328 tooltip = g_strdup_printf("%s(%s) - %s(%d%%)\n %s\t%s\n %s\t%s\n %s\t%s\n %s\t%s\n %s\t%s\n\n %s(%s/%s)\n %lu/%lu %s\n %lu/%lu %s",
329 ptr->info.ifname, _("Wireless"),
330 ptr->info.essid, ptr->info.quality,
331 _("Protocol:"), ptr->info.protocol,
332 _("IP Address:"), ptr->info.ipaddr,
333 _("Broadcast:"), ptr->info.bcast,
334 _("Netmask:"), ptr->info.mask,
335 _("HW Address:"), ptr->info.mac,
336 _("Activity"), _("Sent"), _("Received"),
337 ptr->info.trans_bytes, ptr->info.recv_bytes, _("bytes"),
338 ptr->info.trans_packets, ptr->info.recv_packets, _("packets"));
339
340 else
341 tooltip = g_strdup_printf("%s\n %s\t%s\n %s\t%s\n %s\t%s\n %s\t%s\n\n %s(%s/%s)\n %lu/%lu %s\n %lu/%lu %s",
342 ptr->info.ifname,
343 _("IP Address:"), ptr->info.ipaddr,
344 _("Broadcast:"), ptr->info.bcast,
345 _("Netmask:"), ptr->info.mask,
346 _("HW Address:"), ptr->info.mac,
347 _("Activity"), _("Sent"), _("Received"),
348 ptr->info.trans_bytes, ptr->info.recv_bytes, _("bytes"),
349 ptr->info.trans_packets, ptr->info.recv_packets, _("packets"));
350
351 /* status icon doesn't exist */
352 if (ptr->info.status_icon==NULL) {
353 netdev_info *ni;
354 ni = malloc(sizeof(netdev_info));
355 ni->ns = ns;
356 ni->netdev_list = ptr;
357
358 ptr->info.status_icon = create_statusicon(ns->mainw, select_icon(ptr->info.plug, ptr->info.connected, ptr->info.status), tooltip);
359 g_signal_connect(ptr->info.status_icon->main, "button_press_event", G_CALLBACK(menupopup), ni);
360 g_object_weak_ref(G_OBJECT(ptr->info.status_icon->main), g_free_weaknotify, ni);
361 } else {
362 set_statusicon_tooltips(ptr->info.status_icon, tooltip);
363 set_statusicon_image_from_file(ptr->info.status_icon, select_icon(ptr->info.plug, ptr->info.connected, ptr->info.status));
364 set_statusicon_visible(ptr->info.status_icon, TRUE);
365 }
366 g_free(tooltip);
367 }
368 ptr = ptr->next;
369 } while(ptr!=NULL);
370 }
371
372 static gboolean refresh_devstat(netstat *ns)
373 {
374 netproc_listener(ns->fnetd);
375 #ifdef DEBUG
376 netproc_print(ns->fnetd->netdevlist);
377 #endif
378 refresh_systray(ns, ns->fnetd->netdevlist);
379 netproc_devicelist_clear(&ns->fnetd->netdevlist);
380 return TRUE;
381 }
382
383 /* Plugin constructor */
384 static void netstat_destructor(Plugin *p)
385 {
386 netstat *ns = (netstat *) p->priv;
387
388 ENTER;
389 g_source_remove(ns->ttag);
390 netproc_netdevlist_clear(&ns->fnetd->netdevlist);
391 /* The widget is destroyed in plugin_stop().
392 gtk_widget_destroy(ns->mainw);
393 */
394 lxnm_close(ns->fnetd->lxnmchannel);
395 close(ns->fnetd->sockfd);
396 close(ns->fnetd->iwsockfd);
397 g_free(ns->fnetd);
398 g_free(ns->fixcmd);
399 g_free(ns);
400 RET();
401 }
402
403 static int netstat_constructor(Plugin *p, char **fp)
404 {
405 netstat *ns;
406 line s;
407
408 ENTER;
409 s.len = 256;
410 ns = g_new0(netstat, 1);
411 g_return_val_if_fail(ns != NULL, 0);
412 p->priv = ns;
413 ns->fixcmd = NULL;
414
415 if( fp ) {
416 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END) {
417 if (s.type == LINE_NONE) {
418 ERR( "netstat: illegal token %s\n", s.str);
419 goto error;
420 }
421 if (s.type == LINE_VAR) {
422 if (!g_ascii_strcasecmp(s.t[0], "FixCommand"))
423 ns->fixcmd = g_strdup(s.t[1]);
424 else {
425 ERR( "netstat: unknown var %s\n", s.t[0]);
426 }
427 } else {
428 ERR( "netstat: illegal in this context %s\n", s.str);
429 goto error;
430 }
431 }
432 }
433
434 /* initializing */
435 ns->fnetd = malloc(sizeof(FNETD));
436 ns->fnetd->netdevlist = NULL;
437 ns->fnetd->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
438 ns->fnetd->iwsockfd = iw_sockets_open();
439 ns->fnetd->lxnmchannel = lxnm_socket();
440
441 /* main */
442 ns->mainw = p->panel->my_box_new(FALSE, 1);
443 gtk_widget_show_all(ns->mainw);
444
445 /* Initializing network device list*/
446 ns->fnetd->netdev_fp = netproc_open();
447 ns->fnetd->dev_count = netproc_netdevlist_clear(&ns->fnetd->netdevlist);
448 ns->fnetd->dev_count = netproc_scandevice(ns->fnetd->sockfd, ns->fnetd->iwsockfd, ns->fnetd->netdev_fp, &ns->fnetd->netdevlist);
449 netproc_close(ns->fnetd->netdev_fp);
450 refresh_systray(ns, ns->fnetd->netdevlist);
451
452 ns->ttag = g_timeout_add(NETSTAT_IFACE_POLL_DELAY, (GSourceFunc)refresh_devstat, ns);
453
454 p->pwid = gtk_event_box_new();
455 GTK_WIDGET_SET_FLAGS(p->pwid, GTK_NO_WINDOW);
456 gtk_container_add((GtkContainer*)p->pwid, ns->mainw);
457
458 RET(1);
459 error:
460 g_free(ns->fnetd);
461 g_free(ns->fixcmd);
462 g_free(ns);
463 RET(0);
464 }
465
466 static void orientation_changed(Plugin* p)
467 {
468 netstat *ns = (netstat *)p->priv;
469 GtkBox* newbox;
470 newbox = GTK_BOX(recreate_box(GTK_BOX(ns->mainw), p->panel->orientation));
471 if( GTK_WIDGET(newbox) != ns->mainw ) {
472 /* Since the old box has been destroyed,
473 we need to re-add the new box to the container */
474 ns->mainw = GTK_WIDGET(newbox);
475 gtk_container_add(GTK_CONTAINER(p->pwid), ns->mainw);
476 }
477 }
478
479 PluginClass netstat_plugin_class = {
480
481 PLUGINCLASS_VERSIONING,
482
483 type : "netstat",
484 name : N_("Manage Networks"),
485 version: "1.0",
486 description : N_("Monitor and Manage networks"),
487
488 constructor : netstat_constructor,
489 destructor : netstat_destructor,
490 panel_configuration_changed : orientation_changed,
491 config : NULL,
492 save : NULL
493 };
494
495 /* vim: set sw=4 sts=4 et : */