1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/* misc.c
* miscellaneous code
*
* Copyright (C) 2000,2001,2002 by Salvatore Sanfilippo
* <antirez@invece.org>
*
* This code is under the GPL license
* See the COPYING file for more information
*/
/* ens.h must be included before all other includes */
#include "ens.h"
#include "aht.h"
#ifdef __unix__
#include <sys/time.h>
#include <unistd.h>
#endif /* __unix__ */
#include <time.h>
#include <string.h>
#include <stdlib.h>
/* not exported functions */
/* exported functions */
u_int32_t get_rand32(void);
time_t get_sec(void);
char *qtype_to_str(unsigned short qtype);
char *qclass_to_str(unsigned short qclass);
int line_splitter(char *buffer, char *commandargs[], int argmax);
void dump_state(void);
u_int16_t get_rand_id(void);
/* --------------------------------------------------------------------------- */
time_t get_sec(void)
{
return time(NULL);
}
char *qtype_to_str(unsigned short qtype)
{
int i;
static struct _QTYPE {
unsigned short code;
char *str;
} qtype_str[] = {
{ 1, "A" },
{ 2, "NS" },
{ 3, "MD" },
{ 4, "MF" },
{ 5, "CNAME" },
{ 6, "SOA" },
{ 7, "MB" },
{ 8, "MG" },
{ 9, "MR" },
{ 10, "NULL" },
{ 11, "WKS" },
{ 12, "PTR" },
{ 13, "HINFO" },
{ 14, "MINFO" },
{ 15, "MX" },
{ 16, "TXT" },
{ 17, "RP" },
{ 18, "AFSDB" },
{ 19, "X25" },
{ 20, "ISDN" },
{ 21, "RT" },
{ 22, "NSAP" },
{ 23, "NSAP_PTR"},
{ 24, "SIG"},
{ 25, "KEY"},
{ 26, "PX"},
{ 27, "GPOS"},
{ 28, "AAAA"},
{ 29, "LOC"},
{ 30, "NXT"},
{ 31, "EID"},
{ 32, "NIMLOC"},
{ 33, "SRV"},
{ 34, "ATMA"},
{ 35, "NAPTR"},
{ 36, "KX"},
{ 37, "CERT"},
{ 38, "A6"},
{ 39, "DNAME"},
{ 40, "SINK"},
{ 41, "OPT"},
/* non standard */
{ 100, "UINFO"},
{ 101, "UID"},
{ 102, "GID"},
{ 103, "UNSPEC"},
/* Query type values which do not appear in resource records */
{ 249, "TKEY"},
{ 250, "TSIG"},
{ 251, "IXFR"},
{ 252, "AXFR" },
{ 253, "MAILB" },
{ 254, "MAILA" },
{ 255, "*" },
{ 256, "bind-ZXFR"},
{ 0, NULL} /* NUL TERM */
};
for (i = 0; qtype_str[i].code; i++) {
if (qtype_str[i].code == qtype)
return qtype_str[i].str;
}
return "UNKNOWN";
}
char *qclass_to_str(unsigned short qclass)
{
int i;
static struct _QCLASS {
unsigned short code;
char *str;
} qclass_str[] = {
{ 1, "IN" },
{ 2, "CS" },
{ 3, "CH" },
{ 4, "HS" },
{ 255, "*" },
{ 0, NULL} /* NUL TERM */
};
for (i = 0; qclass_str[i].code; i++) {
if (qclass_str[i].code == qclass)
return qclass_str[i].str;
}
return "UNKNOWN";
}
#define skip_spacetab() while(*p == ' ' || *p == '\t') p++
int line_splitter(char *buffer, char *commandargs[], int argmax)
{
char *p = buffer, *d;
char tmp[1024];
int size;
int argindex = 0;
/* if buffer is a NULL pointer free commandargs memory */
if (buffer == NULL) {
for (; commandargs[argindex] != NULL; argindex++)
free(commandargs[argindex]);
return argindex;
}
/* otherwise parse the command line */
while(*p != '\0') {
size = 0;
d = tmp;
skip_spacetab();
while(*p != ' ' && *p != '\t') {
if (*p == '\0' || *p == '\n' ||
*p == '\r' || size >= 1023)
break;
*d++ = *p++;
size++;
}
if (size != 0) {
commandargs[argindex] = malloc(size+1);
if (commandargs[argindex] == NULL) {
perror("[line_splitter] malloc");
exit(1);
}
strlcpy(commandargs[argindex], tmp, size+1);
} else {
break;
}
argindex++;
if (argindex >= argmax)
break;
}
commandargs[argindex] = NULL;
return argindex;
}
void dump_state(void)
{
ylog(VERB_FORCE,
"dump_state() requested, dump follows\n"
"\n-- GENERAL\n"
"s = %d\n"
"configfile = %s\n"
"opt_udp_port = %d\n"
"local_size = %u\n"
"local_used = %u\n"
"\n-- FORWARDING\n"
"opt_forward = %d\n"
"dns_forward_port = %d\n"
"forward_server = %s\n"
"forward_count = %d\n"
"forward_size = %u\n"
"forward_used = %u\n"
"\n-- CACHE\n"
"opt_cache = %d\n"
"cache_count = %d\n"
"cache_table_size = %u\n"
"cache_table_used = %u\n"
"\n-- STATS\n"
"statistic_received_packet = %d\n"
"statistic_invalid_packet = %d\n"
"statistic_query_count = %d\n"
"statistic_iquery_count = %d\n"
"statistic_response_count = %d\n"
"\n", s, configfile, opt_udp_port, local_table.size, local_table.used,
opt_forward, dns_forward_port, inet_ntoa(forward_server[0]),
forward_count, forward_table.size, forward_table.used,
opt_cache, cache_count, cache_table.size, cache_table.used,
statistic_received_packet, statistic_invalid_packet,
statistic_query_count, statistic_iquery_count,
statistic_response_count);
fflush(logfp);
}
/* Don't expect maximun security here, anyway the id is 16 bit large */
u_int16_t get_rand_id(void)
{
u_int32_t id;
static u_int16_t inc = 0;
#ifdef __unix__
struct timeval tmptv;
gettimeofday(&tmptv, NULL);
id = tmptv.tv_usec ^ tmptv.tv_sec ^ getpid() ^ inc++;
#else /* not __unix__ */
id = rand() ^ time(NULL) ^ clock() ^ inc++;
#endif /* not __unix__ */
return (u_int16_t) (id & 0xffff);
}
/* WARNING: THIS MAY WORK ONLY WITH GNU MALLOC
but this stuff is used only to trace memory leaks
and isn't part of a normal ENS binary */
#ifdef TRACE_LEAKS
#undef malloc
#undef realloc
#undef free
void tl_current(int x, int y);
void *tl_malloc(char *file, int line, size_t size)
{
void *ptr = malloc(size);
int *l = (int*) ptr;
printf("%s %d: malloc(%d) = %p, ", file, line, size, ptr);
if (size == 0) {
printf("malloc zero\n");
exit(1);
}
printf("allocated %d bytes\n", *(l-1));
tl_current(*(l-1), 1);
return ptr;
}
void *tl_realloc(char *file, int line, void *ptr, size_t size)
{
int *o = (int*) ptr;
int old = (o != NULL) ? *(o-1) : 0;
void *newptr = realloc(ptr, size);
int *l = (int*) newptr;
printf("%s %d: realloc(%p, %d) = %p, ", file, line, newptr, size, ptr);
printf("allocated %d bytes\n", *(l-1) - old);
tl_current(*(l-1)-old, ptr ? 0 : 1);
return newptr;
}
void tl_free(char *file, int line, void *ptr)
{
int *l = (int*) ptr;
printf("%s %d: free(%p), ", file, line, ptr);
printf("freed %d bytes\n", *(l-1));
tl_current(-(*(l-1)), -1);
free(ptr);
}
void tl_current(int x, int y)
{
static int current = 0;
static int chunkes = 0;
current += x;
chunkes += y;
printf("current: %d bytes (in %d chunkes)\n", current, chunkes);
}
#endif
|