e82f7154 |
1 | /* -*-c-*- |
2 | * |
e82f7154 |
3 | * Channel management |
4 | * |
e5398e09 |
5 | * (c) 1999 Straylight/Edgeware |
e82f7154 |
6 | */ |
7 | |
206212ca |
8 | /*----- Licensing notice --------------------------------------------------* |
e82f7154 |
9 | * |
9155ea97 |
10 | * This file is part of the `fwd' port forwarder. |
e82f7154 |
11 | * |
9155ea97 |
12 | * `fwd' is free software; you can redistribute it and/or modify |
e82f7154 |
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. |
206212ca |
16 | * |
9155ea97 |
17 | * `fwd' is distributed in the hope that it will be useful, |
e82f7154 |
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. |
206212ca |
21 | * |
e82f7154 |
22 | * You should have received a copy of the GNU General Public License |
9155ea97 |
23 | * along with `fwd'; if not, write to the Free Software Foundation, |
e82f7154 |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | */ |
26 | |
9155ea97 |
27 | #include "fwd.h" |
e82f7154 |
28 | |
08cb0dd8 |
29 | #ifdef CHAN_DEBUG |
30 | # define D(x) x |
31 | #else |
32 | # define D(x) |
33 | #endif |
34 | |
e82f7154 |
35 | /*----- Main code ---------------------------------------------------------*/ |
36 | |
37 | /* --- @writechan@ --- * |
38 | * |
39 | * Arguments: @int fd@ = file descriptor to write to |
40 | * @unsigned mode@ = what the descriptor is ready for |
41 | * @void *vp@ = pointer to channel block |
42 | * |
43 | * Returns: --- |
44 | * |
45 | * Use: Writes to a channel. |
46 | */ |
47 | |
48 | static void writechan(int fd, unsigned mode, void *vp) |
49 | { |
50 | chan *c = vp; |
51 | int w; |
52 | unsigned base = c->base; |
53 | unsigned len = c->len; |
54 | |
55 | /* --- Write data from my buffer --- */ |
56 | |
57 | if (len) { |
58 | |
59 | /* --- Do the write --- */ |
60 | |
08cb0dd8 |
61 | D( printf("writechan %d: base = %u, len = %u; ", fd, base, len); ) |
62 | if (base + len <= CHAN_BUFSZ) { |
63 | D( printf("%u:%u", base, len); ) |
e82f7154 |
64 | w = write(fd, c->buf + base, len); |
08cb0dd8 |
65 | } else { |
e82f7154 |
66 | struct iovec iov[2]; |
67 | iov[0].iov_base = c->buf + base; |
68 | iov[0].iov_len = CHAN_BUFSZ - base; |
69 | iov[1].iov_base = c->buf; |
70 | iov[1].iov_len = len - iov[0].iov_len; |
08cb0dd8 |
71 | D( printf("%u:%u, %u:%u", |
72 | base, CHAN_BUFSZ - base, |
73 | 0, len + base - CHAN_BUFSZ); ) |
e82f7154 |
74 | w = writev(fd, iov, 2); |
75 | } |
08cb0dd8 |
76 | D( printf("; returned %d\n", w); ) |
e82f7154 |
77 | |
78 | /* --- Sift through the results --- */ |
79 | |
80 | if (w < 0) { |
81 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
82 | return; |
86c9bc4b |
83 | c->err = errno; |
e82f7154 |
84 | goto close; |
86c9bc4b |
85 | } else if (w == 0) |
e82f7154 |
86 | goto close; |
87 | else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE)) |
88 | sel_addfile(&c->r); |
89 | c->len -= w; |
08cb0dd8 |
90 | c->base += w; |
e82f7154 |
91 | } |
92 | if (c->len == 0) |
93 | sel_rmfile(&c->w); |
94 | |
95 | /* --- Close the output end if necessary --- */ |
96 | |
046a152e |
97 | if (c->len == 0) { |
98 | c->base = 0; |
99 | if (c->f & CHANF_CLOSE) |
100 | c->func(c->p); |
101 | } |
e82f7154 |
102 | return; |
103 | |
104 | /* --- Force a close if an error occurred --- */ |
105 | |
106 | close: |
107 | chan_close(c); |
108 | c->func(c->p); |
109 | } |
110 | |
111 | /* --- @readchan@ --- * |
112 | * |
113 | * Arguments: @int fd@ = file descriptor to read from |
114 | * @unsigned mode@ = what the descriptor is ready for |
115 | * @void *vp@ = pointer to channel block |
116 | * |
117 | * Returns: --- |
118 | * |
119 | * Use: Reads from a channel. |
120 | */ |
121 | |
122 | static void readchan(int fd, unsigned mode, void *vp) |
123 | { |
124 | chan *c = vp; |
125 | int r; |
08cb0dd8 |
126 | unsigned base = c->base; |
127 | unsigned len = c->len; |
e82f7154 |
128 | |
129 | /* --- Do the read --- */ |
130 | |
08cb0dd8 |
131 | D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); ) |
132 | if (base == 0) { |
133 | D( printf("%u:%u", len, CHAN_BUFSZ - len); ) |
134 | r = read(fd, c->buf + len, CHAN_BUFSZ - len); |
135 | } else if (base + len >= CHAN_BUFSZ) { |
136 | D( printf("%u:%u", base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); ) |
137 | r = read(fd, c->buf + base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); |
138 | } else { |
e82f7154 |
139 | struct iovec iov[2]; |
08cb0dd8 |
140 | iov[0].iov_base = c->buf + base + len; |
141 | iov[0].iov_len = CHAN_BUFSZ - base - len; |
e82f7154 |
142 | iov[1].iov_base = c->buf; |
08cb0dd8 |
143 | iov[1].iov_len = base; |
144 | D( printf("%u:%u, %u:%u", |
145 | base + len, CHAN_BUFSZ - base - len, |
146 | 0, base); ) |
e82f7154 |
147 | r = readv(fd, iov, 2); |
148 | } |
08cb0dd8 |
149 | D( printf("; returned %d\n", r); ) |
e82f7154 |
150 | |
151 | /* --- Sift through the results --- */ |
152 | |
153 | if (r < 0) { |
154 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
155 | return; |
86c9bc4b |
156 | c->err = errno; |
e82f7154 |
157 | goto close; |
86c9bc4b |
158 | } else if (r == 0) |
e82f7154 |
159 | goto close; |
3eb9f39e |
160 | else if (c->len == 0 && (c->f & CHANF_READY)) { |
e82f7154 |
161 | sel_addfile(&c->w); |
3eb9f39e |
162 | sel_force(&c->w); |
163 | } |
e82f7154 |
164 | c->len += r; |
165 | if (c->len == CHAN_BUFSZ) |
166 | sel_rmfile(&c->r); |
167 | return; |
168 | |
169 | /* --- Close the read end of the channel --- */ |
170 | |
171 | close: |
172 | c->f |= CHANF_CLOSE; |
3eb9f39e |
173 | if (!c->len && (c->f & CHANF_READY)) { |
e82f7154 |
174 | sel_addfile(&c->w); |
3eb9f39e |
175 | sel_force(&c->w); |
176 | } |
e82f7154 |
177 | sel_rmfile(&c->r); |
178 | } |
179 | |
180 | /* --- @chan_close@ --- * |
181 | * |
182 | * Arguments: @chan *c@ = pointer to channel |
183 | * |
184 | * Returns: --- |
185 | * |
186 | * Use: Closes down a channel prematurely. |
187 | */ |
188 | |
189 | void chan_close(chan *c) |
190 | { |
191 | if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ) |
192 | sel_rmfile(&c->r); |
193 | if ((c->f & CHANF_READY) && c->len != 0) |
194 | sel_rmfile(&c->w); |
195 | } |
196 | |
197 | /* --- @chan_dest@ --- * |
198 | * |
199 | * Arguments: @chan *c@ = pointer to channel |
200 | * @int fd@ = destination file descriptor for channel |
201 | * |
202 | * Returns: --- |
203 | * |
204 | * Use: Sets the channel's destination so it knows where to put |
205 | * data. |
206 | */ |
207 | |
208 | void chan_dest(chan *c, int fd) |
209 | { |
210 | if (c->f & CHANF_READY) |
211 | return; |
212 | sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c); |
3eb9f39e |
213 | if (c->len || (c->f & CHANF_CLOSE)) { |
e82f7154 |
214 | sel_addfile(&c->w); |
3eb9f39e |
215 | sel_force(&c->w); |
216 | } |
e5398e09 |
217 | c->f |= CHANF_READY; |
e82f7154 |
218 | } |
219 | |
220 | /* --- @chan_open@ --- * |
221 | * |
222 | * Arguments: @chan *c@ = pointer to channel to open |
223 | * @int from, to@ = source and destination file descriptors |
224 | * @void (*func)(void *p)@ = function to call on closure |
225 | * @void *p@ = argument to pass to function |
226 | * |
227 | * Returns: --- |
228 | * |
229 | * Use: Opens a channel. Data is copied from the source to the |
230 | * destination. The @to@ argument may be @-1@ if the file |
231 | * descriptor isn't known yet. |
232 | */ |
233 | |
234 | void chan_open(chan *c, int from, int to, |
235 | void (*func)(void */*p*/), void *p) |
236 | { |
237 | c->func = func; |
238 | c->p = p; |
239 | |
240 | c->base = 0; |
241 | c->len = 0; |
242 | c->f = 0; |
755e6619 |
243 | c->err = 0; |
e82f7154 |
244 | |
245 | sel_initfile(sel, &c->r, from, SEL_READ, readchan, c); |
246 | sel_addfile(&c->r); |
247 | |
248 | if (to != -1) |
249 | chan_dest(c, to); |
250 | } |
251 | |
252 | /*----- That's all, folks -------------------------------------------------*/ |