libcw/dns_sd/dns_sd_print.cpp

104 lines
2.1 KiB
C++
Raw Normal View History

2020-02-12 18:43:25 +00:00
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#ifdef cwLINUX
2020-02-12 18:43:25 +00:00
#include <arpa/inet.h>
#endif
#ifdef ARDUINO
#include <Ethernet.h>
#include <utility/w5100.h>
#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"
int _print_name( printCallback_t printCbFunc, const unsigned char* s, const unsigned char* buf )
2020-02-12 18:43:25 +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)
{
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 )
++n;
2020-02-12 18:43:25 +00:00
}
s += s[0]+1;
n += 1;
if(*s)
{
rpt(printCbFunc,".");
2020-02-12 18:43:25 +00:00
}
}
}
return n;
}
void dns_sd_print( printCallback_t printCbFunc, const void* buf, unsigned bufByteN )
2020-02-12 18:43:25 +00:00
{
(void)bufByteN;
2020-02-12 18:43:25 +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
int n = _print_name(printCbFunc,b,(const unsigned char*)buf);
rpt(printCbFunc," slen:%i ", n);
2020-02-12 18:43:25 +00:00
u = (uint16_t*)(b + n + 1); // advance past name
2020-02-12 18:43:25 +00:00
switch( ntohs(u[0]) )
{
case kA_DnsTId: rpt(printCbFunc,"A ");
2020-02-12 18:43:25 +00:00
break;
case kPTR_DnsTId: rpt(printCbFunc,"PTR ");
2020-02-12 18:43:25 +00:00
break;
case kTXT_DnsTId: rpt(printCbFunc,"TXT ");
2020-02-12 18:43:25 +00:00
break;
case kSRV_DnsTId: rpt(printCbFunc,"SRV ");
2020-02-12 18:43:25 +00:00
break;
case kAAAA_DnsTId:rpt(printCbFunc,"AAAA ");
2020-02-12 18:43:25 +00:00
break;
case kOPT_DnsTId: rpt(printCbFunc,"OPT ");
2020-02-12 18:43:25 +00:00
break;
case kNSEC_DnsTId:rpt(printCbFunc,"NSEC "); break;
case kANY_DnsTId: rpt(printCbFunc,"ANY "); break;
2020-02-12 18:43:25 +00:00
default:
rpt(printCbFunc,"<unk> 0x%2x",ntohs(u[0])); break;
2020-02-12 18:43:25 +00:00
}
if( ntohs(u[1]) & 0x80 )
rpt(printCbFunc,"flush ");
2020-02-12 18:43:25 +00:00
rpt(printCbFunc,"\n");
2020-02-12 18:43:25 +00:00
}