Download | Plain Text | No Line Numbers


  1. diff -urN djbdns-1.05/Makefile djbdns-1.05_new/Makefile
  2. --- djbdns-1.05/Makefile Sun Feb 11 23:11:45 2001
  3. +++ djbdns-1.05_new/Makefile Sun Sep 14 18:07:13 2008
  4. @@ -152,9 +152,14 @@
  5. uint32.h uint64.h
  6. ./compile cache.c
  7.  
  8. +cache_djb.o: \
  9. +compile cache_djb.c alloc.h byte.h uint32.h exit.h tai.h uint64.h cache.h \
  10. +uint32.h uint64.h
  11. + ./compile cache_djb.c
  12. +
  13. cachetest: \
  14. -load cachetest.o cache.o libtai.a buffer.a alloc.a unix.a byte.a
  15. - ./load cachetest cache.o libtai.a buffer.a alloc.a unix.a \
  16. +load cachetest.o cache_djb.o libtai.a buffer.a alloc.a unix.a byte.a
  17. + ./load cachetest cache_djb.o libtai.a buffer.a alloc.a unix.a \
  18. byte.a
  19.  
  20. cachetest.o: \
  21. diff -urN djbdns-1.05/README.dumpcache djbdns-1.05_new/README.dumpcache
  22. --- djbdns-1.05/README.dumpcache Thu Jan 1 02:00:00 1970
  23. +++ djbdns-1.05_new/README.dumpcache Tue Sep 9 14:57:40 2008
  24. @@ -0,0 +1,23 @@
  25. +dnscache-conf sets variables:
  26. + DUMPCACHE=14400:dump/data
  27. + SLURPCACHE=dump/data
  28. +The number 14400 is the dump interval. Set it between 3600 and 43200.
  29. +
  30. +Understands signals:
  31. + SIGTERM dump cache and exit
  32. + SIGALARM dump cache
  33. + SIGHUP reload cache
  34. +
  35. +Filtering like multilog. To use filtering edit run file. Example:
  36. +...
  37. +exec envdir ./env envuidgid dnscache softlimit -o250 -d 3000000 \
  38. + /usr/local/bin/dnscache '-*' '+stats *' '+slurp *' '+dump *' '+tcp *'
  39. +
  40. +You can comment line 32 of log.c to remove prefix "dns: ".
  41. +
  42. +dnscache ignores SIGPIPE.
  43. +
  44. +I will put documention on:
  45. +http://riemann.fmi.uni-sofia.bg/docs/djbdns-dumpcache.html
  46. +
  47. +Enjoy, Nikola
  48. diff -urN djbdns-1.05/TARGETS djbdns-1.05_new/TARGETS
  49. --- djbdns-1.05/TARGETS Sun Feb 11 23:11:45 2001
  50. +++ djbdns-1.05_new/TARGETS Sun Sep 14 18:12:47 2008
  51. @@ -86,6 +86,7 @@
  52. okclient.o
  53. log.o
  54. cache.o
  55. +cache_djb.o
  56. query.o
  57. response.o
  58. dd.o
  59. diff -urN djbdns-1.05/cache.c djbdns-1.05_new/cache.c
  60. --- djbdns-1.05/cache.c Sun Feb 11 23:11:45 2001
  61. +++ djbdns-1.05_new/cache.c Tue Sep 9 14:57:40 2008
  62. @@ -1,3 +1,16 @@
  63. +#ifdef DUMPCACHE
  64. +#include <unistd.h>
  65. +#include <fcntl.h>
  66. +#include <sys/mman.h>
  67. +#include <stdio.h>
  68. +#include <errno.h>
  69. +#include <stdlib.h>
  70. +#include "str.h"
  71. +#include "open.h"
  72. +#include "buffer.h"
  73. +#include "scan.h"
  74. +#include "log.h"
  75. +#endif
  76. #include "alloc.h"
  77. #include "byte.h"
  78. #include "uint32.h"
  79. @@ -137,6 +150,7 @@
  80. if (datalen > MAXDATALEN) return;
  81.  
  82. if (!ttl) return;
  83. + if (cache_get(key, keylen, &entrylen, &pos)) return;
  84. if (ttl > 604800) ttl = 604800;
  85.  
  86. entrylen = keylen + datalen + 20;
  87. @@ -183,25 +197,137 @@
  88.  
  89. int cache_init(unsigned int cachesize)
  90. {
  91. +#ifdef DUMPCACHE
  92. + char *y=x;
  93. + uint32 old_size=size;
  94. +#else
  95. if (x) {
  96. alloc_free(x);
  97. x = 0;
  98. }
  99. +#endif
  100.  
  101. if (cachesize > 1000000000) cachesize = 1000000000;
  102. if (cachesize < 100) cachesize = 100;
  103. size = cachesize;
  104.  
  105. - hsize = 4;
  106. - while (hsize <= (size >> 5)) hsize <<= 1;
  107. -
  108. +#ifdef DUMPCACHE
  109. + x = realloc(x,size);
  110. + if (!x) { x = y; size = old_size; }
  111. + log_cachesize(size);
  112. +#else
  113. x = alloc(size);
  114. +#endif
  115. if (!x) return 0;
  116. byte_zero(x,size);
  117.  
  118. + hsize = 4;
  119. + while (hsize <= (size >> 5)) hsize <<= 1;
  120. +
  121. writer = hsize;
  122. oldest = size;
  123. unused = size;
  124.  
  125. return 1;
  126. }
  127. +
  128. +#ifdef DUMPCACHE
  129. +int cache_dump(char *fn)
  130. +{
  131. + uint32 pos;
  132. + uint32 len, last;
  133. + int r=-1;
  134. + char *tmp;
  135. + buffer b = BUFFER_INIT((int (*)(int, char*, unsigned int))write,
  136. + -1, 0, 8000);
  137. + len=str_len(fn);
  138. + tmp = alloc(8032+len);
  139. + if (tmp == 0) return -1;
  140. + byte_copy(tmp,len,fn);
  141. + byte_copy(tmp+len,5,".tmp");
  142. + if ((b.fd = open_trunc(tmp)) == -1) goto ready;
  143. + b.x = tmp + len + 16;
  144. +
  145. + for (r=0, pos=oldest, last=unused; r<2; pos=hsize, last=writer, r++)
  146. + while (pos < last) {
  147. + len = get4(pos + 4) + get4(pos + 8) + 16;
  148. + if (buffer_put(&b, x + pos + 4, len)) goto close_fd;
  149. + pos += 4 + len;
  150. + }
  151. +
  152. + r = 0;
  153. + if (buffer_flush(&b) || fsync(b.fd) || close(b.fd) || rename(tmp,fn)) {
  154. + close_fd:
  155. + r = errno;
  156. + close(b.fd);
  157. + unlink(tmp);
  158. + errno = r;
  159. + r = -1;
  160. + }
  161. + ready:
  162. + alloc_free(tmp);
  163. + return r;
  164. +}
  165. +
  166. +int cache_slurp(char *fn, int reinit)
  167. +{
  168. + char *map, *data;
  169. + uint32 pos;
  170. + uint32 keylen;
  171. + uint32 datalen;
  172. + uint32 len;
  173. + struct tai now, expire;
  174. + int fd;
  175. + unsigned long a=200, b=32768;
  176. +
  177. + if ((fd = open_read(fn)) == -1) return -1;
  178. + len = lseek(fd,0,SEEK_END);
  179. +
  180. + if (reinit) {
  181. + static unsigned int cachesize;
  182. + if (!cachesize) cachesize = size;
  183. + if ((pos=str_len(fn)) + 48 < size) {
  184. + int r;
  185. + data = x + 32;
  186. + byte_copy(data,pos,fn);
  187. + byte_copy(data+pos,8,".blowup");
  188. + r=readlink(data,x,24);
  189. + if (r > 0) {
  190. + x[r] = 0;
  191. + if ((r = scan_ulong(x,&a)) && x[r] == ':')
  192. + scan_ulong(x+r+1,&b);
  193. + }
  194. + }
  195. +
  196. + pos = ((a*(len/100) + b) & ~4095) - 40;
  197. + if (pos > cachesize) pos = cachesize;
  198. + /* next is impossible, since cache is already init */
  199. + if (!cache_init(pos)) cache_impossible();
  200. + cache_motion = 0;
  201. + }
  202. +
  203. + map=mmap(0,len,PROT_READ,MAP_SHARED,fd,0);
  204. + close(fd);
  205. + if (map==MAP_FAILED) return -1;
  206. +
  207. + tai_now(&now);
  208. + pos = 0;
  209. + a = 0;
  210. + while (pos + 16 <= len) {
  211. + data = map + pos;
  212. + uint32_unpack(data, &keylen);
  213. + uint32_unpack(data + 4, &datalen);
  214. + tai_unpack(data + 8, &expire);
  215. + pos += 16 + keylen + datalen;
  216. + data += 16;
  217. + if (pos > len) break; /* missing data */
  218. + if (expire.x > now.x) {
  219. + cache_set(data, keylen, data + keylen, datalen,
  220. + (uint32)(expire.x - now.x));
  221. + a++;
  222. + }
  223. + }
  224. + munmap(map,len);
  225. + return a;
  226. +}
  227. +#endif
  228. diff -urN djbdns-1.05/cache.h djbdns-1.05_new/cache.h
  229. --- djbdns-1.05/cache.h Sun Feb 11 23:11:45 2001
  230. +++ djbdns-1.05_new/cache.h Tue Sep 9 14:57:40 2008
  231. @@ -8,5 +8,9 @@
  232. extern int cache_init(unsigned int);
  233. extern void cache_set(const char *,unsigned int,const char *,unsigned int,uint32);
  234. extern char *cache_get(const char *,unsigned int,unsigned int *,uint32 *);
  235. +#ifdef DUMPCACHE
  236. +extern int cache_dump(char *);
  237. +extern int cache_slurp(char *, int);
  238. +#endif
  239.  
  240. #endif
  241. diff -urN djbdns-1.05/cache_djb.c djbdns-1.05_new/cache_djb.c
  242. --- djbdns-1.05/cache_djb.c Thu Jan 1 02:00:00 1970
  243. +++ djbdns-1.05_new/cache_djb.c Sun Sep 14 18:02:31 2008
  244. @@ -0,0 +1,4 @@
  245. +#ifdef DUMPCACHE
  246. +#undef DUMPCACHE
  247. +#endif
  248. +#include "cache.c"
  249. diff -urN djbdns-1.05/conf-cc djbdns-1.05_new/conf-cc
  250. --- djbdns-1.05/conf-cc Sun Feb 11 23:11:45 2001
  251. +++ djbdns-1.05_new/conf-cc Sun Sep 14 18:14:05 2008
  252. @@ -1,3 +1,3 @@
  253. -gcc -O2 -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized -Wshadow -Wcast-qual -Wcast-align -Wwrite-strings
  254. +gcc -O2 -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized -Wshadow -Wcast-qual -Wcast-align -Wwrite-strings -DDUMPCACHE
  255.  
  256. This will be used to compile .c files.
  257. diff -urN djbdns-1.05/dnscache-conf.c djbdns-1.05_new/dnscache-conf.c
  258. --- djbdns-1.05/dnscache-conf.c Sun Feb 11 23:11:45 2001
  259. +++ djbdns-1.05_new/dnscache-conf.c Tue Sep 9 14:57:40 2008
  260. @@ -32,6 +32,10 @@
  261. char *user;
  262. char *loguser;
  263. struct passwd *pw;
  264. +#ifdef DUMPCACHE
  265. +int useruid;
  266. +int usergid;
  267. +#endif
  268. const char *myip;
  269.  
  270. uint32 seed[32];
  271. @@ -81,6 +85,14 @@
  272. myip = argv[4];
  273. if (!myip) myip = "127.0.0.1";
  274.  
  275. +#ifdef DUMPCACHE
  276. + pw = getpwnam(user);
  277. + seed_addtime();
  278. + if (!pw)
  279. + strerr_die3x(111,FATAL,"unknown account ",user);
  280. + useruid = pw->pw_uid;
  281. + usergid = pw->pw_gid;
  282. +#endif
  283. pw = getpwnam(loguser);
  284. seed_addtime();
  285. if (!pw)
  286. @@ -120,6 +132,12 @@
  287. seed_addtime(); perm(0644);
  288. seed_addtime(); start("env/DATALIMIT"); outs("3000000\n"); finish();
  289. seed_addtime(); perm(0644);
  290. +#ifdef DUMPCACHE
  291. + seed_addtime(); start("env/SLURPCACHE"); outs("dump/data\n"); finish();
  292. + seed_addtime(); perm(0644);
  293. + seed_addtime(); start("env/DUMPCACHE"); outs("14400:dump/data\n"); finish();
  294. + seed_addtime(); perm(0644);
  295. +#endif
  296. seed_addtime(); start("run");
  297. outs("#!/bin/sh\nexec 2>&1\nexec <seed\nexec envdir ./env sh -c '\n exec envuidgid "); outs(user);
  298. outs(" softlimit -o250 -d \"$DATALIMIT\" ");
  299. @@ -131,6 +149,11 @@
  300. seed_addtime(); perm(0755);
  301. seed_addtime(); makedir("root");
  302. seed_addtime(); perm(02755);
  303. +#ifdef DUMPCACHE
  304. + seed_addtime(); makedir("root/dump");
  305. + seed_addtime(); owner(useruid,usergid);
  306. + seed_addtime(); perm(02755);
  307. +#endif
  308. seed_addtime(); makedir("root/ip");
  309. seed_addtime(); perm(02755);
  310. seed_addtime(); start("root/ip/127.0.0.1"); finish();
  311. diff -urN djbdns-1.05/dnscache.c djbdns-1.05_new/dnscache.c
  312. --- djbdns-1.05/dnscache.c Sun Feb 11 23:11:45 2001
  313. +++ djbdns-1.05_new/dnscache.c Tue Sep 9 14:57:40 2008
  314. @@ -22,6 +22,21 @@
  315. #include "log.h"
  316. #include "okclient.h"
  317. #include "droproot.h"
  318. +#include <signal.h>
  319. +char **ARGV;
  320. +void sig_catch(int sig, void (*f)()) {
  321. + struct sigaction sa;
  322. + sa.sa_handler = f;
  323. + sa.sa_flags = 0;
  324. + sigemptyset(&sa.sa_mask);
  325. + sigaction(sig,&sa,(struct sigaction *) 0);
  326. +}
  327. +#ifdef DUMPCACHE
  328. +static unsigned long dumptime, flagsignal;
  329. +static char *slurpcache, *dumpcache;
  330. +static struct tai nextdump;
  331. +void got_signal(int sig) { flagsignal=sig; }
  332. +#endif
  333.  
  334. static int packetquery(char *buf,unsigned int len,char **q,char qtype[2],char qclass[2],char id[2])
  335. {
  336. @@ -115,6 +130,12 @@
  337. if (x->port < 1024) if (x->port != 53) return;
  338. if (!okclient(x->ip)) return;
  339.  
  340. +#ifdef DUMPCACHE
  341. + if (dumptime && nextdump.x < x->start.sec.x) {
  342. + flagsignal=9999;
  343. + nextdump.x = x->start.sec.x + (uint64)dumptime;
  344. + }
  345. +#endif
  346. if (!packetquery(buf,len,&q,qtype,qclass,x->id)) return;
  347.  
  348. x->active = ++numqueries; ++uactive;
  349. @@ -379,6 +400,18 @@
  350. if (tcp53io)
  351. if (tcp53io->revents)
  352. t_new();
  353. +#ifdef DUMPCACHE
  354. + if (flagsignal && flagsignal != SIGHUP) { /* SIGTERM SIGALRM 9999 */
  355. + r=cache_dump(dumpcache);
  356. + log_dump(r);
  357. + if (flagsignal == SIGTERM) _exit(0);
  358. + if (flagsignal == SIGALRM) flagsignal = 0;
  359. + else if (!r && slurpcache) flagsignal = SIGHUP; /* 9999 */
  360. + }
  361. + if (flagsignal == SIGHUP)
  362. + log_slurp(cache_slurp(slurpcache, 1));
  363. + flagsignal=0;
  364. +#endif
  365. }
  366. }
  367.  
  368. @@ -386,11 +419,13 @@
  369.  
  370. char seed[128];
  371.  
  372. -int main()
  373. +int main(int argc, char **argv)
  374. {
  375. char *x;
  376. unsigned long cachesize;
  377.  
  378. + sig_catch(SIGPIPE, SIG_IGN);
  379. + ARGV = argv+1; (void)argc;
  380. x = env_get("IP");
  381. if (!x)
  382. strerr_die2x(111,FATAL,"$IP not set");
  383. @@ -443,5 +478,22 @@
  384. strerr_die2sys(111,FATAL,"unable to listen on TCP socket: ");
  385.  
  386. log_startup();
  387. +#ifdef DUMPCACHE
  388. + if ((slurpcache=env_get("SLURPCACHE"))) {
  389. + log_slurp(cache_slurp(slurpcache, 0));
  390. + sig_catch(SIGHUP, got_signal);
  391. + }
  392. + if ((x=env_get("DUMPCACHE"))) {
  393. + int k=scan_ulong(x, &dumptime);
  394. + if (dumptime)
  395. + if (x[k]=':' && x[k+1]) {
  396. + dumpcache = x+k+1;
  397. + tai_now(&nextdump);
  398. + nextdump.x += (uint64)dumptime;
  399. + sig_catch(SIGTERM, got_signal);
  400. + sig_catch(SIGALRM, got_signal);
  401. + } else dumptime = 0;
  402. + }
  403. +#endif
  404. doit();
  405. }
  406. diff -urN djbdns-1.05/log.c djbdns-1.05_new/log.c
  407. --- djbdns-1.05/log.c Sun Feb 11 23:11:45 2001
  408. +++ djbdns-1.05_new/log.c Tue Sep 9 14:57:40 2008
  409. @@ -4,6 +4,67 @@
  410. #include "error.h"
  411. #include "byte.h"
  412. #include "log.h"
  413. +#include "str.h"
  414. +
  415. +int match(const char *pattern,const char *buf,unsigned int len) {
  416. + char ch;
  417. + for (;;) {
  418. + ch = *pattern++;
  419. + if (!ch) return !len;
  420. + if (ch == '*') {
  421. + ch = *pattern;
  422. + if (!ch) return 1;
  423. + for (;;) {
  424. + if (!len) return 0;
  425. + if (*buf == ch) break;
  426. + ++buf; --len;
  427. + }
  428. + continue;
  429. + }
  430. + if (!len) return 0;
  431. + if (*buf != ch) return 0;
  432. + ++buf; --len;
  433. + }
  434. +}
  435. +
  436. +extern char **ARGV;
  437. +static int ok;
  438. +#define LOG_PREFIX "dns: "
  439. +
  440. +static void buffer_check() {
  441. + unsigned int len= buffer_2->p;
  442. + char **p=ARGV, *x= buffer_2->x;
  443. +
  444. +#ifdef LOG_PREFIX
  445. + if (len>=5) {len -= 5; x += 5;}
  446. +#endif
  447. + ok = 1;
  448. + for (; *p; p++) {
  449. + if (**p=='+' && ok!= 1 && match(*p+1,x,len)) ok= 1;
  450. + if (**p=='-' && ok!=-1 && match(*p+1,x,len)) ok=-1;
  451. + }
  452. + if (ok==-1) buffer_2->p=0;
  453. +}
  454. +
  455. +/* ok==0 no prefix; ok==-2 has prefix; ok==-1 no print; ok==1 print */
  456. +static void out_put(const char *s, unsigned long n) {
  457. + switch (ok) {
  458. + case 0: ok=-2;
  459. +#ifdef LOG_PREFIX
  460. + buffer_puts(buffer_2, LOG_PREFIX);
  461. +#endif
  462. + case -2:
  463. + if (n > buffer_2->n - buffer_2->p) { /* no free space */
  464. + buffer_check();
  465. + if (ok==-1) break;
  466. + }
  467. + case 1: buffer_put(buffer_2,s,n);
  468. + }
  469. +}
  470. +
  471. +static void out_puts(const char *s) {out_put(s,str_len(s));}
  472. +#define buffer_put(a,b,c) out_put(b,c)
  473. +#define buffer_puts(a,b) out_puts(b)
  474.  
  475. /* work around gcc 2.95.2 bug */
  476. #define number(x) ( (u64 = (x)), u64_print() )
  477. @@ -37,7 +98,9 @@
  478. static void line(void)
  479. {
  480. string("\n");
  481. + if (ok!=1 && ok!=-1) buffer_check();
  482. buffer_flush(buffer_2);
  483. + ok = 0;
  484. }
  485.  
  486. static void space(void)
  487. @@ -93,6 +156,32 @@
  488. string("starting");
  489. line();
  490. }
  491. +
  492. +
  493. +#ifdef DUMPCACHE
  494. +void log_slurp(int nb)
  495. +{
  496. + string("slurp ");
  497. + if (nb<0) { string("err "); string(error_str(errno)); }
  498. + else number(nb);
  499. + line();
  500. +}
  501. +
  502. +void log_dump(int e)
  503. +{
  504. + string("dump ");
  505. + if (e) { string("err "); string(error_str(errno)); }
  506. + else string("ok");
  507. + line();
  508. +}
  509. +
  510. +void log_cachesize(unsigned int e)
  511. +{
  512. + string("cachesize ");
  513. + number(e);
  514. + line();
  515. +}
  516. +#endif
  517.  
  518. void log_query(uint64 *qnum,const char client[4],unsigned int port,const char id[2],const char *q,const char qtype[2])
  519. {
  520. diff -urN djbdns-1.05/log.h djbdns-1.05_new/log.h
  521. --- djbdns-1.05/log.h Sun Feb 11 23:11:45 2001
  522. +++ djbdns-1.05_new/log.h Tue Sep 9 14:57:40 2008
  523. @@ -33,4 +33,9 @@
  524.  
  525. extern void log_stats(void);
  526.  
  527. +#ifdef DUMPCACHE
  528. +extern void log_slurp(int);
  529. +extern void log_dump(int);
  530. +extern void log_cachesize(unsigned int);
  531. +#endif
  532. #endif
  533.