File: multinet.c

package info (click to toggle)
dnprogs 2.52
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,644 kB
  • ctags: 4,021
  • sloc: ansic: 26,737; cpp: 10,666; makefile: 832; sh: 537; awk: 13
file content (739 lines) | stat: -rw-r--r-- 16,931 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/**********************************************************************************
    (c) 2006,2008     Christine Caulfield        christine.caulfield@googlemail.com

    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
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    ********************************************************************************


    Multinet server proxy server for Linux.

    This daemon talks to a VMS system running a multinet DECnet/IP tunnel.
    (www.process.com/tcpip/multinet.html)

    You'll need to set up the VMS end to talk to this box.
    It creates a tun/tap device and assigns it a suitable
    MAC address.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <errno.h>
#include <sys/signal.h>
#include <sys/signal.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#ifdef __NetBSD__
#include <net/if.h>
#include <net/if_tun.h>
#else
#include <linux/if.h>
#include <linux/if_tun.h>
#endif

static int verbose;
static int ipfd;
static int tunfd;
static unsigned short local_addr[2];
static struct sockaddr_in remote_addr;
static int got_remote_addr;
static unsigned char remote_decnet_addr[2];
static char *remote_host_name;
static int got_verification;

static int router_priority = 64;
static int router_level = 2;
static int mtu = 578;
static int port = 700;
static int ip_timeout = 300;
static int hello_timer = 60;
static char old_default[1024];
static time_t last_ip_packet;
static sig_atomic_t running;
static sig_atomic_t reload_config;

#define DUMP_MAX 1024

/* Resolve the remote IP host name.
 *  Called at startup and on receipt of SIGHUP
 */
static int lookup_name(void)
{
	struct addrinfo *ainfo;
	struct addrinfo ahints;
	int res;

	memset(&ahints, 0, sizeof(ahints));
	ahints.ai_socktype = SOCK_DGRAM;
	ahints.ai_protocol = IPPROTO_UDP;

	/* Lookup the nodename address */
	if ( (res=getaddrinfo(remote_host_name, NULL, &ahints, &ainfo)) )
	{
		fprintf(stderr, "Can't resolve name '%s': %s\n", remote_host_name, gai_strerror(res));
		return 2;
	}

	memcpy(&remote_addr, ainfo->ai_addr, sizeof(struct sockaddr_in));
	remote_addr.sin_family = AF_INET;
	remote_addr.sin_port = htons(port);

	return 0;
}

static void do_sighup(int sig)
{
	if (verbose)
		fprintf(stderr, "Got sighup, re-reading config\n");
	reload_config = 1;
}

static void do_shutdown(int sig)
{
	if (verbose)
		fprintf(stderr, "Got signal, shutting down\n");
	running = 0;
}

static int send_ip(int fudge_header, unsigned char *, int len);

static void dump_data(char *from, unsigned char *databuf, int datalen)
{
	int i;
	if (!verbose)
		return;

	fprintf(stderr, "%s (%d)", from, datalen);
	for (i=0; i<(datalen>DUMP_MAX?DUMP_MAX:datalen); i++)
	{
		fprintf(stderr, "%02x  ", databuf[i]);
	}
	fprintf(stderr, "\n");
}

static void send_start(unsigned short addr)
{
	unsigned char start[] = { 0x01, 0x08, 0x05, 0x05, 0x40,0x02, 0x02,0x00, 0x00,0x2c, 0x01,0x00, 0x00,0x00,};
	unsigned char verf[] =  { 0x03, 0x02, 0x0c, 0x00};

	start[1] = addr & 0xff;
	start[2] = addr >> 8;

	send_ip(0, start, sizeof(start));

	verf[1] = addr & 0xff;
	verf[2] = addr >> 8;

	send_ip(0, verf, sizeof(verf));

}


static int send_tun(int mcast, unsigned char *buf, int len)
{
	unsigned char header[38];
	struct iovec iov[2];
	int header_len;

	if (!got_remote_addr)
		return 0; /* Can't send yet */

	memset(header, 0, sizeof(header));

	/* Add ethernet header */
	header[0] = 0xAA;
	header[1] = 0x00;
	header[2] = 0x04;
	header[3] = 0x00;
	header[4] = local_addr[0];
	header[5] = local_addr[1];

	if (mcast) /* Routing multicast - type may need to be in callers params */
	{
		header[6]  = 0xab;
		header[7]  = 0x00;
		header[8]  = 0x00;
		header[9]  = 0x03;
		header[10] = 0x00;
		header[11] = 0x00;

		header_len = 14;
		// TODO Maybe want to send to 09:00:2b:02:00:00 too,
		// Not sure why VMS sends to both, probably backward compatibility
	}
	else
	{
		header[6] = 0xAA;
		header[7] = 0x00;
		header[8] = 0x04;
		header[9] = 0x00;
		header[10] = remote_decnet_addr[0];
		header[11] = remote_decnet_addr[1];

		header_len = sizeof(header);
	}
	header[12] = 0x60; /* DECnet packet type */
	header[13] = 0x03;

	if (!mcast)
	{
		/* DECnet packet length */
		header[14] = (len+16) & 0xFF;
		header[15] = (len+16) >> 8;

		/* Fake Long DECnet header */
		header[16] = 0x81; header[17] = 0x26;  // TODO Don't know what this is!
		header[18] = header[19] = 0;

		header[20] = header[28] = 0xAA;
		header[21] = header[29] = 0x00;
		header[22] = header[30] = 0x04;
		header[23] = header[31] = 0x00;
		header[24] = buf[1]; /* Dest addr */
		header[25] = buf[2];
		header[32] = buf[3]; /* src addr */
		header[33] = buf[4];

		buf += 6;
		len -= 6;
	}

	iov[0].iov_base = header;
	iov[0].iov_len = header_len;
	iov[1].iov_base = buf;
	iov[1].iov_len = len;

	dump_data("to TUN0:", header, header_len);
	dump_data("to TUN1:", buf, len);

	writev(tunfd, iov, 2);
	return len;
}

static int send_ip(int fudge_header, unsigned char *buf, int len)
{
	struct iovec iov[2];
	unsigned char header[4];
	struct msghdr msg;
	static unsigned short seq;

	memset(&msg, 0, sizeof(msg));
	seq++;
	header[0] = seq & 0xFF;
	header[1] = seq >> 8;
	header[2] = header[3] = 0;

	if (fudge_header)
	{
		/* Shorten DECnet addresses */
		buf[0] = 0x02;    /* Short data message */
		buf[1] = buf[8];  /* Destination */
		buf[2] = buf[9];
		buf[3] = buf[16]; /* Source */
		buf[4] = buf[17];
		buf[5] = 0;
		memmove(buf+6, buf+22, len-16);

		len -= 16;
	}

	iov[0].iov_base = header;
	iov[0].iov_len = sizeof(header);
	iov[1].iov_base = buf;
	iov[1].iov_len = len;

	msg.msg_iov = iov;
	msg.msg_iovlen = 2;
	msg.msg_name = (void *)&remote_addr;
	msg.msg_namelen = sizeof(struct sockaddr_in);

	dump_data("Send to IP0", header, sizeof(header));
	dump_data("Send to IP1:", buf, len);

	if (sendmsg(ipfd, &msg, 0) <= 0)
		perror("sendmsg");

	return 0;
}


static int setup_ip(int port)
{
	int fd;
	int flag = 1;
	struct sockaddr_in sin;

	fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (fd < 0)
		return -1;


	setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof(flag));

	sin.sin_family = AF_INET;
	sin.sin_port = htons(port);
	sin.sin_addr.s_addr = INADDR_ANY;

	if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)))
	{
		perror("bind");
		close(fd);
		return -1;
	}

	return fd;
}

void resend_start(int sig)
{
	if (!got_verification)
	{
		unsigned short addr = (local_addr[0] | local_addr[1]<<8);
		send_start(addr);
		alarm(10);
	}
}

static void read_ip(void)
{
	unsigned char buf[1600];
	int len;
	struct sockaddr_in sin;
	unsigned int sinlen = sizeof(sin);

	len = recvfrom(ipfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, &sinlen);
	if (len <= 0) return;

	/* Ignore packets from people we're not talking to */
	if (sin.sin_port != remote_addr.sin_port ||
	    sin.sin_addr.s_addr != remote_addr.sin_addr.s_addr)
		return;

	last_ip_packet = time(NULL);

	dump_data("from IP:", buf, len);

	if (buf[4] == 0x05) /* PtP hello, make into ethernet hello */
	{
		unsigned char hello[] = {
			0x00, 0x00,           /* Length, filled in later */
			0x0b,                 /* FLAGS: Router hello */
			0x02, 0x00, 0x00,     /* Router version */
			0xaa, 0x00, 0x04, 0x00, buf[5], buf[6], /* Routers MAC addr */
			3-router_level,         /* Info, including routing level */
			mtu % 0xFF, mtu >> 8, /* Data block size  */
			router_priority,      /* Priority */
			0x00,                 /* Reserved */
			hello_timer&0xFF,
			hello_timer >> 8,     /* Hello timer (seconds) */
			0x00,                 /* Reserved */
			0x0f,                 /* Length of (other 'logical' ethernets) message that follows */
			0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* "e-list" name */
			0x07,                 /* Elist name */
			0xaa, 0x00, 0x04, 0x00, remote_decnet_addr[0], remote_decnet_addr[1],
			0x40 /* state : (=priority) */
		};

		hello[0] = sizeof(hello); /* Allow me to edit it at will */
		send_tun(1, hello, sizeof(hello));

		got_verification = 1;
		alarm(0); /* cancel START timer */
	}

	/* Trap INIT & VERF & test messages, they're for us */
	if (buf[4] == 0x01 || buf[4] == 0x05 || buf[4] == 0x03)
	{
		if (!got_remote_addr)
		{
			unsigned short addr = buf[6]<<8 | buf[5];
			got_remote_addr = 1;
			remote_decnet_addr[0] = buf[5];
			remote_decnet_addr[1] = buf[6];

			printf("Remote address = %d.%d (%d)\n", addr>>10, addr&1023, addr);
		}
		return;
	}

	if (buf[4] == 0x07 || buf[4] == 0x09) /* Routing info */
	{
		/*
		  off ethernet:
		  13:17:58.021480 lev-2-routing src 3.35 {areas 1-64 cost 4 hops 1}
		  0x0000:  8800 0923 0c00 3f00 0100 0404 0a04 0000
		  0x0010:  ff7f ff7f ff7f ff7f ff7f ff7f ff7f 0404
		  0x0020:  ff7f ff7f ff7f ff7f ff7f ff7f ff7f ff7f
		  0x0030:  ff7f ff7f ff7f ff7f ff7f ff7f 2428 1e08
		  0x0040:  ff7f ff7f ff7f ff7f ff7f ff7f ff7f ff7f
		  0x0050:  ff7f ff7f ff7f ff7f ff7f ff7f ff7f ff7f
		  0x0060:  ff7f ff7f ff7f ff7f ff7f ff7f ff7f ff7f
		  0x0070:  ff7f ff7f ff7f ff7f ff7f ff7f ff7f 1408
		  0x0080:  ff7f ff7f ff7f ff7f 8d44

		  off Multinet:
		  09  00  00  00  multinet header
		  09  23  0c  00  3f  00  01  00  04  04  0a  04  00  00
		  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  04  04
		  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f
		  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  04  04  1e  08
		  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f
		  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f
		  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f
		  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  ff  7f  14  08
		  ff  7f  ff  7f  ff  7f  ff  7f  6d  20
		*/
		buf[2] = len % 0xFF;
		buf[3] = len >> 8;
		send_tun(1, buf+2, len-2);
		return;
	}

	send_tun(0, buf+4, len-4);
}

static void read_tun(void)
{
	unsigned char buf[1600];
	int len;

	len = read(tunfd, buf, sizeof(buf));
	if (len <= 0) return;

	/* We get local HELLOs from time to time so this should ensure that we're not
	   flooding the IP link with dodgy UDP continously
	*/
	if (time(NULL) - last_ip_packet > ip_timeout)
	{
		got_verification = 0;
		resend_start(SIGALRM);
	}

	/* Only forward DECnet packets... */
	if (buf[12] == 0x60 && buf[13] == 0x03)
	{
		dump_data("DECnet from TUN:", buf, len);

		/* Ignore our echoed packets */
		if (buf[4] == local_addr[0] &&
		    buf[5] == local_addr[1])
		{
			if (verbose)
				fprintf(stderr, "Ignoring our own packet\n");
			return;
		}

		/* Ethernet endnode or router hello,
		   we replace these with PtP hello messages */
		if (buf[16] == 0x0d || buf[16] == 0x0b)
		{
			unsigned char ptp_hello[] = { 0x5, buf[10], buf[11], 0252, 0252, 0252, 0252};
			if (verbose)
				fprintf(stderr, "Sending PTP hello\n");
			send_ip(0, ptp_hello, sizeof(ptp_hello));
			return ;
		}
		/* Routing messages, Don't fudge the header on these */
		if (buf[16] == 0x07 || buf[16] == 0x09)
		{
			send_ip(0, buf+16, len-16);
			return;
		}

		/* Data or other packet */
		send_ip(1, buf+16, len-16);
	}
}

static int setup_tun(unsigned short addr, int make_default)
{
	int ret;
	struct ifreq ifr;
	char cmd[132];
	FILE *procfile;

	tunfd = open("/dev/net/tun", O_RDWR);
	if (tunfd < 0)
	{
		perror("could not open /dev/net/tun");
		exit(2);
	}
	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
	strcpy(ifr.ifr_name, "tap%d");
	ret = ioctl(tunfd, TUNSETIFF, (void *) &ifr);
	if (ret != 0)
	{
		perror("could not configure /dev/net/tun");
		exit(2);
	}
	fprintf(stderr, "using tun device %s\n", ifr.ifr_name);

	/* Bring the interface up
	 *   TODO: use ioctls...perhaps
	 */
	sprintf(cmd, "/sbin/ifconfig %s hw ether AA:00:04:00:%02X:%02X allmulti mtu %d up\n",
		ifr.ifr_name, addr & 0xFF, addr>>8, mtu);
	system(cmd);


	/* Configure the interface.
	 * Sigh, this maybe should be done using sysctl but that interface
	 * is probably worse than poking values into /proc !
	 */
	sprintf(cmd, "/proc/sys/net/decnet/conf/%s/forwarding", ifr.ifr_name);
	procfile = fopen(cmd, "w");
	if (!procfile)
	{
		fprintf(stderr, "Cannot set forwarding on interface\n");
	}
	else
	{
		fprintf(procfile, "1");
		fclose(procfile);
	}

	sprintf(cmd, "/proc/sys/net/decnet/conf/%s/priority", ifr.ifr_name);
	procfile = fopen(cmd, "w");
	if (!procfile)
	{
		fprintf(stderr, "Cannot set priority on interface\n");
	}
	else
	{
		fprintf(procfile, "%d", router_priority);
		fclose(procfile);
	}

	sprintf(cmd, "/proc/sys/net/decnet/conf/%s/t3", ifr.ifr_name);
	procfile = fopen(cmd, "w");
	if (!procfile)
	{
		fprintf(stderr, "Cannot set hello timer\n");
	}
	else
	{
		fprintf(procfile, "%d", hello_timer);
		fclose(procfile);
	}

	if (make_default)
	{
		procfile = fopen("/proc/sys/net/decnet/default_device", "w+");
		if (!procfile)
		{
			fprintf(stderr, "Cannot set default device\n");
		}
		else
		{
			fgets(old_default, sizeof(old_default), procfile);
			fprintf(procfile, "%s", ifr.ifr_name);
			fclose(procfile);
		}
	}

	return tunfd;
}

static void restore_interface(void)
{
	if (old_default[0])
	{
		FILE *procfile;

		procfile = fopen("/proc/sys/net/decnet/default_device", "w+");
		if (!procfile)
		{
			fprintf(stderr, "Cannot restore default device\n");
		}
		else
		{
			fprintf(procfile, "%s", old_default);
			fclose(procfile);
		}
	}
}

static void usage(char *cmd)
{

	printf("Usage: %s [options] <local-decnet-addr> <remote-IP-host>\n", cmd);
	printf("eg     %s -D 3.2 zarqon\n\n", cmd);

	printf("    -v       Verbose output\n");
	printf("    -1       Advertise as a level 1 router\n");
	printf("    -2       Advertise as a level 2 router (default)\n");
	printf("    -D       Make this the default DECnet interface (default no)\n");
	printf("    -p<prio> Router priority (default 64)\n");
	printf("    -P<port> IP port to talk to multinet on (default 700)\n");
	printf("    -m<MTU>  MTU of interface (default 576)\n");
	printf("    -t<secs> Timeout to restart IP connections (default 600)\n");
	printf("    -H<secs> Hello timer (default 60)\n");

}

int main(int argc, char *argv[])
{
	struct pollfd pfds[2];
	unsigned int area, node;
	unsigned short addr;
	int make_default = 0;
	int opt;

	while ((opt=getopt(argc,argv,"vp:12m:P:t:H:D?h")) != EOF)
	{
		switch(opt)
		{
		case 'v':
			verbose++;
			break;

		case '2':
			router_level = 2;
			break;
		case '1':
			router_level = 1;
			break;

		case 'p':
			router_priority = atoi(optarg);
			if (router_priority > 127 || router_priority < 0)
			{
				fprintf(stderr, "Router priority must be between 0 & 127\n");
				exit(2);
			}
			break;
		case 'P':
			port = atoi(optarg);
			break;

		case 'D':
			make_default = 1;
			break;

		case 'H':
			hello_timer = atoi(optarg);
			break;

		case 't':
			ip_timeout = atoi(optarg);
			break;

		case 'm':
			mtu = atoi(optarg);
			if (mtu > 1500 || mtu < 20)
			{
				fprintf(stderr, "MTU is invalid\n");
				exit(2);
			}
			break;

		case '?':
		case 'h':
			usage(argv[0]);
			break;

		}
	}

	if (argc - optind < 2)
	{
		usage(argv[0]);
		exit(2);
	}

	if (sscanf(argv[optind], "%d.%d", &area, &node) != 2)
	{
		fprintf(stderr, "DECnet address %s not valid\n", argv[optind]);
		return -1;
	}
	if (area > 63 || node > 1023)
	{
		fprintf(stderr, "DECnet address %d.%d not valid\n", area, node);
		return -1;
	}

	/* Save address for later */
	addr = (area<<10) | node;
	local_addr[0] = addr & 0xFF;
	local_addr[1] = addr >> 8;

	/* Check remote address is valid */
	optind++;
	remote_host_name = argv[optind];

	if (lookup_name())
		return 2;

	if (!verbose)
		daemon(0,0);

	signal(SIGINT, do_shutdown);
	signal(SIGTERM, do_shutdown);
	signal(SIGHUP, do_sighup);

	/* Initialise network ports */
	tunfd = setup_tun(addr, make_default);
	ipfd = setup_ip(port);

	/* Wait for START */
	signal(SIGALRM, resend_start);
	alarm(10);
	send_start(addr);

	pfds[0].fd = ipfd;
	pfds[0].events = POLLIN;

	pfds[1].fd = tunfd;
	pfds[1].events = POLLIN;

	fcntl(ipfd, F_SETFL, fcntl(ipfd, F_GETFL, 0) | O_NONBLOCK);
	fcntl(tunfd, F_SETFL, fcntl(tunfd, F_GETFL, 0) | O_NONBLOCK);

	running = 1;
	while (running)
	{
		int status;

		status = poll(pfds, 2, -1);
		if (status == -1 && errno != EINTR)
		{
			perror("poll error");
			exit(1);
		}

		if (reload_config)
		{
			lookup_name();
			reload_config = 0;
		}

		if (pfds[0].revents & POLLIN)
			read_ip();
		if (pfds[1].revents & POLLIN)
			read_tun();
	}

	if (make_default)
		restore_interface();

	return 0;
}