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
)
68 conn
= calloc(1, sizeof(raop_conn_t
));
73 conn
->raop_rtp
= NULL
;
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
]);
79 logger_log(&conn
->raop
->logger
, LOGGER_INFO
, "\n");
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
]);
84 logger_log(&conn
->raop
->logger
, LOGGER_INFO
, "\n");
86 conn
->local
= malloc(locallen
);
88 memcpy(conn
->local
, local
, locallen
);
90 conn
->remote
= malloc(remotelen
);
92 memcpy(conn
->remote
, remote
, remotelen
);
94 conn
->locallen
= locallen
;
95 conn
->remotelen
= remotelen
;
100 conn_request(void *ptr
, http_request_t
*request
, http_response_t
**response
)
102 raop_conn_t
*conn
= ptr
;
103 raop_t
*raop
= conn
->raop
;
105 http_response_t
*res
;
108 const char *challenge
;
110 method
= http_request_get_method(request
);
111 cseq
= http_request_get_header(request
, "CSeq");
112 if (!method
|| !cseq
) {
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");
120 challenge
= http_request_get_header(request
, "Apple-Challenge");
122 char signature
[MAX_SIGNATURE_LEN
];
124 memset(signature
, 0, sizeof(signature
));
125 rsakey_sign(raop
->rsakey
, signature
, sizeof(signature
), challenge
,
126 conn
->local
, conn
->locallen
, raop
->hwaddr
, raop
->hwaddrlen
);
127 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "Got signature: %s\n", signature
);
128 http_response_add_header(res
, "Apple-Response", signature
);
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")) {
136 unsigned char aeskey
[16];
137 unsigned char aesiv
[16];
138 int aeskeylen
, aesivlen
;
140 data
= http_request_get_data(request
, &datalen
);
142 sdp_t
*sdp
= sdp_init(data
, datalen
);
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
));
146 aeskeylen
= rsakey_decrypt(raop
->rsakey
, aeskey
, sizeof(aeskey
),
147 sdp_get_rsaaeskey(sdp
));
148 aesivlen
= rsakey_parseiv(raop
->rsakey
, aesiv
, sizeof(aesiv
),
150 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "aeskeylen: %d\n", aeskeylen
);
151 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "aesivlen: %d\n", aesivlen
);
153 conn
->raop_rtp
= raop_rtp_init(&raop
->logger
, &raop
->callbacks
, sdp_get_fmtp(sdp
), aeskey
, aesiv
);
156 } else if (!strcmp(method
, "SETUP")) {
157 unsigned short cport
=0, tport
=0, dport
=0;
158 const char *transport
;
162 transport
= http_request_get_header(request
, "Transport");
165 logger_log(&conn
->raop
->logger
, LOGGER_INFO
, "Transport: %s\n", transport
);
166 use_udp
= strncmp(transport
, "RTP/AVP/TCP", 11);
168 /* FIXME: Should use the parsed ports for resend */
169 raop_rtp_start(conn
->raop_rtp
, use_udp
, 1234, 1234, &cport
, &tport
, &dport
);
171 memset(buffer
, 0, sizeof(buffer
));
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
);
177 snprintf(buffer
, sizeof(buffer
)-1,
178 "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u",
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")) {
189 data
= http_request_get_data(request
, &datalen
);
190 datastr
= calloc(1, datalen
+1);
192 memcpy(datastr
, data
, datalen
);
193 if (!strncmp(datastr
, "volume: ", 8)) {
195 sscanf(data
+8, "%f", &vol
);
196 raop_rtp_set_volume(conn
->raop_rtp
, vol
);
199 } else if (!strcmp(method
, "FLUSH")) {
203 rtpinfo
= http_request_get_header(request
, "RTP-Info");
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);
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
;
217 http_response_finish(res
, NULL
, 0);
219 logger_log(&conn
->raop
->logger
, LOGGER_DEBUG
, "Got request %s with URL %s\n", method
, http_request_get_url(request
));
224 conn_destroy(void *ptr
)
226 raop_conn_t
*conn
= ptr
;
228 if (conn
->raop_rtp
) {
229 raop_rtp_destroy(conn
->raop_rtp
);
237 raop_init(raop_callbacks_t
*callbacks
, const char *pemkey
)
242 httpd_callbacks_t httpd_cbs
;
247 /* Initialize the network */
248 if (netutils_init() < 0) {
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
) {
259 /* Allocate the raop_t structure */
260 raop
= calloc(1, sizeof(raop_t
));
265 /* Initialize the logger */
266 logger_init(&raop
->logger
);
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
;
275 /* Initialize the http daemon */
276 httpd
= httpd_init(&raop
->logger
, &httpd_cbs
, 10, 1);
282 /* Copy callbacks structure */
283 memcpy(&raop
->callbacks
, callbacks
, sizeof(raop_callbacks_t
));
285 /* Initialize RSA key handler */
286 rsakey
= rsakey_init_pem(pemkey
);
294 raop
->rsakey
= rsakey
;
300 raop_init_from_keyfile(raop_callbacks_t
*callbacks
, const char *keyfile
)
305 if (utils_read_file(&pemstr
, keyfile
) < 0) {
308 raop
= raop_init(callbacks
, pemstr
);
314 raop_destroy(raop_t
*raop
)
319 httpd_destroy(raop
->httpd
);
320 rsakey_destroy(raop
->rsakey
);
323 /* Cleanup the network */
329 raop_start(raop_t
*raop
, unsigned short *port
, const char *hwaddr
, int hwaddrlen
)
335 /* Validate hardware address */
336 if (hwaddrlen
> MAX_HWADDR_LEN
) {
340 /* Copy hwaddr to the raop structure */
341 memcpy(raop
->hwaddr
, hwaddr
, hwaddrlen
);
342 raop
->hwaddrlen
= hwaddrlen
;
344 return httpd_start(raop
->httpd
, port
);
348 raop_stop(raop_t
*raop
)
352 httpd_stop(raop
->httpd
);