From: Teemu Toivola Date: Sat, 31 Dec 2016 23:25:54 +0000 (+0200) Subject: add function for getting interface list from database X-Git-Tag: v2.0~98^2~89 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c7a4295d5d1b37aed510d02c00784f3bad33dce6;p=vnstat add function for getting interface list from database --- diff --git a/src/dbsql.c b/src/dbsql.c index 141912b..9e12f78 100644 --- a/src/dbsql.c +++ b/src/dbsql.c @@ -357,6 +357,32 @@ char *db_getinfo(const char *name) 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); @@ -488,3 +514,30 @@ int db_rollbacktransaction(void) } 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); + } +} diff --git a/src/dbsql.h b/src/dbsql.h index cd15b06..50281b8 100644 --- a/src/dbsql.h +++ b/src/dbsql.h @@ -3,6 +3,11 @@ #include +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); @@ -17,6 +22,7 @@ int db_getcounters(const char *iface, uint64_t *rxcounter, uint64_t *txcounter); 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); @@ -25,6 +31,9 @@ int db_begintransaction(void); int db_committransaction(void); int db_rollbacktransaction(void); +int dbiflistadd(dbiflist **dbifl, const char *iface); +void dbiflistfree(dbiflist **dbifl); + /* global db */ sqlite3 *db; diff --git a/tests/dbsql_tests.c b/tests/dbsql_tests.c index 037f076..08ccdc7 100644 --- a/tests/dbsql_tests.c +++ b/tests/dbsql_tests.c @@ -519,6 +519,52 @@ START_TEST(db_counters_with_interface) } 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"); @@ -548,5 +594,6 @@ void add_dbsql_tests(Suite *s) 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); }