Do not crash to assertion failure on unknown audio codec
[deb_shairplay.git] / src / lib / raop.c
CommitLineData
23e7e3ae
JVH
1/**
2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
3 *
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.
8 *
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.
13 */
14
2340bcd3
JVH
15#include <stdlib.h>
16#include <stdio.h>
17#include <string.h>
18#include <assert.h>
19
20#include "raop.h"
21#include "raop_rtp.h"
22#include "rsakey.h"
e4169f77 23#include "digest.h"
2340bcd3
JVH
24#include "httpd.h"
25#include "sdp.h"
26
27#include "global.h"
28#include "utils.h"
29#include "netutils.h"
30#include "logger.h"
566c9bf8 31#include "compat.h"
2340bcd3
JVH
32
33/* Actually 345 bytes for 2048-bit key */
34#define MAX_SIGNATURE_LEN 512
35
e4169f77
JVH
36/* Let's just decide on some length */
37#define MAX_PASSWORD_LEN 64
38
39/* MD5 as hex fits here */
268f72c8 40#define MAX_NONCE_LEN 32
e4169f77 41
2340bcd3
JVH
42struct raop_s {
43 /* Callbacks for audio */
44 raop_callbacks_t callbacks;
45
46 /* Logger instance */
fda63ad4 47 logger_t *logger;
2340bcd3
JVH
48
49 /* HTTP daemon and RSA key */
50 httpd_t *httpd;
51 rsakey_t *rsakey;
52
53 /* Hardware address information */
54 unsigned char hwaddr[MAX_HWADDR_LEN];
55 int hwaddrlen;
e4169f77
JVH
56
57 /* Password information */
58 char password[MAX_PASSWORD_LEN+1];
2340bcd3
JVH
59};
60
61struct raop_conn_s {
62 raop_t *raop;
63 raop_rtp_t *raop_rtp;
64
65 unsigned char *local;
66 int locallen;
67
68 unsigned char *remote;
69 int remotelen;
e4169f77
JVH
70
71 char nonce[MAX_NONCE_LEN+1];
2340bcd3
JVH
72};
73typedef struct raop_conn_s raop_conn_t;
74
75static void *
76conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remote, int remotelen)
77{
78 raop_conn_t *conn;
2340bcd3
JVH
79
80 conn = calloc(1, sizeof(raop_conn_t));
81 if (!conn) {
82 return NULL;
83 }
84 conn->raop = opaque;
85 conn->raop_rtp = NULL;
86
c891f978 87 if (locallen == 4) {
fda63ad4 88 logger_log(conn->raop->logger, LOGGER_INFO,
46212791 89 "Local: %d.%d.%d.%d",
c891f978
JVH
90 local[0], local[1], local[2], local[3]);
91 } else if (locallen == 16) {
fda63ad4 92 logger_log(conn->raop->logger, LOGGER_INFO,
46212791 93 "Local: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
c891f978
JVH
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]);
2340bcd3 96 }
c891f978 97 if (remotelen == 4) {
fda63ad4 98 logger_log(conn->raop->logger, LOGGER_INFO,
46212791 99 "Remote: %d.%d.%d.%d",
c891f978
JVH
100 remote[0], remote[1], remote[2], remote[3]);
101 } else if (remotelen == 16) {
fda63ad4 102 logger_log(conn->raop->logger, LOGGER_INFO,
46212791 103 "Remote: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
c891f978
JVH
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]);
2340bcd3 106 }
2340bcd3
JVH
107
108 conn->local = malloc(locallen);
109 assert(conn->local);
110 memcpy(conn->local, local, locallen);
111
112 conn->remote = malloc(remotelen);
113 assert(conn->remote);
114 memcpy(conn->remote, remote, remotelen);
115
116 conn->locallen = locallen;
117 conn->remotelen = remotelen;
e4169f77
JVH
118
119 digest_generate_nonce(conn->nonce, sizeof(conn->nonce));
2340bcd3
JVH
120 return conn;
121}
122
123static void
124conn_request(void *ptr, http_request_t *request, http_response_t **response)
125{
126 raop_conn_t *conn = ptr;
127 raop_t *raop = conn->raop;
128
129 http_response_t *res;
130 const char *method;
131 const char *cseq;
132 const char *challenge;
e4169f77 133 int require_auth = 0;
2340bcd3
JVH
134
135 method = http_request_get_method(request);
136 cseq = http_request_get_header(request, "CSeq");
137 if (!method || !cseq) {
138 return;
139 }
140
141 res = http_response_init("RTSP/1.0", 200, "OK");
e4169f77
JVH
142 if (strlen(raop->password)) {
143 const char *authorization;
144
145 authorization = http_request_get_header(request, "Authorization");
146 if (authorization) {
fda63ad4
JVH
147 logger_log(conn->raop->logger, LOGGER_DEBUG, "Our nonce: %s", conn->nonce);
148 logger_log(conn->raop->logger, LOGGER_DEBUG, "Authorization: %s", authorization);
e4169f77 149 }
268f72c8 150 if (!digest_is_valid("AppleTV", raop->password, conn->nonce, method, http_request_get_url(request), authorization)) {
e4169f77
JVH
151 char *authstr;
152 int authstrlen;
153
154 /* Allocate the authenticate string */
155 authstrlen = sizeof("Digest realm=\"AppleTV\", nonce=\"\"") + sizeof(conn->nonce) + 1;
156 authstr = malloc(authstrlen);
157
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, "\"");
163
164 /* Construct a new response */
165 require_auth = 1;
166 http_response_destroy(res);
167 res = http_response_init("RTSP/1.0", 401, "Unauthorized");
168 http_response_add_header(res, "WWW-Authenticate", authstr);
169 free(authstr);
170 } else {
fda63ad4 171 logger_log(conn->raop->logger, LOGGER_DEBUG, "AUTHENTICATION SUCCESS!");
e4169f77
JVH
172 }
173 }
174
2340bcd3
JVH
175 http_response_add_header(res, "CSeq", cseq);
176 http_response_add_header(res, "Apple-Jack-Status", "connected; type=analog");
177
178 challenge = http_request_get_header(request, "Apple-Challenge");
91c41e1d 179 if (!require_auth && challenge) {
2340bcd3
JVH
180 char signature[MAX_SIGNATURE_LEN];
181
182 memset(signature, 0, sizeof(signature));
183 rsakey_sign(raop->rsakey, signature, sizeof(signature), challenge,
184 conn->local, conn->locallen, raop->hwaddr, raop->hwaddrlen);
2340bcd3 185 http_response_add_header(res, "Apple-Response", signature);
c891f978 186
fda63ad4
JVH
187 logger_log(conn->raop->logger, LOGGER_DEBUG, "Got challenge: %s", challenge);
188 logger_log(conn->raop->logger, LOGGER_DEBUG, "Got response: %s", signature);
2340bcd3 189 }
e4169f77
JVH
190
191 if (require_auth) {
192 /* Do nothing in case of authentication request */
193 } else if (!strcmp(method, "OPTIONS")) {
2340bcd3
JVH
194 http_response_add_header(res, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER");
195 } else if (!strcmp(method, "ANNOUNCE")) {
196 const char *data;
197 int datalen;
198
199 unsigned char aeskey[16];
200 unsigned char aesiv[16];
201 int aeskeylen, aesivlen;
202
203 data = http_request_get_data(request, &datalen);
204 if (data) {
ba0970e1 205 sdp_t *sdp;
597bc69b 206 const char *remotestr, *rtpmapstr, *fmtpstr, *aeskeystr, *aesivstr;
ba0970e1
JVH
207
208 sdp = sdp_init(data, datalen);
209 remotestr = sdp_get_connection(sdp);
597bc69b 210 rtpmapstr = sdp_get_rtpmap(sdp);
ba0970e1
JVH
211 fmtpstr = sdp_get_fmtp(sdp);
212 aeskeystr = sdp_get_rsaaeskey(sdp);
213 aesivstr = sdp_get_aesiv(sdp);
214
fda63ad4 215 logger_log(conn->raop->logger, LOGGER_DEBUG, "connection: %s", remotestr);
597bc69b 216 logger_log(conn->raop->logger, LOGGER_DEBUG, "rtpmap: %s", rtpmapstr);
fda63ad4
JVH
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);
ba0970e1
JVH
220
221 aeskeylen = rsakey_decrypt(raop->rsakey, aeskey, sizeof(aeskey), aeskeystr);
222 aesivlen = rsakey_parseiv(raop->rsakey, aesiv, sizeof(aesiv), aesivstr);
fda63ad4
JVH
223 logger_log(conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d", aeskeylen);
224 logger_log(conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d", aesivlen);
2340bcd3 225
c891f978
JVH
226 if (conn->raop_rtp) {
227 /* This should never happen */
228 raop_rtp_destroy(conn->raop_rtp);
229 conn->raop_rtp = NULL;
230 }
597bc69b 231 conn->raop_rtp = raop_rtp_init(raop->logger, &raop->callbacks, remotestr, rtpmapstr, fmtpstr, aeskey, aesiv);
fd7070e2
JVH
232 if (!conn->raop_rtp) {
233 logger_log(conn->raop->logger, LOGGER_ERR, "Error initializing the audio decoder");
234 }
2340bcd3
JVH
235 sdp_destroy(sdp);
236 }
237 } else if (!strcmp(method, "SETUP")) {
ba0970e1 238 unsigned short remote_cport=0, remote_tport=0;
2340bcd3
JVH
239 unsigned short cport=0, tport=0, dport=0;
240 const char *transport;
241 char buffer[1024];
242 int use_udp;
243
244 transport = http_request_get_header(request, "Transport");
245 assert(transport);
246
fda63ad4 247 logger_log(conn->raop->logger, LOGGER_INFO, "Transport: %s", transport);
2340bcd3 248 use_udp = strncmp(transport, "RTP/AVP/TCP", 11);
ba0970e1
JVH
249 if (use_udp) {
250 char *original, *current, *tmpstr;
251
252 current = original = strdup(transport);
253 if (original) {
254 while ((tmpstr = utils_strsep(&current, ";")) != NULL) {
255 unsigned short value;
256 int ret;
257
258 ret = sscanf(tmpstr, "control_port=%hu", &value);
259 if (ret == 1) {
fda63ad4 260 logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote control port: %hu", value);
ba0970e1
JVH
261 remote_cport = value;
262 }
263 ret = sscanf(tmpstr, "timing_port=%hu", &value);
264 if (ret == 1) {
fda63ad4 265 logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote timing port: %hu", value);
ba0970e1
JVH
266 remote_tport = value;
267 }
268 }
269 }
270 free(original);
271 }
fd7070e2
JVH
272 if (conn->raop_rtp) {
273 raop_rtp_start(conn->raop_rtp, use_udp, remote_cport, remote_tport, &cport, &tport, &dport);
274 } else {
275 logger_log(conn->raop->logger, LOGGER_CRIT, "RAOP not initialized at SETUP, playing will fail!");
276 }
2340bcd3
JVH
277
278 memset(buffer, 0, sizeof(buffer));
279 if (use_udp) {
280 snprintf(buffer, sizeof(buffer)-1,
ba0970e1 281 "RTP/AVP/UDP;unicast;mode=record;timing_port=%hu;events;control_port=%hu;server_port=%hu",
2340bcd3
JVH
282 tport, cport, dport);
283 } else {
284 snprintf(buffer, sizeof(buffer)-1,
285 "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u",
286 dport);
287 }
fda63ad4 288 logger_log(conn->raop->logger, LOGGER_INFO, "Responding with %s", buffer);
2340bcd3
JVH
289 http_response_add_header(res, "Transport", buffer);
290 http_response_add_header(res, "Session", "DEADBEEF");
291 } else if (!strcmp(method, "SET_PARAMETER")) {
036dec08 292 const char *content_type;
2340bcd3
JVH
293 const char *data;
294 int datalen;
2340bcd3 295
036dec08 296 content_type = http_request_get_header(request, "Content-Type");
2340bcd3 297 data = http_request_get_data(request, &datalen);
036dec08
JVH
298 if (!strcmp(content_type, "text/parameters")) {
299 char *datastr;
300 datastr = calloc(1, datalen+1);
301 if (data && datastr && conn->raop_rtp) {
302 memcpy(datastr, data, datalen);
303 if (!strncmp(datastr, "volume: ", 8)) {
304 float vol = 0.0;
305 sscanf(datastr+8, "%f", &vol);
306 raop_rtp_set_volume(conn->raop_rtp, vol);
307 }
fd7070e2
JVH
308 } else if (!conn->raop_rtp) {
309 logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER volume");
2340bcd3 310 }
036dec08
JVH
311 free(datastr);
312 } else if (!strcmp(content_type, "image/jpeg")) {
fda63ad4 313 logger_log(conn->raop->logger, LOGGER_INFO, "Got image data of %d bytes", datalen);
fd7070e2
JVH
314 if (conn->raop_rtp) {
315 raop_rtp_set_coverart(conn->raop_rtp, data, datalen);
316 } else {
317 logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER coverart");
318 }
036dec08 319 } else if (!strcmp(content_type, "application/x-dmap-tagged")) {
fda63ad4 320 logger_log(conn->raop->logger, LOGGER_INFO, "Got metadata of %d bytes", datalen);
fd7070e2
JVH
321 if (conn->raop_rtp) {
322 raop_rtp_set_metadata(conn->raop_rtp, data, datalen);
323 } else {
324 logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER metadata");
325 }
2340bcd3
JVH
326 }
327 } else if (!strcmp(method, "FLUSH")) {
328 const char *rtpinfo;
329 int next_seq = -1;
330
331 rtpinfo = http_request_get_header(request, "RTP-Info");
c891f978 332 if (rtpinfo) {
fda63ad4 333 logger_log(conn->raop->logger, LOGGER_INFO, "Flush with RTP-Info: %s", rtpinfo);
c891f978
JVH
334 if (!strncmp(rtpinfo, "seq=", 4)) {
335 next_seq = strtol(rtpinfo+4, NULL, 10);
336 }
337 }
338 if (conn->raop_rtp) {
339 raop_rtp_flush(conn->raop_rtp, next_seq);
fd7070e2
JVH
340 } else {
341 logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at FLUSH");
2340bcd3 342 }
2340bcd3
JVH
343 } else if (!strcmp(method, "TEARDOWN")) {
344 http_response_add_header(res, "Connection", "close");
c891f978
JVH
345 if (conn->raop_rtp) {
346 /* Destroy our RTP session */
347 raop_rtp_stop(conn->raop_rtp);
348 raop_rtp_destroy(conn->raop_rtp);
349 conn->raop_rtp = NULL;
350 }
2340bcd3
JVH
351 }
352 http_response_finish(res, NULL, 0);
353
fda63ad4 354 logger_log(conn->raop->logger, LOGGER_DEBUG, "Got request %s with URL %s", method, http_request_get_url(request));
2340bcd3
JVH
355 *response = res;
356}
357
358static void
359conn_destroy(void *ptr)
360{
361 raop_conn_t *conn = ptr;
362
363 if (conn->raop_rtp) {
c891f978 364 /* This is done in case TEARDOWN was not called */
2340bcd3
JVH
365 raop_rtp_destroy(conn->raop_rtp);
366 }
367 free(conn->local);
368 free(conn->remote);
369 free(conn);
370}
371
372raop_t *
1d0adde5 373raop_init(int max_clients, raop_callbacks_t *callbacks, const char *pemkey, int *error)
2340bcd3
JVH
374{
375 raop_t *raop;
376 httpd_t *httpd;
377 rsakey_t *rsakey;
378 httpd_callbacks_t httpd_cbs;
379
380 assert(callbacks);
72fe063c
JVH
381 assert(max_clients > 0);
382 assert(max_clients < 100);
2340bcd3 383 assert(pemkey);
2340bcd3
JVH
384
385 /* Initialize the network */
386 if (netutils_init() < 0) {
387 return NULL;
388 }
389
390 /* Validate the callbacks structure */
b4cc5b07
JVH
391 if (!callbacks->audio_init ||
392 !callbacks->audio_process ||
2340bcd3
JVH
393 !callbacks->audio_destroy) {
394 return NULL;
395 }
396
2340bcd3
JVH
397 /* Allocate the raop_t structure */
398 raop = calloc(1, sizeof(raop_t));
399 if (!raop) {
400 return NULL;
401 }
402
403 /* Initialize the logger */
fda63ad4 404 raop->logger = logger_init();
2340bcd3
JVH
405
406 /* Set HTTP callbacks to our handlers */
407 memset(&httpd_cbs, 0, sizeof(httpd_cbs));
408 httpd_cbs.opaque = raop;
409 httpd_cbs.conn_init = &conn_init;
410 httpd_cbs.conn_request = &conn_request;
411 httpd_cbs.conn_destroy = &conn_destroy;
412
413 /* Initialize the http daemon */
462c72aa 414 httpd = httpd_init(raop->logger, &httpd_cbs, max_clients);
2340bcd3
JVH
415 if (!httpd) {
416 free(raop);
417 return NULL;
418 }
419
420 /* Copy callbacks structure */
421 memcpy(&raop->callbacks, callbacks, sizeof(raop_callbacks_t));
422
423 /* Initialize RSA key handler */
424 rsakey = rsakey_init_pem(pemkey);
425 if (!rsakey) {
426 free(httpd);
427 free(raop);
428 return NULL;
429 }
430
431 raop->httpd = httpd;
432 raop->rsakey = rsakey;
433
2340bcd3
JVH
434 return raop;
435}
436
437raop_t *
1d0adde5 438raop_init_from_keyfile(int max_clients, raop_callbacks_t *callbacks, const char *keyfile, int *error)
2340bcd3
JVH
439{
440 raop_t *raop;
441 char *pemstr;
442
443 if (utils_read_file(&pemstr, keyfile) < 0) {
444 return NULL;
445 }
1d0adde5 446 raop = raop_init(max_clients, callbacks, pemstr, error);
2340bcd3
JVH
447 free(pemstr);
448 return raop;
449}
450
451void
452raop_destroy(raop_t *raop)
453{
454 if (raop) {
455 raop_stop(raop);
456
457 httpd_destroy(raop->httpd);
458 rsakey_destroy(raop->rsakey);
fda63ad4 459 logger_destroy(raop->logger);
2340bcd3
JVH
460 free(raop);
461
462 /* Cleanup the network */
463 netutils_cleanup();
464 }
465}
466
5a746b97
JVH
467int
468raop_is_running(raop_t *raop)
469{
470 assert(raop);
471
472 return httpd_is_running(raop->httpd);
473}
474
fda63ad4
JVH
475void
476raop_set_log_level(raop_t *raop, int level)
477{
478 assert(raop);
479
480 logger_set_level(raop->logger, level);
481}
482
483void
2975b4b8 484raop_set_log_callback(raop_t *raop, raop_log_callback_t callback, void *cls)
fda63ad4
JVH
485{
486 assert(raop);
487
2975b4b8 488 logger_set_callback(raop->logger, callback, cls);
fda63ad4
JVH
489}
490
2340bcd3 491int
e4169f77 492raop_start(raop_t *raop, unsigned short *port, const char *hwaddr, int hwaddrlen, const char *password)
2340bcd3
JVH
493{
494 assert(raop);
495 assert(port);
406e9777
JVH
496 assert(hwaddr);
497
498 /* Validate hardware address */
499 if (hwaddrlen > MAX_HWADDR_LEN) {
500 return -1;
501 }
502
a68fedbb
JVH
503 memset(raop->password, 0, sizeof(raop->password));
504 if (password) {
505 /* Validate password */
506 if (strlen(password) > MAX_PASSWORD_LEN) {
507 return -1;
508 }
509
510 /* Copy password to the raop structure */
511 strncpy(raop->password, password, MAX_PASSWORD_LEN);
e4169f77
JVH
512 }
513
406e9777
JVH
514 /* Copy hwaddr to the raop structure */
515 memcpy(raop->hwaddr, hwaddr, hwaddrlen);
516 raop->hwaddrlen = hwaddrlen;
2340bcd3
JVH
517
518 return httpd_start(raop->httpd, port);
519}
520
521void
522raop_stop(raop_t *raop)
523{
524 assert(raop);
525
526 httpd_stop(raop->httpd);
527}
528