mirror of
https://github.com/mfontanini/libtins
synced 2026-01-24 19:21:35 +01:00
Added Utils::interface_info.
This commit is contained in:
@@ -49,6 +49,7 @@ struct InterfaceCollector {
|
||||
}
|
||||
};
|
||||
|
||||
/** \cond */
|
||||
struct IPv4Collector {
|
||||
uint32_t ip;
|
||||
bool found;
|
||||
@@ -64,6 +65,7 @@ struct IPv4Collector {
|
||||
}
|
||||
};
|
||||
|
||||
/** \cond */
|
||||
struct HWAddressCollector {
|
||||
uint8_t *result;
|
||||
bool found;
|
||||
@@ -79,6 +81,30 @@ struct HWAddressCollector {
|
||||
}
|
||||
};
|
||||
|
||||
/** \cond */
|
||||
struct InterfaceInfoCollector {
|
||||
Tins::Utils::InterfaceInfo *info;
|
||||
const char *iface;
|
||||
bool found;
|
||||
|
||||
InterfaceInfoCollector(Tins::Utils::InterfaceInfo *res, const char *interface) :
|
||||
info(res), iface(interface), found(false) { }
|
||||
|
||||
void operator() (struct ifaddrs *addr) {
|
||||
if(addr->ifa_addr->sa_family == AF_PACKET && !strcmp(addr->ifa_name, iface))
|
||||
memcpy(info->hw_addr, ((struct sockaddr_ll*)addr->ifa_addr)->sll_addr, sizeof(info->hw_addr));
|
||||
else if(addr->ifa_addr->sa_family == AF_INET && !strcmp(addr->ifa_name, iface)) {
|
||||
info->ip_addr = ((struct sockaddr_in *)addr->ifa_addr)->sin_addr.s_addr;
|
||||
info->netmask = ((struct sockaddr_in *)addr->ifa_netmask)->sin_addr.s_addr;
|
||||
if((addr->ifa_flags & (IFF_BROADCAST | IFF_POINTOPOINT)))
|
||||
info->bcast_addr = ((struct sockaddr_in *)addr->ifa_ifu.ifu_broadaddr)->sin_addr.s_addr;
|
||||
else
|
||||
info->bcast_addr = 0;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool from_hex(const string &str, uint32_t &result) {
|
||||
unsigned i(0);
|
||||
result = 0;
|
||||
@@ -284,6 +310,15 @@ bool Tins::Utils::interface_hwaddr(const string &iface, uint8_t *buffer) {
|
||||
return collector.found;
|
||||
}
|
||||
|
||||
bool Tins::Utils::interface_info(const string &iface, InterfaceInfo &info) {
|
||||
InterfaceInfoCollector collector(&info, iface.c_str());
|
||||
generic_iface_loop(collector);
|
||||
info.ip_addr = net_to_host_l(info.ip_addr);
|
||||
info.netmask = net_to_host_l(info.netmask);
|
||||
info.bcast_addr = net_to_host_l(info.bcast_addr);
|
||||
return collector.found;
|
||||
}
|
||||
|
||||
bool Tins::Utils::interface_id(const string &iface, uint32_t &id) {
|
||||
id = if_nametoindex(iface.c_str());
|
||||
return (((int32_t)id) != -1);
|
||||
|
||||
Reference in New Issue
Block a user