📄 rr.c~
字号:
/************************************************************************************************** $Id: rr.c,v 1.65 2005/04/29 16:10:27 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 "mydns.h"char mydns_rr_table_name[PATH_MAX] = MYDNS_RR_TABLE;char *mydns_rr_where_clause = NULL;/* Optional columns */int mydns_rr_use_active = 0;int mydns_rr_use_stamp = 0;/* Make this nonzero to enable debugging within this source file */#define DEBUG_LIB_RR 1/************************************************************************************************** MYDNS_RR_COUNT Returns the number of records in the rr table.**************************************************************************************************/longmydns_rr_count(SQL *sqlConn){ return sql_count(sqlConn, "SELECT COUNT(*) FROM %s", mydns_rr_table_name);}/*--- mydns_rr_count() --------------------------------------------------------------------------*//************************************************************************************************** MYDNS_SET_RR_TABLE_NAME**************************************************************************************************/voidmydns_set_rr_table_name(char *name){ if (!name) strncpy(mydns_rr_table_name, MYDNS_RR_TABLE, sizeof(mydns_rr_table_name)-1); else strncpy(mydns_rr_table_name, name, sizeof(mydns_rr_table_name)-1);}/*--- mydns_set_rr_table_name() -----------------------------------------------------------------*//************************************************************************************************** MYDNS_SET_RR_WHERE_CLAUSE**************************************************************************************************/voidmydns_set_rr_where_clause(char *where){ if (where && strlen(where)) { if (!(mydns_rr_where_clause = strdup(where))) Errx("out of memory"); }}/*--- mydns_set_rr_where_clause() ---------------------------------------------------------------*//************************************************************************************************** MYDNS_RR_GET_TYPE**************************************************************************************************/inline dns_qtype_tmydns_rr_get_type(char *type){ register char *c; for (c = type; *c; c++) *c = toupper(*c); switch (type[0]) { case 'A': if (!type[1]) return DNS_QTYPE_A; if (type[1] == 'A' && type[2] == 'A' && type[3] == 'A' && !type[4]) return DNS_QTYPE_AAAA;#if ALIAS_ENABLED if (type[1] == 'L' && type[2] == 'I' && type[3] == 'A' && type[4] == 'S' && !type[5]) return DNS_QTYPE_ALIAS;#endif break; case 'C': if (type[1] == 'N' && type[2] == 'A' && type[3] == 'M' && type[4] == 'E' && !type[5]) return DNS_QTYPE_CNAME; break; case 'H': if (type[1] == 'I' && type[2] == 'N' && type[3] == 'F' && type[4] == 'O' && !type[5]) return DNS_QTYPE_HINFO; break; case 'M': if (type[1] == 'X' && !type[2]) return DNS_QTYPE_MX; break; case 'N': if (type[1] == 'S' && !type[2]) return DNS_QTYPE_NS; if (type[1] == 'A' && type[2] == 'P' && type[3] == 'T' && type[4] == 'R' && !type[5]) return DNS_QTYPE_NAPTR; break; case 'T': if (type[1] == 'X' && type[2] == 'T' && !type[3]) return DNS_QTYPE_TXT; break; case 'P': if (type[1] == 'T' && type[2] == 'R' && !type[3]) return DNS_QTYPE_PTR; break; case 'R': if (type[1] == 'P' && !type[2]) return DNS_QTYPE_RP; break; case 'S': if (type[1] == 'R' && type[2] == 'V' && !type[3]) return DNS_QTYPE_SRV; break; } return 0;}/*--- mydns_rr_get_type() -----------------------------------------------------------------------*//************************************************************************************************** MYDNS_RR_PARSE_RP RP contains two names in 'data' -- the mbox and the txt. NUL-terminate mbox and fill 'rp_txt' with the txt part of the record.**************************************************************************************************/static inline voidmydns_rr_parse_rp(SQL_ROW row, const char *origin, MYDNS_RR *rr){ char *c; /* If no space, set txt to '.' */ if (!(c = strchr(rr->data, ' '))) { rr->rp_txt[0] = '.'; rr->rp_txt[1] = '\0'; } else { strncpy(rr->rp_txt, c+1, sizeof(rr->rp_txt)-1); *c = '\0'; /* Append origin to rp_txt if necessary */ { int namelen = strlen(rr->rp_txt); if (namelen && rr->rp_txt[namelen-1] != '.') { strncat(rr->rp_txt, ".", sizeof(rr->rp_txt) - namelen - 1); strncat(rr->rp_txt, origin, sizeof(rr->rp_txt) - namelen - 2); } } }}/*--- mydns_rr_parse_rp() -----------------------------------------------------------------------*//************************************************************************************************** MYDNS_RR_PARSE_SRV SRV records contain two unsigned 16-bit integers in the "data" field before the target, 'srv_weight' and 'srv_port' - parse them and make "data" contain only the target. Also, make sure 'aux' fits into 16 bits, clamping values above 65535.**************************************************************************************************/static inline voidmydns_rr_parse_srv(SQL_ROW row, const char *origin, MYDNS_RR *rr){ char *weight, *port, *target; /* Clamp 'aux' if necessary */ if (rr->aux > 65535) rr->aux = 65535; /* Parse weight (into srv_weight), port (into srv_port), and target */ target = rr->data; if ((weight = strsep(&target, " \t"))) { rr->srv_weight = atoi(weight); if ((port = strsep(&target, " \t"))) rr->srv_port = atoi(port); memmove(rr->data, target, strlen(target)+1); }}/*--- mydns_rr_parse_srv() ----------------------------------------------------------------------*//************************************************************************************************** MYDNS_RR_PARSE_NAPTR Returns 0 on success, -1 on error.**************************************************************************************************/static inline intmydns_rr_parse_naptr(SQL_ROW row, const char *origin, MYDNS_RR *rr){ char int_tmp[12], data_copy[DNS_MAXNAMELEN * 2 + 2], *p; strncpy(data_copy, rr->data, sizeof(data_copy) - 1); p = data_copy; if (!strsep_quotes(&p, int_tmp, sizeof(int_tmp))) return (-1); rr->naptr_order = atoi(int_tmp); if (!strsep_quotes(&p, int_tmp, sizeof(int_tmp))) return (-1); rr->naptr_pref = atoi(int_tmp); if (!strsep_quotes(&p, rr->naptr_flags, sizeof(rr->naptr_flags))) return (-1); if (!strsep_quotes(&p, rr->naptr_service, sizeof(rr->naptr_service))) return (-1); if (!strsep_quotes(&p, rr->naptr_regex, sizeof(rr->naptr_regex))) return (-1); if (!strsep_quotes(&p, rr->naptr_replacement, sizeof(rr->naptr_replacement))) return (-1); return 0;}/*--- mydns_rr_parse_naptr() --------------------------------------------------------------------*//************************************************************************************************** MYDNS_RR_PARSE Given the SQL results with RR data, populates and returns a matching MYDNS_RR structure. Returns NULL on error.**************************************************************************************************/inline MYDNS_RR *mydns_rr_parse(SQL_ROW row, const char *origin){ MYDNS_RR *rr; if ((rr = (MYDNS_RR *)calloc(1, sizeof(MYDNS_RR)))) { rr->next = NULL; rr->id = atou(row[0]); rr->zone = atou(row[1]); strncpy(rr->name, row[2], sizeof(rr->name)-1); strncpy(rr->data, row[3], sizeof(rr->data)-1); rr->class = DNS_CLASS_IN; rr->aux = atou(row[4]); rr->ttl = atou(row[5]); if (!(rr->type = mydns_rr_get_type(row[6]))) { /* Ignore unknown RR type(s) */ free(rr); return (NULL); }#if ALIAS_ENABLED if (rr->type == DNS_QTYPE_ALIAS) { rr->type = DNS_QTYPE_A; rr->alias = 1; } else rr->alias = 0;#endif /* Populate special fields for RP records */ if (rr->type == DNS_QTYPE_RP) mydns_rr_parse_rp(row, origin, rr); /* Populate special fields for NAPTR records */ if (rr->type == DNS_QTYPE_NAPTR) { if (mydns_rr_parse_naptr(row, origin, rr) < 0) { free(rr); return (NULL); } } /* Append origin to data if it's not there for these types: */ if (origin) switch (rr->type) { case DNS_QTYPE_CNAME: case DNS_QTYPE_MX: case DNS_QTYPE_NS:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -