Beispiel #1
0
status_t handle_cdup_command(user_session_t *session, string_t *args, size_t len)
{
	status_t error;
	if (!session->logged_in)
	{
		error = send_530(session);
		goto exit0;
	}

	string_t s;
	string_initialize(&s);
	string_assign_from_char_array(&s, session->directory);
	string_concatenate_char_array_with_size(&s, "/..", 3);

	char *resolved_dir = realpath(string_c_str(&s), NULL);
	if (resolved_dir == NULL || !is_directory(resolved_dir))
	{
		error = send_550(session);
		goto exit1;
	}

	free(session->directory);
	session->directory = resolved_dir;

	error = send_200(session);
	if (error)
	{
		goto exit1;
	}

exit1:
	string_uninitialize(&s);
exit0:
	return error;
}
Beispiel #2
0
status_t handle_port_command(user_session_t *session, string_t *args, size_t len)
{
	status_t error;
	if (!session->server->port_enabled)
	{
		//This command can't actually send back a 'not implemented' response, so
		//approximate it with an "unrecognized command" response
		error = send_500(session);
		goto exit0;
	}

	if (!session->logged_in)
	{
		error = send_530(session);
		goto exit0;
	}

	size_t ip_len;
	string_t *split = string_split(args + 1, ',', &ip_len);

	string_t host;
	string_initialize(&host);
	uint16_t port;
	error = parse_ip_and_port(split, ip_len, &host, &port);
	if (error)
	{
		error = send_501(session);
		goto exit1;
	}

	error = make_connection(&session->data_sock, string_c_str(&host), port);
	if (error)
	{
		send_response(session->command_sock, SERVICE_NOT_AVAILABLE, "Could not connect to port", session->server->log, 0);
		goto exit1;
	}

	error = send_200(session);
	if (error)
	{
		goto exit1;
	}

exit1:
	string_uninitialize(&host);
exit0:
	return error;
}
Beispiel #3
0
void osd_http_send(struct mg_connection *nc, struct http_message *hm)
{
    int last_hashs[NUM_BLOCKS];
    int i;
    char hex[9];
    int send_count;
    int num_blocks;
    long long end_time;
    uint32_t x = 0xffffffff; // end of data

    s_request_time = get_current_time();

    if (s_osd_data_ready == false) {
        //print_log("s_osd_data not ready");
        send_200(nc);
        return;
    }

    hex[8] = '\0';
    for (i = 0; i < (hm->body.len - 6) / 8; i++) {
        strncpy(hex, (hm->body.p + 6) + i * 8, 8);
        last_hashs[i] = (uint32_t)strtol(hex, NULL, 16);
        //print_log("hex = %s", hex);
    }

    pthread_mutex_lock(&s_mutex);

    num_blocks = ntohs(s_osd_data.frame_width) * ntohs(s_osd_data.frame_height)
                 / ntohs(s_osd_data.block_width) / ntohs(s_osd_data.block_height);
    //print_log("num_blocks = %d", num_blocks);

    send_count = 0;
    for (i = 0; i < num_blocks; i++) {
        if (ntohl(s_osd_data.blocks[i].hash) != last_hashs[i]) {
            send_count++;
        }
    }

    mg_printf(nc, "HTTP/1.1 200 OK\r\n"
                  "Access-Control-Allow-Origin: *\r\n"
                  "Access-Control-Allow-Headers: X-Requested-With\r\n"
                  "Content-Length: %d\r\n"
                  "Content-Type: text/plain; charset=x-user-defined\r\n"
                  "\r\n", 8 + send_count * sizeof(s_osd_data.blocks[0]) + 4);

    mg_send(nc, (const void *)&s_osd_data, 8);

    send_count = 0;
    for (i = 0; i < num_blocks; i++) {
        if (ntohl(s_osd_data.blocks[i].hash) != last_hashs[i]) {
            mg_send(nc, &s_osd_data.blocks[i], sizeof(s_osd_data.blocks[i]));
            send_count++;
        }
    }

    pthread_mutex_unlock(&s_mutex);

    mg_send(nc, &x, 4);

    end_time = get_current_time();
    if (send_count != 0) {
        print_log("send_count = %d, time = %lld",
                  send_count, end_time - s_request_time);
    }
}