mirror of
https://github.com/mfontanini/libtins
synced 2026-01-27 12:14:26 +01:00
Documented and fixed some bugs in IPv4Address.
This commit is contained in:
@@ -265,7 +265,7 @@ bool DHCP::generic_search(Options opt, uint32_t *value) {
|
||||
bool DHCP::generic_search(Options opt, ipaddress_type *value) {
|
||||
uint32_t ip_int;
|
||||
if(generic_search(opt, &ip_int)) {
|
||||
*value = ip_int;
|
||||
*value = IPv4Address(Utils::host_to_be(ip_int));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -40,22 +40,20 @@ IPv4Address::IPv4Address(const std::string &ip)
|
||||
|
||||
}
|
||||
|
||||
IPv4Address &IPv4Address::operator=(uint32_t ip) {
|
||||
ip_addr = ip;
|
||||
return *this;
|
||||
}
|
||||
|
||||
IPv4Address &Tins::IPv4Address::operator=(const string &ip) {
|
||||
ip_addr = ip_to_int(ip);
|
||||
return *this;
|
||||
}
|
||||
|
||||
IPv4Address::operator uint32_t() const {
|
||||
return Utils::host_to_be(ip_addr);
|
||||
}
|
||||
|
||||
std::string IPv4Address::to_string() const {
|
||||
return Utils::ip_to_string(ip_addr);
|
||||
std::ostringstream oss;
|
||||
int mask(24);
|
||||
while(mask >=0) {
|
||||
oss << ((ip_addr >> mask) & 0xff);
|
||||
if(mask)
|
||||
oss << '.';
|
||||
mask -= 8;
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
uint32_t IPv4Address::ip_to_int(const string &ip) {
|
||||
@@ -81,4 +79,8 @@ uint32_t IPv4Address::ip_to_int(const string &ip) {
|
||||
throw std::runtime_error("Invalid ip address");
|
||||
return result;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &output, const IPv4Address &addr) {
|
||||
return output << addr.to_string();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,15 +42,16 @@ struct InterfaceInfoCollector {
|
||||
|
||||
bool operator() (struct ifaddrs *addr) {
|
||||
using Tins::Utils::host_to_be;
|
||||
using Tins::IPv4Address;
|
||||
const struct sockaddr_ll* addr_ptr = ((struct sockaddr_ll*)addr->ifa_addr);
|
||||
|
||||
if(addr->ifa_addr->sa_family == AF_PACKET && addr_ptr->sll_ifindex == iface_id)
|
||||
info->hw_addr = addr_ptr->sll_addr;
|
||||
else if(addr->ifa_addr->sa_family == AF_INET && !std::strcmp(addr->ifa_name, iface_name)) {
|
||||
info->ip_addr = host_to_be(((struct sockaddr_in *)addr->ifa_addr)->sin_addr.s_addr);
|
||||
info->netmask = host_to_be(((struct sockaddr_in *)addr->ifa_netmask)->sin_addr.s_addr);
|
||||
info->ip_addr = IPv4Address(((struct sockaddr_in *)addr->ifa_addr)->sin_addr.s_addr);
|
||||
info->netmask = IPv4Address(((struct sockaddr_in *)addr->ifa_netmask)->sin_addr.s_addr);
|
||||
if((addr->ifa_flags & (IFF_BROADCAST | IFF_POINTOPOINT)))
|
||||
info->bcast_addr = host_to_be(((struct sockaddr_in *)addr->ifa_ifu.ifu_broadaddr)->sin_addr.s_addr);
|
||||
info->bcast_addr = IPv4Address(((struct sockaddr_in *)addr->ifa_ifu.ifu_broadaddr)->sin_addr.s_addr);
|
||||
else
|
||||
info->bcast_addr = 0;
|
||||
found = true;
|
||||
|
||||
Reference in New Issue
Block a user