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