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));
