forked from Mirrors/sngrep
Refactor local address detection
This commit is contained in:
parent
cdeda05b8a
commit
b8009a0640
|
@ -30,6 +30,10 @@
|
|||
#include "config.h"
|
||||
#include "address.h"
|
||||
#include <string.h>
|
||||
#include <pcap.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue