/* generate and write out an appropriate response for the http request */
int generate_response(int sd, char *http_req, int http_req_len)
{
	enum http_req_type request_type = decode_http_request(http_req, http_req_len);

	switch(request_type) {
	case HTTP_GET:
		return do_http_get(sd, http_req, http_req_len);
	case HTTP_POST:
		return do_http_post(sd, http_req, http_req_len);
	default:
		return do_404(sd, http_req, http_req_len);
	}
}
/* 	this assumes that tcp_sndbuf is high enough to send atleast 1 packet */
int generate_response(struct tcp_pcb *pcb, char *http_req, int http_req_len)
{
	enum http_req_type request_type = decode_http_request(http_req, http_req_len);

	switch(request_type) {
	case HTTP_GET:
		return do_http_get(pcb, http_req, http_req_len);
	case HTTP_POST:
		return do_http_post(pcb, http_req, http_req_len);
	default:
		xil_printf("request_type != GET|POST\r\n");
		dump_payload(http_req, http_req_len);
		return do_404(pcb, http_req, http_req_len);
	}
}
示例#3
0
/** Send a resolve request for <b>hostname</b> to the Tor listening on
 * <b>sockshost</b>:<b>socksport</b>.  Store the resulting IPv4
 * address (in host order) into *<b>result_addr</b>.
 */
static int
do_connect(const char *hostname, const char *filename, uint32_t sockshost, uint16_t socksport,
           int reverse, int version, size_t expected_bytes,
           uint32_t *result_addr, char **result_hostname)
{

  int s;
  struct sockaddr_in socksaddr;
  char *req = NULL;
  int len = 0;
  int retval;

  assert(hostname);
  assert(filename);
  assert(result_addr);
  assert(version == 4 || version == 5);

  *result_addr = 0;
  *result_hostname = NULL;

  // Get time that connection was started
  if (gettimeofday(&starttime, NULL)) {
    perror("getting starttime");
    return -1;
  }

  // Create the socket for connecting to SOCKS server
  s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  if (s<0) {
    perror("creating socket");
    return -1;
  }
  // Get time that socket was created
  if (gettimeofday(&sockettime, NULL)) {
    perror("getting sockettime");
    return -1;
  }

  // Connect to the SOCKS server
  memset(&socksaddr, 0, sizeof(socksaddr));
  socksaddr.sin_family = AF_INET;
  socksaddr.sin_port = htons(socksport);
  socksaddr.sin_addr.s_addr = htonl(sockshost);
  if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
    perror("connecting to SOCKS host");
    return -1;
  }
  // Get time that socket was connected
  if (gettimeofday(&connecttime, NULL)) {
    perror("getting connecttime");
    return -1;
  }

  // Negotiate authentication method for SOCKS 5
  if (version == 5) {
    retval = do_socks5_negotiate(s);
    if (retval)
      return retval;
  }  
  // Get time that negotiation was completed
  if (gettimeofday(&negotiatetime, NULL)) {
    perror("getting negotiatetime");
    return -1;
  }

  if ((len = build_socks_connect_request(&req, "", hostname, reverse,
                                         version))<0) {
    fprintf(stderr, "error generating SOCKS request: %d\n", len);
    return -1;
  }
  if (write_all(s, req, len, 1) != len) {
    perror("sending SOCKS request");
    free(req);
    return -1;
  }
  free(req);
  // Get time that request was sent
  if (gettimeofday(&requesttime, NULL)) {
    perror("getting requesttime");
    return -1;
  }

  if (version == 4) {
    char reply_buf[RESPONSE_LEN_4];
    if (read_all(s, reply_buf, RESPONSE_LEN_4, 1) != RESPONSE_LEN_4) {
      fprintf(stderr, "Error reading SOCKS4 response.\n");
      return -1;
    }
    if (parse_socks4a_connect_response(reply_buf, RESPONSE_LEN_4,
                                       result_addr)<0){
      return -1;
    }
  } else {
    char reply_buf[RESPONSE_LEN_5];
    if (read_all(s, reply_buf, RESPONSE_LEN_5, 1) != RESPONSE_LEN_5) {
      fprintf(stderr, "Error reading SOCKS5 response\n");
      return -1;
    }
    if (parse_socks5_connect_response(reply_buf, RESPONSE_LEN_5, s,
                                      result_addr, result_hostname)<0){
      return -1;
    }
  }
  // Get time that response was received
  if (gettimeofday(&responsetime, NULL)) {
    perror("getting responsetime");
    return -1;
  }

  /*
  char reply_buf[1];
  while (read_all(s, reply_buf, 1, 1) != 0) {
    fprintf(stderr,"Extra data: 0x%x\n", reply_buf[0]);
  } 
  */
  
  // Request a file using HTTP
  do_http_get(s, filename, hostname, expected_bytes, &read_bytes, &write_bytes,
             &datarequesttime, &dataresponsetime, &datacompletetime,
             dataperctime);


  didtimeout = 0;

  // Output status information
  output_status_information();

  return 0;
}