comparison src/dnsbl.cpp @ 385:be7355b47051

start parsing spf txt records
author Carl Byington <carl@five-ten-sg.com>
date Mon, 06 Mar 2017 14:30:41 -0800
parents 7b7066a51c33
children e27e22f6a49a
comparison
equal deleted inserted replaced
384:7b7066a51c33 385:be7355b47051
335 335
336 glommer glom; 336 glommer glom;
337 char *buf = (char *)&glom; 337 char *buf = (char *)&glom;
338 priv.my_read(buf, sizeof(glom.length)); 338 priv.my_read(buf, sizeof(glom.length));
339 buf += sizeof(glom.length); 339 buf += sizeof(glom.length);
340 #ifdef RESOLVER_DEBUG 340 //#ifdef RESOLVER_DEBUG
341 char text[1000]; 341 char text[1000];
342 snprintf(text, sizeof(text), "dns_interface() wrote question %s and has answer length %d", question, glom.length); 342 snprintf(text, sizeof(text), "dns_interface() wrote question %s and has answer length %d", question, glom.length);
343 my_syslog(text); 343 my_syslog(text);
344 #endif 344 //#endif
345 if (glom.length == 0) return 0; 345 if (glom.length == 0) return 0;
346 if (glom.length > sizeof(glom.answer)) { 346 if (glom.length > sizeof(glom.answer)) {
347 priv.err = true; 347 priv.err = true;
348 return 0; // cannot process overlarge answers 348 return 0; // cannot process overlarge answers
349 } 349 }
420 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) { 420 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) {
421 if (ns_rr_type(rr) == qtype) { 421 if (ns_rr_type(rr) == qtype) {
422 size_t offset = 0; 422 size_t offset = 0;
423 size_t rdlen = ns_rr_rdlen(rr); 423 size_t rdlen = ns_rr_rdlen(rr);
424 const unsigned char *rdata = ns_rr_rdata(rr); 424 const unsigned char *rdata = ns_rr_rdata(rr);
425 //#ifdef RESOLVER_DEBUG
426 char text[1000];
427 snprintf(text, sizeof(text), "found txt record rdlen = %d", rdlen);
428 my_syslog(text);
429 //#endif
425 while ((offset < txt_size) && rdlen) { 430 while ((offset < txt_size) && rdlen) {
426 size_t slen = size_t(*(rdata++)); 431 size_t slen = size_t(*(rdata++));
427 rdlen--; 432 rdlen--;
428 size_t m = min(slen, rdlen); 433 size_t m = min(slen, rdlen);
429 m = min(m, txt_size-offset); 434 m = min(m, txt_size-offset);
431 offset += m; 436 offset += m;
432 rdata += m; 437 rdata += m;
433 rdlen -= m; 438 rdlen -= m;
434 } 439 }
435 txt_answer[offset] = '\0'; // trailing null 440 txt_answer[offset] = '\0'; // trailing null
441 //#ifdef RESOLVER_DEBUG
442 snprintf(text, sizeof(text), "found txt record %s", txt_answer);
443 my_syslog(text);
444 //#endif
436 if (strncasecmp(txt_answer, "v=spf1 ", 7) == 0) break; 445 if (strncasecmp(txt_answer, "v=spf1 ", 7) == 0) break;
437 } 446 }
438 } 447 }
439 //if (strncasecmp(txt_answer, "v=spf1 ", 7) != 0) { 448 //if (strncasecmp(txt_answer, "v=spf1 ", 7) != 0) {
440 // txt_answer[0] = '\0'; // return null string if there are no spf1 txt answers 449 // txt_answer[0] = '\0'; // return null string if there are no spf1 txt answers
876 } 885 }
877 question[rs-1] = '\0'; // ensure null termination 886 question[rs-1] = '\0'; // ensure null termination
878 887
879 // find the answer 888 // find the answer
880 #ifdef NS_PACKETSZ 889 #ifdef NS_PACKETSZ
881 #ifdef RESOLVER_DEBUG 890 //#ifdef RESOLVER_DEBUG
882 char text[1000]; 891 char text[1000];
883 snprintf(text, sizeof(text), "process_resolver_requests() has a question %s qtype %d", question+1, int8_t(question[0])); 892 snprintf(text, sizeof(text), "process_resolver_requests() has a question %s qtype %d", question+1, int8_t(question[0]));
884 my_syslog(text); 893 my_syslog(text);
885 #endif 894 //#endif
886 int res_result = res_search(question+1, ns_c_in, int8_t(question[0]), glom.answer, sizeof(glom.answer)); 895 int res_result = res_search(question+1, ns_c_in, int8_t(question[0]), glom.answer, sizeof(glom.answer));
887 if (res_result < 0) glom.length = 0; // represent all errors as zero length answers 896 if (res_result < 0) glom.length = 0; // represent all errors as zero length answers
888 else glom.length = (size_t)res_result; 897 else glom.length = (size_t)res_result;
889 #else 898 #else
890 glom.length = sizeof(glom.answer); 899 glom.length = sizeof(glom.answer);