def port_to_rule(ty, rules):
if ty == "port" :
ast = parse_ast(rules, parse_numbers_expr)
- return "if (tcp == NULL && udp == NULL) break;\nif (!( " + ast.write("sport", "dport") + " )) break;"
+ return "if (!ports_valid) break;\nif (!( " + ast.write("sport", "dport") + " )) break;"
ast = parse_ast(rules, parse_numbers_expr)
- return "if (tcp == NULL && udp == NULL) break;\nif (!( " + ast.write(ty) + " )) break;"
+ return "if (!ports_valid) break;\nif (!( " + ast.write(ty) + " )) break;"
def tcp_flags_to_rule(rules):
ast = parse_ast(rules, parse_bit_expr)
const void *l4hdr = NULL;
const struct tcphdr *tcp = NULL;
- const struct udphdr *udp = NULL;
+ uint8_t ports_valid = 0;
uint16_t sport, dport; // Host Endian! Only valid with tcp || udp
#ifdef NEED_V4_PARSE
tcp = (struct tcphdr*) l4hdr;
sport = BE16(tcp->source);
dport = BE16(tcp->dest);
+ ports_valid = 1;
} else if (ip->protocol == IP_PROTO_UDP) {
CHECK_LEN(l4hdr, udphdr);
- udp = (struct udphdr*) l4hdr;
+ const struct udphdr *udp = (struct udphdr*) l4hdr;
sport = BE16(udp->source);
dport = BE16(udp->dest);
+ ports_valid = 1;
} else if (ip->protocol == IP_PROTO_ICMP) {
CHECK_LEN(l4hdr, icmphdr);
icmp = (struct icmphdr*) l4hdr;
tcp = (struct tcphdr*) l4hdr;
sport = BE16(tcp->source);
dport = BE16(tcp->dest);
+ ports_valid = 1;
} else if (v6nexthdr == IP_PROTO_UDP) {
CHECK_LEN(l4hdr, udphdr);
- udp = (struct udphdr*) l4hdr;
+ const struct udphdr *udp = (struct udphdr*) l4hdr;
sport = BE16(udp->source);
dport = BE16(udp->dest);
+ ports_valid = 1;
} else if (v6nexthdr == IP6_PROTO_ICMPV6) {
CHECK_LEN(l4hdr, icmp6hdr);
icmpv6 = (struct icmp6hdr*) l4hdr;