15
15
#include "mdns.h"
16
16
#include "mdns_private.h"
17
17
#include "mdns_networking.h"
18
+ #include "esp_log.h"
18
19
#include <string.h>
19
20
20
21
#ifdef MDNS_ENABLE_DEBUG
@@ -26,6 +27,8 @@ static const char * MDNS_SUB_STR = "_sub";
26
27
27
28
mdns_server_t * _mdns_server = NULL ;
28
29
30
+ static const char * TAG = "MDNS" ;
31
+
29
32
static volatile TaskHandle_t _mdns_service_task_handle = NULL ;
30
33
static SemaphoreHandle_t _mdns_service_semaphore = NULL ;
31
34
@@ -63,11 +66,16 @@ static char * _mdns_mangle_name(char* in) {
63
66
//need to add -2 to string
64
67
ret = malloc (strlen (in ) + 3 );
65
68
if (ret == NULL ) {
69
+ HOOK_MALLOC_FAILED ;
66
70
return NULL ;
67
71
}
68
72
sprintf (ret , "%s-2" , in );
69
73
} else {
70
74
ret = malloc (strlen (in ) + 2 ); //one extra byte in case 9-10 or 99-100 etc
75
+ if (ret == NULL ) {
76
+ HOOK_MALLOC_FAILED ;
77
+ return NULL ;
78
+ }
71
79
strcpy (ret , in );
72
80
int baseLen = p - in ; //length of 'bla' in 'bla-123'
73
81
//overwrite suffix with new suffix
@@ -117,6 +125,7 @@ esp_err_t _mdns_send_rx_action(mdns_rx_packet_t * packet)
117
125
118
126
action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
119
127
if (!action ) {
128
+ HOOK_MALLOC_FAILED ;
120
129
return ESP_ERR_NO_MEM ;
121
130
}
122
131
@@ -557,6 +566,9 @@ static uint16_t _mdns_append_txt_record(uint8_t * packet, uint16_t * index, mdns
557
566
return 0 ;
558
567
}
559
568
data_len += l ;
569
+ } else {
570
+ HOOK_MALLOC_FAILED ;
571
+ // continue
560
572
}
561
573
txt = txt -> next ;
562
574
}
@@ -1130,6 +1142,7 @@ static bool _mdns_alloc_answer(mdns_out_answer_t ** destnation, uint16_t type, m
1130
1142
1131
1143
mdns_out_answer_t * a = (mdns_out_answer_t * )malloc (sizeof (mdns_out_answer_t ));
1132
1144
if (!a ) {
1145
+ HOOK_MALLOC_FAILED ;
1133
1146
return false;
1134
1147
}
1135
1148
a -> type = type ;
@@ -1148,6 +1161,7 @@ static mdns_tx_packet_t * _mdns_alloc_packet_default(tcpip_adapter_if_t tcpip_if
1148
1161
{
1149
1162
mdns_tx_packet_t * packet = (mdns_tx_packet_t * )malloc (sizeof (mdns_tx_packet_t ));
1150
1163
if (!packet ) {
1164
+ HOOK_MALLOC_FAILED ;
1151
1165
return NULL ;
1152
1166
}
1153
1167
memset ((uint8_t * )packet , 0 , sizeof (mdns_tx_packet_t ));
@@ -1285,6 +1299,7 @@ static mdns_tx_packet_t * _mdns_create_probe_packet(tcpip_adapter_if_t tcpip_if,
1285
1299
for (i = 0 ; i < len ; i ++ ) {
1286
1300
mdns_out_question_t * q = (mdns_out_question_t * )malloc (sizeof (mdns_out_question_t ));
1287
1301
if (!q ) {
1302
+ HOOK_MALLOC_FAILED ;
1288
1303
_mdns_free_tx_packet (packet );
1289
1304
return NULL ;
1290
1305
}
@@ -1310,6 +1325,7 @@ static mdns_tx_packet_t * _mdns_create_probe_packet(tcpip_adapter_if_t tcpip_if,
1310
1325
if (include_ip && !_str_null_or_empty (_mdns_server -> hostname )) {
1311
1326
mdns_out_question_t * q = (mdns_out_question_t * )malloc (sizeof (mdns_out_question_t ));
1312
1327
if (!q ) {
1328
+ HOOK_MALLOC_FAILED ;
1313
1329
_mdns_free_tx_packet (packet );
1314
1330
return NULL ;
1315
1331
}
@@ -1457,6 +1473,7 @@ static void _mdns_init_pcb_probe(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t
1457
1473
if (services_final_len ) {
1458
1474
_services = (mdns_srv_item_t * * )malloc (sizeof (mdns_srv_item_t * ) * services_final_len );
1459
1475
if (!_services ) {
1476
+ HOOK_MALLOC_FAILED ;
1460
1477
return ;
1461
1478
}
1462
1479
@@ -1737,6 +1754,7 @@ static mdns_txt_linked_item_t * _mdns_allocate_txt(size_t num_items, mdns_txt_it
1737
1754
for (i = 0 ; i < num_items ; i ++ ) {
1738
1755
mdns_txt_linked_item_t * new_item = (mdns_txt_linked_item_t * )malloc (sizeof (mdns_txt_linked_item_t ));
1739
1756
if (!new_item ) {
1757
+ HOOK_MALLOC_FAILED ;
1740
1758
break ;
1741
1759
}
1742
1760
new_item -> key = strdup (txt [i ].key );
@@ -1783,6 +1801,7 @@ static mdns_service_t * _mdns_create_service(const char * service, const char *
1783
1801
{
1784
1802
mdns_service_t * s = (mdns_service_t * )malloc (sizeof (mdns_service_t ));
1785
1803
if (!s ) {
1804
+ HOOK_MALLOC_FAILED ;
1786
1805
return NULL ;
1787
1806
}
1788
1807
@@ -2046,6 +2065,9 @@ static int _mdns_check_txt_collision(mdns_service_t * service, const uint8_t * d
2046
2065
sprintf (tmp , "%s=%s" , txt -> key , txt -> value );
2047
2066
_mdns_append_string (ours , & index , tmp );
2048
2067
free (tmp );
2068
+ } else {
2069
+ HOOK_MALLOC_FAILED ;
2070
+ // continue
2049
2071
}
2050
2072
txt = txt -> next ;
2051
2073
}
@@ -2402,6 +2424,10 @@ static void _mdns_result_txt_create(const uint8_t * data, size_t len, mdns_txt_i
2402
2424
}
2403
2425
2404
2426
mdns_txt_item_t * txt = (mdns_txt_item_t * )malloc (sizeof (mdns_txt_item_t ) * num_items );
2427
+ if (!txt ) {
2428
+ HOOK_MALLOC_FAILED ;
2429
+ return ;
2430
+ }
2405
2431
memset (txt , 0 , sizeof (mdns_txt_item_t ) * num_items );
2406
2432
size_t txt_num = 0 ;
2407
2433
@@ -2422,6 +2448,7 @@ static void _mdns_result_txt_create(const uint8_t * data, size_t len, mdns_txt_i
2422
2448
}
2423
2449
char * key = (char * )malloc (name_len + 1 );
2424
2450
if (!key ) {
2451
+ HOOK_MALLOC_FAILED ;
2425
2452
goto handle_error ;//error
2426
2453
}
2427
2454
@@ -2435,6 +2462,10 @@ static void _mdns_result_txt_create(const uint8_t * data, size_t len, mdns_txt_i
2435
2462
int value_len = partLen - name_len - 1 ;
2436
2463
if (value_len > 0 ) {
2437
2464
char * value = (char * )malloc (value_len + 1 );
2465
+ if (!value ) {
2466
+ HOOK_MALLOC_FAILED ;
2467
+ goto handle_error ;//error
2468
+ }
2438
2469
memcpy (value , data + i , value_len );
2439
2470
value [value_len ] = 0 ;
2440
2471
i += value_len ;
@@ -2498,6 +2529,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
2498
2529
2499
2530
mdns_parsed_packet_t * parsed_packet = (mdns_parsed_packet_t * )malloc (sizeof (mdns_parsed_packet_t ));
2500
2531
if (!parsed_packet ) {
2532
+ HOOK_MALLOC_FAILED ;
2501
2533
return ;
2502
2534
}
2503
2535
memset (parsed_packet , 0 , sizeof (mdns_parsed_packet_t ));
@@ -2560,6 +2592,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
2560
2592
while (a ) {
2561
2593
mdns_parsed_question_t * question = (mdns_parsed_question_t * )malloc (sizeof (mdns_parsed_question_t ));
2562
2594
if (!question ) {
2595
+ HOOK_MALLOC_FAILED ;
2563
2596
goto clear_rx_packet ;
2564
2597
}
2565
2598
question -> next = parsed_packet -> questions ;
@@ -2587,6 +2620,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
2587
2620
2588
2621
mdns_parsed_question_t * question = (mdns_parsed_question_t * )malloc (sizeof (mdns_parsed_question_t ));
2589
2622
if (!question ) {
2623
+ HOOK_MALLOC_FAILED ;
2590
2624
goto clear_rx_packet ;
2591
2625
}
2592
2626
question -> next = parsed_packet -> questions ;
@@ -3046,6 +3080,7 @@ static mdns_search_once_t * _mdns_search_init(const char * name, const char * se
3046
3080
{
3047
3081
mdns_search_once_t * search = (mdns_search_once_t * )malloc (sizeof (mdns_search_once_t ));
3048
3082
if (!search ) {
3083
+ HOOK_MALLOC_FAILED ;
3049
3084
return NULL ;
3050
3085
}
3051
3086
memset (search , 0 , sizeof (mdns_search_once_t ));
@@ -3137,6 +3172,7 @@ static mdns_ip_addr_t * _mdns_result_addr_create_ip(ip_addr_t * ip)
3137
3172
{
3138
3173
mdns_ip_addr_t * a = (mdns_ip_addr_t * )malloc (sizeof (mdns_ip_addr_t ));
3139
3174
if (!a ) {
3175
+ HOOK_MALLOC_FAILED ;
3140
3176
return NULL ;
3141
3177
}
3142
3178
memset (a , 0 , sizeof (mdns_ip_addr_t ));
@@ -3196,6 +3232,7 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char *
3196
3232
if (!search -> max_results || search -> num_results < search -> max_results ) {
3197
3233
r = (mdns_result_t * )malloc (sizeof (mdns_result_t ));
3198
3234
if (!r ) {
3235
+ HOOK_MALLOC_FAILED ;
3199
3236
return ;
3200
3237
}
3201
3238
@@ -3241,6 +3278,7 @@ static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search,
3241
3278
if (!search -> max_results || search -> num_results < search -> max_results ) {
3242
3279
r = (mdns_result_t * )malloc (sizeof (mdns_result_t ));
3243
3280
if (!r ) {
3281
+ HOOK_MALLOC_FAILED ;
3244
3282
return NULL ;
3245
3283
}
3246
3284
@@ -3276,6 +3314,7 @@ static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char
3276
3314
if (!search -> max_results || search -> num_results < search -> max_results ) {
3277
3315
r = (mdns_result_t * )malloc (sizeof (mdns_result_t ));
3278
3316
if (!r ) {
3317
+ HOOK_MALLOC_FAILED ;
3279
3318
return ;
3280
3319
}
3281
3320
@@ -3315,6 +3354,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
3315
3354
if (!search -> max_results || search -> num_results < search -> max_results ) {
3316
3355
r = (mdns_result_t * )malloc (sizeof (mdns_result_t ));
3317
3356
if (!r ) {
3357
+ HOOK_MALLOC_FAILED ;
3318
3358
goto free_txt ;
3319
3359
}
3320
3360
@@ -3420,6 +3460,7 @@ static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search
3420
3460
3421
3461
mdns_out_question_t * q = (mdns_out_question_t * )malloc (sizeof (mdns_out_question_t ));
3422
3462
if (!q ) {
3463
+ HOOK_MALLOC_FAILED ;
3423
3464
_mdns_free_tx_packet (packet );
3424
3465
return NULL ;
3425
3466
}
@@ -3442,6 +3483,7 @@ static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search
3442
3483
}
3443
3484
mdns_out_answer_t * a = (mdns_out_answer_t * )malloc (sizeof (mdns_out_answer_t ));
3444
3485
if (!a ) {
3486
+ HOOK_MALLOC_FAILED ;
3445
3487
_mdns_free_tx_packet (packet );
3446
3488
return NULL ;
3447
3489
}
@@ -3687,6 +3729,7 @@ static void _mdns_execute_action(mdns_action_t * action)
3687
3729
if (!txt ) {
3688
3730
txt = (mdns_txt_linked_item_t * )malloc (sizeof (mdns_txt_linked_item_t ));
3689
3731
if (!txt ) {
3732
+ HOOK_MALLOC_FAILED ;
3690
3733
_mdns_free_action (action );
3691
3734
return ;
3692
3735
}
@@ -3795,6 +3838,7 @@ static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_o
3795
3838
3796
3839
action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
3797
3840
if (!action ) {
3841
+ HOOK_MALLOC_FAILED ;
3798
3842
return ESP_ERR_NO_MEM ;
3799
3843
}
3800
3844
@@ -3830,6 +3874,9 @@ static void _mdns_scheduler_run()
3830
3874
free (action );
3831
3875
_mdns_server -> tx_queue_head = p ;
3832
3876
}
3877
+ } else {
3878
+ HOOK_MALLOC_FAILED ;
3879
+ // continue
3833
3880
}
3834
3881
}
3835
3882
MDNS_SERVICE_UNLOCK ();
@@ -3992,6 +4039,7 @@ esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event)
3992
4039
3993
4040
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
3994
4041
if (!action ) {
4042
+ HOOK_MALLOC_FAILED ;
3995
4043
return ESP_OK ;
3996
4044
}
3997
4045
action -> type = ACTION_SYSTEM_EVENT ;
@@ -4013,6 +4061,7 @@ esp_err_t mdns_init()
4013
4061
4014
4062
_mdns_server = (mdns_server_t * )malloc (sizeof (mdns_server_t ));
4015
4063
if (!_mdns_server ) {
4064
+ HOOK_MALLOC_FAILED ;
4016
4065
return ESP_ERR_NO_MEM ;
4017
4066
}
4018
4067
memset ((uint8_t * )_mdns_server , 0 , sizeof (mdns_server_t ));
@@ -4115,6 +4164,7 @@ esp_err_t mdns_hostname_set(const char * hostname)
4115
4164
4116
4165
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4117
4166
if (!action ) {
4167
+ HOOK_MALLOC_FAILED ;
4118
4168
free (new_hostname );
4119
4169
return ESP_ERR_NO_MEM ;
4120
4170
}
@@ -4143,6 +4193,7 @@ esp_err_t mdns_instance_name_set(const char * instance)
4143
4193
4144
4194
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4145
4195
if (!action ) {
4196
+ HOOK_MALLOC_FAILED ;
4146
4197
free (new_instance );
4147
4198
return ESP_ERR_NO_MEM ;
4148
4199
}
@@ -4182,6 +4233,7 @@ esp_err_t mdns_service_add(const char * instance, const char * service, const ch
4182
4233
4183
4234
item = (mdns_srv_item_t * )malloc (sizeof (mdns_srv_item_t ));
4184
4235
if (!item ) {
4236
+ HOOK_MALLOC_FAILED ;
4185
4237
_mdns_free_service (s );
4186
4238
return ESP_ERR_NO_MEM ;
4187
4239
}
@@ -4191,6 +4243,7 @@ esp_err_t mdns_service_add(const char * instance, const char * service, const ch
4191
4243
4192
4244
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4193
4245
if (!action ) {
4246
+ HOOK_MALLOC_FAILED ;
4194
4247
_mdns_free_service (s );
4195
4248
free (item );
4196
4249
return ESP_ERR_NO_MEM ;
@@ -4227,6 +4280,7 @@ esp_err_t mdns_service_port_set(const char * service, const char * proto, uint16
4227
4280
4228
4281
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4229
4282
if (!action ) {
4283
+ HOOK_MALLOC_FAILED ;
4230
4284
return ESP_ERR_NO_MEM ;
4231
4285
}
4232
4286
action -> type = ACTION_SERVICE_PORT_SET ;
@@ -4259,6 +4313,7 @@ esp_err_t mdns_service_txt_set(const char * service, const char * proto, mdns_tx
4259
4313
4260
4314
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4261
4315
if (!action ) {
4316
+ HOOK_MALLOC_FAILED ;
4262
4317
_mdns_free_linked_txt (new_txt );
4263
4318
return ESP_ERR_NO_MEM ;
4264
4319
}
@@ -4286,6 +4341,7 @@ esp_err_t mdns_service_txt_item_set(const char * service, const char * proto, co
4286
4341
}
4287
4342
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4288
4343
if (!action ) {
4344
+ HOOK_MALLOC_FAILED ;
4289
4345
return ESP_ERR_NO_MEM ;
4290
4346
}
4291
4347
@@ -4322,6 +4378,7 @@ esp_err_t mdns_service_txt_item_remove(const char * service, const char * proto,
4322
4378
}
4323
4379
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4324
4380
if (!action ) {
4381
+ HOOK_MALLOC_FAILED ;
4325
4382
return ESP_ERR_NO_MEM ;
4326
4383
}
4327
4384
@@ -4359,6 +4416,7 @@ esp_err_t mdns_service_instance_name_set(const char * service, const char * prot
4359
4416
4360
4417
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4361
4418
if (!action ) {
4419
+ HOOK_MALLOC_FAILED ;
4362
4420
free (new_instance );
4363
4421
return ESP_ERR_NO_MEM ;
4364
4422
}
@@ -4385,6 +4443,7 @@ esp_err_t mdns_service_remove(const char * service, const char * proto)
4385
4443
4386
4444
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4387
4445
if (!action ) {
4446
+ HOOK_MALLOC_FAILED ;
4388
4447
return ESP_ERR_NO_MEM ;
4389
4448
}
4390
4449
action -> type = ACTION_SERVICE_DEL ;
@@ -4407,6 +4466,7 @@ esp_err_t mdns_service_remove_all()
4407
4466
4408
4467
mdns_action_t * action = (mdns_action_t * )malloc (sizeof (mdns_action_t ));
4409
4468
if (!action ) {
4469
+ HOOK_MALLOC_FAILED ;
4410
4470
return ESP_ERR_NO_MEM ;
4411
4471
}
4412
4472
action -> type = ACTION_SERVICES_CLEAR ;
0 commit comments