Skip to contentSkip to conversation

inner structure of iptables

DISCUSSION Ozgur AKAN 2 participants 2 messages Mar 16, 2004 – Mar 17, 2004 netfilter

lore
Share & tools
lore

Ozgur AKAN

author
#0 lore raw

I included some simple code to express myself clearly.

I have a problem with the 4th line. When we execute the code for the first time these happen (I wrote down)

In the 1st line we create the pointer to the structure of the table
In the 2nd and 3rd line we get the adress of first chain and print its name to screen.
In the 4th line "e = iptc_first_rule(chain, &h)" we get the adress of the first rule of the first chain and by the loop it goes to next rule until there is no next rule. How does it know that it is the last rule of this chain? Why doesn`t it continue to read the first rule of next chain? (Because there is no chain information here -> e = iptc_next_rule(e, &h))

1 h = iptc_init(tablename);
2 for ( chain = iptc_first_chain(&h); chain; chain = iptc_next_chain(&h)) {
3 printf("%s\n", chain);
4 for ( e = iptc_first_rule(chain, &h); e; e = iptc_next_rule(e, &h)) {
5 printf("n. -%u- i. -%u-\n", e->ip.invflags, e->ip.invflags & IPT_INV_SRCIP);
6 }
7 }

I hope my question is clear enough.

thanks,
Ozgur AKAN

#1 lore raw
Quoted 6 lines · Ozgur AKAN

In the 4th line "e = iptc_first_rule(chain, &h)" we get the adress of the first rule of the first chain and by the loop it goes to next rule until there is no next rule. How does it know that it is the last rule of this chain? Why doesn`t it continue to read the first rule of next chain? (Because there is no chain information here -> e = iptc_next_rule(e, &h))

Because iptc_first_rule remembered where the end of this chain is in the handle.

See TC_FIRST_RULE/TC_NEXT_RULE in libiptc/libiptc.c

Regards
Henrik