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