Пример #1
0
int main(int argc , char *argv[])
{
    Client* client = mymalloc(sizeof(Client));
    init_client(client);
    client->fdmax=STDIN_FILENO;
    FD_SET(STDIN_FILENO, &(client->fd_read_set));

    while(TRUE) {
        client->fd_read_set_select = client->fd_read_set;
        client->fd_write_set_select = client->fd_write_set;
        select(client->fdmax+1, &(client->fd_read_set_select),
               &(client->fd_write_set_select),
               NULL, NULL);
        if (FD_ISSET(STDIN_FILENO, &(client->fd_read_set_select)))
            handle_std_input(client);
        else if(FD_ISSET(client->listening_fd, &(client->fd_read_set_select)))
            handle_new_client(client);
        else if(FD_ISSET(client->maintainer_fd, &(client->fd_read_set_select)))
            read_server_msg(client);
        else if(FD_ISSET(client->maintainer_fd, &(client->fd_write_set_select)))
            write_server_msg(client);
        else
            for(int i=1; i<=client->fdmax; ++i)
                if(FD_ISSET(i, &(client->fd_read_set_select)))
                    read_client_msg(client, i);
                else if(FD_ISSET(i, &(client->fd_write_set_select)))
                    write_client_msg(client, i);
    }
    myfree(client);
    return 0;
}
Пример #2
0
int run_client(int client, struct sockaddr_in *client_addr) {
  char incoming_msg[1024];
  char buf[4096];
  unsigned long long canary_val = 0;
  char *msg = "Welcome %s, the stack cookie is %llx.\nPlease, enter you message to be echoed.\n> ";
  int ret;

  asm("mov %%fs:0x28, %0" :"=r"(canary_val));
  snprintf(buf, 4096, msg, inet_ntoa(client_addr->sin_addr), canary_val);
  send(client, buf, strlen(buf), 0);
  memset(incoming_msg, 0, sizeof(incoming_msg));
  while(strncmp(incoming_msg, "QUIT", 4)) {
    int incoming_msg_len = read_client_msg(client, incoming_msg, 1024);
    if (incoming_msg_len == 0) break;
    ret = send(client, incoming_msg, incoming_msg_len, 0);
    if (ret < 0) {
      perror("send()");
      return -1;
    }
  }
  return 0;
}