From b8009a0640da8146600b5b5826997a79edf0c87c Mon Sep 17 00:00:00 2001 From: Kaian Date: Thu, 28 Jan 2016 20:51:49 +0100 Subject: [PATCH] Refactor local address detection --- src/address.c | 54 +++++++++++++++++++++++++++++++++++++++ src/address.h | 12 ++++++--- src/capture.c | 29 --------------------- src/capture.h | 11 -------- src/curses/ui_call_flow.c | 8 +++--- src/curses/ui_call_flow.h | 2 +- 6 files changed, 69 insertions(+), 47 deletions(-) diff --git a/src/address.c b/src/address.c index 955d4c1..582f8bd 100644 --- a/src/address.c +++ b/src/address.c @@ -30,6 +30,10 @@ #include "config.h" #include "address.h" #include +#include +#include +#include +#include bool addressport_equals(address_t addr1, address_t addr2) @@ -42,3 +46,53 @@ address_equals(address_t addr1, address_t addr2) { return !strcmp(addr1.ip, addr2.ip); } + +bool +address_is_local(address_t addr) +{ + //! Local devices pointer + static pcap_if_t *devices = 0; + pcap_if_t *dev; + pcap_addr_t *da; + char errbuf[PCAP_ERRBUF_SIZE]; + struct sockaddr_in *ipaddr; +#ifdef USE_IPV6 + struct sockaddr_in6 *ip6addr; +#endif + char ip[ADDRESSLEN]; + + // Get all network devices + if (!devices) { + // Get Local devices addresses + pcap_findalldevs(&devices, errbuf); + } + + for (dev = devices; dev; dev = dev->next) { + for (da = dev->addresses; da ; da = da->next) { + // Ingore empty addresses + if (!da->addr) + continue; + + // Get address representation + switch (da->addr->sa_family) { + case AF_INET: + ipaddr = (struct sockaddr_in *) da->addr; + inet_ntop(AF_INET, &ipaddr->sin_addr, ip, sizeof(ip)); + break; +#ifdef USE_IPV6 + case AF_INET6: + ip6addr = (struct sockaddr_in6 *) da->addr; + inet_ntop(AF_INET, &ip6addr->sin6_addr, ip, sizeof(ip)); + break; +#endif + } + + // Check if this address matches + if (!strcmp(addr.ip, ip)) { + return true; + } + + } + } + return false; +} diff --git a/src/address.h b/src/address.h index 47abe01..51579eb 100644 --- a/src/address.h +++ b/src/address.h @@ -48,9 +48,6 @@ #define ADDRESSLEN INET_ADDRSTRLEN #endif -//! Address string Length (including colons and port) -#define ADDRESSPORTLEN ADDRESSLEN + 6 - //! Shorter declaration of address structure typedef struct address address_t; @@ -84,4 +81,13 @@ addressport_equals(address_t addr1, address_t addr2); bool address_equals(address_t addr1, address_t addr2); +/** + * @brief Check if a given IP address belongs to a local device + * + * @param address Address structure + * @return true if address is local, false otherwise + */ +bool +address_is_local(address_t addr); + #endif /* __SNGREP_ADDRESS_H */ diff --git a/src/capture.c b/src/capture.c index cce0843..294eceb 100644 --- a/src/capture.c +++ b/src/capture.c @@ -142,9 +142,6 @@ capture_online(const char *dev, const char *outfile) return 3; } - // Get Local devices addresses - pcap_findalldevs(&capture_cfg.devices, errbuf); - // Add this capture information as packet source vector_append(capture_cfg.sources, capinfo); @@ -972,29 +969,3 @@ dump_close(pcap_dumper_t *pd) return; pcap_dump_close(pd); } - -int -is_local_address_str(address_t addr) -{ - char straddress[ADDRESSLEN], *end; - strcpy(straddress, addr.ip); - // If address comes with port, remove it - if ((end = strchr(straddress, ':'))) - *end = '\0'; - return is_local_address(inet_addr(straddress)); -} - -int -is_local_address(in_addr_t address) -{ - pcap_if_t *device; - pcap_addr_t *dev_addr; - - for (device = capture_cfg.devices; device; device = device->next) { - for (dev_addr = device->addresses; dev_addr; dev_addr = dev_addr->next) - if (dev_addr->addr && dev_addr->addr->sa_family == AF_INET - && ((struct sockaddr_in*) dev_addr->addr)->sin_addr.s_addr == address) - return 1; - } - return 0; -} diff --git a/src/capture.h b/src/capture.h index d090d29..7bb1698 100644 --- a/src/capture.h +++ b/src/capture.h @@ -122,8 +122,6 @@ struct capture_config { struct bpf_program fp; //! libpcap dump file handler pcap_dumper_t *pd; - //! Local devices pointer - pcap_if_t *devices; //! Capture sources vector_t *sources; //! Packets pending IP reassembly @@ -424,15 +422,6 @@ dump_packet(pcap_dumper_t *pd, const packet_t *packet); void dump_close(pcap_dumper_t *pd); -/** - * @brief Check if a given address belongs to a local device - * - * @param address IPv4 string format for address - * @return 1 if address is local, 0 otherwise - */ -int -is_local_address_str(address_t addr); - /** * @brief Check if a given address belongs to a local device * diff --git a/src/curses/ui_call_flow.c b/src/curses/ui_call_flow.c index 6a523f5..739230d 100644 --- a/src/curses/ui_call_flow.c +++ b/src/curses/ui_call_flow.c @@ -286,7 +286,7 @@ call_flow_draw_columns(PANEL *panel) vector_iter_t streams; vector_iter_t columns; int flow_height, flow_width; - char coltext[ADDRESSPORTLEN]; + char coltext[MAX_SETTING_LEN]; address_t addr; // Get panel information @@ -329,8 +329,10 @@ call_flow_draw_columns(PANEL *panel) mvwaddch(win, 3, 20 + 30 * column->colpos, ACS_TTEE); // Set bold to this address if it's local - if (is_local_address_str(column->addr) && setting_enabled(SETTING_CF_LOCALHIGHLIGHT)) - wattron(win, A_BOLD); + if (setting_enabled(SETTING_CF_LOCALHIGHLIGHT)) { + if (address_is_local(column->addr)) + wattron(win, A_BOLD); + } if (setting_enabled(SETTING_CF_SPLITCALLID)) { sprintf(coltext, "%s", column->alias); diff --git a/src/curses/ui_call_flow.h b/src/curses/ui_call_flow.h index 479b2a5..7d323e4 100644 --- a/src/curses/ui_call_flow.h +++ b/src/curses/ui_call_flow.h @@ -110,7 +110,7 @@ struct call_flow_column { //! Address header for this column address_t addr; //! Alias for the given address - char alias[ADDRESSPORTLEN]; + char alias[MAX_SETTING_LEN]; //! Call Ids const char *callid; const char *callid2;