import of 0.2-rm+zomb-pre3
[vpnc.git] / tun_dev-bsd.c
blobc7a5fe17172da4de18e15e59324ec0d775692de1
1 /*
2 VTun - Virtual Tunnel over TCP/IP network.
4 Copyright (C) 1998-2000 Maxim Krasnyansky <max_mk@yahoo.com>
6 VTun has been derived from VPPP package by Maxim Krasnyansky.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 * $Id: tun_dev.c,v 1.1.2.2 2000/11/20 08:15:53 maxk Exp $
21 */
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <syslog.h>
29 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <net/if.h>
33 #include <net/if_tun.h>
35 /*
36 * Allocate TUN device, returns opened fd.
37 * Stores dev name in the first arg(must be large enough).
38 */
39 int tun_open(char *dev)
41 char tunname[14];
42 int i, fd;
44 if( *dev ) {
45 sprintf(tunname, "/dev/%s", dev);
46 return open(tunname, O_RDWR);
49 for(i=0; i < 255; i++){
50 sprintf(tunname, "/dev/tun%d", i);
51 /* Open device */
52 if( (fd=open(tunname, O_RDWR)) > 0 ){
53 sprintf(dev, "tun%d", i);
54 return fd;
57 return -1;
60 int tun_close(int fd, char *dev)
62 return close(fd);
65 /* Read/write frames from TUN device */
66 int tun_write(int fd, char *buf, int len)
68 return write(fd, buf, len);
71 int tun_read(int fd, char *buf, int len)
73 return read(fd, buf, len);