33 lines
791 B
C
33 lines
791 B
C
#include <pcap.h>
|
|
#include <string.h>
|
|
|
|
#include "host.h"
|
|
|
|
int get_interface_name(struct host *host)
|
|
{
|
|
pcap_if_t *alldevs;
|
|
char errbuf[PCAP_ERRBUF_SIZE];
|
|
|
|
if (pcap_findalldevs(&alldevs, errbuf) == -1) {
|
|
dprintf(2, "Error finding devices: %s\n", errbuf);
|
|
return -1;
|
|
}
|
|
|
|
for (pcap_if_t *it = alldevs; it != NULL; it = it->next) {
|
|
if (!(it->flags & PCAP_IF_LOOPBACK)) {
|
|
strcpy(host->interface, it->name);
|
|
for (pcap_addr_t *a = it->addresses; a != NULL; a = a->next) {
|
|
if (a->addr && a->addr->sa_family == AF_INET) { // Only IPv4
|
|
struct sockaddr_in *sa = (struct sockaddr_in *)a->addr;
|
|
strcpy(host->ip, inet_ntoa(sa->sin_addr));
|
|
pcap_freealldevs(alldevs);
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
dprintf(2, "No non-loopback interface found.\n");
|
|
return -1;
|
|
}
|