Improve lexical analysis. In particular, `chmod' patterns don't have to
[fwd] / chan.c
1 /* -*-c-*-
2 *
3 * $Id: chan.c,v 1.3 1999/07/27 18:30:53 mdw Exp $
4 *
5 * Channel management
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
14 * `fw' 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 * `fw' 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 `fw'; 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: chan.c,v $
32 * Revision 1.3 1999/07/27 18:30:53 mdw
33 * Various minor portability fixes.
34 *
35 * Revision 1.2 1999/07/26 23:27:52 mdw
36 * Minor modifications for new design.
37 *
38 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
39 * Initial revision.
40 *
41 */
42
43 /*----- Header files ------------------------------------------------------*/
44
45 #include "config.h"
46
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include <sys/types.h>
53 #include <sys/time.h>
54 #include <unistd.h>
55 #include <sys/uio.h>
56
57 #include <mLib/alloc.h>
58 #include <mLib/conn.h>
59 #include <mLib/sel.h>
60
61 #include "chan.h"
62 #include "fw.h"
63
64 /*----- Main code ---------------------------------------------------------*/
65
66 /* --- @writechan@ --- *
67 *
68 * Arguments: @int fd@ = file descriptor to write to
69 * @unsigned mode@ = what the descriptor is ready for
70 * @void *vp@ = pointer to channel block
71 *
72 * Returns: ---
73 *
74 * Use: Writes to a channel.
75 */
76
77 static void writechan(int fd, unsigned mode, void *vp)
78 {
79 chan *c = vp;
80 int w;
81 unsigned base = c->base;
82 unsigned len = c->len;
83
84 /* --- Write data from my buffer --- */
85
86 if (len) {
87
88 /* --- Do the write --- */
89
90 if (base + len <= CHAN_BUFSZ)
91 w = write(fd, c->buf + base, len);
92 else {
93 struct iovec iov[2];
94 iov[0].iov_base = c->buf + base;
95 iov[0].iov_len = CHAN_BUFSZ - base;
96 iov[1].iov_base = c->buf;
97 iov[1].iov_len = len - iov[0].iov_len;
98 w = writev(fd, iov, 2);
99 }
100
101 /* --- Sift through the results --- */
102
103 if (w < 0) {
104 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
105 return;
106 goto close;
107 }
108 else if (w == 0)
109 goto close;
110 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
111 sel_addfile(&c->r);
112 c->len -= w;
113 }
114 if (c->len == 0)
115 sel_rmfile(&c->w);
116
117 /* --- Close the output end if necessary --- */
118
119 if (c->len == 0 && (c->f & CHANF_CLOSE))
120 c->func(c->p);
121 return;
122
123 /* --- Force a close if an error occurred --- */
124
125 close:
126 chan_close(c);
127 c->func(c->p);
128 }
129
130 /* --- @readchan@ --- *
131 *
132 * Arguments: @int fd@ = file descriptor to read from
133 * @unsigned mode@ = what the descriptor is ready for
134 * @void *vp@ = pointer to channel block
135 *
136 * Returns: ---
137 *
138 * Use: Reads from a channel.
139 */
140
141 static void readchan(int fd, unsigned mode, void *vp)
142 {
143 chan *c = vp;
144 int r;
145 unsigned base = (c->base + c->len) & (CHAN_BUFSZ - 1);
146 unsigned len = CHAN_BUFSZ - c->len;
147
148 /* --- Do the read --- */
149
150 if (base + len <= CHAN_BUFSZ)
151 r = read(fd, c->buf + base, len);
152 else {
153 struct iovec iov[2];
154 iov[0].iov_base = c->buf + base;
155 iov[0].iov_len = CHAN_BUFSZ - base;
156 iov[1].iov_base = c->buf;
157 iov[1].iov_len = len - iov[0].iov_len;
158 r = readv(fd, iov, 2);
159 }
160
161 /* --- Sift through the results --- */
162
163 if (r < 0) {
164 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
165 return;
166 goto close;
167 }
168 else if (r == 0)
169 goto close;
170 else if (c->len == 0 && (c->f & CHANF_READY))
171 sel_addfile(&c->w);
172 c->len += r;
173 if (c->len == CHAN_BUFSZ)
174 sel_rmfile(&c->r);
175 return;
176
177 /* --- Close the read end of the channel --- */
178
179 close:
180 c->f |= CHANF_CLOSE;
181 if (!c->len && (c->f & CHANF_READY))
182 sel_addfile(&c->w);
183 sel_rmfile(&c->r);
184 }
185
186 /* --- @chan_close@ --- *
187 *
188 * Arguments: @chan *c@ = pointer to channel
189 *
190 * Returns: ---
191 *
192 * Use: Closes down a channel prematurely.
193 */
194
195 void chan_close(chan *c)
196 {
197 if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ)
198 sel_rmfile(&c->r);
199 if ((c->f & CHANF_READY) && c->len != 0)
200 sel_rmfile(&c->w);
201 }
202
203 /* --- @chan_dest@ --- *
204 *
205 * Arguments: @chan *c@ = pointer to channel
206 * @int fd@ = destination file descriptor for channel
207 *
208 * Returns: ---
209 *
210 * Use: Sets the channel's destination so it knows where to put
211 * data.
212 */
213
214 void chan_dest(chan *c, int fd)
215 {
216 if (c->f & CHANF_READY)
217 return;
218 sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c);
219 if (c->len || (c->f & CHANF_CLOSE))
220 sel_addfile(&c->w);
221 c->f |= CHANF_READY;
222 }
223
224 /* --- @chan_open@ --- *
225 *
226 * Arguments: @chan *c@ = pointer to channel to open
227 * @int from, to@ = source and destination file descriptors
228 * @void (*func)(void *p)@ = function to call on closure
229 * @void *p@ = argument to pass to function
230 *
231 * Returns: ---
232 *
233 * Use: Opens a channel. Data is copied from the source to the
234 * destination. The @to@ argument may be @-1@ if the file
235 * descriptor isn't known yet.
236 */
237
238 void chan_open(chan *c, int from, int to,
239 void (*func)(void */*p*/), void *p)
240 {
241 c->func = func;
242 c->p = p;
243
244 c->base = 0;
245 c->len = 0;
246 c->f = 0;
247
248 sel_initfile(sel, &c->r, from, SEL_READ, readchan, c);
249 sel_addfile(&c->r);
250
251 if (to != -1)
252 chan_dest(c, to);
253 }
254
255 /*----- That's all, folks -------------------------------------------------*/