3 * $Id: tun-unet.c,v 1.2 2001/02/04 17:10:58 mdw Exp $
5 * Tunnel interface based on Linux Usernet
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
31 * $Log: tun-unet.c,v $
32 * Revision 1.2 2001/02/04 17:10:58 mdw
33 * Make file descriptors be nonblocking and close-on-exec.
35 * Revision 1.1 2001/02/03 20:26:37 mdw
40 /*----- Header files ------------------------------------------------------*/
44 #include <sys/ioctl.h>
47 /*----- Main code ---------------------------------------------------------*/
51 * Arguments: @int fd@ = file descriptor to read
52 * @unsigned mode@ = what's happened
53 * @void *v@ = pointer to tunnel block
57 * Use: Reads data from the tunnel.
60 void t_read(int fd
, unsigned mode
, void *v
)
66 n
= read(fd
, buf_i
, sizeof(buf_i
));
68 a_warn("tunnel read failed (%s): %s", tun_ifname(t
), strerror(errno
));
71 IF_TRACING(T_TUNNEL
, {
72 trace(T_TUNNEL
, "tunnel: packet arrived");
73 trace_block(T_PACKET
, "tunnel: packet contents", buf_i
, n
);
75 buf_init(&b
, buf_i
, n
);
79 /* --- @tun_init@ --- *
85 * Use: Initializes the tunneling system. Maybe this will require
86 * opening file descriptors or something.
94 /* --- @tun_create@ --- *
96 * Arguments: @tunnel *t@ = pointer to tunnel block
97 * @peer *p@ = pointer to peer block
99 * Returns: Zero if it worked, nonzero on failure.
101 * Use: Initializes a new tunnel.
104 int tun_create(tunnel
*t
, peer
*p
)
108 if ((fd
= open("/dev/unet", O_RDWR
)) < 0) {
109 a_warn("open `/dev/unet' failed: %s", strerror(errno
));
112 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
114 sel_initfile(&sel
, &t
->f
, fd
, SEL_READ
, t_read
, t
);
116 T( trace(T_TUNNEL
, "tunnel: attached interface %s to peer `%s'",
117 tun_ifname(t
), p_name(p
)); )
121 /* --- @tun_ifname@ --- *
123 * Arguments: @tunnel *t@ = pointer to tunnel block
125 * Returns: A pointer to the tunnel's interface name.
128 const char *tun_ifname(tunnel
*t
)
130 static char b
[UNET_NAMEMAX
];
131 struct unet_info uni
;
132 if (ioctl(t
->f
.fd
, UNIOCGINFO
, &uni
)) {
133 a_warn("ioctl(UNIOCGINFO) failed: %s", strerror(errno
));
136 if (strlen(uni
.uni_ifname
) + 1 > sizeof(b
)) {
137 a_warn("interface name too long!");
140 strcpy(b
, uni
.uni_ifname
);
144 /* --- @tun_inject@ --- *
146 * Arguments: @tunnel *t@ = pointer to tunnel block
147 * @buf *b@ = buffer to send
151 * Use: Injects a packet into the local network stack.
154 void tun_inject(tunnel
*t
, buf
*b
)
156 IF_TRACING(T_TUNNEL
, {
157 trace(T_TUNNEL
, "tunnel: inject decrypted packet");
158 trace_block(T_PACKET
, "tunnel: packet contents", BBASE(b
), BLEN(b
));
160 write(t
->f
.fd
, BBASE(b
), BLEN(b
));
163 /* --- @tun_destroy@ --- *
165 * Arguments: @tunnel *t@ = pointer to tunnel block
169 * Use: Destroys a tunnel.
172 void tun_destroy(tunnel
*t
)
178 /*----- That's all, folks -------------------------------------------------*/