Exemple #1
0
int main(int argc, char **argv)
{
	int listenfd, connfd, port; 
	socklen_t clientlen = sizeof(struct sockaddr_in);
	struct sockaddr_in clientaddr;
	static pool pool; 

	if (argc != 2) {
		fprintf(stderr, "usage: %s <port>\n", argv[0]);
		exit(0);
	}
	port = atoi(argv[1]);

	listenfd = Open_listenfd(port);
	init_pool(listenfd, &pool); //line:conc:echoservers:initpool
	while (1) {
		/* Wait for listening/connected descriptor(s) to become ready */
		pool.ready_set = pool.read_set;
		pool.nready = Select(pool.maxfd+1, &pool.ready_set, NULL, NULL, NULL);

		/* If listening descriptor ready, add new client to pool */
		if (FD_ISSET(listenfd, &pool.ready_set)) { //line:conc:echoservers:listenfdready
			connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); //line:conc:echoservers:accept
			add_client(connfd, &pool); //line:conc:echoservers:addclient
		}

		/* Echo a text line from each ready connected descriptor */ 
		check_clients(&pool); //line:conc:echoservers:checkclients
	}
}
Exemple #2
0
void setup_socket()
{
    printf("%s\n","We are starting zzz...");
    int listen_fd = Open_listenfd(my_port);
    printf("Setting up listen_fd with number: %d\n", listen_fd);
    SA clientaddr;
    /*// Setup your read_set with FD_ZERO and the server socket descriptor
    FD_ZERO(&p.read_set);
    FD_SET(listen_fd, &p.read_set);
    p.maxfd = listen_fd + 1;*/
    init_pool(listen_fd, &p);
    while(1)
    {
        p.ready_set = p.read_set;
        p.nready = select(p.maxfd + 1, &p.ready_set, &p.write_set, NULL, NULL);
        if(FD_ISSET(listen_fd, &p.ready_set))
        {
            socklen_t clientlen = sizeof(clientaddr);
            int conn_fd = Accept(listen_fd, &clientaddr, &clientlen);//If there is a request then accept it
            printf("Accepting with conn_fd: %d\n", conn_fd);
            add_client(conn_fd, &p);
        }
        check_clients(&p);
        /*socklen_t clientlen = sizeof(clientaddr);
        int conn_fd = Accept(listen_fd, &clientaddr, &clientlen);//If there is a request then accept it
        printf("Accepting with conn_fd: %d\n", conn_fd);
        break;*/
    }
}
int main(int argc, char **argv)
{
    int port, listenfd, connfd;
    struct sockaddr_in clientaddr;
    int clientlen = sizeof(struct sockaddr_in);
    Pool pool;

    if(argc != 2){
        fprintf(stderr, "Usage: %s <port>\n", argv[0]);
        exit(0);
    }
    port = atoi(argv[1]);

    listenfd = open_listenfd(port);
    init_pool(listenfd, &pool);
    while(1){
        /* wait for listening/connected descriptors to become ready */
        pool.ready_set = pool.read_set;
        pool.nready = select(pool.maxfd + 1, &pool.ready_set, NULL, NULL, NULL);

        /* If listening descriptor become ready, add new client to pool */
        if(FD_ISSET(listenfd, &pool.ready_set)){
            connfd = accept(listenfd, (SA *)&clientaddr, &clientlen);
            add_client(connfd, &pool);
        }

        /* echo a textline from each ready connected descriptor */
        check_clients(&pool);
    }
    return 0;
}
int main(int argc, char **argv[])
{
  int listenfd, connfd, port;
  socklen_t clientlen = sizeo(struct sockaddr_in);
  struct sockaddr_in clientaddr;
  static pool pool;
  
  if (argc != 2) {
    fprintf(stderr, "usage:  %s <port>\n", argv[0]);
    exit(0);
  }
  port = atoi(argv[1]);
  
  listenfd = Open_listenfd(port);
  init_pool(listenfd, &pool);
  
  while(1) {
    //wait for listening/connected descriptor(s) to become ready
    pool.ready_set = pool.read_set;
    pool.nready = Select(pool.maxfd+1, &pool.ready_set, NULL, NULL, NULL);
    
    //if listening descriptors ready. add new client to pool
    if (FD_ISSET(listenfd, &pool.ready_set)) {
      connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
      add_client(connfd, &pool);
    }
    
    //echo a text line from each ready connected descriptor
    check_clients(&pool);
  }
}
Exemple #5
0
int main(int argc, char **argv)
{
  int serfd;

  if (argc != 4)
    {
      fprintf(stderr,
	      "Usage: %s <port> <device> <rate> - act as a serial forwarder on <port>\n"
	      "(listens to serial port <device> at baud rate <rate>)\n" ,
	      argv[0]);
      exit(2);
    }

  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
    fprintf(stderr, "Warning: failed to ignore SIGPIPE.\n");

  open_serial(argv[2], platform_baud_rate(argv[3]));
  serfd = serial_source_fd(src);
  open_server_socket(atoi(argv[1]));

  for (;;)
    {
      fd_set rfds;
      int maxfd = -1;
      struct timeval zero;
      int serial_empty;
      int ret;

      zero.tv_sec = zero.tv_usec = 0;

      FD_ZERO(&rfds);
      fd_wait(&rfds, &maxfd, serfd);
      fd_wait(&rfds, &maxfd, server_socket);
      wait_clients(&rfds, &maxfd);

      serial_empty = serial_source_empty(src);
      if (serial_empty)
	ret = select(maxfd + 1, &rfds, NULL, NULL, NULL);
      else
	{
	  ret = select(maxfd + 1, &rfds, NULL, NULL, &zero);
	  check_serial();
	}
      if (ret >= 0)
	{
	  if (FD_ISSET(serfd, &rfds))
	    check_serial();

	  if (FD_ISSET(server_socket, &rfds))
	    check_new_client();

	  check_clients(&rfds);
	}
    }
}
int			check_select(Uint32 timeout)
{
  SOCKET		maxfd;
  int			retval;
  struct timeval	tv;
  struct timeval	before;
  struct timeval	left;
  fd_set		maskr;
  fd_set		maskw;
  int			done;

  check_dead();
  FD_ZERO(&maskr);
  FD_ZERO(&maskw);
  maxfd = fill_fd(&maskr, &maskw); 
  tv.tv_sec = timeout / 1000;
  tv.tv_usec = (timeout % 1000) * 1000;
  done = 0;
  while (!done)
    {
      cnt->select_recv = 0;
      gettimeofday(&before, NULL);
      retval = select((int)maxfd + 1, &maskr, &maskw, NULL, &tv);
      done = 0;
      if (retval > 0 && check_clients(&maskr, &maskw, &retval))
	done += 1;
      if (retval > 0 && check_server(&maskr, &maskw, &retval))
	done += 8;
      if (retval)
	{
	  if (retval < 0 && errno != EINTR)
	    fprintf(stderr, "select: %s", strerror(errno));
	  else
	    fprintf(stderr, "select: on m'aurait mentit (reste:%d) ?\n",
		    retval);
	  exit(-1);
	}
      check_dead();
      if (!timeout)
	return (done);
      gettimeofday(&left, NULL);
      left.tv_sec -= before.tv_sec;
      left.tv_usec -= before.tv_usec;
      if (cnt->select_recv &&
	  left.tv_sec < tv.tv_sec && left.tv_usec < tv.tv_usec)
	{
	  done = 0;
	  memcpy(&tv, &left, sizeof(left));
	}
    }
  return (done);
}
Exemple #7
0
int main(int argc, char **argv) {
    int listenfd, connfd;
    socklen_t clientlen = sizeof(struct sockaddr_in);
    struct sockaddr_in clientaddr;
    user* u;

    /* Init node and set port accoriding to configue file */
    init_node(argc, argv);

    /* Init the linked list of channel and the table of user */
    init_channel();
    init_user();
    
    /* Init server socket and set it as unblocked */
    listenfd = init_unblocking_server_socket(curr_node_config_entry->irc_port);

    /* connect to local daemon */
    local_client_fd = socket_connect(curr_node_config_entry->ipaddr,curr_node_config_entry->local_port);

    /* Init struct pool */
    init_pool();

    add_listen_fd(listenfd);

    while (1) {
        /* Wait for listening/connected descriptor(s) to become ready */
        p.ready_set = p.read_set;
        p.nready = Select(p.maxfd+1, &p.ready_set, NULL, NULL, NULL);

        /* If listening descriptor ready, add new client to pool */
        if (FD_ISSET(listenfd, &p.ready_set)) {
            connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

            /* create a user struct for the connect fd */
            u = (user*) Calloc(1,sizeof(user));
            user_table[connfd] = u;

            /*set server_name and host_name for user */
            u->host_name = strdup(Gethostbyaddr((const char*)&clientaddr.sin_addr,sizeof(clientaddr.sin_addr),AF_INET)->h_name);
            u->server_name = strdup(Gethostbyname("localhost")->h_name);

            add_client(connfd);
        }

        /* Echo a text line from each ready connected descriptor */
        check_clients();
    }
}
Exemple #8
0
int main(int argc, char* argv[])
{
    int sock, client_sock;
    socklen_t cli_size;
    struct sockaddr_in addr, cli_addr;
    static pool pool;

    if (argc != 4)
    {
        fprintf(stderr, "usage: %s <HTTP port> <log file> <www folder>\n",
                argv[0]);
        return EXIT_FAILURE;
    }

    http_port = atoi(argv[1]);
    log_file = argv[2];
    root = argv[3];

    if (write_log(DEBUG, "Server is starting...") == E_FILEOPEN)
    {
        fprintf(stderr, "Failed to start log engine... Closing the server\n");
        return EXIT_FAILURE;
    }

    fprintf(stdout, "----- Echo Server -----\n");

    /* all networked programs must create a socket */
    if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    {
        write_log(ERROR, "Failed creating socket... Closing the server...");
        return EXIT_FAILURE;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(http_port);
    addr.sin_addr.s_addr = INADDR_ANY;

    /* servers bind sockets to ports---notify the OS they accept connections */
    if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)))
    {
        close_socket(sock);
        write_log(ERROR, "Failed binding socket... Closing the server...");
        return EXIT_FAILURE;
    }

    if (listen(sock, 5))
    {
        close_socket(sock);
        write_log(ERROR, "Failed listening on socket... Closing the server...");
        return EXIT_FAILURE;
    }

    init_pool(sock, &pool);

    /* finally, loop waiting for input and then write it back */
    while (1)
    {
        pool.ready_set = pool.read_set;
        if ((pool.nready = select(pool.maxfd + 1, &pool.ready_set, NULL, NULL,
                NULL)) == -1)
        {
            close_socket(sock);
            write_log(ERROR,
                    "Failed selecting from pool... Closing the server...");
            return EXIT_FAILURE;
        }

        if (FD_ISSET(sock, &pool.ready_set))
        {
            cli_size = sizeof(cli_addr);
            if ((client_sock = accept(sock, (struct sockaddr *) &cli_addr,
                    &cli_size)) == -1)
            {
                close_socket(sock);
                write_log(ERROR,
                        "Failed accepting connection.. Closing the server...");
                return EXIT_FAILURE;
            }

            log_fp = fopen(log_file, "a");
            fprintf(log_fp, "%s\t%s\tAccepting a new client: %d\n",
                    current_time(), DEBUG, client_sock);
            fclose(log_fp);

            /* add the client to the pool */
            if (add_client(client_sock, &pool) != 0)
            {
                close_socket(sock);
                // TODO: send some error code to client??
                write_log(ERROR,
                        "Failed adding client: Too many clients... Closing the server...");
                return EXIT_FAILURE;
            }
        }

        /* check each client and process the ready connected descriptor */
        if (check_clients(&pool) != 0)
        {
            close_socket(sock);
            return EXIT_FAILURE;
        }
    }

    close_socket(sock);
    return EXIT_SUCCESS;
}
Exemple #9
0
int main(int argc, char* argv[])
{
    int sock, s_sock, client_fd;
    socklen_t client_size;
    struct sockaddr_in addr, client_addr;
    struct timeval tv;
    static pool pool;
    sigset_t mask;

    if (argc != 9)  usage_exit();

    // parse arguments
    STATE.port = (int)strtol(argv[1], (char**)NULL, 10);
    STATE.s_port = (int)strtol(argv[2], (char**)NULL, 10);
    strcpy(STATE.log_path, argv[3]);
    strcpy(STATE.lck_path, argv[4]);
    strcpy(STATE.www_path, argv[5]);
    strcpy(STATE.cgi_path, argv[6]);
    strcpy(STATE.key_path, argv[7]);
    strcpy(STATE.ctf_path, argv[8]);

    if (STATE.www_path[strlen(STATE.www_path)-1] == '/')
         STATE.www_path[strlen(STATE.www_path)-1] = '\0';

    daemonize();
    STATE.log = log_open(STATE.log_path);
    
    Log("Start Liso server. Server is running in background. \n");

    /* all networked programs must create a socket
     * PF_INET - IPv4 Internet protocols
     * SOCK_STREAM - sequenced, reliable, two-way, connection-based byte stream
     * 0 (protocol) - use default protocol
     */
    // create sock for HTTP connection
    if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    {
        Log("Error: failed creating socket for HTTP connection.\n");
        fclose(STATE.log);
        return EXIT_FAILURE;
    }

    STATE.sock = sock;
    Log("Create socket success: sock =  %d \n", sock);

    addr.sin_family = AF_INET;
    addr.sin_port = htons(STATE.port);
    addr.sin_addr.s_addr = INADDR_ANY;
    /* servers bind sockets to ports---notify the OS they accept connections */
    if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)))
    {
        Log("Error: failed binding socket.\n");
        clean();
        return EXIT_FAILURE;
    }
    Log("Bind success! \n");

    if (listen(sock, MAX_CONN))
    {
        Log("Error: listening on socket.\n");
        clean();
        return EXIT_FAILURE;
    }
    Log("Listen success! >>>>>>>>>>>>>>>>>>>> \n");

    // create sock for HTTPS connection
    Log("Create sock for HTTPS connection \n");
    if ((s_sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    {
        Log("Error: failed creating socket for HTTPS connection.\n");
        close(sock);
        fclose(STATE.log);
        return EXIT_FAILURE;
    }

    STATE.s_sock = s_sock;
    Log("Create HTTPS socket success: sock =  %d \n", s_sock);

    addr.sin_family = AF_INET;
    addr.sin_port = htons(STATE.s_port);
    addr.sin_addr.s_addr = INADDR_ANY;
    /* servers bind sockets to ports---notify the OS they accept connections */
    if (bind(s_sock, (struct sockaddr *) &addr, sizeof(addr)))
    {
        Log("Error: failed binding socket.\n");
        close(sock); close(s_sock); fclose(STATE.log);
        return EXIT_FAILURE;
    }
    Log("Bind success! \n");

    if (listen(s_sock, MAX_CONN))
    {
        Log("Error: listening on socket.\n");
        close(sock); close(s_sock); fclose(STATE.log);
        return EXIT_FAILURE;
    }
    Log("Listen success! >>>>>>>>>>>>>>>>>>>> \n");
    init_pool(&pool);

    // the main loop to wait for connections and serve requests
    while (KEEPON)
    {
       tv.tv_sec = 1; // timeout = 1 sec
       tv.tv_usec = 0;
       pool.ready_set = pool.read_set;

       sigemptyset(&mask);
       sigaddset(&mask, SIGHUP);
       sigprocmask(SIG_BLOCK, &mask, NULL);
       pool.nready = select(pool.maxfd+1, &pool.ready_set, NULL, NULL, &tv);
       sigprocmask(SIG_UNBLOCK, &mask, NULL);

       if (pool.nready < 0)
       {
           if (errno == EINTR)
           {
               Log("Shut down Server >>>>>>>>>>>>>>>>>>>> \n");
               break;
           }
          
           Log("Error: select error \n");
           continue;
       }

       // if there is new connection, accept and add the new client to pool
       if (FD_ISSET(sock, &pool.ready_set))
       {
           client_size = sizeof(client_addr);
           client_fd = accept(sock, (struct sockaddr *) &client_addr,
                              &client_size);

           if (client_fd < 0) ///TODO
           {
               Log("Error: accepting connection. \n");
               continue;
           }

           Log("accept client: client_fd=%d \n", client_fd);

           if (STATE.is_full)
           {
               pool.nready--;
               serve_error(client_fd, "503", "Service Unavailable",
                    "Server is too busy right now. Please try again later.", 1);
               close(client_fd);
           }
           else
              add_client(client_fd, &pool);
       }

       // process each ready connected descriptor
       check_clients(&pool);
    }

    lisod_shutdown();

    return EXIT_SUCCESS; // to make compiler happy
}