I have inherited some C code, and I am a bit rusty on macros. We use libdnet to create packets. I have code that does this:
ip_pack_hdr(
/* hdr = */ &(pkt->ip),
/* tos = */ 0, // Fixed
/* len = */ ICMP4_ECHO_PKT_LEN_NO_ETH + data_len, // Fixed
/* id = */ 0, // Dynamic (self)
/* off = */ IP_DF, // Fixed
/* ttl = */ 0, // Dynamic (caller)
/* p = */ IP_PROTO_ICMP, // Fixed
/* src = */ src.addr_ip, // Fixed
/* dst = */ dst.addr_ip // Fixed
);
and the definition of ip_pack_hdr in libdnet is
#define ip_pack_hdr(hdr, tos, len, id, off, ttl, p, src, dst) do { \
struct ip_hdr *ip_pack_p = (struct ip_hdr *)(hdr); \
ip_pack_p->ip_v = 4; ip_pack_p->ip_hl = 5; \
ip_pack_p->ip_tos = tos; ip_pack_p->ip_len = htons(len); \
ip_pack_p->ip_id = htons(id); ip_pack_p->ip_off = htons(off); \
ip_pack_p->ip_ttl = ttl; ip_pack_p->ip_p = p; \
ip_pack_p->ip_src = src; ip_pack_p->ip_dst = dst; \
} while (0)
I am trying to understand what exactly happens when the macro is called. I understand from this SO question why there is a do-while(0) loop, but what I don't understand is does this macro modify my data in place? It is supposed to act like a function but where does the final value of ip_pack_p get stored?