[ACCEPTED]-Convert source IP address from struct iphdr* to string equivalent using Linux netfilter-netfilter
Accepted answer
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.
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
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.