Commit | Line | Data |
---|---|---|
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 | ||
34 | struct 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 | ||
50 | struct 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 | }; | |
60 | typedef struct raop_conn_s raop_conn_t; | |
61 | ||
62 | static void * | |
63 | conn_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 | ||
99 | static void | |
100 | conn_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 | ||
223 | static void | |
224 | conn_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 | ||
236 | raop_t * | |
237 | raop_init(raop_callbacks_t *callbacks, const char *pemkey, const char *hwaddr, int hwaddrlen) | |
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); | |
246 | assert(hwaddr); | |
247 | ||
248 | /* Initialize the network */ | |
249 | if (netutils_init() < 0) { | |
250 | return NULL; | |
251 | } | |
252 | ||
253 | /* Validate the callbacks structure */ | |
254 | if (!callbacks->audio_init || !callbacks->audio_set_volume || | |
255 | !callbacks->audio_process || !callbacks->audio_flush || | |
256 | !callbacks->audio_destroy) { | |
257 | return NULL; | |
258 | } | |
259 | ||
260 | /* Validate hardware address */ | |
261 | if (hwaddrlen > MAX_HWADDR_LEN) { | |
262 | return NULL; | |
263 | } | |
264 | ||
265 | /* Allocate the raop_t structure */ | |
266 | raop = calloc(1, sizeof(raop_t)); | |
267 | if (!raop) { | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | /* Initialize the logger */ | |
272 | logger_init(&raop->logger); | |
273 | ||
274 | /* Set HTTP callbacks to our handlers */ | |
275 | memset(&httpd_cbs, 0, sizeof(httpd_cbs)); | |
276 | httpd_cbs.opaque = raop; | |
277 | httpd_cbs.conn_init = &conn_init; | |
278 | httpd_cbs.conn_request = &conn_request; | |
279 | httpd_cbs.conn_destroy = &conn_destroy; | |
280 | ||
281 | /* Initialize the http daemon */ | |
282 | httpd = httpd_init(&raop->logger, &httpd_cbs, 10, 1); | |
283 | if (!httpd) { | |
284 | free(raop); | |
285 | return NULL; | |
286 | } | |
287 | ||
288 | /* Copy callbacks structure */ | |
289 | memcpy(&raop->callbacks, callbacks, sizeof(raop_callbacks_t)); | |
290 | ||
291 | /* Initialize RSA key handler */ | |
292 | rsakey = rsakey_init_pem(pemkey); | |
293 | if (!rsakey) { | |
294 | free(httpd); | |
295 | free(raop); | |
296 | return NULL; | |
297 | } | |
298 | ||
299 | raop->httpd = httpd; | |
300 | raop->rsakey = rsakey; | |
301 | ||
302 | /* Copy hwaddr to resulting structure */ | |
303 | memcpy(raop->hwaddr, hwaddr, hwaddrlen); | |
304 | raop->hwaddrlen = hwaddrlen; | |
305 | ||
306 | return raop; | |
307 | } | |
308 | ||
309 | raop_t * | |
310 | raop_init_from_keyfile(raop_callbacks_t *callbacks, const char *keyfile, const char *hwaddr, int hwaddrlen) | |
311 | { | |
312 | raop_t *raop; | |
313 | char *pemstr; | |
314 | ||
315 | if (utils_read_file(&pemstr, keyfile) < 0) { | |
316 | return NULL; | |
317 | } | |
318 | raop = raop_init(callbacks, pemstr, hwaddr, hwaddrlen); | |
319 | free(pemstr); | |
320 | return raop; | |
321 | } | |
322 | ||
323 | void | |
324 | raop_destroy(raop_t *raop) | |
325 | { | |
326 | if (raop) { | |
327 | raop_stop(raop); | |
328 | ||
329 | httpd_destroy(raop->httpd); | |
330 | rsakey_destroy(raop->rsakey); | |
331 | free(raop); | |
332 | ||
333 | /* Cleanup the network */ | |
334 | netutils_cleanup(); | |
335 | } | |
336 | } | |
337 | ||
338 | int | |
339 | raop_start(raop_t *raop, unsigned short *port) | |
340 | { | |
341 | assert(raop); | |
342 | assert(port); | |
343 | ||
344 | return httpd_start(raop->httpd, port); | |
345 | } | |
346 | ||
347 | void | |
348 | raop_stop(raop_t *raop) | |
349 | { | |
350 | assert(raop); | |
351 | ||
352 | httpd_stop(raop->httpd); | |
353 | } | |
354 |