3 * $Id: chan.c,v 1.2 1999/07/26 23:27:52 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/07/26 23:27:52 mdw
33 * Minor modifications for new design.
35 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
40 /*----- Header files ------------------------------------------------------*/
49 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
58 #include <mLib/alloc.h>
59 #include <mLib/conn.h>
65 /*----- Main code ---------------------------------------------------------*/
67 /* --- @writechan@ --- *
69 * Arguments: @int fd@ = file descriptor to write to
70 * @unsigned mode@ = what the descriptor is ready for
71 * @void *vp@ = pointer to channel block
75 * Use: Writes to a channel.
78 static void writechan(int fd, unsigned mode, void *vp)
82 unsigned base = c->base;
83 unsigned len = c->len;
85 /* --- Write data from my buffer --- */
89 /* --- Do the write --- */
91 if (base + len <= CHAN_BUFSZ)
92 w = write(fd, c->buf + base, len);
95 iov[0].iov_base = c->buf + base;
96 iov[0].iov_len = CHAN_BUFSZ - base;
97 iov[1].iov_base = c->buf;
98 iov[1].iov_len = len - iov[0].iov_len;
99 w = writev(fd, iov, 2);
102 /* --- Sift through the results --- */
105 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
111 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
118 /* --- Close the output end if necessary --- */
120 if (c->len == 0 && (c->f & CHANF_CLOSE))
124 /* --- Force a close if an error occurred --- */
131 /* --- @readchan@ --- *
133 * Arguments: @int fd@ = file descriptor to read from
134 * @unsigned mode@ = what the descriptor is ready for
135 * @void *vp@ = pointer to channel block
139 * Use: Reads from a channel.
142 static void readchan(int fd, unsigned mode, void *vp)
146 unsigned base = (c->base + c->len) & (CHAN_BUFSZ - 1);
147 unsigned len = CHAN_BUFSZ - c->len;
149 /* --- Do the read --- */
151 if (base + len <= CHAN_BUFSZ)
152 r = read(fd, c->buf + base, len);
155 iov[0].iov_base = c->buf + base;
156 iov[0].iov_len = CHAN_BUFSZ - base;
157 iov[1].iov_base = c->buf;
158 iov[1].iov_len = len - iov[0].iov_len;
159 r = readv(fd, iov, 2);
162 /* --- Sift through the results --- */
165 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
171 else if (c->len == 0 && (c->f & CHANF_READY))
174 if (c->len == CHAN_BUFSZ)
178 /* --- Close the read end of the channel --- */
182 if (!c->len && (c->f & CHANF_READY))
187 /* --- @chan_close@ --- *
189 * Arguments: @chan *c@ = pointer to channel
193 * Use: Closes down a channel prematurely.
196 void chan_close(chan *c)
198 if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ)
200 if ((c->f & CHANF_READY) && c->len != 0)
204 /* --- @chan_dest@ --- *
206 * Arguments: @chan *c@ = pointer to channel
207 * @int fd@ = destination file descriptor for channel
211 * Use: Sets the channel's destination so it knows where to put
215 void chan_dest(chan *c, int fd)
217 if (c->f & CHANF_READY)
219 sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c);
220 if (c->len || (c->f & CHANF_CLOSE))
225 /* --- @chan_open@ --- *
227 * Arguments: @chan *c@ = pointer to channel to open
228 * @int from, to@ = source and destination file descriptors
229 * @void (*func)(void *p)@ = function to call on closure
230 * @void *p@ = argument to pass to function
234 * Use: Opens a channel. Data is copied from the source to the
235 * destination. The @to@ argument may be @-1@ if the file
236 * descriptor isn't known yet.
239 void chan_open(chan *c, int from, int to,
240 void (*func)(void */*p*/), void *p)
249 sel_initfile(sel, &c->r, from, SEL_READ, readchan, c);
256 /*----- That's all, folks -------------------------------------------------*/