Skip to content

Commit

Permalink
fix(libsinsp): network address printing endianness
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Pittman <joseph.pittman@sysdig.com>
Co-Authored-By: Luca Guerra <luca@guerra.sh>
  • Loading branch information
LucaGuerra authored and poiana committed Jul 17, 2023
1 parent 6d6ab12 commit 140f9cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 14 additions & 9 deletions userspace/libsinsp/ifinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ sinsp_ipv4_ifinfo::sinsp_ipv4_ifinfo(uint32_t addr, uint32_t netmask, uint32_t b

void sinsp_ipv4_ifinfo::convert_to_string(char * dest, size_t len, const uint32_t addr)
{
uint32_t addr_network_byte_order = htonl(addr);
snprintf(
dest,
len,
"%d.%d.%d.%d",
(addr & 0xFF),
((addr & 0xFF00) >> 8),
((addr & 0xFF0000) >> 16),
((addr & 0xFF000000) >> 24));
((addr_network_byte_order & 0xFF000000) >> 24),
((addr_network_byte_order & 0xFF0000) >> 16),
((addr_network_byte_order & 0xFF00) >> 8),
(addr_network_byte_order & 0xFF));
}

std::string sinsp_ipv4_ifinfo::address() const
Expand Down Expand Up @@ -93,7 +94,7 @@ uint32_t sinsp_network_interfaces::infer_ipv4_address(uint32_t destination_addre
// otherwise take the first non loopback interface
for(it = m_ipv4_interfaces.begin(); it != m_ipv4_interfaces.end(); it++)
{
if(it->m_addr != LOOPBACK_ADDR)
if(it->m_addr != ntohl(INADDR_LOOPBACK))
{
return it->m_addr;
}
Expand Down Expand Up @@ -199,11 +200,15 @@ bool sinsp_network_interfaces::is_ipv4addr_in_subnet(uint32_t addr)
std::vector<sinsp_ipv4_ifinfo>::iterator it;

//
// Accept everything that comes from 192.168.0.0/16 or 10.0.0.0/8
// Accept everything that comes from private internets:
// - 10.0.0.0/8
// - 192.168.0.0/16
// - 172.16.0.0/12
//
if((addr & 0x000000ff) == 0x0000000a ||
(addr & 0x0000ffff) == 0x0000a8c0 ||
(addr & 0x00003fff) == 0x000010ac)
uint32_t addr_network_byte_order = htonl(addr);
if((addr_network_byte_order & 0xff000000) == 0x0a000000 ||
(addr_network_byte_order & 0xffff0000) == 0xc0a80000 ||
(addr_network_byte_order & 0xff3f0000) == 0xac100000)
{
return true;
}
Expand Down
4 changes: 1 addition & 3 deletions userspace/libsinsp/ifinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ limitations under the License.

#include "tuples.h"

#define LOOPBACK_ADDR 0x0100007f

#ifndef VISIBILITY_PRIVATE
#define VISIBILITY_PRIVATE private:
#endif
Expand Down Expand Up @@ -91,4 +89,4 @@ void sinsp_network_interfaces::clear()
{
m_ipv4_interfaces.clear();
m_ipv6_interfaces.clear();
}
}

0 comments on commit 140f9cc

Please sign in to comment.