I am trying to write some gdb scripts with python.
There is a version neutral C definition of an ip address as follows :
typedef struct ip_addr {
union {
struct in6_addr v6;
struct {
uint32_t v4_unused[2];
uint32_t v4_marker;
uint32_t v4;
};
};
#define v6_addr v6.s6_addr
} ip_addr_t;
In C, one can use a following macro to check if a given IP address is v4 or v6 :
netinet/in.h
# define IN6_IS_ADDR_V4MAPPED(a) \
(__extension__ \
({ const struct in6_addr *__a = (const struct in6_addr *) (a); \
__a->__in6_u.__u6_addr32[0] == 0 \
&& __a->__in6_u.__u6_addr32[1] == 0 \
&& __a->__in6_u.__u6_addr32[2] == htonl (0xffff); }))
for the gdb script i am trying to do something similar to above macro:
python printers for the ipaddress :
def ip_to_string(ip, isV4):
if isv4 == 1:
return ipv4_to_string(ip)
else:
return ipv6_to_string(ip)
def ip4_to_string(ip):
return socket.inet_ntop(socket.AF_INET, struct.pack("=L", int(ip)))
def ipv6_to_string(ip):
return socket.inet_ntop(socket.AF_INET6, socket.inet_pton(socket.AF_INET6, ip.v6_addr))
The Tuple printer has the following invocation :
def to_string(self):
src_ip = ip_to_string(self.val["src_ipx_addr"])
dst_ip = ip_to_string(self.val["dst_ipx_addr"])
Any easier way i can identify if the C struct ip_addr_t is a v4 or v6 address in python ?
TIA.