⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lookup.c

📁 This a good VPN source
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * DNS KEY lookup helper * Copyright (C) 2002 Michael Richardson <mcr@freeswan.org> *  * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * for more details. */char lookup_c_version[] = "@(#) RCSID $Id: lookup.c,v 1.8 2004/12/02 06:16:19 mcr Exp $";#include <stdio.h>#include <string.h>#include <stdlib.h> #include <unistd.h> #include <errno.h>#include <getopt.h>#include <setjmp.h>#include <ctype.h>#include <signal.h>#include <time.h>#include <isc/mem.h>#include <isc/buffer.h>#include <dns/rdata.h>#include <dns/rdatastruct.h>#include <dns/name.h>#include <lwres/netdb.h>#include <lwres/async.h>#include "lwdnsq.h"static int lwresd_has_spoken = 0;char *xstrdup(const char *s){	char *n;	n = strdup(s);	if(n == NULL) {		abort();	}	return n;}static void free_dl(dnskey_glob *gs, dnskey_lookup *dl){	dnskey_lookup **walk;	walk = &gs->dns_outstanding;	while(*walk!=NULL && *walk != dl)	{		walk = &((*walk)->next);	}	if(*walk != NULL)	{		/* if we exit with it non-null, then we		 * found a matching location, remove		 * it.		 */		*walk = dl->next;		dl->next = NULL;	}	gs->dns_inflight--;	if(dl->tracking_id) {		free(dl->tracking_id);		dl->tracking_id = NULL;	}	if(dl->wantedtype_name) {		free(dl->wantedtype_name);		dl->wantedtype_name = NULL;	}	if(dl->fqdn) {		free(dl->fqdn);		dl->fqdn = NULL;	}#if 0	if(dl->last_cname_used) {		dns_name_free(&dl->last_cname, gs->iscmem);	}#endif		free(dl);}void lookup_thing(dnskey_glob *gs,		  dns_rdatatype_t wantedtype,		  const char *wantedtype_name,		  char *id,		  char *fqdn){	isc_mem_t    *iscmem;	isc_buffer_t *iscbuf;	int success;	dnskey_lookup *dl;	extern void lwres_sanitize_list(lwres_context_t *ctx);	iscmem=NULL;	iscbuf=NULL;	dl = malloc(sizeof(*dl));	memset(dl, 0, sizeof(*dl));	dl->tracking_id = strdup(id);	dl->step = dkl_start;	output_transaction_line(gs, id, 0, "START", NULL);	success = lwres_getrrsetbyname_init(fqdn, dns_rdataclass_in,					    wantedtype, 0 /*flags*/,					    gs->lwctx, &dl->las);	if(success != ERRSET_SUCCESS) {		/* screwed: */		output_transaction_line(gs, id, 0, "FATAL", "isc buffer error");		return;	}	lwres_sanitize_list(gs->lwctx);	lwres_getrrsetbyname_xmit(gs->lwctx, &dl->las);	lwres_sanitize_list(gs->lwctx);	dl->step = dkl_first;	dl->wantedtype = wantedtype;	dl->wantedtype_name = xstrdup(wantedtype_name);	dl->fqdn = xstrdup(fqdn);	dl->tracking_id = xstrdup(id);	time(&dl->resend_time);	dl->resend_time += LWDNSQ_RETRANSMIT_INTERVAL;	dl->retry_count = LWDNSQ_RETRANSMIT_INTERVAL;	/* link it in */	dl->next = gs->dns_outstanding;	gs->dns_outstanding = dl;		gs->dns_inflight++;	return;}static int setup_follow_possible_cname(dnskey_glob   *gs,				       dnskey_lookup *dl){	int ret;	dl->cname_count++;	/*	 * If we are on an odd cycle (starting with 1),	 * then convert to dns_name_t so that we can compare later.	 *	 * This detects loops in the CNAME processing, while still	 * allowing an arbitrary number of CNAMEs to be followed. 	 */	if(dl->cname_count & 1) 	{		isc_buffer_t fqdn_src;		isc_buffer_t *fqdn_dst;		if(dl->cname_count == 1)		{			memset(&dl->last_cname, 0, sizeof(dl->last_cname));			dns_name_init(&dl->last_cname, NULL);		}		else		{			dns_name_reset(&dl->last_cname);		}		fqdn_dst=NULL;		isc_buffer_init(&fqdn_src, dl->fqdn, strlen(dl->fqdn));		isc_buffer_add(&fqdn_src, strlen(dl->fqdn));		isc_buffer_allocate(gs->iscmem, &fqdn_dst, strlen(dl->fqdn)+1);#if 0		if(dl->last_cname_used) {			dns_name_free(&dl->last_cname, gs->iscmem);		}#endif		dl->last_cname_used = 1;		if(dns_name_fromtext(&dl->last_cname,				     &fqdn_src,				     NULL,				     1,				     fqdn_dst) != ISC_R_SUCCESS) {			return 0;		}		/* something else here ? */	}				 	ret = lwres_getrrsetbyname_init(dl->fqdn, dns_rdataclass_in,					dns_rdatatype_cname, 0 /*flags*/,					gs->lwctx,					&dl->las);	if(ret != ERRSET_SUCCESS) {		return 0;	}	lwres_getrrsetbyname_xmit(gs->lwctx, &dl->las);	return 1;}/* * we asked for, and got a CNAME of some kind. */static void process_step_cname(dnskey_glob *gs,			       dnskey_lookup *dl,			       struct rrsetinfo *ans,			       int success){	struct rdatainfo *ri;	isc_region_t  region;	dns_rdata_t   rd;	dns_rdata_cname_t cn;	char simplebuf[80];	isc_buffer_t *cname_text;	char cname_buf[DNS_NAME_MAXTEXT];	/* char cname_buf2[DNS_NAME_MAXTEXT]; */	switch(success) {	case ERRSET_NONAME:	case ERRSET_NODATA:		/* no, no CNAME found, thing isn't there */		snprintf(simplebuf, sizeof(simplebuf),			 "RR of type %s for %s was not found (tried CNAMEs)",			 dl->wantedtype_name,			 dl->fqdn);		output_transaction_line(gs, dl->tracking_id, 0, "RETRY", 					simplebuf);		dl->step = dkl_done;		return;			case 0:		/* aha! found a CNAME */		break;	default:	fatal:		/* some other error */		snprintf(simplebuf, sizeof(simplebuf), "err=%d", success);		output_transaction_line(gs, dl->tracking_id, 0, "FATAL", simplebuf);		dl->step = dkl_done;		return;	}	/*	 * now process out the CNAMEs, and look them up, one by one...	 * there should be only one... We just use the first one that works.	 */	if(ans->rri_flags & RRSET_VALIDATED) {		output_transaction_line(gs, dl->tracking_id, 0, "DNSSEC", "OKAY");	} else {		output_transaction_line(gs, dl->tracking_id, 0, "DNSSEC", "not present");	}	if(ans->rri_nrdatas != 1) {		/* we got a number of CNAMEs different from 1! */		success=0;		snprintf(simplebuf, sizeof(simplebuf), "illegal number of CNAMES: %d", ans->rri_nrdatas);		output_transaction_line(gs, dl->tracking_id, 0, "FATAL", simplebuf);		dl->step = dkl_done;		return;	}	/* process first CNAME record */	ri= &ans->rri_rdatas[0];	memset(&region, 0, sizeof(region));	memset(&rd,     0, sizeof(rd));		region.base   =  ri->rdi_data;	region.length =  ri->rdi_length;	dns_rdata_fromregion(&rd, dns_rdataclass_in,			     dns_rdatatype_cname, &region);		/* we set mctx to NULL, which means that the tenure for	 * the stuff pointed to by cn will persist only as long	 * as rd persists.	 */	if(dns_rdata_tostruct(&rd, &cn, NULL) != ISC_R_SUCCESS) {		/* failed, try next return error */		success=0;		goto fatal;	}	cname_text=NULL;	if(isc_buffer_allocate(gs->iscmem, &cname_text, DNS_NAME_MAXTEXT)) {		success=0;		goto fatal;	}	if(dns_name_totext(&cn.cname, ISC_TRUE, cname_text) !=	   ISC_R_SUCCESS) {		success=0;		goto fatal;	}		cname_buf[0]='\0';	strncat(cname_buf,		isc_buffer_base(cname_text),		isc_buffer_usedlength(cname_text));	/* free up buffer */	isc_buffer_free(&cname_text);		{		/* add a trailing . */		char *end;		end = &cname_buf[strlen(cname_buf)];		if(*end != '.') {			strncat(cname_buf, ".", sizeof(cname_buf));		}	}		/* format out a text version */	output_transaction_line(gs, dl->tracking_id, 0, "CNAME", cname_buf);	output_transaction_line(gs, dl->tracking_id, 0, "CNAMEFROM", dl->fqdn);		/* check for loops in the CNAMEs! */	if(dns_name_equal(&dl->last_cname, &cn.cname) == ISC_TRUE) {		/* damn, we found a loop! */		dl->step = dkl_done;		return;	}	/* send new request. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -