Update paths in the README file.
[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;
66 int i;
67
68 conn = calloc(1, sizeof(raop_conn_t));
69 if (!conn) {
70 return NULL;
71 }
72 conn->raop = opaque;
73 conn->raop_rtp = NULL;
74
75 logger_log(&conn->raop->logger, LOGGER_INFO, "Local: ");
76 for (i=0; i<locallen; i++) {
77 logger_log(&conn->raop->logger, LOGGER_INFO, "%02x", local[i]);
78 }
14003408 79 logger_log(&conn->raop->logger, LOGGER_INFO, "\n");
2340bcd3
JVH
80 logger_log(&conn->raop->logger, LOGGER_INFO, "Remote: ");
81 for (i=0; i<remotelen; i++) {
82 logger_log(&conn->raop->logger, LOGGER_INFO, "%02x", remote[i]);
83 }
84 logger_log(&conn->raop->logger, LOGGER_INFO, "\n");
85
86 conn->local = malloc(locallen);
87 assert(conn->local);
88 memcpy(conn->local, local, locallen);
89
90 conn->remote = malloc(remotelen);
91 assert(conn->remote);
92 memcpy(conn->remote, remote, remotelen);
93
94 conn->locallen = locallen;
95 conn->remotelen = remotelen;
96 return conn;
97}
98
99static void
100conn_request(void *ptr, http_request_t *request, http_response_t **response)
101{
102 raop_conn_t *conn = ptr;
103 raop_t *raop = conn->raop;
104
105 http_response_t *res;
106 const char *method;
107 const char *cseq;
108 const char *challenge;
109
110 method = http_request_get_method(request);
111 cseq = http_request_get_header(request, "CSeq");
112 if (!method || !cseq) {
113 return;
114 }
115
116 res = http_response_init("RTSP/1.0", 200, "OK");
117 http_response_add_header(res, "CSeq", cseq);
118 http_response_add_header(res, "Apple-Jack-Status", "connected; type=analog");
119
120 challenge = http_request_get_header(request, "Apple-Challenge");
121 if (challenge) {
122 char signature[MAX_SIGNATURE_LEN];
123
124 memset(signature, 0, sizeof(signature));
125 rsakey_sign(raop->rsakey, signature, sizeof(signature), challenge,
126 conn->local, conn->locallen, raop->hwaddr, raop->hwaddrlen);
14003408 127 logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got signature: %s\n", signature);
2340bcd3
JVH
128 http_response_add_header(res, "Apple-Response", signature);
129 }
130 if (!strcmp(method, "OPTIONS")) {
131 http_response_add_header(res, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER");
132 } else if (!strcmp(method, "ANNOUNCE")) {
133 const char *data;
134 int datalen;
135
136 unsigned char aeskey[16];
137 unsigned char aesiv[16];
138 int aeskeylen, aesivlen;
139
140 data = http_request_get_data(request, &datalen);
141 if (data) {
142 sdp_t *sdp = sdp_init(data, datalen);
14003408
JVH
143 logger_log(&conn->raop->logger, LOGGER_DEBUG, "rsaaeskey: %s\n", sdp_get_rsaaeskey(sdp));
144 logger_log(&conn->raop->logger, LOGGER_DEBUG, "aesiv: %s\n", sdp_get_aesiv(sdp));
2340bcd3
JVH
145
146 aeskeylen = rsakey_decrypt(raop->rsakey, aeskey, sizeof(aeskey),
147 sdp_get_rsaaeskey(sdp));
148 aesivlen = rsakey_parseiv(raop->rsakey, aesiv, sizeof(aesiv),
149 sdp_get_aesiv(sdp));
14003408
JVH
150 logger_log(&conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d\n", aeskeylen);
151 logger_log(&conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d\n", aesivlen);
2340bcd3
JVH
152
153 conn->raop_rtp = raop_rtp_init(&raop->logger, &raop->callbacks, sdp_get_fmtp(sdp), aeskey, aesiv);
154 sdp_destroy(sdp);
155 }
156 } else if (!strcmp(method, "SETUP")) {
157 unsigned short cport=0, tport=0, dport=0;
158 const char *transport;
159 char buffer[1024];
160 int use_udp;
161
162 transport = http_request_get_header(request, "Transport");
163 assert(transport);
164
165 logger_log(&conn->raop->logger, LOGGER_INFO, "Transport: %s\n", transport);
166 use_udp = strncmp(transport, "RTP/AVP/TCP", 11);
167
168 /* FIXME: Should use the parsed ports for resend */
169 raop_rtp_start(conn->raop_rtp, use_udp, 1234, 1234, &cport, &tport, &dport);
2340bcd3
JVH
170
171 memset(buffer, 0, sizeof(buffer));
172 if (use_udp) {
173 snprintf(buffer, sizeof(buffer)-1,
174 "RTP/AVP/UDP;unicast;mode=record;timing_port=%u;events;control_port=%u;server_port=%u",
175 tport, cport, dport);
176 } else {
177 snprintf(buffer, sizeof(buffer)-1,
178 "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u",
179 dport);
180 }
181 logger_log(&conn->raop->logger, LOGGER_INFO, "Responding with %s\n", buffer);
182 http_response_add_header(res, "Transport", buffer);
183 http_response_add_header(res, "Session", "DEADBEEF");
184 } else if (!strcmp(method, "SET_PARAMETER")) {
185 const char *data;
186 int datalen;
187 char *datastr;
188
189 data = http_request_get_data(request, &datalen);
190 datastr = calloc(1, datalen+1);
191 if (datastr) {
192 memcpy(datastr, data, datalen);
193 if (!strncmp(datastr, "volume: ", 8)) {
194 float vol = 0.0;
195 sscanf(data+8, "%f", &vol);
196 raop_rtp_set_volume(conn->raop_rtp, vol);
197 }
198 }
199 } else if (!strcmp(method, "FLUSH")) {
200 const char *rtpinfo;
201 int next_seq = -1;
202
203 rtpinfo = http_request_get_header(request, "RTP-Info");
204 assert(rtpinfo);
205
206 logger_log(&conn->raop->logger, LOGGER_INFO, "RTP-Info: %s\n", rtpinfo);
207 if (!strncmp(rtpinfo, "seq=", 4)) {
208 next_seq = strtol(rtpinfo+4, NULL, 10);
209 }
210 raop_rtp_flush(conn->raop_rtp, next_seq);
211 } else if (!strcmp(method, "TEARDOWN")) {
212 http_response_add_header(res, "Connection", "close");
213 raop_rtp_stop(conn->raop_rtp);
214 raop_rtp_destroy(conn->raop_rtp);
215 conn->raop_rtp = NULL;
216 }
217 http_response_finish(res, NULL, 0);
218
14003408 219 logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got request %s with URL %s\n", method, http_request_get_url(request));
2340bcd3
JVH
220 *response = res;
221}
222
223static void
224conn_destroy(void *ptr)
225{
226 raop_conn_t *conn = ptr;
227
228 if (conn->raop_rtp) {
229 raop_rtp_destroy(conn->raop_rtp);
230 }
231 free(conn->local);
232 free(conn->remote);
233 free(conn);
234}
235
236raop_t *
406e9777 237raop_init(raop_callbacks_t *callbacks, const char *pemkey)
2340bcd3
JVH
238{
239 raop_t *raop;
240 httpd_t *httpd;
241 rsakey_t *rsakey;
242 httpd_callbacks_t httpd_cbs;
243
244 assert(callbacks);
245 assert(pemkey);
2340bcd3
JVH
246
247 /* Initialize the network */
248 if (netutils_init() < 0) {
249 return NULL;
250 }
251
252 /* Validate the callbacks structure */
253 if (!callbacks->audio_init || !callbacks->audio_set_volume ||
254 !callbacks->audio_process || !callbacks->audio_flush ||
255 !callbacks->audio_destroy) {
256 return NULL;
257 }
258
2340bcd3
JVH
259 /* Allocate the raop_t structure */
260 raop = calloc(1, sizeof(raop_t));
261 if (!raop) {
262 return NULL;
263 }
264
265 /* Initialize the logger */
266 logger_init(&raop->logger);
267
268 /* Set HTTP callbacks to our handlers */
269 memset(&httpd_cbs, 0, sizeof(httpd_cbs));
270 httpd_cbs.opaque = raop;
271 httpd_cbs.conn_init = &conn_init;
272 httpd_cbs.conn_request = &conn_request;
273 httpd_cbs.conn_destroy = &conn_destroy;
274
275 /* Initialize the http daemon */
276 httpd = httpd_init(&raop->logger, &httpd_cbs, 10, 1);
277 if (!httpd) {
278 free(raop);
279 return NULL;
280 }
281
282 /* Copy callbacks structure */
283 memcpy(&raop->callbacks, callbacks, sizeof(raop_callbacks_t));
284
285 /* Initialize RSA key handler */
286 rsakey = rsakey_init_pem(pemkey);
287 if (!rsakey) {
288 free(httpd);
289 free(raop);
290 return NULL;
291 }
292
293 raop->httpd = httpd;
294 raop->rsakey = rsakey;
295
2340bcd3
JVH
296 return raop;
297}
298
299raop_t *
406e9777 300raop_init_from_keyfile(raop_callbacks_t *callbacks, const char *keyfile)
2340bcd3
JVH
301{
302 raop_t *raop;
303 char *pemstr;
304
305 if (utils_read_file(&pemstr, keyfile) < 0) {
306 return NULL;
307 }
406e9777 308 raop = raop_init(callbacks, pemstr);
2340bcd3
JVH
309 free(pemstr);
310 return raop;
311}
312
313void
314raop_destroy(raop_t *raop)
315{
316 if (raop) {
317 raop_stop(raop);
318
319 httpd_destroy(raop->httpd);
320 rsakey_destroy(raop->rsakey);
321 free(raop);
322
323 /* Cleanup the network */
324 netutils_cleanup();
325 }
326}
327
328int
406e9777 329raop_start(raop_t *raop, unsigned short *port, const char *hwaddr, int hwaddrlen)
2340bcd3
JVH
330{
331 assert(raop);
332 assert(port);
406e9777
JVH
333 assert(hwaddr);
334
335 /* Validate hardware address */
336 if (hwaddrlen > MAX_HWADDR_LEN) {
337 return -1;
338 }
339
340 /* Copy hwaddr to the raop structure */
341 memcpy(raop->hwaddr, hwaddr, hwaddrlen);
342 raop->hwaddrlen = hwaddrlen;
2340bcd3
JVH
343
344 return httpd_start(raop->httpd, port);
345}
346
347void
348raop_stop(raop_t *raop)
349{
350 assert(raop);
351
352 httpd_stop(raop->httpd);
353}
354