// // Created by akp on 06/10/23. // #pragma once #include "akpa_util.c" #include #include #include struct IP { unsigned char val[4]; }; int compare_ip(struct IP *a, struct IP *b) { int result = 0; for (int i = 0; i < 4 && result == 0; i += 1) { result = akpa_numcmp(a->val[i], b->val[i]); } return result; } struct IPRange { struct IP start; struct IP *end; }; typedef unsigned short Port; struct PortRange { Port start; Port *end; }; struct Rule { struct IPRange ip; struct PortRange port; }; int compare_rule(struct Rule *a, struct Rule *b) { int result = compare_ip(&a->ip.start, &b->ip.start); if (result == 0) { result = akpa_numcmp(a->port.start, b->port.start); } if (result == 0) { if ((a->port.end != NULL) || (b->port.end != NULL)) { result = akpa_numcmp(*a->port.end, *b->port.end); } } return result; } int compare_rule_v(const void *a, const void *b) { return compare_rule(*((struct Rule **)a), *((struct Rule **)b)); } void free_rule(struct Rule *r) { if (r->ip.end != NULL) { free(r->ip.end); } if (r->port.end != NULL) { free(r->port.end); } free(r); } void print_ip(struct IP *ip) { printf("%d.%d.%d.%d", ip->val[0], ip->val[1], ip->val[2], ip->val[3]); } void print_port(Port port) { printf("%d", port); } void print_rule(struct Rule *r) { print_ip(&r->ip.start); if (r->ip.end != NULL) { printf("-"); print_ip(r->ip.end); } printf(" "); print_port(r->port.start); if (r->port.end != NULL) { printf("-"); print_port(*r->port.end); } }