File: map.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 (404 lines) | stat: -rw-r--r-- 8,960 bytes parent folder | download | duplicates (4)
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
/*
 * 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 <arpa/inet.h>
#define __BSD_SOURCE
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <time.h>
#include <string.h>
#include <netdb.h>
#include "cheops.h"
#if (__GLIBC__ < 2)
#include "libc5.h"
#endif
struct blah {
	unsigned int addr;
	struct net_page *np;
};

static struct map_request *mrq=NULL;
static struct plink *pq=NULL;

static unsigned int map_lock=0;

static int timeout_installed=0;

static int map_anyway(void *blah_v)
{
	struct blah *blah = (struct blah *)blah_v;
	if (map_lock) {
		if (map_lock == blah->addr)
			return FALSE;
		else
			return TRUE;
	}
	map_lock = blah->addr;
	if (!get_object(blah->np, blah->addr)) 
		add_unique(blah->addr, "unknown.xpm", "  Non-responsive", blah->np);
	g_free(blah);
	map_lock = 0;
	return FALSE;
}

static struct link *connection_exists(struct net_object *no1, struct net_object *no2)
{
	struct link *l;
	l = no1->links;
	while(l) {
		if (l->other == no2)
			break;
		l=l->next;
	}
	return l;
}

static struct plink *plink_exists(unsigned int addr1, unsigned int addr2)
{
	struct plink *p;
	p = pq;
	while(p) {
		if (((p->addr1 == addr1) && (p->addr2 == addr2)) ||
		    ((p->addr2 == addr1) && (p->addr1 == addr2)))
		    	break;
		p=p->next;
	}
	return p;
}

void check_connections(struct net_page *np, struct net_object *no)
{
	struct plink *p;
	struct plink *last;
	struct net_object *no2;
	struct in_addr ia;
	last=NULL;
	p = pq;
	ia.s_addr = no->ip_addr;
	while(p) {
		if (((no->ip_addr == p->addr1) && (no2 = get_object(np, p->addr2))) ||
		    ((no->ip_addr == p->addr2) && (no2 = get_object(np, p->addr1)))) {
			if (last) {
				last->next = p->next;
				g_free(p);
				p=last->next;
			} else {
				pq = p->next;
				g_free(p);
				p = pq;
			}
			if (!connection_exists(no, no2))
				object_link(np, no, no2);
		} else {			
			last=p;
			p=p->next;
		}
	}
	
}

void map_connect(struct net_page *np, unsigned int addr1, unsigned int addr2, int loadasneeded)
{
	struct net_object *no1, *no2;
	struct plink *p;
	struct in_addr ia;
	struct blah *blah;
#if 0
	ia.s_addr = addr1;
	printf("adding connection from %s",inet_ntoa(ia));
	ia.s_addr = addr2;
	printf(" to %s\n", inet_ntoa(ia));
#endif
	if (!valid_np(np))
		return;
	no1 = get_object(np, addr1);
	no2 = get_object(np, addr2);
	if (no1 && no2) {
		if (!connection_exists(no1, no2))
			object_link(np, no1, no2);
	} else {
		if (!no2 && loadasneeded) {
			ia.s_addr = addr2;
#ifdef DEBUG
			printf("Starting search for %s\n",inet_ntoa(ia));
#endif
			if (option_queso) {
				/* In case we don't discover anything, queue up a forced host */
				blah = g_new0(struct blah, 1);
				blah->np = np;
				blah->addr = addr2;
				gtk_timeout_add(10000, map_anyway, blah);
				discover_network_a(np, inet_ntoa(ia), "255.255.255.255", option_add_routes);
			} else {
				add_unique(addr2, "desktop.xpm", "  Not examined",np);
				no2 = get_object(np, addr2);
				if (!no1) {
					add_unique(addr1, "desktop.xpm", "  Not examined", np);
					no1 = get_object(np, addr1);
				}
				if (no1 && no2)
					object_link(np, no1, no2);
				return;
			}
		}
		if (!plink_exists(addr1, addr2)) {
#if 0
			ia.s_addr = addr1;
			printf("Queuing a connection from %s",inet_ntoa(ia));
			ia.s_addr = addr2;
			printf(" to %s\n", inet_ntoa(ia));
#endif
			/* Queue a connection for when this host is discovered */
			p = g_new0(struct plink, 1);
			p->addr1 = addr1;
			p->addr2 = addr2;
			p->next = pq;
			pq = p;
		} 
#ifdef DEBUG
			else
			printf("Connection already queued\n");
#endif

	}
}

static void mr_probe(struct map_request *mr)
{
	struct in_addr ia;
	ia.s_addr = mr->dest;
	mr->retries++;
	mr->sent=time(NULL);
	sendicmp(mr->ttl, ia, 1);
}

static int map_timeout(void *data)
{
	time_t current = time(NULL);
	struct map_request *mr, *l;
	struct in_addr ia;
	int cnt=0;
	if (!mrq) {
		timeout_installed=0;
		set_status("Mapping Complete");
		return FALSE;
	}
	l=NULL;
	mr = mrq;
	while(mr) {
		if (current > mr->sent + REQUEST_TIMEOUT) {
			cnt++;
			/* Limit how many retransmits we do.  This should stabalize replies a bit and
			   reduce flooding */
			if (cnt > MAP_TIMEOUT_MAX) {
#ifdef DEBUG
				printf("Too many timeouts!  Waiting for another opportunity\n");
#endif
				return TRUE;
			}
			ia.s_addr=mr->dest;
			if (mr->retries > MAX_REQUEST_RETRIES) {
#ifdef DEBUG
				printf("too many retries on host %s...  Skipping\n",inet_ntoa(ia));
#endif
				if (l) {
					l->next = mr->next;
					g_free(mr);
					mr = l->next;
				} else {
					mrq = mr->next;
					mr = mrq;
				}
			} else {
#ifdef DEBUG
				printf("retransmit %d to host %s\n", mr->retries + 1, inet_ntoa(ia));
#endif
				mr_probe(mr);
			}
		} else {
			l = mr;
			mr = mr->next;
		}
			
	}
	return TRUE;
}

void start_mapping(struct net_object *no)
{
	struct map_request *mr;
	struct net_page *np;
	struct in_addr lip;
	char buf[256];
	lip = getlocalip(no->ip_addr);
	np = no->np;
	/* First check to see if there is already a request */
	mr = mrq;
	while(mr) {
		if ((mr->dest == no->ip_addr) && (np == mr->np)) {
#ifdef DEBUG
			printf("Warning: duplicate map request\n");
#endif
			return;
		}
		mr=mr->next;
	}
	g_snprintf(buf,sizeof(buf), "Mapping '%s'", no->hostname);
	set_status(buf);
	mr = g_new0(struct map_request, 1);
	mr->np = np;
	mr->dest = no->ip_addr;
	mr->last = lip.s_addr;
	if (!valid_np(np))
		return;
	if (!get_object(np, lip.s_addr))
		if (option_queso)
			discover_network_a(np, inet_ntoa(lip), "255.255.255.255", option_add_routes);
	mr->ttl = 1;
	mr->retries = 0;
	mr->sent=0;
	mr->next = mrq;
	mrq = mr;
	if (!timeout_installed)
		timeout_installed=gtk_timeout_add(100, map_timeout, NULL);

#ifdef DEBUG
	printf("mapping %s\n",no->hostname);
#endif
	mr_probe(mr);

}

int handle_map(char *buf, int len, struct sockaddr_in *sin)
{
	struct ip *ip = (struct ip *)buf;
	struct icmp *icmp = (struct icmp *)(buf + (ip->ip_hl << 2));
	struct udphdr *udp = (struct udphdr *)(buf + (ip->ip_hl << 2));
	struct map_request *mr, *last=NULL;
	char host[256];
	unsigned char ttl;
	int res=0;
#ifdef DEBUG
	printf("map reply of %d bytes\n",len);
#endif
	
	switch(icmp->icmp_type) {
	case ICMP_TIME_EXCEEDED:
		/* Get original header, etc */
		buf += ((ip->ip_hl << 2) + ICMP_MINLEN);
		len -= ((ip->ip_hl << 2) + ICMP_MINLEN);
		ip = (struct ip *)buf;
		icmp = (struct icmp *)(buf + (ip->ip_hl << 2));
		udp = (struct udphdr *)(buf + (ip->ip_hl << 2));
		if (len < (ip->ip_hl << 2) + ICMP_MINLEN)
			break;
#ifdef DEBUG
		printf("Revised length: %d\n",len);
#endif
		if (ip->ip_p == IPPROTO_ICMP)
			ttl = ntohs(icmp->icmp_seq);
		else
			ttl = ntohs(udp->dest) - EMPTY_PORT;
		strcpy(host,inet_ntoa(sin->sin_addr));
#ifdef DEBUG
		printf("%s is hop %hu to %s\n", host,ttl, inet_ntoa(ip->ip_dst));
#endif
		mr=mrq;
		last=NULL;
		while(mr) {
			/* See if this is the right place and the right time */
			if ((mr->dest == ip->ip_dst.s_addr) && (ttl = mr->ttl)) {
#ifdef DEBUG
				printf("Found map record for %s\n",inet_ntoa(ip->ip_dst));
#endif
				if (ttl < MAP_MAX) {
					mr->ttl++;
					mr->retries=0;
					map_connect(mr->np, mr->last, sin->sin_addr.s_addr, 1);
					mr->last = sin->sin_addr.s_addr;
					mr_probe(mr);
					last = mr;
					mr = mr->next;
				} else {
					if (last) {
						last->next = mr->next;
						g_free(mr);
						mr = last->next;
					} else {
						mrq = mr->next;
						mr = mrq;
					}
				}
			} else {
				last=mr;
				mr=mr->next;
			}
		}
		res=1;
		break;
	case ICMP_DEST_UNREACH:
		/* Get original header, etc */
#if 0
		if (icmp->icmp_id != htons((getpid() & 0xffff)))
			break;
#endif
		buf += ((ip->ip_hl << 2) + ICMP_MINLEN);
		len -= ((ip->ip_hl << 2) + ICMP_MINLEN);
		ip = (struct ip *)buf;
		
		icmp = (struct icmp *)(buf + (ip->ip_hl << 2));
		udp = (struct udphdr *)(buf + (ip->ip_hl << 2));
		if (ntohs(udp->dest) < EMPTY_PORT) 
			break;
		/* Fall through */
	case ICMP_ECHOREPLY:
		if (len != (ip->ip_hl << 2) + ICMP_MINLEN)
			break;
		if (ip->ip_p == IPPROTO_ICMP)
			ttl = ntohs(icmp->icmp_seq);
		else 
			/* SNMP responses can also generate DEST_UNREACH */
			ttl = ntohs(udp->dest) - EMPTY_PORT;
		if (ttl >= MAP_MAX) 
			break;
		strcpy(host,inet_ntoa(sin->sin_addr));
#ifdef DEBUG
		printf("%s is last hop %u\n", host, ttl);
#endif
		last=NULL;
		mr=mrq;
		while(mr) {
			/* See if this is the right place and the right time */
			if ((mr->dest == sin->sin_addr.s_addr) && (ttl = mr->ttl)) {
#ifdef DEBUG
				printf("Found map record for %s\n",inet_ntoa(sin->sin_addr));
#endif
				map_connect(mr->np, mr->last, sin->sin_addr.s_addr, 1);
				if (last) {
					last->next = mr->next;
					g_free(mr);
					mr = last->next;
				} else {
					mrq = mr->next;
					mr = mrq;
				}
			} else {
				last=mr;
				mr=mr->next;
			}
		}
		res=1;
	}
	return res;
}