File: main.c

package info (click to toggle)
gpe-mininet 0.7-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 860 kB
  • ctags: 162
  • sloc: sh: 8,474; ansic: 956; xml: 68; makefile: 40
file content (395 lines) | stat: -rw-r--r-- 8,731 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * gpe-mininet (c) 2004 - 2006 Florian Boor <florian.boor@kernelconcepts.de>
 *
 * Basic applet skeleton taken from gpe-bluetooth (see below)
 *
 * Copyright (C) 2002, 2003 Philip Blundell <philb@gnu.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 */

#include <sys/types.h>
#include <stdlib.h>
#include <libintl.h>

#include <locale.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include <gtk/gtk.h>
#include <gdk/gdkx.h>

#include <gpe/init.h>
#include <gpe/pixmaps.h>
#include <gpe/errorbox.h>
#include <gpe/tray.h>
#include <gpe/infoprint.h>
#include <gpe/popup.h>
#include <gpe/spacing.h>

#include "main.h"
#include "netlink.h"

#define _(x) gettext(x)

#define BIN_INFO PREFIX "/bin/gpe-info"
#define BIN_CONFIG PREFIX "/bin/gpe-conf"
#define BIN_PING "/bin/ping"

#define PARAM_INFO "network"
#define PARAM_CONFIG "network"

struct gpe_icon my_icons[] = {
	{"net-on", "net-on"},
	{"net-off", "net-off"},
	{"net-on-48", "net-on-48"},
	{"net-off-48", "net-off-48"},
	{"gpe-mininet", "gpe-mininet"},
	{NULL}
};

typedef enum
{
	ICON_NET_OFF,
	ICON_NET_ON,
	NUM_ICONS
}
n_images;


static GtkWidget *window;
static GtkWidget *menu;
static GtkWidget *icon;

gboolean net_is_on = FALSE;

static int netlink_fd;

/* Iface   Destination     Gateway         Flags   RefCnt  Use     Metric  Mask   MTU      Window  IRTT
   eth0    0105A8C0        0102A8C0        0006    0       0       0       FFFFFFF0                     */

#define RTF_UP  0x1

static void
update_icon(gint size)
{
	GdkBitmap *bitmap;
	GdkPixbuf *sbuf, *dbuf;
	
	if (size <= 0)
		size = gdk_pixbuf_get_width(gtk_image_get_pixbuf(GTK_IMAGE(icon)));
	
	if (size > 16)
		sbuf = gpe_find_icon ( net_is_on ? "net-on-48" : "net-off-48" );
	else
		sbuf = gpe_find_icon ( net_is_on ? "net-on" : "net-off" );
	dbuf = gdk_pixbuf_scale_simple(sbuf, size, size, GDK_INTERP_HYPER);
	gdk_pixbuf_render_pixmap_and_mask (dbuf, NULL, &bitmap, 64);
	gtk_widget_shape_combine_mask (GTK_WIDGET(window), NULL, 1, 0);
	gtk_widget_shape_combine_mask (GTK_WIDGET(window), bitmap, 1, 0);
	gdk_bitmap_unref (bitmap);
	gtk_image_set_from_pixbuf (GTK_IMAGE(icon), dbuf);
}

static gboolean
net_get_status(void)
{
	FILE *pipe;
	char buffer[256];
	int result = FALSE;
	char gw[9];
	char *ip = NULL;
	int ipdigit[4];
	
	pipe = fopen ("/proc/net/route", "r");

	if (pipe)
	{
  		while ((feof(pipe) == 0))
		{
			if (fgets (buffer, 255, pipe))
			{
				char *p, *q;
				int flags;

				p = strchr (buffer, '\t');
				if (!p)
					continue;
				while (isspace (*p))
					p++;
				q = strchr (p, '\t');
				if (!q)
					continue;
				*q++ = 0;
				if (strcmp (p, "00000000"))
					continue;	// Not default route
				while (isspace (*q))
					q++;
				strncpy(gw,q,8); // copy ip of gw
				gw[8] = 0;
				p = strchr (q, '\t');	// Skip over gateway
				if (!p)
					continue;
				while (isspace (*p))
					p++;
				q = strchr (p, '\t');
				if (!q)
					continue;
				*q = 0;
				if (!sscanf (p, "%x", &flags))
					continue;
				if (flags & RTF_UP)
				{
					result = TRUE;
					break;
				}
			}
		}
		pclose(pipe);		
	}
	/* check if we can ping our default gw */
	if (result)
	{
		sscanf(gw,"%02x%02x%02x%02x",
			&ipdigit[0],&ipdigit[1],&ipdigit[2],&ipdigit[3]);
		ip = g_strdup_printf("%s -c 1 -q %i.%i.%i.%i",
			BIN_PING,ipdigit[3],ipdigit[2],ipdigit[1],ipdigit[0]);

		pipe = popen (ip, "r");
		if (pipe > 0)
		{
			while ((feof(pipe) == 0))
			{
				fgets (buffer, 255, pipe);
				if (strstr(buffer,"100%"))
				  result = FALSE;
			}
			pclose(pipe);		
		}		
	}
	return result;
}

static gboolean
cancel_message(gpointer data)
{
	guint id = (guint)data;
	gpe_system_tray_cancel_message(window->window, id);
	return FALSE;
}

gboolean
update_netstatus (void)
{
	gboolean oldstatus = net_is_on;
	guint msgid;
	
	net_is_on = net_get_status();
	
	if (net_is_on != oldstatus)
	{
		msgid = gpe_system_tray_send_message (window->window, 
		          net_is_on ? _("Network connection established.") 
                  : _("Network connection lost."), (unsigned int)0);
		g_timeout_add(4500, (GSourceFunc)cancel_message, (gpointer)msgid);
		update_icon(0);
	}
	return TRUE;
}

gboolean
update_wireless (gchar *essid)
{
	guint msgid;
	gchar *msg = g_strdup_printf("%s %s", _("New wireless link: "), essid);
	
	msgid = gpe_system_tray_send_message (window->window, msg, (unsigned int)0);
	g_timeout_add(4500, (GSourceFunc)cancel_message, (gpointer)msgid);

	g_free(msg);
	
	return TRUE;
}

static void
app_shutdown ()
{
	gtk_main_quit ();
}


static void
do_network_info (void)
{
	GError *err = NULL;

	if (!g_spawn_command_line_async (BIN_INFO " " PARAM_INFO, &err))
	{
		gpe_error_box (_("Could not start network info tool!"));
		g_printerr("err gpe-mininet: %s\n", err->message);
		g_error_free(err);
	}
}


static void
do_network_config (void)
{
	GError *err = NULL;

	if (!g_spawn_command_line_async (BIN_CONFIG " " PARAM_CONFIG, &err))
	{
		gpe_error_box (_("Could not start network configuration tool!"));
		g_printerr("err gpe-mininet: %s\n", err->message);
		g_error_free(err);
	}
}

static void
sigterm_handler (int sig)
{
	app_shutdown ();
}


static void
clicked (GtkWidget *w, GdkEventButton *ev, gpointer data)
{
	gtk_menu_popup (GTK_MENU (menu), NULL, NULL, gpe_popup_menu_position,
	                w, ev->button, ev->time);
}


static gboolean
rtnl_callback (GIOChannel *source, GIOCondition cond, gpointer data)
{
	rtnl_process ((int)data);

	return TRUE;
}


void
rtnl_connect_glib (int fd)
{
	GIOChannel *chan;

	chan = g_io_channel_unix_new (fd);

	g_io_add_watch (chan, G_IO_IN, rtnl_callback, (gpointer)fd);
}

/* handle resizing */
gboolean 
external_event(GtkWindow *window, GdkEventConfigure *event, gpointer user_data)
{
	gint size;

	if (event->type == GDK_CONFIGURE)
	{
		size = (event->width < event->height) ? event->height : event->width;
		update_icon(size);
	}
	return FALSE;
}


int
main (int argc, char *argv[])
{
	Display *dpy;
	GtkWidget *menu_remove;
	GtkWidget *menu_config;
	GtkWidget *menu_info;
	GtkTooltips *tooltips;

	if (gpe_application_init (&argc, &argv) == FALSE)
		exit (1);

	setlocale (LC_ALL, "");

	bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
	bind_textdomain_codeset (PACKAGE, "UTF-8");
	textdomain (PACKAGE);

	netlink_fd = rtnl_open ();
	if (netlink_fd < 0)
	  {
	    gpe_error_box (_("Couldn't open netlink device"));
	    exit (1);
	  }

	rtnl_connect_glib (netlink_fd);

	window = gtk_plug_new (0);
	gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
	gtk_widget_realize (window);

	gtk_window_set_title (GTK_WINDOW (window), _("Network Control"));

	signal (SIGTERM, sigterm_handler);

	menu = gtk_menu_new ();
	menu_info = gtk_menu_item_new_with_label (_("Network Status"));
	menu_config =
		gtk_menu_item_new_with_label (_("Configure Network"));
	menu_remove = gtk_menu_item_new_with_label (_("Remove from dock"));

	g_signal_connect (G_OBJECT (menu_info), "activate",
			  G_CALLBACK (do_network_info), NULL);
	g_signal_connect (G_OBJECT (menu_config), "activate",
			  G_CALLBACK (do_network_config), NULL);
	g_signal_connect (G_OBJECT (menu_remove), "activate",
			  G_CALLBACK (app_shutdown), NULL);

	gtk_widget_set_sensitive(menu_info, !access(BIN_INFO, X_OK));
	gtk_widget_set_sensitive(menu_config, !access(BIN_CONFIG, X_OK));

	gtk_widget_show (menu_info);
	gtk_widget_show (menu_config);
	gtk_widget_show (menu_remove);

	gtk_menu_append (GTK_MENU (menu), menu_info);
	gtk_menu_append (GTK_MENU (menu), menu_config);
	gtk_menu_append (GTK_MENU (menu), menu_remove);

	if (gpe_load_icons (my_icons) == FALSE)
		exit (1);

	net_is_on = net_get_status();

	icon = gtk_image_new_from_pixbuf (gpe_find_icon(net_is_on ? "net-on" :
	                                                "net-off")); 
	gtk_misc_set_alignment (GTK_MISC(icon), 0, 0);
	gtk_widget_show (icon);	
	gpe_set_window_icon (window, "gpe-mininet");

	tooltips = gtk_tooltips_new ();
	gtk_tooltips_set_tip (GTK_TOOLTIPS (tooltips), window,
			      _("This is gpe-mininet - the network control applet."), NULL);

	g_signal_connect (G_OBJECT (window), "button-press-event",
	                  G_CALLBACK (clicked), NULL);
	g_signal_connect (G_OBJECT (window), "configure-event", 
	                  G_CALLBACK (external_event), NULL);
	gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK);

	gtk_container_add (GTK_CONTAINER (window), icon);
	
	dpy = GDK_WINDOW_XDISPLAY (window->window);

	gtk_widget_show (window);

	gpe_system_tray_dock (window->window);

	gtk_main ();
	
	exit (0);
}