Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple simultaneous mDNS requests #21952

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions include/net/dns_resolve.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ struct dns_resolve_context {

/** DNS id of this query */
u16_t id;

/** Hash of the DNS name + query type we are querying.
* This hash is calculated so we can match the response that
* we are receiving. This is needed mainly for mDNS which is
* setting the DNS id to 0, which means that the id alone
* cannot be used to find correct pending query.
*/
u16_t query_hash;
} queries[CONFIG_DNS_NUM_CONCUR_QUERIES];

/** Is this context in use */
Expand Down Expand Up @@ -270,6 +278,23 @@ int dns_resolve_close(struct dns_resolve_context *ctx);
int dns_resolve_cancel(struct dns_resolve_context *ctx,
u16_t dns_id);

/**
* @brief Cancel a pending DNS query using id, name and type.
*
* @details This releases DNS resources used by a pending query.
*
* @param ctx DNS context
* @param dns_id DNS id of the pending query
* @param query_name Name of the resource we are trying to query (hostname)
* @param query_type Type of the query (A or AAAA)
*
* @return 0 if ok, <0 if error.
*/
int dns_resolve_cancel_with_name(struct dns_resolve_context *ctx,
u16_t dns_id,
const char *query_name,
enum dns_query_type query_type);

/**
* @brief Resolve DNS name.
*
Expand Down
40 changes: 18 additions & 22 deletions samples/net/dns_resolve/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ void mdns_result_cb(enum dns_resolve_status status,

switch (status) {
case DNS_EAI_CANCELED:
LOG_INF("DNS query was canceled");
LOG_INF("mDNS query was canceled");
return;
case DNS_EAI_FAIL:
LOG_INF("DNS resolve failed");
LOG_INF("mDNS resolve failed");
return;
case DNS_EAI_NODATA:
LOG_INF("Cannot resolve address");
LOG_INF("Cannot resolve address using mDNS");
return;
case DNS_EAI_ALLDONE:
LOG_INF("DNS resolving finished");
LOG_INF("mDNS resolving finished");
return;
case DNS_EAI_INPROGRESS:
break;
default:
LOG_INF("DNS resolving error (%d)", status);
LOG_INF("mDNS resolving error (%d)", status);
return;
}

Expand Down Expand Up @@ -135,7 +135,7 @@ static struct k_delayed_work ipv4_timer;
static void do_ipv4_lookup(struct k_work *work)
{
static const char *query = "www.zephyrproject.org";
u16_t dns_id;
static u16_t dns_id;
int ret;

ret = dns_get_addr_info(query,
Expand Down Expand Up @@ -247,25 +247,12 @@ static void do_mdns_ipv4_lookup(struct k_work *work)
#error "You need to define an IPv4 address or enable DHCPv4!"
#endif

static void setup_ipv4(struct net_if *iface)
static void do_ipv4_lookup(void)
{
static const char *query = "www.zephyrproject.org";
char hr_addr[NET_IPV4_ADDR_LEN];
struct in_addr addr;
u16_t dns_id;
static u16_t dns_id;
int ret;

if (net_addr_pton(AF_INET, CONFIG_NET_CONFIG_MY_IPV4_ADDR, &addr)) {
LOG_ERR("Invalid address: %s", CONFIG_NET_CONFIG_MY_IPV4_ADDR);
return;
}

net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0);

LOG_INF("IPv4 address: %s",
log_strdup(net_addr_ntop(AF_INET, &addr, hr_addr,
NET_IPV4_ADDR_LEN)));

ret = dns_get_addr_info(query,
DNS_QUERY_TYPE_A,
&dns_id,
Expand All @@ -278,6 +265,13 @@ static void setup_ipv4(struct net_if *iface)
}

LOG_DBG("DNS id %u", dns_id);
}

static void setup_ipv4(struct net_if *iface)
{
ARG_UNUSED(iface);

do_ipv4_lookup();

#if defined(CONFIG_MDNS_RESOLVER) && defined(CONFIG_NET_IPV4)
k_delayed_work_init(&mdns_ipv4_timer, do_mdns_ipv4_lookup);
Expand All @@ -298,7 +292,7 @@ static void setup_ipv4(struct net_if *iface)
static void do_ipv6_lookup(void)
{
static const char *query = "www.zephyrproject.org";
u16_t dns_id;
static u16_t dns_id;
int ret;

ret = dns_get_addr_info(query,
Expand All @@ -317,6 +311,8 @@ static void do_ipv6_lookup(void)

static void setup_ipv6(struct net_if *iface)
{
ARG_UNUSED(iface);

do_ipv6_lookup();

#if defined(CONFIG_MDNS_RESOLVER) && defined(CONFIG_NET_IPV6)
Expand Down
Loading