/* -*-c-*-
- *
- * $Id: chan.c,v 1.4 1999/08/31 17:42:49 mdw Exp $
*
* Channel management
*
* (c) 1999 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of the `fw' port forwarder.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
- *
+ *
* `fw' is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with `fw'; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: chan.c,v $
- * Revision 1.4 1999/08/31 17:42:49 mdw
- * Use `sel_force' to avoid a `select' call between reads and writes.
- *
- * Revision 1.3 1999/07/27 18:30:53 mdw
- * Various minor portability fixes.
- *
- * Revision 1.2 1999/07/26 23:27:52 mdw
- * Minor modifications for new design.
- *
- * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
- * Initial revision.
- *
- */
-
-/*----- Header files ------------------------------------------------------*/
-
-#include "config.h"
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <sys/types.h>
-#include <sys/time.h>
-#include <unistd.h>
-#include <sys/uio.h>
-
-#include <mLib/alloc.h>
-#include <mLib/conn.h>
-#include <mLib/sel.h>
-
-#include "chan.h"
#include "fw.h"
+#ifdef CHAN_DEBUG
+# define D(x) x
+#else
+# define D(x)
+#endif
+
/*----- Main code ---------------------------------------------------------*/
/* --- @writechan@ --- *
/* --- Do the write --- */
- if (base + len <= CHAN_BUFSZ)
+ D( printf("writechan %d: base = %u, len = %u; ", fd, base, len); )
+ if (base + len <= CHAN_BUFSZ) {
+ D( printf("%u:%u", base, len); )
w = write(fd, c->buf + base, len);
- else {
+ } else {
struct iovec iov[2];
iov[0].iov_base = c->buf + base;
iov[0].iov_len = CHAN_BUFSZ - base;
iov[1].iov_base = c->buf;
iov[1].iov_len = len - iov[0].iov_len;
+ D( printf("%u:%u, %u:%u",
+ base, CHAN_BUFSZ - base,
+ 0, len + base - CHAN_BUFSZ); )
w = writev(fd, iov, 2);
}
+ D( printf("; returned %d\n", w); )
/* --- Sift through the results --- */
else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
sel_addfile(&c->r);
c->len -= w;
+ c->base += w;
}
if (c->len == 0)
sel_rmfile(&c->w);
/* --- Close the output end if necessary --- */
- if (c->len == 0 && (c->f & CHANF_CLOSE))
- c->func(c->p);
+ if (c->len == 0) {
+ c->base = 0;
+ if (c->f & CHANF_CLOSE)
+ c->func(c->p);
+ }
return;
/* --- Force a close if an error occurred --- */
{
chan *c = vp;
int r;
- unsigned base = (c->base + c->len) & (CHAN_BUFSZ - 1);
- unsigned len = CHAN_BUFSZ - c->len;
+ unsigned base = c->base;
+ unsigned len = c->len;
/* --- Do the read --- */
- if (base + len <= CHAN_BUFSZ)
- r = read(fd, c->buf + base, len);
- else {
+ D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); )
+ if (base == 0) {
+ D( printf("%u:%u", len, CHAN_BUFSZ - len); )
+ r = read(fd, c->buf + len, CHAN_BUFSZ - len);
+ } else if (base + len >= CHAN_BUFSZ) {
+ D( printf("%u:%u", base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); )
+ r = read(fd, c->buf + base + len - CHAN_BUFSZ, CHAN_BUFSZ - len);
+ } else {
struct iovec iov[2];
- iov[0].iov_base = c->buf + base;
- iov[0].iov_len = CHAN_BUFSZ - base;
+ iov[0].iov_base = c->buf + base + len;
+ iov[0].iov_len = CHAN_BUFSZ - base - len;
iov[1].iov_base = c->buf;
- iov[1].iov_len = len - iov[0].iov_len;
+ iov[1].iov_len = base;
+ D( printf("%u:%u, %u:%u",
+ base + len, CHAN_BUFSZ - base - len,
+ 0, base); )
r = readv(fd, iov, 2);
}
+ D( printf("; returned %d\n", r); )
/* --- Sift through the results --- */