Esempio n. 1
0
// given an addr:port string, opens a listening socket on that address.
int create_listener(io_poller *poller, const char *str)
{
	io_atom *atom;
	socket_addr sock = { { htonl(INADDR_ANY) }, DEFAULT_PORT };
	const char *errstr;
	int err;

	atom = malloc(sizeof(io_atom));
	if(!atom) {
		perror("malloc");
		exit(1);
	}

	// if a string was supplied, we use it, else we just
	// use the defaults that are already stored in sock.
	if(str) {
		errstr = io_parse_address(str, &sock);
		if(errstr) {
			fprintf(stderr, errstr, str);
			exit(1);
		}
	}

	err = io_listen(poller, atom, accept_proc, sock);
	if(err) {
		fprintf(stderr, "io_listen on %s:%d failed: %s\n", 
				inet_ntoa(sock.addr), sock.port, strerror(err));
		return -1;
	}
	
	printf("Opened listening socket on %s:%d, fd=%d\n",
		inet_ntoa(sock.addr), sock.port, atom->fd );
	
	return 0;
}
Esempio n. 2
0
static struct resource *
make_lshd_port(struct lshd_context *ctx, socklen_t addr_len, struct sockaddr *addr)
{
  int fd = io_bind_sockaddr(addr, addr_len);

  if (fd < 0)
    {
      werror("socket failed: %e.\n", errno);
      return NULL;
    }
  else
    {
      NEW(lshd_port, self);
      
      init_io_listen_port(&self->super, fd, lshd_port_accept);
      self->ctx = ctx;

      if (!io_listen(&self->super))
	{
	  werror("listen failed: %e.\n", errno);
	  KILL_RESOURCE(&self->super.super.super);
	  return NULL;
	}

      return &self->super.super.super;
    }
}