io.c 8.45 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3 4 5 6 7
/* io.c
 *
 */

#include <unistd.h>
#include <poll.h>
#include <errno.h>
Niels Möller's avatar
Niels Möller committed
8
#include <string.h>
Niels Möller's avatar
Niels Möller committed
9 10 11 12 13

#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>

Niels Möller's avatar
Niels Möller committed
14 15 16 17 18
#include "io.h"
#include "werror.h"
#include "write_buffer.h"
#include "xalloc.h"

Niels Möller's avatar
Niels Möller committed
19 20 21 22 23 24 25 26 27 28 29
/* A little more than an hour */
#define MAX_TIMEOUT 4000

struct fd_read
{
  struct abstract_read a;
  int fd;
};

static int do_read(struct fd_read *closure, UINT8 *buffer, UINT32 length)
{
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  while(1)
    {
      int res = read(closure->fd, buffer, length);
      if (!res)
	return A_EOF;
      if (res > 0)
	return res;
      
      switch(errno)
	{
	case EINTR:
	  continue;
	case EWOULDBLOCK:  /* aka EAGAIN */
	  return 0;
	default:
Niels Möller's avatar
Niels Möller committed
45
	  werror("io.c: do_read: read() failed, %s\n", strerror(errno));
46 47 48 49
	  return A_FAIL;
	}
    }
}
Niels Möller's avatar
Niels Möller committed
50

51 52 53
#define FOR_FDS(type, fd, list, extra)				\
{								\
  type **(_fd);							\
Niels Möller's avatar
Niels Möller committed
54 55
  type *(fd);							\
  for(_fd = &(list); ((fd) = *_fd); _fd = &(*_fd)->next, (extra))	
56 57 58 59 60 61


#define END_FOR_FDS } 
     
#define UNLINK_FD (*_fd = (*_fd)->next)
    
Niels Möller's avatar
Niels Möller committed
62 63 64 65 66 67 68 69 70 71
void io_run(struct io_backend *b)
{
  while(1)
    {
      struct pollfd *fds;
      int i;
      nfds_t nfds;
      int timeout;
      int res;
      
Niels Möller's avatar
Niels Möller committed
72
      nfds = b->nio + b->nlisten + b->nconnect;
Niels Möller's avatar
Niels Möller committed
73 74 75

      if (b->callouts)
	{
Niels Möller's avatar
Niels Möller committed
76 77
	  time_t now = time(NULL);
	  if (now >= b->callouts->when)
Niels Möller's avatar
Niels Möller committed
78 79 80
	    timeout = 0;
	  else
	    {
Niels Möller's avatar
Niels Möller committed
81
	      if (b->callouts->when > now + MAX_TIMEOUT)
Niels Möller's avatar
Niels Möller committed
82 83
		timeout = MAX_TIMEOUT * 1000;
	      else
Niels Möller's avatar
Niels Möller committed
84
		timeout = (b->callouts->when - now) * 1000;
Niels Möller's avatar
Niels Möller committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
	    }
	}
      else
	{
	  if (!nfds)
	    /* All done */
	    break;
	  timeout = -1;
	}
      
      fds = alloca(sizeof(struct pollfd) * nfds);

      /* Handle fds in order: read, accept, connect, write, */
      i = 0;
99

100
      FOR_FDS(struct io_fd, fd, b->io, i++)
101
	{
Niels Möller's avatar
Niels Möller committed
102 103 104 105 106 107 108 109
	  fds[i].fd = fd->on_hold ? -1 : fd->fd;
	  fds[i].events = 0;
	  if (!fd->on_hold)
	    fds[i].events |= POLLIN;

	  /* pre_write returns 0 if the buffer is empty */
	  if (write_buffer_pre_write(fd->buffer))
	    fds[i].events |= POLLOUT;
110 111 112
	}
      END_FOR_FDS;

Niels Möller's avatar
Niels Möller committed
113
      FOR_FDS(struct listen_fd, fd, b->listen, i++)
114
	{
Niels Möller's avatar
Niels Möller committed
115 116
	  fds[i].fd = fd->fd;
	  fds[i].events = POLLIN;
117 118 119 120 121
	}
      END_FOR_FDS;

      FOR_FDS(struct connect_fd, fd, b->connect, i++)
	{
Niels Möller's avatar
Niels Möller committed
122 123
	  fds[i].fd = fd->fd;
	  fds[i].events = POLLOUT;
124 125 126
	}
      END_FOR_FDS;

Niels Möller's avatar
Niels Möller committed
127 128 129 130 131
      res = poll(fds, nfds, timeout);

      if (!res)
	{
	  /* Timeout. Run the callout */
132 133
	  struct callout *f = b->callouts;
	  
Niels Möller's avatar
Niels Möller committed
134
	  if (!CALLBACK(f->callout))
Niels Möller's avatar
Niels Möller committed
135
	    fatal("What now?");
136 137
	  b->callouts = f->next;
	  free(f);
Niels Möller's avatar
Niels Möller committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	}
      if (res<0)
	{
	  switch(errno)
	    {
	    case EAGAIN:
	    case EINTR:
	      continue;
	    default:
	      fatal("io_run:poll failed: %s", strerror(errno));
	    }
	}
      else
	{ /* Process files */
	  i = 0;
153

154 155 156
	  /* Handle writing first */
	  FOR_FDS(struct io_fd, fd, b->io, i++)
	    {
Niels Möller's avatar
Niels Möller committed
157
	      if (fds[i].revents & POLLOUT)
158 159 160
		{
		  UINT32 size = MIN(fd->buffer->end - fd->buffer->start,
				    fd->buffer->block_size);
Niels Möller's avatar
Niels Möller committed
161 162
		  int res = write(fd->fd,
				  fd->buffer->buffer + fd->buffer->start,
163 164 165 166 167 168 169 170 171 172 173 174 175
				  size);
		  if (!res)
		    fatal("Closed?");
		  if (res < 0)
		    switch(errno)
		      {
		      case EINTR:
		      case EAGAIN:
			break;
		      default:
			CALLBACK(fd->close_callback);
			/* FIXME: Must do this later. Perhaps add a
			 * closed flag to th io_fd struct? */
Niels Möller's avatar
Niels Möller committed
176 177
			fd->please_close = 1;

178 179 180 181 182 183 184 185 186 187 188
			break;
		      }
		  else
		    fd->buffer->start += res;
		}
	    }
	  END_FOR_FDS;

	  /* Handle reading */
	  i = 0; /* Start over */
	  FOR_FDS(struct io_fd, fd, b->io, i++)
189
	    {
Niels Möller's avatar
Niels Möller committed
190 191
	      if (!fd->please_close
		  && (fds[i].revents & POLLIN))
192 193
		{
		  struct fd_read r =
Niels Möller's avatar
Niels Möller committed
194
		  { { (abstract_read_f) do_read }, fd->fd };
195

196
		  /* The handler function returns a new handler for the
197
		   * file, or NULL. */
Niels Möller's avatar
Niels Möller committed
198 199 200
		  if (!(fd->handler
			= READ_HANDLER(fd->handler,
				       (struct abstract_read *) &r)))
201
		    {
Niels Möller's avatar
Niels Möller committed
202
		      fd->please_close = 1;
203 204
		    }
		}
Niels Möller's avatar
Niels Möller committed
205 206 207 208 209 210 211 212 213 214
	      if (fd->please_close)
		{
		  /* FIXME: Cleanup properly...
		   *
		   * After a write error, read state must be freed,
		   * and vice versa. */
		  UNLINK_FD;
		  free(fd->buffer);
		  free(fd);
		}
215 216 217
	    }
	  END_FOR_FDS;

Niels Möller's avatar
Niels Möller committed
218
	  FOR_FDS(struct listen_fd, fd, b->listen, i++)
Niels Möller's avatar
Niels Möller committed
219
	    {
Niels Möller's avatar
Niels Möller committed
220
	      if (fds[i].revents & POLLIN)
Niels Möller's avatar
Niels Möller committed
221
		{
Niels Möller's avatar
Niels Möller committed
222 223 224 225
		  /* FIXME: Do something with the peer address? */
		  struct sockaddr_in peer;
		  
		  int conn = accept(fd->fd, &peer, sizeof(peer));
226 227 228 229 230 231 232 233 234 235 236
		  if (conn < 0)
		    {
		      werror("io.c: accept() failed, %s", strerror(errno));
		      continue;
		    }
		  if (!FD_CALLBACK(fd->callback, conn))
		    {
		      /* FIXME: Should fd be closed here? */
		      UNLINK_FD;
		      free(fd);
		    }
Niels Möller's avatar
Niels Möller committed
237 238
		}
	    }
239 240 241
	  END_FOR_FDS;
	  
	  FOR_FDS(struct connect_fd, fd, b->connect, i++)
Niels Möller's avatar
Niels Möller committed
242
	    {
243
	      if (fds[i]->revents & POLLOUT)
Niels Möller's avatar
Niels Möller committed
244
		{
245 246 247 248
		  if (!FD_CALLBACK(fd->callback, fd->fd))
		    fatal("What now?");
		  UNLINK_FD;
		  free(fd);
Niels Möller's avatar
Niels Möller committed
249 250
		}
	    }
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	  END_FOR_FDS;
	}
    }
}

/*
 * Fill in ADDR from HOST, SERVICE and PROTOCOL.
 * Supplying a null pointer for HOST means use INADDR_ANY.
 * Otherwise HOST is an numbers-and-dits ip-number or a dns name.
 *
 * 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
321 322
	}
    }
323 324

  return 1;
Niels Möller's avatar
Niels Möller committed
325 326 327 328 329 330 331 332
}

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

333 334 335 336 337
/* 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
338
{
339 340
  struct connect_fd *fd;
  int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Niels Möller's avatar
Niels Möller committed
341
  
342 343
  if (s<0)
    return NULL;
Niels Möller's avatar
Niels Möller committed
344

345
  io_set_nonblocking(s);
Niels Möller's avatar
Niels Möller committed
346

347 348 349 350 351 352 353
  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
354

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  if ( (connect(s, (struct sockaddr *)remote, sizeof *remote) < 0)
       && (errno != EINPROGRESS) )       
    {
      int saved_errno = errno;
      close(s);
      errno = saved_errno;
      return NULL;
    }
  
  fd = xalloc(sizeof(struct connect_fd));
  fd->fd = s;
  fd->callback = callback;

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

371 372 373
  b->nconnect++;
  
  return fd;
Niels Möller's avatar
Niels Möller committed
374 375
}

376 377 378 379 380 381
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
382
  
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  if (s<0)
    return NULL;

  io_set_nonblocking(s);

  {
    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;
    }

  fd = xalloc(sizeof(struct connect_fd));

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

  fd->next = b->listen;
  b->listen = fd;
  b->nlisten++;
Niels Möller's avatar
Niels Möller committed
413
  
414 415 416
  return fd;
}

Niels Möller's avatar
Niels Möller committed
417 418 419 420 421
struct abstract_write *io_read_write(struct io_backend *b,
				     int fd,
				     struct read_callback *read_callback,
				     UINT32 block_size,
				     struct callback *close_callback)
422
{
Niels Möller's avatar
Niels Möller committed
423
  struct io_fd = xalloc(sizeof(struct io_fd));
424 425 426
  struct write_buffer *buffer = write_buffer_alloc(block_size);
  
  fd->fd = fd;
Niels Möller's avatar
Niels Möller committed
427 428 429
  fd->please_close = 0;
  
  fd->read_callback = read_callback;
430 431 432
  fd->close_callback = close_callback;
  fd->buffer = buffer;

Niels Möller's avatar
Niels Möller committed
433 434 435
  fd->next = b->io;
  b->io = fd;
  b->nio++;
436 437 438

  return (struct abstract_write *) buffer;
}