chiark / gitweb /
The `DAEMON' notification to stdout is replaced by a warning. The
[tripe] / tun-unet.c
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id: tun-unet.c,v 1.1 2001/02/03 20:26:37 mdw Exp $
4 *
5 * Tunnel interface based on Linux Usernet
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Trivial IP Encryption (TrIPE).
13 *
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.
18 *
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.
23 *
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.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: tun-unet.c,v $
32 * Revision 1.1 2001/02/03 20:26:37 mdw
33 * Initial checkin.
34 *
35 */
36
37/*----- Header files ------------------------------------------------------*/
38
39#include "tripe.h"
40
41#include <sys/ioctl.h>
42#include <unet.h>
43
44/*----- Main code ---------------------------------------------------------*/
45
46/* --- @t_read@ --- *
47 *
48 * Arguments: @int fd@ = file descriptor to read
49 * @unsigned mode@ = what's happened
50 * @void *v@ = pointer to tunnel block
51 *
52 * Returns: ---
53 *
54 * Use: Reads data from the tunnel.
55 */
56
57void t_read(int fd, unsigned mode, void *v)
58{
59 tunnel *t = v;
60 ssize_t n;
61 buf b;
62
63 n = read(fd, buf_i, sizeof(buf_i));
64 if (n < 0) {
65 a_warn("tunnel read failed (%s): %s", tun_ifname(t), strerror(errno));
66 return;
67 }
68 IF_TRACING(T_TUNNEL, {
69 trace(T_TUNNEL, "tunnel: packet arrived");
70 trace_block(T_PACKET, "tunnel: packet contents", buf_i, n);
71 })
72 buf_init(&b, buf_i, n);
73 p_tun(t->p, &b);
74}
75
76/* --- @tun_init@ --- *
77 *
78 * Arguments: ---
79 *
80 * Returns: ---
81 *
82 * Use: Initializes the tunneling system. Maybe this will require
83 * opening file descriptors or something.
84 */
85
86void tun_init(void)
87{
88 return;
89}
90
91/* --- @tun_create@ --- *
92 *
93 * Arguments: @tunnel *t@ = pointer to tunnel block
94 * @peer *p@ = pointer to peer block
95 *
96 * Returns: Zero if it worked, nonzero on failure.
97 *
98 * Use: Initializes a new tunnel.
99 */
100
101int tun_create(tunnel *t, peer *p)
102{
103 int fd;
104
105 if ((fd = open("/dev/unet", O_RDWR)) < 0) {
106 a_warn("open `/dev/unet' failed: %s", strerror(errno));
107 return (-1);
108 }
109 t->p = p;
110 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
111 sel_addfile(&t->f);
112 T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'",
113 tun_ifname(t), p_name(p)); )
114 return (0);
115}
116
117/* --- @tun_ifname@ --- *
118 *
119 * Arguments: @tunnel *t@ = pointer to tunnel block
120 *
121 * Returns: A pointer to the tunnel's interface name.
122 */
123
124const char *tun_ifname(tunnel *t)
125{
126 static char b[UNET_NAMEMAX];
127 struct unet_info uni;
128 if (ioctl(t->f.fd, UNIOCGINFO, &uni)) {
129 a_warn("ioctl(UNIOCGINFO) failed: %s", strerror(errno));
130 return ("<error>");
131 }
132 if (strlen(uni.uni_ifname) + 1 > sizeof(b)) {
133 a_warn("interface name too long!");
134 return ("<error>");
135 }
136 strcpy(b, uni.uni_ifname);
137 return (b);
138}
139
140/* --- @tun_inject@ --- *
141 *
142 * Arguments: @tunnel *t@ = pointer to tunnel block
143 * @buf *b@ = buffer to send
144 *
145 * Returns: ---
146 *
147 * Use: Injects a packet into the local network stack.
148 */
149
150void tun_inject(tunnel *t, buf *b)
151{
152 IF_TRACING(T_TUNNEL, {
153 trace(T_TUNNEL, "tunnel: inject decrypted packet");
154 trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b));
155 })
156 write(t->f.fd, BBASE(b), BLEN(b));
157}
158
159/* --- @tun_destroy@ --- *
160 *
161 * Arguments: @tunnel *t@ = pointer to tunnel block
162 *
163 * Returns: ---
164 *
165 * Use: Destroys a tunnel.
166 */
167
168void tun_destroy(tunnel *t)
169{
170 sel_rmfile(&t->f);
171 close(t->f.fd);
172}
173
174/*----- That's all, folks -------------------------------------------------*/