Beispiel #1
0
int main(int argc, char *argv[]){
    if(argc<3){
	error("Usage: \n\t./ethtest client hostname port \n\t./ethtest server port\n\t./ethtest mvm_server port\n");
	_Exit(1);
    }
    set_realtime(0, -20);

   
    char *NBUF=getenv("NBUF");
    if(NBUF){
	nbuf=strtol(NBUF, NULL, 10);
	dbg("nbuf=%d\n", nbuf);
    }
    char *ip=0;
    if(argc>3){
	ip=argv[3];
    }
    
    if(!strcmp(argv[1], "server")){
	int port=strtol(argv[2], NULL, 10);
	listen_port(port, ip, server, 0, NULL, 1);
    }else if(!strcmp(argv[1],"mvm_server")){
	int port=strtol(argv[2], NULL, 10);
	listen_port(port, ip, mvm_server, 0, NULL, 1);
    }else{
	if(argc<4){
	    error("Usage: ./ethtest client hostname port [nrep] [nmin] [nmax] [nstep]");
	}
	const char *host=argv[2];
	int port=strtol(argv[3], NULL, 10);
	int nstep=100;
	int nmin=409584;
	int nmax=409584;
	int nrep=1000;
	if(argc>4){
	    nrep=strtol(argv[4], NULL, 10);
	}
	if(argc>5){
	    nmin=strtol(argv[5], NULL, 10);
	}
	if(argc>6){
	    nmax=strtol(argv[6], NULL, 10);
	}
	if(argc>7){
	    nstep=strtol(argv[7], NULL, 10);
	}

	client(host, port, nmin, nmax, nstep, nrep);
    }
}
Beispiel #2
0
RsiServer::RsiServer(int port, SysInfo *sysinfo){
    this->_sysinfo = sysinfo;
    if(signal(SIGCHLD, sig_child) == SIG_ERR){
        LOG_ERROR("could not bind SIGCHLD to sig_child");
        exit(-1);
    }
    if(signal(SIGINT, sig_int) == SIG_ERR){
        LOG_ERROR("could not bind SIGINT to sig_int");
        exit(-1);
    }
    int server_sockfd = listen_port(port);
    int keep_alive = 1;
    if(setsockopt(server_sockfd, SOL_SOCKET,
                  SO_KEEPALIVE, (void *)&keep_alive,
                  sizeof(keep_alive)) == -1){
        LOG_ERROR("Could not set keep_alive option");
        exit(-1);
    }
    while(1){
        int client_sockfd = accept_client(server_sockfd);
        pid_t pid = fork();
        if(pid == 0){ // child process
            close(server_sockfd);
            while (true) {
                if(communicate(client_sockfd) < 0)
                    exit(-1);
            }
        }
        else{
            close(client_sockfd);
        }
    }
}
Beispiel #3
0
int main()
{
  setbuf(stdout, NULL);
  signal(SIGINT, &user_exit);
  signal(SIGABRT, &user_exit);
  signal(SIGTERM, &user_exit);
  printf("\x1b[0;31m\n[[[ " SERVER_NAME " ]]]\n\n\x1b[0m");

  // LOAD HTML

  char buffer[MAX_INDEX_HTM_SIZE];
  FILE *f;
  f = fopen("index.htm", "rb");
  if (!f) die();
  fread(buffer, MAX_INDEX_HTM_SIZE, 1, f);
  fclose(f);
  char str_port[255]; sprintf(str_port, "%d", LISTEN_PORT);
  int n = strlen(buffer) - 8 + strlen(str_port);
  index_response = new char[n + 2000];
  char* p_port = strstr(buffer, "[%PORT%]");
  *p_port = 0;
  sprintf(index_response, "HTTP/1.1 200 OK\r\nServer: " SERVER_NAME "\r\nContent-Length: %d\r\nConnection: close\r\nContent-Type: text/html; charset=utf-8\r\n\r\n%s%s%s", n, buffer, str_port, p_port + 8);
  index_response_size = strlen(index_response);

  // SERVER LOGIC

  pthread_t io_thread[IO_THREADS];
  int io_thread_index[IO_THREADS];
  for (int i = 0; i < IO_THREADS; i++) {
    epoll[i] = epoll_create1(0);
    io_thread_index[i] = i;
    if (pthread_create(&io_thread[i], NULL, loop_io, &io_thread_index[i]) != 0) die();
  }
  listen_port(LISTEN_PORT);
  sockaddr_in sin;
  socklen_t sock_len = sizeof(sockaddr_in);
  int client = 0;
  int ee = 0;
  epoll_event ev;
  while ((client = accept(server, (sockaddr*) &sin, &sock_len)) > 0) {
    setnonblocking(client);
    if (client >= MAX_FD) {
      shutdown(client, SHUT_RDWR);
      close(client);
    }
    ev.data.fd = client;
    fd_status[client] = 0;
    ev.events = EPOLLIN | EPOLLET;
    epoll_ctl(epoll[ee], EPOLL_CTL_ADD, client, &ev);
    event_count[ee]++;
    echo("thr %d con %d add %s:%d \x1b[0;31mto %d\n\x1b[0m", ee, event_count[ee], inet_ntoa(sin.sin_addr), sin.sin_port, client);
    ee = (ee + 1) % IO_THREADS;
  }
  close(server);
  return 0;
}
Beispiel #4
0
int main(int argc, char **argv) {
    FILE *client_sockf;
    char *client_input = NULL;
    char protoname[] = "tcp";
    int client_input_len;
    int client_sockfd;
    int i;
    int server_sockfd;
    size_t client_input_getline_size = 0;
    socklen_t client_len;
    struct sockaddr_in client_address;
    unsigned short server_port = 12345u;

    if (argc > 1) {
        server_port = strtol(argv[1], NULL, 10);
    }
    server_sockfd = listen_port(protoname, server_port);
    while (1) {
        client_len = sizeof(client_address);
        puts("waiting for client");
        client_sockfd = accept(
            server_sockfd,
            (struct sockaddr*)&client_address,
            &client_len
        );
        if (client_sockfd < 0) {
            perror("accept");
            exit(EXIT_FAILURE);
        }
        print_client_address(client_sockfd);
        client_sockf = fdopen(client_sockfd, "r");
        while (1) {
            client_input_len = getline(&client_input, &client_input_getline_size, client_sockf);
            if (client_input_len == -1)
                break;
            puts("received:");
            printf("%s", client_input);
            for (i = 0; i < client_input_len - 1; i++)
                client_input[i]++;
            printf("%s", client_input);
            write(client_sockfd, client_input, client_input_len);
            puts("");
        }
        close(client_sockfd);
    }
    close(server_sockfd);
    return EXIT_SUCCESS;
}
Beispiel #5
0
tcp_client tcp_server::accept()
{
    struct sockaddr_in clientAddr;
    socklen_t cliLen = sizeof(clientAddr);

    int clientSocket = ::accept(_sock_fd, (struct sockaddr *)&clientAddr, &cliLen);
    if (clientSocket == -1 ) {
        throw socket_exception(system::error_code(errno));
    }

    end_point dest, src;
    dest.port = listen_port();
    src.ip = ::inet_ntoa(clientAddr.sin_addr);

    return tcp_client(clientSocket, src, dest);
}
Beispiel #6
0
int main(int argc, char *argv[]){
    if(argc<3){
	error("Usage: ./ethtest client servername port or ./ethtest server port");
    }
    int type=1;
    if(!temp) temp=mymalloc(N,double);
    if(!strcmp(argv[1], "server")){
	int port=strtol(argv[2], NULL, 10);
	if(argc>3){
	    type=strtol(argv[3], NULL, 10);
	}
	listen_port(port, server, 0, NULL);
    }else{
	if(argc<4){
	    error("Usage: ./ethtest client hostname port");
	}
	const char *host=argv[2];
	int port=strtol(argv[3], NULL, 10);
	if(argc>4){
	    type=strtol(argv[4], NULL, 10);
	}
	client(host, port, type);
    }
}
Beispiel #7
0
int main(int argc, char** argv) {

    int port = atoi(argv[1]);

    Server* server = create_server(port);

    listen_port(server);

    Connection* con = accept_connection(server);

    char client_addr[50];
    get_connection_address(con, client_addr);

    printf("CLIENT %s CONNECTED\n", client_addr);

    receive_greet(con);
    send_greet_ack(con);


    //Receive folder anem
    char buffer[MAX_MSG];
    receive_msg(con, buffer);
    send_ack(con);

    char filename[100];
    strcpy(filename, client_addr);
    strcat(filename, buffer);

    int i;
    for(i = 0; filename[i] != '\0'; i++){
        filename[i] = filename[i] == '/'? '.': filename[i];
    }

    char* filebuffer = calloc(sizeof(char), MAX_MSG);
    int filebuffer_size = MAX_MSG;
    filebuffer[0] = '\0';

    int usedbuffer = 0;

    //receive filenames
    while(receive_msg(con, buffer)){
        usedbuffer += strlen(buffer)+1;
        if(usedbuffer > filebuffer_size){
            filebuffer_size *= 2;
            filebuffer = (char*) realloc(filebuffer, sizeof(char)*filebuffer_size*2);
        }
        printf("%s\n", buffer);
        sprintf(filebuffer, "%s%s\n", filebuffer, buffer);
        send_ack(con);
    }

    shutdown_server(server);
    close_connection(con);

    FILE* f = fopen(filename, "w");
    fprintf(f, "%s", filebuffer);
    fclose(f);

    free(filebuffer);


    return 0;
}
Beispiel #8
0
int stdio_wrap(const char *ifname, const char *ofname)
{
	int master __attribute__((cleanup(close_fd_))) = -1,
	    slave __attribute__((cleanup(close_fd_))) = -1,
	    ifd __attribute__((cleanup(close_fd_))) = -1,
	    ofd __attribute__((cleanup(close_fd_))) = -1,
	    listen_sock __attribute__((cleanup(close_fd_))) = -1,
	    sock = -1;
	fd_set rfd_set;
	struct termios termios;
	int nfds = 0;

	if (ifname &&
	    (ifd = mfifo(ifname, O_RDWR)) == -1)
		say_error("can't open `%s': %m", ifname);

	if (ofname &&
	    (ofd = open(ofname, O_WRONLY | O_CREAT | O_TRUNC)) == -1)
		say_error("can't open `%s': %m", ofname);

	if (config.debug_port != 0 &&
	    (listen_sock = listen_port(config.debug_ipaddr, config.debug_port)) == -1)
		say_error("can't create debug socket");

	if (pty_open(&master, &slave) != 0) {
		say_error("can't open pty: %m");

		return -1;
	}

	switch (fork()) {
	case -1:
		say_error("can't fork: %m");

		return -1;
	case 0:
		close(master);

		(void)login_tty(slave);

		(void)setvbuf(stdout, NULL, _IOLBF, 0);
		(void)setvbuf(stderr, NULL, _IONBF, 0);

		return 0;
	default:
		close(slave);

		if (isatty(STDIN_FILENO)) {
			(void)tcgetattr(STDIN_FILENO, &termios);
			cfmakeraw(&termios);
			(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &termios);
		}

		break;
	}

	FD_ZERO(&rfd_set);
	FD_SET(master, &rfd_set);
	FD_SET(STDIN_FILENO, &rfd_set);
	if (ifd != -1)
		FD_SET(ifd, &rfd_set);
	if (listen_sock != -1)
		FD_SET(listen_sock, &rfd_set);
	nfds = MAX(master, MAX(ifd, listen_sock));

	while (true) {
		fd_set rfd_;
		int n;
		char buf[4096];
		int r;
		int i = -1,
		    o[3] = {-1, -1, -1};

		rfd_ = rfd_set;
		n = select(nfds + 1, &rfd_, NULL, NULL, NULL);
		if (n < 0 && errno != EINTR) {
			say_error("select failed: %m");

			_exit(EXIT_FAILURE);
		}
		for (; n > 0; n--) {
			if (listen_sock != -1 &&
			    FD_ISSET(listen_sock, &rfd_)) {
				if ((sock = accept(listen_sock, NULL, NULL)) == -1) {
					say_error("can't accept connection: %m");

					continue;
				}

				// Force client into `raw' mode
				if (telnet_client_force_raw(sock) != 0) {
					say_error("failed to force client into `raw' mode:"
						  " disconnecting");

					close(sock);

					continue;
				}

				FD_CLR(listen_sock, &rfd_set);
				FD_SET(sock, &rfd_set);
				nfds = MAX(nfds, sock);

				continue;
			} else if (sock != -1 &&
				   FD_ISSET(sock, &rfd_)) {
				i = sock;
				o[0] = master;
			} else if (FD_ISSET(STDIN_FILENO, &rfd_)) {
				i = STDIN_FILENO;
				o[0] = master;
			} else if (ifd != -1 &&
				   FD_ISSET(ifd, &rfd_)) {
				i = ifd;
				o[0] = master;
			} else if (FD_ISSET(master, &rfd_)) {
				i = master;
				o[0] = sock;
				o[1] = STDOUT_FILENO;
				o[2] = ofd;
			} else
				// not reacheable
				assert(0);

			FD_CLR(i, &rfd_);

			if ((r = read(i, buf, sizeof(buf))) <= 0) {
				if (r < 0)
					say_error("couldn't read from %d: %m", i);

				if (i == master)
					_exit(EXIT_FAILURE);

				FD_CLR(i, &rfd_set);

				close(i);

				if (i == sock) {
					sock = -1;
					FD_SET(listen_sock, &rfd_set);
				}

				continue;
			}
			if (buf[r - 1] == '\0')
				// telnet client sent <CR><NUL>
				r -= 1;

			for (unsigned j = 0; j < 3; j++) {
				if (o[j] != -1 &&
				    write(o[j], buf, r) == -1 && errno != EBADF) {
					say_error("couldn't write to %d: %m", o[j]);

					if (o[j] == master)
						_exit(EXIT_FAILURE);

					close(o[j]);

					if (o[j] == sock) {
						sock = -1;
						FD_SET(listen_sock, &rfd_set);
					}
				}
			}
		}
	}

	_exit(EXIT_SUCCESS);
}