summaryrefslogtreecommitdiff
path: root/package/dsniff/patches/patch-record_c
blob: cd63e09696fb11c442ca53d12672072f2ba54323 (plain)
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
$Id$
--- dsniff-2.4.orig/record.c	2001-03-15 09:33:04.000000000 +0100
+++ dsniff-2.4/record.c	2007-01-23 00:20:48.000000000 +0100
@@ -15,12 +15,7 @@
 #include <stdio.h>
 #include <time.h>
 #include <md5.h>
-#ifdef HAVE_DB_185_H
-#define DB_LIBRARY_COMPATIBILITY_API
-#include <db_185.h>
-#elif HAVE_DB_H
-#include <db.h>
-#endif
+#include <gdbm.h>
 #include <libnet.h>
 
 #include "options.h"
@@ -37,7 +32,7 @@ struct rec {
 	struct netobj	data;
 };
 	
-static DB *db;
+GDBM_FILE dbf;
 
 static int
 xdr_rec(XDR *xdrs, struct rec *rec)
@@ -64,7 +59,6 @@ record_print(struct rec *rec)
 	
 	tm = localtime(&rec->time);
 	strftime(tstr, sizeof(tstr), "%x %X", tm);
-	
 	srcp = libnet_host_lookup(rec->src, Opt_dns);
 	dstp = libnet_host_lookup(rec->dst, Opt_dns);
 
@@ -89,10 +83,10 @@ record_print(struct rec *rec)
 	fflush(stdout);
 }
 
-static DBT *
+static datum
 record_hash(struct rec *rec)
 {
-	static DBT key;
+	static datum key;
 	static u_char hash[16];
 	MD5_CTX ctx;
 
@@ -105,16 +99,16 @@ record_hash(struct rec *rec)
 	MD5Update(&ctx, rec->data.n_bytes, rec->data.n_len);
 	MD5Final(hash, &ctx);
 
-	key.data = hash;
-	key.size = sizeof(hash);
+	key.dptr = hash;
+	key.dsize = sizeof(hash);
 	
-	return (&key);
+	return (key);
 }
 
 static int
 record_save(struct rec *rec)
 {
-	DBT *key, data;
+	datum key, data;
 	XDR xdrs;
 	u_char buf[2048];
 	
@@ -123,15 +117,15 @@ record_save(struct rec *rec)
 	if (!xdr_rec(&xdrs, rec))
 		return (0);
 	
-	data.data = buf;
-	data.size = xdr_getpos(&xdrs);
+	data.dptr = buf;
+	data.dsize = xdr_getpos(&xdrs);
 	
 	xdr_destroy(&xdrs);
 
 	key = record_hash(rec);
 	
-	if (db->put(db, key, &data, R_NOOVERWRITE) == 0)
-		db->sync(db, 0);
+	if (gdbm_store(dbf, key, data, GDBM_INSERT) == 0)
+		gdbm_sync(dbf);
 	
 	return (1);
 }
@@ -139,18 +133,22 @@ record_save(struct rec *rec)
 void
 record_dump(void)
 {
-	DBT key, data;
+	datum nextkey, key, content;
 	XDR xdrs;
 	struct rec rec;
 	
-	while (db->seq(db, &key, &data, R_NEXT) == 0) {	
+	key = gdbm_firstkey(dbf);
+	while (key.dptr) {	
+		nextkey = gdbm_nextkey(dbf, key);
+		content = gdbm_fetch(dbf, key);
 		memset(&rec, 0, sizeof(rec));
-		xdrmem_create(&xdrs, data.data, data.size, XDR_DECODE);
-		
+		xdrmem_create(&xdrs, content.dptr, content.dsize, XDR_DECODE);
 		if (xdr_rec(&xdrs, &rec)) {
 			record_print(&rec);
 		}
 		xdr_destroy(&xdrs);
+		free(key.dptr);
+		key = nextkey;
 	}
 }
 
@@ -158,16 +156,23 @@ int
 record_init(char *file)
 {
 	int flags, mode;
-	
+	// needed for gdbm_open, which does not have the option to create
+	// a database in memory
+	if(file == NULL) {
+		char *record_file = "/tmp/.dsniff.db";
+		file = record_file;
+	}
+
 	if (Opt_read) {
-		flags = O_RDONLY;
+		flags = GDBM_READER;
 		mode = 0;
 	}
 	else {
-		flags = O_RDWR|O_CREAT;
+		flags = GDBM_WRCREAT;
 		mode = S_IRUSR|S_IWUSR;
 	}
-	if ((db = dbopen(file, flags, mode, DB_BTREE, NULL)) == NULL)
+
+	if ((dbf = gdbm_open(file, 1024, flags, mode, NULL)) == NULL)
 		return (0);
 
 	return (1);
@@ -206,6 +211,6 @@ record(in_addr_t src, in_addr_t dst, int
 void
 record_close(void)
 {
-	db->close(db);
+	gdbm_close(dbf);
 }