mDNS: split probe packet definition and split outpkt creation

Probe packet creation -> by splitting the definition and the sending
we can use the definition function for probe tiebreaking.
outpkt creation -> by splitting the creation and sending we can use
the creation function for probe tiebreaking.
This commit is contained in:
Jasper Verschueren 2018-11-08 16:55:54 +01:00 committed by Dirk Ziegelmeier
parent 2a236088ae
commit a09646c507
3 changed files with 82 additions and 58 deletions

View File

@ -1303,39 +1303,35 @@ mdns_netif_ext_status_callback(struct netif *netif, netif_nsc_reason_t reason, c
} }
#endif /* LWIP_NETIF_EXT_STATUS_CALLBACK && MDNS_RESP_USENETIF_EXTCALLBACK */ #endif /* LWIP_NETIF_EXT_STATUS_CALLBACK && MDNS_RESP_USENETIF_EXTCALLBACK */
static err_t static void
mdns_send_probe(struct netif* netif, const ip_addr_t *destination) mdns_define_probe_rrs_to_send(struct netif *netif, struct mdns_outmsg *outmsg)
{ {
struct mdns_host* mdns; struct mdns_host *mdns = NETIF_TO_HOST(netif);
struct mdns_outmsg outmsg; int i;
u8_t i;
err_t res;
mdns = NETIF_TO_HOST(netif); memset(outmsg, 0, sizeof(struct mdns_outmsg));
memset(&outmsg, 0, sizeof(outmsg));
/* Add unicast questions with rtype ANY for all our desired records */ /* Add unicast questions with rtype ANY for all our desired records */
outmsg.host_questions = QUESTION_PROBE_HOST_ANY; outmsg->host_questions = QUESTION_PROBE_HOST_ANY;
for (i = 0; i < MDNS_MAX_SERVICES; i++) { for (i = 0; i < MDNS_MAX_SERVICES; i++) {
struct mdns_service* service = mdns->services[i]; struct mdns_service* service = mdns->services[i];
if (!service) { if (!service) {
continue; continue;
} }
outmsg.serv_questions[i] = QUESTION_PROBE_SERVICE_NAME_ANY; outmsg->serv_questions[i] = QUESTION_PROBE_SERVICE_NAME_ANY;
} }
/* Add answers to the questions above into the authority section for tiebreaking */ /* Add answers to the questions above into the authority section for tiebreaking */
#if LWIP_IPV4 #if LWIP_IPV4
if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) { if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
outmsg.host_replies = REPLY_HOST_A; outmsg->host_replies = REPLY_HOST_A;
} }
#endif #endif
#if LWIP_IPV6 #if LWIP_IPV6
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) { if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
outmsg.host_replies |= REPLY_HOST_AAAA; outmsg->host_replies |= REPLY_HOST_AAAA;
} }
} }
#endif #endif
@ -1343,16 +1339,22 @@ mdns_send_probe(struct netif* netif, const ip_addr_t *destination)
for (i = 0; i < MDNS_MAX_SERVICES; i++) { for (i = 0; i < MDNS_MAX_SERVICES; i++) {
struct mdns_service *serv = mdns->services[i]; struct mdns_service *serv = mdns->services[i];
if (serv) { if (serv) {
outmsg.serv_replies[i] = REPLY_SERVICE_SRV | REPLY_SERVICE_TXT; outmsg->serv_replies[i] = REPLY_SERVICE_SRV | REPLY_SERVICE_TXT;
} }
} }
}
static err_t
mdns_send_probe(struct netif* netif, const ip_addr_t *destination)
{
struct mdns_outmsg outmsg;
mdns_define_probe_rrs_to_send(netif, &outmsg);
outmsg.tx_id = 0; outmsg.tx_id = 0;
outmsg.dest_port = LWIP_IANA_PORT_MDNS; outmsg.dest_port = LWIP_IANA_PORT_MDNS;
SMEMCPY(&outmsg.dest_addr, destination, sizeof(outmsg.dest_addr)); SMEMCPY(&outmsg.dest_addr, destination, sizeof(outmsg.dest_addr));
res = mdns_send_outpacket(&outmsg, netif); return mdns_send_outpacket(&outmsg, netif);
return res;
} }
/** /**

View File

@ -550,42 +550,40 @@ mdns_add_probe_questions_to_outpacket(struct mdns_outpacket *outpkt, struct mdns
} }
/** /**
* Send chosen answers as a reply * Create packet with chosen answers as a reply
* *
* Add all selected answers (first write will allocate pbuf) * Add all selected answers / questions
* Add additional answers based on the selected answers * Add additional answers based on the selected answers
* Send the packet
*/ */
err_t err_t
mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif) mdns_create_outpacket(struct netif *netif, struct mdns_outmsg *msg,
struct mdns_outpacket *outpkt)
{ {
struct mdns_service *service;
struct mdns_outpacket outpkt;
err_t res = ERR_ARG;
int i;
struct mdns_host *mdns = netif_mdns_data(netif); struct mdns_host *mdns = netif_mdns_data(netif);
struct mdns_service *service;
err_t res;
int i;
u16_t answers = 0; u16_t answers = 0;
memset(&outpkt, 0, sizeof(outpkt));
res = mdns_add_probe_questions_to_outpacket(&outpkt, msg, netif); res = mdns_add_probe_questions_to_outpacket(outpkt, msg, netif);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
/* Write answers to host questions */ /* Write answers to host questions */
#if LWIP_IPV4 #if LWIP_IPV4
if (msg->host_replies & REPLY_HOST_A) { if (msg->host_replies & REPLY_HOST_A) {
res = mdns_add_a_answer(&outpkt, msg, netif); res = mdns_add_a_answer(outpkt, msg, netif);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
answers++; answers++;
} }
if (msg->host_replies & REPLY_HOST_PTR_V4) { if (msg->host_replies & REPLY_HOST_PTR_V4) {
res = mdns_add_hostv4_ptr_answer(&outpkt, msg, netif); res = mdns_add_hostv4_ptr_answer(outpkt, msg, netif);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
answers++; answers++;
} }
@ -595,9 +593,9 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
int addrindex; int addrindex;
for (addrindex = 0; addrindex < LWIP_IPV6_NUM_ADDRESSES; addrindex++) { for (addrindex = 0; addrindex < LWIP_IPV6_NUM_ADDRESSES; addrindex++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addrindex))) { if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addrindex))) {
res = mdns_add_aaaa_answer(&outpkt, msg, netif, addrindex); res = mdns_add_aaaa_answer(outpkt, msg, netif, addrindex);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
answers++; answers++;
} }
@ -608,9 +606,9 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
int addrindex = 0; int addrindex = 0;
while (rev_addrs) { while (rev_addrs) {
if (rev_addrs & 1) { if (rev_addrs & 1) {
res = mdns_add_hostv6_ptr_answer(&outpkt, msg, netif, addrindex); res = mdns_add_hostv6_ptr_answer(outpkt, msg, netif, addrindex);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
answers++; answers++;
} }
@ -628,33 +626,33 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
} }
if (msg->serv_replies[i] & REPLY_SERVICE_TYPE_PTR) { if (msg->serv_replies[i] & REPLY_SERVICE_TYPE_PTR) {
res = mdns_add_servicetype_ptr_answer(&outpkt, msg, service); res = mdns_add_servicetype_ptr_answer(outpkt, msg, service);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
answers++; answers++;
} }
if (msg->serv_replies[i] & REPLY_SERVICE_NAME_PTR) { if (msg->serv_replies[i] & REPLY_SERVICE_NAME_PTR) {
res = mdns_add_servicename_ptr_answer(&outpkt, msg, service); res = mdns_add_servicename_ptr_answer(outpkt, msg, service);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
answers++; answers++;
} }
if (msg->serv_replies[i] & REPLY_SERVICE_SRV) { if (msg->serv_replies[i] & REPLY_SERVICE_SRV) {
res = mdns_add_srv_answer(&outpkt, msg, mdns, service); res = mdns_add_srv_answer(outpkt, msg, mdns, service);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
answers++; answers++;
} }
if (msg->serv_replies[i] & REPLY_SERVICE_TXT) { if (msg->serv_replies[i] & REPLY_SERVICE_TXT) {
res = mdns_add_txt_answer(&outpkt, msg, service); res = mdns_add_txt_answer(outpkt, msg, service);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
answers++; answers++;
} }
@ -663,9 +661,9 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
/* if this is a response, the data above is anwers, else this is a probe and /* if this is a response, the data above is anwers, else this is a probe and
* the answers above goes into auth section */ * the answers above goes into auth section */
if (msg->flags & DNS_FLAG1_RESPONSE) { if (msg->flags & DNS_FLAG1_RESPONSE) {
outpkt.answers += answers; outpkt->answers += answers;
} else { } else {
outpkt.authoritative += answers; outpkt->authoritative += answers;
} }
/* All answers written, add additional RRs */ /* All answers written, add additional RRs */
@ -679,19 +677,19 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
/* Our service instance requested, include SRV & TXT /* Our service instance requested, include SRV & TXT
* if they are already not requested. */ * if they are already not requested. */
if (!(msg->serv_replies[i] & REPLY_SERVICE_SRV)) { if (!(msg->serv_replies[i] & REPLY_SERVICE_SRV)) {
res = mdns_add_srv_answer(&outpkt, msg, mdns, service); res = mdns_add_srv_answer(outpkt, msg, mdns, service);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
outpkt.additional++; outpkt->additional++;
} }
if (!(msg->serv_replies[i] & REPLY_SERVICE_TXT)) { if (!(msg->serv_replies[i] & REPLY_SERVICE_TXT)) {
res = mdns_add_txt_answer(&outpkt, msg, service); res = mdns_add_txt_answer(outpkt, msg, service);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
outpkt.additional++; outpkt->additional++;
} }
} }
@ -705,11 +703,11 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
int addrindex; int addrindex;
for (addrindex = 0; addrindex < LWIP_IPV6_NUM_ADDRESSES; addrindex++) { for (addrindex = 0; addrindex < LWIP_IPV6_NUM_ADDRESSES; addrindex++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addrindex))) { if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addrindex))) {
res = mdns_add_aaaa_answer(&outpkt, msg, netif, addrindex); res = mdns_add_aaaa_answer(outpkt, msg, netif, addrindex);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
outpkt.additional++; outpkt->additional++;
} }
} }
} }
@ -717,16 +715,38 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
#if LWIP_IPV4 #if LWIP_IPV4
if (!(msg->host_replies & REPLY_HOST_A) && if (!(msg->host_replies & REPLY_HOST_A) &&
!ip4_addr_isany_val(*netif_ip4_addr(netif))) { !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
res = mdns_add_a_answer(&outpkt, msg, netif); res = mdns_add_a_answer(outpkt, msg, netif);
if (res != ERR_OK) { if (res != ERR_OK) {
goto cleanup; return res;
} }
outpkt.additional++; outpkt->additional++;
} }
#endif #endif
} }
} }
return res;
}
/**
* Send chosen answers as a reply
*
* Create the packet
* Send the packet
*/
err_t
mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
{
struct mdns_outpacket outpkt;
err_t res;
memset(&outpkt, 0, sizeof(outpkt));
res = mdns_create_outpacket(netif, msg, &outpkt);
if (res != ERR_OK) {
goto cleanup;
}
if (outpkt.pbuf) { if (outpkt.pbuf) {
struct dns_hdr hdr; struct dns_hdr hdr;

View File

@ -103,6 +103,8 @@ extern "C" {
*/ */
#define MDNS_MULTICAST_TIMEOUT_25TTL 30000 #define MDNS_MULTICAST_TIMEOUT_25TTL 30000
err_t mdns_create_outpacket(struct netif *netif, struct mdns_outmsg *msg,
struct mdns_outpacket *outpkt);
err_t mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif); err_t mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif);
void mdns_set_timeout(struct netif *netif, u32_t msecs, void mdns_set_timeout(struct netif *netif, u32_t msecs,
sys_timeout_handler handler, u8_t *busy_flag); sys_timeout_handler handler, u8_t *busy_flag);