Update AirTV-Qt project to work with the latest library version.
[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"
23#include "httpd.h"
24#include "sdp.h"
25
26#include "global.h"
27#include "utils.h"
28#include "netutils.h"
29#include "logger.h"
30
31/* Actually 345 bytes for 2048-bit key */
32#define MAX_SIGNATURE_LEN 512
33
34struct raop_s {
35 /* Callbacks for audio */
36 raop_callbacks_t callbacks;
37
38 /* Logger instance */
39 logger_t logger;
40
41 /* HTTP daemon and RSA key */
42 httpd_t *httpd;
43 rsakey_t *rsakey;
44
45 /* Hardware address information */
46 unsigned char hwaddr[MAX_HWADDR_LEN];
47 int hwaddrlen;
48};
49
50struct raop_conn_s {
51 raop_t *raop;
52 raop_rtp_t *raop_rtp;
53
54 unsigned char *local;
55 int locallen;
56
57 unsigned char *remote;
58 int remotelen;
59};
60typedef struct raop_conn_s raop_conn_t;
61
62static void *
63conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remote, int remotelen)
64{
65 raop_conn_t *conn;
2340bcd3
JVH
66
67 conn = calloc(1, sizeof(raop_conn_t));
68 if (!conn) {
69 return NULL;
70 }
71 conn->raop = opaque;
72 conn->raop_rtp = NULL;
73
c891f978
JVH
74 if (locallen == 4) {
75 logger_log(&conn->raop->logger, LOGGER_INFO,
76 "Local: %d.%d.%d.%d\n",
77 local[0], local[1], local[2], local[3]);
78 } else if (locallen == 16) {
79 logger_log(&conn->raop->logger, LOGGER_INFO,
80 "Local: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
81 local[0], local[1], local[2], local[3], local[4], local[5], local[6], local[7],
82 local[8], local[9], local[10], local[11], local[12], local[13], local[14], local[15]);
2340bcd3 83 }
c891f978
JVH
84 if (remotelen == 4) {
85 logger_log(&conn->raop->logger, LOGGER_INFO,
86 "Remote: %d.%d.%d.%d\n",
87 remote[0], remote[1], remote[2], remote[3]);
88 } else if (remotelen == 16) {
89 logger_log(&conn->raop->logger, LOGGER_INFO,
90 "Remote: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
91 remote[0], remote[1], remote[2], remote[3], remote[4], remote[5], remote[6], remote[7],
92 remote[8], remote[9], remote[10], remote[11], remote[12], remote[13], remote[14], remote[15]);
2340bcd3 93 }
2340bcd3
JVH
94
95 conn->local = malloc(locallen);
96 assert(conn->local);
97 memcpy(conn->local, local, locallen);
98
99 conn->remote = malloc(remotelen);
100 assert(conn->remote);
101 memcpy(conn->remote, remote, remotelen);
102
103 conn->locallen = locallen;
104 conn->remotelen = remotelen;
105 return conn;
106}
107
108static void
109conn_request(void *ptr, http_request_t *request, http_response_t **response)
110{
111 raop_conn_t *conn = ptr;
112 raop_t *raop = conn->raop;
113
114 http_response_t *res;
115 const char *method;
116 const char *cseq;
117 const char *challenge;
118
119 method = http_request_get_method(request);
120 cseq = http_request_get_header(request, "CSeq");
121 if (!method || !cseq) {
122 return;
123 }
124
125 res = http_response_init("RTSP/1.0", 200, "OK");
126 http_response_add_header(res, "CSeq", cseq);
127 http_response_add_header(res, "Apple-Jack-Status", "connected; type=analog");
128
129 challenge = http_request_get_header(request, "Apple-Challenge");
130 if (challenge) {
131 char signature[MAX_SIGNATURE_LEN];
132
133 memset(signature, 0, sizeof(signature));
134 rsakey_sign(raop->rsakey, signature, sizeof(signature), challenge,
135 conn->local, conn->locallen, raop->hwaddr, raop->hwaddrlen);
2340bcd3 136 http_response_add_header(res, "Apple-Response", signature);
c891f978
JVH
137
138 logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got challenge: %s\n", challenge);
139 logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got response: %s\n", signature);
2340bcd3
JVH
140 }
141 if (!strcmp(method, "OPTIONS")) {
142 http_response_add_header(res, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER");
143 } else if (!strcmp(method, "ANNOUNCE")) {
144 const char *data;
145 int datalen;
146
147 unsigned char aeskey[16];
148 unsigned char aesiv[16];
149 int aeskeylen, aesivlen;
150
151 data = http_request_get_data(request, &datalen);
152 if (data) {
ba0970e1
JVH
153 sdp_t *sdp;
154 const char *remotestr, *fmtpstr, *aeskeystr, *aesivstr;
155
156 sdp = sdp_init(data, datalen);
157 remotestr = sdp_get_connection(sdp);
158 fmtpstr = sdp_get_fmtp(sdp);
159 aeskeystr = sdp_get_rsaaeskey(sdp);
160 aesivstr = sdp_get_aesiv(sdp);
161
162 logger_log(&conn->raop->logger, LOGGER_DEBUG, "connection: %s\n", remotestr);
163 logger_log(&conn->raop->logger, LOGGER_DEBUG, "fmtp: %s\n", fmtpstr);
164 logger_log(&conn->raop->logger, LOGGER_DEBUG, "rsaaeskey: %s\n", aeskeystr);
165 logger_log(&conn->raop->logger, LOGGER_DEBUG, "aesiv: %s\n", aesivstr);
166
167 aeskeylen = rsakey_decrypt(raop->rsakey, aeskey, sizeof(aeskey), aeskeystr);
168 aesivlen = rsakey_parseiv(raop->rsakey, aesiv, sizeof(aesiv), aesivstr);
14003408
JVH
169 logger_log(&conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d\n", aeskeylen);
170 logger_log(&conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d\n", aesivlen);
2340bcd3 171
c891f978
JVH
172 if (conn->raop_rtp) {
173 /* This should never happen */
174 raop_rtp_destroy(conn->raop_rtp);
175 conn->raop_rtp = NULL;
176 }
ba0970e1 177 conn->raop_rtp = raop_rtp_init(&raop->logger, &raop->callbacks, remotestr, fmtpstr, aeskey, aesiv);
2340bcd3
JVH
178 sdp_destroy(sdp);
179 }
180 } else if (!strcmp(method, "SETUP")) {
ba0970e1 181 unsigned short remote_cport=0, remote_tport=0;
2340bcd3
JVH
182 unsigned short cport=0, tport=0, dport=0;
183 const char *transport;
184 char buffer[1024];
185 int use_udp;
186
187 transport = http_request_get_header(request, "Transport");
188 assert(transport);
189
190 logger_log(&conn->raop->logger, LOGGER_INFO, "Transport: %s\n", transport);
191 use_udp = strncmp(transport, "RTP/AVP/TCP", 11);
ba0970e1
JVH
192 if (use_udp) {
193 char *original, *current, *tmpstr;
194
195 current = original = strdup(transport);
196 if (original) {
197 while ((tmpstr = utils_strsep(&current, ";")) != NULL) {
198 unsigned short value;
199 int ret;
200
201 ret = sscanf(tmpstr, "control_port=%hu", &value);
202 if (ret == 1) {
203 logger_log(&conn->raop->logger, LOGGER_DEBUG, "Found remote control port: %hu\n", value);
204 remote_cport = value;
205 }
206 ret = sscanf(tmpstr, "timing_port=%hu", &value);
207 if (ret == 1) {
208 logger_log(&conn->raop->logger, LOGGER_DEBUG, "Found remote timing port: %hu\n", value);
209 remote_tport = value;
210 }
211 }
212 }
213 free(original);
214 }
215 raop_rtp_start(conn->raop_rtp, use_udp, remote_cport, remote_tport, &cport, &tport, &dport);
2340bcd3
JVH
216
217 memset(buffer, 0, sizeof(buffer));
218 if (use_udp) {
219 snprintf(buffer, sizeof(buffer)-1,
ba0970e1 220 "RTP/AVP/UDP;unicast;mode=record;timing_port=%hu;events;control_port=%hu;server_port=%hu",
2340bcd3
JVH
221 tport, cport, dport);
222 } else {
223 snprintf(buffer, sizeof(buffer)-1,
224 "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u",
225 dport);
226 }
227 logger_log(&conn->raop->logger, LOGGER_INFO, "Responding with %s\n", buffer);
228 http_response_add_header(res, "Transport", buffer);
229 http_response_add_header(res, "Session", "DEADBEEF");
230 } else if (!strcmp(method, "SET_PARAMETER")) {
231 const char *data;
232 int datalen;
233 char *datastr;
234
235 data = http_request_get_data(request, &datalen);
236 datastr = calloc(1, datalen+1);
c891f978 237 if (data && datastr && conn->raop_rtp) {
2340bcd3
JVH
238 memcpy(datastr, data, datalen);
239 if (!strncmp(datastr, "volume: ", 8)) {
240 float vol = 0.0;
241 sscanf(data+8, "%f", &vol);
242 raop_rtp_set_volume(conn->raop_rtp, vol);
243 }
244 }
245 } else if (!strcmp(method, "FLUSH")) {
246 const char *rtpinfo;
247 int next_seq = -1;
248
249 rtpinfo = http_request_get_header(request, "RTP-Info");
c891f978
JVH
250 if (rtpinfo) {
251 logger_log(&conn->raop->logger, LOGGER_INFO, "Flush with RTP-Info: %s\n", rtpinfo);
252 if (!strncmp(rtpinfo, "seq=", 4)) {
253 next_seq = strtol(rtpinfo+4, NULL, 10);
254 }
255 }
256 if (conn->raop_rtp) {
257 raop_rtp_flush(conn->raop_rtp, next_seq);
2340bcd3 258 }
2340bcd3
JVH
259 } else if (!strcmp(method, "TEARDOWN")) {
260 http_response_add_header(res, "Connection", "close");
c891f978
JVH
261 if (conn->raop_rtp) {
262 /* Destroy our RTP session */
263 raop_rtp_stop(conn->raop_rtp);
264 raop_rtp_destroy(conn->raop_rtp);
265 conn->raop_rtp = NULL;
266 }
2340bcd3
JVH
267 }
268 http_response_finish(res, NULL, 0);
269
14003408 270 logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got request %s with URL %s\n", method, http_request_get_url(request));
2340bcd3
JVH
271 *response = res;
272}
273
274static void
275conn_destroy(void *ptr)
276{
277 raop_conn_t *conn = ptr;
278
279 if (conn->raop_rtp) {
c891f978 280 /* This is done in case TEARDOWN was not called */
2340bcd3
JVH
281 raop_rtp_destroy(conn->raop_rtp);
282 }
283 free(conn->local);
284 free(conn->remote);
285 free(conn);
286}
287
288raop_t *
406e9777 289raop_init(raop_callbacks_t *callbacks, const char *pemkey)
2340bcd3
JVH
290{
291 raop_t *raop;
292 httpd_t *httpd;
293 rsakey_t *rsakey;
294 httpd_callbacks_t httpd_cbs;
295
296 assert(callbacks);
297 assert(pemkey);
2340bcd3
JVH
298
299 /* Initialize the network */
300 if (netutils_init() < 0) {
301 return NULL;
302 }
303
304 /* Validate the callbacks structure */
305 if (!callbacks->audio_init || !callbacks->audio_set_volume ||
306 !callbacks->audio_process || !callbacks->audio_flush ||
307 !callbacks->audio_destroy) {
308 return NULL;
309 }
310
2340bcd3
JVH
311 /* Allocate the raop_t structure */
312 raop = calloc(1, sizeof(raop_t));
313 if (!raop) {
314 return NULL;
315 }
316
317 /* Initialize the logger */
318 logger_init(&raop->logger);
319
320 /* Set HTTP callbacks to our handlers */
321 memset(&httpd_cbs, 0, sizeof(httpd_cbs));
322 httpd_cbs.opaque = raop;
323 httpd_cbs.conn_init = &conn_init;
324 httpd_cbs.conn_request = &conn_request;
325 httpd_cbs.conn_destroy = &conn_destroy;
326
327 /* Initialize the http daemon */
328 httpd = httpd_init(&raop->logger, &httpd_cbs, 10, 1);
329 if (!httpd) {
330 free(raop);
331 return NULL;
332 }
333
334 /* Copy callbacks structure */
335 memcpy(&raop->callbacks, callbacks, sizeof(raop_callbacks_t));
336
337 /* Initialize RSA key handler */
338 rsakey = rsakey_init_pem(pemkey);
339 if (!rsakey) {
340 free(httpd);
341 free(raop);
342 return NULL;
343 }
344
345 raop->httpd = httpd;
346 raop->rsakey = rsakey;
347
2340bcd3
JVH
348 return raop;
349}
350
351raop_t *
406e9777 352raop_init_from_keyfile(raop_callbacks_t *callbacks, const char *keyfile)
2340bcd3
JVH
353{
354 raop_t *raop;
355 char *pemstr;
356
357 if (utils_read_file(&pemstr, keyfile) < 0) {
358 return NULL;
359 }
406e9777 360 raop = raop_init(callbacks, pemstr);
2340bcd3
JVH
361 free(pemstr);
362 return raop;
363}
364
365void
366raop_destroy(raop_t *raop)
367{
368 if (raop) {
369 raop_stop(raop);
370
371 httpd_destroy(raop->httpd);
372 rsakey_destroy(raop->rsakey);
373 free(raop);
374
375 /* Cleanup the network */
376 netutils_cleanup();
377 }
378}
379
380int
406e9777 381raop_start(raop_t *raop, unsigned short *port, const char *hwaddr, int hwaddrlen)
2340bcd3
JVH
382{
383 assert(raop);
384 assert(port);
406e9777
JVH
385 assert(hwaddr);
386
387 /* Validate hardware address */
388 if (hwaddrlen > MAX_HWADDR_LEN) {
389 return -1;
390 }
391
392 /* Copy hwaddr to the raop structure */
393 memcpy(raop->hwaddr, hwaddr, hwaddrlen);
394 raop->hwaddrlen = hwaddrlen;
2340bcd3
JVH
395
396 return httpd_start(raop->httpd, port);
397}
398
399void
400raop_stop(raop_t *raop)
401{
402 assert(raop);
403
404 httpd_stop(raop->httpd);
405}
406