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