42#pragma comment(lib, "dnsapi.lib")
50} SPF_dns_windns_config_t;
53#define SPF_h_errno WSAGetLastError()
56static inline SPF_dns_windns_config_t *SPF_voidp2spfhook(
void *hook )
57 {
return (SPF_dns_windns_config_t *)hook; }
58static inline void *SPF_spfhook2voidp( SPF_dns_windns_config_t *spfhook )
59 {
return (
void *)spfhook; }
62LPSTR SPF_dns_create_error_message_windns(DWORD last_error)
67 (FORMAT_MESSAGE_ALLOCATE_BUFFER |
68 FORMAT_MESSAGE_FROM_SYSTEM |
69 FORMAT_MESSAGE_IGNORE_INSERTS),
72 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
73 (LPSTR) &error_message,
84void SPF_dns_destroy_error_message_windns(LPSTR error_message)
86 LocalFree( error_message );
90size_t SPF_dns_txt_get_length_windns(DWORD count, PSTR strings[])
97 for( i = 0; i < count; i++ )
99 length = length + strlen(strings[i]);
106char *SPF_dns_txt_concat_windns(
char *buffer, DWORD count, PSTR strings[])
112 for( i = 0; i < count; i++ )
114 if ( strcat( buffer, strings[i] ) ==
NULL )
122static SPF_dns_rr_t *SPF_dns_lookup_windns( SPF_dns_config_t spfdcid,
const char *domain,
ns_type rr_type,
int should_cache )
124 SPF_dns_iconfig_t *spfdic = SPF_dcid2spfdic( spfdcid );
125 SPF_dns_windns_config_t *spfhook = SPF_voidp2spfhook( spfdic->hook );
130 PDNS_RECORDA pDnsRecord;
135 char ip4_buf[ INET_ADDRSTRLEN ];
136 char ip6_buf[ INET6_ADDRSTRLEN ];
141 DNS_AAAA_DATA *pAAAA_data;
142 DNS_MX_DATAA *pMX_data;
143 DNS_TXT_DATAA *pTXT_data;
144 DNS_PTR_DATAA *pPTR_data;
153 spfrr = &spfhook->spfrr;
154 SPF_dns_reset_rr( spfrr );
157 if ( domain && domain[0] !=
'\0' )
160 size_t new_len = strlen( domain ) + 1;
164 new_domain = realloc( spfrr->
domain, new_len );
165 if ( new_domain ==
NULL )
168 spfrr->
domain = new_domain;
171 strcpy( spfrr->
domain, domain );
178 if ( spfhook->debug )
182 (rr_type ==
ns_t_a) ?
"A" :
196 status = DnsQuery_A( domain, rr_type,
197 (DNS_QUERY_STANDARD + DNS_QUERY_TREAT_AS_FQDN),
200 if ( status != DNS_RCODE_NOERROR )
202 if ( spfhook->debug )
204 error_message = SPF_dns_create_error_message_windns(
SPF_h_errno);
209 SPF_dns_destroy_error_message_windns(error_message);
214 ( spfdic->layer_below )
216 return SPF_dcid2spfdic( spfdic->layer_below )->lookup( spfdic->layer_below, domain, rr_type, should_cache );
226 rdlen = pDnsRecord->wDataLength;
228 if ( spfhook->debug > 1 )
229 SPF_debugf(
"name: %s type: %d ttl: %d rdlen: %d",
230 pDnsRecord->pName, pDnsRecord->wType,
231 pDnsRecord->dwTtl, rdlen );
235 pDnsRecord = pDnsRecord->pNext;
242 pTXT_data = &pDnsRecord->Data.TXT;
245 SPF_dns_txt_get_length_windns(
246 pTXT_data->dwStringCount,
247 pTXT_data->pStringArray
251 if ( spfhook->debug > 1 )
253 switch( pDnsRecord->wType )
257 pA_data = &pDnsRecord->Data.A;
260 inet_ntop( AF_INET, &pA_data->IpAddress,
261 ip4_buf,
sizeof( ip4_buf ) ));
266 pAAAA_data = &pDnsRecord->Data.AAAA;
269 inet_ntop( AF_INET6, &pAAAA_data->Ip6Address,
270 ip6_buf,
sizeof( ip6_buf ) ));
275 SPF_debugf(
"NS: %s", pDnsRecord->Data.NS.pNameHost );
280 SPF_debugf(
"CNAME: %s", pDnsRecord->Data.CNAME.pNameHost );
285 pMX_data = &pDnsRecord->Data.MX;
288 pMX_data->wPreference, pMX_data->pNameExchange );
293 txt_concat = malloc(txt_data_len + 1);
295 if ( txt_concat ==
NULL )
296 SPF_debugf(
"TXT: (%d) - no memory for concatination",
300 if ( SPF_dns_txt_concat_windns(
302 pTXT_data->dwStringCount,
303 pTXT_data->pStringArray
305 SPF_debugf(
"TXT: (%d) - error in concatination",
310 txt_data_len, txt_concat );
318 pPTR_data = &pDnsRecord->Data.PTR;
320 SPF_debugf(
"PTR: %s", pPTR_data->pNameHost );
324 SPF_debugf(
"not parsed: type: %d", pDnsRecord->wType );
330 ( pDnsRecord->Flags.S.Section != DNSREC_ANSWER ) &&
331 ( spfhook->debug > 1 )
334 pDnsRecord = pDnsRecord->pNext;
340 ( pDnsRecord->wType != spfrr->
rr_type ) &&
344 SPF_debugf(
"unexpected rr type: %d expected: %d",
345 pDnsRecord->wType, rr_type );
346 pDnsRecord = pDnsRecord->pNext;
350 switch( pDnsRecord->wType )
354 pA_data = &pDnsRecord->Data.A;
356 if ( SPF_dns_rr_buf_malloc(
357 spfrr, cnt,
sizeof( pA_data->IpAddress )
361 memmove( &spfrr->
rr[cnt]->
a, &pA_data->IpAddress,
362 sizeof( pA_data->IpAddress ) );
369 pAAAA_data = &pDnsRecord->Data.AAAA;
371 if ( SPF_dns_rr_buf_malloc(
372 spfrr, cnt,
sizeof( pAAAA_data->Ip6Address )
376 memmove( &spfrr->
rr[cnt]->
aaaa, &pAAAA_data->Ip6Address,
377 sizeof( pAAAA_data->Ip6Address ) );
391 pMX_data = &pDnsRecord->Data.MX;
393 if ( SPF_dns_rr_buf_malloc(
394 spfrr, cnt, strlen( pMX_data->pNameExchange ) + 1
398 strcpy( spfrr->
rr[cnt]->
mx, pMX_data->pNameExchange );
405 if ( SPF_dns_rr_buf_malloc(
406 spfrr, cnt, txt_data_len + 1
410 if ( SPF_dns_txt_concat_windns(
412 pTXT_data->dwStringCount,
413 pTXT_data->pStringArray
422 pPTR_data = &pDnsRecord->Data.PTR;
424 if ( SPF_dns_rr_buf_malloc(
425 spfrr, cnt, strlen( pPTR_data->pNameHost ) + 1
429 strcpy( spfrr->
rr[cnt]->
ptr, pPTR_data->pNameHost );
440 pDnsRecord = pDnsRecord->pNext;
444 spfhook->spfrr.herrno =
NO_DATA;
452 SPF_dns_iconfig_t *spfdic;
453 SPF_dns_windns_config_t *spfhook;
456 spfdic = malloc(
sizeof( *spfdic ) );
457 if ( spfdic ==
NULL )
460 spfdic->hook = calloc( 1,
sizeof( SPF_dns_windns_config_t ) );
461 if ( spfdic->hook ==
NULL )
468 spfdic->lookup = SPF_dns_lookup_windns;
469 spfdic->get_spf =
NULL;
470 spfdic->get_exp =
NULL;
471 spfdic->add_cache =
NULL;
472 spfdic->layer_below = layer_below;
473 spfdic->name =
"windns";
475 spfhook = SPF_voidp2spfhook( spfdic->hook );
477 spfhook->debug =
debug;
478 SPF_dns_reset_rr( &spfhook->spfrr );
479 spfhook->spfrr.source = SPF_spfdic2dcid( spfdic );
481 return SPF_spfdic2dcid( spfdic );
486 SPF_dns_iconfig_t *spfdic = SPF_dcid2spfdic( spfdcid );
489 if ( spfdcid ==
NULL )
493 SPF_dns_reset_rr( &(SPF_voidp2spfhook( spfdic->hook )->spfrr) );
498 SPF_dns_iconfig_t *spfdic = SPF_dcid2spfdic( spfdcid );
500 if ( spfdcid ==
NULL )
505 SPF_dns_windns_config_t *spfhook = SPF_voidp2spfhook( spfdic->hook );
507 SPF_dns_destroy_rr_var( &spfhook->spfrr );
509 free( spfdic->hook );
void SPF_dns_reset_config_windns(SPF_dns_config_t spfdcid)
SPF_dns_config_t SPF_dns_create_config_windns(SPF_dns_config_t layer_below, int debug)
void SPF_dns_destroy_config_windns(SPF_dns_config_t spfdcid)
#define SPF_error(errmsg)