Minify memory usage in ipset, check addr struct size

This commit is contained in:
ruti 2024-12-27 04:07:34 +03:00
parent 2673e5f909
commit 58f9ad5b07
2 changed files with 15 additions and 5 deletions

16
main.c
View File

@ -27,6 +27,9 @@
#define VERSION "15"
ASSERT_SIZE(struct in_addr, 4)
ASSERT_SIZE(struct in6_addr, 16)
char ip_option[1] = "\0";
struct packet fake_tls = {
@ -348,13 +351,13 @@ static int parse_ip(char *out, char *str, size_t size)
}
*sep = 0;
}
int len = 4;
int len = sizeof(struct in_addr);
if (inet_pton(AF_INET, str, out) <= 0) {
if (inet_pton(AF_INET6, str, out) <= 0) {
return 0;
}
else len = 16;
else len = sizeof(struct in6_addr);
}
if (!bits || bits > len * 8) bits = len * 8;
return (int )bits;
@ -386,13 +389,16 @@ struct mphdr *parse_ipset(char *buffer, size_t size)
num++;
s = e + 1;
char *ip_raw = malloc(16);
int bits = parse_ip(ip_raw, ip, sizeof(ip));
char ip_stack[sizeof(struct in6_addr)];
int bits = parse_ip(ip_stack, ip, sizeof(ip));
if (bits <= 0) {
LOG(LOG_E, "invalid ip: num: %zd\n", num);
free(ip_raw);
continue;
}
int len = bits / 8 + (bits % 8 ? 1 : 0);
char *ip_raw = malloc(len);
memcpy(ip_raw, ip_stack, len);
struct elem *elem = mem_add(hdr, ip_raw, bits, sizeof(struct elem));
if (!elem) {
free(ip_raw);

View File

@ -130,4 +130,8 @@ extern struct packet fake_http;
extern struct packet fake_udp;
extern char ip_option[1];
#define ASSERT_SIZE(stc, len) \
static char t[-(int)sizeof(stc) + len]; \
static char tt[(int)sizeof(t) * -1];
#endif