Esempio n. 1
0
/** select a free connection
 *
 * \param[in] sock_xml: socket on which it receives commands.
 */
static void
handle_connection(int msg_sock)
{
  int i;
  static cl_error_desc_t busy = { .code = -EXA_ERR_ADM_BUSY,
                                  .msg  = "Too many connections."};

  /* Search free slot in connection array */
  for (i = 0; i < MAX_CONNECTION; i++)
    {
      if (connectlist[i].uid == CMD_UID_INVALID && connectlist[i].fd == -1)
	{
	  exalog_debug("CONNECTION %d: Using sock %d", i, msg_sock);

	  /* uid is set when the command is actually scheduled */
	  connectlist[i].fd = msg_sock;
	  FD_SET(msg_sock, &setSocks);
	  return;
	}
    }

  /*
   * No free connection found. We disconnect the caller now because we
   * are no room in our connectionlist
   */
  exalog_error("All connections are busy sock=%d", msg_sock);

  cli_command_end_complete(msg_sock, &busy);
  os_closesocket(msg_sock);
}

/*-------------------------------------------------------------------------*/
/** \brief Connection thread: wait on xml message to pass the command
 * to the work thread.
 *
 * \param[in] sock_xml_proto: socket xml on which it receives commands.
 * \return the selected working thread or a negative error code
 *
 */
/*-------------------------------------------------------------------------*/
static void
accept_new_client(void)
{
  int fd = os_accept(listen_fd, NULL, NULL);

  exalog_debug("Got an incoming XML Request on socket: %d", fd);
  if (fd < 0)
    {
      exalog_error("Failed during admind xml connection accept: error=%s",
                   exa_error_msg(-fd));
      return;
    }

  handle_connection(fd);
}
Esempio n. 2
0
ut_setup()
{
    int len;
    struct in_addr addr;
    int reuse;

    UT_ASSERT(!os_net_init());

    /* create sockets */
    server_sock = os_socket(AF_INET, SOCK_STREAM, 0);
    UT_ASSERT(server_sock >= 0);

    reuse = 1;
    UT_ASSERT(os_setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR,
			    &reuse, sizeof(reuse)) >= 0);

    client_sock = os_socket(AF_INET, SOCK_STREAM, 0);
    UT_ASSERT(client_sock >= 0);

    /* fill in server information */
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    server_addr.sin_port = htons(DATA_PORT);

    /* bind sockets */

    UT_ASSERT(os_bind(server_sock, (struct sockaddr *)&server_addr,
		      sizeof(server_addr)) >= 0);

    UT_ASSERT(os_listen(server_sock, BACKLOG) >= 0);

    /* connect to server */
    os_inet_aton("127.0.0.1", &addr);
    memset(&client_addr, 0, sizeof(client_addr));
    client_addr.sin_family = AF_INET;
    client_addr.sin_addr.s_addr = addr.s_addr;
    client_addr.sin_port = htons(DATA_PORT);
    if (os_connect(client_sock, (struct sockaddr *)&client_addr, sizeof(client_addr)) < 0)
	UT_FAIL();

    len = sizeof(struct sockaddr_in);
    accept_sock = os_accept(server_sock, (struct sockaddr *)&server_addr, &len);
    UT_ASSERT(accept_sock >= 0);
}
Esempio n. 3
0
/**
 * Thread responsible for accepting connections
 *
 * It's a separate thread because we accept do some memory allocation and we
 * must avoid that in recv thread.
 *
 * @param unused  Unused parameter
 */
static void accept_thread(void *unused)
{
    exalog_as(EXAMSG_ISCSI_ID);

    while (algopr_run)
    {
        exa_nodeid_t node_id;
        const char *ip_addr;
        struct sockaddr_in client_address;
        int size = sizeof(client_address);

        int sock =
            os_accept(eth.accept_sock, (struct sockaddr *)&client_address, &size);

        if (sock < 0)
            continue; /* it's a false accept */

        ip_addr = os_inet_ntoa(client_address.sin_addr);

        if (!suspended)
        {
            exalog_warning("Closing incoming connection from %s while not"
                           " suspended.", ip_addr);
            __close_socket(sock);
            continue;
        }

        internal_setsock_opt(sock, SOCK_FLAGS);

        node_id = get_peer_id_from_ip_addr(ip_addr);
        if (!EXA_NODEID_VALID(node_id))
        {
            exalog_warning("Closing incoming connection from unknown node %s.",
                           ip_addr);
            __close_socket(sock);
            continue;
        }

        set_peer_socket(node_id, ip_addr, sock);
    }
}
int 
main(int argc, char *argv[])
{
	int listenfd;
	int connfd;
	int on = 1;
	struct sockaddr_in serv_addr, cli_addr;
	socklen_t cli_len;
	int client_fd[FD_SETSIZE];/* each for every client  */
	fd_set allset; /* save interested descriptors */
	fd_set rdset; /* select on this set */
	int maxfd;
	int max_indx; /* max index in client_fd[] array */
	int i, n;
	int nready;
	char buf[MAXLINE];

	if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		fprintf(stderr, "socket error: %s\n", strerror(errno));
		exit(1);
	}

	os_setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

	bzero(&serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_addr.sin_port = htons(SERV_PORT);

	if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
		fprintf(stderr, "bind error: %s\n", strerror(errno));
		exit(1);
	}
	
	if (listen(listenfd,  LISTEN_QUEUE) < 0) {
		fprintf(stderr, "listern error: %s\n", strerror(errno));
		exit(1);
	}

	for (i=0; i < FD_SETSIZE; i++)
		client_fd[i] = -1;
	
	FD_ZERO(&allset);
	FD_SET(listenfd, &allset);
	maxfd = listenfd;
	max_indx = 0;
	for (; ;) {
		rdset = allset;
		nready  = os_select(maxfd + 1, &rdset, NULL, NULL, NULL); /* select will modify rdset once return */

		/* task 1: listen... accept new connection */
		if(FD_ISSET(listenfd, &rdset)) {
			cli_len = sizeof(cli_addr);
			connfd = os_accept(listenfd, (struct sockaddr *)&cli_addr, &cli_len);

			for (i=0; i < FD_SETSIZE; i++) { /* a new client coming, select a useable fd */
				if (client_fd[i] == -1) {
					client_fd[i] = connfd;
					maxfd = (connfd > maxfd) ? connfd : maxfd;
					break;
				}
			}
			FD_SET(connfd, &allset);
			if (i > max_indx)
				max_indx = i;
			if (--nready <= 0)
				continue;  /* no more readable descriptors */
		}
		
		/* task 2: deal with all  clients' data  */
		for (i=0; i < maxfd; i++) {
			if (client_fd[i] == -1)
				continue;
			if (FD_ISSET(client_fd[i], &rdset)) {
				if ((n = os_read(client_fd[i], buf, MAXLINE)) == 0) {
					close(client_fd[i]);
					FD_CLR(client_fd[i], &allset);
					client_fd[i] = -1;
				}
				else {
#ifdef DEBUG
					os_write(fileno(stdout), buf, n);
#endif
					os_write(client_fd[i], buf, n);
				}
			}
			if (--nready <= 0)
				break;  /* no more readable descriptors */
		}
#if 0

		cli_len = sizeof(cli_addr);
	    if ((connfd = accept(listenfd, (struct sockaddr *)&cli_addr, &cli_len)) < 0) {
			if (errno == EINTR)
				continue;
			else
				os_err_sys("accept error");
		}

			
		if ((childpid = fork()) == 0) {
			close(listenfd);
			echo_string(connfd);
			exit(1);
		}
		close(connfd);
#endif
	}
	return 0;
}