io.c 15.3 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
/* io.c
 *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Niels Möller's avatar
Niels Möller committed
24 25
 */

Niels Möller's avatar
Niels Möller committed
26 27 28
#include <assert.h>
#include <string.h>

Niels Möller's avatar
Niels Möller committed
29 30 31 32 33 34
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
Niels Möller's avatar
Niels Möller committed
35
#include <arpa/inet.h>
36
#include <signal.h>
Niels Möller's avatar
Niels Möller committed
37

Niels Möller's avatar
Niels Möller committed
38 39 40 41 42
#include "io.h"
#include "werror.h"
#include "write_buffer.h"
#include "xalloc.h"

Niels Möller's avatar
Niels Möller committed
43 44 45 46 47
/* A little more than an hour */
#define MAX_TIMEOUT 4000

struct fd_read
{
48
  struct abstract_read super;
Niels Möller's avatar
Niels Möller committed
49 50 51
  int fd;
};

Niels Möller's avatar
Niels Möller committed
52
static int do_read(struct abstract_read **r, UINT32 length, UINT8 *buffer)
Niels Möller's avatar
Niels Möller committed
53
{
54 55 56
  struct fd_read *closure
    = (struct fd_read *) *r;

57 58
  MDEBUG(closure);
  
59 60 61 62 63 64 65
  while(1)
    {
      int res = read(closure->fd, buffer, length);
      if (!res)
	return A_EOF;
      if (res > 0)
	return res;
66

67 68 69
      switch(errno)
	{
	case EINTR:
70 71
	  continue;  /* FIXME: Is it really worth looping here,
		      * instead of in the select loop? */
72 73 74
	case EWOULDBLOCK:  /* aka EAGAIN */
	  return 0;
	default:
Niels Möller's avatar
Niels Möller committed
75 76 77 78
	  werror("io.c: do_read: read() failed (errno %d), %s\n",
		 errno, strerror(errno));
	  debug("  fd = %d, buffer = %p, length = %ud\n",
		closure->fd, buffer, length);
79 80 81 82
	  return A_FAIL;
	}
    }
}
Niels Möller's avatar
Niels Möller committed
83

84 85 86
#define FOR_FDS(type, fd, list, extra)				\
{								\
  type **(_fd);							\
Niels Möller's avatar
Niels Möller committed
87
  type *(fd);							\
Niels Möller's avatar
Niels Möller committed
88
  for(_fd = &(list); ((fd) = *_fd); (extra)) {
89 90


Niels Möller's avatar
Niels Möller committed
91 92 93
#define END_FOR_FDS _fd = &(*_fd)->next; } }

/* UNLINK_FD must be followed by a continue, to avoid updating _fd */
94
#define UNLINK_FD (*_fd = (*_fd)->next)
95

96 97 98 99 100 101 102 103
static void close_fd(struct io_fd *fd)
{
  /* FIXME: The value returned from the close callback could be used
   * to choose an exit code. */
  if (fd->close_callback && fd->close_reason)
    (void) CLOSE_CALLBACK(fd->close_callback, fd->close_reason);
  
  close(fd->fd);
104 105 106 107 108

  /* Make sure writing to the buffer fails. */
  if (fd->buffer)
    write_buffer_close(fd->buffer);
  
109 110 111 112
  /* There can be other objects around that may still
   * attempt to write to the buffer. So let gc handle it
   * instead of freeing it explicitly */
#if 0
113
  lsh_object_free(fd->buffer);
114
#endif
115 116 117

  /* There may be pointers to fd objects. So don't free them here. */
#if 0
118
  /* Handlers are not shared, so it should be ok to free them. */
119 120
  lsh_object_free(fd->handler);
  lsh_object_free(fd);
121
#endif
122 123
}

124
static int io_iter(struct io_backend *b)
Niels Möller's avatar
Niels Möller committed
125
{
126 127 128 129 130 131 132 133 134
  struct pollfd *fds;
  int i;
  unsigned long nfds; /* FIXME: Should be nfds_t if that type is defined */
  int timeout;
  int res;

  nfds = b->nio + b->nlisten + b->nconnect;

  if (b->callouts)
Niels Möller's avatar
Niels Möller committed
135
    {
136 137 138
      time_t now = time(NULL);
      if (now >= b->callouts->when)
	timeout = 0;
Niels Möller's avatar
Niels Möller committed
139
      else
140 141 142 143 144 145 146 147 148 149 150 151 152 153
	{
	  if (b->callouts->when > now + MAX_TIMEOUT)
	    timeout = MAX_TIMEOUT * 1000;
	  else
	    timeout = (b->callouts->when - now) * 1000;
	}
    }
  else
    {
      if (!nfds)
	/* All done */
	return 0;
      timeout = -1;
    }
154

155
  fds = alloca(sizeof(struct pollfd) * nfds);
Niels Möller's avatar
Niels Möller committed
156

157 158
  /* Handle fds in order: write, read, accept, connect. */
  i = 0;
159

160 161 162 163
  FOR_FDS(struct io_fd, fd, b->io, i++)
    {
      fds[i].fd = fd->fd;
      fds[i].events = 0;
Niels Möller's avatar
Niels Möller committed
164
      if (fd->handler && !fd->on_hold)
165 166 167
	fds[i].events |= POLLIN;

      /* pre_write returns 0 if the buffer is empty */
168 169 170 171 172 173 174 175 176 177 178
      if (fd->buffer)
	{
	  if (write_buffer_pre_write(fd->buffer))
	    fds[i].events |= POLLOUT;
	  else
	    /* Buffer is empty. Should we close? */
	    if (fd->buffer->closed)
	      {
		fd->close_now = 1;
	      }
	}
179 180
    }
  END_FOR_FDS;
181

182 183 184 185 186 187
  FOR_FDS(struct listen_fd, fd, b->listen, i++)
    {
      fds[i].fd = fd->fd;
      fds[i].events = POLLIN;
    }
  END_FOR_FDS;
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  FOR_FDS(struct connect_fd, fd, b->connect, i++)
    {
      fds[i].fd = fd->fd;
      fds[i].events = POLLOUT;
    }
  END_FOR_FDS;

  res = poll(fds, nfds, timeout);

  if (!res)
    {
      /* Timeout. Run the callout */
      struct callout *f = b->callouts;

      if (!CALLBACK(f->callout))
	fatal("What now?");
      b->callouts = f->next;
206
      lsh_object_free(f);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    }
  if (res<0)
    {
      switch(errno)
	{
	case EAGAIN:
	case EINTR:
	  return 1;
	default:
	  fatal("io_run:poll failed: %s", strerror(errno));
	}
    }
  else
    { /* Process files */
      i = 0;

      /* Handle writing first */
      FOR_FDS(struct io_fd, fd, b->io, i++)
	{
	  if (fds[i].revents & POLLOUT)
	    {
	      UINT32 size = MIN(fd->buffer->end - fd->buffer->start,
				fd->buffer->block_size);
	      int res = write(fd->fd,
			      fd->buffer->buffer + fd->buffer->start,
			      size);
	      if (!res)
		fatal("Closed?");
	      if (res < 0)
		switch(errno)
		  {
		  case EINTR:
		  case EAGAIN:
		    break;
241 242 243 244 245
		  case EPIPE:
		    werror("Broken pipe\n");
		    fd->close_reason = CLOSE_WRITE_FAILED;
		    fd->close_now = 1;
		    break;
246 247 248
		  default:
		    werror("io.c: write failed, %s\n", strerror(errno));

249 250
		    fd->close_reason = CLOSE_WRITE_FAILED;
		    fd->close_now = 1;
251 252 253 254 255 256 257

		    break;
		  }
	      else
		fd->buffer->start += res;
	    }
	}
258 259
      END_FOR_FDS;

260 261 262 263
      /* Handle reading */
      i = 0; /* Start over */
      FOR_FDS(struct io_fd, fd, b->io, i++)
	{
264
	  if (!fd->close_now
265 266
	      && (fds[i].revents & POLLIN))
	    {
267 268
	      int res;
	      
269
	      struct fd_read r =
Niels Möller's avatar
Niels Möller committed
270
	      { { STACK_HEADER, do_read }, fd->fd };
Niels Möller's avatar
Niels Möller committed
271

272
	      /* The handler function may install a new handler */
273 274
	      res = READ_HANDLER(fd->handler,
				 &r.super);
275 276 277
	      /* NOTE: These flags are not mutually exclusive. All
	       * combination must be handled correctly. */

278 279 280 281
	      /* NOTE: (i) If LSH_DIE is set, LSH_CLOSE is ignored.
	       * (ii) If the fd is read_only, LSH_CLOSE is the same as LSH_DIE.
	       */
#if 0
282 283 284 285 286 287 288 289 290 291
	      if ( (res & (LSH_CLOSE | LSH_DIE)) == (LSH_CLOSE | LSH_DIE) )
		{
		  debug("return code %x, both LSH_CLOSE and LSH_DIE set.\n",
			res);
		  /* LSH_DIE takes precedence */
		  res &= ~LSH_CLOSE;

		  /* FIXME: Perhaps we should always set LSH_FAIL in
		   * this case? */
		}
292 293 294 295 296 297 298
#endif
	      if (res & LSH_HOLD)
		{
		  /* This flag should not be combined with anything else */
		  assert(res == LSH_HOLD);
		  fd->on_hold = 1;
		}
299 300 301 302 303 304 305
	      if (res & LSH_DIE)
		{
		  if (fd->buffer)
		    write_buffer_close(fd->buffer);
		  
		  fd->close_reason = LSH_FAILUREP(res)
		    ? CLOSE_PROTOCOL_FAILURE : 0;
306
		  fd->close_now = 1;
307
		}
308 309 310 311 312 313 314 315 316 317
	      else if (res & LSH_CLOSE)
		{
		  if (fd->buffer)
		    write_buffer_close(fd->buffer);
		  else
		    fd->close_now = 1;
		  
		  fd->close_reason
		    = LSH_FAILUREP(res) ? CLOSE_PROTOCOL_FAILURE : CLOSE_EOF;
		}
318 319 320 321 322 323 324
	      if (res & LSH_KILL_OTHERS)
		{
		  /* Close all other files. We have probably fork()ed. */
		  {
		    struct io_fd *p;
		    struct io_fd *next;
		    
325
		    for (p = b->io; p; p = next)
326 327 328 329 330 331 332 333 334
		      {
			next = p->next;
			
			if (p->fd != fd->fd)
			  {
			    p->close_reason = 0;
			    
			    /* In this case, it should be safe to
			     * deallocate the buffer immediately */
335
			    lsh_object_free(p->buffer);
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
			    close_fd(p);
			  }
		      }
		    if (fd->close_now)
		      {
			/* Some error occured. So close this fd too! */
			close_fd(fd);
			b->io = NULL;
			b->nio = 0;
		      }
		    else
		      { /* Keep this single descriptor open */
			fd->next = NULL;
			b->io = fd;
			b->nio = 1;
		      }
		  }{
		    struct listen_fd *p;
		    struct listen_fd *next;
355 356

		    for (p = b->listen; p; p = next)
357 358 359
		      {
			next = p->next;
			close(p->fd);
360
			lsh_space_free(p);
361 362 363 364 365 366
		      }
		    b->listen = NULL;
		    b->nlisten = 0;
		  }{
		    struct connect_fd *p;
		    struct connect_fd *next;
367 368

		    for (p = b->connect; p; p = next)
369 370 371
		      {
			next = p->next;
			close(p->fd);
372
			lsh_space_free(p);
373 374 375 376 377 378
		      }
		    b->connect = NULL;
		    b->nconnect = 0;
		  }{
		    struct callout *p;
		    struct callout *next;
379 380

		    for (p = b->callouts; p; p = next)
381 382
		      {
			next = p->next;
383
			lsh_space_free(p);
384 385 386 387 388
		      }
		    b->callouts = NULL;
		  }
		  /* Skip the rest od this iteration */
		  return 1;
Niels Möller's avatar
Niels Möller committed
389
		}
390
	    }
391
	  if (fd->close_now)
392 393 394 395 396
	    {
	      /* FIXME: Cleanup properly...
	       *
	       * After a write error, read state must be freed,
	       * and vice versa. */
397

398
	      close_fd(fd);
399

400
	      UNLINK_FD;
401

402 403 404 405 406
	      b->nio--;
	      continue;
	    }
	}
      END_FOR_FDS;
407

408 409 410
      FOR_FDS(struct listen_fd, fd, b->listen, i++)
	{
	  if (fds[i].revents & POLLIN)
Niels Möller's avatar
Niels Möller committed
411
	    {
412 413 414
	      /* FIXME: Do something with the peer address? */
	      struct sockaddr_in peer;
	      size_t addr_len = sizeof(peer);
415 416
	      int res;
	      
417 418 419 420 421 422
	      int conn = accept(fd->fd,
				(struct sockaddr *) &peer, &addr_len);
	      if (conn < 0)
		{
		  werror("io.c: accept() failed, %s", strerror(errno));
		  continue;
Niels Möller's avatar
Niels Möller committed
423
		}
424
	      res = FD_CALLBACK(fd->callback, conn);
425
	      if (LSH_ACTIONP(res))
Niels Möller's avatar
Niels Möller committed
426
		{
427 428 429
		  werror("Strange: Accepted a connection, "
			 "but failed before writing anything.\n");
		  close(fd->fd);
430
		  UNLINK_FD;
431
		  lsh_object_free(fd);
Niels Möller's avatar
Niels Möller committed
432
		  continue;
Niels Möller's avatar
Niels Möller committed
433 434
		}
	    }
435
	}
436 437 438 439 440 441
      END_FOR_FDS;
	  
      FOR_FDS(struct connect_fd, fd, b->connect, i++)
	{
	  if (fds[i].revents & POLLOUT)
	    {
442 443
	      int res = FD_CALLBACK(fd->callback, fd->fd);

444
	      if (LSH_ACTIONP(res))
445 446
		werror("Strange: Connected, "
		       "but failed before writing anything.\n");
447 448
	      b->nconnect--;
	      UNLINK_FD;
449
	      lsh_object_free(fd);
450 451 452 453 454 455 456 457
	      continue;
	    }
	}
      END_FOR_FDS;
    }
  return 1;
}

458
/* FIXME: Prehaps this function should return a suitable exit code? */
459 460
void io_run(struct io_backend *b)
{
461 462
  signal(SIGPIPE, SIG_IGN);
  
463 464
  while(io_iter(b))
    ;
465 466
}

467 468 469 470 471 472 473 474 475 476 477
void init_backend(struct io_backend *b)
{
  b->nio = 0;
  b->io = NULL;
  b->nlisten = 0;
  b->listen = NULL;
  b->nconnect = 0;
  b->connect = NULL;
  b->callouts = NULL;
}

478 479 480
/*
 * Fill in ADDR from HOST, SERVICE and PROTOCOL.
 * Supplying a null pointer for HOST means use INADDR_ANY.
481
 * Otherwise HOST is an numbers-and-dots ip-number or a dns name.
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
 *
 * PROTOCOL can be tcp or udp.
 *
 * Supplying a null pointer for SERVICE, means use port 0, i.e. no port.
 * 
 * Returns zero on errors, 1 if everything is ok.
 */
int
get_inaddr(struct sockaddr_in	* addr,
	   const char		* host,
	   const char		* service,
	   const char		* protocol)
{
  memset(addr, 0, sizeof *addr);
  addr->sin_family = AF_INET;

  /*
   *  Set host part of ADDR
   */
  if (host == NULL)
    addr->sin_addr.s_addr = INADDR_ANY;
  else
    {
      /* First check for numerical ip-number */
      addr->sin_addr.s_addr = inet_addr(host);
      if (addr->sin_addr.s_addr == (unsigned long)-1)
	{
	  struct hostent * hp;
	  
	  hp = gethostbyname(host);
	  if (hp == NULL)
	    return 0;
	  memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
	  addr->sin_family = hp->h_addrtype;
	}
    }

  /*
   *  Set port part of ADDR
   */
  if (service == NULL)
    addr->sin_port = htons(0);
  else
    {
      char		* end;
      long		  portno;

      portno = strtol(service, &end, 10);
      if (portno > 0  &&  portno <= 65535
	  &&  end != service  &&  *end == '\0')
	{
	  addr->sin_port = htons(portno);
	}
      else
	{
	  struct servent	* serv;

	  serv = getservbyname(service, "tcp");
	  if (serv == NULL)
	    return 0;
	  addr->sin_port = serv->s_port;
Niels Möller's avatar
Niels Möller committed
543 544
	}
    }
545 546

  return 1;
Niels Möller's avatar
Niels Möller committed
547 548 549 550 551 552 553 554
}

void io_set_nonblocking(int fd)
{
  if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
    fatal("io_set_nonblocking: fcntl() failed, %s", strerror(errno));
}

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
void io_set_close_on_exec(int fd)
{
  if (fcntl(fd, F_SETFD, 1) < 0)
    fatal("Can't set close-on-exec flag for fd %d: %s\n",
	  fd, strerror(errno));
}

/* ALL file descripters handled by the backend should use non-blocking mode,
 * and have the close-on-exec flag set. */

void io_init_fd(int fd)
{
  io_set_nonblocking(fd);
  io_set_close_on_exec(fd);
}

571 572 573 574 575
/* Some code is taken from bellman's tcputils. */
struct connect_fd *io_connect(struct io_backend *b,
			      struct sockaddr_in *remote,
			      struct sockaddr_in *local,
			      struct fd_callback *f)
Niels Möller's avatar
Niels Möller committed
576
{
577 578
  struct connect_fd *fd;
  int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Niels Möller's avatar
Niels Möller committed
579
  
580 581
  if (s<0)
    return NULL;
Niels Möller's avatar
Niels Möller committed
582

583 584
  debug("io.c: connecting using fd %d\n", s);
  
585
  io_init_fd(s);
Niels Möller's avatar
Niels Möller committed
586

587 588 589 590 591 592 593
  if (local  &&  bind(s, (struct sockaddr *)local, sizeof *local) < 0)
    {
      int saved_errno = errno;
      close(s);
      errno = saved_errno;
      return NULL;
    }
Niels Möller's avatar
Niels Möller committed
594

595 596 597 598 599 600 601 602 603
  if ( (connect(s, (struct sockaddr *)remote, sizeof *remote) < 0)
       && (errno != EINPROGRESS) )       
    {
      int saved_errno = errno;
      close(s);
      errno = saved_errno;
      return NULL;
    }
  
Niels Möller's avatar
Niels Möller committed
604
  NEW(fd);
605
  fd->fd = s;
Niels Möller's avatar
Niels Möller committed
606
  fd->callback = f;
607 608 609

  fd->next = b->connect;
  b->connect = fd;
Niels Möller's avatar
Niels Möller committed
610

611 612 613
  b->nconnect++;
  
  return fd;
Niels Möller's avatar
Niels Möller committed
614 615
}

616 617 618 619 620 621
struct listen_fd *io_listen(struct io_backend *b,
			    struct sockaddr_in *local,
			    struct fd_callback *callback)
{
  struct listen_fd *fd;
  int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Niels Möller's avatar
Niels Möller committed
622
  
623 624 625
  if (s<0)
    return NULL;

626 627
  debug("io.c: listening on fd %d\n", s);
  
628
  io_init_fd(s);
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

  {
    int yes = 1;
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof yes);
  }

  if (bind(s, (struct sockaddr *)local, sizeof *local) < 0)
    {
      close(s);
      return NULL;
    }

  if (listen(s, 256) < 0) 
    {
      close(s);
      return NULL;
    }

Niels Möller's avatar
Niels Möller committed
647
  NEW(fd);
648 649 650 651 652 653 654

  fd->fd = s;
  fd->callback = callback;

  fd->next = b->listen;
  b->listen = fd;
  b->nlisten++;
Niels Möller's avatar
Niels Möller committed
655
  
656 657 658
  return fd;
}

Niels Möller's avatar
Niels Möller committed
659 660
struct abstract_write *io_read_write(struct io_backend *b,
				     int fd,
Niels Möller's avatar
Niels Möller committed
661
				     struct read_handler *read_callback,
Niels Möller's avatar
Niels Möller committed
662
				     UINT32 block_size,
663
				     struct close_callback *close_callback)
664
{
Niels Möller's avatar
Niels Möller committed
665
  struct io_fd *f;
666
  struct write_buffer *buffer = write_buffer_alloc(block_size);
Niels Möller's avatar
Niels Möller committed
667

668 669
  debug("io.c: Preparing fd %d for reading and writing\n", fd);
  
670 671
  io_init_fd(fd);
  
Niels Möller's avatar
Niels Möller committed
672
  NEW(f);
Niels Möller's avatar
Niels Möller committed
673
  f->fd = fd;
674 675 676
  
  f->close_reason = -1; /* Invalid reason */
  f->close_now = 0;
677 678

  /* Reading */
Niels Möller's avatar
Niels Möller committed
679
  f->handler = read_callback;
680 681 682
  f->on_hold = 0;

  /* Writing */
Niels Möller's avatar
Niels Möller committed
683
  f->buffer = buffer;
684
  f->close_callback = close_callback;
685

Niels Möller's avatar
Niels Möller committed
686 687
  f->next = b->io;
  b->io = f;
Niels Möller's avatar
Niels Möller committed
688
  b->nio++;
689

690
  return &buffer->super;
691
}
692 693 694 695 696 697 698 699

struct io_fd *io_read(struct io_backend *b,
		      int fd,
		      struct read_handler *read_callback,
		      struct close_callback *close_callback)
{
  struct io_fd *f;

700 701
  debug("io.c: Preparing fd %d for reading\n", fd);
  
702 703
  io_init_fd(fd);

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
  NEW(f);
  f->fd = fd;
  
  f->close_reason = -1; /* Invalid reason */
  f->close_now = 0;

  /* Reading */
  f->handler = read_callback;
  f->on_hold = 0;

  /* Writing */
  f->buffer = NULL;

  f->close_callback = close_callback;

  f->next = b->io;
  b->io = f;
  b->nio++;

  return f;
}

726 727 728 729
struct io_fd *io_write(struct io_backend *b,
		       int fd,
		       UINT32 block_size,
		       struct close_callback *close_callback)
730 731 732 733
{
  struct io_fd *f;
  struct write_buffer *buffer = write_buffer_alloc(block_size);

734 735
  debug("io.c: Preparing fd %d for writing\n", fd);
  
736 737
  io_init_fd(fd);
  
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
  NEW(f);
  f->fd = fd;
  
  f->close_reason = -1; /* Invalid reason */
  f->close_now = 0;

  /* Reading */
  f->handler = NULL;

  /* Writing */
  f->buffer = buffer;
  f->close_callback = close_callback;

  f->next = b->io;
  b->io = f;
  b->nio++;

755
  return f;
756
}