diff options
Diffstat (limited to 'channels.c')
-rw-r--r-- | channels.c | 79 |
1 files changed, 47 insertions, 32 deletions
diff --git a/channels.c b/channels.c index 35edff6dc..7bf127d91 100644 --- a/channels.c +++ b/channels.c | |||
@@ -39,7 +39,7 @@ | |||
39 | */ | 39 | */ |
40 | 40 | ||
41 | #include "includes.h" | 41 | #include "includes.h" |
42 | RCSID("$OpenBSD: channels.c,v 1.131 2001/07/02 22:52:56 markus Exp $"); | 42 | RCSID("$OpenBSD: channels.c,v 1.132 2001/07/17 21:04:56 markus Exp $"); |
43 | 43 | ||
44 | #include "ssh.h" | 44 | #include "ssh.h" |
45 | #include "ssh1.h" | 45 | #include "ssh1.h" |
@@ -266,6 +266,37 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd, | |||
266 | return c; | 266 | return c; |
267 | } | 267 | } |
268 | 268 | ||
269 | static int | ||
270 | channel_find_maxfd(void) | ||
271 | { | ||
272 | int i, max = 0; | ||
273 | Channel *c; | ||
274 | |||
275 | for (i = 0; i < channels_alloc; i++) { | ||
276 | c = channels[i]; | ||
277 | if (c != NULL) { | ||
278 | max = MAX(max, c->rfd); | ||
279 | max = MAX(max, c->wfd); | ||
280 | max = MAX(max, c->efd); | ||
281 | } | ||
282 | } | ||
283 | return max; | ||
284 | } | ||
285 | |||
286 | int | ||
287 | channel_close_fd(int *fdp) | ||
288 | { | ||
289 | int ret = 0, fd = *fdp; | ||
290 | |||
291 | if (fd != -1) { | ||
292 | ret = close(fd); | ||
293 | *fdp = -1; | ||
294 | if (fd == channel_max_fd) | ||
295 | channel_max_fd = channel_find_maxfd(); | ||
296 | } | ||
297 | return ret; | ||
298 | } | ||
299 | |||
269 | /* Close all channel fd/socket. */ | 300 | /* Close all channel fd/socket. */ |
270 | 301 | ||
271 | static void | 302 | static void |
@@ -274,22 +305,10 @@ channel_close_fds(Channel *c) | |||
274 | debug3("channel_close_fds: channel %d: r %d w %d e %d", | 305 | debug3("channel_close_fds: channel %d: r %d w %d e %d", |
275 | c->self, c->rfd, c->wfd, c->efd); | 306 | c->self, c->rfd, c->wfd, c->efd); |
276 | 307 | ||
277 | if (c->sock != -1) { | 308 | channel_close_fd(&c->sock); |
278 | close(c->sock); | 309 | channel_close_fd(&c->rfd); |
279 | c->sock = -1; | 310 | channel_close_fd(&c->wfd); |
280 | } | 311 | channel_close_fd(&c->efd); |
281 | if (c->rfd != -1) { | ||
282 | close(c->rfd); | ||
283 | c->rfd = -1; | ||
284 | } | ||
285 | if (c->wfd != -1) { | ||
286 | close(c->wfd); | ||
287 | c->wfd = -1; | ||
288 | } | ||
289 | if (c->efd != -1) { | ||
290 | close(c->efd); | ||
291 | c->efd = -1; | ||
292 | } | ||
293 | } | 312 | } |
294 | 313 | ||
295 | /* Free the channel and close its fd/socket. */ | 314 | /* Free the channel and close its fd/socket. */ |
@@ -387,7 +406,7 @@ channel_stop_listening(void) | |||
387 | case SSH_CHANNEL_PORT_LISTENER: | 406 | case SSH_CHANNEL_PORT_LISTENER: |
388 | case SSH_CHANNEL_RPORT_LISTENER: | 407 | case SSH_CHANNEL_RPORT_LISTENER: |
389 | case SSH_CHANNEL_X11_LISTENER: | 408 | case SSH_CHANNEL_X11_LISTENER: |
390 | close(c->sock); | 409 | channel_close_fd(&c->sock); |
391 | channel_free(c); | 410 | channel_free(c); |
392 | break; | 411 | break; |
393 | } | 412 | } |
@@ -842,7 +861,7 @@ channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset) | |||
842 | log("X11 connection rejected because of wrong authentication."); | 861 | log("X11 connection rejected because of wrong authentication."); |
843 | buffer_clear(&c->input); | 862 | buffer_clear(&c->input); |
844 | buffer_clear(&c->output); | 863 | buffer_clear(&c->output); |
845 | close(c->sock); | 864 | channel_close_fd(&c->sock); |
846 | c->sock = -1; | 865 | c->sock = -1; |
847 | c->type = SSH_CHANNEL_CLOSED; | 866 | c->type = SSH_CHANNEL_CLOSED; |
848 | packet_start(SSH_MSG_CHANNEL_CLOSE); | 867 | packet_start(SSH_MSG_CHANNEL_CLOSE); |
@@ -1333,8 +1352,7 @@ channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset) | |||
1333 | if (len <= 0) { | 1352 | if (len <= 0) { |
1334 | debug2("channel %d: closing write-efd %d", | 1353 | debug2("channel %d: closing write-efd %d", |
1335 | c->self, c->efd); | 1354 | c->self, c->efd); |
1336 | close(c->efd); | 1355 | channel_close_fd(&c->efd); |
1337 | c->efd = -1; | ||
1338 | } else { | 1356 | } else { |
1339 | buffer_consume(&c->extended, len); | 1357 | buffer_consume(&c->extended, len); |
1340 | c->local_consumed += len; | 1358 | c->local_consumed += len; |
@@ -1349,8 +1367,7 @@ channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset) | |||
1349 | if (len <= 0) { | 1367 | if (len <= 0) { |
1350 | debug2("channel %d: closing read-efd %d", | 1368 | debug2("channel %d: closing read-efd %d", |
1351 | c->self, c->efd); | 1369 | c->self, c->efd); |
1352 | close(c->efd); | 1370 | channel_close_fd(&c->efd); |
1353 | c->efd = -1; | ||
1354 | } else { | 1371 | } else { |
1355 | buffer_append(&c->extended, buf, len); | 1372 | buffer_append(&c->extended, buf, len); |
1356 | } | 1373 | } |
@@ -1532,7 +1549,7 @@ channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset) | |||
1532 | */ | 1549 | */ |
1533 | void | 1550 | void |
1534 | channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp, | 1551 | channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp, |
1535 | int rekeying) | 1552 | int *nallocp, int rekeying) |
1536 | { | 1553 | { |
1537 | int n; | 1554 | int n; |
1538 | u_int sz; | 1555 | u_int sz; |
@@ -1540,15 +1557,13 @@ channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp, | |||
1540 | n = MAX(*maxfdp, channel_max_fd); | 1557 | n = MAX(*maxfdp, channel_max_fd); |
1541 | 1558 | ||
1542 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); | 1559 | sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); |
1543 | if (*readsetp == NULL || n > *maxfdp) { | 1560 | /* perhaps check sz < nalloc/2 and shrink? */ |
1544 | if (*readsetp) | 1561 | if (*readsetp == NULL || sz > *nallocp) { |
1545 | xfree(*readsetp); | 1562 | *readsetp = xrealloc(*readsetp, sz); |
1546 | if (*writesetp) | 1563 | *writesetp = xrealloc(*writesetp, sz); |
1547 | xfree(*writesetp); | 1564 | *nallocp = sz; |
1548 | *readsetp = xmalloc(sz); | ||
1549 | *writesetp = xmalloc(sz); | ||
1550 | *maxfdp = n; | ||
1551 | } | 1565 | } |
1566 | *maxfdp = n; | ||
1552 | memset(*readsetp, 0, sz); | 1567 | memset(*readsetp, 0, sz); |
1553 | memset(*writesetp, 0, sz); | 1568 | memset(*writesetp, 0, sz); |
1554 | 1569 | ||