Rtos API example

Embed: (wiki syntax)

« Back to documentation index

lwip_mdns.c File Reference

lwip_mdns.c File Reference

MDNS responder implementation. More...

Go to the source code of this file.

Functions

err_t mdns_domain_add_label (struct mdns_domain *domain, const char *label, u8_t len)
 Add a label part to a domain.
static u16_t mdns_readname_loop (struct pbuf *p, u16_t offset, struct mdns_domain *domain, unsigned depth)
 Internal readname function with max 6 levels of recursion following jumps while decompressing name.
u16_t mdns_readname (struct pbuf *p, u16_t offset, struct mdns_domain *domain)
 Read possibly compressed domain name from packet buffer.
static void mdns_domain_debug_print (struct mdns_domain *domain)
 Print domain name to debug output.
int mdns_domain_eq (struct mdns_domain *a, struct mdns_domain *b)
 Return 1 if contents of domains match (case-insensitive)
static void mdns_prepare_txtdata (struct mdns_service *service)
 Call user supplied function to setup TXT data.
static err_t mdns_build_reverse_v4_domain (struct mdns_domain *domain, const ip4_addr_t *addr)
 Build domain for reverse lookup of IPv4 address like 12.0.168.192.in-addr.arpa.
static err_t mdns_build_reverse_v6_domain (struct mdns_domain *domain, const ip6_addr_t *addr)
 Build domain for reverse lookup of IP address like b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.
static err_t mdns_build_host_domain (struct mdns_domain *domain, struct mdns_host *mdns)
 Build the <hostname>.local.
static err_t mdns_build_dnssd_domain (struct mdns_domain *domain)
 Build the lookup-all-services special DNS-SD domain name.
static err_t mdns_build_service_domain (struct mdns_domain *domain, struct mdns_service *service, int include_name)
 Build domain name for a service.
static int check_host (struct netif *netif, struct mdns_rr_info *rr, u8_t *reverse_v6_reply)
 Check which replies we should send for a host/netif based on question.
static int check_service (struct mdns_service *service, struct mdns_rr_info *rr)
 Check which replies we should send for a service based on question.
u16_t mdns_compress_domain (struct pbuf *pbuf, u16_t *offset, struct mdns_domain *domain)
 Return bytes needed to write before jump for best result of compressing supplied domain against domain in outpacket starting at specified offset.
static err_t mdns_write_domain (struct mdns_outpacket *outpkt, struct mdns_domain *domain)
 Write domain to outpacket.
static err_t mdns_add_question (struct mdns_outpacket *outpkt, struct mdns_domain *domain, u16_t type, u16_t klass, u16_t unicast)
 Write a question to an outpacket A question contains domain, type and class.
static err_t mdns_add_answer (struct mdns_outpacket *reply, struct mdns_domain *domain, u16_t type, u16_t klass, u16_t cache_flush, u32_t ttl, const u8_t *buf, size_t buf_length, struct mdns_domain *answer_domain)
 Write answer to reply packet.
static err_t mdns_read_rr_info (struct mdns_packet *pkt, struct mdns_rr_info *info)
 Helper function for mdns_read_question/mdns_read_answer Reads a domain, type and class from the packet.
static err_t mdns_read_question (struct mdns_packet *pkt, struct mdns_question *question)
 Read a question from the packet.
static err_t mdns_read_answer (struct mdns_packet *pkt, struct mdns_answer *answer)
 Read an answer from the packet The variable length reply is not copied, its pbuf offset and length is stored instead.
static err_t mdns_add_a_answer (struct mdns_outpacket *reply, u16_t cache_flush, struct netif *netif)
 Write an IPv4 address (A) RR to outpacket.
static err_t mdns_add_hostv4_ptr_answer (struct mdns_outpacket *reply, u16_t cache_flush, struct netif *netif)
 Write a 4.3.2.1.in-addr.arpa -> hostname.local PTR RR to outpacket.
static err_t mdns_add_aaaa_answer (struct mdns_outpacket *reply, u16_t cache_flush, struct netif *netif, int addrindex)
 Write an IPv6 address (AAAA) RR to outpacket.
static err_t mdns_add_hostv6_ptr_answer (struct mdns_outpacket *reply, u16_t cache_flush, struct netif *netif, int addrindex)
 Write a x.y.z.ip6.arpa -> hostname.local PTR RR to outpacket.
static err_t mdns_add_servicetype_ptr_answer (struct mdns_outpacket *reply, struct mdns_service *service)
 Write an all-services -> servicetype PTR RR to outpacket.
static err_t mdns_add_servicename_ptr_answer (struct mdns_outpacket *reply, struct mdns_service *service)
 Write a servicetype -> servicename PTR RR to outpacket.
static err_t mdns_add_srv_answer (struct mdns_outpacket *reply, u16_t cache_flush, struct mdns_host *mdns, struct mdns_service *service)
 Write a SRV RR to outpacket.
static err_t mdns_add_txt_answer (struct mdns_outpacket *reply, u16_t cache_flush, struct mdns_service *service)
 Write a TXT RR to outpacket.
static void mdns_init_outpacket (struct mdns_outpacket *out, struct mdns_packet *in)
 Setup outpacket as a reply to the incoming packet.
static void mdns_send_outpacket (struct mdns_outpacket *outpkt)
 Send chosen answers as a reply.
static void mdns_announce (struct netif *netif, const ip_addr_t *destination)
 Send unsolicited answer containing all our known data.
static void mdns_handle_question (struct mdns_packet *pkt)
 Handle question MDNS packet 1.
static void mdns_handle_response (struct mdns_packet *pkt)
 Handle response MDNS packet Only prints debug for now.
static void mdns_recv (void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
 Receive input function for MDNS packets.
void mdns_resp_init (void)
 Initiate MDNS responder.
void mdns_resp_netif_settings_changed (struct netif *netif)
 Announce IP settings have changed on netif.
err_t mdns_resp_add_netif (struct netif *netif, const char *hostname, u32_t dns_ttl)
 Activate MDNS responder for a network interface and send announce packets.
err_t mdns_resp_remove_netif (struct netif *netif)
 Stop responding to MDNS queries on this interface, leave multicast groups, and free the helper structure and any of its services.
err_t mdns_resp_add_service (struct netif *netif, const char *name, const char *service, enum mdns_sd_proto proto, u16_t port, u32_t dns_ttl, service_get_txt_fn_t txt_fn, void *txt_data)
 Add a service to the selected network interface.
err_t mdns_resp_add_service_txtitem (struct mdns_service *service, const char *txt, u8_t txt_len)
 Call this function from inside the service_get_txt_fn_t callback to add text data.

Detailed Description

MDNS responder implementation.

Definition in file lwip_mdns.c.


Function Documentation

static int check_host ( struct netif netif,
struct mdns_rr_info *  rr,
u8_t *  reverse_v6_reply 
) [static]

Check which replies we should send for a host/netif based on question.

Parameters:
netifThe network interface that received the question
rrDomain/type/class from a question
reverse_v6_replyBitmask of which IPv6 addresses to send reverse PTRs for if reply bit has REPLY_HOST_PTR_V6 set
Returns:
Bitmask of which replies to send

Definition at line 611 of file lwip_mdns.c.

static int check_service ( struct mdns_service *  service,
struct mdns_rr_info *  rr 
) [static]

Check which replies we should send for a service based on question.

Parameters:
serviceA registered MDNS service
rrDomain/type/class from a question
Returns:
Bitmask of which replies to send

Definition at line 678 of file lwip_mdns.c.

static err_t mdns_add_a_answer ( struct mdns_outpacket *  reply,
u16_t  cache_flush,
struct netif netif 
) [static]

Write an IPv4 address (A) RR to outpacket.

Definition at line 1113 of file lwip_mdns.c.

static err_t mdns_add_aaaa_answer ( struct mdns_outpacket *  reply,
u16_t  cache_flush,
struct netif netif,
int  addrindex 
) [static]

Write an IPv6 address (AAAA) RR to outpacket.

Definition at line 1136 of file lwip_mdns.c.

static err_t mdns_add_answer ( struct mdns_outpacket *  reply,
struct mdns_domain *  domain,
u16_t  type,
u16_t  klass,
u16_t  cache_flush,
u32_t  ttl,
const u8_t *  buf,
size_t  buf_length,
struct mdns_domain *  answer_domain 
) [static]

Write answer to reply packet.

buf or answer_domain can be null. The rd_length written will be buf_length + size of (compressed) domain. Most uses will need either buf or answer_domain, special case is SRV that starts with 3 u16 and then a domain name.

Parameters:
replyThe outpacket to write to
domainThe domain name the answer is for
typeThe DNS type of the answer (like 'AAAA', 'SRV')
klassThe DNS type of the answer (like 'IN')
cache_flushIf highest bit in class should be set, to instruct receiver that this reply replaces any earlier answer for this domain/type/class
ttlValidity time in seconds to send out for IP address data in DNS replies
bufPointer to buffer of answer data
buf_lengthLength of variable data
answer_domainA domain to write after any buffer data as answer
Returns:
ERR_OK on success, an err_t otherwise

Definition at line 910 of file lwip_mdns.c.

static err_t mdns_add_hostv4_ptr_answer ( struct mdns_outpacket *  reply,
u16_t  cache_flush,
struct netif netif 
) [static]

Write a 4.3.2.1.in-addr.arpa -> hostname.local PTR RR to outpacket.

Definition at line 1123 of file lwip_mdns.c.

static err_t mdns_add_hostv6_ptr_answer ( struct mdns_outpacket *  reply,
u16_t  cache_flush,
struct netif netif,
int  addrindex 
) [static]

Write a x.y.z.ip6.arpa -> hostname.local PTR RR to outpacket.

Definition at line 1146 of file lwip_mdns.c.

static err_t mdns_add_question ( struct mdns_outpacket *  outpkt,
struct mdns_domain *  domain,
u16_t  type,
u16_t  klass,
u16_t  unicast 
) [static]

Write a question to an outpacket A question contains domain, type and class.

Since an answer also starts with these fields this function is also called from mdns_add_answer().

Parameters:
outpktThe outpacket to write to
domainThe domain name the answer is for
typeThe DNS type of the answer (like 'AAAA', 'SRV')
klassThe DNS type of the answer (like 'IN')
unicastIf highest bit in class should be set, to instruct the responder to reply with a unicast packet
Returns:
ERR_OK on success, an err_t otherwise

Definition at line 842 of file lwip_mdns.c.

static err_t mdns_add_servicename_ptr_answer ( struct mdns_outpacket *  reply,
struct mdns_service *  service 
) [static]

Write a servicetype -> servicename PTR RR to outpacket.

Definition at line 1169 of file lwip_mdns.c.

static err_t mdns_add_servicetype_ptr_answer ( struct mdns_outpacket *  reply,
struct mdns_service *  service 
) [static]

Write an all-services -> servicetype PTR RR to outpacket.

Definition at line 1158 of file lwip_mdns.c.

static err_t mdns_add_srv_answer ( struct mdns_outpacket *  reply,
u16_t  cache_flush,
struct mdns_host *  mdns,
struct mdns_service *  service 
) [static]

Write a SRV RR to outpacket.

Definition at line 1180 of file lwip_mdns.c.

static err_t mdns_add_txt_answer ( struct mdns_outpacket *  reply,
u16_t  cache_flush,
struct mdns_service *  service 
) [static]

Write a TXT RR to outpacket.

Definition at line 1203 of file lwip_mdns.c.

static void mdns_announce ( struct netif netif,
const ip_addr_t destination 
) [static]

Send unsolicited answer containing all our known data.

Parameters:
netifThe network interface to send on
destinationThe target address to send to (usually multicast address)

Definition at line 1452 of file lwip_mdns.c.

static err_t mdns_build_dnssd_domain ( struct mdns_domain *  domain ) [static]

Build the lookup-all-services special DNS-SD domain name.

Parameters:
domainWhere to write the domain name
Returns:
ERR_OK if domain _services._dns-sd._udp.local. was written, an err_t otherwise

Definition at line 564 of file lwip_mdns.c.

static err_t mdns_build_host_domain ( struct mdns_domain *  domain,
struct mdns_host *  mdns 
) [static]

Build the <hostname>.local.

domain name

Parameters:
domainWhere to write the domain name
mdnsTMDNS netif descriptor.
Returns:
ERR_OK if domain <hostname>.local. was written, an err_t otherwise

Definition at line 548 of file lwip_mdns.c.

static err_t mdns_build_reverse_v4_domain ( struct mdns_domain *  domain,
const ip4_addr_t addr 
) [static]

Build domain for reverse lookup of IPv4 address like 12.0.168.192.in-addr.arpa.

for 192.168.0.12

Parameters:
domainWhere to write the domain name
addrPointer to an IPv4 address to encode
Returns:
ERR_OK if domain was written, an err_t otherwise

Definition at line 458 of file lwip_mdns.c.

static err_t mdns_build_reverse_v6_domain ( struct mdns_domain *  domain,
const ip6_addr_t addr 
) [static]

Build domain for reverse lookup of IP address like b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.

for 2001:db8::567:89ab

Parameters:
domainWhere to write the domain name
addrPointer to an IPv6 address to encode
Returns:
ERR_OK if domain was written, an err_t otherwise

Definition at line 496 of file lwip_mdns.c.

static err_t mdns_build_service_domain ( struct mdns_domain *  domain,
struct mdns_service *  service,
int  include_name 
) [static]

Build domain name for a service.

Parameters:
domainWhere to write the domain name
serviceThe service struct, containing service name, type and protocol
include_nameWhether to include the service name in the domain
Returns:
ERR_OK if domain was written. If service name is included, <name>.<type>.<proto>.local. will be written, otherwise <type>.<proto>.local. An err_t is returned on error.

Definition at line 587 of file lwip_mdns.c.

u16_t mdns_compress_domain ( struct pbuf pbuf,
u16_t *  offset,
struct mdns_domain *  domain 
)

Return bytes needed to write before jump for best result of compressing supplied domain against domain in outpacket starting at specified offset.

If a match is found, offset is updated to where to jump to

Parameters:
pbufPointer to pbuf with the partially constructed DNS packet
offsetStart position of a domain written earlier. If this location is suitable for compression, the pointer is updated to where in the domain to jump to.
domainThe domain to write
Returns:
Number of bytes to write of the new domain before writing a jump to the offset. If compression can not be done against this previous domain name, the full new domain length is returned.

Definition at line 730 of file lwip_mdns.c.

err_t mdns_domain_add_label ( struct mdns_domain *  domain,
const char *  label,
u8_t  len 
)

Add a label part to a domain.

Parameters:
domainThe domain to add a label to
labelThe label to add, like <hostname>, 'local', 'com' or ''
lenThe length of the label
Returns:
ERR_OK on success, an err_t otherwise if label too long

Definition at line 272 of file lwip_mdns.c.

static void mdns_domain_debug_print ( struct mdns_domain *  domain ) [static]

Print domain name to debug output.

Parameters:
domainThe domain name

Definition at line 381 of file lwip_mdns.c.

int mdns_domain_eq ( struct mdns_domain *  a,
struct mdns_domain *  b 
)

Return 1 if contents of domains match (case-insensitive)

Parameters:
aDomain name to compare 1
bDomain name to compare 2
Returns:
1 if domains are equal ignoring case, 0 otherwise

Definition at line 404 of file lwip_mdns.c.

static void mdns_handle_question ( struct mdns_packet *  pkt ) [static]

Handle question MDNS packet 1.

Parse all questions and set bits what answers to send 2. Clear pending answers if known answers are supplied 3. Put chosen answers in new packet and send as reply

Definition at line 1494 of file lwip_mdns.c.

static void mdns_handle_response ( struct mdns_packet *  pkt ) [static]

Handle response MDNS packet Only prints debug for now.

Will need more code to do conflict resolution.

Definition at line 1708 of file lwip_mdns.c.

static void mdns_init_outpacket ( struct mdns_outpacket *  out,
struct mdns_packet *  in 
) [static]

Setup outpacket as a reply to the incoming packet.

Definition at line 1217 of file lwip_mdns.c.

static void mdns_prepare_txtdata ( struct mdns_service *  service ) [static]

Call user supplied function to setup TXT data.

Parameters:
serviceThe service to build TXT record for

Definition at line 441 of file lwip_mdns.c.

static err_t mdns_read_answer ( struct mdns_packet *  pkt,
struct mdns_answer *  answer 
) [static]

Read an answer from the packet The variable length reply is not copied, its pbuf offset and length is stored instead.

Parameters:
pktThe MDNS packet to read. The answers_left field will be decremented and the parse_offset will be updated.
answerThe struct to fill with answer data
Returns:
ERR_OK on success, an err_t otherwise

Definition at line 1060 of file lwip_mdns.c.

static err_t mdns_read_question ( struct mdns_packet *  pkt,
struct mdns_question *  question 
) [static]

Read a question from the packet.

All questions have to be read before the answers.

Parameters:
pktThe MDNS packet to read from. The questions_left field will be decremented and the parse_offset will be updated.
questionThe struct to fill with question data
Returns:
ERR_OK on success, an err_t otherwise

Definition at line 1025 of file lwip_mdns.c.

static err_t mdns_read_rr_info ( struct mdns_packet *  pkt,
struct mdns_rr_info *  info 
) [static]

Helper function for mdns_read_question/mdns_read_answer Reads a domain, type and class from the packet.

Parameters:
pktThe MDNS packet to read from. The parse_offset field will be incremented to point to the next unparsed byte.
infoThe struct to fill with domain, type and class
Returns:
ERR_OK on success, an err_t otherwise

Definition at line 991 of file lwip_mdns.c.

u16_t mdns_readname ( struct pbuf p,
u16_t  offset,
struct mdns_domain *  domain 
)

Read possibly compressed domain name from packet buffer.

Parameters:
pThe packet
offsetstart position of domain name in packet
domainThe domain name destination
Returns:
The new offset after the domain, or MDNS_READNAME_ERROR if reading failed

Definition at line 370 of file lwip_mdns.c.

static u16_t mdns_readname_loop ( struct pbuf p,
u16_t  offset,
struct mdns_domain *  domain,
unsigned  depth 
) [static]

Internal readname function with max 6 levels of recursion following jumps while decompressing name.

Definition at line 298 of file lwip_mdns.c.

static void mdns_recv ( void *  arg,
struct udp_pcb pcb,
struct pbuf p,
const ip_addr_t addr,
u16_t  port 
) [static]

Receive input function for MDNS packets.

Handles both IPv4 and IPv6 UDP pcbs.

Definition at line 1743 of file lwip_mdns.c.

static void mdns_send_outpacket ( struct mdns_outpacket *  outpkt ) [static]

Send chosen answers as a reply.

Add all selected answers (first write will allocate pbuf) Add additional answers based on the selected answers Send the packet

Definition at line 1251 of file lwip_mdns.c.

static err_t mdns_write_domain ( struct mdns_outpacket *  outpkt,
struct mdns_domain *  domain 
) [static]

Write domain to outpacket.

Compression will be attempted, unless domain->skip_compression is set.

Parameters:
outpktThe outpacket to write to
domainThe domain name to write
Returns:
ERR_OK on success, an err_t otherwise

Definition at line 779 of file lwip_mdns.c.