This repository has been archived on 2025-07-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
ossp/firewall-rules/types.c
AKP be3eece61c
Alter 5 files
Add `.clang-format`
Update `checkPacket.c`
Update `parser.c`
Update `readFirewall.c`
Update `types.c`
2023-10-08 19:35:23 +01:00

82 lines
No EOL
1.5 KiB
C

//
// Created by akp on 06/10/23.
//
#include "akpa_util.c"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
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);
}
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);
}
}