فهرست منبع

memory leak in AirPlay

philippe44 5 سال پیش
والد
کامیت
8a0763cc27
4فایلهای تغییر یافته به همراه30 افزوده شده و 29 حذف شده
  1. 1 0
      components/io/led.c
  2. 19 20
      components/raop/raop.c
  3. 8 7
      components/raop/rtp.c
  4. 2 2
      components/raop/util.c

+ 1 - 0
components/io/led.c

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

+ 19 - 20
components/raop/raop.c

@@ -102,10 +102,10 @@ struct raop_ctx_s *raop_create(struct in_addr host, char *name,
 						raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb) {
 	struct raop_ctx_s *ctx = malloc(sizeof(struct raop_ctx_s));
 	struct sockaddr_in addr;
-	char id[64];
+	char id[64];
 
#ifdef WIN32
-
	socklen_t nlen = sizeof(struct sockaddr);
-
	char *txt[] = { "am=esp32", "tp=UDP", "sm=false", "sv=false", "ek=1",
+	socklen_t nlen = sizeof(struct sockaddr);
+	char *txt[] = { "am=esp32", "tp=UDP", "sm=false", "sv=false", "ek=1",
 					"et=0,1", "md=0,1,2", "cn=0,1", "ch=2",
 					"ss=16", "sr=44100", "vn=3", "txtvers=1",
 					NULL };
@@ -167,21 +167,20 @@ struct raop_ctx_s *raop_create(struct in_addr host, char *name,
 #ifdef WIN32
 	getsockname(ctx->sock, (struct sockaddr *) &addr, &nlen);
 	ctx->port = ntohs(addr.sin_port);
-
#endif
-
+#endif
 
	ctx->running = true;
 
	memcpy(ctx->mac, mac, 6);
-
	snprintf(id, 64, "%02X%02X%02X%02X%02X%02X@%s",  mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], name);
+	snprintf(id, 64, "%02X%02X%02X%02X%02X%02X@%s",  mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], name);
 
#ifdef WIN32
-
	// seems that Windows snprintf does not add NULL char if actual size > max
-
	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);
+	// seems that Windows snprintf does not add NULL char if actual size > max
+	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);
 
#else
-
	LOG_INFO("starting mDNS with %s", id);
-
	ESP_ERROR_CHECK( mdns_service_add(id, "_raop", "_tcp", ctx->port, txt, sizeof(txt) / sizeof(mdns_txt_item_t)) );
-
	xTaskCreate((TaskFunction_t) rtsp_thread, "RTSP_thread", 8*1024, ctx, ESP_TASK_PRIO_MIN + 1, &ctx->thread);
-
#endif
+	LOG_INFO("starting mDNS with %s", id);
+	ESP_ERROR_CHECK( mdns_service_add(id, "_raop", "_tcp", ctx->port, txt, sizeof(txt) / sizeof(mdns_txt_item_t)) );
+	xTaskCreate((TaskFunction_t) rtsp_thread, "RTSP_thread", 8*1024, ctx, ESP_TASK_PRIO_MIN + 1, &ctx->thread);
+#endif
 
 	return ctx;
 }
@@ -366,13 +365,13 @@ static bool handle_rtsp(raop_ctx_t *ctx, int sock)
 	key_data_t headers[16], resp[8] = { {NULL, NULL} };
 	int len;
 	bool success = true;
-
+	
 	if (!http_parse(sock, method, headers, &body, &len)) {
 		NFREE(body);
 		kd_free(headers);
 		return false;
 	}
-
+	
 	if (strcmp(method, "OPTIONS")) {
 		LOG_INFO("[%p]: received %s", ctx, method);
 	}
@@ -402,7 +401,7 @@ static bool handle_rtsp(raop_ctx_t *ctx, int sock)
 		NFREE(buf_pad);
 		NFREE(data_b64);
 	}
-
+	
 	if (!strcmp(method, "OPTIONS")) {
 
 		kd_add(resp, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER");
@@ -467,9 +466,9 @@ static bool handle_rtsp(raop_ctx_t *ctx, int sock)
 
 		rtp = rtp_init(ctx->peer, ctx->latency,	ctx->rtsp.aeskey, ctx->rtsp.aesiv,
 					   ctx->rtsp.fmtp, cport, tport, ctx->cmd_cb, ctx->data_cb);
-
+						
 		ctx->rtp = rtp.ctx;
-
+		
 		if (cport * tport * rtp.cport * rtp.tport * rtp.aport && rtp.ctx) {
 			char *transport;
 			asprintf(&transport, "RTP/AVP/UDP;unicast;mode=record;control_port=%u;timing_port=%u;server_port=%u", rtp.cport, rtp.tport, rtp.aport);
@@ -672,7 +671,7 @@ static char *rsa_apply(unsigned char *input, int inlen, int *outlen, int mode)
 	mbedtls_pk_context pkctx;
 	mbedtls_rsa_context *trsa;
 	size_t olen;
-
+	
 	/*
 	we should do entropy initialization & pass a rng function but this
 	consumes a ton of stack and there is no security concern here. Anyway,

+ 8 - 7
components/raop/rtp.c

@@ -80,7 +80,7 @@
 
 #define RESEND_TO	200
 
-enum { DATA, CONTROL, TIMING };
+enum { DATA = 0, CONTROL, TIMING };
 
 static const u8_t silence_frame[MAX_PACKET] = { 0 };
 
@@ -159,7 +159,7 @@ static int	  	seq_order(seq_t a, seq_t b);
 static alac_file* alac_init(int fmtp[32]) {
 	alac_file *alac;
 	int sample_size = fmtp[3];
-
+	
 	if (sample_size != 16) {
 		LOG_ERROR("sample size must be 16 %d", sample_size);
 		return false;
@@ -257,7 +257,7 @@ rtp_resp_t rtp_init(struct in_addr host, int latency, char *aeskey, char *aesiv,
 	resp.cport = ctx->rtp_sockets[CONTROL].lport;
 	resp.tport = ctx->rtp_sockets[TIMING].lport;
 	resp.aport = ctx->rtp_sockets[DATA].lport;
-
+	
 	if (rc) {
 		ctx->running = true;
 #ifdef WIN32
@@ -291,13 +291,14 @@ void rtp_end(rtp_t *ctx)
 		xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
 #endif
 	}
-
-	for (i = 0; i < 3; i++) shutdown_socket(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);
+	
 	free(ctx);
 
 #ifdef __RTP_STORE
@@ -374,7 +375,7 @@ static void alac_decode(rtp_t *ctx, s16_t *dest, char *buf, int len, int *outsiz
 	unsigned char iv[16];
 	int aeslen;
 	assert(len<=MAX_PACKET);
-
+	
 	if (ctx->decrypt) {
 		aeslen = len & ~0xf;
 		memcpy(iv, ctx->aesiv, sizeof(iv));

+ 2 - 2
components/raop/util.c

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