File: fcup_request.h

package info (click to toggle)
uxplay 1.73.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,392 kB
  • sloc: ansic: 24,133; cpp: 2,956; python: 705; makefile: 10
file content (113 lines) | stat: -rw-r--r-- 5,068 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (c) 2022 fduncanh
 * All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 */

/* this file is part of raop.c via http_handlers.h and should not be included in any other file */


//produces the fcup request plist in xml format as a null-terminated string
char *create_fcup_request(const char *url, int request_id, const char *client_session_id, int *datalen) {
    char *plist_xml = NULL;
    /* values taken from apsdk-public;  */
    /* these seem to be arbitrary choices */
    const int sessionID = 1;
    const int FCUP_Response_ClientInfo = 1;
    const int FCUP_Response_ClientRef = 40030004;

    /* taken from a working AppleTV? */
    const char User_Agent[] = "AppleCoreMedia/1.0.0.11B554a (Apple TV; U; CPU OS 7_0_4 like Mac OS X; en_us";
    
    plist_t req_root_node = plist_new_dict();
    
    plist_t session_id_node = plist_new_uint((int64_t) sessionID);   
    plist_dict_set_item(req_root_node, "sessionID", session_id_node);
    plist_t type_node = plist_new_string("unhandledURLRequest");
    plist_dict_set_item(req_root_node, "type", type_node);

    plist_t fcup_request_node = plist_new_dict();

    plist_t client_info_node = plist_new_uint(FCUP_Response_ClientInfo);
    plist_dict_set_item(fcup_request_node, "FCUP_Response_ClientInfo", client_info_node);
    plist_t client_ref_node = plist_new_uint((int64_t) FCUP_Response_ClientRef);
    plist_dict_set_item(fcup_request_node, "FCUP_Response_ClientRef", client_ref_node);
    plist_t request_id_node = plist_new_uint((int64_t) request_id);
    plist_dict_set_item(fcup_request_node, "FCUP_Response_RequestID", request_id_node);
    plist_t url_node = plist_new_string(url);
    plist_dict_set_item(fcup_request_node, "FCUP_Response_URL", url_node);
    plist_t session_id1_node = plist_new_uint((int64_t) sessionID);    
    plist_dict_set_item(fcup_request_node, "sessionID", session_id1_node);
				    
    plist_t fcup_response_header_node = plist_new_dict();
    plist_t playback_session_id_node = plist_new_string(client_session_id);       
    plist_dict_set_item(fcup_response_header_node, "X-Playback-Session-Id", playback_session_id_node);
    plist_t user_agent_node = plist_new_string(User_Agent);
    plist_dict_set_item(fcup_response_header_node, "User-Agent", user_agent_node);

    plist_dict_set_item(fcup_request_node, "FCUP_Response_Headers", fcup_response_header_node);
    plist_dict_set_item(req_root_node, "request", fcup_request_node);
    
    uint32_t uint_val = 0;
    
    plist_to_xml(req_root_node, &plist_xml, &uint_val);
    *datalen = (int) uint_val;
    plist_free(req_root_node);
    assert(plist_xml[*datalen] == '\0');
    return plist_xml; //needs to be freed after use
}

int fcup_request(void *conn_opaque, const char *media_url, const char *client_session_id, int request_id) {

    raop_conn_t *conn = (raop_conn_t *) conn_opaque;
    raop_t *raop = conn->raop;
    int datalen = 0;
    int requestlen = 0;

    int socket_fd = httpd_get_connection_socket_by_type(raop->httpd, CONNECTION_TYPE_PTTH, 1);
    
    logger_log(raop->logger, LOGGER_DEBUG, "fcup_request send socket = %d", socket_fd);
    
    /* create xml plist request data */
    char *plist_xml = create_fcup_request(media_url, request_id, client_session_id, &datalen);

    /* use http_response tools for creating the reverse http request */
    http_response_t *request = http_response_create();
    http_response_reverse_request_init(request, "POST", "/event", "HTTP/1.1");
    http_response_add_header(request, "X-Apple-Session-ID", client_session_id);
    http_response_add_header(request, "Content-Type", "text/x-apple-plist+xml");
    http_response_finish(request, plist_xml, datalen);

    free(plist_xml);

    const char *http_request = http_response_get_data(request, &requestlen); 
    int send_len = send(socket_fd, http_request, requestlen, 0);
    if (send_len < 0) {
        int sock_err = SOCKET_GET_ERROR();
	logger_log(raop->logger, LOGGER_ERR, "fcup_request: send  error %d:%s\n",
		 sock_err, SOCKET_ERROR_STRING(sock_err));
	http_response_destroy(request);
        /* shut down connection? */
        return -1;
    }

    if (logger_get_level(raop->logger) >= LOGGER_DEBUG) {
      char *request_str =  utils_data_to_text(http_request, requestlen);
        logger_log(raop->logger, LOGGER_DEBUG, "\n%s", request_str);
        free (request_str);
    }
    http_response_destroy(request);
    logger_log(raop->logger, LOGGER_DEBUG,"fcup_request: send sent Request of %d bytes from socket %d\n",
               send_len, socket_fd);
    return 0;
}