From 507df1c157ec254dbdf8c7513e7b7b699d96da4e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 2 Jan 2023 20:37:50 +0100 Subject: [PATCH] Move /api/network to /api/ftl/network Signed-off-by: DL6ER --- src/api/CMakeLists.txt | 3 +- src/api/api.c | 9 +- src/api/api.h | 2 +- src/api/ftl.c | 142 --------------------------- src/api/ftl/config.c | 180 +++++++++++++++++++++++++++++++++++ src/api/{ => ftl}/network.c | 31 +++--- src/database/network-table.c | 16 ++-- src/database/network-table.h | 4 +- 8 files changed, 218 insertions(+), 169 deletions(-) create mode 100644 src/api/ftl/config.c rename src/api/{ => ftl}/network.c (80%) diff --git a/src/api/CMakeLists.txt b/src/api/CMakeLists.txt index f56b470e..98a96acd 100644 --- a/src/api/CMakeLists.txt +++ b/src/api/CMakeLists.txt @@ -15,9 +15,10 @@ set(sources dns.c dns.h ftl.c + ftl/config.c + ftl/network.c history.c list.c - network.c queries.c settings.c stats_database.c diff --git a/src/api/api.c b/src/api/api.c index 0e09922a..3506d296 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -16,7 +16,7 @@ #include "api.h" #include "../shmem.h" -static int api_endpoints(struct ftl_conn *api); +static int api_ftl_endpoints(struct ftl_conn *api); static struct { const char *uri; @@ -38,8 +38,8 @@ static struct { { "/api/ftl/config", api_ftl_config, { false, false } }, { "/api/ftl/gateway", api_ftl_gateway, { false, false } }, { "/api/ftl/interfaces", api_ftl_interfaces, { false, false } }, - { "/api/ftl/endpoints", api_endpoints, { false, false } }, - { "/api/network", api_network, { false, false } }, + { "/api/ftl/network", api_ftl_network, { false, false } }, + { "/api/ftl/endpoints", api_ftl_endpoints, { false, false } }, { "/api/history/clients", api_history_clients, { false, false } }, { "/api/history", api_history, { false, false } }, { "/api/queries/suggestions", api_queries_suggestions, { false, false } }, @@ -65,7 +65,6 @@ static struct { { "/api/settings/web", api_settings_web, { false, false } }, { "/api/docs", api_docs, { false, false } }, }; -#define API_ENDPOINTS "/api/endpoints" int api_handler(struct mg_connection *conn, void *ignored) { @@ -127,7 +126,7 @@ int api_handler(struct mg_connection *conn, void *ignored) return ret; } -static int api_endpoints(struct ftl_conn *api) +static int api_ftl_endpoints(struct ftl_conn *api) { // Verify requesting client is allowed to see this ressource if(check_client_auth(api) == API_AUTH_UNAUTHORIZED) diff --git a/src/api/api.h b/src/api/api.h index 04d94c98..ac44b5a1 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -53,9 +53,9 @@ int get_system_obj(struct ftl_conn *api, cJSON *system); int api_ftl_config(struct ftl_conn *api); int api_ftl_gateway(struct ftl_conn *api); int api_ftl_interfaces(struct ftl_conn *api); +int api_ftl_network(struct ftl_conn *api); // Network methods -int api_network(struct ftl_conn *api); // DNS methods int api_dns_blocking(struct ftl_conn *api); diff --git a/src/api/ftl.c b/src/api/ftl.c index ee8dbbbc..9c228abc 100644 --- a/src/api/ftl.c +++ b/src/api/ftl.c @@ -499,148 +499,6 @@ int api_ftl_sysinfo(struct ftl_conn *api) JSON_SEND_OBJECT(json); } -int api_ftl_config(struct ftl_conn *api) -{ - // Verify requesting client is allowed to see this ressource - if(check_client_auth(api) == API_AUTH_UNAUTHORIZED) - { - return send_json_unauthorized(api); - } - - cJSON *config_j = JSON_NEW_OBJECT(); - JSON_ADD_NUMBER_TO_OBJECT(config_j, "debug",config.debug); /* TODO: Split into individual fields */ - - cJSON *dns = JSON_NEW_OBJECT(); - JSON_ADD_BOOL_TO_OBJECT(dns, "CNAMEdeepInspect", config.dns.CNAMEdeepInspect); - JSON_ADD_BOOL_TO_OBJECT(dns, "blockESNI", config.dns.blockESNI); - JSON_ADD_BOOL_TO_OBJECT(dns, "EDNS0ECS", config.dns.EDNS0ECS); - JSON_ADD_BOOL_TO_OBJECT(dns, "ignoreLocalhost", config.dns.ignoreLocalhost); - JSON_ADD_BOOL_TO_OBJECT(dns, "showDNSSEC", config.dns.showDNSSEC); - JSON_ADD_BOOL_TO_OBJECT(dns, "analyzeAAAA", config.dns.analyzeAAAA); - JSON_ADD_BOOL_TO_OBJECT(dns, "analyzeOnlyAandAAAA", config.dns.analyzeOnlyAandAAAA); - const char *piholePTR = get_ptr_type_str(config.dns.piholePTR); - JSON_REF_STR_IN_OBJECT(dns, "piholePTR", piholePTR); - const char *replyWhenBusy = get_busy_reply_str(config.dns.replyWhenBusy); - JSON_REF_STR_IN_OBJECT(dns, "replyWhenBusy", replyWhenBusy); - JSON_ADD_NUMBER_TO_OBJECT(dns, "blockTTL", config.dns.blockTTL); - const char *blockingmode = get_blocking_mode_str(config.dns.blockingmode); - JSON_REF_STR_IN_OBJECT(dns, "blockingmode", blockingmode); - JSON_ADD_NUMBER_TO_OBJECT(dns, "port", config.dns.port); - cJSON *specialDomains = JSON_NEW_OBJECT(); - JSON_ADD_BOOL_TO_OBJECT(specialDomains, "mozillaCanary", config.dns.specialDomains.mozillaCanary); - JSON_ADD_BOOL_TO_OBJECT(specialDomains, "iCloudPrivateRelay", config.dns.specialDomains.iCloudPrivateRelay); - JSON_ADD_ITEM_TO_OBJECT(dns, "specialDomains", specialDomains); - cJSON *reply = JSON_NEW_OBJECT(); - cJSON *host = JSON_NEW_OBJECT(); - { - if(config.dns.reply.host.overwrite_v4) - { - JSON_COPY_STR_TO_OBJECT(host, "IPv4", inet_ntoa(config.dns.reply.host.v4)); - } - else - { - JSON_ADD_NULL_TO_OBJECT(host, "IPv4"); - } - char ip6[INET6_ADDRSTRLEN] = { 0 }; - if(config.dns.reply.host.overwrite_v6) - { - JSON_COPY_STR_TO_OBJECT(host, "IPv6", inet_ntop(AF_INET6, &config.dns.reply.host.v6, ip6, INET6_ADDRSTRLEN)); - } - else - { - JSON_ADD_NULL_TO_OBJECT(host, "IPv6"); - } - } - JSON_ADD_ITEM_TO_OBJECT(reply, "host", host); - cJSON *blocking = JSON_NEW_OBJECT(); - { - if(config.dns.reply.blocking.overwrite_v4) - { - JSON_COPY_STR_TO_OBJECT(blocking, "IPv4", inet_ntoa(config.dns.reply.blocking.v4)); - } - else - { - JSON_ADD_NULL_TO_OBJECT(blocking, "IPv4"); - } - char ip6[INET6_ADDRSTRLEN] = { 0 }; - if(config.dns.reply.blocking.overwrite_v6) - { - JSON_COPY_STR_TO_OBJECT(blocking, "IPv6", inet_ntop(AF_INET6, &config.dns.reply.blocking.v6, ip6, INET6_ADDRSTRLEN)); - } - else - { - JSON_ADD_NULL_TO_OBJECT(blocking, "IPv6"); - } - } - JSON_ADD_ITEM_TO_OBJECT(reply, "blocking", blocking); - JSON_ADD_ITEM_TO_OBJECT(dns, "reply", reply); - cJSON *rateLimit = JSON_NEW_OBJECT(); - JSON_ADD_NUMBER_TO_OBJECT(rateLimit, "count", config.dns.rateLimit.count); - JSON_ADD_NUMBER_TO_OBJECT(rateLimit, "interval", config.dns.rateLimit.interval); - JSON_ADD_ITEM_TO_OBJECT(dns, "rateLimit", rateLimit); - JSON_ADD_ITEM_TO_OBJECT(config_j, "dns", dns); - - cJSON *resolver = JSON_NEW_OBJECT(); - JSON_ADD_BOOL_TO_OBJECT(resolver, "resolveIPv4", config.resolver.resolveIPv4); - JSON_ADD_BOOL_TO_OBJECT(resolver, "resolveIPv6", config.resolver.resolveIPv6); - JSON_ADD_BOOL_TO_OBJECT(resolver, "networkNames", config.resolver.networkNames); - const char *refreshstr = get_refresh_hostnames_str(config.resolver.refreshNames); - JSON_REF_STR_IN_OBJECT(resolver, "refreshNames", refreshstr); - JSON_ADD_ITEM_TO_OBJECT(config_j, "resolver", resolver); - - cJSON *database = JSON_NEW_OBJECT(); - JSON_ADD_BOOL_TO_OBJECT(database, "DBimport", config.database.DBimport); - JSON_ADD_BOOL_TO_OBJECT(database, "DBexport", config.database.DBexport); - JSON_ADD_NUMBER_TO_OBJECT(database, "maxHistory", config.database.maxHistory); - JSON_ADD_NUMBER_TO_OBJECT(database, "maxDBdays", config.database.maxDBdays); - JSON_ADD_NUMBER_TO_OBJECT(database, "DBinterval", config.database.DBinterval); - cJSON *network = JSON_NEW_OBJECT(); - JSON_ADD_BOOL_TO_OBJECT(network, "parseARPcache", config.database.network.parseARPcache); - JSON_ADD_NUMBER_TO_OBJECT(network, "expire", config.database.network.expire); - JSON_ADD_ITEM_TO_OBJECT(database, "network", network); - JSON_ADD_ITEM_TO_OBJECT(config_j, "database", database); - - cJSON *http = JSON_NEW_OBJECT(); - JSON_ADD_BOOL_TO_OBJECT(http, "localAPIauth", config.http.localAPIauth); - JSON_ADD_BOOL_TO_OBJECT(http, "prettyJSON", config.http.prettyJSON); - JSON_ADD_NUMBER_TO_OBJECT(http, "sessionTimeout", config.http.sessionTimeout); - JSON_REF_STR_IN_OBJECT(http, "domain", config.http.domain); - JSON_REF_STR_IN_OBJECT(http, "acl", config.http.acl); - JSON_REF_STR_IN_OBJECT(http, "port", config.http.port); - cJSON *paths = JSON_NEW_OBJECT(); - JSON_REF_STR_IN_OBJECT(paths, "webroot", config.http.paths.webroot); - JSON_REF_STR_IN_OBJECT(paths, "webhome", config.http.paths.webhome); - JSON_ADD_ITEM_TO_OBJECT(http, "paths", paths); - JSON_ADD_ITEM_TO_OBJECT(config_j, "http", http); - - cJSON *files = JSON_NEW_OBJECT(); - JSON_REF_STR_IN_OBJECT(files, "log", config.files.log); - JSON_REF_STR_IN_OBJECT(files, "pid", config.files.pid); - JSON_REF_STR_IN_OBJECT(files, "database", config.files.database); - JSON_REF_STR_IN_OBJECT(files, "gravity", config.files.gravity); - JSON_REF_STR_IN_OBJECT(files, "macvendor", config.files.macvendor); - JSON_REF_STR_IN_OBJECT(files, "setupVars", config.files.setupVars); - JSON_REF_STR_IN_OBJECT(files, "http_info", config.files.http_info); - JSON_REF_STR_IN_OBJECT(files, "ph7_error", config.files.ph7_error); - JSON_ADD_ITEM_TO_OBJECT(config_j, "files", files); - - cJSON *misc = JSON_NEW_OBJECT(); - JSON_ADD_NUMBER_TO_OBJECT(misc, "nice", config.misc.nice); - JSON_ADD_NUMBER_TO_OBJECT(misc, "delay_startup", config.misc.delay_startup); - JSON_ADD_BOOL_TO_OBJECT(misc, "addr2line", config.misc.addr2line); - JSON_ADD_NUMBER_TO_OBJECT(misc, "privacylevel", config.misc.privacylevel); - cJSON *check = JSON_NEW_OBJECT(); - JSON_ADD_BOOL_TO_OBJECT(check, "load", config.misc.check.load); - JSON_ADD_NUMBER_TO_OBJECT(check, "shmem", config.misc.check.shmem); - JSON_ADD_NUMBER_TO_OBJECT(check, "disk", config.misc.check.disk); - JSON_ADD_ITEM_TO_OBJECT(misc, "check", check); - JSON_ADD_ITEM_TO_OBJECT(config_j, "misc", misc); - - cJSON *json = JSON_NEW_OBJECT(); - JSON_ADD_ITEM_TO_OBJECT(json, "config", config_j); - JSON_SEND_OBJECT(json); -} - static bool getDefaultInterface(char iface[IF_NAMESIZE], in_addr_t *gw) { // Get IPv4 default route gateway and associated interface diff --git a/src/api/ftl/config.c b/src/api/ftl/config.c new file mode 100644 index 00000000..b409b124 --- /dev/null +++ b/src/api/ftl/config.c @@ -0,0 +1,180 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2019 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* API Implementation /api/ftl +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "FTL.h" +#include "webserver/http-common.h" +#include "webserver/json_macros.h" +#include "api/api.h" +// struct fifologData +#include "fifo.h" +// sysinfo() +#include +// get_blockingstatus() +#include "setupVars.h" +// counters +#include "shmem.h" +// get_FTL_db_filesize() +#include "files.h" +// get_sqlite3_version() +#include "database/common.h" +// get_number_of_queries_in_DB() +#include "database/query-table.h" +// getgrgid() +#include +// config struct +#include "config/config.h" +// struct clientsData +#include "datastructure.h" +// Routing information and flags +#include +// Interate through directories +#include + +int api_ftl_config(struct ftl_conn *api) +{ + // Verify requesting client is allowed to see this ressource + if(check_client_auth(api) == API_AUTH_UNAUTHORIZED) + { + return send_json_unauthorized(api); + } + + cJSON *config_j = JSON_NEW_OBJECT(); + JSON_ADD_NUMBER_TO_OBJECT(config_j, "debug",config.debug); /* TODO: Split into individual fields */ + + cJSON *dns = JSON_NEW_OBJECT(); + JSON_ADD_BOOL_TO_OBJECT(dns, "CNAMEdeepInspect", config.dns.CNAMEdeepInspect); + JSON_ADD_BOOL_TO_OBJECT(dns, "blockESNI", config.dns.blockESNI); + JSON_ADD_BOOL_TO_OBJECT(dns, "EDNS0ECS", config.dns.EDNS0ECS); + JSON_ADD_BOOL_TO_OBJECT(dns, "ignoreLocalhost", config.dns.ignoreLocalhost); + JSON_ADD_BOOL_TO_OBJECT(dns, "showDNSSEC", config.dns.showDNSSEC); + JSON_ADD_BOOL_TO_OBJECT(dns, "analyzeAAAA", config.dns.analyzeAAAA); + JSON_ADD_BOOL_TO_OBJECT(dns, "analyzeOnlyAandAAAA", config.dns.analyzeOnlyAandAAAA); + const char *piholePTR = get_ptr_type_str(config.dns.piholePTR); + JSON_REF_STR_IN_OBJECT(dns, "piholePTR", piholePTR); + const char *replyWhenBusy = get_busy_reply_str(config.dns.replyWhenBusy); + JSON_REF_STR_IN_OBJECT(dns, "replyWhenBusy", replyWhenBusy); + JSON_ADD_NUMBER_TO_OBJECT(dns, "blockTTL", config.dns.blockTTL); + const char *blockingmode = get_blocking_mode_str(config.dns.blockingmode); + JSON_REF_STR_IN_OBJECT(dns, "blockingmode", blockingmode); + JSON_ADD_NUMBER_TO_OBJECT(dns, "port", config.dns.port); + cJSON *specialDomains = JSON_NEW_OBJECT(); + JSON_ADD_BOOL_TO_OBJECT(specialDomains, "mozillaCanary", config.dns.specialDomains.mozillaCanary); + JSON_ADD_BOOL_TO_OBJECT(specialDomains, "iCloudPrivateRelay", config.dns.specialDomains.iCloudPrivateRelay); + JSON_ADD_ITEM_TO_OBJECT(dns, "specialDomains", specialDomains); + cJSON *reply = JSON_NEW_OBJECT(); + cJSON *host = JSON_NEW_OBJECT(); + { + if(config.dns.reply.host.overwrite_v4) + { + JSON_COPY_STR_TO_OBJECT(host, "IPv4", inet_ntoa(config.dns.reply.host.v4)); + } + else + { + JSON_ADD_NULL_TO_OBJECT(host, "IPv4"); + } + char ip6[INET6_ADDRSTRLEN] = { 0 }; + if(config.dns.reply.host.overwrite_v6) + { + JSON_COPY_STR_TO_OBJECT(host, "IPv6", inet_ntop(AF_INET6, &config.dns.reply.host.v6, ip6, INET6_ADDRSTRLEN)); + } + else + { + JSON_ADD_NULL_TO_OBJECT(host, "IPv6"); + } + } + JSON_ADD_ITEM_TO_OBJECT(reply, "host", host); + cJSON *blocking = JSON_NEW_OBJECT(); + { + if(config.dns.reply.blocking.overwrite_v4) + { + JSON_COPY_STR_TO_OBJECT(blocking, "IPv4", inet_ntoa(config.dns.reply.blocking.v4)); + } + else + { + JSON_ADD_NULL_TO_OBJECT(blocking, "IPv4"); + } + char ip6[INET6_ADDRSTRLEN] = { 0 }; + if(config.dns.reply.blocking.overwrite_v6) + { + JSON_COPY_STR_TO_OBJECT(blocking, "IPv6", inet_ntop(AF_INET6, &config.dns.reply.blocking.v6, ip6, INET6_ADDRSTRLEN)); + } + else + { + JSON_ADD_NULL_TO_OBJECT(blocking, "IPv6"); + } + } + JSON_ADD_ITEM_TO_OBJECT(reply, "blocking", blocking); + JSON_ADD_ITEM_TO_OBJECT(dns, "reply", reply); + cJSON *rateLimit = JSON_NEW_OBJECT(); + JSON_ADD_NUMBER_TO_OBJECT(rateLimit, "count", config.dns.rateLimit.count); + JSON_ADD_NUMBER_TO_OBJECT(rateLimit, "interval", config.dns.rateLimit.interval); + JSON_ADD_ITEM_TO_OBJECT(dns, "rateLimit", rateLimit); + JSON_ADD_ITEM_TO_OBJECT(config_j, "dns", dns); + + cJSON *resolver = JSON_NEW_OBJECT(); + JSON_ADD_BOOL_TO_OBJECT(resolver, "resolveIPv4", config.resolver.resolveIPv4); + JSON_ADD_BOOL_TO_OBJECT(resolver, "resolveIPv6", config.resolver.resolveIPv6); + JSON_ADD_BOOL_TO_OBJECT(resolver, "networkNames", config.resolver.networkNames); + const char *refreshstr = get_refresh_hostnames_str(config.resolver.refreshNames); + JSON_REF_STR_IN_OBJECT(resolver, "refreshNames", refreshstr); + JSON_ADD_ITEM_TO_OBJECT(config_j, "resolver", resolver); + + cJSON *database = JSON_NEW_OBJECT(); + JSON_ADD_BOOL_TO_OBJECT(database, "DBimport", config.database.DBimport); + JSON_ADD_BOOL_TO_OBJECT(database, "DBexport", config.database.DBexport); + JSON_ADD_NUMBER_TO_OBJECT(database, "maxHistory", config.database.maxHistory); + JSON_ADD_NUMBER_TO_OBJECT(database, "maxDBdays", config.database.maxDBdays); + JSON_ADD_NUMBER_TO_OBJECT(database, "DBinterval", config.database.DBinterval); + cJSON *network = JSON_NEW_OBJECT(); + JSON_ADD_BOOL_TO_OBJECT(network, "parseARPcache", config.database.network.parseARPcache); + JSON_ADD_NUMBER_TO_OBJECT(network, "expire", config.database.network.expire); + JSON_ADD_ITEM_TO_OBJECT(database, "network", network); + JSON_ADD_ITEM_TO_OBJECT(config_j, "database", database); + + cJSON *http = JSON_NEW_OBJECT(); + JSON_ADD_BOOL_TO_OBJECT(http, "localAPIauth", config.http.localAPIauth); + JSON_ADD_BOOL_TO_OBJECT(http, "prettyJSON", config.http.prettyJSON); + JSON_ADD_NUMBER_TO_OBJECT(http, "sessionTimeout", config.http.sessionTimeout); + JSON_REF_STR_IN_OBJECT(http, "domain", config.http.domain); + JSON_REF_STR_IN_OBJECT(http, "acl", config.http.acl); + JSON_REF_STR_IN_OBJECT(http, "port", config.http.port); + cJSON *paths = JSON_NEW_OBJECT(); + JSON_REF_STR_IN_OBJECT(paths, "webroot", config.http.paths.webroot); + JSON_REF_STR_IN_OBJECT(paths, "webhome", config.http.paths.webhome); + JSON_ADD_ITEM_TO_OBJECT(http, "paths", paths); + JSON_ADD_ITEM_TO_OBJECT(config_j, "http", http); + + cJSON *files = JSON_NEW_OBJECT(); + JSON_REF_STR_IN_OBJECT(files, "log", config.files.log); + JSON_REF_STR_IN_OBJECT(files, "pid", config.files.pid); + JSON_REF_STR_IN_OBJECT(files, "database", config.files.database); + JSON_REF_STR_IN_OBJECT(files, "gravity", config.files.gravity); + JSON_REF_STR_IN_OBJECT(files, "macvendor", config.files.macvendor); + JSON_REF_STR_IN_OBJECT(files, "setupVars", config.files.setupVars); + JSON_REF_STR_IN_OBJECT(files, "http_info", config.files.http_info); + JSON_REF_STR_IN_OBJECT(files, "ph7_error", config.files.ph7_error); + JSON_ADD_ITEM_TO_OBJECT(config_j, "files", files); + + cJSON *misc = JSON_NEW_OBJECT(); + JSON_ADD_NUMBER_TO_OBJECT(misc, "nice", config.misc.nice); + JSON_ADD_NUMBER_TO_OBJECT(misc, "delay_startup", config.misc.delay_startup); + JSON_ADD_BOOL_TO_OBJECT(misc, "addr2line", config.misc.addr2line); + JSON_ADD_NUMBER_TO_OBJECT(misc, "privacylevel", config.misc.privacylevel); + cJSON *check = JSON_NEW_OBJECT(); + JSON_ADD_BOOL_TO_OBJECT(check, "load", config.misc.check.load); + JSON_ADD_NUMBER_TO_OBJECT(check, "shmem", config.misc.check.shmem); + JSON_ADD_NUMBER_TO_OBJECT(check, "disk", config.misc.check.disk); + JSON_ADD_ITEM_TO_OBJECT(misc, "check", check); + JSON_ADD_ITEM_TO_OBJECT(config_j, "misc", misc); + + cJSON *json = JSON_NEW_OBJECT(); + JSON_ADD_ITEM_TO_OBJECT(json, "config", config_j); + JSON_SEND_OBJECT(json); +} diff --git a/src/api/network.c b/src/api/ftl/network.c similarity index 80% rename from src/api/network.c rename to src/api/ftl/network.c index cc969556..726a9f05 100644 --- a/src/api/network.c +++ b/src/api/ftl/network.c @@ -8,16 +8,16 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -#include "../FTL.h" -#include "../webserver/http-common.h" -#include "../webserver/json_macros.h" -#include "api.h" +#include "FTL.h" +#include "webserver/http-common.h" +#include "webserver/json_macros.h" +#include "api/api.h" // networkrecord -#include "../database/network-table.h" +#include "database/network-table.h" // dbopen() -#include "../database/common.h" +#include "database/common.h" -int api_network(struct ftl_conn *api) +int api_ftl_network(struct ftl_conn *api) { // Verify requesting client is allowed to see this ressource if(check_client_auth(api) == API_AUTH_UNAUTHORIZED) @@ -54,24 +54,31 @@ int api_network(struct ftl_conn *api) JSON_ADD_NUMBER_TO_OBJECT(item, "id", network.id); JSON_COPY_STR_TO_OBJECT(item, "hwaddr", network.hwaddr); JSON_COPY_STR_TO_OBJECT(item, "interface", network.iface); - JSON_COPY_STR_TO_OBJECT(item, "name", network.name); JSON_ADD_NUMBER_TO_OBJECT(item, "firstSeen", network.firstSeen); JSON_ADD_NUMBER_TO_OBJECT(item, "lastQuery", network.lastQuery); JSON_ADD_NUMBER_TO_OBJECT(item, "numQueries", network.numQueries); JSON_COPY_STR_TO_OBJECT(item, "macVendor", network.macVendor); // Build array of all IP addresses known associated to this client - cJSON *ip = JSON_NEW_ARRAY(); + cJSON *ips = JSON_NEW_ARRAY(); if(networkTable_readIPs(db, &ip_stmt, network.id, &sql_msg)) { - // Walk known IP addresses + // Walk known IP addresses + names network_addresses_record network_address; while(networkTable_readIPsGetRecord(ip_stmt, &network_address, &sql_msg)) - JSON_COPY_STR_TO_ARRAY(ip, network_address.ip); + { + cJSON *ip = JSON_NEW_OBJECT(); + JSON_COPY_STR_TO_OBJECT(ip, "ip", network_address.ip); + JSON_COPY_STR_TO_OBJECT(ip, "name", network_address.name); + JSON_ADD_NUMBER_TO_OBJECT(ip, "lastSeen", network_address.lastSeen); + JSON_ADD_NUMBER_TO_OBJECT(ip, "nameUpdated", network_address.nameUpdated); + JSON_ADD_ITEM_TO_ARRAY(ips, ip); + } // Possible error handling if(sql_msg != NULL) { + cJSON_Delete(ips); cJSON_Delete(json); return send_json_error(api, 500, "database_error", @@ -84,7 +91,7 @@ int api_network(struct ftl_conn *api) } // Add array of IP addresses to device - JSON_ADD_ITEM_TO_OBJECT(item, "ip", ip); + JSON_ADD_ITEM_TO_OBJECT(item, "ips", ips); // Add device to array of all devices JSON_ADD_ITEM_TO_ARRAY(json, item); diff --git a/src/database/network-table.c b/src/database/network-table.c index 8dbc44db..a5b864f5 100644 --- a/src/database/network-table.c +++ b/src/database/network-table.c @@ -2181,7 +2181,7 @@ char *__attribute__((malloc)) getIfaceFromIP(sqlite3 *db, const char *ipaddr) bool networkTable_readDevices(sqlite3 *db, sqlite3_stmt **read_stmt, const char **message) { // Prepare SQLite statement - const char *querystr = "SELECT id,hwaddr,interface,name,firstSeen,lastQuery,numQueries,macVendor FROM network;"; + const char *querystr = "SELECT id,hwaddr,interface,firstSeen,lastQuery,numQueries,macVendor FROM network;"; int rc = sqlite3_prepare_v2(db, querystr, -1, read_stmt, NULL); if( rc != SQLITE_OK ){ *message = sqlite3_errstr(rc); @@ -2205,11 +2205,10 @@ bool networkTable_readDevicesGetRecord(sqlite3_stmt *read_stmt, network_record * network->id = sqlite3_column_int(read_stmt, 0); network->hwaddr = (char*)sqlite3_column_text(read_stmt, 1); network->iface = (char*)sqlite3_column_text(read_stmt, 2); - network->name = (char*)sqlite3_column_text(read_stmt, 3); - network->firstSeen = sqlite3_column_int(read_stmt, 4); - network->lastQuery = sqlite3_column_int(read_stmt, 5); - network->numQueries = sqlite3_column_int(read_stmt, 6); - network->macVendor = (char*)sqlite3_column_text(read_stmt, 7); + network->firstSeen = sqlite3_column_int(read_stmt, 3); + network->lastQuery = sqlite3_column_int(read_stmt, 4); + network->numQueries = sqlite3_column_int(read_stmt, 5); + network->macVendor = (char*)sqlite3_column_text(read_stmt, 6); return true; } @@ -2238,7 +2237,7 @@ void networkTable_readDevicesFinalize(sqlite3_stmt *read_stmt) bool networkTable_readIPs(sqlite3 *db, sqlite3_stmt **read_stmt, const int id, const char **message) { // Prepare SQLite statement - const char *querystr = "SELECT ip FROM network_addresses WHERE network_id = ? ORDER BY lastSeen DESC;"; + const char *querystr = "SELECT ip,lastSeen,name,nameUpdated FROM network_addresses WHERE network_id = ? ORDER BY lastSeen DESC;"; int rc = sqlite3_prepare_v2(db, querystr, -1, read_stmt, NULL); if( rc != SQLITE_OK ){ *message = sqlite3_errstr(rc); @@ -2270,6 +2269,9 @@ bool networkTable_readIPsGetRecord(sqlite3_stmt *read_stmt, network_addresses_re if(rc == SQLITE_ROW) { network_addresses->ip = (char*)sqlite3_column_text(read_stmt, 0); + network_addresses->lastSeen = sqlite3_column_int64(read_stmt, 1); + network_addresses->name = (char*)sqlite3_column_text(read_stmt, 2); + network_addresses->nameUpdated = sqlite3_column_int64(read_stmt, 1); return true; } diff --git a/src/database/network-table.h b/src/database/network-table.h index 42280133..5fe0fafd 100644 --- a/src/database/network-table.h +++ b/src/database/network-table.h @@ -29,7 +29,6 @@ typedef struct { unsigned int id; const char *hwaddr; const char *iface; - const char *name; const char *macVendor; unsigned long numQueries; time_t firstSeen; @@ -42,6 +41,9 @@ void networkTable_readDevicesFinalize(sqlite3_stmt *read_stmt); typedef struct { const char *ip; + const char *name; + time_t lastSeen; + time_t nameUpdated; } network_addresses_record; bool networkTable_readIPs(sqlite3 *db, sqlite3_stmt **read_stmt, const int id, const char **message);