2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
33 /* Actually 345 bytes for 2048-bit key */
34 #define MAX_SIGNATURE_LEN 512
36 /* Let's just decide on some length */
37 #define MAX_PASSWORD_LEN 64
39 /* MD5 as hex fits here */
40 #define MAX_NONCE_LEN 32
43 /* Callbacks for audio */
44 raop_callbacks_t callbacks
;
49 /* HTTP daemon and RSA key */
53 /* Hardware address information */
54 unsigned char hwaddr
[MAX_HWADDR_LEN
];
57 /* Password information */
58 char password
[MAX_PASSWORD_LEN
+1];
68 unsigned char *remote
;
71 char nonce
[MAX_NONCE_LEN
+1];
73 typedef struct raop_conn_s raop_conn_t
;
76 conn_init(void *opaque
, unsigned char *local
, int locallen
, unsigned char *remote
, int remotelen
)
80 conn
= calloc(1, sizeof(raop_conn_t
));
85 conn
->raop_rtp
= NULL
;
88 logger_log(conn
->raop
->logger
, LOGGER_INFO
,
90 local
[0], local
[1], local
[2], local
[3]);
91 } else if (locallen
== 16) {
92 logger_log(conn
->raop
->logger
, LOGGER_INFO
,
93 "Local: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
94 local
[0], local
[1], local
[2], local
[3], local
[4], local
[5], local
[6], local
[7],
95 local
[8], local
[9], local
[10], local
[11], local
[12], local
[13], local
[14], local
[15]);
98 logger_log(conn
->raop
->logger
, LOGGER_INFO
,
99 "Remote: %d.%d.%d.%d",
100 remote
[0], remote
[1], remote
[2], remote
[3]);
101 } else if (remotelen
== 16) {
102 logger_log(conn
->raop
->logger
, LOGGER_INFO
,
103 "Remote: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
104 remote
[0], remote
[1], remote
[2], remote
[3], remote
[4], remote
[5], remote
[6], remote
[7],
105 remote
[8], remote
[9], remote
[10], remote
[11], remote
[12], remote
[13], remote
[14], remote
[15]);
108 conn
->local
= malloc(locallen
);
110 memcpy(conn
->local
, local
, locallen
);
112 conn
->remote
= malloc(remotelen
);
113 assert(conn
->remote
);
114 memcpy(conn
->remote
, remote
, remotelen
);
116 conn
->locallen
= locallen
;
117 conn
->remotelen
= remotelen
;
119 digest_generate_nonce(conn
->nonce
, sizeof(conn
->nonce
));
124 conn_request(void *ptr
, http_request_t
*request
, http_response_t
**response
)
126 raop_conn_t
*conn
= ptr
;
127 raop_t
*raop
= conn
->raop
;
129 http_response_t
*res
;
132 const char *challenge
;
133 int require_auth
= 0;
135 method
= http_request_get_method(request
);
136 cseq
= http_request_get_header(request
, "CSeq");
137 if (!method
|| !cseq
) {
141 res
= http_response_init("RTSP/1.0", 200, "OK");
142 if (strlen(raop
->password
)) {
143 const char *authorization
;
145 authorization
= http_request_get_header(request
, "Authorization");
147 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "Our nonce: %s", conn
->nonce
);
148 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "Authorization: %s", authorization
);
150 if (!digest_is_valid("AppleTV", raop
->password
, conn
->nonce
, method
, http_request_get_url(request
), authorization
)) {
154 /* Allocate the authenticate string */
155 authstrlen
= sizeof("Digest realm=\"AppleTV\", nonce=\"\"") + sizeof(conn
->nonce
) + 1;
156 authstr
= malloc(authstrlen
);
158 /* Concatenate the authenticate string */
159 memset(authstr
, 0, authstrlen
);
160 strcat(authstr
, "Digest realm=\"AppleTV\", nonce=\"");
161 strcat(authstr
, conn
->nonce
);
162 strcat(authstr
, "\"");
164 /* Construct a new response */
166 http_response_destroy(res
);
167 res
= http_response_init("RTSP/1.0", 401, "Unauthorized");
168 http_response_add_header(res
, "WWW-Authenticate", authstr
);
171 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "AUTHENTICATION SUCCESS!");
175 http_response_add_header(res
, "CSeq", cseq
);
176 http_response_add_header(res
, "Apple-Jack-Status", "connected; type=analog");
178 challenge
= http_request_get_header(request
, "Apple-Challenge");
179 if (!require_auth
&& challenge
) {
180 char signature
[MAX_SIGNATURE_LEN
];
182 memset(signature
, 0, sizeof(signature
));
183 rsakey_sign(raop
->rsakey
, signature
, sizeof(signature
), challenge
,
184 conn
->local
, conn
->locallen
, raop
->hwaddr
, raop
->hwaddrlen
);
185 http_response_add_header(res
, "Apple-Response", signature
);
187 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "Got challenge: %s", challenge
);
188 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "Got response: %s", signature
);
192 /* Do nothing in case of authentication request */
193 } else if (!strcmp(method
, "OPTIONS")) {
194 http_response_add_header(res
, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER");
195 } else if (!strcmp(method
, "ANNOUNCE")) {
199 unsigned char aeskey
[16];
200 unsigned char aesiv
[16];
201 int aeskeylen
, aesivlen
;
203 data
= http_request_get_data(request
, &datalen
);
206 const char *remotestr
, *rtpmapstr
, *fmtpstr
, *aeskeystr
, *aesivstr
;
208 sdp
= sdp_init(data
, datalen
);
209 remotestr
= sdp_get_connection(sdp
);
210 rtpmapstr
= sdp_get_rtpmap(sdp
);
211 fmtpstr
= sdp_get_fmtp(sdp
);
212 aeskeystr
= sdp_get_rsaaeskey(sdp
);
213 aesivstr
= sdp_get_aesiv(sdp
);
215 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "connection: %s", remotestr
);
216 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "rtpmap: %s", rtpmapstr
);
217 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "fmtp: %s", fmtpstr
);
218 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "rsaaeskey: %s", aeskeystr
);
219 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "aesiv: %s", aesivstr
);
221 aeskeylen
= rsakey_decrypt(raop
->rsakey
, aeskey
, sizeof(aeskey
), aeskeystr
);
222 aesivlen
= rsakey_parseiv(raop
->rsakey
, aesiv
, sizeof(aesiv
), aesivstr
);
223 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "aeskeylen: %d", aeskeylen
);
224 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "aesivlen: %d", aesivlen
);
226 if (conn
->raop_rtp
) {
227 /* This should never happen */
228 raop_rtp_destroy(conn
->raop_rtp
);
229 conn
->raop_rtp
= NULL
;
231 conn
->raop_rtp
= raop_rtp_init(raop
->logger
, &raop
->callbacks
, remotestr
, rtpmapstr
, fmtpstr
, aeskey
, aesiv
);
232 if (!conn
->raop_rtp
) {
233 logger_log(conn
->raop
->logger
, LOGGER_ERR
, "Error initializing the audio decoder");
234 http_response_set_disconnect(res
, 1);
238 } else if (!strcmp(method
, "SETUP")) {
239 unsigned short remote_cport
=0, remote_tport
=0;
240 unsigned short cport
=0, tport
=0, dport
=0;
241 const char *transport
;
245 transport
= http_request_get_header(request
, "Transport");
248 logger_log(conn
->raop
->logger
, LOGGER_INFO
, "Transport: %s", transport
);
249 use_udp
= strncmp(transport
, "RTP/AVP/TCP", 11);
251 char *original
, *current
, *tmpstr
;
253 current
= original
= strdup(transport
);
255 while ((tmpstr
= utils_strsep(¤t
, ";")) != NULL
) {
256 unsigned short value
;
259 ret
= sscanf(tmpstr
, "control_port=%hu", &value
);
261 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "Found remote control port: %hu", value
);
262 remote_cport
= value
;
264 ret
= sscanf(tmpstr
, "timing_port=%hu", &value
);
266 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "Found remote timing port: %hu", value
);
267 remote_tport
= value
;
273 if (conn
->raop_rtp
) {
274 raop_rtp_start(conn
->raop_rtp
, use_udp
, remote_cport
, remote_tport
, &cport
, &tport
, &dport
);
276 logger_log(conn
->raop
->logger
, LOGGER_ERR
, "RAOP not initialized at SETUP, playing will fail!");
277 http_response_set_disconnect(res
, 1);
280 memset(buffer
, 0, sizeof(buffer
));
282 snprintf(buffer
, sizeof(buffer
)-1,
283 "RTP/AVP/UDP;unicast;mode=record;timing_port=%hu;events;control_port=%hu;server_port=%hu",
284 tport
, cport
, dport
);
286 snprintf(buffer
, sizeof(buffer
)-1,
287 "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u",
290 logger_log(conn
->raop
->logger
, LOGGER_INFO
, "Responding with %s", buffer
);
291 http_response_add_header(res
, "Transport", buffer
);
292 http_response_add_header(res
, "Session", "DEADBEEF");
293 } else if (!strcmp(method
, "SET_PARAMETER")) {
294 const char *content_type
;
298 content_type
= http_request_get_header(request
, "Content-Type");
299 data
= http_request_get_data(request
, &datalen
);
300 if (!strcmp(content_type
, "text/parameters")) {
302 datastr
= calloc(1, datalen
+1);
303 if (data
&& datastr
&& conn
->raop_rtp
) {
304 memcpy(datastr
, data
, datalen
);
305 if (!strncmp(datastr
, "volume: ", 8)) {
307 sscanf(datastr
+8, "%f", &vol
);
308 raop_rtp_set_volume(conn
->raop_rtp
, vol
);
310 } else if (!conn
->raop_rtp
) {
311 logger_log(conn
->raop
->logger
, LOGGER_WARNING
, "RAOP not initialized at SET_PARAMETER volume");
314 } else if (!strcmp(content_type
, "image/jpeg")) {
315 logger_log(conn
->raop
->logger
, LOGGER_INFO
, "Got image data of %d bytes", datalen
);
316 if (conn
->raop_rtp
) {
317 raop_rtp_set_coverart(conn
->raop_rtp
, data
, datalen
);
319 logger_log(conn
->raop
->logger
, LOGGER_WARNING
, "RAOP not initialized at SET_PARAMETER coverart");
321 } else if (!strcmp(content_type
, "application/x-dmap-tagged")) {
322 logger_log(conn
->raop
->logger
, LOGGER_INFO
, "Got metadata of %d bytes", datalen
);
323 if (conn
->raop_rtp
) {
324 raop_rtp_set_metadata(conn
->raop_rtp
, data
, datalen
);
326 logger_log(conn
->raop
->logger
, LOGGER_WARNING
, "RAOP not initialized at SET_PARAMETER metadata");
329 } else if (!strcmp(method
, "FLUSH")) {
333 rtpinfo
= http_request_get_header(request
, "RTP-Info");
335 logger_log(conn
->raop
->logger
, LOGGER_INFO
, "Flush with RTP-Info: %s", rtpinfo
);
336 if (!strncmp(rtpinfo
, "seq=", 4)) {
337 next_seq
= strtol(rtpinfo
+4, NULL
, 10);
340 if (conn
->raop_rtp
) {
341 raop_rtp_flush(conn
->raop_rtp
, next_seq
);
343 logger_log(conn
->raop
->logger
, LOGGER_WARNING
, "RAOP not initialized at FLUSH");
345 } else if (!strcmp(method
, "TEARDOWN")) {
346 http_response_add_header(res
, "Connection", "close");
347 if (conn
->raop_rtp
) {
348 /* Destroy our RTP session */
349 raop_rtp_stop(conn
->raop_rtp
);
350 raop_rtp_destroy(conn
->raop_rtp
);
351 conn
->raop_rtp
= NULL
;
354 http_response_finish(res
, NULL
, 0);
356 logger_log(conn
->raop
->logger
, LOGGER_DEBUG
, "Handled request %s with URL %s", method
, http_request_get_url(request
));
361 conn_destroy(void *ptr
)
363 raop_conn_t
*conn
= ptr
;
365 if (conn
->raop_rtp
) {
366 /* This is done in case TEARDOWN was not called */
367 raop_rtp_destroy(conn
->raop_rtp
);
375 raop_init(int max_clients
, raop_callbacks_t
*callbacks
, const char *pemkey
, int *error
)
380 httpd_callbacks_t httpd_cbs
;
383 assert(max_clients
> 0);
384 assert(max_clients
< 100);
387 /* Initialize the network */
388 if (netutils_init() < 0) {
392 /* Validate the callbacks structure */
393 if (!callbacks
->audio_init
||
394 !callbacks
->audio_process
||
395 !callbacks
->audio_destroy
) {
399 /* Allocate the raop_t structure */
400 raop
= calloc(1, sizeof(raop_t
));
405 /* Initialize the logger */
406 raop
->logger
= logger_init();
408 /* Set HTTP callbacks to our handlers */
409 memset(&httpd_cbs
, 0, sizeof(httpd_cbs
));
410 httpd_cbs
.opaque
= raop
;
411 httpd_cbs
.conn_init
= &conn_init
;
412 httpd_cbs
.conn_request
= &conn_request
;
413 httpd_cbs
.conn_destroy
= &conn_destroy
;
415 /* Initialize the http daemon */
416 httpd
= httpd_init(raop
->logger
, &httpd_cbs
, max_clients
);
422 /* Copy callbacks structure */
423 memcpy(&raop
->callbacks
, callbacks
, sizeof(raop_callbacks_t
));
425 /* Initialize RSA key handler */
426 rsakey
= rsakey_init_pem(pemkey
);
434 raop
->rsakey
= rsakey
;
440 raop_init_from_keyfile(int max_clients
, raop_callbacks_t
*callbacks
, const char *keyfile
, int *error
)
445 if (utils_read_file(&pemstr
, keyfile
) < 0) {
448 raop
= raop_init(max_clients
, callbacks
, pemstr
, error
);
454 raop_destroy(raop_t
*raop
)
459 httpd_destroy(raop
->httpd
);
460 rsakey_destroy(raop
->rsakey
);
461 logger_destroy(raop
->logger
);
464 /* Cleanup the network */
470 raop_is_running(raop_t
*raop
)
474 return httpd_is_running(raop
->httpd
);
478 raop_set_log_level(raop_t
*raop
, int level
)
482 logger_set_level(raop
->logger
, level
);
486 raop_set_log_callback(raop_t
*raop
, raop_log_callback_t callback
, void *cls
)
490 logger_set_callback(raop
->logger
, callback
, cls
);
494 raop_start(raop_t
*raop
, unsigned short *port
, const char *hwaddr
, int hwaddrlen
, const char *password
)
500 /* Validate hardware address */
501 if (hwaddrlen
> MAX_HWADDR_LEN
) {
505 memset(raop
->password
, 0, sizeof(raop
->password
));
507 /* Validate password */
508 if (strlen(password
) > MAX_PASSWORD_LEN
) {
512 /* Copy password to the raop structure */
513 strncpy(raop
->password
, password
, MAX_PASSWORD_LEN
);
516 /* Copy hwaddr to the raop structure */
517 memcpy(raop
->hwaddr
, hwaddr
, hwaddrlen
);
518 raop
->hwaddrlen
= hwaddrlen
;
520 return httpd_start(raop
->httpd
, port
);
524 raop_stop(raop_t
*raop
)
528 httpd_stop(raop
->httpd
);