Fix mdnsresponder compilation issues (#481)
This commit is contained in:
parent
f67495f4f0
commit
6faf7c569d
2 changed files with 72 additions and 69 deletions
|
@ -25,6 +25,7 @@
|
||||||
#include <lwip/sys.h>
|
#include <lwip/sys.h>
|
||||||
#include <lwip/netdb.h>
|
#include <lwip/netdb.h>
|
||||||
#include <lwip/dns.h>
|
#include <lwip/dns.h>
|
||||||
|
#include <lwip/prot/dns.h>
|
||||||
#include <lwip/udp.h>
|
#include <lwip/udp.h>
|
||||||
#include <lwip/igmp.h>
|
#include <lwip/igmp.h>
|
||||||
#include <lwip/netif.h>
|
#include <lwip/netif.h>
|
||||||
|
@ -39,6 +40,7 @@
|
||||||
|
|
||||||
#define DNS_MULTICAST_ADDRESS "224.0.0.251" // RFC 6762
|
#define DNS_MULTICAST_ADDRESS "224.0.0.251" // RFC 6762
|
||||||
#define DNS_MDNS_PORT 5353 // RFC 6762
|
#define DNS_MDNS_PORT 5353 // RFC 6762
|
||||||
|
#define DNS_MSG_SIZE 512
|
||||||
|
|
||||||
//-------------------------------------------------------------------
|
//-------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -100,7 +102,7 @@ PACK_STRUCT_END
|
||||||
#ifdef PACK_STRUCT_USE_INCLUDES
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
||||||
# include "arch/epstruct.h"
|
# include "arch/epstruct.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
|
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
|
||||||
#define UNUSED_ARG(x) (void)x
|
#define UNUSED_ARG(x) (void)x
|
||||||
#define kDummyDataSize 8 // arbitrary, dynamically resized
|
#define kDummyDataSize 8 // arbitrary, dynamically resized
|
||||||
|
@ -145,7 +147,7 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
#define DNS_FLAG2_RESMASK 0x0F
|
#define DNS_FLAG2_RESMASK 0x0F
|
||||||
|
|
||||||
static char qstr[12];
|
static char qstr[12];
|
||||||
|
|
||||||
static char* mdns_qrtype(uint16_t typ)
|
static char* mdns_qrtype(uint16_t typ)
|
||||||
{
|
{
|
||||||
switch(typ) {
|
switch(typ) {
|
||||||
|
@ -161,9 +163,9 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
sprintf(qstr,"type %d",typ);
|
sprintf(qstr,"type %d",typ);
|
||||||
return qstr;
|
return qstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef qLogAllTraffic
|
#ifdef qLogAllTraffic
|
||||||
|
|
||||||
static void mdns_printhex(u8_t* p, int n)
|
static void mdns_printhex(u8_t* p, int n)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -178,14 +180,14 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
{
|
{
|
||||||
int i, n;
|
int i, n;
|
||||||
char* cp;
|
char* cp;
|
||||||
|
|
||||||
n = *p++;
|
n = *p++;
|
||||||
cp = (char*)p;
|
cp = (char*)p;
|
||||||
for (i=0; i<n; i++) putchar(*cp++);
|
for (i=0; i<n; i++) putchar(*cp++);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char cstr[16];
|
static char cstr[16];
|
||||||
|
|
||||||
static char* mdns_qclass(uint16_t cls)
|
static char* mdns_qclass(uint16_t cls)
|
||||||
{
|
{
|
||||||
switch(cls) {
|
switch(cls) {
|
||||||
|
@ -202,7 +204,7 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
{
|
{
|
||||||
char* cp = (char*)p;
|
char* cp = (char*)p;
|
||||||
int i, n;
|
int i, n;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
n = *cp++;
|
n = *cp++;
|
||||||
if ((n & 0xC0) == 0xC0) {
|
if ((n & 0xC0) == 0xC0) {
|
||||||
|
@ -222,7 +224,7 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
return (u8_t*)cp;
|
return (u8_t*)cp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static u8_t* mdns_print_header(struct mdns_hdr* hdr)
|
static u8_t* mdns_print_header(struct mdns_hdr* hdr)
|
||||||
{
|
{
|
||||||
if (hdr->flags1 & DNS_FLAG1_RESP) {
|
if (hdr->flags1 & DNS_FLAG1_RESP) {
|
||||||
|
@ -235,7 +237,7 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
}
|
}
|
||||||
if (hdr->flags1 & DNS_FLAG1_RD) printf("RD ");
|
if (hdr->flags1 & DNS_FLAG1_RD) printf("RD ");
|
||||||
if (hdr->flags1 & DNS_FLAG1_TRUNC) printf("[TRUNC] ");
|
if (hdr->flags1 & DNS_FLAG1_TRUNC) printf("[TRUNC] ");
|
||||||
|
|
||||||
printf(": %d questions", htons(hdr->numquestions) );
|
printf(": %d questions", htons(hdr->numquestions) );
|
||||||
if (hdr->numanswers != 0)
|
if (hdr->numanswers != 0)
|
||||||
printf(", %d answers",htons(hdr->numanswers));
|
printf(", %d answers",htons(hdr->numanswers));
|
||||||
|
@ -252,7 +254,7 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
{
|
{
|
||||||
struct mdns_query q;
|
struct mdns_query q;
|
||||||
uint16_t c;
|
uint16_t c;
|
||||||
|
|
||||||
memcpy(&q,p,SIZEOF_DNS_QUERY);
|
memcpy(&q,p,SIZEOF_DNS_QUERY);
|
||||||
c = htons(q.class);
|
c = htons(q.class);
|
||||||
printf(" %s %s", mdns_qrtype(htons(q.type)), mdns_qclass(c & 0x7FFF) );
|
printf(" %s %s", mdns_qrtype(htons(q.type)), mdns_qclass(c & 0x7FFF) );
|
||||||
|
@ -266,7 +268,7 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
{
|
{
|
||||||
struct mdns_answer ans;
|
struct mdns_answer ans;
|
||||||
u16_t rrlen, atype, rrClass;;
|
u16_t rrlen, atype, rrClass;;
|
||||||
|
|
||||||
memcpy(&ans,p,SIZEOF_DNS_ANSWER);
|
memcpy(&ans,p,SIZEOF_DNS_ANSWER);
|
||||||
atype = htons(ans.type);
|
atype = htons(ans.type);
|
||||||
rrlen = htons(ans.len);
|
rrlen = htons(ans.len);
|
||||||
|
@ -305,7 +307,7 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
u8_t* tp;
|
u8_t* tp;
|
||||||
u8_t* limP = msgP + msgLen;
|
u8_t* limP = msgP + msgLen;
|
||||||
struct mdns_hdr* hdr;
|
struct mdns_hdr* hdr;
|
||||||
|
|
||||||
hdr = (struct mdns_hdr*) msgP;
|
hdr = (struct mdns_hdr*) msgP;
|
||||||
tp = mdns_print_header(hdr);
|
tp = mdns_print_header(hdr);
|
||||||
for (i=0; i<htons(hdr->numquestions); i++) {
|
for (i=0; i<htons(hdr->numquestions); i++) {
|
||||||
|
@ -314,21 +316,21 @@ static mdns_rsrc* gDictP = NULL; // RR database, linked list
|
||||||
tp = mdns_print_query(tp);
|
tp = mdns_print_query(tp);
|
||||||
if (tp > limP) return 0;
|
if (tp > limP) return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<htons(hdr->numanswers); i++) {
|
for (i=0; i<htons(hdr->numanswers); i++) {
|
||||||
printf(" A%d: ",i+1);
|
printf(" A%d: ",i+1);
|
||||||
tp = mdns_print_name(tp,hdr);
|
tp = mdns_print_name(tp,hdr);
|
||||||
tp = mdns_print_answer(tp,hdr);
|
tp = mdns_print_answer(tp,hdr);
|
||||||
if (tp > limP) return 0;
|
if (tp > limP) return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<htons(hdr->numauthrr); i++) {
|
for (i=0; i<htons(hdr->numauthrr); i++) {
|
||||||
printf(" AuRR%d: ",i+1);
|
printf(" AuRR%d: ",i+1);
|
||||||
tp = mdns_print_name(tp,hdr);
|
tp = mdns_print_name(tp,hdr);
|
||||||
tp = mdns_print_answer(tp,hdr);
|
tp = mdns_print_answer(tp,hdr);
|
||||||
if (tp > limP) return 0;
|
if (tp > limP) return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<htons(hdr->numextrarr); i++) {
|
for (i=0; i<htons(hdr->numextrarr); i++) {
|
||||||
printf(" ExRR%d: ",i+1);
|
printf(" ExRR%d: ",i+1);
|
||||||
tp = mdns_print_name(tp,hdr);
|
tp = mdns_print_name(tp,hdr);
|
||||||
|
@ -348,7 +350,7 @@ static u8_t* mdns_labels2str(u8_t* hdrP, u8_t* p, char* qStr)
|
||||||
// Handles compression
|
// Handles compression
|
||||||
{
|
{
|
||||||
int i, n;
|
int i, n;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
n = *p++;
|
n = *p++;
|
||||||
if ((n & 0xC0) == 0xC0) {
|
if ((n & 0xC0) == 0xC0) {
|
||||||
|
@ -374,7 +376,7 @@ static int mdns_str2labels(const char* name, u8_t* lseq, int max)
|
||||||
{
|
{
|
||||||
int i,n,sdx,idx = 0;
|
int i,n,sdx,idx = 0;
|
||||||
int lc = 0;
|
int lc = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
sdx = idx;
|
sdx = idx;
|
||||||
while (name[idx] != '.' && name[idx] != 0) idx++;
|
while (name[idx] != '.' && name[idx] != 0) idx++;
|
||||||
|
@ -399,7 +401,7 @@ static u8_t* mdns_get_question(u8_t* hdrP, u8_t* qp, char* qStr, uint16_t* qClas
|
||||||
{
|
{
|
||||||
struct mdns_query qr;
|
struct mdns_query qr;
|
||||||
uint16_t cls;
|
uint16_t cls;
|
||||||
|
|
||||||
qp = mdns_labels2str(hdrP, qp, qStr);
|
qp = mdns_labels2str(hdrP, qp, qStr);
|
||||||
memcpy(&qr,qp,SIZEOF_DNS_QUERY);
|
memcpy(&qr,qp,SIZEOF_DNS_QUERY);
|
||||||
*qType = htons(qr.type);
|
*qType = htons(qr.type);
|
||||||
|
@ -417,7 +419,7 @@ static void mdns_add_response(const char* vKey, u16_t vType, u32_t ttl, const vo
|
||||||
{
|
{
|
||||||
mdns_rsrc* rsrcP;
|
mdns_rsrc* rsrcP;
|
||||||
int keyLen, recSize;
|
int keyLen, recSize;
|
||||||
|
|
||||||
keyLen = strlen(vKey) + 1;
|
keyLen = strlen(vKey) + 1;
|
||||||
recSize = sizeof(mdns_rsrc) - kDummyDataSize + keyLen + vDataSize;
|
recSize = sizeof(mdns_rsrc) - kDummyDataSize + keyLen + vDataSize;
|
||||||
rsrcP = (mdns_rsrc*)malloc(recSize);
|
rsrcP = (mdns_rsrc*)malloc(recSize);
|
||||||
|
@ -442,7 +444,7 @@ void mdns_add_PTR(const char* rKey, u32_t ttl, const char* nmStr)
|
||||||
{
|
{
|
||||||
int nl;
|
int nl;
|
||||||
u8_t lBuff[kMaxNameSize];
|
u8_t lBuff[kMaxNameSize];
|
||||||
|
|
||||||
nl = mdns_str2labels(nmStr,lBuff,sizeof(lBuff));
|
nl = mdns_str2labels(nmStr,lBuff,sizeof(lBuff));
|
||||||
if (nl>0)
|
if (nl>0)
|
||||||
mdns_add_response(rKey, DNS_RRTYPE_PTR, ttl, lBuff, nl);
|
mdns_add_response(rKey, DNS_RRTYPE_PTR, ttl, lBuff, nl);
|
||||||
|
@ -454,10 +456,10 @@ void mdns_add_SRV(const char* rKey, u32_t ttl, u16_t rPort, const char* targName
|
||||||
struct mdns_rr_srv srvRR;
|
struct mdns_rr_srv srvRR;
|
||||||
u8_t lBuff[kMaxNameSize];
|
u8_t lBuff[kMaxNameSize];
|
||||||
} __attribute__((packed)) SrvRec;
|
} __attribute__((packed)) SrvRec;
|
||||||
|
|
||||||
int nl;
|
int nl;
|
||||||
SrvRec temp;
|
SrvRec temp;
|
||||||
|
|
||||||
temp.srvRR.prio = 0;
|
temp.srvRR.prio = 0;
|
||||||
temp.srvRR.weight = 0;
|
temp.srvRR.weight = 0;
|
||||||
temp.srvRR.port = htons(rPort);
|
temp.srvRR.port = htons(rPort);
|
||||||
|
@ -480,46 +482,45 @@ void mdns_add_TXT(const char* rKey, u32_t ttl, const char* txStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mdns_add_A(const char* rKey, u32_t ttl, struct ip_addr addr)
|
void mdns_add_A(const char* rKey, u32_t ttl, ip_addr_t addr)
|
||||||
{
|
{
|
||||||
mdns_add_response(rKey, DNS_RRTYPE_A, ttl, &addr, sizeof(addr));
|
mdns_add_response(rKey, DNS_RRTYPE_A, ttl, &addr, sizeof(addr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mdns_add_facility( const char* instanceName, // Friendly name, need not be unique
|
void mdns_add_facility( const char* instanceName, // Friendly name, need not be unique
|
||||||
const char* serviceName, // Must be _name
|
const char* serviceName, // Must be "name", e.g. "hap" or "http"
|
||||||
const char* addText, // Must be <key>=<value>
|
const char* addText, // Must be <key>=<value>
|
||||||
mdns_flags flags, // TCP or UDP
|
mdns_flags flags, // TCP or UDP
|
||||||
u16_t onPort, // port number
|
u16_t onPort, // port number
|
||||||
u32_t ttl // seconds
|
u32_t ttl // seconds
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
char key[64];
|
char key[64];
|
||||||
char fullName[128];
|
char fullName[128];
|
||||||
char devName[96];
|
char devName[96];
|
||||||
struct ip_info ipInfo;
|
struct ip_info ipInfo;
|
||||||
|
|
||||||
#ifdef qDebugLog
|
#ifdef qDebugLog
|
||||||
printf("\nmDNS advertising instance %s protocol %s text %s on port %d %s TTL %d secs\n",
|
printf("\nmDNS advertising instance %s protocol %s text %s on port %d %s TTL %d secs\n",
|
||||||
instanceName,serviceName,addText,onPort,(flags & mdns_UDP) ? "UDP" : "TCP", ttl);
|
instanceName, serviceName, addText, onPort, (flags & mdns_UDP) ? "UDP" : "TCP", ttl);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
snprintf(key,sizeof(key),"%s.%s.local.",serviceName,(flags & mdns_UDP) ? "_udp" :"_tcp");
|
snprintf(key, sizeof(key), "%s.%s.local.", serviceName, (flags & mdns_UDP) ? "_udp" :"_tcp");
|
||||||
snprintf(fullName,sizeof(fullName),"%s.%s",instanceName,key);
|
snprintf(fullName, sizeof(fullName), "%s.%s", instanceName, key);
|
||||||
snprintf(devName,sizeof(devName),"%s.local.",instanceName);
|
snprintf(devName, sizeof(devName), "%s.local.", instanceName);
|
||||||
|
|
||||||
if (!sdk_wifi_get_ip_info(STATION_IF,&ipInfo))
|
if (!sdk_wifi_get_ip_info(STATION_IF,&ipInfo))
|
||||||
ipInfo.ip.addr = IPADDR_NONE;
|
ipInfo.ip.addr = IPADDR_NONE;
|
||||||
|
|
||||||
// Order has significance for extraRR feature
|
// Order has significance for extraRR feature
|
||||||
mdns_add_TXT(fullName,ttl,addText);
|
mdns_add_TXT(fullName, ttl, addText);
|
||||||
mdns_add_A(devName,ttl,ipInfo.ip);
|
mdns_add_A(devName, ttl, ipInfo.ip);
|
||||||
mdns_add_SRV(fullName,ttl,onPort,devName);
|
mdns_add_SRV(fullName, ttl, onPort, devName);
|
||||||
mdns_add_PTR(key,ttl,fullName);
|
mdns_add_PTR(key, ttl, fullName);
|
||||||
|
|
||||||
// Optional, makes us browsable
|
// Optional, makes us browsable
|
||||||
if (flags & mdns_Browsable)
|
if (flags & mdns_Browsable)
|
||||||
mdns_add_PTR("_services._dns-sd._udp.local.",ttl,key);
|
mdns_add_PTR("_services._dns-sd._udp.local.",ttl,key);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mdns_update_ipaddr(struct ip_info* ipInfo)
|
static void mdns_update_ipaddr(struct ip_info* ipInfo)
|
||||||
|
@ -573,7 +574,7 @@ static int mdns_add_to_answer(mdns_rsrc* rsrcP, u8_t* resp, int respLen)
|
||||||
// Data for this key
|
// Data for this key
|
||||||
memcpy(&resp[respLen], &rsrcP->rData[rsrcP->rKeySize], rsrcP->rDataSize);
|
memcpy(&resp[respLen], &rsrcP->rData[rsrcP->rKeySize], rsrcP->rDataSize);
|
||||||
respLen += rsrcP->rDataSize;
|
respLen += rsrcP->rDataSize;
|
||||||
|
|
||||||
return respLen;
|
return respLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -599,7 +600,7 @@ static void mdns_send_mcast(u8_t* msgP, int nBytes)
|
||||||
} else
|
} else
|
||||||
printf(">>> mdns_send: alloc failed[%d]\n",nBytes);
|
printf(">>> mdns_send: alloc failed[%d]\n",nBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mdns_reply(struct mdns_hdr* hdrP)
|
static void mdns_reply(struct mdns_hdr* hdrP)
|
||||||
// Message has passed tests, may want to send an answer
|
// Message has passed tests, may want to send an answer
|
||||||
{
|
{
|
||||||
|
@ -609,13 +610,13 @@ static void mdns_reply(struct mdns_hdr* hdrP)
|
||||||
u8_t* qBase = (u8_t*)hdrP;
|
u8_t* qBase = (u8_t*)hdrP;
|
||||||
u8_t* qp;
|
u8_t* qp;
|
||||||
u8_t* mdns_response;
|
u8_t* mdns_response;
|
||||||
|
|
||||||
mdns_response = malloc(DNS_MSG_SIZE);
|
mdns_response = malloc(DNS_MSG_SIZE);
|
||||||
if (mdns_response==NULL) {
|
if (mdns_response==NULL) {
|
||||||
printf(">>> mdns_reply could not alloc %d\n",DNS_MSG_SIZE);
|
printf(">>> mdns_reply could not alloc %d\n",DNS_MSG_SIZE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build response header
|
// Build response header
|
||||||
rHdr = (struct mdns_hdr*) mdns_response;
|
rHdr = (struct mdns_hdr*) mdns_response;
|
||||||
rHdr->id = hdrP->id;
|
rHdr->id = hdrP->id;
|
||||||
|
@ -630,28 +631,28 @@ static void mdns_reply(struct mdns_hdr* hdrP)
|
||||||
extra = NULL;
|
extra = NULL;
|
||||||
qp = qBase + SIZEOF_DNS_HDR;
|
qp = qBase + SIZEOF_DNS_HDR;
|
||||||
nquestions = htons(hdrP->numquestions);
|
nquestions = htons(hdrP->numquestions);
|
||||||
|
|
||||||
for (i=0; i<nquestions; i++) {
|
for (i=0; i<nquestions; i++) {
|
||||||
char qStr[kMaxQStr];
|
char qStr[kMaxQStr];
|
||||||
u16_t qClass, qType;
|
u16_t qClass, qType;
|
||||||
u8_t qUnicast;
|
u8_t qUnicast;
|
||||||
mdns_rsrc* rsrcP;
|
mdns_rsrc* rsrcP;
|
||||||
|
|
||||||
qp = mdns_get_question(qBase, qp, qStr, &qClass, &qType, &qUnicast);
|
qp = mdns_get_question(qBase, qp, qStr, &qClass, &qType, &qUnicast);
|
||||||
if (qClass==DNS_RRCLASS_IN || qClass==DNS_RRCLASS_ANY) {
|
if (qClass==DNS_RRCLASS_IN || qClass==DNS_RRCLASS_ANY) {
|
||||||
rsrcP = mdns_match(qStr,qType);
|
rsrcP = mdns_match(qStr, qType);
|
||||||
if (rsrcP) {
|
if (rsrcP) {
|
||||||
respLen = mdns_add_to_answer(rsrcP, mdns_response, respLen);
|
respLen = mdns_add_to_answer(rsrcP, mdns_response, respLen);
|
||||||
rHdr->numanswers = htons( htons(rHdr->numanswers) + 1 );
|
rHdr->numanswers = htons( htons(rHdr->numanswers) + 1 );
|
||||||
// Extra RR logic: if SRV follows PTR, or A follows SRV, volunteer it in extraRR
|
// Extra RR logic: if SRV follows PTR, or A follows SRV, volunteer it in extraRR
|
||||||
// Not required, but could do more here, see RFC6763 s12
|
// Not required, but could do more here, see RFC6763 s12
|
||||||
if (qType==DNS_RRTYPE_PTR) {
|
if (qType==DNS_RRTYPE_PTR) {
|
||||||
if (rsrcP->rNext && rsrcP->rNext->rType==DNS_RRTYPE_SRV)
|
if (rsrcP->rNext && rsrcP->rNext->rType==DNS_RRTYPE_SRV)
|
||||||
extra = rsrcP->rNext;
|
extra = rsrcP->rNext;
|
||||||
} else if (qType==DNS_RRTYPE_SRV) {
|
} else if (qType==DNS_RRTYPE_SRV) {
|
||||||
if (rsrcP->rNext && rsrcP->rNext->rType==DNS_RRTYPE_A)
|
if (rsrcP->rNext && rsrcP->rNext->rType==DNS_RRTYPE_A)
|
||||||
extra = rsrcP->rNext;
|
extra = rsrcP->rNext;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // for nQuestions
|
} // for nQuestions
|
||||||
|
@ -661,20 +662,20 @@ static void mdns_reply(struct mdns_hdr* hdrP)
|
||||||
respLen = mdns_add_to_answer(extra, mdns_response, respLen);
|
respLen = mdns_add_to_answer(extra, mdns_response, respLen);
|
||||||
rHdr->numextrarr = htons( htons(rHdr->numextrarr) + 1 );
|
rHdr->numextrarr = htons( htons(rHdr->numextrarr) + 1 );
|
||||||
}
|
}
|
||||||
mdns_send_mcast(mdns_response,respLen);
|
mdns_send_mcast(mdns_response, respLen);
|
||||||
}
|
}
|
||||||
free(mdns_response);
|
free(mdns_response);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mdns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
|
static void mdns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
|
||||||
// Callback from udp_recv
|
// Callback from udp_recv
|
||||||
{
|
{
|
||||||
UNUSED_ARG(pcb);
|
UNUSED_ARG(pcb);
|
||||||
UNUSED_ARG(port);
|
UNUSED_ARG(port);
|
||||||
|
|
||||||
u8_t* mdns_payload;
|
u8_t* mdns_payload;
|
||||||
int plen;
|
int plen;
|
||||||
|
|
||||||
// Sanity checks on size
|
// Sanity checks on size
|
||||||
plen = p->tot_len;
|
plen = p->tot_len;
|
||||||
if (plen > DNS_MSG_SIZE) {
|
if (plen > DNS_MSG_SIZE) {
|
||||||
|
@ -691,12 +692,12 @@ static void mdns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_
|
||||||
else {
|
else {
|
||||||
if (pbuf_copy_partial(p, mdns_payload, plen, 0) == plen) {
|
if (pbuf_copy_partial(p, mdns_payload, plen, 0) == plen) {
|
||||||
struct mdns_hdr* hdrP = (struct mdns_hdr*) mdns_payload;
|
struct mdns_hdr* hdrP = (struct mdns_hdr*) mdns_payload;
|
||||||
|
|
||||||
#ifdef qLogAllTraffic
|
#ifdef qLogAllTraffic
|
||||||
mdns_print_msg(mdns_payload, plen);
|
mdns_print_msg(mdns_payload, plen);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if ( (hdrP->flags1 & (DNS_FLAG1_RESP + DNS_FLAG1_OPMASK + DNS_FLAG1_TRUNC) ) == 0
|
if ( (hdrP->flags1 & (DNS_FLAG1_RESP + DNS_FLAG1_OPMASK + DNS_FLAG1_TRUNC) ) == 0
|
||||||
&& hdrP->numquestions > 0 )
|
&& hdrP->numquestions > 0 )
|
||||||
mdns_reply(hdrP);
|
mdns_reply(hdrP);
|
||||||
}
|
}
|
||||||
|
@ -711,19 +712,19 @@ static void mdns_start()
|
||||||
{
|
{
|
||||||
struct ip_info ipInfo;
|
struct ip_info ipInfo;
|
||||||
err_t err;
|
err_t err;
|
||||||
|
|
||||||
if (sdk_wifi_get_opmode() != STATION_MODE) {
|
if (sdk_wifi_get_opmode() != STATION_MODE) {
|
||||||
printf(">>> mDNS_start: wifi opmode not station\n");
|
printf(">>> mDNS_start: wifi opmode not station\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sdk_wifi_get_ip_info(STATION_IF,&ipInfo)) {
|
if (!sdk_wifi_get_ip_info(STATION_IF,&ipInfo)) {
|
||||||
printf(">>> mDNS_start: no IP addr\n");
|
printf(">>> mDNS_start: no IP addr\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mdns_update_ipaddr(&ipInfo);
|
mdns_update_ipaddr(&ipInfo);
|
||||||
|
|
||||||
// Start IGMP on the netif for our interface: this isn't done for us
|
// Start IGMP on the netif for our interface: this isn't done for us
|
||||||
struct netif* nfp = netif_list;
|
struct netif* nfp = netif_list;
|
||||||
while (nfp!=NULL) {
|
while (nfp!=NULL) {
|
||||||
|
@ -739,23 +740,23 @@ static void mdns_start()
|
||||||
}
|
}
|
||||||
nfp = nfp->next;
|
nfp = nfp->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
gMDNS_pcb = udp_new();
|
gMDNS_pcb = udp_new();
|
||||||
if (!gMDNS_pcb) {
|
if (!gMDNS_pcb) {
|
||||||
printf(">>> mDNS_start: udp_new failed\n");
|
printf(">>> mDNS_start: udp_new failed\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((err=igmp_joingroup(&ipInfo.ip, &gMulticastAddr)) != ERR_OK) {
|
if ((err=igmp_joingroup(&ipInfo.ip, &gMulticastAddr)) != ERR_OK) {
|
||||||
printf(">>> mDNS_start: igmp_join failed %d\n",err);
|
printf(">>> mDNS_start: igmp_join failed %d\n",err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((err=udp_bind(gMDNS_pcb, IP_ADDR_ANY, DNS_MDNS_PORT)) != ERR_OK) {
|
if ((err=udp_bind(gMDNS_pcb, IP_ADDR_ANY, DNS_MDNS_PORT)) != ERR_OK) {
|
||||||
printf(">>> mDNS_start: udp_bind failed %d\n",err);
|
printf(">>> mDNS_start: udp_bind failed %d\n",err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
udp_recv(gMDNS_pcb, mdns_recv, NULL);
|
udp_recv(gMDNS_pcb, mdns_recv, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
#ifndef __MDNSRESPONDER_H__
|
#ifndef __MDNSRESPONDER_H__
|
||||||
#define __MDNSRESPONDER_H__
|
#define __MDNSRESPONDER_H__
|
||||||
|
|
||||||
|
#include <lwip/ip_addr.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Basic multicast DNS responder
|
* Basic multicast DNS responder
|
||||||
*
|
*
|
||||||
* Advertises the IP address, port, and characteristics of a service to other devices using multicast DNS on the same LAN,
|
* Advertises the IP address, port, and characteristics of a service to other devices using multicast DNS on the same LAN,
|
||||||
* so they can find devices with addresses dynamically allocated by DHCP. See avahi, Bonjour, etc
|
* so they can find devices with addresses dynamically allocated by DHCP. See avahi, Bonjour, etc
|
||||||
* See RFC6762, RFC6763
|
* See RFC6762, RFC6763
|
||||||
|
@ -26,7 +28,7 @@ typedef enum {
|
||||||
mdns_UDP,
|
mdns_UDP,
|
||||||
mdns_Browsable // see RFC6763:11 - adds a standard record that lets browsers find the service without needing to know its name
|
mdns_Browsable // see RFC6763:11 - adds a standard record that lets browsers find the service without needing to know its name
|
||||||
} mdns_flags;
|
} mdns_flags;
|
||||||
|
|
||||||
void mdns_add_facility( const char* instanceName, // Short user-friendly instance name, should NOT include serial number/MAC/etc
|
void mdns_add_facility( const char* instanceName, // Short user-friendly instance name, should NOT include serial number/MAC/etc
|
||||||
const char* serviceName, // Must be registered, _name, (see RFC6335 5.1 & 5.2)
|
const char* serviceName, // Must be registered, _name, (see RFC6335 5.1 & 5.2)
|
||||||
const char* addText, // Should be <key>=<value>, or "" if unused (see RFC6763 6.3)
|
const char* addText, // Should be <key>=<value>, or "" if unused (see RFC6763 6.3)
|
||||||
|
@ -40,13 +42,13 @@ void mdns_add_facility( const char* instanceName, // Short user-friendly insta
|
||||||
void mdns_add_PTR(const char* rKey, u32_t ttl, const char* nameStr);
|
void mdns_add_PTR(const char* rKey, u32_t ttl, const char* nameStr);
|
||||||
void mdns_add_SRV(const char* rKey, u32_t ttl, u16_t rPort, const char* targname);
|
void mdns_add_SRV(const char* rKey, u32_t ttl, u16_t rPort, const char* targname);
|
||||||
void mdns_add_TXT(const char* rKey, u32_t ttl, const char* txtStr);
|
void mdns_add_TXT(const char* rKey, u32_t ttl, const char* txtStr);
|
||||||
void mdns_add_A (const char* rKey, u32_t ttl, struct ip_addr addr);
|
void mdns_add_A (const char* rKey, u32_t ttl, ip_addr_t addr);
|
||||||
|
|
||||||
/* Sample usage, advertising a secure web service
|
/* Sample usage, advertising a secure web service
|
||||||
|
|
||||||
mdns_init();
|
mdns_init();
|
||||||
mdns_add_facility("Fluffy", "_https", "Zoom=1", mdns_TCP+mdns_Browsable, 443, 600);
|
mdns_add_facility("Fluffy", "_https", "Zoom=1", mdns_TCP+mdns_Browsable, 443, 600);
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue