chiark / gitweb /
server/tun-slip.c: Pass correct stdout to child process.
[tripe] / server / tun-bsd.c
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * Tunnel interface for 4.4BSD-derived systems
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27/*----- Header files ------------------------------------------------------*/
28
29#define TUN_INTERNALS
30
31#include "tripe.h"
32
33/*----- Main code ---------------------------------------------------------*/
34
35#ifdef TUN_BSD
36
37struct tunnel {
38 const tunnel_ops *ops; /* Pointer to operations */
39 sel_file f; /* Selector for tunnel device */
40 struct peer *p; /* Pointer to my peer */
41};
42
43/* --- @t_read@ --- *
44 *
45 * Arguments: @int fd@ = file descriptor to read
46 * @unsigned mode@ = what's happened
47 * @void *v@ = pointer to tunnel block
48 *
49 * Returns: ---
50 *
51 * Use: Reads data from the tunnel.
52 */
53
54static void t_read(int fd, unsigned mode, void *v)
55{
56 tunnel *t = v;
57 ssize_t n;
58 buf b;
59
60 n = read(fd, buf_i, sizeof(buf_i));
61 if (n < 0) {
62 a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
63 return;
64 }
65 IF_TRACING(T_TUNNEL, {
66 trace(T_TUNNEL, "tun-bsd: packet arrived");
67 trace_block(T_PACKET, "tun-bsd: packet contents", buf_i, n);
68 })
69 buf_init(&b, buf_i, n);
70 p_tun(t->p, &b);
71}
72
73/* --- @t_init@ --- *
74 *
75 * Arguments: ---
76 *
77 * Returns: ---
78 *
79 * Use: Initializes the tunneling system. Maybe this will require
80 * opening file descriptors or something.
81 */
82
83static void t_init(void) { return; }
84
85/* --- @t_create@ --- *
86 *
87 * Arguments: @peer *p@ = pointer to peer block
88 * @int fd@ = file descriptor of tunnel device
89 * @char **ifn@ = where to put the interface name
90 *
91 * Returns: A tunnel block if it worked, or null on failure.
92 *
93 * Use: Initializes a new tunnel.
94 */
95
96static tunnel *t_create(peer *p, int fd, char **ifn)
97{
98 tunnel *t;
99
100 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
101 t = CREATE(tunnel);
102 t->ops = &tun_bsd;
103 t->p = p;
104 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
105 sel_addfile(&t->f);
106 return (t);
107}
108
109/* --- @t_inject@ --- *
110 *
111 * Arguments: @tunnel *t@ = pointer to tunnel block
112 * @buf *b@ = buffer to send
113 *
114 * Returns: ---
115 *
116 * Use: Injects a packet into the local network stack.
117 */
118
119static void t_inject(tunnel *t, buf *b)
120{
121 IF_TRACING(T_TUNNEL, {
122 trace(T_TUNNEL, "tun-bsd: inject decrypted packet");
123 trace_block(T_PACKET, "tun-bsd: packet contents", BBASE(b), BLEN(b));
124 })
125 write(t->f.fd, BBASE(b), BLEN(b));
126}
127
128/* --- @t_destroy@ --- *
129 *
130 * Arguments: @tunnel *t@ = pointer to tunnel block
131 *
132 * Returns: ---
133 *
134 * Use: Destroys a tunnel.
135 */
136
137static void t_destroy(tunnel *t)
138 { sel_rmfile(&t->f); close(t->f.fd); DESTROY(t); }
139
140const tunnel_ops tun_bsd = {
141 "bsd",
142 TUNF_PRIVOPEN,
143 t_init,
144 t_create,
145 0,
146 t_inject,
147 t_destroy
148};
149
150#endif
151
152/*----- That's all, folks -------------------------------------------------*/