Ejemplo n.º 1
0
static void handle_client_hello(int fd)
{
    struct msg_header hdr;
    struct peer_info info;

    if (!read_all(fd, &hdr, sizeof hdr)) {
        terminate_client(fd);
        return;
    }
    if (hdr.type != MSG_HELLO || hdr.len != sizeof(info)) {
        fprintf(stderr, "Invalid HELLO packet received from client %d: "
                "type %d, len %d\n", fd, hdr.type, hdr.len);
        terminate_client(fd);
        return;
    }
    if (!read_all(fd, &info, sizeof info)) {
        terminate_client(fd);
        return;
    }
    if (info.version != QREXEC_PROTOCOL_VERSION) {
        fprintf(stderr, "Incompatible client protocol version (remote %d, local %d)\n", info.version, QREXEC_PROTOCOL_VERSION);
        terminate_client(fd);
        return;
    }
    clients[fd].state = CLIENT_CMDLINE;
}
Ejemplo n.º 2
0
static void release_vchan_port(int port, int expected_remote_id)
{
    /* release only if was reserved for connection to given domain */
    if (used_vchan_ports[port-VCHAN_BASE_DATA_PORT] == expected_remote_id) {
        used_vchan_ports[port-VCHAN_BASE_DATA_PORT] = VCHAN_PORT_UNUSED;
        /* notify client if requested - it will clear notification request */
        if (vchan_port_notify_client[port-VCHAN_BASE_DATA_PORT] != VCHAN_PORT_UNUSED)
            terminate_client(vchan_port_notify_client[port-VCHAN_BASE_DATA_PORT]);
    }
}
Ejemplo n.º 3
0
static void handle_cmdline_message_from_client(int fd)
{
    struct msg_header hdr;
    if (!read_all(fd, &hdr, sizeof hdr)) {
        terminate_client(fd);
        return;
    }
    switch (hdr.type) {
        case MSG_EXEC_CMDLINE:
        case MSG_JUST_EXEC:
        case MSG_SERVICE_CONNECT:
            break;
        default:
            terminate_client(fd);
            return;
    }

    if (!handle_cmdline_body_from_client(fd, &hdr))
        // client disconnected while sending cmdline, above call already
        // cleaned up client info
        return;
    clients[fd].state = CLIENT_RUNNING;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[]) {
  u_short port = DEFAULT_PORT;
  char server_name[MAX_LEN_NAME];

  sprintf(server_name, "localhost");

  switch(argc) {
  case 1:
    break;
  case 2:
    sprintf(server_name, "%s", argv[1]);
    break;
  case 3:
    sprintf(server_name, "%s", argv[1]);
    port = (u_short)atoi(argv[2]); //16bitに変換
    break;
  default:
    fprintf(stderr, "Usage: %s [server name] [port number]\n", argv[0]);
    return 1;
  }

  setup_client(server_name, port); //クライアントのセットアップ

  //SDL_AddTimer(5000,SignalHandler,NULL);
  int cond = 1;
  
  // テストだよん
  Mix_PlayMusic(bgm.start, -1);
  
  while(cond) {
      cond = control_requests(); //クライアント中の動作

      TimeFrames();
      SDL_Flip(window);
  }

  SDL_Quit();
  HaikeiFree();
  PlayerFree();
  EnemyFree();
  GameTitleFree();
  PlSeFree();
  terminate_client(); //クライアント
  
  return 0;
}
Ejemplo n.º 5
0
/* handle data received from one of qrexec_client processes */
static void handle_message_from_client(int fd)
{
    char buf[MAX_DATA_CHUNK];

    switch (clients[fd].state) {
        case CLIENT_HELLO:
            handle_client_hello(fd);
            return;
        case CLIENT_CMDLINE:
            handle_cmdline_message_from_client(fd);
            return;
        case CLIENT_RUNNING:
            // expected EOF
            if (read(fd, buf, sizeof(buf)) != 0) {
                fprintf(stderr, "Unexpected data received from client %d\n", fd);
            }
            terminate_client(fd);
            return;
        default:
            fprintf(stderr, "Invalid client state %d\n", clients[fd].state);
            exit(1);
    }
}
Ejemplo n.º 6
0
int main (int argc, char *argv[])
{
    int sd, ci, backlog = MAX_BACKLOG;
    short port = PORTNUM;

    if (argc > 1)
        ncons = atoi (argv[1]);

    /* decrease stack size, non-privilged operation */
    /*    shrink_stack (STACK_SIZE); not needed for parent */

    /* increase maximum number of file descriptors, must be root! */
    /* a few extra, for 0, 1, 2 etc. */
    check_and_set_max_fd (ncons + 8);

    cd = malloc (ncons * sizeof (int));
    isopen = malloc (ncons * sizeof (int));
    pid = malloc (ncons * sizeof (pid_t));
    gethostname (hostname, 128);

    signal (SIGCHLD, cleanupkids);

    /* open an internet tcp stream socket */
    /* socket, setsockopt for reuse, bind, listen; */

    sd = get_socket (port, backlog);

    for (ci = 0; ci < ncons; ci++)
        isopen[ci] = 0;

    for (;;) {

        /* accept new connection only if less than
           the maximum is there */
        while (nopen == ncons) {
            /* reap any children; there may be a
               race condition or signal pileup! */
            cleanupkids (SIGCHLD);
        }

        /*  find the first open one */

        ci = accept_one (sd, isopen, cd, ncons);
        isopen[ci] = 1;
        nopen++;
        printf ("connection accepted (cd[%2d] = %2d), nopen = %2d\n",
                ci, cd[ci], nopen);
        fflush (stdout);

        /* fork off a child to handle the connection */

        pid[ci] = fork ();
        if (pid[ci] < 0)
            DEATH ("Forking");

        if (pid[ci] == 0) {     /* child */

            /* decrease stack size, non-privilged operation */
            shrink_stack (STACK_SIZE);
            /* can set back for kids */
            set_max_fd (16);

            while (!handle_client (cd[ci])) ;
            terminate_client (ci);
        } else {                /* parent */
            printf (" I forked for ci=%d, pid=%d\n", ci, pid[ci]);
            fflush (stdout);
        }
    }

    close (sd);
    free (isopen);
    free (cd);
    exit (EXIT_SUCCESS);
}
Ejemplo n.º 7
0
int main (int argc, char *argv[])
{
    int sd, ci, n, backlog = MAX_BACKLOG, timeout = -1, nfds, nconsp1;
    short port = PORTNUM;
    struct pollfd *pfd, *pfd0;

    if (argc > 1)
        ncons = atoi (argv[1]);
    nconsp1 = ncons + 1;

    cd = malloc ((nconsp1 + 8) * sizeof (int));
    isopen = malloc ((nconsp1 + 8) * sizeof (int));
    ufds = malloc ((nconsp1 + 8) * sizeof (struct pollfd));
    memset (ufds, 0, (nconsp1 + 8) * sizeof (struct pollfd));

    /* increase maximum number of file descriptors, must be root! */

    /* a few extra, for 0, 1, 2 etc. */
    check_and_set_max_fd (nconsp1 + 8);

    gethostname (hostname, 128);

    /* open an internet tcp stream socket */
    /* socket, setsockopt for reuse, bind, listen; */
    sd = get_socket (port, backlog);

    /* for the listening socket */

    pfd0 = &ufds[0];
    pfd0->fd = sd;
    pfd0->events = POLLIN;

    isopen[0] = 0;

    for (ci = 1; ci < nconsp1 + 8; ci++) {
        pfd = &ufds[ci];
        pfd->fd = -1;
        pfd->events = 0;
        isopen[ci] = 0;
    }

    for (;;) {

        /* wait for something to happen on one of the descriptors */

        nfds = 1 + nopen;
        if (sd > fdmax)
            fdmax = sd;         /* to begin with */

        /*        n = poll (ufds, nconsp1, timeout);  */
        n = poll (&ufds[0], fdmax + 1, timeout);

        if (n < 0)
            DEATH ("poll");

        /* if you do a time out you have to deal with n = 0 */

        /* accept new connection only if less than the maximum is there */

        if ((pfd0->revents && POLLIN) && (nopen < ncons)) {
            isopen[0] = 1;

            /*  find the first open one */

            ci = accept_one (sd, isopen, cd, ncons);
            isopen[ci] = 1;
            pfd = &ufds[ci];
            pfd->fd = cd[ci];
            pfd->events = POLLIN;
            nopen++;
            if (cd[ci] > fdmax)
                fdmax = cd[ci];
            printf
            ("connection accepted (cd[%2d] = %2d), nopen = %2d\n",
             ci, cd[ci], nopen);

        }
        /* service existing connections */

        for (ci = 1; ci < nconsp1; ci++) {
            pfd = &ufds[ci];
            if (isopen[ci] && (pfd->revents && POLLIN)) {
                if (handle_client (cd[ci]))
                    terminate_client (ci);
            }
            fflush (stdout);
        }
    }
    close (sd);
    free (cd);
    free (isopen);
    exit (EXIT_SUCCESS);
}
Ejemplo n.º 8
0
int main (int argc, char *argv[])
{
    int sd, ci, n, backlog = MAX_BACKLOG;
    short port = PORTNUM;

    if (argc > 1)
        ncons = atoi (argv[1]);
    if (ncons > FD_SETSIZE)
        DEATH ("Can't have more than 1024 for select(), and ulimit -s"
               "  must be a bit larger")

            cd = malloc (ncons * sizeof (int));
    isopen = malloc (ncons * sizeof (int));
    gethostname (hostname, 128);

    /* open an internet tcp stream socket */
    /* socket, setsockopt for reuse, bind, listen; */

    sd = get_socket (port, backlog);

    for (ci = 0; ci < ncons; ci++)
        isopen[ci] = 0;

    FD_ZERO (&fdset);
    FD_SET (sd, &fdset);

    for (;;) {

        /* wait for something to happen on one of the descriptors */

        testset = fdset;
        if (sd > fdmax)
            fdmax = sd;         /* to begin with */
        n = select (fdmax + 1, &testset, NULL, NULL, NULL);

        if (n < 0)
            DEATH ("select");

        /* if you do a time out you have to deal with n = 0 */

        /* accept new connection only if less than the maximum is there */

        if (FD_ISSET (sd, &testset) && (nopen < ncons)) {

            /*  find the first open one */

            ci = accept_one (sd, isopen, cd, ncons);
            isopen[ci] = 1;
            FD_SET (cd[ci], &fdset);
            nopen++;
            if (cd[ci] > fdmax)
                fdmax = cd[ci];
            printf
                ("connection accepted (cd[%2d] = %2d), nopen = %2d\n",
                 ci, cd[ci], nopen);

        }
        /* service existing connections */

        for (ci = 0; ci < ncons; ci++) {
            if (isopen[ci] && FD_ISSET (cd[ci], &testset))
                if (handle_client (cd[ci]))
                    terminate_client (ci);
        }
    }

    close (sd);
    free (cd);
    free (isopen);
    exit (EXIT_SUCCESS);
}
Ejemplo n.º 9
0
static int handle_cmdline_body_from_client(int fd, struct msg_header *hdr)
{
    struct exec_params params;
    int len = hdr->len-sizeof(params);
    char buf[len];
    int use_default_user = 0;
    int i;

    if (!read_all(fd, &params, sizeof(params))) {
        terminate_client(fd);
        return 0;
    }
    if (!read_all(fd, buf, len)) {
        terminate_client(fd);
        return 0;
    }

    if (hdr->type == MSG_SERVICE_CONNECT) {
        /* if the service was accepted, do not send spurious
         * MSG_SERVICE_REFUSED when service process itself exit with non-zero
         * code */
        for (i = 0; i <= policy_pending_max; i++) {
            if (policy_pending[i].pid &&
                    strncmp(policy_pending[i].params.ident, buf, len) == 0) {
                policy_pending[i].pid = 0;
                while (policy_pending_max > 0 &&
                        policy_pending[policy_pending_max].pid == 0)
                    policy_pending_max--;
                break;
            }
        }
    }

    if (!params.connect_port) {
        struct exec_params client_params;
        /* allocate port and send it to the client */
        params.connect_port = allocate_vchan_port(params.connect_domain);
        if (params.connect_port <= 0) {
            fprintf(stderr, "Failed to allocate new vchan port, too many clients?\n");
            terminate_client(fd);
            return 0;
        }
        /* notify the client when this connection got terminated */
        vchan_port_notify_client[params.connect_port-VCHAN_BASE_DATA_PORT] = fd;
        client_params.connect_port = params.connect_port;
        client_params.connect_domain = remote_domain_id;
        hdr->len = sizeof(client_params);
        if (!write_all(fd, hdr, sizeof(*hdr))) {
            terminate_client(fd);
            release_vchan_port(params.connect_port, params.connect_domain);
            return 0;
        }
        if (!write_all(fd, &client_params, sizeof(client_params))) {
            terminate_client(fd);
            release_vchan_port(params.connect_port, params.connect_domain);
            return 0;
        }
        /* restore original len value */
        hdr->len = len+sizeof(params);
    } else {
        assert(params.connect_port >= VCHAN_BASE_DATA_PORT);
        assert(params.connect_port < VCHAN_BASE_DATA_PORT+MAX_CLIENTS);
    }

    if (!strncmp(buf, default_user_keyword, default_user_keyword_len_without_colon+1)) {
        use_default_user = 1;
        hdr->len -= default_user_keyword_len_without_colon;
        hdr->len += strlen(default_user);
    }
    if (libvchan_send(vchan, hdr, sizeof(*hdr)) < 0)
        handle_vchan_error("send");
    if (libvchan_send(vchan, &params, sizeof(params)) < 0)
        handle_vchan_error("send params");
    if (use_default_user) {
        if (libvchan_send(vchan, default_user, strlen(default_user)) < 0)
            handle_vchan_error("send default_user");
        if (libvchan_send(vchan, buf+default_user_keyword_len_without_colon,
                    len-default_user_keyword_len_without_colon) < 0)
            handle_vchan_error("send buf");
    } else
        if (libvchan_send(vchan, buf, len) < 0)
            handle_vchan_error("send buf");
    return 1;
}