/* tables of names for values defined in constants.h * Copyright (C) 1998, 1999 D. Hugh Redelmeier. * * 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 . * * 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. * * RCSID $Id: constants.c,v 1.31 2000/06/18 19:15:34 dhr Exp $ */ /* * Note that the array sizes are all specified; this is to enable range * checking by code that only includes constants.h. */ #include #include #include #include "constants.h" #include "packet.h" struct enum_names { unsigned long en_first; /* first value in range */ unsigned long en_last; /* last value in range (inclusive) */ const char *const *en_names; const struct enum_names *en_next_range; /* descriptor of next range */ }; /* version */ static const char *const version_name[] = { "ISAKMP Version 1.0", }; enum_names version_names = { ISAKMP_MAJOR_VERSION< static const char *const rr_type_name[] = { "T_A", /* 1 host address */ "T_NS", /* 2 authoritative server */ "T_MD", /* 3 mail destination */ "T_MF", /* 4 mail forwarder */ "T_CNAME", /* 5 canonical name */ "T_SOA", /* 6 start of authority zone */ "T_MB", /* 7 mailbox domain name */ "T_MG", /* 8 mail group member */ "T_MR", /* 9 mail rename name */ "T_NULL", /* 10 null resource record */ "T_WKS", /* 11 well known service */ "T_PTR", /* 12 domain name pointer */ "T_HINFO", /* 13 host information */ "T_MINFO", /* 14 mailbox information */ "T_MX", /* 15 mail routing information */ "T_TXT", /* 16 text strings */ "T_RP", /* 17 responsible person */ "T_AFSDB", /* 18 AFS cell database */ "T_X25", /* 19 X_25 calling address */ "T_ISDN", /* 20 ISDN calling address */ "T_RT", /* 21 router */ "T_NSAP", /* 22 NSAP address */ "T_NSAP_PTR", /* 23 reverse NSAP lookup (deprecated) */ "T_SIG", /* 24 security signature */ "T_KEY", /* 25 security key */ "T_PX", /* 26 X.400 mail mapping */ "T_GPOS", /* 27 geographical position (withdrawn) */ "T_AAAA", /* 28 IP6 Address */ "T_LOC", /* 29 Location Information */ "T_NXT", /* 30 Next Valid Name in Zone */ "T_EID", /* 31 Endpoint identifier */ "T_NIMLOC", /* 32 Nimrod locator */ "T_SRV", /* 33 Server selection */ "T_ATMA", /* 34 ATM Address */ "T_NAPTR", /* 35 Naming Authority PoinTeR */ NULL }; enum_names rr_type_names = { T_A, T_NAPTR, rr_type_name, NULL }; /* Query type values which do not appear in resource records */ static const char *const rr_qtype_name[] = { "T_IXFR", /* 251 incremental zone transfer */ "T_AXFR", /* 252 transfer zone of authority */ "T_MAILB", /* 253 transfer mailbox records */ "T_MAILA", /* 254 transfer mail agent records */ "T_ANY", /* 255 wildcard match */ NULL }; enum_names rr_qtype_names = { T_IXFR, T_ANY, rr_qtype_name, &rr_type_names }; static const char *const rr_class_name[] = { "C_IN", /* 1 the arpa internet */ NULL }; enum_names rr_class_names = { C_IN, C_IN, rr_class_name, NULL }; /* look up enum names in an enum_names */ const char *enum_name(enum_names *ed, unsigned long val) { enum_names *p; for (p = ed; p != NULL; p = p->en_next_range) if (p->en_first <= val && val <= p->en_last) return p->en_names[val - p->en_first]; return NULL; } /* find or construct a string to describe an enum value * Result may be in STATIC buffer! */ const char * enum_show(enum_names *ed, unsigned long val) { const char *p = enum_name(ed, val); if (p == NULL) { static char buf[12]; /* only one! I hope that it is big enough */ snprintf(buf, sizeof(buf), "%lu??", val); p = buf; } return p; } /* construct a string to name the bits on in a set * Result may be in STATIC buffer! */ const char * bitnamesof(const char *const table[], lset_t val) { static char buf[100]; /* only one! I hope that it is big enough! */ char *p = buf; lset_t bit; const char *const *tp; if (val == 0) return "none"; for (tp = table, bit = 01; val != 0; bit <<= 1) { if (val & bit) { const char *n = *tp; size_t nl; if (n == NULL || *n == '\0') { /* no name for this bit, so use hex */ static char flagbuf[10]; snprintf(flagbuf, sizeof(flagbuf), "0x%lx", bit); n = flagbuf; } nl = strlen(n); if (p != buf && buf < buf+sizeof(buf) - 1) *p++ = '+'; if (buf+sizeof(buf) - p > (ptrdiff_t)nl) { strcpy(p, n); p += nl; } val -= bit; } if (*tp != NULL) tp++; /* move on, but not past end */ } *p = '\0'; return buf; } /* test a set by seeing if all bits have names */ bool testset(const char *const table[], lset_t val) { lset_t bit; const char *const *tp; for (tp = table, bit = 01; val != 0; bit <<= 1, tp++) { const char *n = *tp; if (n == NULL || ((val & bit) && *n == '\0')) return FALSE; val &= ~bit; } return TRUE; }