memory leak in AirPlay

This commit is contained in:
philippe44
2019-08-23 00:23:00 -07:00
parent 0ee2783b49
commit 8a0763cc27
4 changed files with 30 additions and 29 deletions

View File

@@ -109,6 +109,7 @@ bool led_unconfig(int idx) {
if (idx >= MAX_LED) return false; if (idx >= MAX_LED) return false;
if (leds[idx].timer) xTimerDelete(leds[idx].timer, BLOCKTIME); if (leds[idx].timer) xTimerDelete(leds[idx].timer, BLOCKTIME);
leds[idx].timer = NULL;
return true; return true;
} }

View File

@@ -102,10 +102,10 @@ struct raop_ctx_s *raop_create(struct in_addr host, char *name,
struct raop_ctx_s *raop_create(struct in_addr host, char *name, struct raop_ctx_s *raop_create(struct in_addr host, char *name,
unsigned char mac[6], int latency, unsigned char mac[6], int latency,
raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb) { raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb) {
struct raop_ctx_s *ctx = malloc(sizeof(struct raop_ctx_s)); struct raop_ctx_s *ctx = malloc(sizeof(struct raop_ctx_s));
struct sockaddr_in addr; struct sockaddr_in addr;
char id[64]; char id[64];
#ifdef WIN32 #ifdef WIN32
socklen_t nlen = sizeof(struct sockaddr); socklen_t nlen = sizeof(struct sockaddr);
char *txt[] = { "am=esp32", "tp=UDP", "sm=false", "sv=false", "ek=1", char *txt[] = { "am=esp32", "tp=UDP", "sm=false", "sv=false", "ek=1",
@@ -167,21 +167,20 @@ struct raop_ctx_s *raop_create(struct in_addr host, char *name,
return NULL; return NULL;
} }
} #ifdef WIN32
getsockname(ctx->sock, (struct sockaddr *) &addr, &nlen); getsockname(ctx->sock, (struct sockaddr *) &addr, &nlen);
ctx->port = ntohs(addr.sin_port); ctx->port = ntohs(addr.sin_port);
ctx->port = ntohs(addr.sin_port); #endif
#endif ctx->running = true;
memcpy(ctx->mac, mac, 6);
ctx->running = true; snprintf(id, 64, "%02X%02X%02X%02X%02X%02X@%s", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], name);
memcpy(ctx->mac, mac, 6); #ifdef WIN32
// seems that Windows snprintf does not add NULL char if actual size > max
snprintf(id, 64, "%02X%02X%02X%02X%02X%02X@%s", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], name); id[63] = '\0';
ctx->svc = mdnsd_register_svc(ctx->svr, id, "_raop._tcp.local", ctx->port, NULL, (const char**) txt);
pthread_create(&ctx->thread, NULL, &rtsp_thread, ctx); pthread_create(&ctx->thread, NULL, &rtsp_thread, ctx);
#else #else
@@ -366,13 +365,13 @@ static bool handle_rtsp(raop_ctx_t *ctx, int sock)
#endif #endif
return NULL; return NULL;
sock = -1; }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
static bool handle_rtsp(raop_ctx_t *ctx, int sock) static bool handle_rtsp(raop_ctx_t *ctx, int sock)
{ {
#ifndef WIN32 char *buf = NULL, *body = NULL, method[16] = "";
key_data_t headers[16], resp[8] = { {NULL, NULL} }; key_data_t headers[16], resp[8] = { {NULL, NULL} };
int len; int len;
bool success = true; bool success = true;
@@ -402,7 +401,7 @@ static bool handle_rtsp(raop_ctx_t *ctx, int sock)
memset(p, 0, 32 - (p - data)); memset(p, 0, 32 - (p - data));
p = rsa_apply((unsigned char*) data, 32, &n, RSA_MODE_AUTH); p = rsa_apply((unsigned char*) data, 32, &n, RSA_MODE_AUTH);
n = base64_encode(p, n, &data_b64); n = base64_encode(p, n, &data_b64);
// remove padding as well (seems to be optional now) // remove padding as well (seems to be optional now)
for (n = strlen(data_b64) - 1; n > 0 && data_b64[n] == '='; data_b64[n--] = '\0'); for (n = strlen(data_b64) - 1; n > 0 && data_b64[n] == '='; data_b64[n--] = '\0');
@@ -467,9 +466,9 @@ static bool handle_rtsp(raop_ctx_t *ctx, int sock)
} else if (!strcmp(method, "SETUP") && ((buf = kd_lookup(headers, "Transport")) != NULL)) { } else if (!strcmp(method, "SETUP") && ((buf = kd_lookup(headers, "Transport")) != NULL)) {
char *p; char *p;
rtp_resp_t rtp = { 0 }; rtp_resp_t rtp = { 0 };
// on announce, search remote short unsigned tport = 0, cport = 0;
if ((buf = kd_lookup(headers, "DACP-ID")) != NULL) strcpy(ctx->active_remote.DACPid, buf); // we are about to stream, do something if needed
ctx->cmd_cb(RAOP_SETUP, NULL); ctx->cmd_cb(RAOP_SETUP, NULL);
if ((p = strcasestr(buf, "timing_port")) != NULL) sscanf(p, "%*[^=]=%hu", &tport); if ((p = strcasestr(buf, "timing_port")) != NULL) sscanf(p, "%*[^=]=%hu", &tport);
@@ -672,7 +671,7 @@ static char *rsa_apply(unsigned char *input, int inlen, int *outlen, int mode)
RSA_PKCS1_PADDING); RSA_PKCS1_PADDING);
break; break;
case RSA_MODE_KEY: case RSA_MODE_KEY:
*outlen = RSA_private_decrypt(inlen, input, out, rsa,
RSA_PKCS1_OAEP_PADDING); RSA_PKCS1_OAEP_PADDING);
break; break;
} }

View File

@@ -80,7 +80,7 @@
#define RTP_SYNC (0x01) #define RTP_SYNC (0x01)
#define NTP_SYNC (0x02) #define NTP_SYNC (0x02)
#define RESEND_TO 200 #define RESEND_TO 200
enum { DATA = 0, CONTROL, TIMING }; enum { DATA = 0, CONTROL, TIMING };
@@ -159,7 +159,7 @@ static int seq_order(seq_t a, seq_t b);
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static alac_file* alac_init(int fmtp[32]) { static alac_file* alac_init(int fmtp[32]) {
alac_file *alac; alac_file *alac;
int sample_size = fmtp[3]; int sample_size = fmtp[3];
if (sample_size != 16) { if (sample_size != 16) {
@@ -257,7 +257,7 @@ rtp_resp_t rtp_init(struct in_addr host, int latency, char *aeskey, char *aesiv,
// create http port and start listening // create http port and start listening
resp.cport = ctx->rtp_sockets[CONTROL].lport; resp.cport = ctx->rtp_sockets[CONTROL].lport;
resp.tport = ctx->rtp_sockets[TIMING].lport; resp.tport = ctx->rtp_sockets[TIMING].lport;
resp.aport = ctx->rtp_sockets[DATA].lport; resp.aport = ctx->rtp_sockets[DATA].lport;
if (rc) { if (rc) {
@@ -291,13 +291,14 @@ void rtp_end(rtp_t *ctx)
#else #else
ctx->joiner = xTaskGetCurrentTaskHandle(); ctx->joiner = xTaskGetCurrentTaskHandle();
xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
#endif #endif
}
} }
for (i = 0; i < 3; i++) closesocket(ctx->rtp_sockets[i].sock); for (i = 0; i < 3; i++) closesocket(ctx->rtp_sockets[i].sock);
delete_alac(ctx->alac_codec);
if (ctx->decrypt_buf) free(ctx->decrypt_buf);
pthread_mutex_destroy(&ctx->ab_mutex);
buffer_release(ctx->audio_buffer); buffer_release(ctx->audio_buffer);
free(ctx); free(ctx);
@@ -374,7 +375,7 @@ static void alac_decode(rtp_t *ctx, s16_t *dest, char *buf, int len, int *outsiz
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void alac_decode(rtp_t *ctx, s16_t *dest, char *buf, int len, int *outsize) { static void alac_decode(rtp_t *ctx, s16_t *dest, char *buf, int len, int *outsize) {
unsigned char iv[16]; unsigned char iv[16];
int aeslen; int aeslen;
assert(len<=MAX_PACKET); assert(len<=MAX_PACKET);
if (ctx->decrypt) { if (ctx->decrypt) {

View File

@@ -377,7 +377,7 @@ bool http_parse(int sock, char *method, key_data_t *rkd, char **body, int *len)
// line folding should be deprecated // line folding should be deprecated
if (i && rkd[i].key && (line[0] == ' ' || line[0] == '\t')) { if (i && rkd[i].key && (line[0] == ' ' || line[0] == '\t')) {
for(j = 0; j < strlen(line); j++) if (line[j] != ' ' && line[j] != '\t') break; for(j = 0; j < strlen(line); j++) if (line[j] != ' ' && line[j] != '\t') break;
rkd[i].data = realloc(rkd[i].data, strlen(rkd[i].data) + strlen(line + j) + 1); rkd[i].data = realloc(rkd[i].data, strlen(rkd[i].data) + strlen(line + j) + 1);
strcat(rkd[i].data, line + j); strcat(rkd[i].data, line + j);
continue; continue;
} }
@@ -403,7 +403,7 @@ bool http_parse(int sock, char *method, key_data_t *rkd, char **body, int *len)
if (*len) { if (*len) {
int size = 0; int size = 0;
*body = malloc(*len + 1); *body = malloc(*len + 1);
while (*body && size < *len) { while (*body && size < *len) {
int bytes = recv(sock, *body + size, *len - size, 0); int bytes = recv(sock, *body + size, *len - size, 0);
if (bytes <= 0) break; if (bytes <= 0) break;