CI: fixing the files to be complient with pre-commit hooks

This commit is contained in:
Suren Gabrielyan
2022-10-11 16:31:57 +02:00
parent 9d45d505d5
commit 945bd17701
205 changed files with 3130 additions and 3441 deletions

View File

@ -1,10 +1,12 @@
from esp_docs.conf_docs import * # noqa: F403,F401
# flake8: noqa
from esp_docs.conf_docs import *
extensions += ['sphinx_copybutton',
# Needed as a trigger for running doxygen
'esp_docs.esp_extensions.dummy_build_system',
'esp_docs.esp_extensions.run_doxygen',
]
extensions += [
'sphinx_copybutton',
# Needed as a trigger for running doxygen
'esp_docs.esp_extensions.dummy_build_system',
'esp_docs.esp_extensions.run_doxygen',
]
# link roles config
github_repo = 'espressif/esp-protocols'
@ -14,7 +16,7 @@ html_context['github_user'] = 'espressif'
html_context['github_repo'] = 'esp-protocols'
# Extra options required by sphinx_idf_theme
project_slug = 'esp-idf' # >=5.0
project_slug = 'esp-idf' # >=5.0
versions_url = 'https://github.com/espressif/esp-protocols/docs/docs_versions.js'
idf_targets = ['esp32']

View File

@ -201,5 +201,3 @@ API Reference
-------------
.. include-build-file:: inc/mdns.inc

View File

@ -15,7 +15,7 @@ window.onload =(function() {
mySpan.value = 'latest';
mySpan.setAttribute('disabled', true);
myAnchor.parentNode.replaceChild(mySpan, myAnchor);
var myAnchor = document.getElementById('target-select');
var mySpan = document.createElement('input');
mySpan.setAttribute('type', 'text');

View File

@ -185,5 +185,3 @@ API 参考
--------
.. include-build-file:: inc/mdns.inc

View File

@ -1,10 +1,11 @@
/* MDNS-SD Query and advertise Example
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
/*
* MDNS-SD Query and advertise Example
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
@ -24,17 +25,17 @@
#define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE
#define EXAMPLE_BUTTON_GPIO CONFIG_MDNS_BUTTON_GPIO
static const char * TAG = "mdns-test";
static char * generate_hostname(void);
static const char *TAG = "mdns-test";
static char *generate_hostname(void);
#if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
static void query_mdns_host_with_gethostbyname(char * host);
static void query_mdns_host_with_getaddrinfo(char * host);
static void query_mdns_host_with_gethostbyname(char *host);
static void query_mdns_host_with_getaddrinfo(char *host);
#endif
static void initialise_mdns(void)
{
char * hostname = generate_hostname();
char *hostname = generate_hostname();
//initialize mDNS
ESP_ERROR_CHECK( mdns_init() );
@ -84,7 +85,7 @@ static void initialise_mdns(void)
}
/* these strings match mdns_ip_protocol_t enumeration */
static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
static const char *ip_protocol_str[] = {"V4", "V6", "MAX"};
static void mdns_print_results(mdns_result_t *results)
{
@ -120,17 +121,17 @@ static void mdns_print_results(mdns_result_t *results)
}
}
static void query_mdns_service(const char * service_name, const char * proto)
static void query_mdns_service(const char *service_name, const char *proto)
{
ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto);
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
if(err){
if (err) {
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
return;
}
if(!results){
if (!results) {
ESP_LOGW(TAG, "No results found!");
return;
}
@ -142,7 +143,7 @@ static void query_mdns_service(const char * service_name, const char * proto)
static bool check_and_print_result(mdns_search_once_t *search)
{
// Check if any result is available
mdns_result_t * result = NULL;
mdns_result_t *result = NULL;
if (!mdns_query_async_get_results(search, 0, &result, NULL)) {
return false;
}
@ -152,9 +153,9 @@ static bool check_and_print_result(mdns_search_once_t *search)
}
// If yes, print the result
mdns_ip_addr_t * a = result->addr;
mdns_ip_addr_t *a = result->addr;
while (a) {
if(a->addr.type == ESP_IPADDR_TYPE_V6){
if (a->addr.type == ESP_IPADDR_TYPE_V6) {
printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
} else {
printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
@ -166,7 +167,7 @@ static bool check_and_print_result(mdns_search_once_t *search)
return true;
}
static void query_mdns_hosts_async(const char * host_name)
static void query_mdns_hosts_async(const char *host_name)
{
ESP_LOGI(TAG, "Query both A and AAA: %s.local", host_name);
@ -187,7 +188,7 @@ static void query_mdns_hosts_async(const char * host_name)
}
}
static void query_mdns_host(const char * host_name)
static void query_mdns_host(const char *host_name)
{
ESP_LOGI(TAG, "Query A: %s.local", host_name);
@ -195,8 +196,8 @@ static void query_mdns_host(const char * host_name)
addr.addr = 0;
esp_err_t err = mdns_query_a(host_name, 2000, &addr);
if(err){
if(err == ESP_ERR_NOT_FOUND){
if (err) {
if (err == ESP_ERR_NOT_FOUND) {
ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err));
return;
}
@ -285,7 +286,7 @@ void app_main(void)
/** Generate host name based on sdkconfig, optionally adding a portion of MAC address to it.
* @return host name string allocated from the heap
*/
static char* generate_hostname(void)
static char *generate_hostname(void)
{
#ifndef CONFIG_MDNS_ADD_MAC_TO_HOSTNAME
return strdup(CONFIG_MDNS_HOSTNAME);
@ -305,7 +306,7 @@ static char* generate_hostname(void)
* @brief Executes gethostbyname and displays list of resolved addresses.
* Note: This function is used only to test advertised mdns hostnames resolution
*/
static void query_mdns_host_with_gethostbyname(char * host)
static void query_mdns_host_with_gethostbyname(char *host)
{
struct hostent *res = gethostbyname(host);
if (res) {
@ -321,10 +322,10 @@ static void query_mdns_host_with_gethostbyname(char * host)
* @brief Executes getaddrinfo and displays list of resolved addresses.
* Note: This function is used only to test advertised mdns hostnames resolution
*/
static void query_mdns_host_with_getaddrinfo(char * host)
static void query_mdns_host_with_getaddrinfo(char *host)
{
struct addrinfo hints;
struct addrinfo * res;
struct addrinfo *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
@ -335,8 +336,8 @@ static void query_mdns_host_with_getaddrinfo(char * host)
char *resolved_addr;
#if CONFIG_LWIP_IPV6
resolved_addr = res->ai_family == AF_INET ?
inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr) :
inet_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr);
inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr) :
inet_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr);
#else
resolved_addr = inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr);
#endif // CONFIG_LWIP_IPV6

View File

@ -1,28 +1,30 @@
import os
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Unlicense OR CC0-1.0
import re
import select
import socket
import struct
import subprocess
import time
import pytest
from threading import Event, Thread
import dpkt
import dpkt.dns
from pytest_embedded import Dut
import subprocess
def get_dns_query_for_esp(esp_host):
dns = dpkt.dns.DNS(b'\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01')
dns = dpkt.dns.DNS(
b'\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01'
)
dns.qd[0].name = esp_host + u'.local'
print('Created query for esp host: {} '.format(dns.__repr__()))
return dns.pack()
def get_dns_answer_to_mdns(tester_host):
dns = dpkt.dns.DNS(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
dns = dpkt.dns.DNS(
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
)
dns.op = dpkt.dns.DNS_QR | dpkt.dns.DNS_AA
dns.rcode = dpkt.dns.DNS_RCODE_NOERR
arr = dpkt.dns.DNS.RR()
@ -36,9 +38,10 @@ def get_dns_answer_to_mdns(tester_host):
def get_dns_answer_to_mdns_lwip(tester_host, id):
dns = dpkt.dns.DNS(b'\x5e\x39\x84\x00\x00\x01\x00\x01\x00\x00\x00\x00\x0a\x64\x61\x76\x69\x64'
b'\x2d\x63\x6f\x6d\x70\x05\x6c\x6f\x63\x61\x6c\x00\x00\x01\x00\x01\xc0\x0c'
b'\x00\x01\x00\x01\x00\x00\x00\x0a\x00\x04\xc0\xa8\x0a\x6c')
dns = dpkt.dns.DNS(
b'\x5e\x39\x84\x00\x00\x01\x00\x01\x00\x00\x00\x00\x0a\x64\x61\x76\x69\x64'
b'\x2d\x63\x6f\x6d\x70\x05\x6c\x6f\x63\x61\x6c\x00\x00\x01\x00\x01\xc0\x0c'
b'\x00\x01\x00\x01\x00\x00\x00\x0a\x00\x04\xc0\xa8\x0a\x6c')
dns.qd[0].name = tester_host
dns.an[0].name = tester_host
dns.an[0].ip = socket.inet_aton('127.0.0.1')
@ -69,10 +72,13 @@ def mdns_server(esp_host, events):
if current_time - last_query_timepoint > QUERY_TIMEOUT:
last_query_timepoint = current_time
if not events['esp_answered'].is_set():
sock.sendto(get_dns_query_for_esp(esp_host), (MCAST_GRP, UDP_PORT))
sock.sendto(get_dns_query_for_esp(esp_host),
(MCAST_GRP, UDP_PORT))
if not events['esp_delegated_answered'].is_set():
sock.sendto(get_dns_query_for_esp(esp_host + '-delegated'), (MCAST_GRP, UDP_PORT))
timeout = max(0, QUERY_TIMEOUT - (current_time - last_query_timepoint))
sock.sendto(get_dns_query_for_esp(esp_host + '-delegated'),
(MCAST_GRP, UDP_PORT))
timeout = max(
0, QUERY_TIMEOUT - (current_time - last_query_timepoint))
read_socks, _, _ = select.select([sock], [], [], timeout)
if not read_socks:
continue
@ -81,17 +87,22 @@ def mdns_server(esp_host, events):
if len(dns.qd) > 0 and dns.qd[0].type == dpkt.dns.DNS_A:
if dns.qd[0].name == TESTER_NAME:
print('Received query: {} '.format(dns.__repr__()))
sock.sendto(get_dns_answer_to_mdns(TESTER_NAME), (MCAST_GRP, UDP_PORT))
sock.sendto(get_dns_answer_to_mdns(TESTER_NAME),
(MCAST_GRP, UDP_PORT))
elif dns.qd[0].name == TESTER_NAME_LWIP:
print('Received query: {} '.format(dns.__repr__()))
sock.sendto(get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id), addr)
sock.sendto(
get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id),
addr)
if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A:
print('Received answer from {}'.format(dns.an[0].name))
if dns.an[0].name == esp_host + u'.local':
print('Received answer to esp32-mdns query: {}'.format(dns.__repr__()))
print('Received answer to esp32-mdns query: {}'.format(
dns.__repr__()))
events['esp_answered'].set()
if dns.an[0].name == esp_host + u'-delegated.local':
print('Received answer to esp32-mdns-delegate query: {}'.format(dns.__repr__()))
print('Received answer to esp32-mdns-delegate query: {}'.
format(dns.__repr__()))
events['esp_delegated_answered'].set()
except socket.timeout:
break
@ -108,30 +119,54 @@ def test_examples_protocol_mdns(dut):
4. check DUT output if mdns advertized host is resolved
"""
specific_host = dut.expect(re.compile(b'mdns hostname set to: \[(.*?)\]')).group(1).decode()
specific_host = dut.expect(re.compile(
b'mdns hostname set to: \[(.*?)\]')).group(1).decode() # noqa: W605
mdns_server_events = {'stop': Event(), 'esp_answered': Event(), 'esp_delegated_answered': Event()}
mdns_responder = Thread(target=mdns_server, args=(str(specific_host), mdns_server_events))
ip_address = dut.expect(re.compile(b'IPv4 address:([a-zA-Z0-9]*).*')).group(1).decode()
mdns_server_events = {
'stop': Event(),
'esp_answered': Event(),
'esp_delegated_answered': Event()
}
mdns_responder = Thread(target=mdns_server,
args=(str(specific_host), mdns_server_events))
ip_address = dut.expect(
re.compile(b'IPv4 address:([a-zA-Z0-9]*).*')).group(1).decode()
print('Connected to AP with IP: {}'.format(ip_address))
try:
# 3. check the mdns name is accessible.
mdns_responder.start()
if not mdns_server_events['esp_answered'].wait(timeout=30):
raise ValueError('Test has failed: did not receive mdns answer within timeout')
raise ValueError(
'Test has failed: did not receive mdns answer within timeout')
if not mdns_server_events['esp_delegated_answered'].wait(timeout=30):
raise ValueError('Test has failed: did not receive mdns answer for delegated host within timeout')
raise ValueError(
'Test has failed: did not receive mdns answer for delegated host within timeout'
)
# 4. check DUT output if mdns advertized host is resolved
dut.expect(re.compile(b'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1'), timeout=30)
dut.expect(re.compile(b'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1'), timeout=30)
dut.expect(re.compile(b'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1'), timeout=30)
dut.expect(
re.compile(
b'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1')
)
dut.expect(
re.compile(
b'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1'
))
dut.expect(
re.compile(
b'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1'
))
# 5. check the DUT answers to `dig` command
dig_output = subprocess.check_output(['dig', '+short', '-p', '5353', '@224.0.0.251',
'{}.local'.format(specific_host)])
print('Resolving {} using "dig" succeeded with:\n{}'.format(specific_host, dig_output))
dig_output = subprocess.check_output([
'dig', '+short', '-p', '5353', '@224.0.0.251',
'{}.local'.format(specific_host)
])
print('Resolving {} using "dig" succeeded with:\n{}'.format(
specific_host, dig_output))
if not ip_address.encode('utf-8') in dig_output:
raise ValueError('Test has failed: Incorrectly resolved DUT hostname using dig'
"Output should've contained DUT's IP address:{}".format(ip_address))
raise ValueError(
'Test has failed: Incorrectly resolved DUT hostname using dig'
"Output should've contained DUT's IP address:{}".format(
ip_address))
finally:
mdns_server_events['stop'].set()
mdns_responder.join()

View File

@ -50,8 +50,8 @@ typedef enum {
* Used in mdns_service_add()
*/
typedef struct {
const char * key; /*!< item key name */
const char * value; /*!< item value string */
const char *key; /*!< item key name */
const char *value; /*!< item value string */
} mdns_txt_item_t;
/**
@ -59,7 +59,7 @@ typedef struct {
*/
typedef struct mdns_ip_addr_s {
esp_ip_addr_t addr; /*!< IP address */
struct mdns_ip_addr_s * next; /*!< next IP, or NULL for the last IP in the list */
struct mdns_ip_addr_s *next; /*!< next IP, or NULL for the last IP in the list */
} mdns_ip_addr_t;
/**
@ -74,25 +74,25 @@ typedef enum {
* @brief mDNS query result structure
*/
typedef struct mdns_result_s {
struct mdns_result_s * next; /*!< next result, or NULL for the last result in the list */
struct mdns_result_s *next; /*!< next result, or NULL for the last result in the list */
esp_netif_t* esp_netif; /*!< ptr to corresponding esp-netif */
esp_netif_t *esp_netif; /*!< ptr to corresponding esp-netif */
uint32_t ttl; /*!< time to live */
mdns_ip_protocol_t ip_protocol; /*!< ip_protocol type of the interface (v4/v6) */
// PTR
char * instance_name; /*!< instance name */
char * service_type; /*!< service type */
char * proto; /*!< srevice protocol */
char *instance_name; /*!< instance name */
char *service_type; /*!< service type */
char *proto; /*!< srevice protocol */
// SRV
char * hostname; /*!< hostname */
char *hostname; /*!< hostname */
uint16_t port; /*!< service port */
// TXT
mdns_txt_item_t * txt; /*!< txt record */
mdns_txt_item_t *txt; /*!< txt record */
uint8_t *txt_value_len; /*!< array of txt value len of each record */
size_t txt_count; /*!< number of txt items */
// A and AAAA
mdns_ip_addr_t * addr; /*!< linked list of IP addresses found */
mdns_ip_addr_t *addr; /*!< linked list of IP addresses found */
} mdns_result_t;
typedef void (*mdns_query_notify_t)(mdns_search_once_t *search);
@ -125,7 +125,7 @@ void mdns_free(void);
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_hostname_set(const char * hostname);
esp_err_t mdns_hostname_set(const char *hostname);
/**
* @brief Adds a hostname and address to be delegated
@ -142,7 +142,7 @@ esp_err_t mdns_hostname_set(const char * hostname);
* - ESP_ERR_NO_MEM memory error
*
*/
esp_err_t mdns_delegate_hostname_add(const char * hostname, const mdns_ip_addr_t *address_list);
esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t *address_list);
/**
* @brief Remove a delegated hostname
@ -157,7 +157,7 @@ esp_err_t mdns_delegate_hostname_add(const char * hostname, const mdns_ip_addr_t
* - ESP_ERR_NO_MEM memory error
*
*/
esp_err_t mdns_delegate_hostname_remove(const char * hostname);
esp_err_t mdns_delegate_hostname_remove(const char *hostname);
/**
* @brief Query whether a hostname has been added
@ -169,7 +169,7 @@ esp_err_t mdns_delegate_hostname_remove(const char * hostname);
* - false The hostname has not been added.
*
*/
bool mdns_hostname_exists(const char * hostname);
bool mdns_hostname_exists(const char *hostname);
/**
* @brief Set the default instance name for mDNS server
@ -181,7 +181,7 @@ bool mdns_hostname_exists(const char * hostname);
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_instance_name_set(const char * instance_name);
esp_err_t mdns_instance_name_set(const char *instance_name);
/**
* @brief Add service to mDNS server
@ -205,7 +205,7 @@ esp_err_t mdns_instance_name_set(const char * instance_name);
* - ESP_ERR_NO_MEM memory error
* - ESP_FAIL failed to add service
*/
esp_err_t mdns_service_add(const char * instance_name, const char * service_type, const char * proto, uint16_t port, mdns_txt_item_t txt[], size_t num_items);
esp_err_t mdns_service_add(const char *instance_name, const char *service_type, const char *proto, uint16_t port, mdns_txt_item_t txt[], size_t num_items);
/**
* @brief Add service to mDNS server with a delegated hostname
@ -230,8 +230,8 @@ esp_err_t mdns_service_add(const char * instance_name, const char * service_type
* - ESP_ERR_NO_MEM memory error
* - ESP_FAIL failed to add service
*/
esp_err_t mdns_service_add_for_host(const char * instance_name, const char * service_type, const char * proto,
const char * hostname, uint16_t port, mdns_txt_item_t txt[], size_t num_items);
esp_err_t mdns_service_add_for_host(const char *instance_name, const char *service_type, const char *proto,
const char *hostname, uint16_t port, mdns_txt_item_t txt[], size_t num_items);
/**
* @brief Check whether a service has been added.
@ -244,7 +244,7 @@ esp_err_t mdns_service_add_for_host(const char * instance_name, const char * ser
* - true Correspondding service has been added.
* - false Service not found.
*/
bool mdns_service_exists(const char * service_type, const char * proto, const char * hostname);
bool mdns_service_exists(const char *service_type, const char *proto, const char *hostname);
/**
@ -274,7 +274,7 @@ bool mdns_service_exists_with_instance(const char *instance, const char *service
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_remove(const char * service_type, const char * proto);
esp_err_t mdns_service_remove(const char *service_type, const char *proto);
/**
* @brief Remove service from mDNS server with hostname
@ -290,7 +290,7 @@ esp_err_t mdns_service_remove(const char * service_type, const char * proto);
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_remove_for_host(const char *instance, const char * service_type, const char * proto, const char *hostname);
esp_err_t mdns_service_remove_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname);
/**
* @brief Set instance name for service
@ -305,7 +305,7 @@ esp_err_t mdns_service_remove_for_host(const char *instance, const char * servic
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_instance_name_set(const char * service_type, const char * proto, const char * instance_name);
esp_err_t mdns_service_instance_name_set(const char *service_type, const char *proto, const char *instance_name);
/**
* @brief Set instance name for service with hostname
@ -322,8 +322,8 @@ esp_err_t mdns_service_instance_name_set(const char * service_type, const char *
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_instance_name_set_for_host(const char * instance_old, const char * service_type, const char * proto, const char * hostname,
const char * instance_name);
esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service_type, const char *proto, const char *hostname,
const char *instance_name);
/**
* @brief Set service port
@ -338,7 +338,7 @@ esp_err_t mdns_service_instance_name_set_for_host(const char * instance_old, con
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_port_set(const char * service_type, const char * proto, uint16_t port);
esp_err_t mdns_service_port_set(const char *service_type, const char *proto, uint16_t port);
/**
@ -356,8 +356,8 @@ esp_err_t mdns_service_port_set(const char * service_type, const char * proto, u
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_port_set_for_host(const char * instance, const char * service_type, const char * proto, const char * hostname,
uint16_t port);
esp_err_t mdns_service_port_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname,
uint16_t port);
/**
* @brief Replace all TXT items for service
@ -375,7 +375,7 @@ esp_err_t mdns_service_port_set_for_host(const char * instance, const char * ser
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_txt_set(const char * service_type, const char * proto, mdns_txt_item_t txt[], uint8_t num_items);
esp_err_t mdns_service_txt_set(const char *service_type, const char *proto, mdns_txt_item_t txt[], uint8_t num_items);
/**
* @brief Replace all TXT items for service with hostname
@ -395,7 +395,7 @@ esp_err_t mdns_service_txt_set(const char * service_type, const char * proto, md
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_txt_set_for_host(const char * instance, const char * service_type, const char * proto, const char * hostname,
esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname,
mdns_txt_item_t txt[], uint8_t num_items);
/**
@ -414,7 +414,7 @@ esp_err_t mdns_service_txt_set_for_host(const char * instance, const char * serv
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_txt_item_set(const char * service_type, const char * proto, const char * key, const char * value);
esp_err_t mdns_service_txt_item_set(const char *service_type, const char *proto, const char *key, const char *value);
/**
* @brief Set/Add TXT item for service TXT record
@ -432,7 +432,7 @@ esp_err_t mdns_service_txt_item_set(const char * service_type, const char * prot
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service_type, const char *proto,
const char *key, const char *value, uint8_t value_len);
const char *key, const char *value, uint8_t value_len);
/**
* @brief Set/Add TXT item for service TXT record with hostname
@ -452,8 +452,8 @@ esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service_
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_txt_item_set_for_host(const char * instance, const char * service_type, const char * proto, const char * hostname,
const char * key, const char * value);
esp_err_t mdns_service_txt_item_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname,
const char *key, const char *value);
/**
* @brief Set/Add TXT item for service TXT record with hostname and txt value length
@ -472,9 +472,9 @@ esp_err_t mdns_service_txt_item_set_for_host(const char * instance, const char *
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char * instance, const char *service_type, const char *proto,
const char *hostname, const char *key,
const char *value, uint8_t value_len);
esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *instance, const char *service_type, const char *proto,
const char *hostname, const char *key,
const char *value, uint8_t value_len);
/**
* @brief Remove TXT item for service TXT record
@ -489,7 +489,7 @@ esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_txt_item_remove(const char * service_type, const char * proto, const char * key);
esp_err_t mdns_service_txt_item_remove(const char *service_type, const char *proto, const char *key);
/**
* @brief Remove TXT item for service TXT record with hostname
@ -506,8 +506,8 @@ esp_err_t mdns_service_txt_item_remove(const char * service_type, const char * p
* - ESP_ERR_NOT_FOUND Service not found
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_txt_item_remove_for_host(const char * instance, const char * service_type, const char * proto, const char * hostname,
const char * key);
esp_err_t mdns_service_txt_item_remove_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname,
const char *key);
/**
* @brief Add subtype for service.
@ -525,7 +525,7 @@ esp_err_t mdns_service_txt_item_remove_for_host(const char * instance, const cha
* - ESP_ERR_NO_MEM memory error
*/
esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const char *service_type, const char *proto,
const char *hostname, const char *subtype);
const char *hostname, const char *subtype);
/**
* @brief Remove and free all services from mDNS server
@ -546,7 +546,7 @@ esp_err_t mdns_service_remove_all(void);
* - ESP_ERR_INVALID_STATE search has not finished
* - ESP_ERR_INVALID_ARG pointer to search object is NULL
*/
esp_err_t mdns_query_async_delete(mdns_search_once_t* search);
esp_err_t mdns_query_async_delete(mdns_search_once_t *search);
/**
* @brief Get results from search pointer. Results available as a pointer to the output parameter.
@ -562,7 +562,7 @@ esp_err_t mdns_query_async_delete(mdns_search_once_t* search);
* True if search has finished before or at timeout
* False if search timeout is over
*/
bool mdns_query_async_get_results(mdns_search_once_t* search, uint32_t timeout, mdns_result_t ** results, uint8_t * num_results);
bool mdns_query_async_get_results(mdns_search_once_t *search, uint32_t timeout, mdns_result_t **results, uint8_t *num_results);
/**
* @brief Query mDNS for host or service asynchronousely.
@ -580,7 +580,7 @@ bool mdns_query_async_get_results(mdns_search_once_t* search, uint32_t timeout,
* NULL otherwise.
*/
mdns_search_once_t *mdns_query_async_new(const char *name, const char *service_type, const char *proto, uint16_t type,
uint32_t timeout, size_t max_results, mdns_query_notify_t notifier);
uint32_t timeout, size_t max_results, mdns_query_notify_t notifier);
/**
* @brief Generic mDNS query
@ -602,8 +602,8 @@ mdns_search_once_t *mdns_query_async_new(const char *name, const char *service_t
* - ESP_ERR_NO_MEM memory error
* - ESP_ERR_INVALID_ARG timeout was not given
*/
esp_err_t mdns_query_generic(const char * name, const char * service_type, const char * proto, uint16_t type,
mdns_query_transmission_type_t transmission_type, uint32_t timeout, size_t max_results, mdns_result_t ** results);
esp_err_t mdns_query_generic(const char *name, const char *service_type, const char *proto, uint16_t type,
mdns_query_transmission_type_t transmission_type, uint32_t timeout, size_t max_results, mdns_result_t **results);
/**
* @brief Query mDNS for host or service
@ -625,14 +625,14 @@ esp_err_t mdns_query_generic(const char * name, const char * service_type, const
* - ESP_ERR_NO_MEM memory error
* - ESP_ERR_INVALID_ARG timeout was not given
*/
esp_err_t mdns_query(const char * name, const char * service_type, const char * proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t ** results);
esp_err_t mdns_query(const char *name, const char *service_type, const char *proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t **results);
/**
* @brief Free query results
*
* @param results linked list of results to be freed
*/
void mdns_query_results_free(mdns_result_t * results);
void mdns_query_results_free(mdns_result_t *results);
/**
* @brief Query mDNS for service
@ -649,7 +649,7 @@ void mdns_query_results_free(mdns_result_t * results);
* - ESP_ERR_NO_MEM memory error
* - ESP_ERR_INVALID_ARG parameter error
*/
esp_err_t mdns_query_ptr(const char * service_type, const char * proto, uint32_t timeout, size_t max_results, mdns_result_t ** results);
esp_err_t mdns_query_ptr(const char *service_type, const char *proto, uint32_t timeout, size_t max_results, mdns_result_t **results);
/**
* @brief Query mDNS for SRV record
@ -666,7 +666,7 @@ esp_err_t mdns_query_ptr(const char * service_type, const char * proto, uint32_t
* - ESP_ERR_NO_MEM memory error
* - ESP_ERR_INVALID_ARG parameter error
*/
esp_err_t mdns_query_srv(const char * instance_name, const char * service_type, const char * proto, uint32_t timeout, mdns_result_t ** result);
esp_err_t mdns_query_srv(const char *instance_name, const char *service_type, const char *proto, uint32_t timeout, mdns_result_t **result);
/**
* @brief Query mDNS for TXT record
@ -683,7 +683,7 @@ esp_err_t mdns_query_srv(const char * instance_name, const char * service_type,
* - ESP_ERR_NO_MEM memory error
* - ESP_ERR_INVALID_ARG parameter error
*/
esp_err_t mdns_query_txt(const char * instance_name, const char * service_type, const char * proto, uint32_t timeout, mdns_result_t ** result);
esp_err_t mdns_query_txt(const char *instance_name, const char *service_type, const char *proto, uint32_t timeout, mdns_result_t **result);
/**
* @brief Query mDNS for A record
@ -698,7 +698,7 @@ esp_err_t mdns_query_txt(const char * instance_name, const char * service_type,
* - ESP_ERR_NO_MEM memory error
* - ESP_ERR_INVALID_ARG parameter error
*/
esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t * addr);
esp_err_t mdns_query_a(const char *host_name, uint32_t timeout, esp_ip4_addr_t *addr);
#if CONFIG_LWIP_IPV6
/**
@ -716,7 +716,7 @@ esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t
* - ESP_ERR_NO_MEM memory error
* - ESP_ERR_INVALID_ARG parameter error
*/
esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr_t * addr);
esp_err_t mdns_query_aaaa(const char *host_name, uint32_t timeout, esp_ip6_addr_t *addr);
#endif

View File

@ -1,16 +1,8 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _MDNS_CONSOLE_H_
#define _MDNS_CONSOLE_H_

File diff suppressed because it is too large Load Diff

View File

@ -9,12 +9,12 @@
#include "argtable3/argtable3.h"
#include "mdns.h"
static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
static const char *ip_protocol_str[] = {"V4", "V6", "MAX"};
static void mdns_print_results(mdns_result_t * results)
static void mdns_print_results(mdns_result_t *results)
{
mdns_result_t * r = results;
mdns_ip_addr_t * a = NULL;
mdns_result_t *r = results;
mdns_ip_addr_t *a = NULL;
int i = 1;
while (r) {
printf("%d: Interface: %s, Type: %s\n", i++, esp_netif_get_ifkey(r->esp_netif), ip_protocol_str[r->ip_protocol]);
@ -26,7 +26,7 @@ static void mdns_print_results(mdns_result_t * results)
}
if (r->txt_count) {
printf(" TXT : [%u] ", r->txt_count);
for (size_t t=0; t<r->txt_count; t++) {
for (size_t t = 0; t < r->txt_count; t++) {
printf("%s=%s; ", r->txt[t].key, r->txt[t].value);
}
printf("\n");
@ -50,15 +50,15 @@ static struct {
struct arg_end *end;
} mdns_query_a_args;
static int cmd_mdns_query_a(int argc, char** argv)
static int cmd_mdns_query_a(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_a_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_query_a_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_query_a_args.end, argv[0]);
return 1;
}
const char * hostname = mdns_query_a_args.hostname->sval[0];
const char *hostname = mdns_query_a_args.hostname->sval[0];
int timeout = mdns_query_a_args.timeout->ival[0];
if (!hostname || !hostname[0]) {
@ -108,15 +108,15 @@ static void register_mdns_query_a(void)
}
#if CONFIG_LWIP_IPV6
static int cmd_mdns_query_aaaa(int argc, char** argv)
static int cmd_mdns_query_aaaa(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_a_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_query_a_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_query_a_args.end, argv[0]);
return 1;
}
const char * hostname = mdns_query_a_args.hostname->sval[0];
const char *hostname = mdns_query_a_args.hostname->sval[0];
int timeout = mdns_query_a_args.timeout->ival[0];
if (!hostname || !hostname[0]) {
@ -174,17 +174,17 @@ static struct {
struct arg_end *end;
} mdns_query_srv_args;
static int cmd_mdns_query_srv(int argc, char** argv)
static int cmd_mdns_query_srv(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_srv_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_query_srv_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_query_srv_args.end, argv[0]);
return 1;
}
const char * instance = mdns_query_srv_args.instance->sval[0];
const char * service = mdns_query_srv_args.service->sval[0];
const char * proto = mdns_query_srv_args.proto->sval[0];
const char *instance = mdns_query_srv_args.instance->sval[0];
const char *service = mdns_query_srv_args.service->sval[0];
const char *proto = mdns_query_srv_args.proto->sval[0];
int timeout = mdns_query_srv_args.timeout->ival[0];
if (timeout <= 0) {
@ -193,7 +193,7 @@ static int cmd_mdns_query_srv(int argc, char** argv)
printf("Query SRV: %s.%s.%s.local, Timeout: %d\n", instance, service, proto, timeout);
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_err_t err = mdns_query_srv(instance, service, proto, timeout, &results);
if (err) {
printf("ERROR: Query Failed\n");
@ -235,17 +235,17 @@ static struct {
struct arg_end *end;
} mdns_query_txt_args;
static int cmd_mdns_query_txt(int argc, char** argv)
static int cmd_mdns_query_txt(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_txt_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_query_txt_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_query_txt_args.end, argv[0]);
return 1;
}
const char * instance = mdns_query_txt_args.instance->sval[0];
const char * service = mdns_query_txt_args.service->sval[0];
const char * proto = mdns_query_txt_args.proto->sval[0];
const char *instance = mdns_query_txt_args.instance->sval[0];
const char *service = mdns_query_txt_args.service->sval[0];
const char *proto = mdns_query_txt_args.proto->sval[0];
int timeout = mdns_query_txt_args.timeout->ival[0];
printf("Query TXT: %s.%s.%s.local, Timeout: %d\n", instance, service, proto, timeout);
@ -254,7 +254,7 @@ static int cmd_mdns_query_txt(int argc, char** argv)
timeout = 5000;
}
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_err_t err = mdns_query_txt(instance, service, proto, timeout, &results);
if (err) {
printf("ERROR: Query Failed\n");
@ -297,16 +297,16 @@ static struct {
struct arg_end *end;
} mdns_query_ptr_args;
static int cmd_mdns_query_ptr(int argc, char** argv)
static int cmd_mdns_query_ptr(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_ptr_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_query_ptr_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_query_ptr_args.end, argv[0]);
return 1;
}
const char * service = mdns_query_ptr_args.service->sval[0];
const char * proto = mdns_query_ptr_args.proto->sval[0];
const char *service = mdns_query_ptr_args.service->sval[0];
const char *proto = mdns_query_ptr_args.proto->sval[0];
int timeout = mdns_query_ptr_args.timeout->ival[0];
int max_results = mdns_query_ptr_args.max_results->ival[0];
@ -320,7 +320,7 @@ static int cmd_mdns_query_ptr(int argc, char** argv)
printf("Query PTR: %s.%s.local, Timeout: %d, Max Results: %d\n", service, proto, timeout, max_results);
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_err_t err = mdns_query_ptr(service, proto, timeout, max_results, &results);
if (err) {
printf("ERROR: Query Failed\n");
@ -362,15 +362,15 @@ static struct {
struct arg_end *end;
} mdns_query_ip_args;
static int cmd_mdns_query_ip(int argc, char** argv)
static int cmd_mdns_query_ip(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_ip_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_query_ip_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_query_ip_args.end, argv[0]);
return 1;
}
const char * hostname = mdns_query_ip_args.hostname->sval[0];
const char *hostname = mdns_query_ip_args.hostname->sval[0];
int timeout = mdns_query_ip_args.timeout->ival[0];
int max_results = mdns_query_ip_args.max_results->ival[0];
@ -389,7 +389,7 @@ static int cmd_mdns_query_ip(int argc, char** argv)
printf("Query IP: %s.local, Timeout: %d, Max Results: %d\n", hostname, timeout, max_results);
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_err_t err = mdns_query(hostname, NULL, NULL, MDNS_TYPE_ANY, timeout, max_results, &results);
if (err) {
printf("ERROR: Query Failed\n");
@ -432,17 +432,17 @@ static struct {
struct arg_end *end;
} mdns_query_svc_args;
static int cmd_mdns_query_svc(int argc, char** argv)
static int cmd_mdns_query_svc(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_query_svc_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_query_svc_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_query_svc_args.end, argv[0]);
return 1;
}
const char * instance = mdns_query_svc_args.instance->sval[0];
const char * service = mdns_query_svc_args.service->sval[0];
const char * proto = mdns_query_svc_args.proto->sval[0];
const char *instance = mdns_query_svc_args.instance->sval[0];
const char *service = mdns_query_svc_args.service->sval[0];
const char *proto = mdns_query_svc_args.proto->sval[0];
int timeout = mdns_query_svc_args.timeout->ival[0];
int max_results = mdns_query_svc_args.max_results->ival[0];
@ -456,7 +456,7 @@ static int cmd_mdns_query_svc(int argc, char** argv)
printf("Query SVC: %s.%s.%s.local, Timeout: %d, Max Results: %d\n", instance, service, proto, timeout, max_results);
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_err_t err = mdns_query(instance, service, proto, MDNS_TYPE_ANY, timeout, max_results, &results);
if (err) {
printf("ERROR: Query Failed\n");
@ -498,9 +498,9 @@ static struct {
struct arg_end *end;
} mdns_init_args;
static int cmd_mdns_init(int argc, char** argv)
static int cmd_mdns_init(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_init_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_init_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_init_args.end, argv[0]);
return 1;
@ -538,7 +538,7 @@ static void register_mdns_init(void)
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_init) );
}
static int cmd_mdns_free(int argc, char** argv)
static int cmd_mdns_free(int argc, char **argv)
{
mdns_free();
return 0;
@ -562,9 +562,9 @@ static struct {
struct arg_end *end;
} mdns_set_hostname_args;
static int cmd_mdns_set_hostname(int argc, char** argv)
static int cmd_mdns_set_hostname(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_set_hostname_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_set_hostname_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_set_hostname_args.end, argv[0]);
return 1;
@ -600,9 +600,9 @@ static struct {
struct arg_end *end;
} mdns_set_instance_args;
static int cmd_mdns_set_instance(int argc, char** argv)
static int cmd_mdns_set_instance(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_set_instance_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_set_instance_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_set_instance_args.end, argv[0]);
return 1;
@ -633,11 +633,11 @@ static void register_mdns_set_instance(void)
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_set_instance) );
}
static mdns_txt_item_t * _convert_items(const char **values, int count)
static mdns_txt_item_t *_convert_items(const char **values, int count)
{
int i=0,e;
const char * value = NULL;
mdns_txt_item_t * items = (mdns_txt_item_t*) malloc(sizeof(mdns_txt_item_t) * count);
int i = 0, e;
const char *value = NULL;
mdns_txt_item_t *items = (mdns_txt_item_t *) malloc(sizeof(mdns_txt_item_t) * count);
if (!items) {
printf("ERROR: No Memory!\n");
goto fail;
@ -645,21 +645,21 @@ static mdns_txt_item_t * _convert_items(const char **values, int count)
}
memset(items, 0, sizeof(mdns_txt_item_t) * count);
for (i=0; i<count; i++) {
for (i = 0; i < count; i++) {
value = values[i];
char * esign = strchr(value, '=');
char *esign = strchr(value, '=');
if (!esign) {
printf("ERROR: Equal sign not found in '%s'!\n", value);
goto fail;
}
int var_len = esign - value;
int val_len = strlen(value) - var_len - 1;
char * var = (char*)malloc(var_len+1);
char *var = (char *)malloc(var_len + 1);
if (var == NULL) {
printf("ERROR: No Memory!\n");
goto fail;
}
char * val = (char*)malloc(val_len+1);
char *val = (char *)malloc(val_len + 1);
if (val == NULL) {
printf("ERROR: No Memory!\n");
free(var);
@ -667,7 +667,7 @@ static mdns_txt_item_t * _convert_items(const char **values, int count)
}
memcpy(var, value, var_len);
var[var_len] = 0;
memcpy(val, esign+1, val_len);
memcpy(val, esign + 1, val_len);
val[val_len] = 0;
items[i].key = var;
@ -677,7 +677,7 @@ static mdns_txt_item_t * _convert_items(const char **values, int count)
return items;
fail:
for (e=0;e<i;e++) {
for (e = 0; e < i; e++) {
free((char *)items[e].key);
free((char *)items[e].value);
}
@ -694,9 +694,9 @@ static struct {
struct arg_end *end;
} mdns_add_args;
static int cmd_mdns_service_add(int argc, char** argv)
static int cmd_mdns_service_add(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_add_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_add_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_add_args.end, argv[0]);
return 1;
@ -706,12 +706,12 @@ static int cmd_mdns_service_add(int argc, char** argv)
printf("ERROR: Bad arguments!\n");
return 1;
}
const char * instance = NULL;
const char *instance = NULL;
if (mdns_add_args.instance->sval[0] && mdns_add_args.instance->sval[0][0]) {
instance = mdns_add_args.instance->sval[0];
printf("MDNS: Service Instance: %s\n", instance);
}
mdns_txt_item_t * items = NULL;
mdns_txt_item_t *items = NULL;
if (mdns_add_args.txt->count) {
items = _convert_items(mdns_add_args.txt->sval, mdns_add_args.txt->count);
if (!items) {
@ -752,9 +752,9 @@ static struct {
struct arg_end *end;
} mdns_remove_args;
static int cmd_mdns_service_remove(int argc, char** argv)
static int cmd_mdns_service_remove(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_remove_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_remove_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_remove_args.end, argv[0]);
return 1;
@ -793,9 +793,9 @@ static struct {
struct arg_end *end;
} mdns_service_instance_set_args;
static int cmd_mdns_service_instance_set(int argc, char** argv)
static int cmd_mdns_service_instance_set(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_service_instance_set_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_service_instance_set_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_service_instance_set_args.end, argv[0]);
return 1;
@ -835,8 +835,9 @@ static struct {
struct arg_end *end;
} mdns_service_port_set_args;
static int cmd_mdns_service_port_set(int argc, char** argv) {
int nerrors = arg_parse(argc, argv, (void**) &mdns_service_port_set_args);
static int cmd_mdns_service_port_set(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void **) &mdns_service_port_set_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_service_port_set_args.end, argv[0]);
return 1;
@ -876,10 +877,10 @@ static struct {
struct arg_end *end;
} mdns_txt_replace_args;
static int cmd_mdns_service_txt_replace(int argc, char** argv)
static int cmd_mdns_service_txt_replace(int argc, char **argv)
{
mdns_txt_item_t * items = NULL;
int nerrors = arg_parse(argc, argv, (void**) &mdns_txt_replace_args);
mdns_txt_item_t *items = NULL;
int nerrors = arg_parse(argc, argv, (void **) &mdns_txt_replace_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_txt_replace_args.end, argv[0]);
return 1;
@ -929,9 +930,9 @@ static struct {
struct arg_end *end;
} mdns_txt_set_args;
static int cmd_mdns_service_txt_set(int argc, char** argv)
static int cmd_mdns_service_txt_set(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_txt_set_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_txt_set_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_txt_set_args.end, argv[0]);
return 1;
@ -972,9 +973,9 @@ static struct {
struct arg_end *end;
} mdns_txt_remove_args;
static int cmd_mdns_service_txt_remove(int argc, char** argv)
static int cmd_mdns_service_txt_remove(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void**) &mdns_txt_remove_args);
int nerrors = arg_parse(argc, argv, (void **) &mdns_txt_remove_args);
if (nerrors != 0) {
arg_print_errors(stderr, mdns_txt_remove_args.end, argv[0]);
return 1;
@ -1007,7 +1008,7 @@ static void register_mdns_service_txt_remove(void)
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_txt_remove) );
}
static int cmd_mdns_service_remove_all(int argc, char** argv)
static int cmd_mdns_service_remove_all(int argc, char **argv)
{
mdns_service_remove_all();
return 0;

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* MDNS Server Networking
@ -16,7 +21,7 @@
#include "mdns_networking.h"
#include "esp_netif_net_stack.h"
extern mdns_server_t * _mdns_server;
extern mdns_server_t *_mdns_server;
/*
* MDNS Server Networking
@ -24,7 +29,7 @@ extern mdns_server_t * _mdns_server;
*/
static const char *TAG = "MDNS_Networking";
static struct udp_pcb * _pcb_main = NULL;
static struct udp_pcb *_pcb_main = NULL;
static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip_addr_t *raddr, uint16_t rport);
@ -33,7 +38,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
*/
static esp_err_t _udp_pcb_main_init(void)
{
if(_pcb_main) {
if (_pcb_main) {
return ESP_OK;
}
_pcb_main = udp_new();
@ -57,7 +62,7 @@ static esp_err_t _udp_pcb_main_init(void)
*/
static void _udp_pcb_main_deinit(void)
{
if(_pcb_main){
if (_pcb_main) {
udp_recv(_pcb_main, NULL, NULL);
udp_disconnect(_pcb_main);
udp_remove(_pcb_main);
@ -70,7 +75,7 @@ static void _udp_pcb_main_deinit(void)
*/
static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protocol, bool join)
{
struct netif * netif = NULL;
struct netif *netif = NULL;
esp_netif_t *tcpip_if = _mdns_get_esp_netif(if_inx);
if (!esp_netif_is_netif_up(tcpip_if)) {
@ -85,7 +90,7 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
ip4_addr_t multicast_addr;
IP4_ADDR(&multicast_addr, 224, 0, 0, 251);
if(join){
if (join) {
if (igmp_joingroup_netif(netif, &multicast_addr)) {
return ESP_ERR_INVALID_STATE;
}
@ -99,7 +104,7 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco
else {
ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
if(join){
if (join) {
if (mld6_joingroup_netif(netif, &(multicast_addr.u_addr.ip6))) {
return ESP_ERR_INVALID_STATE;
}
@ -122,11 +127,11 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
uint8_t i;
while (pb != NULL) {
struct pbuf * this_pb = pb;
struct pbuf *this_pb = pb;
pb = pb->next;
this_pb->next = NULL;
mdns_rx_packet_t * packet = (mdns_rx_packet_t *)malloc(sizeof(mdns_rx_packet_t));
mdns_rx_packet_t *packet = (mdns_rx_packet_t *)malloc(sizeof(mdns_rx_packet_t));
if (!packet) {
HOOK_MALLOC_FAILED;
//missed packet - no memory
@ -148,23 +153,23 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
if (packet->src.type == IPADDR_TYPE_V4) {
packet->ip_protocol = MDNS_IP_PROTOCOL_V4;
struct ip_hdr * iphdr = (struct ip_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP_HLEN);
struct ip_hdr *iphdr = (struct ip_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP_HLEN);
packet->dest.u_addr.ip4.addr = iphdr->dest.addr;
packet->multicast = ip4_addr_ismulticast(&(packet->dest.u_addr.ip4));
}
#if CONFIG_LWIP_IPV6
else {
packet->ip_protocol = MDNS_IP_PROTOCOL_V6;
struct ip6_hdr * ip6hdr = (struct ip6_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP6_HLEN);
struct ip6_hdr *ip6hdr = (struct ip6_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP6_HLEN);
memcpy(&packet->dest.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6));
}
#endif
//lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces)
struct netif * netif = NULL;
struct udp_pcb * pcb = NULL;
for (i=0; i<MDNS_MAX_INTERFACES; i++) {
struct netif *netif = NULL;
struct udp_pcb *pcb = NULL;
for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
pcb = _mdns_server->interfaces[i].pcbs[packet->ip_protocol].pcb;
netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i));
if (pcb && netif && netif == ip_current_input_netif ()) {
@ -185,7 +190,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
}
if (!pcb || !_mdns_server || !_mdns_server->action_queue
|| _mdns_send_rx_action(packet) != ESP_OK) {
|| _mdns_send_rx_action(packet) != ESP_OK) {
pbuf_free(this_pb);
free(packet);
}
@ -196,11 +201,12 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
/**
* @brief Check if any of the interfaces is up
*/
static bool _udp_pcb_is_in_use(void){
static bool _udp_pcb_is_in_use(void)
{
int i, p;
for (i=0; i<MDNS_MAX_INTERFACES; i++) {
for (p=0; p<MDNS_IP_PROTOCOL_MAX; p++) {
if(_mdns_server->interfaces[i].pcbs[p].pcb){
for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
for (p = 0; p < MDNS_IP_PROTOCOL_MAX; p++) {
if (_mdns_server->interfaces[i].pcbs[p].pcb) {
return true;
}
}
@ -216,7 +222,7 @@ static void _udp_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
if (!_mdns_server) {
return;
}
mdns_pcb_t * _pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
mdns_pcb_t *_pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
if (_pcb->pcb) {
free(_pcb->probe_services);
_pcb->state = PCB_OFF;
@ -227,7 +233,7 @@ static void _udp_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
_pcb->probe_running = false;
_pcb->failed_probes = 0;
_udp_join_group(tcpip_if, ip_protocol, false);
if(!_udp_pcb_is_in_use()) {
if (!_udp_pcb_is_in_use()) {
_udp_pcb_main_deinit();
}
}
@ -243,12 +249,12 @@ static esp_err_t _udp_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protoco
}
esp_err_t err = _udp_join_group(tcpip_if, ip_protocol, true);
if(err){
if (err) {
return err;
}
err = _udp_pcb_main_init();
if(err){
if (err) {
return err;
}
@ -272,7 +278,7 @@ typedef struct {
*/
static err_t _mdns_pcb_init_api(struct tcpip_api_call_data *api_call_msg)
{
mdns_api_call_t * msg = (mdns_api_call_t *)api_call_msg;
mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg;
msg->err = _udp_pcb_init(msg->tcpip_if, msg->ip_protocol);
return msg->err;
}
@ -282,7 +288,7 @@ static err_t _mdns_pcb_init_api(struct tcpip_api_call_data *api_call_msg)
*/
static err_t _mdns_pcb_deinit_api(struct tcpip_api_call_data *api_call_msg)
{
mdns_api_call_t * msg = (mdns_api_call_t *)api_call_msg;
mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg;
_udp_pcb_deinit(msg->tcpip_if, msg->ip_protocol);
msg->err = ESP_OK;
return ESP_OK;
@ -315,9 +321,9 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
static err_t _mdns_udp_pcb_write_api(struct tcpip_api_call_data *api_call_msg)
{
void * nif = NULL;
mdns_api_call_t * msg = (mdns_api_call_t *)api_call_msg;
mdns_pcb_t * _pcb = &_mdns_server->interfaces[msg->tcpip_if].pcbs[msg->ip_protocol];
void *nif = NULL;
mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg;
mdns_pcb_t *_pcb = &_mdns_server->interfaces[msg->tcpip_if].pcbs[msg->ip_protocol];
nif = esp_netif_get_netif_impl(_mdns_get_esp_netif(msg->tcpip_if));
if (!nif) {
pbuf_free(msg->pbt);
@ -330,9 +336,9 @@ static err_t _mdns_udp_pcb_write_api(struct tcpip_api_call_data *api_call_msg)
return err;
}
size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t * data, size_t len)
size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len)
{
struct pbuf* pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
struct pbuf *pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (pbt == NULL) {
return 0;
}
@ -361,7 +367,7 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c
return len;
}
void* _mdns_get_packet_data(mdns_rx_packet_t *packet)
void *_mdns_get_packet_data(mdns_rx_packet_t *packet)
{
return packet->pb->payload;
}

View File

@ -27,7 +27,7 @@
#include <net/if.h>
#endif
extern mdns_server_t * _mdns_server;
extern mdns_server_t *_mdns_server;
static const char *TAG = "MDNS_Networking";
static bool s_run_sock_recv_task = false;
@ -37,8 +37,8 @@ static int join_mdns_multicast_group(int sock, esp_netif_t *netif, mdns_ip_proto
#if defined(CONFIG_IDF_TARGET_LINUX)
// Need to define packet buffer struct on linux
struct pbuf {
struct pbuf * next;
void * payload;
struct pbuf *next;
void *payload;
size_t tot_len;
size_t len;
};
@ -52,17 +52,17 @@ static void delete_socket(int sock)
close(sock);
}
static struct udp_pcb* sock_to_pcb(int sock)
static struct udp_pcb *sock_to_pcb(int sock)
{
if (sock < 0) {
return NULL;
}
// Note: sock=0 is a valid descriptor, so save it as +1 ("1" is a valid pointer)
intptr_t sock_plus_one = sock + 1;
return (struct udp_pcb*)sock_plus_one;
return (struct udp_pcb *)sock_plus_one;
}
static int pcb_to_sock(struct udp_pcb* pcb)
static int pcb_to_sock(struct udp_pcb *pcb)
{
if (pcb == NULL) {
return -1;
@ -71,7 +71,7 @@ static int pcb_to_sock(struct udp_pcb* pcb)
return sock_plus_one - 1;
}
void* _mdns_get_packet_data(mdns_rx_packet_t *packet)
void *_mdns_get_packet_data(mdns_rx_packet_t *packet)
{
return packet->pb->payload;
}
@ -90,10 +90,10 @@ void _mdns_packet_free(mdns_rx_packet_t *packet)
esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
{
struct udp_pcb * pcb = _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb;
struct udp_pcb *pcb = _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb;
_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb = NULL;
if (_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].pcb == NULL &&
_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].pcb == NULL) {
_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].pcb == NULL) {
// if the interface for both protocol uninitialized, close the interface socket
int sock = pcb_to_sock(pcb);
if (sock >= 0) {
@ -101,11 +101,13 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
}
}
for (int i=0; i<MDNS_MAX_INTERFACES; i++) {
for (int j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
for (int i = 0; i < MDNS_MAX_INTERFACES; i++) {
for (int j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
if (_mdns_server->interfaces[i].pcbs[j].pcb)
// If any of the interfaces/protocol initialized
{
return ESP_OK;
}
}
}
@ -117,15 +119,15 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
#if defined(CONFIG_IDF_TARGET_LINUX)
#ifdef CONFIG_LWIP_IPV6
static char* inet6_ntoa_r(struct in6_addr addr, char* ptr, size_t size)
static char *inet6_ntoa_r(struct in6_addr addr, char *ptr, size_t size)
{
inet_ntop(AF_INET6, &(addr.s6_addr32[0]), ptr, size);
return ptr;
}
#endif // CONFIG_LWIP_IPV6
static char* inet_ntoa_r(struct in_addr addr, char* ptr, size_t size)
static char *inet_ntoa_r(struct in_addr addr, char *ptr, size_t size)
{
char * res = inet_ntoa(addr);
char *res = inet_ntoa(addr);
if (res && strlen(res) < size) {
strcpy(ptr, res);
}
@ -133,7 +135,7 @@ static char* inet_ntoa_r(struct in_addr addr, char* ptr, size_t size)
}
#endif // CONFIG_IDF_TARGET_LINUX
static inline char* get_string_address(struct sockaddr_storage *source_addr)
static inline char *get_string_address(struct sockaddr_storage *source_addr)
{
static char address_str[40]; // 40=(8*4+7+term) is the max size of ascii IPv6 addr "XXXX:XX...XX:XXXX"
char *res = NULL;
@ -175,7 +177,7 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p
in_addr->s2_len = sizeof(struct sockaddr_in6);
#endif
ss_addr_len = sizeof(struct sockaddr_in6);
struct sockaddr_in6 * in_addr_ip6 = (struct sockaddr_in6 *)in_addr;
struct sockaddr_in6 *in_addr_ip6 = (struct sockaddr_in6 *)in_addr;
uint32_t *u32_addr = in_addr_ip6->sin6_addr.s6_addr32;
in_addr_ip6->sin6_port = port;
u32_addr[0] = addr->u_addr.ip6.addr[0];
@ -187,7 +189,7 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p
return ss_addr_len;
}
size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t * data, size_t len)
size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len)
{
int sock = pcb_to_sock(_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb);
if (sock < 0) {
@ -210,7 +212,7 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c
static inline void inet_to_espaddr(const struct sockaddr_storage *in_addr, esp_ip_addr_t *addr, uint16_t *port)
{
if (in_addr->ss_family == PF_INET) {
struct sockaddr_in * in_addr_ip4 = (struct sockaddr_in *)in_addr;
struct sockaddr_in *in_addr_ip4 = (struct sockaddr_in *)in_addr;
memset(addr, 0, sizeof(esp_ip_addr_t));
*port = in_addr_ip4->sin_port;
addr->u_addr.ip4.addr = in_addr_ip4->sin_addr.s_addr;
@ -218,7 +220,7 @@ static inline void inet_to_espaddr(const struct sockaddr_storage *in_addr, esp_i
}
#if CONFIG_LWIP_IPV6
else if (in_addr->ss_family == PF_INET6) {
struct sockaddr_in6 * in_addr_ip6 = (struct sockaddr_in6 *)in_addr;
struct sockaddr_in6 *in_addr_ip6 = (struct sockaddr_in6 *)in_addr;
memset(addr, 0, sizeof(esp_ip_addr_t));
*port = in_addr_ip6->sin6_port;
uint32_t *u32_addr = in_addr_ip6->sin6_addr.s6_addr32;
@ -237,7 +239,7 @@ static inline void inet_to_espaddr(const struct sockaddr_storage *in_addr, esp_i
#endif // CONFIG_LWIP_IPV6
}
void sock_recv_task(void* arg)
void sock_recv_task(void *arg)
{
while (s_run_sock_recv_task) {
struct timeval tv = {
@ -247,8 +249,8 @@ void sock_recv_task(void* arg)
fd_set rfds;
FD_ZERO(&rfds);
int max_sock = -1;
for (int i=0; i<MDNS_MAX_INTERFACES; i++) {
for (int j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
for (int i = 0; i < MDNS_MAX_INTERFACES; i++) {
for (int j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
int sock = pcb_to_sock(_mdns_server->interfaces[i].pcbs[j].pcb);
if (sock >= 0) {
FD_SET(sock, &rfds);
@ -267,7 +269,7 @@ void sock_recv_task(void* arg)
ESP_LOGE(TAG, "Select failed. errno=%d: %s", errno, strerror(errno));
break;
} else if (s > 0) {
for (int tcpip_if=0; tcpip_if<MDNS_MAX_INTERFACES; tcpip_if++) {
for (int tcpip_if = 0; tcpip_if < MDNS_MAX_INTERFACES; tcpip_if++) {
// Both protocols share once socket
int sock = pcb_to_sock(_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].pcb);
if (sock < 0) {
@ -320,7 +322,7 @@ void sock_recv_task(void* arg)
packet->multicast = 1;
packet->dest.type = packet->src.type;
packet->ip_protocol =
packet->src.type == ESP_IPADDR_TYPE_V4 ? MDNS_IP_PROTOCOL_V4 : MDNS_IP_PROTOCOL_V6;
packet->src.type == ESP_IPADDR_TYPE_V4 ? MDNS_IP_PROTOCOL_V4 : MDNS_IP_PROTOCOL_V6;
if (!_mdns_server || !_mdns_server->action_queue || _mdns_send_rx_action(packet) != ESP_OK) {
ESP_LOGE(TAG, "_mdns_send_rx_action failed!");
free(packet->pb->payload);
@ -338,19 +340,19 @@ static void mdns_networking_init(void)
{
if (s_run_sock_recv_task == false) {
s_run_sock_recv_task = true;
xTaskCreate( sock_recv_task, "mdns recv task", 3*1024, NULL, 5, NULL );
xTaskCreate( sock_recv_task, "mdns recv task", 3 * 1024, NULL, 5, NULL );
}
}
static struct udp_pcb* create_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
static struct udp_pcb *create_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
{
if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) {
return _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb;
}
mdns_ip_protocol_t other_ip_proto = ip_protocol==MDNS_IP_PROTOCOL_V4?MDNS_IP_PROTOCOL_V6:MDNS_IP_PROTOCOL_V4;
mdns_ip_protocol_t other_ip_proto = ip_protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_IP_PROTOCOL_V6 : MDNS_IP_PROTOCOL_V4;
esp_netif_t *netif = _mdns_get_esp_netif(tcpip_if);
if (_mdns_server->interfaces[tcpip_if].pcbs[other_ip_proto].pcb) {
struct udp_pcb* other_pcb = _mdns_server->interfaces[tcpip_if].pcbs[other_ip_proto].pcb;
struct udp_pcb *other_pcb = _mdns_server->interfaces[tcpip_if].pcbs[other_ip_proto].pcb;
int err = join_mdns_multicast_group(pcb_to_sock(other_pcb), netif, ip_protocol);
if (err < 0) {
ESP_LOGE(TAG, "Failed to add ipv6 multicast group for protocol %d", ip_protocol);
@ -420,7 +422,7 @@ static int create_socket(esp_netif_t *netif)
#endif // CONFIG_LWIP_IPV6
struct ifreq ifr;
esp_netif_get_netif_impl_name(netif, ifr.ifr_name);
int ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(struct ifreq));
int ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(struct ifreq));
if (ret < 0) {
ESP_LOGE(TAG, "\"%s\" Unable to bind socket to specified interface. errno=%d: %s", esp_netif_get_desc(netif), errno, strerror(errno));
goto err;
@ -477,7 +479,7 @@ static int socket_add_ipv4_multicast_group(int sock, esp_netif_t *netif)
goto err;
}
err:
err:
return err;
}

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ESP_MDNS_NETWORKING_H_
#define ESP_MDNS_NETWORKING_H_
@ -12,7 +17,7 @@
/**
* @brief Queue RX packet action
*/
esp_err_t _mdns_send_rx_action(mdns_rx_packet_t * packet);
esp_err_t _mdns_send_rx_action(mdns_rx_packet_t *packet);
/**
* @brief Start PCB
@ -33,12 +38,12 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
*
* @return length of sent packet or 0 on error
*/
size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t * data, size_t len);
size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len);
/**
* @brief Gets data pointer to the mDNS packet
*/
void* _mdns_get_packet_data(mdns_rx_packet_t *packet);
void *_mdns_get_packet_data(mdns_rx_packet_t *packet);
/**
* @brief Gets data length of c

View File

@ -231,27 +231,27 @@ typedef struct {
} mdns_name_t;
typedef struct mdns_parsed_question_s {
struct mdns_parsed_question_s * next;
struct mdns_parsed_question_s *next;
uint16_t type;
bool sub;
bool unicast;
char * host;
char * service;
char * proto;
char * domain;
char *host;
char *service;
char *proto;
char *domain;
} mdns_parsed_question_t;
typedef struct mdns_parsed_record_s {
struct mdns_parsed_record_s * next;
struct mdns_parsed_record_s *next;
mdns_parsed_record_type_t record_type;
uint16_t type;
uint16_t clas;
uint8_t flush;
uint32_t ttl;
char * host;
char * service;
char * proto;
char * domain;
char *host;
char *service;
char *proto;
char *domain;
uint16_t data_len;
uint8_t *data;
} mdns_parsed_record_t;
@ -266,8 +266,8 @@ typedef struct {
uint8_t probe;
uint8_t discovery;
uint8_t distributed;
mdns_parsed_question_t * questions;
mdns_parsed_record_t * records;
mdns_parsed_question_t *questions;
mdns_parsed_record_t *records;
uint16_t id;
} mdns_parsed_packet_t;
@ -282,65 +282,65 @@ typedef struct {
} mdns_rx_packet_t;
typedef struct mdns_txt_linked_item_s {
const char * key; /*!< item key name */
char * value; /*!< item value string */
const char *key; /*!< item key name */
char *value; /*!< item value string */
uint8_t value_len; /*!< item value length */
struct mdns_txt_linked_item_s * next; /*!< next result, or NULL for the last result in the list */
struct mdns_txt_linked_item_s *next; /*!< next result, or NULL for the last result in the list */
} mdns_txt_linked_item_t;
typedef struct mdns_subtype_s {
const char *subtype; /*!< subtype */
struct mdns_subtype_s * next; /*!< next result, or NULL for the last result in the list */
struct mdns_subtype_s *next; /*!< next result, or NULL for the last result in the list */
} mdns_subtype_t;
typedef struct {
const char * instance;
const char * service;
const char * proto;
const char * hostname;
const char *instance;
const char *service;
const char *proto;
const char *hostname;
uint16_t priority;
uint16_t weight;
uint16_t port;
mdns_txt_linked_item_t * txt;
mdns_txt_linked_item_t *txt;
mdns_subtype_t *subtype;
} mdns_service_t;
typedef struct mdns_srv_item_s {
struct mdns_srv_item_s * next;
mdns_service_t * service;
struct mdns_srv_item_s *next;
mdns_service_t *service;
} mdns_srv_item_t;
typedef struct mdns_out_question_s {
struct mdns_out_question_s * next;
struct mdns_out_question_s *next;
uint16_t type;
bool unicast;
const char * host;
const char * service;
const char * proto;
const char * domain;
const char *host;
const char *service;
const char *proto;
const char *domain;
bool own_dynamic_memory;
} mdns_out_question_t;
typedef struct mdns_host_item_t {
const char * hostname;
const char *hostname;
mdns_ip_addr_t *address_list;
struct mdns_host_item_t *next;
} mdns_host_item_t;
typedef struct mdns_out_answer_s {
struct mdns_out_answer_s * next;
struct mdns_out_answer_s *next;
uint16_t type;
uint8_t bye;
uint8_t flush;
mdns_service_t * service;
mdns_host_item_t* host;
const char * custom_instance;
const char * custom_service;
const char * custom_proto;
mdns_service_t *service;
mdns_host_item_t *host;
const char *custom_instance;
const char *custom_service;
const char *custom_proto;
} mdns_out_answer_t;
typedef struct mdns_tx_packet_s {
struct mdns_tx_packet_s * next;
struct mdns_tx_packet_s *next;
uint32_t send_at;
mdns_if_t tcpip_if;
mdns_ip_protocol_t ip_protocol;
@ -348,18 +348,18 @@ typedef struct mdns_tx_packet_s {
uint16_t port;
uint16_t flags;
uint8_t distributed;
mdns_out_question_t * questions;
mdns_out_answer_t * answers;
mdns_out_answer_t * servers;
mdns_out_answer_t * additional;
mdns_out_question_t *questions;
mdns_out_answer_t *answers;
mdns_out_answer_t *servers;
mdns_out_answer_t *additional;
bool queued;
uint16_t id;
} mdns_tx_packet_t;
typedef struct {
mdns_pcb_state_t state;
struct udp_pcb * pcb;
mdns_srv_item_t ** probe_services;
struct udp_pcb *pcb;
mdns_srv_item_t **probe_services;
uint8_t probe_services_len;
uint8_t probe_ip;
uint8_t probe_running;
@ -374,7 +374,7 @@ typedef enum {
} mdns_search_once_state_t;
typedef struct mdns_search_once_s {
struct mdns_search_once_s * next;
struct mdns_search_once_s *next;
mdns_search_once_state_t state;
uint32_t started_at;
@ -386,23 +386,23 @@ typedef struct mdns_search_once_s {
bool unicast;
uint8_t max_results;
uint8_t num_results;
char * instance;
char * service;
char * proto;
mdns_result_t * result;
char *instance;
char *service;
char *proto;
mdns_result_t *result;
} mdns_search_once_t;
typedef struct mdns_server_s {
struct {
mdns_pcb_t pcbs[MDNS_IP_PROTOCOL_MAX];
} interfaces[MDNS_MAX_INTERFACES];
const char * hostname;
const char * instance;
mdns_srv_item_t * services;
const char *hostname;
const char *instance;
mdns_srv_item_t *services;
SemaphoreHandle_t lock;
QueueHandle_t action_queue;
mdns_tx_packet_t * tx_queue_head;
mdns_search_once_t * search_once;
mdns_tx_packet_t *tx_queue_head;
mdns_search_once_t *search_once;
esp_timer_handle_t timer_handle;
} mdns_server_t;
@ -410,57 +410,57 @@ typedef struct {
mdns_action_type_t type;
union {
struct {
char * hostname;
char *hostname;
TaskHandle_t calling_task;
} hostname_set;
char * instance;
char *instance;
struct {
mdns_if_t interface;
mdns_event_actions_t event_action;
} sys_event;
struct {
mdns_srv_item_t * service;
mdns_srv_item_t *service;
} srv_add;
struct {
mdns_srv_item_t * service;
mdns_srv_item_t *service;
} srv_del;
struct {
mdns_srv_item_t * service;
char * instance;
mdns_srv_item_t *service;
char *instance;
} srv_instance;
struct {
mdns_srv_item_t * service;
mdns_srv_item_t *service;
uint16_t port;
} srv_port;
struct {
mdns_srv_item_t * service;
mdns_txt_linked_item_t * txt;
mdns_srv_item_t *service;
mdns_txt_linked_item_t *txt;
} srv_txt_replace;
struct {
mdns_srv_item_t * service;
char * key;
char * value;
mdns_srv_item_t *service;
char *key;
char *value;
uint8_t value_len;
} srv_txt_set;
struct {
mdns_srv_item_t * service;
char * key;
mdns_srv_item_t *service;
char *key;
} srv_txt_del;
struct {
mdns_srv_item_t * service;
char * subtype;
mdns_srv_item_t *service;
char *subtype;
} srv_subtype_add;
struct {
mdns_search_once_t * search;
mdns_search_once_t *search;
} search_add;
struct {
mdns_tx_packet_t * packet;
mdns_tx_packet_t *packet;
} tx_handle;
struct {
mdns_rx_packet_t * packet;
mdns_rx_packet_t *packet;
} rx_handle;
struct {
const char * hostname;
const char *hostname;
mdns_ip_addr_t *address_list;
} delegate_hostname;
} data;

View File

@ -1,29 +1,21 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp_err.h"
#include "esp_event.h"
const char * WIFI_EVENT = "WIFI_EVENT";
const char * IP_EVENT = "IP_EVENT";
const char *WIFI_EVENT = "WIFI_EVENT";
const char *IP_EVENT = "IP_EVENT";
esp_err_t esp_event_handler_register(const char * event_base, int32_t event_id, void* event_handler, void* event_handler_arg)
esp_err_t esp_event_handler_register(const char *event_base, int32_t event_id, void *event_handler, void *event_handler_arg)
{
return ESP_OK;
}
esp_err_t esp_event_handler_unregister(const char * event_base, int32_t event_id, void* event_handler)
esp_err_t esp_event_handler_unregister(const char *event_base, int32_t event_id, void *event_handler)
{
return ESP_OK;
}

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "stdbool.h"
@ -21,12 +13,12 @@
#define ESP_EVENT_DECLARE_BASE(x)
#define ESP_EVENT_ANY_ID (-1)
typedef void * esp_event_base_t;
typedef void * system_event_t;
typedef void *esp_event_base_t;
typedef void *system_event_t;
const char* WIFI_EVENT;
const char* IP_EVENT;
const char *WIFI_EVENT;
const char *IP_EVENT;
esp_err_t esp_event_handler_register(const char * event_base, int32_t event_id, void* event_handler, void* event_handler_arg);
esp_err_t esp_event_handler_register(const char *event_base, int32_t event_id, void *event_handler, void *event_handler_arg);
esp_err_t esp_event_handler_unregister(const char * event_base, int32_t event_id, void* event_handler);
esp_err_t esp_event_handler_unregister(const char *event_base, int32_t event_id, void *event_handler);

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
typedef enum {

View File

@ -1,24 +1,16 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include<stdio.h>
#include <stdlib.h>
#include "esp_netif.h"
#include "esp_err.h"
#include <string.h> //strlen
#include <string.h> //strlen
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <arpa/inet.h> //inet_addr
#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if.h>
@ -26,17 +18,16 @@
#define MAX_NETIFS 4
static esp_netif_t* s_netif_list[MAX_NETIFS] = { 0 };
static esp_netif_t *s_netif_list[MAX_NETIFS] = { 0 };
struct esp_netif_obj
{
struct esp_netif_obj {
const char *if_key;
const char *if_desc;
};
esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key)
{
for (int i=0; i<MAX_NETIFS; ++i) {
for (int i = 0; i < MAX_NETIFS; ++i) {
if (s_netif_list[i] && strcmp(s_netif_list[i]->if_key, if_key) == 0) {
return s_netif_list[i];
}
@ -84,15 +75,14 @@ esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if
getifaddrs(&addrs);
tmp = addrs;
while (tmp)
{
while (tmp) {
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET6) {
char addr[64];
struct sockaddr_in6 *pAddr = (struct sockaddr_in6 *)tmp->ifa_addr;
inet_ntop(AF_INET6, &pAddr->sin6_addr, addr, sizeof(addr) );
if (strcmp(esp_netif->if_desc, tmp->ifa_name) == 0) {
printf("AF_INET6: %s: %s\n", tmp->ifa_name, addr);
memcpy(if_ip6->addr, &pAddr->sin6_addr, 4*4);
memcpy(if_ip6->addr, &pAddr->sin6_addr, 4 * 4);
break;
}
}
@ -113,7 +103,7 @@ int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif)
return interfaceIndex;
}
esp_err_t esp_netif_get_netif_impl_name(esp_netif_t *esp_netif, char* name)
esp_err_t esp_netif_get_netif_impl_name(esp_netif_t *esp_netif, char *name)
{
if (esp_netif == NULL) {
return ESP_ERR_INVALID_STATE;
@ -135,13 +125,13 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *config)
if (esp_netif_get_handle_from_ifkey(config->base->if_key)) {
return NULL;
}
esp_netif_t* netif = calloc(1, sizeof(struct esp_netif_obj));
esp_netif_t *netif = calloc(1, sizeof(struct esp_netif_obj));
if (netif) {
netif->if_desc = config->base->if_desc;
netif->if_key = config->base->if_key;
}
for (int i=0; i<MAX_NETIFS; ++i) {
for (int i = 0; i < MAX_NETIFS; ++i) {
if (s_netif_list[i] == NULL) {
s_netif_list[i] = netif;
break;
@ -153,7 +143,7 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *config)
void esp_netif_destroy(esp_netif_t *esp_netif)
{
for (int i=0; i<MAX_NETIFS; ++i) {
for (int i = 0; i < MAX_NETIFS; ++i) {
if (s_netif_list[i] == esp_netif) {
s_netif_list[i] = NULL;
break;

View File

@ -1,15 +1,7 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_event.h"

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp_err.h"
#include "esp_log.h"
#include <stdlib.h>
@ -27,8 +19,8 @@ void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16
{
if ( LOG_LOCAL_LEVEL >= log_level ) {
ESP_LOG_LEVEL(log_level, tag, "Buffer:%p length:%d", buffer, buff_len);
for (int i=0; i<buff_len; ++i) {
printf("%02x ", ((uint8_t*)buffer)[i]);
for (int i = 0; i < buff_len; ++i) {
printf("%02x ", ((uint8_t *)buffer)[i]);
}
printf("\n");
}

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
size_t strlcat(char *dest, const char *src, size_t size);

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include_next "endian.h"

View File

@ -1,4 +1,4 @@
/* $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ */
/* $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ */
/*-
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
@ -38,9 +38,9 @@
*/
size_t
strlcat(dst, src, siz)
char *dst;
const char *src;
size_t siz;
char *dst;
const char *src;
size_t siz;
{
char *d = dst;
const char *s = src;
@ -48,13 +48,15 @@ strlcat(dst, src, siz)
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
while (n-- != 0 && *d != '\0') {
d++;
}
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
if (n == 0) {
return (dlen + strlen(s));
}
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
@ -64,5 +66,5 @@ strlcat(dst, src, siz)
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
return (dlen + (s - src)); /* count does not include NUL */
}

View File

@ -1,29 +1,21 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp_err.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
void * create_tt(esp_timer_cb_t cb);
void *create_tt(esp_timer_cb_t cb);
void destroy_tt(void* tt);
void destroy_tt(void *tt);
void set_tout(void* tt, uint32_t ms);
void set_tout(void *tt, uint32_t ms);
esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
esp_timer_handle_t* out_handle)
esp_err_t esp_timer_create(const esp_timer_create_args_t *create_args,
esp_timer_handle_t *out_handle)
{
*out_handle = (esp_timer_handle_t)create_tt(create_args->callback);
return ESP_OK;
@ -31,7 +23,7 @@ esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period)
{
set_tout(timer, period/1000);
set_tout(timer, period / 1000);
return ESP_OK;
}

View File

@ -1,24 +1,16 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdbool.h>
#include <stdint.h>
typedef struct esp_timer* esp_timer_handle_t;
typedef struct esp_timer *esp_timer_handle_t;
typedef void (*esp_timer_cb_t)(void* arg);
typedef void (*esp_timer_cb_t)(void *arg);
typedef enum {
ESP_TIMER_TASK,
@ -26,14 +18,14 @@ typedef enum {
typedef struct {
esp_timer_cb_t callback; //!< Function to call when timer expires
void* arg; //!< Argument to pass to the callback
void *arg; //!< Argument to pass to the callback
esp_timer_dispatch_t dispatch_method; //!< Call the callback from task or from ISR
const char* name; //!< Timer name, used in esp_timer_dump function
const char *name; //!< Timer name, used in esp_timer_dump function
bool skip_unhandled_events; //!< Skip unhandled events for periodic timers
} esp_timer_create_args_t;
esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
esp_timer_handle_t* out_handle);
esp_err_t esp_timer_create(const esp_timer_create_args_t *create_args,
esp_timer_handle_t *out_handle);
esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period);
esp_err_t esp_timer_stop(esp_timer_handle_t timer);

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "timer_task.hpp"
#include <cstdint>
@ -18,21 +10,21 @@
#include <memory>
#include <cstring>
extern "C" void * create_tt(cb_t cb)
extern "C" void *create_tt(cb_t cb)
{
auto * tt = new TimerTaskMock(cb);
auto *tt = new TimerTaskMock(cb);
return tt;
}
extern "C" void destroy_tt(void* tt)
extern "C" void destroy_tt(void *tt)
{
auto * timer_task = static_cast<TimerTaskMock *>(tt);
delete(timer_task);
auto *timer_task = static_cast<TimerTaskMock *>(tt);
delete (timer_task);
}
extern "C" void set_tout(void* tt, uint32_t ms)
extern "C" void set_tout(void *tt, uint32_t ms)
{
auto * timer_task = static_cast<TimerTaskMock *>(tt);
auto *timer_task = static_cast<TimerTaskMock *>(tt);
timer_task->SetTimeout(ms);
}

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <queue>
@ -20,13 +12,16 @@
#include <thread>
#include <atomic>
typedef void (*cb_t)(void* arg);
typedef void (*cb_t)(void *arg);
class TimerTaskMock
{
class TimerTaskMock {
public:
TimerTaskMock(cb_t cb): cb(cb), t(run_static, this), active(false), ms(INT32_MAX) {}
~TimerTaskMock(void) { active = false; t.join(); }
~TimerTaskMock(void)
{
active = false;
t.join();
}
void SetTimeout(uint32_t m)
{
@ -36,7 +31,7 @@ public:
private:
static void run_static(TimerTaskMock* timer)
static void run_static(TimerTaskMock *timer)
{
timer->run();
}

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <unistd.h>
#include "freertos/FreeRTOS.h"
@ -20,39 +12,39 @@
#include <stdlib.h>
#include <string.h>
void * create_q(void);
void *create_q(void);
void destroy_q(void* q);
void destroy_q(void *q);
bool send_q(void* q, uint8_t *data, size_t len);
bool send_q(void *q, uint8_t *data, size_t len);
bool recv_q(void* q, uint8_t *data, size_t len, uint32_t ms);
bool recv_q(void *q, uint8_t *data, size_t len, uint32_t ms);
static uint64_t s_semaphore_data = 0;
struct queue_handle {
size_t item_size;
void * q;
void *q;
};
QueueHandle_t xQueueCreate( uint32_t uxQueueLength, uint32_t uxItemSize )
{
struct queue_handle * h = calloc(1, sizeof(struct queue_handle));
struct queue_handle *h = calloc(1, sizeof(struct queue_handle));
h->item_size = uxItemSize;
h->q = create_q();
return (QueueHandle_t)h;
}
uint32_t xQueueSend(QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait)
uint32_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait)
{
struct queue_handle * h = xQueue;
return send_q(h->q, (uint8_t*)pvItemToQueue, h->item_size) ? pdTRUE : pdFAIL;
struct queue_handle *h = xQueue;
return send_q(h->q, (uint8_t *)pvItemToQueue, h->item_size) ? pdTRUE : pdFAIL;
}
uint32_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait)
{
struct queue_handle * h = xQueue;
return recv_q(h->q, (uint8_t*)pvBuffer, h->item_size, xTicksToWait) ? pdTRUE : pdFAIL;
struct queue_handle *h = xQueue;
return recv_q(h->q, (uint8_t *)pvBuffer, h->item_size, xTicksToWait) ? pdTRUE : pdFAIL;
}
BaseType_t xSemaphoreGive( QueueHandle_t xQueue)
@ -67,7 +59,7 @@ BaseType_t xSemaphoreTake( QueueHandle_t xQueue, TickType_t pvTask )
void vQueueDelete( QueueHandle_t xQueue )
{
struct queue_handle * h = xQueue;
struct queue_handle *h = xQueue;
if (h->q) {
destroy_q(h->q);
}
@ -107,18 +99,18 @@ TickType_t xTaskGetTickCount( void )
void vTaskDelay( const TickType_t xTicksToDelay )
{
usleep(xTicksToDelay*1000);
usleep(xTicksToDelay * 1000);
}
void * pthread_task(void * params)
void *pthread_task(void *params)
{
struct {
void * const param;
void *const param;
TaskFunction_t task;
bool started;
} *pthread_params = params;
void * const param = pthread_params->param;
void *const param = pthread_params->param;
TaskFunction_t task = pthread_params->task;
pthread_params->started = true;
@ -127,25 +119,25 @@ void * pthread_task(void * params)
return NULL;
}
BaseType_t xTaskCreatePinnedToCore( TaskFunction_t pvTaskCode,
const char * const pcName,
const uint32_t usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t * const pvCreatedTask,
const BaseType_t xCoreID)
BaseType_t xTaskCreatePinnedToCore( TaskFunction_t pvTaskCode,
const char *const pcName,
const uint32_t usStackDepth,
void *const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *const pvCreatedTask,
const BaseType_t xCoreID)
{
xTaskCreate(pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pvCreatedTask);
return pdTRUE;
}
void xTaskCreate(TaskFunction_t pvTaskCode, const char * const pcName, const uint32_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pvCreatedTask)
void xTaskCreate(TaskFunction_t pvTaskCode, const char *const pcName, const uint32_t usStackDepth, void *const pvParameters, UBaseType_t uxPriority, TaskHandle_t *const pvCreatedTask)
{
pthread_t new_thread = (pthread_t)NULL;
pthread_attr_t attr;
struct {
void * const param;
void *const param;
TaskFunction_t task;
bool started;
} pthread_params = { .param = pvParameters, .task = pvTaskCode};
@ -157,7 +149,7 @@ void xTaskCreate(TaskFunction_t pvTaskCode, const char * const pcName, const uin
assert(res == 0);
if (pvCreatedTask) {
*pvCreatedTask = (void*)new_thread;
*pvCreatedTask = (void *)new_thread;
}
// just wait till the task started so we can unwind params from the stack

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "freertos/FreeRTOS.h"

View File

@ -13,20 +13,20 @@
#define portTICK_PERIOD_MS 1
#define portMAX_DELAY ( TickType_t ) 0xffffffffUL
typedef void * SemaphoreHandle_t;
typedef void * QueueHandle_t;
typedef void * TaskHandle_t;
typedef void *SemaphoreHandle_t;
typedef void *QueueHandle_t;
typedef void *TaskHandle_t;
typedef uint32_t TickType_t;
typedef void (*TaskFunction_t)( void * );
typedef unsigned int UBaseType_t;
typedef int BaseType_t;
typedef unsigned int UBaseType_t;
typedef int BaseType_t;
#define pdFALSE ( ( BaseType_t ) 0 )
#define pdTRUE ( ( BaseType_t ) 1 )
#define pdFALSE ( ( BaseType_t ) 0 )
#define pdTRUE ( ( BaseType_t ) 1 )
#define pdPASS ( pdTRUE )
#define pdFAIL ( pdFALSE )
#define pdPASS ( pdTRUE )
#define pdFAIL ( pdFALSE )
#define pdMS_TO_TICKS(tick) (tick)

View File

@ -18,15 +18,15 @@ TaskHandle_t xTaskGetCurrentTaskHandle(void);
BaseType_t xTaskNotifyWait(uint32_t bits_entry_clear, uint32_t bits_exit_clear, uint32_t *value, TickType_t wait_time );
BaseType_t xTaskCreatePinnedToCore( TaskFunction_t pvTaskCode,
const char * const pcName,
const uint32_t usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t * const pvCreatedTask,
const BaseType_t xCoreID);
BaseType_t xTaskCreatePinnedToCore( TaskFunction_t pvTaskCode,
const char *const pcName,
const uint32_t usStackDepth,
void *const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *const pvCreatedTask,
const BaseType_t xCoreID);
void xTaskCreate(TaskFunction_t pvTaskCode, const char * const pcName, const uint32_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pvCreatedTask);
void xTaskCreate(TaskFunction_t pvTaskCode, const char *const pcName, const uint32_t usStackDepth, void *const pvParameters, UBaseType_t uxPriority, TaskHandle_t *const pvCreatedTask);
TickType_t xTaskGetTickCount( void );
@ -45,6 +45,6 @@ void vTaskDelete(TaskHandle_t *task);
QueueHandle_t xQueueCreate( uint32_t uxQueueLength,
uint32_t uxItemSize );
uint32_t xQueueSend(QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait);
uint32_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait);
uint32_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait);

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "queue_unique_ptr.hpp"
#include <cstdint>
@ -18,28 +10,28 @@
#include <memory>
#include <cstring>
extern "C" void * create_q(void)
extern "C" void *create_q(void)
{
auto * q = new QueueMock<std::vector<uint8_t>>();
auto *q = new QueueMock<std::vector<uint8_t>>();
return q;
}
extern "C" void destroy_q(void* q)
extern "C" void destroy_q(void *q)
{
auto * queue = static_cast<QueueMock<std::vector<uint8_t>> *>(q);
delete(queue);
auto *queue = static_cast<QueueMock<std::vector<uint8_t>> *>(q);
delete (queue);
}
extern "C" bool send_q(void* q, uint8_t *data, size_t len)
extern "C" bool send_q(void *q, uint8_t *data, size_t len)
{
auto v = std::make_unique<std::vector<uint8_t>>(len);
v->assign(data, data+len);
v->assign(data, data + len);
auto queue = static_cast<QueueMock<std::vector<uint8_t>> *>(q);
queue->send(std::move(v));
return true;
}
extern "C" bool recv_q(void* q, uint8_t *data, size_t len, uint32_t ms)
extern "C" bool recv_q(void *q, uint8_t *data, size_t len, uint32_t ms)
{
auto queue = static_cast<QueueMock<std::vector<uint8_t>> *>(q);
auto v = queue->receive(ms);

View File

@ -1,16 +1,8 @@
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
@ -22,31 +14,30 @@
#include <atomic>
template <class T>
class QueueMock
{
class QueueMock {
public:
QueueMock(void): q(), m(), c() {}
~QueueMock(void) {}
void send(std::unique_ptr<T> t)
{
std::lock_guard<std::mutex> lock(m);
q.push(std::move(t));
c.notify_one();
}
std::unique_ptr<T> receive(uint32_t ms)
{
std::unique_lock<std::mutex> lock(m);
while(q.empty()) {
if (c.wait_for(lock, std::chrono::milliseconds(ms)) == std::cv_status::timeout) {
return nullptr;
}
void send(std::unique_ptr<T> t)
{
std::lock_guard<std::mutex> lock(m);
q.push(std::move(t));
c.notify_one();
}
std::unique_ptr<T> receive(uint32_t ms)
{
std::unique_lock<std::mutex> lock(m);
while (q.empty()) {
if (c.wait_for(lock, std::chrono::milliseconds(ms)) == std::cv_status::timeout) {
return nullptr;
}
}
std::unique_ptr<T> val = std::move(q.front());
q.pop();
return val;
}
std::unique_ptr<T> val = std::move(q.front());
q.pop();
return val;
}
private:
std::queue<std::unique_ptr<T>> q;

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include "mdns.h"
#include "esp_log.h"
@ -6,7 +11,7 @@
static const char *TAG = "mdns-test";
static void query_mdns_host(const char * host_name)
static void query_mdns_host(const char *host_name)
{
ESP_LOGI(TAG, "Query A: %s.local", host_name);
@ -14,8 +19,8 @@ static void query_mdns_host(const char * host_name)
addr.addr = 0;
esp_err_t err = mdns_query_a(host_name, 2000, &addr);
if(err){
if(err == ESP_ERR_NOT_FOUND){
if (err) {
if (err == ESP_ERR_NOT_FOUND) {
ESP_LOGW(TAG, "%x: Host was not found!", (err));
return;
}
@ -26,7 +31,7 @@ static void query_mdns_host(const char * host_name)
ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr));
}
int main(int argc , char *argv[])
int main(int argc, char *argv[])
{
setvbuf(stdout, NULL, _IONBF, 0);
@ -42,9 +47,9 @@ int main(int argc , char *argv[])
mdns_instance_name_set("myesp-inst");
//structure with TXT records
mdns_txt_item_t serviceTxtData[3] = {
{"board","esp32"},
{"u","user"},
{"p","password"}
{"board", "esp32"},
{"u", "user"},
{"p", "password"}
};
vTaskDelay(1000);
ESP_ERROR_CHECK(mdns_service_add("myesp-service2", "_http", "_tcp", 80, serviceTxtData, 3));

View File

@ -39,7 +39,7 @@ The rest of the document will refer to that folder as ```PATH_TO_AFL```.
```
- On Ubuntu you need the following packages:
```bash
sudo apt-get install make clang-4.0(or <=4.0) llvm-4.0(or <=4.0) libbsd-dev
```

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include <string.h>
#include <pthread.h>
@ -5,22 +10,22 @@
#include <unistd.h>
#include "esp32_mock.h"
void* g_queue;
void *g_queue;
int g_queue_send_shall_fail = 0;
int g_size = 0;
const char * WIFI_EVENT = "wifi_event";
const char * ETH_EVENT = "eth_event";
const char *WIFI_EVENT = "wifi_event";
const char *ETH_EVENT = "eth_event";
esp_err_t esp_event_handler_register(const char * event_base,
int32_t event_id,
void* event_handler,
void* event_handler_arg)
esp_err_t esp_event_handler_register(const char *event_base,
int32_t event_id,
void *event_handler,
void *event_handler_arg)
{
return ESP_OK;
}
esp_err_t esp_event_handler_unregister(const char * event_base, int32_t event_id, void* event_handler)
esp_err_t esp_event_handler_unregister(const char *event_base, int32_t event_id, void *event_handler)
{
return ESP_OK;
}
@ -40,8 +45,8 @@ esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period)
return ESP_OK;
}
esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
esp_timer_handle_t* out_handle)
esp_err_t esp_timer_create(const esp_timer_create_args_t *create_args,
esp_timer_handle_t *out_handle)
{
return ESP_OK;
}
@ -53,12 +58,12 @@ uint32_t xTaskGetTickCount(void)
}
/// Queue mock
QueueHandle_t xQueueCreate( uint32_t uxQueueLength, uint32_t uxItemSize )
{
g_size = uxItemSize;
g_queue = malloc((uxQueueLength)*(uxItemSize));
return g_queue;
}
QueueHandle_t xQueueCreate( uint32_t uxQueueLength, uint32_t uxItemSize )
{
g_size = uxItemSize;
g_queue = malloc((uxQueueLength) * (uxItemSize));
return g_queue;
}
void vQueueDelete( QueueHandle_t xQueue )
@ -66,14 +71,11 @@ void vQueueDelete( QueueHandle_t xQueue )
free(xQueue);
}
uint32_t xQueueSend(QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait)
uint32_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait)
{
if (g_queue_send_shall_fail)
{
if (g_queue_send_shall_fail) {
return pdFALSE;
}
else
{
} else {
memcpy(xQueue, pvItemToQueue, g_size);
return pdPASS;
}
@ -105,7 +107,7 @@ void xTaskNotifyGive(TaskHandle_t task)
return;
}
BaseType_t xTaskNotifyWait(uint32_t bits_entry_clear, uint32_t bits_exit_clear, uint32_t * value, TickType_t wait_time)
BaseType_t xTaskNotifyWait(uint32_t bits_entry_clear, uint32_t bits_exit_clear, uint32_t *value, TickType_t wait_time)
{
return pdTRUE;
}

View File

@ -40,8 +40,8 @@
#define pdTRUE true
#define pdFALSE false
#define pdPASS ( pdTRUE )
#define pdFAIL ( pdFALSE )
#define pdPASS ( pdTRUE )
#define pdFAIL ( pdFALSE )
#define portMAX_DELAY 0xFFFFFFFF
#define portTICK_PERIOD_MS 1
@ -80,45 +80,45 @@
typedef int32_t esp_err_t;
typedef void * SemaphoreHandle_t;
typedef void * QueueHandle_t;
typedef void * TaskHandle_t;
typedef void *SemaphoreHandle_t;
typedef void *QueueHandle_t;
typedef void *TaskHandle_t;
typedef int BaseType_t;
typedef uint32_t TickType_t;
extern const char * WIFI_EVENT;
extern const char * IP_EVENT;
extern const char * ETH_EVENT;
extern const char *WIFI_EVENT;
extern const char *IP_EVENT;
extern const char *ETH_EVENT;
struct udp_pcb {
uint8_t dummy;
};
struct ip4_addr {
uint32_t addr;
uint32_t addr;
};
typedef struct ip4_addr ip4_addr_t;
struct ip6_addr {
uint32_t addr[4];
uint32_t addr[4];
};
typedef struct ip6_addr ip6_addr_t;
typedef void* system_event_t;
typedef void *system_event_t;
struct pbuf {
struct pbuf *next;
void *payload;
uint16_t tot_len;
uint16_t len;
uint8_t /*pbuf_type*/ type;
uint8_t flags;
uint16_t ref;
struct pbuf *next;
void *payload;
uint16_t tot_len;
uint16_t len;
uint8_t /*pbuf_type*/ type;
uint8_t flags;
uint16_t ref;
};
uint32_t xTaskGetTickCount(void);
typedef void (*esp_timer_cb_t)(void* arg);
typedef void (*esp_timer_cb_t)(void *arg);
// Queue mock
QueueHandle_t xQueueCreate( uint32_t uxQueueLength,
@ -126,7 +126,7 @@ QueueHandle_t xQueueCreate( uint32_t uxQueueLength,
void vQueueDelete( QueueHandle_t xQueue );
uint32_t xQueueSend(QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait);
uint32_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait);
uint32_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait);
@ -134,9 +134,9 @@ void GetLastItem(void *pvBuffer);
void ForceTaskDelete(void);
esp_err_t esp_event_handler_register(const char * event_base, int32_t event_id, void* event_handler, void* event_handler_arg);
esp_err_t esp_event_handler_register(const char *event_base, int32_t event_id, void *event_handler, void *event_handler_arg);
esp_err_t esp_event_handler_unregister(const char * event_base, int32_t event_id, void* event_handler);
esp_err_t esp_event_handler_unregister(const char *event_base, int32_t event_id, void *event_handler);
TaskHandle_t xTaskGetCurrentTaskHandle(void);

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#pragma once
#define IRAM_ATTR
#define FLAG_ATTR(TYPE)

View File

@ -1,16 +1,8 @@
// Copyright 2020 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
@ -24,7 +16,7 @@ typedef struct esp_netif_ip_info esp_netif_ip_info_t;
typedef struct esp_netif_dhcp_status esp_netif_dhcp_status_t;
const char * IP_EVENT = "IP_EVENT";
const char *IP_EVENT = "IP_EVENT";
esp_err_t esp_netif_add_to_list(esp_netif_t *netif)
@ -37,12 +29,12 @@ esp_err_t esp_netif_remove_from_list(esp_netif_t *netif)
return ESP_ERR_NOT_FOUND;
}
esp_netif_t* esp_netif_next(esp_netif_t* netif)
esp_netif_t *esp_netif_next(esp_netif_t *netif)
{
return NULL;
}
esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif)
esp_netif_t *esp_netif_next_unsafe(esp_netif_t *netif)
{
return NULL;
}

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
* MDNS Dependecy injection -- preincluded to inject interface test functions into static variables
*
@ -7,19 +12,19 @@
#include "mdns_private.h"
void (*mdns_test_static_execute_action)(mdns_action_t *) = NULL;
mdns_srv_item_t * (*mdns_test_static_mdns_get_service_item)(const char * service, const char * proto, const char *hostname) = NULL;
mdns_srv_item_t *(*mdns_test_static_mdns_get_service_item)(const char *service, const char *proto, const char *hostname) = NULL;
mdns_search_once_t *(*mdns_test_static_search_init)(const char *name, const char *service, const char *proto, uint16_t type, bool unicast,
uint32_t timeout, uint8_t max_results,
mdns_query_notify_t notifier) = NULL;
esp_err_t (*mdns_test_static_send_search_action)(mdns_action_type_t type, mdns_search_once_t * search) = NULL;
void (*mdns_test_static_search_free)(mdns_search_once_t * search) = NULL;
uint32_t timeout, uint8_t max_results,
mdns_query_notify_t notifier) = NULL;
esp_err_t (*mdns_test_static_send_search_action)(mdns_action_type_t type, mdns_search_once_t *search) = NULL;
void (*mdns_test_static_search_free)(mdns_search_once_t *search) = NULL;
static void _mdns_execute_action(mdns_action_t * action);
static mdns_srv_item_t * _mdns_get_service_item(const char * service, const char * proto, const char *hostname);
static void _mdns_execute_action(mdns_action_t *action);
static mdns_srv_item_t *_mdns_get_service_item(const char *service, const char *proto, const char *hostname);
static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type, bool unicast,
uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier);
static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t * search);
static void _mdns_search_free(mdns_search_once_t * search);
uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier);
static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t *search);
static void _mdns_search_free(mdns_search_once_t *search);
void mdns_test_init_di(void)
{
@ -30,27 +35,27 @@ void mdns_test_init_di(void)
mdns_test_static_search_free = _mdns_search_free;
}
void mdns_test_execute_action(void * action)
void mdns_test_execute_action(void *action)
{
mdns_test_static_execute_action((mdns_action_t *)action);
}
void mdns_test_search_free(mdns_search_once_t * search)
void mdns_test_search_free(mdns_search_once_t *search)
{
return mdns_test_static_search_free(search);
}
esp_err_t mdns_test_send_search_action(mdns_action_type_t type, mdns_search_once_t * search)
esp_err_t mdns_test_send_search_action(mdns_action_type_t type, mdns_search_once_t *search)
{
return mdns_test_static_send_search_action(type, search);
}
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)
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)
{
return mdns_test_static_search_init(name, service, proto, type, timeout, type != MDNS_TYPE_PTR, max_results, NULL);
}
mdns_srv_item_t * mdns_test_mdns_get_service_item(const char * service, const char * proto)
mdns_srv_item_t *mdns_test_mdns_get_service_item(const char *service, const char *proto)
{
return mdns_test_static_mdns_get_service_item(service, proto, NULL);
}

View File

@ -1,10 +1,15 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#pragma once
#include "esp32_mock.h"
#include "mdns.h"
#include "mdns_private.h"
static inline void* _mdns_get_packet_data(mdns_rx_packet_t *packet)
static inline void *_mdns_get_packet_data(mdns_rx_packet_t *packet)
{
return packet->pb->payload;
}

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
* This config file commited in order to not run `idf.py reconfigure` each time when running fuzzer test. You can modify it manually or run `idf.py reconfigure` to generate new one if needed.
* Espressif IoT Development Framework (ESP-IDF) Configuration Header

View File

@ -18,73 +18,73 @@
// Global stuctures containing packet payload, search
mdns_rx_packet_t g_packet;
struct pbuf mypbuf;
mdns_search_once_t * search = NULL;
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);
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);
void mdns_test_init_di(void);
extern mdns_server_t * _mdns_server;
extern mdns_server_t *_mdns_server;
//
// mdns function wrappers for mdns setup in test mode
static int mdns_test_hostname_set(const char * mdns_hostname)
static int mdns_test_hostname_set(const char *mdns_hostname)
{
for (int i=0; i<MDNS_MAX_INTERFACES; i++) {
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;
}
int ret = mdns_hostname_set(mdns_hostname);
mdns_action_t * a = NULL;
mdns_action_t *a = NULL;
GetLastItem(&a);
mdns_test_execute_action(a);
return ret;
}
static int mdns_test_add_delegated_host(const char * mdns_hostname)
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;
mdns_action_t *a = NULL;
GetLastItem(&a);
mdns_test_execute_action(a);
return ret;
}
static int mdns_test_service_instance_name_set(const char * service, const char * proto, const char * instance)
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;
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[])
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;
mdns_action_t *a = NULL;
GetLastItem(&a);
mdns_test_execute_action(a);
return ret;
}
static int mdns_test_sub_service_add(const char * sub_name, const char * service_name, const char * proto, uint32_t port)
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;
mdns_action_t *a = NULL;
GetLastItem(&a);
mdns_test_execute_action(a);
if (mdns_test_mdns_get_service_item(service_name, proto)==NULL) {
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);
@ -94,22 +94,22 @@ static int mdns_test_sub_service_add(const char * sub_name, const char * service
return ret;
}
static int mdns_test_service_add(const char * service_name, const char * proto, uint32_t port)
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;
mdns_action_t *a = NULL;
GetLastItem(&a);
mdns_test_execute_action(a);
if (mdns_test_mdns_get_service_item(service_name, proto)==NULL) {
if (mdns_test_mdns_get_service_item(service_name, proto) == NULL) {
return ESP_FAIL;
}
return ESP_OK;
}
static mdns_result_t* mdns_test_query(const char * name, const char * service, const char * proto, uint16_t type)
static mdns_result_t *mdns_test_query(const char *name, const char *service, const char *proto, uint16_t type)
{
search = mdns_test_search_init(name, service, proto, type, 3000, 20);
if (!search) {
@ -121,7 +121,7 @@ static mdns_result_t* mdns_test_query(const char * name, const char * service, c
abort();
}
mdns_action_t * a = NULL;
mdns_action_t *a = NULL;
GetLastItem(&a);
mdns_test_execute_action(a);
return NULL;
@ -135,27 +135,27 @@ static void mdns_test_query_free(void)
//
// function "under test" where afl-mangled packets passed
//
void mdns_parse_packet(mdns_rx_packet_t * packet);
void mdns_parse_packet(mdns_rx_packet_t *packet);
//
// Test starts here
//
int main(int argc, char** argv)
int main(int argc, char **argv)
{
int i;
const char * mdns_hostname = "minifritz";
const char * mdns_instance = "Hristo's Time Capsule";
const char *mdns_hostname = "minifritz";
const char *mdns_instance = "Hristo's Time Capsule";
mdns_txt_item_t arduTxtData[4] = {
{"board","esp32"},
{"tcp_check","no"},
{"ssh_upload","no"},
{"auth_upload","no"}
{"board", "esp32"},
{"tcp_check", "no"},
{"ssh_upload", "no"},
{"auth_upload", "no"}
};
const uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x32};
uint8_t buf[1460];
char winstance[21+strlen(mdns_hostname)];
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]);
@ -208,7 +208,7 @@ int main(int argc, char** argv)
}
if (
mdns_test_service_add("_afpovertcp", "_tcp", 548)
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)
@ -224,12 +224,11 @@ int main(int argc, char** argv)
|| 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))
{
|| mdns_test_service_add("_sleep-proxy", "_udp", 885)) {
abort();
}
#endif
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
FILE *file;
size_t nread;
@ -237,13 +236,10 @@ int main(int argc, char** argv)
size_t len = 1460;
memset(buf, 0, 1460);
if (argc != 2)
{
if (argc != 2) {
printf("Non-instrumentation mode: please supply a file name created by AFL to reproduce crash\n");
return 1;
}
else
{
} else {
//
// Note: parameter1 is a file (mangled packet) which caused the crash
file = fopen(argv[1], "r");
@ -252,7 +248,7 @@ int main(int argc, char** argv)
fclose(file);
}
for (i=0; i<1; i++) {
for (i = 0; i < 1; i++) {
#else
while (__AFL_LOOP(1000)) {
memset(buf, 0, 1460);

View File

@ -65,7 +65,8 @@ def get_mdns_sub_service_query(sub_service): # type:(str) -> dpkt.dns.Msg
arr.target = socket.inet_aton('127.0.0.1')
arr.ptrname = sub_service
dns.qd.append(arr)
console_log('Created mdns subtype service query: {} '.format(dns.__repr__()))
console_log('Created mdns subtype service query: {} '.format(
dns.__repr__()))
return dns.pack()
@ -90,13 +91,14 @@ def get_dns_answer_to_mdns(tester_host): # type:(str) -> dpkt.dns.Msg
arr.type = dpkt.dns.DNS_A
arr.name = tester_host
arr.ip = socket.inet_aton('127.0.0.1')
dns. an.append(arr)
dns.an.append(arr)
console_log('Created answer to mdns query: {} '.format(dns.__repr__()))
return dns.pack()
# Get mdns answer for service query
def get_dns_answer_to_service_query(mdns_service): # type:(str) -> dpkt.dns.Msg
def get_dns_answer_to_service_query(
mdns_service): # type:(str) -> dpkt.dns.Msg
dns = dpkt.dns.DNS()
dns.op = dpkt.dns.DNS_QR | dpkt.dns.DNS_AA
dns.rcode = dpkt.dns.DNS_RCODE_NOERR
@ -109,7 +111,7 @@ def get_dns_answer_to_service_query(mdns_service): # type:(str) -> dpkt.dns.Msg
arr.port = 100
arr.srvname = mdns_service
arr.ip = socket.inet_aton('127.0.0.1')
dns. an.append(arr)
dns.an.append(arr)
console_log('Created answer to mdns query: {} '.format(dns.__repr__()))
return dns.pack()
@ -122,7 +124,7 @@ def mdns_listener(esp_host): # type:(str) -> None
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.setblocking(False)
sock.bind((UDP_IP,UDP_PORT))
sock.bind((UDP_IP, UDP_PORT))
mreq = struct.pack('4sl', socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
last_query_timepoint = time.time()
@ -133,7 +135,8 @@ def mdns_listener(esp_host): # type:(str) -> None
current_time = time.time()
if current_time - last_query_timepoint > QUERY_TIMEOUT:
last_query_timepoint = current_time
timeout = max(0, QUERY_TIMEOUT - (current_time - last_query_timepoint))
timeout = max(
0, QUERY_TIMEOUT - (current_time - last_query_timepoint))
read_socks, _, _ = select.select([sock], [], [], timeout)
if not read_socks:
continue
@ -143,29 +146,40 @@ def mdns_listener(esp_host): # type:(str) -> None
if len(dns.qd) > 0:
if dns.qd[0].name == HOST_NAME:
console_log('Received query: {} '.format(dns.__repr__()))
sock.sendto(get_dns_answer_to_mdns(HOST_NAME), (MCAST_GRP,UDP_PORT))
sock.sendto(get_dns_answer_to_mdns(HOST_NAME),
(MCAST_GRP, UDP_PORT))
if dns.qd[0].name == HOST_NAME:
console_log('Received query: {} '.format(dns.__repr__()))
sock.sendto(get_dns_answer_to_mdns(HOST_NAME), (MCAST_GRP,UDP_PORT))
sock.sendto(get_dns_answer_to_mdns(HOST_NAME),
(MCAST_GRP, UDP_PORT))
if dns.qd[0].name == MDNS_HOST_SERVICE:
print(dns.qd[0].name)
console_log('Received query: {} '.format(dns.__repr__()))
sock.sendto(get_dns_answer_to_service_query(MDNS_HOST_SERVICE), (MCAST_GRP,UDP_PORT))
sock.sendto(
get_dns_answer_to_service_query(MDNS_HOST_SERVICE),
(MCAST_GRP, UDP_PORT))
# Receives answers from esp board and sets event flags for python test cases
if len(dns.an) == 1:
if dns.an[0].name.startswith(SERVICE_NAME):
console_log('Received answer to service query: {}'.format(dns.__repr__()))
console_log('Received answer to service query: {}'.format(
dns.__repr__()))
esp_service_answered.set()
if len(dns.an) > 1:
if dns.an[1].name.startswith(SUB_SERVICE_NAME):
console_log('Received answer for sub service query: {}'.format(dns.__repr__()))
console_log(
'Received answer for sub service query: {}'.format(
dns.__repr__()))
esp_sub_service_answered.set()
if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A:
if dns.an[0].name == esp_host + u'.local':
console_log('Received answer to esp32-mdns query: {}'.format(dns.__repr__()))
console_log(
'Received answer to esp32-mdns query: {}'.format(
dns.__repr__()))
esp_host_answered.set()
if dns.an[0].name == esp_host + u'-delegated.local':
console_log('Received answer to esp32-mdns-delegate query: {}'.format(dns.__repr__()))
console_log(
'Received answer to esp32-mdns-delegate query: {}'.
format(dns.__repr__()))
esp_delegated_host_answered.set()
except socket.timeout:
break
@ -179,7 +193,7 @@ def create_socket(): # type:() -> socket.socket
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.setblocking(False)
sock.bind((UDP_IP,UDP_PORT))
sock.bind((UDP_IP, UDP_PORT))
mreq = struct.pack('4sl', socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
return sock
@ -192,9 +206,10 @@ def test_query_dns_http_service(service): # type: (str) -> None
for _ in range(RETRY_COUNT):
if esp_service_answered.wait(timeout=25):
break
sock.sendto(packet, (MCAST_GRP,UDP_PORT))
sock.sendto(packet, (MCAST_GRP, UDP_PORT))
else:
raise RuntimeError('Test has failed: did not receive mdns answer within timeout')
raise RuntimeError(
'Test has failed: did not receive mdns answer within timeout')
def test_query_dns_sub_service(sub_service): # type: (str) -> None
@ -204,9 +219,10 @@ def test_query_dns_sub_service(sub_service): # type: (str) -> None
for _ in range(RETRY_COUNT):
if esp_sub_service_answered.wait(timeout=25):
break
sock.sendto(packet, (MCAST_GRP,UDP_PORT))
sock.sendto(packet, (MCAST_GRP, UDP_PORT))
else:
raise RuntimeError('Test has failed: did not receive mdns answer within timeout')
raise RuntimeError(
'Test has failed: did not receive mdns answer within timeout')
def test_query_dns_host(esp_host): # type: (str) -> None
@ -216,9 +232,10 @@ def test_query_dns_host(esp_host): # type: (str) -> None
for _ in range(RETRY_COUNT):
if esp_host_answered.wait(timeout=25):
break
sock.sendto(packet, (MCAST_GRP,UDP_PORT))
sock.sendto(packet, (MCAST_GRP, UDP_PORT))
else:
raise RuntimeError('Test has failed: did not receive mdns answer within timeout')
raise RuntimeError(
'Test has failed: did not receive mdns answer within timeout')
def test_query_dns_host_delegated(esp_host): # type: (str) -> None
@ -228,24 +245,30 @@ def test_query_dns_host_delegated(esp_host): # type: (str) -> None
for _ in range(RETRY_COUNT):
if esp_delegated_host_answered.wait(timeout=25):
break
sock.sendto(packet, (MCAST_GRP,UDP_PORT))
sock.sendto(packet, (MCAST_GRP, UDP_PORT))
else:
raise RuntimeError('Test has failed: did not receive mdns answer within timeout')
raise RuntimeError(
'Test has failed: did not receive mdns answer within timeout')
@ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps')
def test_app_esp_mdns(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
dut1 = env.get_dut('mdns', 'tools/test_apps/protocols/mdns', dut_class=ttfw_idf.ESP32DUT)
dut1 = env.get_dut('mdns',
'tools/test_apps/protocols/mdns',
dut_class=ttfw_idf.ESP32DUT)
# 1. start mdns application
dut1.start_app()
# 2. get the dut host name (and IP address)
specific_host = dut1.expect(re.compile(r'mdns hostname set to: \[([^\]]+)\]'), timeout=30)[0]
specific_host = dut1.expect(
re.compile(r'mdns hostname set to: \[([^\]]+)\]'), timeout=30)[0]
esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30)
esp_ip = dut1.expect(
re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'),
timeout=30)
print('Got IP={}'.format(esp_ip[0]))
mdns_responder = Thread(target=mdns_listener, args=(str(specific_host),))
mdns_responder = Thread(target=mdns_listener, args=(str(specific_host), ))
def start_case(case, desc, result): # type: (str, str, str) -> None
print('Starting {}: {}'.format(case, desc))
@ -258,7 +281,8 @@ def test_app_esp_mdns(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
# wait untill mdns listener thred started
if not start_mdns_listener.wait(timeout=5):
raise ValueError('Test has failed: mdns listener thread did not start')
raise ValueError(
'Test has failed: mdns listener thread did not start')
# query dns service from host, answer should be received from esp board
test_query_dns_http_service(SERVICE_NAME)
@ -273,13 +297,17 @@ def test_app_esp_mdns(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
test_query_dns_host_delegated(specific_host)
# query service from esp board, answer should be received from host
start_case('CONFIG_TEST_QUERY_SERVICE', 'Query SRV ESP32._http._tcp.local', 'SRV:ESP32')
start_case('CONFIG_TEST_QUERY_SERVICE',
'Query SRV ESP32._http._tcp.local', 'SRV:ESP32')
# query dns-host from esp board, answer should be received from host
start_case('CONFIG_TEST_QUERY_HOST', 'Query tinytester.local', 'tinytester.local resolved to: 127.0.0.1')
start_case('CONFIG_TEST_QUERY_HOST', 'Query tinytester.local',
'tinytester.local resolved to: 127.0.0.1')
# query dns-host aynchrounusely from esp board, answer should be received from host
start_case('CONFIG_TEST_QUERY_HOST_ASYNC', 'Query tinytester.local async', 'Async query resolved to A:127.0.0.1')
start_case('CONFIG_TEST_QUERY_HOST_ASYNC',
'Query tinytester.local async',
'Async query resolved to A:127.0.0.1')
finally:
stop_mdns_listener.set()

View File

@ -35,7 +35,7 @@ static void get_string(char *line, size_t size)
/** Generate host name based on sdkconfig, optionally adding a portion of MAC address to it.
* @return host name string allocated from the heap
*/
static char* generate_hostname(void)
static char *generate_hostname(void)
{
#ifndef CONFIG_TEST_MDNS_ADD_MAC_TO_HOSTNAME
return strdup(CONFIG_TEST_MDNS_HOSTNAME);
@ -52,7 +52,7 @@ static char* generate_hostname(void)
static void initialise_mdns(void)
{
char * hostname = generate_hostname();
char *hostname = generate_hostname();
//initialize mDNS
ESP_ERROR_CHECK( mdns_init() );

View File

@ -10,7 +10,7 @@
#include "esp_log.h"
#include "esp_netif.h"
static const char * TAG = "MDNS_TEST_APP";
static const char *TAG = "MDNS_TEST_APP";
static const int RETRY_COUNT = 10;
static void mdns_print_results(mdns_result_t *results)
@ -48,7 +48,7 @@ static void mdns_print_results(mdns_result_t *results)
static bool check_and_print_result(mdns_search_once_t *search)
{
// Check if any result is available
mdns_result_t * result = NULL;
mdns_result_t *result = NULL;
if (!mdns_query_async_get_results(search, 0, &result, NULL)) {
return false;
}
@ -58,9 +58,9 @@ static bool check_and_print_result(mdns_search_once_t *search)
}
// If yes, print the result
mdns_ip_addr_t * a = result->addr;
mdns_ip_addr_t *a = result->addr;
while (a) {
if(a->addr.type == ESP_IPADDR_TYPE_V6){
if (a->addr.type == ESP_IPADDR_TYPE_V6) {
printf("Async query resolved to AAAA:" IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
} else {
printf("Async query resolved to A:" IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
@ -72,7 +72,7 @@ static bool check_and_print_result(mdns_search_once_t *search)
return true;
}
static bool query_mdns_hosts_async(const char * host_name)
static bool query_mdns_hosts_async(const char *host_name)
{
ESP_LOGI(TAG, "Query both A and AAA: %s.local", host_name);
bool res = false;
@ -97,7 +97,7 @@ static bool query_mdns_hosts_async(const char * host_name)
return res;
}
static esp_err_t query_mdns_host(const char * host_name)
static esp_err_t query_mdns_host(const char *host_name)
{
ESP_LOGI(TAG, "Query A: %s.local", host_name);
@ -105,8 +105,8 @@ static esp_err_t query_mdns_host(const char * host_name)
addr.addr = 0;
esp_err_t err = mdns_query_a(host_name, 2000, &addr);
if(err){
if(err == ESP_ERR_NOT_FOUND){
if (err) {
if (err == ESP_ERR_NOT_FOUND) {
ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err));
}
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
@ -117,17 +117,17 @@ static esp_err_t query_mdns_host(const char * host_name)
return ESP_OK;
}
static esp_err_t query_mdns_service(const char * instance, const char * service_name, const char * proto)
static esp_err_t query_mdns_service(const char *instance, const char *service_name, const char *proto)
{
ESP_LOGI(TAG, "Query SRV: %s.%s.local", service_name, proto);
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_err_t err = mdns_query_srv(instance, service_name, proto, 3000, &results);
if(err){
if (err) {
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
return err;
}
if(!results){
if (!results) {
ESP_LOGW(TAG, "No results found!");
}
@ -136,15 +136,16 @@ static esp_err_t query_mdns_service(const char * instance, const char * service_
return ESP_OK;
}
void query_mdns_service_sub_type(const char * subtype, const char * service_name, const char * proto) {
void query_mdns_service_sub_type(const char *subtype, const char *service_name, const char *proto)
{
ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto);
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
if(err){
if (err) {
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
}
if(!results){
if (!results) {
ESP_LOGW(TAG, "No results found!");
}

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "test_utils.h"
#include "mdns.h"
#include "esp_event.h"
@ -48,7 +53,7 @@ TEST_CASE("mdns api return expected err-code and do not leak memory", "[mdns][le
addr.addr.type = ESP_IPADDR_TYPE_V4;
addr.addr.u_addr.ip4.addr = esp_ip4addr_aton("127.0.0.1");
addr.next = NULL;
for (int i=0; i<CONFIG_MDNS_MAX_SERVICES; ++i) {
for (int i = 0; i < CONFIG_MDNS_MAX_SERVICES; ++i) {
serviceTxtData[i].key = "Key";
serviceTxtData[i].value = "Value";
}
@ -64,7 +69,7 @@ TEST_CASE("mdns api return expected err-code and do not leak memory", "[mdns][le
TEST_ASSERT_EQUAL(ESP_OK, mdns_service_add(MDNS_INSTANCE, MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, MDNS_SERVICE_PORT, serviceTxtData, CONFIG_MDNS_MAX_SERVICES) );
TEST_ASSERT_FALSE(mdns_service_exists(MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, MDNS_DELEGATE_HOSTNAME) );
TEST_ASSERT_EQUAL(ESP_OK, mdns_service_add_for_host(MDNS_INSTANCE, MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, MDNS_DELEGATE_HOSTNAME,
MDNS_SERVICE_PORT, serviceTxtData, CONFIG_MDNS_MAX_SERVICES) );
MDNS_SERVICE_PORT, serviceTxtData, CONFIG_MDNS_MAX_SERVICES) );
TEST_ASSERT_TRUE(mdns_service_exists(MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, MDNS_DELEGATE_HOSTNAME) );
TEST_ASSERT_EQUAL(ESP_OK, mdns_service_txt_set(MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, serviceTxtData, CONFIG_MDNS_MAX_SERVICES) );
TEST_ASSERT_EQUAL(ESP_OK, mdns_service_txt_item_set(MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, "key1", "value1") );
@ -88,7 +93,7 @@ TEST_CASE("mdns api return expected err-code and do not leak memory", "[mdns][le
TEST_CASE("mdns query api return expected err-code and do not leak memory", "[leaks=64]")
{
mdns_result_t * results = NULL;
mdns_result_t *results = NULL;
esp_ip6_addr_t addr6;
esp_ip4_addr_t addr4;
test_case_uses_tcpip();