Add a clarifying comment to the last commit
[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{
8e679577 126 const char realm[] = "airplay";
2340bcd3
JVH
127 raop_conn_t *conn = ptr;
128 raop_t *raop = conn->raop;
129
130 http_response_t *res;
131 const char *method;
132 const char *cseq;
133 const char *challenge;
e4169f77 134 int require_auth = 0;
2340bcd3
JVH
135
136 method = http_request_get_method(request);
137 cseq = http_request_get_header(request, "CSeq");
138 if (!method || !cseq) {
139 return;
140 }
141
142 res = http_response_init("RTSP/1.0", 200, "OK");
8c3f8f7a
JVH
143
144 /* We need authorization for everything else than OPTIONS request */
145 if (strcmp(method, "OPTIONS") != 0 && strlen(raop->password)) {
e4169f77
JVH
146 const char *authorization;
147
148 authorization = http_request_get_header(request, "Authorization");
149 if (authorization) {
fda63ad4
JVH
150 logger_log(conn->raop->logger, LOGGER_DEBUG, "Our nonce: %s", conn->nonce);
151 logger_log(conn->raop->logger, LOGGER_DEBUG, "Authorization: %s", authorization);
e4169f77 152 }
8e679577 153 if (!digest_is_valid(realm, raop->password, conn->nonce, method, http_request_get_url(request), authorization)) {
e4169f77
JVH
154 char *authstr;
155 int authstrlen;
156
157 /* Allocate the authenticate string */
8e679577 158 authstrlen = sizeof("Digest realm=\"\", nonce=\"\"") + sizeof(realm) + sizeof(conn->nonce) + 1;
e4169f77
JVH
159 authstr = malloc(authstrlen);
160
161 /* Concatenate the authenticate string */
162 memset(authstr, 0, authstrlen);
8e679577
JVH
163 strcat(authstr, "Digest realm=\"");
164 strcat(authstr, realm);
165 strcat(authstr, "\", nonce=\"");
e4169f77
JVH
166 strcat(authstr, conn->nonce);
167 strcat(authstr, "\"");
168
169 /* Construct a new response */
170 require_auth = 1;
171 http_response_destroy(res);
172 res = http_response_init("RTSP/1.0", 401, "Unauthorized");
173 http_response_add_header(res, "WWW-Authenticate", authstr);
174 free(authstr);
88631dc4 175 logger_log(conn->raop->logger, LOGGER_DEBUG, "Authentication unsuccessful, sending Unauthorized");
e4169f77 176 } else {
88631dc4 177 logger_log(conn->raop->logger, LOGGER_DEBUG, "Authentication successful!");
e4169f77
JVH
178 }
179 }
180
2340bcd3
JVH
181 http_response_add_header(res, "CSeq", cseq);
182 http_response_add_header(res, "Apple-Jack-Status", "connected; type=analog");
183
184 challenge = http_request_get_header(request, "Apple-Challenge");
91c41e1d 185 if (!require_auth && challenge) {
2340bcd3
JVH
186 char signature[MAX_SIGNATURE_LEN];
187
188 memset(signature, 0, sizeof(signature));
189 rsakey_sign(raop->rsakey, signature, sizeof(signature), challenge,
190 conn->local, conn->locallen, raop->hwaddr, raop->hwaddrlen);
2340bcd3 191 http_response_add_header(res, "Apple-Response", signature);
c891f978 192
fda63ad4
JVH
193 logger_log(conn->raop->logger, LOGGER_DEBUG, "Got challenge: %s", challenge);
194 logger_log(conn->raop->logger, LOGGER_DEBUG, "Got response: %s", signature);
2340bcd3 195 }
e4169f77
JVH
196
197 if (require_auth) {
198 /* Do nothing in case of authentication request */
199 } else if (!strcmp(method, "OPTIONS")) {
2340bcd3
JVH
200 http_response_add_header(res, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER");
201 } else if (!strcmp(method, "ANNOUNCE")) {
202 const char *data;
203 int datalen;
204
205 unsigned char aeskey[16];
206 unsigned char aesiv[16];
207 int aeskeylen, aesivlen;
208
209 data = http_request_get_data(request, &datalen);
210 if (data) {
ba0970e1 211 sdp_t *sdp;
597bc69b 212 const char *remotestr, *rtpmapstr, *fmtpstr, *aeskeystr, *aesivstr;
ba0970e1
JVH
213
214 sdp = sdp_init(data, datalen);
215 remotestr = sdp_get_connection(sdp);
597bc69b 216 rtpmapstr = sdp_get_rtpmap(sdp);
ba0970e1
JVH
217 fmtpstr = sdp_get_fmtp(sdp);
218 aeskeystr = sdp_get_rsaaeskey(sdp);
219 aesivstr = sdp_get_aesiv(sdp);
220
fda63ad4 221 logger_log(conn->raop->logger, LOGGER_DEBUG, "connection: %s", remotestr);
597bc69b 222 logger_log(conn->raop->logger, LOGGER_DEBUG, "rtpmap: %s", rtpmapstr);
fda63ad4
JVH
223 logger_log(conn->raop->logger, LOGGER_DEBUG, "fmtp: %s", fmtpstr);
224 logger_log(conn->raop->logger, LOGGER_DEBUG, "rsaaeskey: %s", aeskeystr);
225 logger_log(conn->raop->logger, LOGGER_DEBUG, "aesiv: %s", aesivstr);
ba0970e1
JVH
226
227 aeskeylen = rsakey_decrypt(raop->rsakey, aeskey, sizeof(aeskey), aeskeystr);
228 aesivlen = rsakey_parseiv(raop->rsakey, aesiv, sizeof(aesiv), aesivstr);
fda63ad4
JVH
229 logger_log(conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d", aeskeylen);
230 logger_log(conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d", aesivlen);
2340bcd3 231
c891f978
JVH
232 if (conn->raop_rtp) {
233 /* This should never happen */
234 raop_rtp_destroy(conn->raop_rtp);
235 conn->raop_rtp = NULL;
236 }
597bc69b 237 conn->raop_rtp = raop_rtp_init(raop->logger, &raop->callbacks, remotestr, rtpmapstr, fmtpstr, aeskey, aesiv);
fd7070e2
JVH
238 if (!conn->raop_rtp) {
239 logger_log(conn->raop->logger, LOGGER_ERR, "Error initializing the audio decoder");
c5f65268 240 http_response_set_disconnect(res, 1);
fd7070e2 241 }
2340bcd3
JVH
242 sdp_destroy(sdp);
243 }
244 } else if (!strcmp(method, "SETUP")) {
ba0970e1 245 unsigned short remote_cport=0, remote_tport=0;
2340bcd3
JVH
246 unsigned short cport=0, tport=0, dport=0;
247 const char *transport;
248 char buffer[1024];
249 int use_udp;
250
251 transport = http_request_get_header(request, "Transport");
252 assert(transport);
253
fda63ad4 254 logger_log(conn->raop->logger, LOGGER_INFO, "Transport: %s", transport);
2340bcd3 255 use_udp = strncmp(transport, "RTP/AVP/TCP", 11);
ba0970e1
JVH
256 if (use_udp) {
257 char *original, *current, *tmpstr;
258
259 current = original = strdup(transport);
260 if (original) {
261 while ((tmpstr = utils_strsep(&current, ";")) != NULL) {
262 unsigned short value;
263 int ret;
264
265 ret = sscanf(tmpstr, "control_port=%hu", &value);
266 if (ret == 1) {
fda63ad4 267 logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote control port: %hu", value);
ba0970e1
JVH
268 remote_cport = value;
269 }
270 ret = sscanf(tmpstr, "timing_port=%hu", &value);
271 if (ret == 1) {
fda63ad4 272 logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote timing port: %hu", value);
ba0970e1
JVH
273 remote_tport = value;
274 }
275 }
276 }
277 free(original);
278 }
fd7070e2
JVH
279 if (conn->raop_rtp) {
280 raop_rtp_start(conn->raop_rtp, use_udp, remote_cport, remote_tport, &cport, &tport, &dport);
281 } else {
c5f65268
JVH
282 logger_log(conn->raop->logger, LOGGER_ERR, "RAOP not initialized at SETUP, playing will fail!");
283 http_response_set_disconnect(res, 1);
fd7070e2 284 }
2340bcd3
JVH
285
286 memset(buffer, 0, sizeof(buffer));
287 if (use_udp) {
288 snprintf(buffer, sizeof(buffer)-1,
ba0970e1 289 "RTP/AVP/UDP;unicast;mode=record;timing_port=%hu;events;control_port=%hu;server_port=%hu",
2340bcd3
JVH
290 tport, cport, dport);
291 } else {
292 snprintf(buffer, sizeof(buffer)-1,
293 "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u",
294 dport);
295 }
fda63ad4 296 logger_log(conn->raop->logger, LOGGER_INFO, "Responding with %s", buffer);
2340bcd3
JVH
297 http_response_add_header(res, "Transport", buffer);
298 http_response_add_header(res, "Session", "DEADBEEF");
299 } else if (!strcmp(method, "SET_PARAMETER")) {
036dec08 300 const char *content_type;
2340bcd3
JVH
301 const char *data;
302 int datalen;
2340bcd3 303
036dec08 304 content_type = http_request_get_header(request, "Content-Type");
2340bcd3 305 data = http_request_get_data(request, &datalen);
036dec08
JVH
306 if (!strcmp(content_type, "text/parameters")) {
307 char *datastr;
308 datastr = calloc(1, datalen+1);
309 if (data && datastr && conn->raop_rtp) {
310 memcpy(datastr, data, datalen);
311 if (!strncmp(datastr, "volume: ", 8)) {
312 float vol = 0.0;
313 sscanf(datastr+8, "%f", &vol);
314 raop_rtp_set_volume(conn->raop_rtp, vol);
315 }
fd7070e2
JVH
316 } else if (!conn->raop_rtp) {
317 logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER volume");
2340bcd3 318 }
036dec08
JVH
319 free(datastr);
320 } else if (!strcmp(content_type, "image/jpeg")) {
fda63ad4 321 logger_log(conn->raop->logger, LOGGER_INFO, "Got image data of %d bytes", datalen);
fd7070e2
JVH
322 if (conn->raop_rtp) {
323 raop_rtp_set_coverart(conn->raop_rtp, data, datalen);
324 } else {
325 logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER coverart");
326 }
036dec08 327 } else if (!strcmp(content_type, "application/x-dmap-tagged")) {
fda63ad4 328 logger_log(conn->raop->logger, LOGGER_INFO, "Got metadata of %d bytes", datalen);
fd7070e2
JVH
329 if (conn->raop_rtp) {
330 raop_rtp_set_metadata(conn->raop_rtp, data, datalen);
331 } else {
332 logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER metadata");
333 }
2340bcd3
JVH
334 }
335 } else if (!strcmp(method, "FLUSH")) {
336 const char *rtpinfo;
337 int next_seq = -1;
338
339 rtpinfo = http_request_get_header(request, "RTP-Info");
c891f978 340 if (rtpinfo) {
fda63ad4 341 logger_log(conn->raop->logger, LOGGER_INFO, "Flush with RTP-Info: %s", rtpinfo);
c891f978
JVH
342 if (!strncmp(rtpinfo, "seq=", 4)) {
343 next_seq = strtol(rtpinfo+4, NULL, 10);
344 }
345 }
346 if (conn->raop_rtp) {
347 raop_rtp_flush(conn->raop_rtp, next_seq);
fd7070e2
JVH
348 } else {
349 logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at FLUSH");
2340bcd3 350 }
2340bcd3
JVH
351 } else if (!strcmp(method, "TEARDOWN")) {
352 http_response_add_header(res, "Connection", "close");
c891f978
JVH
353 if (conn->raop_rtp) {
354 /* Destroy our RTP session */
355 raop_rtp_stop(conn->raop_rtp);
356 raop_rtp_destroy(conn->raop_rtp);
357 conn->raop_rtp = NULL;
358 }
2340bcd3
JVH
359 }
360 http_response_finish(res, NULL, 0);
361
c5f65268 362 logger_log(conn->raop->logger, LOGGER_DEBUG, "Handled request %s with URL %s", method, http_request_get_url(request));
2340bcd3
JVH
363 *response = res;
364}
365
366static void
367conn_destroy(void *ptr)
368{
369 raop_conn_t *conn = ptr;
370
371 if (conn->raop_rtp) {
c891f978 372 /* This is done in case TEARDOWN was not called */
2340bcd3
JVH
373 raop_rtp_destroy(conn->raop_rtp);
374 }
375 free(conn->local);
376 free(conn->remote);
377 free(conn);
378}
379
380raop_t *
1d0adde5 381raop_init(int max_clients, raop_callbacks_t *callbacks, const char *pemkey, int *error)
2340bcd3
JVH
382{
383 raop_t *raop;
384 httpd_t *httpd;
385 rsakey_t *rsakey;
386 httpd_callbacks_t httpd_cbs;
387
388 assert(callbacks);
72fe063c
JVH
389 assert(max_clients > 0);
390 assert(max_clients < 100);
2340bcd3 391 assert(pemkey);
2340bcd3
JVH
392
393 /* Initialize the network */
394 if (netutils_init() < 0) {
395 return NULL;
396 }
397
398 /* Validate the callbacks structure */
b4cc5b07
JVH
399 if (!callbacks->audio_init ||
400 !callbacks->audio_process ||
2340bcd3
JVH
401 !callbacks->audio_destroy) {
402 return NULL;
403 }
404
2340bcd3
JVH
405 /* Allocate the raop_t structure */
406 raop = calloc(1, sizeof(raop_t));
407 if (!raop) {
408 return NULL;
409 }
410
411 /* Initialize the logger */
fda63ad4 412 raop->logger = logger_init();
2340bcd3
JVH
413
414 /* Set HTTP callbacks to our handlers */
415 memset(&httpd_cbs, 0, sizeof(httpd_cbs));
416 httpd_cbs.opaque = raop;
417 httpd_cbs.conn_init = &conn_init;
418 httpd_cbs.conn_request = &conn_request;
419 httpd_cbs.conn_destroy = &conn_destroy;
420
421 /* Initialize the http daemon */
462c72aa 422 httpd = httpd_init(raop->logger, &httpd_cbs, max_clients);
2340bcd3
JVH
423 if (!httpd) {
424 free(raop);
425 return NULL;
426 }
427
428 /* Copy callbacks structure */
429 memcpy(&raop->callbacks, callbacks, sizeof(raop_callbacks_t));
430
431 /* Initialize RSA key handler */
432 rsakey = rsakey_init_pem(pemkey);
433 if (!rsakey) {
434 free(httpd);
435 free(raop);
436 return NULL;
437 }
438
439 raop->httpd = httpd;
440 raop->rsakey = rsakey;
441
2340bcd3
JVH
442 return raop;
443}
444
445raop_t *
1d0adde5 446raop_init_from_keyfile(int max_clients, raop_callbacks_t *callbacks, const char *keyfile, int *error)
2340bcd3
JVH
447{
448 raop_t *raop;
449 char *pemstr;
450
451 if (utils_read_file(&pemstr, keyfile) < 0) {
452 return NULL;
453 }
1d0adde5 454 raop = raop_init(max_clients, callbacks, pemstr, error);
2340bcd3
JVH
455 free(pemstr);
456 return raop;
457}
458
459void
460raop_destroy(raop_t *raop)
461{
462 if (raop) {
463 raop_stop(raop);
464
465 httpd_destroy(raop->httpd);
466 rsakey_destroy(raop->rsakey);
fda63ad4 467 logger_destroy(raop->logger);
2340bcd3
JVH
468 free(raop);
469
470 /* Cleanup the network */
471 netutils_cleanup();
472 }
473}
474
5a746b97
JVH
475int
476raop_is_running(raop_t *raop)
477{
478 assert(raop);
479
480 return httpd_is_running(raop->httpd);
481}
482
fda63ad4
JVH
483void
484raop_set_log_level(raop_t *raop, int level)
485{
486 assert(raop);
487
488 logger_set_level(raop->logger, level);
489}
490
491void
2975b4b8 492raop_set_log_callback(raop_t *raop, raop_log_callback_t callback, void *cls)
fda63ad4
JVH
493{
494 assert(raop);
495
2975b4b8 496 logger_set_callback(raop->logger, callback, cls);
fda63ad4
JVH
497}
498
2340bcd3 499int
e4169f77 500raop_start(raop_t *raop, unsigned short *port, const char *hwaddr, int hwaddrlen, const char *password)
2340bcd3
JVH
501{
502 assert(raop);
503 assert(port);
406e9777
JVH
504 assert(hwaddr);
505
506 /* Validate hardware address */
507 if (hwaddrlen > MAX_HWADDR_LEN) {
508 return -1;
509 }
510
a68fedbb
JVH
511 memset(raop->password, 0, sizeof(raop->password));
512 if (password) {
513 /* Validate password */
514 if (strlen(password) > MAX_PASSWORD_LEN) {
515 return -1;
516 }
517
518 /* Copy password to the raop structure */
519 strncpy(raop->password, password, MAX_PASSWORD_LEN);
e4169f77
JVH
520 }
521
406e9777
JVH
522 /* Copy hwaddr to the raop structure */
523 memcpy(raop->hwaddr, hwaddr, hwaddrlen);
524 raop->hwaddrlen = hwaddrlen;
2340bcd3
JVH
525
526 return httpd_start(raop->httpd, port);
527}
528
529void
530raop_stop(raop_t *raop)
531{
532 assert(raop);
533
534 httpd_stop(raop->httpd);
535}
536