Example #1
0
/* Display the console Web page */
void show_web_ui(int csock, char *request)
{
    char *data = NULL;
    int data_size = 0;

    if(!request) {
        glog("NULL request received by Web UI",LOG_ERROR_TYPE);
        return;
    }

    /* Point data at the data blob associated with the requested URL */
    if(strstr(request,BODY_BG_PNG_PATH)) {
        data = BODY_BG_PNG;
        data_size = BODY_BG_PNG_SIZE;
    } else if(strstr(request,HEADER_PNG_PATH)) {
        data = HEADER_PNG;
        data_size = HEADER_PNG_SIZE;
    } else if(strstr(request,HEADER_BG_PNG_PATH)) {
        data = HEADER_BG_PNG;
        data_size = HEADER_PNG_SIZE;
    } else if(strstr(request,FOOTER_BG_PNG_PATH)) {
        data = FOOTER_BG_PNG;
        data_size = FOOTER_BG_PNG_SIZE;
    } else if((strlen(request) == INDEX_PATH_SIZE) && (memcmp(request,INDEX_PATH,INDEX_PATH_SIZE) == 0)) {
        data = PROXY_HTML;
        data_size = PROXY_HTML_SIZE;
    }

    if(data) {
        /* Write data to browser */
        if(send_http_response(csock,data,data_size) == SOCK_FAIL) {
            glog("Proxy server failed to write image data to client browser",LOG_ERROR_TYPE);
            return;
        }
    } else {
        /* Write client list to browser */
        print_client_list(csock);
    }

    return;
}
int parse_shell()
{
  char shell_input[MAX_LENGTH];
  char message[MAX_LENGTH];
  static bool is_client_connected = false;
  int ret = 0;
  static int server_sock;

  /*
   * Get the input
   */
  fgets(shell_input, MAX_LENGTH, stdin);
  if(strlen(shell_input) <= 1)
    return 0;
  shell_input[strlen(shell_input)-1] = '\0';
  strcpy(message, shell_input);

  /*
   * Tokenize the strings
   */
  char *arg;
  int argc = 0;
  char argv[4][256];
  char *temp;
  char *command = strtok_r(shell_input, " ", &temp);
  //TODO reorder all commands!!
  // ----------------Commands common for server/client ------------
  if(!strcmp("AUTHOR", command))
  {
    print_success(1, command);
    LOG("I, g8, have read and understood the course academic integrity policy.\n");
  }
  else if(!strcmp("EXIT", command))
  {
    return 1;
  }
  // -------------------Common commands ---------------------------
  else if(!is_server)
  {
    char arg_copy[MAX_LENGTH];
    if(temp!=NULL)
    {
      strcpy(arg_copy, temp);
      for(arg = strtok_r(NULL, " ", &temp); arg && argc<4; arg = strtok_r(NULL, " ", &temp))
      {
        strcpy(argv[argc], arg);
        argc++;
      }
    }
    if(!strcmp("LOGIN", command))
    {
      if(!is_server && !(is_client_connected || argc!=2) && 
          validate_ip(argv[0]) && validate_port(argv[1]))
      {
        int newfd = client_connect(argv[0], argv[1]);
        if(newfd<=1)
        {
          print_success(0, command);
        }
        else
        {
          //TODO client connects to unknown port?
          is_client_connected = true;
          add_fd(newfd);
          client_identify(newfd);
          server_sock = newfd;
          print_success(1, command);
        }
      }
      else
        print_success(0, command);
    }
    else if(is_client_connected)
    {
      if(!strcmp("PORT", command))
      {
        print_success(1, command);
        LOG("PORT:%d\n", get_listening_port());
      }
      else if(!strcmp("IP", command))
      {
        print_success(1, command);
        get_ip();
      }
      else if(!strcmp("SEND", command))
      {
        if(!is_client_connected)
          print_success(0,command);
        else
          print_success(client_send_msg(server_sock, arg_copy), command);
      }
      else if(!strcmp("SENDFILE", command))
      {
        if(argc == 2 && validate_ip(argv[0]))
        {
          print_success(1, command);
          client_send_file(argv[0], argv[1]);
        }
        else
          print_success(0, command);
      }
      else if(!strcmp("LOGOUT", command))
      {
        if(is_client_connected)
        {
          //close(server_sock);
          //clear_fd(server_sock);
          client_send(server_sock, command);
          is_client_connected  = false;
          //server_sock = -1;
          print_success(1, command);
        }
        else
          print_success(0, command);
      }
      else if(!strcmp("LIST", command))
      {
        print_success(1, command);
        print_client_list();
      }
      else if(!strcmp("REFRESH", command))
      {
        client_send(server_sock, command);
        print_success(1, command);
      }
      else if(!strcmp("BROADCAST", command))
      {
        if(argc)
        {
          client_send(server_sock, message);
          print_success(1, command);
        }
        else print_success(0, command);
      }
      else if(!strcmp("BLOCK", command))
      {
        if(argc == 1 && verify_ip(argv[0])
            && !is_client_blocked(argv[0]))
        {
          //TODO CHECK IP in list!! done!
          print_success(1, command);
          add_to_block_list(argv[0]);
          client_send(server_sock, message);
        }
        else
        {
          print_success(0, command);
        }
      }
      else if(!strcmp("UNBLOCK", command))
      {
        if(argc == 1 && verify_ip(argv[0])
            && is_client_blocked(argv[0]))
        {
          print_success(1, command);
          remove_from_block_list(argv[0]);
          client_send(server_sock, message);
        }
        else
          print_success(0, command);
      }
      else
        print_success(0, command);
    }
    else
      print_success(0, command);
  }
  else
  {
    if(!strcmp("STATISTICS", command))
    {
      print_success(1, command);
      print_stats();
    }
    else if(!strcmp("BLOCKED", command))
    {
      if(temp == NULL)
        print_success(0, command);
      else
      {
        char *ip = strtok_r(NULL, " ", &temp);
        if(validate_ip(ip) && find_client_by_ip(ip) !=NULL)
        {
          print_success(1, command);
          print_blocked_clients(ip);
        }
        else
          print_success(0, command);
      }
    }
    else if(!strcmp("LIST", command))
    {
      print_success(1, command);
      print_connected_client_list();
    }
    else if(!strcmp("PORT", command))
    {
      print_success(1, command);
      LOG("PORT:%d\n", get_listening_port());
    }
    else if(!strcmp("IP", command))
    {
      print_success(1, command);
      get_ip();
    }
    else
      print_success(0, command);
  }

  LOG("[%s:END]\n", command);
  return 0;
}