Back to index

courier  0.68.2
rfc1035str.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "rfc1035.h"
00007 #include      <string.h>
00008 
00009 
00010 static struct { const char *name; int num; } typetab[]={
00011        {"A",         1},
00012        {"NS",        2},
00013        {"MD",        3},
00014        {"MF",        4},
00015        {"CNAME",     5},
00016        {"SOA",              6},
00017        {"MB",        7},
00018        {"MG",        8},
00019        {"MR",        9},
00020        {"NULL",      10},
00021        {"WKS",              11},
00022        {"PTR",              12},
00023        {"HINFO",     13},
00024        {"MINFO",     14},
00025        {"MX",        15},
00026        {"TXT",              16},
00027        {"AAAA",      28},
00028        {"RRSIG",     46},
00029        {"AXFR",      252},
00030        {"MAILB",     253},
00031        {"MAILA",     254},
00032        {"ANY",              255}},
00033 
00034        classtab[]={
00035 
00036        {"IN",        1},
00037        {"CSNET",     2},
00038        {"CHAOS",     3},
00039        {"HESIOD",    4},
00040        {"ANY",              255}},
00041 
00042        opcodetab[]={
00043        {"QUERY",     0},
00044        {"IQUERY",    1},
00045        {"STATUS",    2}},
00046 
00047        rcodetab[]={
00048        {"NOERROR",   0},
00049        {"FORMAT",    1},
00050        {"SERVFAIL",  2},
00051        {"NXDOMAIN",  3},
00052        {"UNIMPLEMENTED", 4},
00053        {"REFUSED",   5}};
00054 
00055 #if HAVE_STRCASECMP
00056 #define       COMPARE(a,b)  strcasecmp((a), (b))
00057 #else
00058 #define       COMPARE(a,b)  stricmp((a), (b))
00059 #endif
00060 
00061 void rfc1035_type_itostr(int n, void (*func)(const char *, void *), void *arg)
00062 {
00063        unsigned i;
00064        char buf[30];
00065 
00066        for (i=0; i<sizeof(typetab)/sizeof(typetab[0]); i++)
00067               if (typetab[i].num == n)
00068               {
00069                      (*func)(typetab[i].name, arg);
00070                      return;
00071               }
00072 
00073        snprintf(buf, sizeof(buf), "TYPE%d", n);
00074 
00075        buf[sizeof(buf)-1]=0;
00076 
00077        (*func)(buf, arg);
00078 }
00079 
00080 int rfc1035_type_strtoi(const char *n)
00081 {
00082 unsigned i;
00083 
00084        for (i=0; i<sizeof(typetab)/sizeof(typetab[0]); i++)
00085               if (COMPARE(typetab[i].name, n) == 0) return (typetab[i].num);
00086        return (-1);
00087 }
00088 
00089 const char *rfc1035_class_itostr(int n)
00090 {
00091 unsigned i;
00092 
00093        for (i=0; i<sizeof(classtab)/sizeof(classtab[0]); i++)
00094               if (classtab[i].num == n) return (classtab[i].name);
00095        return ("unknown");
00096 }
00097 
00098 int rfc1035_class_strtoi(const char *n)
00099 {
00100 unsigned i;
00101 
00102        for (i=0; i<sizeof(classtab)/sizeof(classtab[0]); i++)
00103               if (COMPARE(classtab[i].name, n) == 0) return (classtab[i].num);
00104        return (-1);
00105 }
00106 
00107 const char *rfc1035_opcode_itostr(int n)
00108 {
00109 unsigned i;
00110 
00111        for (i=0; i<sizeof(opcodetab)/sizeof(opcodetab[0]); i++)
00112               if (opcodetab[i].num == n) return (opcodetab[i].name);
00113        return ("unknown");
00114 }
00115 
00116 int rfc1035_opcode_strtoi(const char *n)
00117 {
00118 unsigned i;
00119 
00120        for (i=0; i<sizeof(opcodetab)/sizeof(opcodetab[0]); i++)
00121               if (COMPARE(opcodetab[i].name, n) == 0)
00122                             return (opcodetab[i].num);
00123        return (-1);
00124 }
00125 
00126 const char *rfc1035_rcode_itostr(int n)
00127 {
00128 unsigned i;
00129 
00130        for (i=0; i<sizeof(rcodetab)/sizeof(rcodetab[0]); i++)
00131               if (rcodetab[i].num == n) return (rcodetab[i].name);
00132        return ("unknown");
00133 }
00134 
00135 int rfc1035_rcode_strtoi(const char *n)
00136 {
00137 unsigned i;
00138 
00139        for (i=0; i<sizeof(rcodetab)/sizeof(rcodetab[0]); i++)
00140               if (COMPARE(rcodetab[i].name, n) == 0)
00141                             return (rcodetab[i].num);
00142        return (-1);
00143 }