File: libdb

package info (click to toggle)
tcpstat 1.5-8.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: ansic: 2,281; sh: 439; makefile: 31
file content (185 lines) | stat: -rw-r--r-- 5,131 bytes parent folder | download | duplicates (4)
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
From: Rafael Cunha de Almeida <rafael@kontesti.me>
Bug-Debian: http://bugs.debian.org/409372
Forwarded: Yes
Last-Update: 2010-04-14
Description: Upgrades tcpprof to use Berkeley DB 5.1
 Upstream version uses Berkeley DB 1.0, which is old and no longer
 available in debian repositories. This patch changes tcpprof in order
 for it to use version 5.1 of the library. It alsos change bdb version
 requirements in configure.in accordingly.

diff --git a/configure.in b/configure.in
index bb56ea3..fcd48e4 100644
--- a/configure.in
+++ b/configure.in
@@ -152,11 +152,10 @@ AC_CHECK_LIB(pcap, pcap_major_version, LIBS="-lpcap ${LIBS}", [
 dnl look for dbopen for tcpprof
 TCPPROF=""
 LD_TCPPROF=""
-AC_CHECK_LIB(c, dbopen, [ TCPPROF=tcpprof ], [
- AC_CHECK_LIB(db1, dbopen, [
+AC_CHECK_LIB(c, db_create, [ TCPPROF=tcpprof ], [
+ AC_CHECK_LIB(db, db_create, [
   TCPPROF=tcpprof
-  LD_TCPPROF="-ldb1"
-  AC_DEFINE(USE_DB1_LIBRARY, 1, [ Use the sleepycat DB library. ])
+  LD_TCPPROF="-ldb"
   ], [
   AC_MSG_WARN([
 
diff --git a/include/config.h.in b/include/config.h.in
index e7c91e0..67216e2 100644
--- a/include/config.h.in
+++ b/include/config.h.in
@@ -93,9 +93,6 @@
 /* defined on OSF systems. */
 #undef TRU64_STRANGENESS
 
-/* Use the sleepycat DB library. */
-#undef USE_DB1_LIBRARY
-
 /* Version number of package */
 #undef VERSION
 
diff --git a/src/stats.c b/src/stats.c
index 2572286..4971483 100644
--- a/src/stats.c
+++ b/src/stats.c
@@ -30,11 +30,7 @@
 #include "tcpprof.h"
 #include <fcntl.h>
 
-#ifdef USE_DB1_LIBRARY
-#  include <db1/db.h>
-#else
-#  include <db.h>
-#endif
+#include <db.h>
 
 extern char numbers_only;
 extern char registered_only;
@@ -73,7 +69,7 @@ data_base	*dbs;
 #define FIRST_COLUMN_WIDTH	25		/* how wide the first column should be */
 const char *type_str[] = { "Total", "Link", "IP", "Port", "Host", "Network" };
 
-int compare_keys(const DBT *d1, const DBT *d2) {
+int compare_keys(DB *db, const DBT *d1, const DBT *d2) {
 	stkey_t *k1, *k2;
 	if (d1 == NULL || d2 == NULL) return 0;
 	k1 = (stkey_t *)d1->data;
@@ -103,22 +99,28 @@ int find_entry(data_base *dbp, stkey_t *key, stat_unit *su) {
 	DBT dbt, key_dbt;
 	int ret;
 
+	memset(&dbt, 0, sizeof dbt);
+	memset(&key_dbt, 0, sizeof key_dbt);
+
 	key_dbt.data = (void *) key;
 	key_dbt.size = sizeof(stkey_t);
-	ret = db->get(db, &key_dbt, &dbt, 0);
+	ret = db->get(db, 0, &key_dbt, &dbt, 0);
 	if (dbt.data != NULL && ret == 0)
 		bcopy(dbt.data, (void *) su, sizeof(stat_unit) );
 	return ret;
 }
 
-int next_entry(data_base *dbp, stkey_t *key, stat_unit *su) {
-	DB *db = dbp->db;
+int next_entry(DBC *cursorp, stkey_t *key, stat_unit *su) {
 	DBT dbt, key_dbt;
 	int ret;
 
+	memset(&dbt, 0, sizeof dbt);
+	memset(&key_dbt, 0, sizeof key_dbt);
+
 	key_dbt.data = (void *) key;
 	key_dbt.size = sizeof(stkey_t);
-	ret = db->seq(db, &key_dbt, &dbt, (key->key == 0)? R_FIRST : R_NEXT);
+	ret = cursorp->get(cursorp,  &key_dbt, &dbt,
+		(key->key == 0)? DB_FIRST : DB_NEXT);
 	if (dbt.data != NULL && ret == 0) {
 		bcopy(dbt.data, (void *) su, sizeof(stat_unit) );
 		bcopy(key_dbt.data, (void *) key, sizeof(stkey_t) );
@@ -131,39 +133,45 @@ int add_entry(data_base *dbp, stkey_t *key, stat_unit *su) {
 	DBT dbt, key_dbt;
 	int ret;
 
+	memset(&dbt, 0, sizeof dbt);
+	memset(&key_dbt, 0, sizeof key_dbt);
+
 	key_dbt.data = (void *) key;
 	key_dbt.size = sizeof(stkey_t);
 	dbt.data = (void *) su;
 	dbt.size = sizeof(stat_unit);
-	ret = db->put(db, &key_dbt, &dbt, 0);
+	ret = db->put(db, 0, &key_dbt, &dbt, 0);
 	return ret;
 }
 
 int stats_initdb(u_int s_types) {
 	int i, j;
-	BTREEINFO bi;
 
 	tc = count_1bits(s_types);
 	if (tc == 0) return 1;
 	dbs = (data_base *) malloc(tc*sizeof(data_base));
 	if (dbs == NULL) return -1;
 
-	bi.flags = 0;
-	bi.cachesize = 0;
-	bi.maxkeypage = 0;
-	bi.minkeypage = 0;
-	bi.psize = 0;
-	bi.compare = &compare_keys;
-	bi.prefix = NULL;
-	bi.lorder = 0;
-
 		/* Init stat_r */
 	for (i=0,j=0; i<tc && j<32; j++) {
 		if ( (s_types>>j) & 0x1) {
 			dbs[i].type = 1<<j;
-			dbs[i].db = dbopen(NULL, O_RDWR, 0600, DB_BTREE, &bi);
+			if (db_create(&dbs[i].db, 0, 0)) {
+				perror("db_create");
+				return -1;
+				}
+			if (dbs[i].db->set_bt_compare(dbs[i].db, compare_keys))
+				{
+				perror("db->set_bt_compare");
+				return -1;
+				}
+			if(dbs[i].db->open(dbs[i].db, 0, NULL, NULL, DB_BTREE,
+				DB_CREATE, 0600)) {
+				perror("db->open");
+				return -1;
+				} 
 			if (dbs[i].db == NULL) {
-				perror("dbopen");
+				perror("db->open");
 				return -1;
 				}
 			i++;
@@ -178,7 +186,7 @@ int stats_closedb() {
 	for (i=0; i<tc; i++) {
 		db = dbs[i].db;
 		db->sync(db, 0);
-		db->close(db);
+		db->close(db, 0);
 		}
 	return 0;
 }
@@ -486,11 +494,13 @@ u_int extract_entries(data_base *dbp, stat_info **sia) {
 	void *ptr;
 	stat_unit su;
 	stkey_t key;
+	DBC *cursorp;
 
 	key.key = 0;
 	if (*sia != NULL) { free(*sia); *sia = NULL; }
+	dbp->db->cursor(dbp->db, 0, &cursorp, DB_CURSOR_BULK);
 	for (;;) {
-		er = next_entry(dbp, &key, &su);
+		er = next_entry(cursorp, &key, &su);
 		if (er != 0) break;
 		count++;
 		ptr = (void *) realloc(*sia, count*sizeof(stat_info));