File: network.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 (440 lines) | stat: -rw-r--r-- 10,160 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * Cheops Network User Interface
 *
 * Copyright (C) 1999, Adtran, Inc.
 * 
 * Distributed under the terms of the GNU GPL
 *
 */

#include <gtk/gtk.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define __BSD_SOURCE
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>
#include "cheops.h"
#if (__GLIBC__ < 2)
#include "libc5.h"
#endif

int seqno = 0;
static int priv_fd=-1;
static int ident;

static struct ping_request *pqueue=NULL;

static struct sockaddr_in self;
static char hostname[256];
static int caught_idle = 0;
static int caught_discov = 0;

void (*ping_callback)(struct net_object *no)=NULL;

char *print_aliases(struct net_object *no, char c)
{
	static char buf[1024];
	struct alias *a;
	int flag=0;
	char tmp[256];
	buf[0] = '\0';
	a = no->aliases;
	while(a) {
		if (strcasecmp(no->hostname, a->hostname)) {
			if (!flag) {
				if (c == '\n')
					g_snprintf(buf, sizeof(buf), "Aliases:%c", c);
				flag++;
			}
			g_snprintf(tmp, sizeof(tmp), "  %s%c", a->hostname, c);
			strncat(buf, tmp, sizeof(buf) - strlen(buf));
		}
		a=a->next;
	}
	return buf;
}

#ifdef SNMP
void net_snmp_callback(void *data)
{
	struct net_object *no = (struct net_object *)data;
	char *expected=NULL;
#ifdef DEBUG
	printf("SNMP update to %s, \n", no->hostname);
#endif
	no->hassnmp = 1;
	/* We should be a little bit more flexible, but for now
	   we'll just hack a few things in hard-coded like */
	if (strstr(no->sysDescr, "Windows"))
		expected="windows.xpm";
	if (strstr(no->sysDescr, "Linux"))
		expected="linux.xpm";
	if (strstr(no->sysDescr, "Cisco Internetwork"))
		expected="router.xpm";
	if (expected && strcmp(expected, no->fn)) {
		strncpy(no->fn, expected, sizeof(no->fn));
		set_icon(no, no->fn);
	}
	fix_tooltip(no);
}

static void snmp_update(struct net_object *no, struct net_page *np)
{
	/* We'll check out some nice convenient SNMP fields first */
	static char *fields[] = { "system.sysName.0",
				  "system.sysDescr.0",
				  "system.sysObjectID.0",
				  "system.sysContact.0" };
	char **recv;
	recv = g_new0(char *, 4);
	recv[0]=no->sysName;
	recv[1]=no->sysDescr;
	recv[2]=no->sysObjectID;
	recv[3]=no->sysContact;
	snmp_get_async(no, np, "public", fields, 4, recv, net_snmp_callback, no);
}
#endif

void add_unique(unsigned int addr, char *icon, char *os, struct net_page *np)
{
	struct net_object *no;
	struct hostent *hp;
	struct in_addr iadr;
	const char *name;
	struct alias *a;
	iadr.s_addr = addr;
	name = inet_ntoa(iadr);
	if (!valid_np(np))
		return;
	if (!(no = get_object(np, addr))) {
#ifdef DEBUG
		printf("Adding unique '%s' with icon '%s', os is '%s'\n",name, icon, os);
#endif
		a = build_aliases(addr);
		if (a)
			name = a->hostname;
		if (option_reverse_dns) {
			hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
			if (hp) 
				name = hp->h_name;
		}
		no = network_object(np);
		strncpy(no->hostname, name, sizeof(no->hostname));
		no->ip_addr = addr;
		no->aliases = a;
		strncpy(no->os, os, sizeof(no->os));
		strncpy(no->fn, icon, sizeof(no->fn));
		set_icon(no, no->fn);
		fix_label(no);
		add_object(np, no);
		fix_tooltip(no);
#ifdef SNMP
		if (option_snmp) 
			snmp_update(no, np);
#endif
		monitor_update(no);
	} else {
		/* Kludge: Don't overwrite a good analysis with a bad one */
		if (strcmp(icon, "desktop.xpm")) {
			strncpy(no->fn, icon, sizeof(no->fn));
			strncpy(no->os, os, sizeof(no->os));
			set_icon(no, no->fn);
			fix_tooltip(no);
		}
#ifdef SNMP
		if (option_snmp) 
			snmp_update(no, np);
#endif
	}

}

int valid_np(struct net_page *np)
{
	GList *l;
	struct net_page *npt;
	GtkNotebookPage *gnp;
	
	if (!np)
		return 0;
	/* Search for np and be sure it's valid */
	l = g_list_first(GTK_NOTEBOOK(main_window.notebook)->children);
	npt = NULL;
	while(l) {
		gnp = (GtkNotebookPage *)(l->data);
		npt = (struct net_page *)gtk_object_get_user_data(GTK_OBJECT(gnp->child));
		if (np == npt)
			break;
		l = g_list_next(l);
	}
	if (npt == np)
		return -1;
	else
		return 0;
}

int valid_no(struct net_page *np, struct net_object *no)
{
	struct net_object *tno;
	if (!valid_np(np)) {
#ifdef DEBUG
		printf("%s is not on a valid page\n", no->hostname);
#endif
		return 0;
	}
	tno = np->objs;
	while(tno) {
		if (tno == no)
			break;
		tno = tno->next;
	}
	if (tno == no) {
		return -1;
	} else {
#if 0
		printf("%s (%p) is not on the specified page\n", no->hostname, no);
		tno = np->objs;
		while(tno) {
			printf("-- %s (%p)\n", tno->hostname, tno);
			tno = tno->next;
		}
#endif
		return 0;
	}
}	

void handle_packet(char *buf, int len, struct sockaddr_in *sin)
{
	struct ip *ip;
	struct icmp *i;
	struct net_page *np;
	struct net_object *no;
	int hlen;
	ip = (struct ip *)buf;
	hlen = ip->ip_hl << 2;
	if (len < hlen + ICMP_MINLEN) {
		fprintf(stderr, "handle_packet: too short (%d bytes) from %s\n",
			len, inet_ntoa(sin->sin_addr));
		return;
	}
	i = (struct icmp *)(buf + hlen);
	if ((i->icmp_type == ICMP_TIME_EXCEEDED)  || (i->icmp_id == htons(getpid()))
	     || (i->icmp_type == ICMP_DEST_UNREACH))
		if (handle_map(buf, len, sin))
			return;

	if (i->icmp_id != htons(ident)) 
		return;
	
	if (i->icmp_type != ICMP_ECHOREPLY)
		return;
	if (len < hlen + ICMP_MINLEN + sizeof(np)) {
		fprintf(stderr,"handle_packet: too short (%d bytes) from %s (2)\n", len, inet_ntoa(sin->sin_addr));
		return;
	}
	memcpy(&np, &buf[hlen + ICMP_MINLEN], sizeof(np)); 
	/* Ignore if it's invalid, for example if the page is deleted already */
	if (!valid_np(np)) 
		return;
	if (!(no = get_object(np, sin->sin_addr.s_addr))) {
		if (caught_discov) {
			gtk_timeout_remove(caught_discov);
			caught_discov = 0;
		}
		if (option_queso) {
			examine_host_queue(sin->sin_addr.s_addr, try_ports[0], np ? np : current_page);
		} else {
			add_unique(sin->sin_addr.s_addr,"desktop.xpm", "  Not examined", np ? np : current_page);
		}
	} else
		if (no->mon && ping_callback)
			ping_callback(no);
}

void network_socket_cb(void *d, int fd, GdkInputCondition condition)
{
	char buf[1024];
	int len;
	struct sockaddr_in sin;
	int sinlen = sizeof(sin);
     	fd_set fds;
     	struct timeval tv;
	do {
		if ((len = recvfrom(fd, buf, sizeof(buf), 0,
	   	     (struct sockaddr *)&sin, &sinlen)) < 0) {
	    		perror("network_socket_cb: recvfrom");
		} else {
			handle_packet(buf, len, &sin);
		
		}
     		tv.tv_sec = 0;
     		tv.tv_usec = 0;
     		FD_ZERO(&fds);
     		FD_SET(fd, &fds);
	} while (select(fd + 1, &fds, NULL, NULL, &tv) > 0);
}

int net_init()
{
	struct protoent *proto;
	struct hostent *host_ptr;
	int hold;
	
	ident = getpid() & 0xFFFF;
	if (!(proto = getprotobyname("icmp"))) {
                perror("ping: unknown protocol icmp");
                exit(1);
        }
        if ((priv_fd = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
                perror("ping: socket");
                exit(1);
        }
	
		/* Set the receive buffer size	*/
	hold = 48 * 1024;
        setsockopt(priv_fd, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
            sizeof(hold));

	if (gethostname(hostname, sizeof(hostname)) == 0) {
		host_ptr = gethostbyname(hostname);
		if (host_ptr) {
			memcpy(&self.sin_addr, host_ptr->h_addr, sizeof(unsigned int));
			self.sin_family = AF_INET;
			self.sin_port = 0;
			gdk_input_add(priv_fd, GDK_INPUT_READ, network_socket_cb, NULL);
		}
		return 0;
	} else
		perror("ping: gethostname");
		
	exit(1);
}

static int idle_network()
{
	/*
	 * Do a ping that is in the queue
	 */
	struct ping_request *pr;
	static char buf[256];
	struct sockaddr_in to;
	int len=8 + sizeof(void *), sent;
	struct icmp *i;

	pr = pqueue;
	if (pr) {
		memset(&to, 0, sizeof(struct sockaddr_in));
		to.sin_family = AF_INET;
		to.sin_addr.s_addr = pr->addr;
		i = (struct icmp *)buf;
		i->icmp_type = ICMP_ECHO;
		i->icmp_code = 0;
		i->icmp_cksum = htons(0);
		i->icmp_seq = htons(seqno++);
		i->icmp_id = htons(ident);	
		memcpy(&buf[8], &(pr->data), sizeof(pr->data));
		i->icmp_cksum = inet_checksum(i, len);
		sent = sendto(priv_fd, buf, len, 0, (struct sockaddr *)&to, sizeof(to));
#if 0
		if (sent < 0 || sent != len) {
			if (sent < 0)
				perror("init_scan_host: sendto");
			printf("init_scan_host: wrote %s %d chars, ret = %d\n",
				inet_ntoa(to.sin_addr), len, sent);
		}
#endif
#if 1
		usleep(Zzz);
#endif
		pqueue = pqueue->next;
		g_free(pr);
	} else {
		caught_idle = 0;
		if (!option_queso) {
			gdk_window_set_cursor(main_window.window->window, NULL);
			set_status("Discovery complete");
		}
		return FALSE;
	}
	return TRUE;
}

int init_scan_host(unsigned int ip, void *data)
{
	struct ping_request *pr;
	
	pr = g_new0(struct ping_request, 1);
	pr->next = pqueue;
	pr->addr = ip;
	pr->data = data;
	pqueue = pr;
  	if (!caught_idle) {
  		gtk_idle_add(idle_network, NULL);  
		caught_idle =  1;
  	}
	return 0;
}

static int discover_network(struct net_page *np, struct in_addr addr, struct in_addr netmask)
{
	unsigned int start;
	unsigned int finish;
	unsigned int mask;
	unsigned int x;
	
	mask = ntohl(netmask.s_addr);
	start = ntohl(addr.s_addr) & mask;
	finish = start + ~mask;
	if (start != finish) {
		start++;
		finish--;
		
	}
	for (x=start;x <= finish; x++)
		if (!get_object(np, htonl(x))) 
			if (valid_np(np))
				init_scan_host(htonl(x), np);
	return 0;
}

static int cancel_discovery() {
	gdk_window_set_cursor(main_window.window->window, NULL);
	set_status("No hosts discovered");
	caught_discov = 0;
	return FALSE;
};


int discover_network_a(struct net_page *np, char *addr, char *netmask, int add)
{
	struct in_addr a,m;
	struct network *n;
	char buf[256];
	
	if (inet_aton(addr, &a) && inet_aton(netmask, &m)) {
		if (add) {
			n = g_new0(struct network, 1);
			n->next = np->ncontents;
			np->ncontents = n;
			n->net_addr = a.s_addr;
			n->net_mask = m.s_addr;
			append_network(np->pe, n);
		}
		if (!caught_discov)
			caught_discov = gtk_timeout_add(5000, cancel_discovery, NULL);
		immediate_setcursor(GDK_WATCH);
		g_snprintf(buf, sizeof(buf), "Discovering %s/%s", addr, netmask);
		set_status(buf);
		return discover_network(np, a, m);
	} else {
		return -1;
	}
}