- 論壇徽章:
- 0
|
輸出規(guī)則
print_firewall 函數(shù)用于規(guī)則的輸出:
print_firewall(i, iptc_get_target(i, handle), num++,format,*handle);
i:當(dāng)前的規(guī)則;
iptc_get_target(i, handle):用于規(guī)則的target部份的處理;
num:規(guī)則序號(hào);
format:輸出格式;
handler:表的信息;
/* e is called `fw' here for hysterical raisins */
static void
print_firewall(const struct ipt_entry *fw,
const char *targname,
unsigned int num,
unsigned int format,
const iptc_handle_t handle)
{
struct iptables_target *target = NULL;
const struct ipt_entry_target *t;
u_int8_t flags;
char buf[BUFSIZ];
if (!iptc_is_chain(targname, handle))
target = find_target(targname, TRY_LOAD);
else
target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
t = ipt_get_target((struct ipt_entry *)fw);
flags = fw->ip.flags;
if (format & FMT_LINENUMBERS) /*輸出行號(hào)*/
printf(FMT("%-4u ", "%u "), num+1);
if (!(format & FMT_NOCOUNTS)) { /*詳細(xì)模式,列出計(jì)數(shù)器*/
print_num(fw->counters.pcnt, format); /*匹配當(dāng)前規(guī)則的數(shù)據(jù)包個(gè)數(shù)*/
print_num(fw->counters.bcnt, format); /*--------------------大小*/
}
/*輸出目標(biāo)名稱*/
if (!(format & FMT_NOTARGET)) /*目標(biāo)名稱,即攔截、通過(guò)等動(dòng)作*/
printf(FMT("%-9s ", "%s "), targname);
/*輸出協(xié)議名*/
fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
{
char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
if (pname)
printf(FMT("%-5s", "%s "), pname);
else
printf(FMT("%-5hu", "%hu "), fw->ip.proto);
}
/*輸出選項(xiàng)字段*/
if (format & FMT_OPTIONS) {
if (format & FMT_NOTABLE)
fputs("opt ", stdout);
fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout); //#define IP_FW_INV_FRAG 0x0080 /* Invert the sense of IP_FW_F_FRAG. */
fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout); //#define IP_FW_F_FRAG 0x0004 /* Set if rule is a fragment rule */
fputc(' ', stdout);
}
if (format & FMT_VIA) {
char iface[IFNAMSIZ+2];
if (fw->ip.invflags & IPT_INV_VIA_IN) { /*輸入端口取反標(biāo)志*/
iface[0] = '!'; /*設(shè)置取反標(biāo)志符*/
iface[1] = '\0';
}
else iface[0] = '\0';
if (fw->ip.iniface[0] != '\0') {
strcat(iface, fw->ip.iniface);
}
else if (format & FMT_NUMERIC) strcat(iface, "*");
else strcat(iface, "any");
printf(FMT(" %-6s ","in %s "), iface); /*輸出輸入端口*/
if (fw->ip.invflags & IPT_INV_VIA_OUT) { /*輸出端口取反標(biāo)志*/
iface[0] = '!'; /*設(shè)置取反標(biāo)志符*/
iface[1] = '\0';
}
else iface[0] = '\0';
if (fw->ip.outiface[0] != '\0') {
strcat(iface, fw->ip.outiface);
}
else if (format & FMT_NUMERIC) strcat(iface, "*");
else strcat(iface, "any");
printf(FMT("%-6s ","out %s "), iface); /*輸出輸出端口*/
} /*end print in/out interface */
/*輸出源地址及掩碼*/
fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout); /*源地址取反標(biāo)志*/
if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC)) /*源地址為任意*/
printf(FMT("%-19s ","%s "), "anywhere");
else {
if (format & FMT_NUMERIC)
sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
else
sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
printf(FMT("%-19s ","%s "), buf);
}
/*輸出目的地址及掩碼*/
fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
printf(FMT("%-19s","-> %s"), "anywhere");
else {
if (format & FMT_NUMERIC)
sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
else
sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
printf(FMT("%-19s","-> %s"), buf);
}
if (format & FMT_NOTABLE)
fputs(" ", stdout);
/*輸出擴(kuò)展的MATCH*/
IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
/*輸出擴(kuò)展的TARGET*/
if (target) {
if (target->print)
/* Print the target information. */
target->print(&fw->ip, t, format & FMT_NUMERIC);
} else if (t->u.target_size != sizeof(*t))
printf("[%u bytes of unknown target data] ",
t->u.target_size - sizeof(*t));
if (!(format & FMT_NONEWLINE))
fputc('\n', stdout);
}
函數(shù)分為三部份:
輸出標(biāo)準(zhǔn)的match部份;
輸出擴(kuò)展的match部份,調(diào)用IPT_MATCH_ITERATE實(shí)現(xiàn);
調(diào)用對(duì)應(yīng)的target的print函數(shù)輸出target部份。 |
|