and apply actions to only it. For queries, it is possible to merge the
information of two or more interfaces using the
.I interface1+interface2+...
-syntax.
+syntax. All provided interfaces must be unique and must exist in the database
+when the merge syntax is used.
.TP
.B "--iflist"
instead of default or configured interface. It is also possible to merge the
information of two or more interfaces using the
.I interface1+interface2+...
-syntax.
+syntax. All provided interfaces must be unique and must exist in the database
+when the merge syntax is used.
.TP
.BI "--locale " locale
sqlite3_snprintf(512, sql, "select count(*) from interface");
}
} else {
- /* TODO: possibly verify all given interfaces exist in the database */
inquery = getifaceinquery(iface);
if (inquery == NULL) {
return 0;
}
sqlite3_finalize(sqlstmt);
+ /* consider merge query as invalid if not all requested interfaces are found or are not unique */
+ if (strchr(iface, '+') != NULL) {
+ if (result != getqueryinterfacecount(iface)) {
+ result = 0;
+ }
+ }
+
return result;
}
}
-char *getifaceinquery(const char *input)
+unsigned int getqueryinterfacecount(const char *input)
{
- unsigned int i, j, ifacecount = 1;
- char *result;
+ unsigned int i, ifacecount = 1;
if (input[0] == '+' || input[strlen(input)-1] == '+' || !strlen(input)) {
- return NULL;
+ return 0;
}
for (i = 0; i < (unsigned int)strlen(input); i++) {
if (input[i] == '+') {
if (i > 0 && input[i-1] == '+') {
- return NULL;
+ return 0;
} else {
ifacecount++;
}
}
}
+ return ifacecount;
+}
+
+char *getifaceinquery(const char *input)
+{
+ unsigned int i, j, ifacecount = 1;
+ char *result;
+
+ ifacecount = getqueryinterfacecount(input);
+
+ if (ifacecount == 0) {
+ return NULL;
+ }
+
/* each interface requires two quotes and comma or \0 so 3 extra chars */
j = (unsigned int)strlen(input) + ifacecount * 3;
result = malloc(sizeof(char) * j);
int dbdatalistadd(dbdatalist **dbdata, const uint64_t rx, const uint64_t tx, const time_t timestamp, const int64_t rowid);
void dbdatalistfree(dbdatalist **dbdata);
+unsigned int getqueryinterfacecount(const char *input);
char *getifaceinquery(const char *input);
/* global db */
if (strchr(p->interface, '+') == NULL) {
printf("Error: Interface \"%s\" not found in database.\n", p->interface);
} else {
- printf("Error: Not all requested interfaces found in database.\n");
+ printf("Error: Not all requested interfaces found in database or given interfaces aren't unique.\n");
}
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
if (!db_getinterfacecountbyname(p->interface)) {
- printf("Error: Interface \"%s\" not found in database.\n", p->interface);
+ if (strchr(p->interface, '+') == NULL) {
+ printf("Error: Interface \"%s\" not found in database.\n", p->interface);
+ } else {
+ printf("Error: Not all requested interfaces found in database or given interfaces aren't unique.\n");
+ }
exit(EXIT_FAILURE);
}
if (!db_getinterfaceinfo(p->interface, &ic->interface)) {
ck_assert_int_eq(ret, 3);
ret = (int)db_getinterfacecountbyname("eth0+eth1+eth2");
- ck_assert_int_eq(ret, 2);
+ ck_assert_int_eq(ret, 0);
ret = db_close();
ck_assert_int_eq(ret, 1);
}
END_TEST
+START_TEST(getqueryinterfacecount_can_count)
+{
+ ck_assert_int_eq(getqueryinterfacecount("eth0"), 1);
+ ck_assert_int_eq(getqueryinterfacecount("eth1"), 1);
+ ck_assert_int_eq(getqueryinterfacecount("eth1+eth2"), 2);
+ ck_assert_int_eq(getqueryinterfacecount("eth1+eth2+eth3"), 3);
+ ck_assert_int_eq(getqueryinterfacecount("eth1+eth2+eth3+eth1"), 4);
+ ck_assert_int_eq(getqueryinterfacecount("eth0+eth0"), 2);
+ ck_assert_int_eq(getqueryinterfacecount("eth0++eth1"), 0);
+ ck_assert_int_eq(getqueryinterfacecount(""), 0);
+ ck_assert_int_eq(getqueryinterfacecount("1"), 1);
+ ck_assert_int_eq(getqueryinterfacecount("+"), 0);
+ ck_assert_int_eq(getqueryinterfacecount("++"), 0);
+ ck_assert_int_eq(getqueryinterfacecount("+ +"), 0);
+ ck_assert_int_eq(getqueryinterfacecount("+ethsomething"), 0);
+ ck_assert_int_eq(getqueryinterfacecount("ethnothing+"), 0);
+ ck_assert_int_eq(getqueryinterfacecount("eth+nothing"), 2);
+ ck_assert_int_eq(getqueryinterfacecount("ethlongcanbelong+ethnotsoshort+ethdoesnotcare"), 3);
+}
+END_TEST
+
void add_dbsql_tests(Suite *s)
{
TCase *tc_dbsql = tcase_create("DB SQL");
tcase_add_test(tc_dbsql, db_getinterfaceidin_can_handle_error_situations);
tcase_add_test(tc_dbsql, db_getinterfaceinfo_can_handle_interface_merges);
tcase_add_test(tc_dbsql, db_getinterfaceinfo_can_handle_invalid_input);
+ tcase_add_test(tc_dbsql, getqueryinterfacecount_can_count);
suite_add_tcase(s, tc_dbsql);
}