1
0

patch-record_c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. $Id$
  2. --- dsniff-2.4.orig/record.c 2001-03-15 09:33:04.000000000 +0100
  3. +++ dsniff-2.4/record.c 2009-12-11 12:49:32.000000000 +0100
  4. @@ -15,12 +15,7 @@
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include <md5.h>
  8. -#ifdef HAVE_DB_185_H
  9. -#define DB_LIBRARY_COMPATIBILITY_API
  10. -#include <db_185.h>
  11. -#elif HAVE_DB_H
  12. -#include <db.h>
  13. -#endif
  14. +#include <gdbm.h>
  15. #include <libnet.h>
  16. #include "options.h"
  17. @@ -37,7 +32,7 @@ struct rec {
  18. struct netobj data;
  19. };
  20. -static DB *db;
  21. +GDBM_FILE dbf;
  22. static int
  23. xdr_rec(XDR *xdrs, struct rec *rec)
  24. @@ -64,9 +59,8 @@ record_print(struct rec *rec)
  25. tm = localtime(&rec->time);
  26. strftime(tstr, sizeof(tstr), "%x %X", tm);
  27. -
  28. - srcp = libnet_host_lookup(rec->src, Opt_dns);
  29. - dstp = libnet_host_lookup(rec->dst, Opt_dns);
  30. + srcp = libnet_addr2name4(rec->src, Opt_dns);
  31. + dstp = libnet_addr2name4(rec->dst, Opt_dns);
  32. if ((pr = getprotobynumber(rec->proto)) == NULL)
  33. protop = "unknown";
  34. @@ -89,10 +83,10 @@ record_print(struct rec *rec)
  35. fflush(stdout);
  36. }
  37. -static DBT *
  38. +static datum
  39. record_hash(struct rec *rec)
  40. {
  41. - static DBT key;
  42. + static datum key;
  43. static u_char hash[16];
  44. MD5_CTX ctx;
  45. @@ -105,16 +99,16 @@ record_hash(struct rec *rec)
  46. MD5Update(&ctx, rec->data.n_bytes, rec->data.n_len);
  47. MD5Final(hash, &ctx);
  48. - key.data = hash;
  49. - key.size = sizeof(hash);
  50. + key.dptr = hash;
  51. + key.dsize = sizeof(hash);
  52. - return (&key);
  53. + return (key);
  54. }
  55. static int
  56. record_save(struct rec *rec)
  57. {
  58. - DBT *key, data;
  59. + datum key, data;
  60. XDR xdrs;
  61. u_char buf[2048];
  62. @@ -123,15 +117,15 @@ record_save(struct rec *rec)
  63. if (!xdr_rec(&xdrs, rec))
  64. return (0);
  65. - data.data = buf;
  66. - data.size = xdr_getpos(&xdrs);
  67. + data.dptr = buf;
  68. + data.dsize = xdr_getpos(&xdrs);
  69. xdr_destroy(&xdrs);
  70. key = record_hash(rec);
  71. - if (db->put(db, key, &data, R_NOOVERWRITE) == 0)
  72. - db->sync(db, 0);
  73. + if (gdbm_store(dbf, key, data, GDBM_INSERT) == 0)
  74. + gdbm_sync(dbf);
  75. return (1);
  76. }
  77. @@ -139,18 +133,22 @@ record_save(struct rec *rec)
  78. void
  79. record_dump(void)
  80. {
  81. - DBT key, data;
  82. + datum nextkey, key, content;
  83. XDR xdrs;
  84. struct rec rec;
  85. - while (db->seq(db, &key, &data, R_NEXT) == 0) {
  86. + key = gdbm_firstkey(dbf);
  87. + while (key.dptr) {
  88. + nextkey = gdbm_nextkey(dbf, key);
  89. + content = gdbm_fetch(dbf, key);
  90. memset(&rec, 0, sizeof(rec));
  91. - xdrmem_create(&xdrs, data.data, data.size, XDR_DECODE);
  92. -
  93. + xdrmem_create(&xdrs, content.dptr, content.dsize, XDR_DECODE);
  94. if (xdr_rec(&xdrs, &rec)) {
  95. record_print(&rec);
  96. }
  97. xdr_destroy(&xdrs);
  98. + free(key.dptr);
  99. + key = nextkey;
  100. }
  101. }
  102. @@ -158,16 +156,23 @@ int
  103. record_init(char *file)
  104. {
  105. int flags, mode;
  106. -
  107. + // needed for gdbm_open, which does not have the option to create
  108. + // a database in memory
  109. + if(file == NULL) {
  110. + char *record_file = "/tmp/.dsniff.db";
  111. + file = record_file;
  112. + }
  113. +
  114. if (Opt_read) {
  115. - flags = O_RDONLY;
  116. + flags = GDBM_READER;
  117. mode = 0;
  118. }
  119. else {
  120. - flags = O_RDWR|O_CREAT;
  121. + flags = GDBM_WRCREAT;
  122. mode = S_IRUSR|S_IWUSR;
  123. }
  124. - if ((db = dbopen(file, flags, mode, DB_BTREE, NULL)) == NULL)
  125. +
  126. + if ((dbf = gdbm_open(file, 1024, flags, mode, NULL)) == NULL)
  127. return (0);
  128. return (1);
  129. @@ -206,6 +211,6 @@ record(in_addr_t src, in_addr_t dst, int
  130. void
  131. record_close(void)
  132. {
  133. - db->close(db);
  134. + gdbm_close(dbf);
  135. }