return buffer;
}
+int db_getiflist(dbiflist **dbifl)
+{
+ int rc;
+ char sql[512];
+ static sqlite3_stmt *sqlstmt;
+
+ sqlite3_snprintf(512, sql, "select name from interface order by name desc;");
+
+ rc = sqlite3_prepare_v2(db, sql, -1, &sqlstmt, NULL);
+ if (rc) {
+ return 0;
+ }
+
+ rc = 0;
+ while (sqlite3_step(sqlstmt) == SQLITE_ROW) {
+ if (!dbiflistadd(dbifl, (const char *)sqlite3_column_text(sqlstmt, 0))) {
+ break;
+ }
+ rc++;
+ }
+
+ sqlite3_finalize(sqlstmt);
+
+ return rc;
+}
+
int db_addtraffic(const char *iface, const uint64_t rx, const uint64_t tx)
{
return db_addtraffic_dated(iface, rx, tx, 0);
}
return 1;
}
+
+int dbiflistadd(dbiflist **dbifl, const char *iface)
+{
+ dbiflist *newif;
+
+ newif = malloc(sizeof(dbiflist));
+ if (newif == NULL) {
+ return 0;
+ }
+
+ newif->next = *dbifl;
+ *dbifl = newif;
+ strncpy_nt(newif->interface, iface, 32);
+
+ return 1;
+}
+
+void dbiflistfree(dbiflist **dbifl)
+{
+ dbiflist *dbifl_prev;
+
+ while (*dbifl != NULL) {
+ dbifl_prev = *dbifl;
+ *dbifl = (*dbifl)->next;
+ free(dbifl_prev);
+ }
+}
#include <sqlite3.h>
+typedef struct dbiflist {
+ char interface[32];
+ struct dbiflist *next;
+} dbiflist;
+
int db_open(int createifnotfound);
int db_close(void);
int db_exec(const char *sql);
int db_setalias(const char *iface, const char *alias);
int db_setinfo(const char *name, const char *value, const int createifnotfound);
char *db_getinfo(const char *name);
+int db_getiflist(dbiflist **dbifl);
int db_addtraffic(const char *iface, const uint64_t rx, const uint64_t tx);
int db_addtraffic_dated(const char *iface, const uint64_t rx, const uint64_t tx, const uint64_t timestamp);
int db_removeoldentries(void);
int db_committransaction(void);
int db_rollbacktransaction(void);
+int dbiflistadd(dbiflist **dbifl, const char *iface);
+void dbiflistfree(dbiflist **dbifl);
+
/* global db */
sqlite3 *db;
}
END_TEST
+START_TEST(db_getiflist_lists_interfaces)
+{
+ int ret;
+ dbiflist *dbifl = NULL;
+
+ defaultcfg();
+ strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
+ ck_assert_int_eq(clean_testdbdir(), 1);
+
+ ret = db_open(1);
+ ck_assert_int_eq(ret, 1);
+ ret = db_addinterface("a");
+ ck_assert_int_eq(ret, 1);
+ ret = db_addinterface("b");
+ ck_assert_int_eq(ret, 1);
+ ret = db_addinterface("eth0");
+ ck_assert_int_eq(ret, 1);
+ ret = db_addinterface("eth1");
+ ck_assert_int_eq(ret, 1);
+
+ ret = db_getiflist(&dbifl);
+ ck_assert_int_eq(ret, 4);
+
+ ret = db_close();
+ ck_assert_int_eq(ret, 1);
+
+ ck_assert_str_eq(dbifl->interface, "a");
+ ck_assert_ptr_ne(dbifl->next, NULL);
+ dbifl = dbifl->next;
+
+ ck_assert_str_eq(dbifl->interface, "b");
+ ck_assert_ptr_ne(dbifl->next, NULL);
+ dbifl = dbifl->next;
+
+ ck_assert_str_eq(dbifl->interface, "eth0");
+ ck_assert_ptr_ne(dbifl->next, NULL);
+ dbifl = dbifl->next;
+
+ ck_assert_str_eq(dbifl->interface, "eth1");
+ ck_assert_ptr_eq(dbifl->next, NULL);
+
+ dbiflistfree(&dbifl);
+ ck_assert_ptr_eq(dbifl, NULL);
+}
+END_TEST
+
void add_dbsql_tests(Suite *s)
{
TCase *tc_dbsql = tcase_create("DB SQL");
tcase_add_test(tc_dbsql, db_getcounters_with_no_interface);
tcase_add_test(tc_dbsql, db_setcounters_with_no_interface);
tcase_add_test(tc_dbsql, db_counters_with_interface);
+ tcase_add_test(tc_dbsql, db_getiflist_lists_interfaces);
suite_add_tcase(s, tc_dbsql);
}