📄 data.c
字号:
/************************************************************************************************** $Id: data.c,v 1.35 2006/01/18 20:46:46 bboy Exp $ Copyright (C) 2002-2005 Don Moore <bboy@bboy.net> 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. 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. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA**************************************************************************************************/#include "named.h"/* Make this nonzero to enable debugging for this source file */#define DEBUG_DATA 1/************************************************************************************************** ipchs_to_ipuint32 add by zyl 20080408**************************************************************************************************/voidipchs_to_ipuint32(int *ip, char *chs){ char arr[4][4] = {0}; str_to_arr(arr,4,chs,"."); int a = 0; int b = 0; int c = 0; int d = 0; a = atoi(arr[0]); b = atoi(arr[1]); c = atoi(arr[2]); d = atoi(arr[3]); int aa = 0; int bb = 0; int cc = 0; int dd = 0; int ee = 0; aa = a << (8*3); bb = b << (8*2); cc = c << (8*1); dd = d; ee = aa + bb + cc + dd; *ip = ee;}/*--- ipchs_to_ipuint32() ------------------------------------------------------------------------*//************************************************************************************************** get_telecom_operator add by zyl 20080408**************************************************************************************************/int get_telecom_operator(int *telecom_operator, int ipint){ SQL_RES *res; SQL_ROW row; char query[100] = {0}; int querylen = 0; int val = 0; snprintf(query,sizeof(query),"select telecom_operator from ip_sections where %u >= ip_begin and %u <= ip_end;",ipint,ipint); querylen = strlen(query); if( !(res = sql_query(sql,query,querylen)) ) return(-1); if(row = sql_getrow(res)) val = atoi(row[0]); else fprintf(stderr,"no result.\n"); *telecom_operator = val; return 0;}/*--- get_telecom_operator() ---------------------------------------------------------------------*//************************************************************************************************** FIND_SOA Determine the origin in `fqdn' and return the MYDNS_SOA structure for that zone. If `label' is non-NULL, the label part of the fqdn will be copied there. If provided, `label' should be at least DNS_MAXNAMELEN bytes.**************************************************************************************************/MYDNS_SOA *find_soa( TASK *t, char *fqdn, /* The FQDN provided; return the SOA for the zone in this FQDN */ char *label /* The label part of `fqdn' that is below the origin will be stored here */){ MYDNS_SOA *soa = (MYDNS_SOA *)NULL; register size_t fqdnlen = strlen(fqdn); register char *origin, *end; int errflag;#if DEBUG_ENABLED && DEBUG_DATA Debug("%s: find_soa(\"%s\", \"%s\")", desctask(t), fqdn, label);#endif end = fqdn + fqdnlen; for (origin = fqdn; *origin && !soa; origin++) if (origin == fqdn || *origin == '.') { if (*origin == '.' && *(origin+1)) origin++; soa = zone_cache_find(t, 0, NULL, DNS_QTYPE_SOA, origin, end-origin, &errflag, NULL); if (errflag) { dnserror(t, DNS_RCODE_SERVFAIL, ERR_DB_ERROR); return (NULL); } /* Get label */ if (soa && label) { register int origin_len = strlen(soa->origin); register int len = strlen(fqdn) - origin_len - 1; if (origin_len == 1) len++; if (len < 0) len = 0; if (len > DNS_MAXNAMELEN) len = DNS_MAXNAMELEN; memcpy(label, fqdn, len); label[len] = '\0'; } }#if DEBUG_ENABLED && DEBUG_DATA if (soa) Debug("%s: Found SOA: id=%u origin=[%s] ns=[%s] mbox=[%s]", desctask(t), soa->id, soa->origin, soa->ns, soa->mbox);#endif return (soa);}/*--- find_soa() --------------------------------------------------------------------------------*//************************************************************************************************** FIND_RR Look up RR. Returns NULL if not found.**************************************************************************************************/MYDNS_RR *find_rr( TASK *t, MYDNS_SOA *soa, /* The SOA record for this zone */ dns_qtype_t type, /* The type of record to look for */ char *name /* The name to look for in `zone' */){ int errflag; MYDNS_RR *rr;#if DEBUG_ENABLED && DEBUG_DATA Debug("%s: find_rr(%u, \"%s\", %s, \"%s\")", desctask(t), soa->id, soa->origin, mydns_qtype_str(type), name);#endif rr = zone_cache_find(t, soa->id, soa->origin, type, name, strlen(name), &errflag, soa); if (errflag) { if (rr) mydns_rr_free(rr); dnserror(t, DNS_RCODE_SERVFAIL, ERR_DB_ERROR); return (NULL); } if (rr) { register MYDNS_RR *r; /* If the data is empty, reply with error */ for (r = rr; r; r = r->next) if (!r->data || !r->data[0]) { if (rr) mydns_rr_free(rr); dnserror(t, DNS_RCODE_SERVFAIL, ERR_NAME_FORMAT); return (NULL); } }#if DEBUG_ENABLED && DEBUG_DATA if (rr) Debug("%s: Found RR: id=%u name=[%s] type=%s data=[%s]", desctask(t), rr->id, rr->name, mydns_qtype_str(rr->type), rr->data);#endif return (rr);}/*--- find_rr() ---------------------------------------------------------------------------------*//************************************************************************************************** iplong_to_ipchs add by zyl 20080401 convert ip data from long type to chars**************************************************************************************************/voidiplong_to_ipchs(char *chs, int len,int ip_long){ int ip; int ip_tmp = ip_long; char ch[4]; memset(ch,0,4); if(len < 16){ fprintf(stderr,"chs's length shouldn't be less than 16.\n"); return; } unsigned int i = 0; memset(chs,0,len); while(i < 4){ ip = ip_tmp & 0xff; memset(ch,0,4); if(i < 3) sprintf(ch,"%d.",ip); else sprintf(ch,"%d",ip); strncat(chs,ch,strlen(ch)); ip_tmp = ip_tmp >> 8; i++; }}/*--- iplong_to_ipchs() ---------------------------------------------------------------------------------*//**************************************************************************************************split a string into a array**************************************************************************************************/voidstr_to_arr(char (*arr)[4], int m, char *chs, char *sp){ static char* sret[4]; char *pch = NULL; int i = 0; pch = strtok(chs, sp); while (pch != NULL) { sret[i] = pch; pch = strtok(NULL, sp); i++; } m = i; int j = 0; for(j=0; j<m; j++) { strncpy(arr[j], sret[j],4); }}/*--- str_to_arr() ---------------------------------------------------------------------------------*//* vi:set ts=3: *//* NEED_PO */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -