File: status.c

package info (click to toggle)
cheops 0.61-15.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,272 kB
  • ctags: 1,312
  • sloc: ansic: 12,731; sh: 155; makefile: 151
file content (159 lines) | stat: -rw-r--r-- 3,947 bytes parent folder | download | duplicates (3)
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
/*
 * Cheops Network User Interface
 *
 * Copyright (C) 1999, Adtran, Inc.
 * 
 * Distributed under the terms of the GNU GPL
 *
 */

#include <gtk/gtk.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <netdb.h>
#include "cheops.h"

#define MAX_FD 1024		/* Max # of file descriptors */

GtkWidget *status=NULL;
GtkWidget *descriptors=NULL;

static int have_refresh=0;

static void close_status()
{
	if (status) {
		gtk_widget_destroy(status);
		status=NULL;
	}
	gtk_timeout_remove(have_refresh);
}

static void update_desc()
{
	char buf[256];
	int s; int max=0;
	struct sockaddr_in sin;
	int addrlen;
	for (s=0;s<MAX_FD;s++) {
		addrlen = sizeof(sin);
		if ((!getsockname(s, &sin, &addrlen)) || (errno == ENOTSOCK))
			max++;
			
	}
	snprintf(buf, sizeof(buf), "%d  ", max);
	gtk_label_set(GTK_LABEL(descriptors), buf);
}

static void update_status()
{
	update_desc();
}

static int refresh()
{
	update_status();
	return TRUE;
}

static void desc_dump()
{
	int res;
	int s=0;
	struct sockaddr_in sin;
	int addrlen;
	int ret;
	int alen;
	char *t = "unknown";

	fprintf(stderr, "Sockets and File Descriptor Dump\n");
	fprintf(stderr, "================================\n");
	/* Dump location of file descriptors to stderr */
	for (s=0;s<MAX_FD;s++) {
		addrlen = sizeof(sin);
		alen = sizeof(ret);
		res = getsockopt(s, SOL_SOCKET, SO_TYPE, &ret, &alen);
		if (res > -1) {
			printf("ret is %d\n", ret);
			if (ret == SOCK_DGRAM) {
				t = "udp";
			} else if (ret == SOCK_STREAM) {
				t = "tcp";
			} else if (ret == SOCK_RAW) {
				t = "raw";
			}
		}
		res = getsockname(s, &sin, &addrlen);
		if (res > -1) {
			fprintf(stderr, "%d: %s socket from %s:%d", s, t, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
			addrlen = sizeof(sin);
			res = getpeername(s, &sin, &addrlen);
			if (res > -1) 
				fprintf(stderr, " to %s:%d\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
			else
				fprintf(stderr, " to <unconnected>\n");
		} else {
			if (errno == ENOTSOCK) 
				fprintf(stderr, "%d: file descriptor\n", s);
		}	
	}				
}

void show_status()
{
	GtkWidget *table;
	GtkWidget *vbox;
	GtkWidget *bbox;
	GtkWidget *close;
	GtkWidget *label;
	GtkWidget *frame;
	GtkWidget *dump;
	if (status) {
		gtk_widget_show(status);
		return;
	}
	status = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_widget_realize(status);
	gtk_window_set_title(GTK_WINDOW(status), "Cheops Status");
	fix_icon(status->window);
	gtk_signal_connect(GTK_OBJECT(status), "delete_event", close_status, NULL);
	
	vbox = gtk_vbox_new(FALSE, 5);
	gtk_widget_show(vbox);
	table = gtk_table_new(1, 3, FALSE);
	gtk_container_border_width(GTK_CONTAINER(table), 10);
	gtk_widget_show(table);
	frame = gtk_frame_new("Status");
	gtk_widget_show(frame);
	gtk_container_border_width(GTK_CONTAINER(frame), 10);
	gtk_container_add(GTK_CONTAINER(frame), table);
	gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 5);
	
	label = gtk_label_new("Open File Descriptors: ");
	gtk_widget_show(label);
	descriptors = gtk_label_new("<unknown>");
	gtk_widget_show(descriptors);
	dump = gtk_button_new_with_label("Dump to stderr...");
	gtk_widget_show(dump);
	gtk_signal_connect(GTK_OBJECT(dump), "clicked", desc_dump, NULL);
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
	gtk_table_attach_defaults(GTK_TABLE(table), descriptors, 1, 2, 0, 1);
	gtk_table_attach_defaults(GTK_TABLE(table), dump, 2, 3, 0, 1);
	
	close = gtk_button_new_with_label("  Close  ");
	gtk_widget_show(close);
	gtk_signal_connect(GTK_OBJECT(close), "clicked", close_status, NULL);
	bbox = gtk_hbox_new(FALSE, 5);
	gtk_widget_show(bbox);
	gtk_box_pack_end(GTK_BOX(bbox), close, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 5);
	gtk_container_add(GTK_CONTAINER(status), vbox);
	update_status();
	gtk_widget_show(status);
	have_refresh = gtk_timeout_add(2000, refresh, NULL);
}