| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 15:02:48 +02:00
										 |  |  | #include "esp32_mock.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  | #include "mdns.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  | #include "mdns_private.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Global stuctures containing packet payload, search
 | 
					
						
							|  |  |  | mdns_rx_packet_t g_packet; | 
					
						
							|  |  |  | struct pbuf mypbuf; | 
					
						
							|  |  |  | mdns_search_once_t *search = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Dependency injected test functions
 | 
					
						
							|  |  |  | void mdns_test_execute_action(void *action); | 
					
						
							|  |  |  | mdns_srv_item_t *mdns_test_mdns_get_service_item(const char *service, const char *proto); | 
					
						
							|  |  |  | mdns_search_once_t *mdns_test_search_init(const char *name, const char *service, const char *proto, uint16_t type, uint32_t timeout, uint8_t max_results); | 
					
						
							|  |  |  | esp_err_t mdns_test_send_search_action(mdns_action_type_t type, mdns_search_once_t *search); | 
					
						
							|  |  |  | void mdns_test_search_free(mdns_search_once_t *search); | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | void mdns_test_init_di(void); | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  | extern mdns_server_t *_mdns_server; | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // mdns function wrappers for mdns setup in test mode
 | 
					
						
							|  |  |  | static int mdns_test_hostname_set(const char *mdns_hostname) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-14 21:35:27 +01:00
										 |  |  |     for (int i = 0; i < MDNS_MAX_INTERFACES; i++) { | 
					
						
							|  |  |  |         _mdns_server->interfaces[i].pcbs[MDNS_IP_PROTOCOL_V4].state = PCB_RUNNING;    // mark the PCB running to exercise mdns in fully operational mode
 | 
					
						
							|  |  |  |         _mdns_server->interfaces[i].pcbs[MDNS_IP_PROTOCOL_V6].state = PCB_RUNNING; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     int ret = mdns_hostname_set(mdns_hostname); | 
					
						
							|  |  |  |     mdns_action_t *a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  | static int mdns_test_add_delegated_host(const char *mdns_hostname) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     mdns_ip_addr_t addr = { .addr = { .u_addr = ESP_IPADDR_TYPE_V4 } }; | 
					
						
							|  |  |  |     addr.addr.u_addr.ip4.addr = 0x11111111; | 
					
						
							|  |  |  |     int ret = mdns_delegate_hostname_add(mdns_hostname, &addr); | 
					
						
							|  |  |  |     mdns_action_t *a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  | static int mdns_test_service_instance_name_set(const char *service, const char *proto, const char *instance) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret = mdns_service_instance_name_set(service, proto, instance); | 
					
						
							|  |  |  |     mdns_action_t *a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int mdns_test_service_txt_set(const char *service, const char *proto,  uint8_t num_items, mdns_txt_item_t txt[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret = mdns_service_txt_set(service, proto, txt, num_items); | 
					
						
							|  |  |  |     mdns_action_t *a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  | static int mdns_test_sub_service_add(const char *sub_name, const char *service_name, const char *proto, uint32_t port) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (mdns_service_add(NULL, service_name, proto, port, NULL, 0)) { | 
					
						
							|  |  |  |         // This is expected failure as the service thread is not running
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     mdns_action_t *a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (mdns_test_mdns_get_service_item(service_name, proto) == NULL) { | 
					
						
							|  |  |  |         return ESP_FAIL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     int ret = mdns_service_subtype_add_for_host(NULL, service_name, proto, NULL, sub_name); | 
					
						
							|  |  |  |     a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  | static int mdns_test_service_add(const char *service_name, const char *proto, uint32_t port) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (mdns_service_add(NULL, service_name, proto, port, NULL, 0)) { | 
					
						
							|  |  |  |         // This is expected failure as the service thread is not running
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     mdns_action_t *a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (mdns_test_mdns_get_service_item(service_name, proto) == NULL) { | 
					
						
							|  |  |  |         return ESP_FAIL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  | static mdns_result_t *mdns_test_query(const char *name, const char *service, const char *proto, uint16_t type) | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  |     search = mdns_test_search_init(name, service, proto, type, 3000, 20); | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (!search) { | 
					
						
							|  |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (mdns_test_send_search_action(ACTION_SEARCH_ADD, search)) { | 
					
						
							|  |  |  |         mdns_test_search_free(search); | 
					
						
							|  |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mdns_action_t *a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | static void mdns_test_query_free(void) | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     mdns_test_search_free(search); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // function "under test" where afl-mangled packets passed
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | void mdns_parse_packet(mdns_rx_packet_t *packet); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Test starts here
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  | int main(int argc, char **argv) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |     const char *mdns_hostname = "minifritz"; | 
					
						
							|  |  |  |     const char *mdns_instance = "Hristo's Time Capsule"; | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     mdns_txt_item_t arduTxtData[4] = { | 
					
						
							|  |  |  |         {"board", "esp32"}, | 
					
						
							|  |  |  |         {"tcp_check", "no"}, | 
					
						
							|  |  |  |         {"ssh_upload", "no"}, | 
					
						
							|  |  |  |         {"auth_upload", "no"} | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |     const uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x32}; | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |     uint8_t buf[1460]; | 
					
						
							|  |  |  |     char winstance[21 + strlen(mdns_hostname)]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sprintf(winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]", mdns_hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     // Init depencency injected methods
 | 
					
						
							|  |  |  |     mdns_test_init_di(); | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (mdns_init()) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (mdns_test_hostname_set(mdns_hostname)) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  |     if (mdns_test_add_delegated_host(mdns_hostname) || mdns_test_add_delegated_host("megafritz")) { | 
					
						
							|  |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef MDNS_NO_SERVICES
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (mdns_test_sub_service_add("_server", "_fritz", "_tcp", 22)) { | 
					
						
							|  |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (mdns_test_service_add("_telnet", "_tcp", 22)) { | 
					
						
							|  |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (mdns_test_service_add("_workstation", "_tcp", 9)) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (mdns_test_service_instance_name_set("_workstation", "_tcp", winstance)) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (mdns_test_service_add("_arduino", "_tcp", 3232)) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (mdns_test_service_txt_set("_arduino", "_tcp", 4, arduTxtData)) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (mdns_test_service_add("_http", "_tcp", 80)) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     if (mdns_test_service_instance_name_set("_http", "_tcp", "ESP WebServer")) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ( | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |         mdns_test_service_add("_afpovertcp", "_tcp", 548) | 
					
						
							|  |  |  |         || mdns_test_service_add("_rfb", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_smb", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_adisk", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_airport", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_printer", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_airplay", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_raop", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_uscan", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_uscans", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_ippusb", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_scanner", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_ipp", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_ipps", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_pdl-datastream", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_ptp", "_tcp", 885) | 
					
						
							|  |  |  |         || mdns_test_service_add("_sleep-proxy", "_udp", 885)) { | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  |         abort(); | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     mdns_result_t *results = NULL; | 
					
						
							|  |  |  |     FILE *file; | 
					
						
							|  |  |  |     size_t nread; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef INSTR_IS_OFF
 | 
					
						
							|  |  |  |     size_t len = 1460; | 
					
						
							|  |  |  |     memset(buf, 0, 1460); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (argc != 2) { | 
					
						
							|  |  |  |         printf("Non-instrumentation mode: please supply a file name created by AFL to reproduce crash\n"); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         //
 | 
					
						
							|  |  |  |         // Note: parameter1 is a file (mangled packet) which caused the crash
 | 
					
						
							|  |  |  |         file = fopen(argv[1], "r"); | 
					
						
							| 
									
										
										
										
											2021-06-09 09:58:58 +02:00
										 |  |  |         assert(file >= 0 ); | 
					
						
							| 
									
										
										
										
											2021-06-25 08:33:38 +02:00
										 |  |  |         len = fread(buf, 1, 1460, file); | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |         fclose(file); | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     for (i = 0; i < 1; i++) { | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |     while (__AFL_LOOP(1000)) { | 
					
						
							|  |  |  |         memset(buf, 0, 1460); | 
					
						
							|  |  |  |         size_t len = read(0, buf, 1460); | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  |         mypbuf.payload = malloc(len); | 
					
						
							|  |  |  |         memcpy(mypbuf.payload, buf, len); | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |         mypbuf.len = len; | 
					
						
							|  |  |  |         g_packet.pb = &mypbuf; | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  |         mdns_test_query("minifritz", "_fritz", "_tcp", MDNS_TYPE_ANY); | 
					
						
							|  |  |  |         mdns_test_query(NULL, "_fritz", "_tcp", MDNS_TYPE_PTR); | 
					
						
							|  |  |  |         mdns_test_query(NULL, "_afpovertcp", "_tcp", MDNS_TYPE_PTR); | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |         mdns_parse_packet(&g_packet); | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  |         free(mypbuf.payload); | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-01 09:56:32 +01:00
										 |  |  | #ifndef MDNS_NO_SERVICES
 | 
					
						
							|  |  |  |     mdns_service_remove_all(); | 
					
						
							|  |  |  |     mdns_action_t *a = NULL; | 
					
						
							|  |  |  |     GetLastItem(&a); | 
					
						
							|  |  |  |     mdns_test_execute_action(a); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-05-04 16:42:54 +02:00
										 |  |  |     ForceTaskDelete(); | 
					
						
							|  |  |  |     mdns_free(); | 
					
						
							| 
									
										
										
										
											2017-04-03 16:50:12 +03:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } |