2020-02-12 18:43:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2020-02-13 16:29:52 +00:00
|
|
|
#include <stdarg.h>
|
2020-03-23 20:48:17 +00:00
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
#ifdef cwLINUX
|
2020-02-12 18:43:25 +00:00
|
|
|
#include <arpa/inet.h>
|
2020-02-13 16:29:52 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ARDUINO
|
2020-03-23 20:48:17 +00:00
|
|
|
#include <Ethernet.h>
|
|
|
|
#include <utility/w5100.h>
|
2020-02-13 16:29:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "rpt.h"
|
|
|
|
#include "dns_sd.h"
|
2020-02-12 18:43:25 +00:00
|
|
|
#include "dns_sd_print.h"
|
|
|
|
#include "dns_sd_const.h"
|
|
|
|
|
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
int _print_name( printCallback_t printCbFunc, const unsigned char* s, const unsigned char* buf )
|
2020-02-12 18:43:25 +00:00
|
|
|
{
|
2020-02-13 16:29:52 +00:00
|
|
|
int n = 0; // track allocated length of the name in this record
|
2020-02-12 18:43:25 +00:00
|
|
|
bool incrFl = true; // do not incrmement 'n' if the name switches to a ptr segment
|
|
|
|
|
|
|
|
while( *s )
|
|
|
|
{
|
|
|
|
if( (*s & 0xc0) == 0xc0 )
|
|
|
|
{
|
|
|
|
if( incrFl )
|
|
|
|
n += 2;
|
|
|
|
incrFl = false;
|
|
|
|
s = buf + s[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(char i=0; i<s[0]; ++i)
|
|
|
|
{
|
2020-02-13 16:29:52 +00:00
|
|
|
char x[2];
|
|
|
|
x[0] = s[i+1];
|
|
|
|
x[1] = 0;
|
|
|
|
rpt(printCbFunc,"%s",x);
|
2020-02-12 18:43:25 +00:00
|
|
|
if( incrFl )
|
2020-02-16 23:23:34 +00:00
|
|
|
++n;
|
|
|
|
|
2020-02-12 18:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s += s[0]+1;
|
|
|
|
n += 1;
|
|
|
|
|
|
|
|
if(*s)
|
|
|
|
{
|
2020-02-13 16:29:52 +00:00
|
|
|
rpt(printCbFunc,".");
|
2020-02-12 18:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
void dns_sd_print( printCallback_t printCbFunc, const void* buf, unsigned bufByteN )
|
2020-02-12 18:43:25 +00:00
|
|
|
{
|
2020-02-13 16:29:52 +00:00
|
|
|
(void)bufByteN;
|
2020-02-12 18:43:25 +00:00
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
const uint16_t* u = (uint16_t*)buf;
|
|
|
|
const unsigned char* b = (const unsigned char*)(u+6);
|
|
|
|
|
|
|
|
rpt(printCbFunc,"%s ", ntohs(u[1]) & 0x8000 ? "Response:" : "Question:");
|
2020-02-12 18:43:25 +00:00
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
int n = _print_name(printCbFunc,b,(const unsigned char*)buf);
|
|
|
|
|
|
|
|
rpt(printCbFunc," slen:%i ", n);
|
2020-02-12 18:43:25 +00:00
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
u = (uint16_t*)(b + n + 1); // advance past name
|
2020-02-12 18:43:25 +00:00
|
|
|
|
|
|
|
switch( ntohs(u[0]) )
|
|
|
|
{
|
2020-02-13 16:29:52 +00:00
|
|
|
case kA_DnsTId: rpt(printCbFunc,"A ");
|
2020-02-12 18:43:25 +00:00
|
|
|
break;
|
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
case kPTR_DnsTId: rpt(printCbFunc,"PTR ");
|
2020-02-12 18:43:25 +00:00
|
|
|
break;
|
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
case kTXT_DnsTId: rpt(printCbFunc,"TXT ");
|
2020-02-12 18:43:25 +00:00
|
|
|
break;
|
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
case kSRV_DnsTId: rpt(printCbFunc,"SRV ");
|
2020-02-12 18:43:25 +00:00
|
|
|
break;
|
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
case kAAAA_DnsTId:rpt(printCbFunc,"AAAA ");
|
2020-02-12 18:43:25 +00:00
|
|
|
break;
|
2020-02-13 16:29:52 +00:00
|
|
|
case kOPT_DnsTId: rpt(printCbFunc,"OPT ");
|
2020-02-12 18:43:25 +00:00
|
|
|
break;
|
2020-02-13 16:29:52 +00:00
|
|
|
case kNSEC_DnsTId:rpt(printCbFunc,"NSEC "); break;
|
|
|
|
case kANY_DnsTId: rpt(printCbFunc,"ANY "); break;
|
2020-02-12 18:43:25 +00:00
|
|
|
default:
|
2020-02-13 16:29:52 +00:00
|
|
|
rpt(printCbFunc,"<unk> 0x%2x",ntohs(u[0])); break;
|
2020-02-12 18:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ntohs(u[1]) & 0x80 )
|
2020-02-13 16:29:52 +00:00
|
|
|
rpt(printCbFunc,"flush ");
|
2020-02-12 18:43:25 +00:00
|
|
|
|
2020-02-13 16:29:52 +00:00
|
|
|
rpt(printCbFunc,"\n");
|
2020-02-12 18:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|