[ACCEPTED]-Convert source IP address from struct iphdr* to string equivalent using Linux netfilter-netfilter

Accepted answer
Score: 23

The kernel's family of printf() functions has a 7 special format specifier for IP-addresses 6 (%pI4 for IPv4-addresses, %pI6 for IPv6).

So with 5 IPv4, you could use something like:

char source[16];
snprintf(source, 16, "%pI4", &ip_header->saddr); // Mind the &!

Or write 4 to dynamically allocated memory.

If you simply 3 want to print debug-output, you can also 2 use printk(). For the many other features of %p, see 1 this document.

Score: 1

Try in4_pton() function in net/core/utils.c (definition: https://elixir.bootlin.com/linux/latest/source/net/core/utils.c#L118)

#include <linux/inet.h>

char source[16];
in4_pton(source, -1, &ip_header->saddr, '\0', NULL);

0

More Related questions