1 /*
2 VTun - Virtual Tunnel over TCP/IP network.
3
4 Copyright (C) 1998-2000 Maxim Krasnyansky <max_mk@yahoo.com>
5
6 VTun has been derived from VPPP package by Maxim Krasnyansky.
7
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.
12
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.
17 */
18
19 /*
20 * tun_dev.c,v 1.3.2.1.2.1 2006/11/16 04:04:52 mtbishop Exp
21 */
22
23 /* #include "config.h" */
24
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <errno.h>
32
33 #include <sys/types.h>
34 #include <sys/uio.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
37 #include <net/if_tun.h>
38
39 /* #include "vtun.h"
40 #include "lib.h" */
41
42 /*
43 * Allocate TUN device, returns opened fd.
44 * Stores dev name in the first arg(must be large enough).
45 */
tun_open(char * dev)46 int tun_open(char *dev)
47 {
48 char tunname[14];
49 int i, fd = -1;
50
51 if( *dev ){
52 sprintf(tunname, "/dev/%s", dev);
53 fd = open(tunname, O_RDWR);
54 } else {
55 for(i=0; i < 255; i++){
56 sprintf(tunname, "/dev/tun%d", i);
57 /* Open device */
58 if( (fd=open(tunname, O_RDWR)) > 0 ){
59 sprintf(dev, "tun%d", i);
60 break;
61 }
62 }
63 }
64 return fd;
65 }
66
tun_close(int fd,char * dev)67 int tun_close(int fd, char *dev)
68 {
69 return close(fd);
70 }
71
72 /* Read/write frames from TUN device */
tun_write(int fd,char * buf,int len)73 int tun_write(int fd, char *buf, int len)
74 {
75 u_int32_t type = htonl(AF_INET);
76 struct iovec iv[2];
77
78 iv[0].iov_base = &type;
79 iv[0].iov_len = sizeof(type);
80 iv[1].iov_base = buf;
81 iv[1].iov_len = len;
82
83 return writev(fd, iv, 2);
84 }
85
tun_read(int fd,char * buf,int len)86 int tun_read(int fd, char *buf, int len)
87 {
88 struct iovec iv[2];
89 u_int32_t type;
90 register int rlen;
91
92 iv[0].iov_base = &type;
93 iv[0].iov_len = sizeof(type);
94 iv[1].iov_base = buf;
95 iv[1].iov_len = len;
96
97 if( (rlen = readv(fd, iv, 2)) > 0 )
98 return rlen - sizeof(type);
99 else
100 return rlen;
101 }
102
tun_last_error()103 const char *tun_last_error()
104 {
105 return strerror(errno);
106 }
107