1 /* tun.c system tunnel driver interface
3 * $Id: tun.c,v 1.3 1995/03/19 17:21:06 bdale Exp $
5 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
6 * This software may be freely used, distributed, or modified, providing
7 * this header is not removed.
11 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <netinet/in_systm.h>
16 #include <netinet/ip.h>
29 #define FNDELAY O_NDELAY
34 #define IF_NAME "tun" /* for use with the error checking macros */
37 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
38 * open and initialize the IO interface. Return -1 for error.
39 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
42 struct interface *ifp;
45 CK_IFTYPE(ifp,IF_TYPE_TUN);
47 if((ifp->status & IF_STAT_OPEN)) return 1;
49 ifp->fd = open(ifp->devname, O_RDWR);
55 if (fcntl(ifp->fd, F_SETFL, FNDELAY) < 0) {
56 PERR("setting non-blocking I/O on tunnel device");
60 ifp->status = IF_STAT_OPEN;
66 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
67 * Read data from the specified interface. Return a complete IP datagram.
68 * If the packet is not complete, then don't return anything.
69 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
72 struct interface *ifp;
78 CK_IFTYPE(ifp,IF_TYPE_TUN);
82 n = read(ifp->fd, (char *)m->msg, MAX_SIZE);
85 if(errno==EINTR)return 0;
86 if(errno==EWOULDBLOCK)return 0;
87 PERR("read from tunnel device");
95 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
96 * write data from to the specified interface. Return as soon as possible.
97 * The buffer provided will be a complete IP datagram.
98 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
101 struct interface *ifp; /* the interface */
102 struct message *m; /* the message */
108 CK_IFTYPE(ifp,IF_TYPE_TUN);
112 if(m->length<=0)return 0;
114 n = write(ifp->fd, (char *)m->msg, m->length);
116 if(errno==EINTR)return 0;
117 if(errno==EWOULDBLOCK){ /* should never happen :-) */
121 PERR("write to tunnel device");
124 if(n < m->length)ifp->out_overruns++;