2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
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.
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.
31 /* Actually 345 bytes for 2048-bit key */
32 #define MAX_SIGNATURE_LEN 512
35 /* Callbacks for audio */
36 raop_callbacks_t callbacks
;
41 /* HTTP daemon and RSA key */
45 /* Hardware address information */
46 unsigned char hwaddr
[MAX_HWADDR_LEN
];
57 unsigned char *remote
;
60 typedef struct raop_conn_s raop_conn_t
;
63 conn_init(void *opaque
, unsigned char *local
, int locallen
, unsigned char *remote
, int remotelen
)
67 conn
= calloc(1, sizeof(raop_conn_t
));
72 conn
->raop_rtp
= NULL
;
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]);
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]);
95 conn
->local
= malloc(locallen
);
97 memcpy(conn
->local
, local
, locallen
);
99 conn
->remote
= malloc(remotelen
);
100 assert(conn
->remote
);
101 memcpy(conn
->remote
, remote
, remotelen
);
103 conn
->locallen
= locallen
;
104 conn
->remotelen
= remotelen
;
109 conn_request(void *ptr
, http_request_t
*request
, http_response_t
**response
)
111 raop_conn_t
*conn
= ptr
;
112 raop_t
*raop
= conn
->raop
;
114 http_response_t
*res
;
117 const char *challenge
;
119 method
= http_request_get_method(request
);
120 cseq
= http_request_get_header(request
, "CSeq");
121 if (!method
|| !cseq
) {
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");
129 challenge
= http_request_get_header(request
, "Apple-Challenge");
131 char signature
[MAX_SIGNATURE_LEN
];
133 memset(signature
, 0, sizeof(signature
));
134 rsakey_sign(raop
->rsakey
, signature
, sizeof(signature
), challenge
,
135 conn
->local
, conn
->locallen
, raop
->hwaddr
, raop
->hwaddrlen
);
136 http_response_add_header(res
, "Apple-Response", signature
);
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
);
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")) {
147 unsigned char aeskey
[16];
148 unsigned char aesiv
[16];
149 int aeskeylen
, aesivlen
;
151 data
= http_request_get_data(request
, &datalen
);
154 const char *remotestr
, *fmtpstr
, *aeskeystr
, *aesivstr
;
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
);
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
);
167 aeskeylen
= rsakey_decrypt(raop
->rsakey
, aeskey
, sizeof(aeskey
), aeskeystr
);
168 aesivlen
= rsakey_parseiv(raop
->rsakey
, aesiv
, sizeof(aesiv
), aesivstr
);
169 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "aeskeylen: %d\n", aeskeylen
);
170 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "aesivlen: %d\n", aesivlen
);
172 if (conn
->raop_rtp
) {
173 /* This should never happen */
174 raop_rtp_destroy(conn
->raop_rtp
);
175 conn
->raop_rtp
= NULL
;
177 conn
->raop_rtp
= raop_rtp_init(&raop
->logger
, &raop
->callbacks
, remotestr
, fmtpstr
, aeskey
, aesiv
);
180 } else if (!strcmp(method
, "SETUP")) {
181 unsigned short remote_cport
=0, remote_tport
=0;
182 unsigned short cport
=0, tport
=0, dport
=0;
183 const char *transport
;
187 transport
= http_request_get_header(request
, "Transport");
190 logger_log(&conn
->raop
->logger
, LOGGER_INFO
, "Transport: %s\n", transport
);
191 use_udp
= strncmp(transport
, "RTP/AVP/TCP", 11);
193 char *original
, *current
, *tmpstr
;
195 current
= original
= strdup(transport
);
197 while ((tmpstr
= utils_strsep(¤t
, ";")) != NULL
) {
198 unsigned short value
;
201 ret
= sscanf(tmpstr
, "control_port=%hu", &value
);
203 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "Found remote control port: %hu\n", value
);
204 remote_cport
= value
;
206 ret
= sscanf(tmpstr
, "timing_port=%hu", &value
);
208 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "Found remote timing port: %hu\n", value
);
209 remote_tport
= value
;
215 raop_rtp_start(conn
->raop_rtp
, use_udp
, remote_cport
, remote_tport
, &cport
, &tport
, &dport
);
217 memset(buffer
, 0, sizeof(buffer
));
219 snprintf(buffer
, sizeof(buffer
)-1,
220 "RTP/AVP/UDP;unicast;mode=record;timing_port=%hu;events;control_port=%hu;server_port=%hu",
221 tport
, cport
, dport
);
223 snprintf(buffer
, sizeof(buffer
)-1,
224 "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u",
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")) {
235 data
= http_request_get_data(request
, &datalen
);
236 datastr
= calloc(1, datalen
+1);
237 if (data
&& datastr
&& conn
->raop_rtp
) {
238 memcpy(datastr
, data
, datalen
);
239 if (!strncmp(datastr
, "volume: ", 8)) {
241 sscanf(data
+8, "%f", &vol
);
242 raop_rtp_set_volume(conn
->raop_rtp
, vol
);
245 } else if (!strcmp(method
, "FLUSH")) {
249 rtpinfo
= http_request_get_header(request
, "RTP-Info");
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);
256 if (conn
->raop_rtp
) {
257 raop_rtp_flush(conn
->raop_rtp
, next_seq
);
259 } else if (!strcmp(method
, "TEARDOWN")) {
260 http_response_add_header(res
, "Connection", "close");
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
;
268 http_response_finish(res
, NULL
, 0);
270 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "Got request %s with URL %s\n", method
, http_request_get_url(request
));
275 conn_destroy(void *ptr
)
277 raop_conn_t
*conn
= ptr
;
279 if (conn
->raop_rtp
) {
280 /* This is done in case TEARDOWN was not called */
281 raop_rtp_destroy(conn
->raop_rtp
);
289 raop_init(raop_callbacks_t
*callbacks
, const char *pemkey
)
294 httpd_callbacks_t httpd_cbs
;
299 /* Initialize the network */
300 if (netutils_init() < 0) {
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
) {
311 /* Allocate the raop_t structure */
312 raop
= calloc(1, sizeof(raop_t
));
317 /* Initialize the logger */
318 logger_init(&raop
->logger
);
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
;
327 /* Initialize the http daemon */
328 httpd
= httpd_init(&raop
->logger
, &httpd_cbs
, 10, 1);
334 /* Copy callbacks structure */
335 memcpy(&raop
->callbacks
, callbacks
, sizeof(raop_callbacks_t
));
337 /* Initialize RSA key handler */
338 rsakey
= rsakey_init_pem(pemkey
);
346 raop
->rsakey
= rsakey
;
352 raop_init_from_keyfile(raop_callbacks_t
*callbacks
, const char *keyfile
)
357 if (utils_read_file(&pemstr
, keyfile
) < 0) {
360 raop
= raop_init(callbacks
, pemstr
);
366 raop_destroy(raop_t
*raop
)
371 httpd_destroy(raop
->httpd
);
372 rsakey_destroy(raop
->rsakey
);
375 /* Cleanup the network */
381 raop_start(raop_t
*raop
, unsigned short *port
, const char *hwaddr
, int hwaddrlen
)
387 /* Validate hardware address */
388 if (hwaddrlen
> MAX_HWADDR_LEN
) {
392 /* Copy hwaddr to the raop structure */
393 memcpy(raop
->hwaddr
, hwaddr
, hwaddrlen
);
394 raop
->hwaddrlen
= hwaddrlen
;
396 return httpd_start(raop
->httpd
, port
);
400 raop_stop(raop_t
*raop
)
404 httpd_stop(raop
->httpd
);