Add a clarifying comment to the last commit
[deb_shairplay.git] / src / lib / raop.c
index 10c2f12b687772a9d1ebc86fa9442723e144c4d2..0c640b5bb67d85535607f078b148ce0dcb2f0358 100644 (file)
@@ -123,6 +123,7 @@ conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remot
 static void
 conn_request(void *ptr, http_request_t *request, http_response_t **response)
 {
+       const char realm[] = "airplay";
        raop_conn_t *conn = ptr;
        raop_t *raop = conn->raop;
 
@@ -139,7 +140,9 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
        }
 
        res = http_response_init("RTSP/1.0", 200, "OK");
-       if (strlen(raop->password)) {
+
+       /* We need authorization for everything else than OPTIONS request */
+       if (strcmp(method, "OPTIONS") != 0 && strlen(raop->password)) {
                const char *authorization;
 
                authorization = http_request_get_header(request, "Authorization");
@@ -147,17 +150,19 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                        logger_log(conn->raop->logger, LOGGER_DEBUG, "Our nonce: %s", conn->nonce);
                        logger_log(conn->raop->logger, LOGGER_DEBUG, "Authorization: %s", authorization);
                }
-               if (!digest_is_valid("AppleTV", raop->password, conn->nonce, method, http_request_get_url(request), authorization)) {
+               if (!digest_is_valid(realm, raop->password, conn->nonce, method, http_request_get_url(request), authorization)) {
                        char *authstr;
                        int authstrlen;
 
                        /* Allocate the authenticate string */
-                       authstrlen = sizeof("Digest realm=\"AppleTV\", nonce=\"\"") + sizeof(conn->nonce) + 1;
+                       authstrlen = sizeof("Digest realm=\"\", nonce=\"\"") + sizeof(realm) + sizeof(conn->nonce) + 1;
                        authstr = malloc(authstrlen);
 
                        /* Concatenate the authenticate string */
                        memset(authstr, 0, authstrlen);
-                       strcat(authstr, "Digest realm=\"AppleTV\", nonce=\"");
+                       strcat(authstr, "Digest realm=\"");
+                       strcat(authstr, realm);
+                       strcat(authstr, "\", nonce=\"");
                        strcat(authstr, conn->nonce);
                        strcat(authstr, "\"");
 
@@ -167,8 +172,9 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                        res = http_response_init("RTSP/1.0", 401, "Unauthorized");
                        http_response_add_header(res, "WWW-Authenticate", authstr);
                        free(authstr);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "Authentication unsuccessful, sending Unauthorized");
                } else {
-                       logger_log(conn->raop->logger, LOGGER_DEBUG, "AUTHENTICATION SUCCESS!");
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "Authentication successful!");
                }
        }
 
@@ -229,6 +235,10 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                                conn->raop_rtp = NULL;
                        }
                        conn->raop_rtp = raop_rtp_init(raop->logger, &raop->callbacks, remotestr, rtpmapstr, fmtpstr, aeskey, aesiv);
+                       if (!conn->raop_rtp) {
+                               logger_log(conn->raop->logger, LOGGER_ERR, "Error initializing the audio decoder");
+                               http_response_set_disconnect(res, 1);
+                       }
                        sdp_destroy(sdp);
                }
        } else if (!strcmp(method, "SETUP")) {
@@ -266,7 +276,12 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                        }
                        free(original);
                }
-               raop_rtp_start(conn->raop_rtp, use_udp, remote_cport, remote_tport, &cport, &tport, &dport);
+               if (conn->raop_rtp) {
+                       raop_rtp_start(conn->raop_rtp, use_udp, remote_cport, remote_tport, &cport, &tport, &dport);
+               } else {
+                       logger_log(conn->raop->logger, LOGGER_ERR, "RAOP not initialized at SETUP, playing will fail!");
+                       http_response_set_disconnect(res, 1);
+               }
 
                memset(buffer, 0, sizeof(buffer));
                if (use_udp) {
@@ -298,14 +313,24 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                                        sscanf(datastr+8, "%f", &vol);
                                        raop_rtp_set_volume(conn->raop_rtp, vol);
                                }
+                       } else if (!conn->raop_rtp) {
+                               logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER volume");
                        }
                        free(datastr);
                } else if (!strcmp(content_type, "image/jpeg")) {
                        logger_log(conn->raop->logger, LOGGER_INFO, "Got image data of %d bytes", datalen);
-                       raop_rtp_set_coverart(conn->raop_rtp, data, datalen);
+                       if (conn->raop_rtp) {
+                               raop_rtp_set_coverart(conn->raop_rtp, data, datalen);
+                       } else {
+                               logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER coverart");
+                       }
                } else if (!strcmp(content_type, "application/x-dmap-tagged")) {
                        logger_log(conn->raop->logger, LOGGER_INFO, "Got metadata of %d bytes", datalen);
-                       raop_rtp_set_metadata(conn->raop_rtp, data, datalen);
+                       if (conn->raop_rtp) {
+                               raop_rtp_set_metadata(conn->raop_rtp, data, datalen);
+                       } else {
+                               logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER metadata");
+                       }
                }
        } else if (!strcmp(method, "FLUSH")) {
                const char *rtpinfo;
@@ -320,6 +345,8 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                }
                if (conn->raop_rtp) {
                        raop_rtp_flush(conn->raop_rtp, next_seq);
+               } else {
+                       logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at FLUSH");
                }
        } else if (!strcmp(method, "TEARDOWN")) {
                http_response_add_header(res, "Connection", "close");
@@ -332,7 +359,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
        }
        http_response_finish(res, NULL, 0);
 
-       logger_log(conn->raop->logger, LOGGER_DEBUG, "Got request %s with URL %s", method, http_request_get_url(request));
+       logger_log(conn->raop->logger, LOGGER_DEBUG, "Handled request %s with URL %s", method, http_request_get_url(request));
        *response = res;
 }