2024-10-28 11:32:24 +05:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2024-11-14 00:05:06 +05:00
|
|
|
#include <stdlib.h>
|
2024-10-28 11:32:24 +05:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/stat.h>
|
2024-11-14 12:57:02 +05:00
|
|
|
#include <libgen.h>
|
2024-11-19 15:59:46 +05:00
|
|
|
#include <unistd.h>
|
2024-10-28 11:32:24 +05:00
|
|
|
|
2024-11-19 01:05:25 +05:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <linux/tcp.h>
|
|
|
|
#endif
|
|
|
|
|
2024-11-16 20:25:53 +05:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include "andr/ifaddrs.h"
|
|
|
|
#else
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#endif
|
|
|
|
|
2024-11-14 00:05:06 +05:00
|
|
|
#include "helpers.h"
|
|
|
|
|
|
|
|
int unique_size_t(size_t *pu, int ct)
|
|
|
|
{
|
|
|
|
int i, j, u;
|
|
|
|
for (i = j = 0; j < ct; i++)
|
|
|
|
{
|
|
|
|
u = pu[j++];
|
|
|
|
for (; j < ct && pu[j] == u; j++);
|
|
|
|
pu[i] = u;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
static int cmp_size_t(const void * a, const void * b)
|
|
|
|
{
|
|
|
|
return *(size_t*)a < *(size_t*)b ? -1 : *(size_t*)a > *(size_t*)b;
|
|
|
|
}
|
2024-11-19 01:05:25 +05:00
|
|
|
void qsort_size_t(size_t *array, size_t ct)
|
2024-11-14 00:05:06 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
qsort(array, ct, sizeof(*array), cmp_size_t);
|
2024-11-14 00:05:06 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-14 16:58:31 +05:00
|
|
|
void rtrim(char *s)
|
|
|
|
{
|
|
|
|
if (s)
|
|
|
|
for (char *p = s + strlen(s) - 1; p >= s && (*p == '\n' || *p == '\r'); p--) *p = '\0';
|
|
|
|
}
|
|
|
|
|
2024-10-31 19:55:26 +05:00
|
|
|
void replace_char(char *s, char from, char to)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
for (; *s; s++) if (*s == from) *s = to;
|
2024-10-31 19:55:26 +05:00
|
|
|
}
|
|
|
|
|
2024-11-19 01:05:25 +05:00
|
|
|
char *strncasestr(const char *s, const char *find, size_t slen)
|
2024-10-28 11:32:24 +05:00
|
|
|
{
|
|
|
|
char c, sc;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if ((c = *find++) != '\0')
|
|
|
|
{
|
|
|
|
len = strlen(find);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (slen-- < 1 || (sc = *s++) == '\0') return NULL;
|
|
|
|
} while (toupper(c) != toupper(sc));
|
|
|
|
if (len > slen) return NULL;
|
|
|
|
} while (strncasecmp(s, find, len) != 0);
|
|
|
|
s--;
|
|
|
|
}
|
|
|
|
return (char *)s;
|
|
|
|
}
|
|
|
|
|
2024-10-31 19:55:26 +05:00
|
|
|
bool load_file(const char *filename, void *buffer, size_t *buffer_size)
|
|
|
|
{
|
|
|
|
FILE *F;
|
|
|
|
|
|
|
|
F = fopen(filename, "rb");
|
|
|
|
if (!F) return false;
|
|
|
|
|
|
|
|
*buffer_size = fread(buffer, 1, *buffer_size, F);
|
|
|
|
if (ferror(F))
|
|
|
|
{
|
|
|
|
fclose(F);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(F);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-28 11:32:24 +05:00
|
|
|
bool append_to_list_file(const char *filename, const char *s)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
FILE *F = fopen(filename, "at");
|
2024-10-28 11:32:24 +05:00
|
|
|
if (!F) return false;
|
2024-11-19 01:05:25 +05:00
|
|
|
bool bOK = fprintf(F, "%s\n", s) > 0;
|
2024-10-28 11:32:24 +05:00
|
|
|
fclose(F);
|
|
|
|
return bOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ntop46(const struct sockaddr *sa, char *str, size_t len)
|
|
|
|
{
|
|
|
|
if (!len) return;
|
2024-11-19 01:05:25 +05:00
|
|
|
*str = 0;
|
2024-10-28 11:32:24 +05:00
|
|
|
switch (sa->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
inet_ntop(sa->sa_family, &((struct sockaddr_in*)sa)->sin_addr, str, len);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, str, len);
|
|
|
|
break;
|
|
|
|
default:
|
2024-11-19 01:05:25 +05:00
|
|
|
snprintf(str, len, "UNKNOWN_FAMILY_%d", sa->sa_family);
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void ntop46_port(const struct sockaddr *sa, char *str, size_t len)
|
|
|
|
{
|
|
|
|
char ip[40];
|
2024-11-19 01:05:25 +05:00
|
|
|
ntop46(sa, ip, sizeof(ip));
|
2024-10-28 11:32:24 +05:00
|
|
|
switch (sa->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2024-11-19 01:05:25 +05:00
|
|
|
snprintf(str, len, "%s:%u", ip, ntohs(((struct sockaddr_in*)sa)->sin_port));
|
2024-10-28 11:32:24 +05:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2024-11-19 01:05:25 +05:00
|
|
|
snprintf(str, len, "[%s]:%u", ip, ntohs(((struct sockaddr_in6*)sa)->sin6_port));
|
2024-10-28 11:32:24 +05:00
|
|
|
break;
|
|
|
|
default:
|
2024-11-19 01:05:25 +05:00
|
|
|
snprintf(str, len, "%s", ip);
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void print_sockaddr(const struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
char ip_port[48];
|
|
|
|
|
2024-11-19 01:05:25 +05:00
|
|
|
ntop46_port(sa, ip_port, sizeof(ip_port));
|
|
|
|
printf("%s", ip_port);
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// -1 = error, 0 = not local, 1 = local
|
|
|
|
bool check_local_ip(const struct sockaddr *saddr)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
struct ifaddrs *addrs, *a;
|
2024-10-28 11:32:24 +05:00
|
|
|
|
|
|
|
if (is_localnet(saddr))
|
|
|
|
return true;
|
|
|
|
|
2024-11-19 01:05:25 +05:00
|
|
|
if (getifaddrs(&addrs) < 0) return false;
|
|
|
|
a = addrs;
|
2024-10-28 11:32:24 +05:00
|
|
|
|
2024-11-19 01:05:25 +05:00
|
|
|
bool bres = false;
|
2024-10-28 11:32:24 +05:00
|
|
|
while (a)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
if (a->ifa_addr && sacmp(a->ifa_addr, saddr))
|
2024-10-28 11:32:24 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
bres = true;
|
2024-10-28 11:32:24 +05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
a = a->ifa_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(addrs);
|
|
|
|
return bres;
|
|
|
|
}
|
|
|
|
void print_addrinfo(const struct addrinfo *ai)
|
|
|
|
{
|
|
|
|
char str[64];
|
|
|
|
while (ai)
|
|
|
|
{
|
|
|
|
switch (ai->ai_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
if (inet_ntop(ai->ai_family, &((struct sockaddr_in*)ai->ai_addr)->sin_addr, str, sizeof(str)))
|
|
|
|
printf("%s\n", str);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (inet_ntop(ai->ai_family, &((struct sockaddr_in6*)ai->ai_addr)->sin6_addr, str, sizeof(str)))
|
2024-11-19 01:05:25 +05:00
|
|
|
printf("%s\n", str);
|
2024-10-28 11:32:24 +05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ai = ai->ai_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool saismapped(const struct sockaddr_in6 *sa)
|
|
|
|
{
|
|
|
|
// ::ffff:1.2.3.4
|
2024-11-19 01:05:25 +05:00
|
|
|
return !memcmp(sa->sin6_addr.s6_addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", 12);
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
2024-11-19 01:05:25 +05:00
|
|
|
bool samappedcmp(const struct sockaddr_in *sa1, const struct sockaddr_in6 *sa2)
|
2024-10-28 11:32:24 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
return saismapped(sa2) && !memcmp(sa2->sin6_addr.s6_addr + 12, &sa1->sin_addr.s_addr, 4);
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
2024-11-19 01:05:25 +05:00
|
|
|
bool sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
|
2024-10-28 11:32:24 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
return (sa1->sa_family == AF_INET && sa2->sa_family == AF_INET && !memcmp(&((struct sockaddr_in*)sa1)->sin_addr, &((struct sockaddr_in*)sa2)->sin_addr, sizeof(struct in_addr))) ||
|
|
|
|
(sa1->sa_family == AF_INET6 && sa2->sa_family == AF_INET6 && !memcmp(&((struct sockaddr_in6*)sa1)->sin6_addr, &((struct sockaddr_in6*)sa2)->sin6_addr, sizeof(struct in6_addr))) ||
|
|
|
|
(sa1->sa_family == AF_INET && sa2->sa_family == AF_INET6 && samappedcmp((struct sockaddr_in*)sa1, (struct sockaddr_in6*)sa2)) ||
|
|
|
|
(sa1->sa_family == AF_INET6 && sa2->sa_family == AF_INET && samappedcmp((struct sockaddr_in*)sa2, (struct sockaddr_in6*)sa1));
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
uint16_t saport(const struct sockaddr *sa)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
return htons(sa->sa_family == AF_INET ? ((struct sockaddr_in*)sa)->sin_port :
|
|
|
|
sa->sa_family == AF_INET6 ? ((struct sockaddr_in6*)sa)->sin6_port : 0);
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
bool saconvmapped(struct sockaddr_storage *a)
|
|
|
|
{
|
|
|
|
if ((a->ss_family == AF_INET6) && saismapped((struct sockaddr_in6*)a))
|
|
|
|
{
|
|
|
|
uint32_t ip4 = IN6_EXTRACT_MAP4(((struct sockaddr_in6*)a)->sin6_addr.s6_addr);
|
|
|
|
uint16_t port = ((struct sockaddr_in6*)a)->sin6_port;
|
|
|
|
a->ss_family = AF_INET;
|
|
|
|
((struct sockaddr_in*)a)->sin_addr.s_addr = ip4;
|
|
|
|
((struct sockaddr_in*)a)->sin_port = port;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sacopy(struct sockaddr_storage *sa_dest, const struct sockaddr *sa)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
switch (sa->sa_family)
|
2024-10-28 11:32:24 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
case AF_INET:
|
|
|
|
memcpy(sa_dest, sa, sizeof(struct sockaddr_in));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(sa_dest, sa, sizeof(struct sockaddr_in6));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sa_dest->ss_family = 0;
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
}
|
2024-10-15 00:32:50 +05:00
|
|
|
void sa46copy(sockaddr_in46 *sa_dest, const struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
sacopy((struct sockaddr_storage*)sa_dest, sa);
|
|
|
|
}
|
2024-10-28 11:32:24 +05:00
|
|
|
|
|
|
|
bool is_localnet(const struct sockaddr *a)
|
|
|
|
{
|
|
|
|
// match 127.0.0.0/8, 0.0.0.0, ::1, ::0, :ffff:127.0.0.0/104, :ffff:0.0.0.0
|
2024-11-19 01:05:25 +05:00
|
|
|
return (a->sa_family == AF_INET && (IN_LOOPBACK(ntohl(((struct sockaddr_in *)a)->sin_addr.s_addr)) ||
|
|
|
|
INADDR_ANY == ntohl((((struct sockaddr_in *)a)->sin_addr.s_addr)))) ||
|
|
|
|
(a->sa_family == AF_INET6 && (IN6_IS_ADDR_LOOPBACK(&((struct sockaddr_in6 *)a)->sin6_addr) ||
|
|
|
|
IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)a)->sin6_addr) ||
|
|
|
|
(IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)a)->sin6_addr) && (IN_LOOPBACK(ntohl(IN6_EXTRACT_MAP4(((struct sockaddr_in6*)a)->sin6_addr.s6_addr))) ||
|
|
|
|
INADDR_ANY == ntohl(IN6_EXTRACT_MAP4(((struct sockaddr_in6*)a)->sin6_addr.s6_addr))))));
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
bool is_linklocal(const struct sockaddr_in6 *a)
|
|
|
|
{
|
|
|
|
// fe80::/10
|
2024-11-19 01:05:25 +05:00
|
|
|
return a->sin6_addr.s6_addr[0] == 0xFE && (a->sin6_addr.s6_addr[1] & 0xC0) == 0x80;
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
bool is_private6(const struct sockaddr_in6* a)
|
|
|
|
{
|
|
|
|
// fc00::/7
|
|
|
|
return (a->sin6_addr.s6_addr[0] & 0xFE) == 0xFC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool set_keepalive(int fd)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
int yes = 1;
|
|
|
|
return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(int)) != -1;
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
bool set_ttl(int fd, int ttl)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
return setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) != -1;
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
bool set_hl(int fd, int hl)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
return setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hl, sizeof(hl)) != -1;
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
bool set_ttl_hl(int fd, int ttl)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
bool b1, b2;
|
2024-10-28 11:32:24 +05:00
|
|
|
// try to set both but one may fail if family is wrong
|
2024-11-19 01:05:25 +05:00
|
|
|
b1 = set_ttl(fd, ttl);
|
|
|
|
b2 = set_hl(fd, ttl);
|
2024-10-28 11:32:24 +05:00
|
|
|
return b1 || b2;
|
|
|
|
}
|
|
|
|
int get_so_error(int fd)
|
|
|
|
{
|
|
|
|
// getsockopt(SO_ERROR) clears error
|
|
|
|
int errn;
|
|
|
|
socklen_t optlen = sizeof(errn);
|
2024-11-19 01:05:25 +05:00
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errn, &optlen) == -1)
|
|
|
|
errn = errno;
|
2024-10-28 11:32:24 +05:00
|
|
|
return errn;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fprint_localtime(FILE *F)
|
|
|
|
{
|
|
|
|
struct tm t;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
time(&now);
|
2024-11-19 01:05:25 +05:00
|
|
|
localtime_r(&now, &t);
|
2024-10-28 11:32:24 +05:00
|
|
|
return fprintf(F, "%02d.%02d.%04d %02d:%02d:%02d", t.tm_mday, t.tm_mon + 1, t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t file_mod_time(const char *filename)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2024-11-19 01:05:25 +05:00
|
|
|
return stat(filename, &st) == -1 ? 0 : st.st_mtime;
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pf_in_range(uint16_t port, const port_filter *pf)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
return port && (((!pf->from && !pf->to) || (port >= pf->from && port <= pf->to)) ^ pf->neg);
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
bool pf_parse(const char *s, port_filter *pf)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
unsigned int v1, v2;
|
2024-10-28 11:32:24 +05:00
|
|
|
char c;
|
|
|
|
|
|
|
|
if (!s) return false;
|
2024-11-19 01:05:25 +05:00
|
|
|
if (*s == '*' && s[1] == 0)
|
2024-10-29 19:17:58 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
pf->from = 1; pf->to = 0xFFFF;
|
2024-10-29 19:17:58 +05:00
|
|
|
return true;
|
|
|
|
}
|
2024-11-19 01:05:25 +05:00
|
|
|
if (*s == '~')
|
2024-10-28 11:32:24 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
pf->neg = true;
|
2024-10-28 11:32:24 +05:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else
|
2024-11-19 01:05:25 +05:00
|
|
|
pf->neg = false;
|
|
|
|
if (sscanf(s, "%u-%u%c", &v1, &v2, &c) == 2)
|
2024-10-28 11:32:24 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
if (v1 > 65535 || v2 > 65535 || v1 > v2) return false;
|
|
|
|
pf->from = (uint16_t)v1;
|
|
|
|
pf->to = (uint16_t)v2;
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
2024-11-19 01:05:25 +05:00
|
|
|
else if (sscanf(s, "%u%c", &v1, &c) == 1)
|
2024-10-28 11:32:24 +05:00
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
if (v1 > 65535) return false;
|
|
|
|
pf->to = pf->from = (uint16_t)v1;
|
2024-10-28 11:32:24 +05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
// deny all case
|
2024-11-19 01:05:25 +05:00
|
|
|
if (!pf->from && !pf->to) pf->neg = true;
|
2024-10-28 11:32:24 +05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool pf_is_empty(const port_filter *pf)
|
|
|
|
{
|
|
|
|
return !pf->neg && !pf->from && !pf->to;
|
|
|
|
}
|
2024-10-14 16:58:31 +05:00
|
|
|
|
|
|
|
|
2024-11-14 12:57:02 +05:00
|
|
|
bool set_env_exedir(const char *argv0)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
char *s, *d;
|
|
|
|
bool bOK = false;
|
2024-11-14 12:57:02 +05:00
|
|
|
if ((s = strdup(argv0)))
|
|
|
|
{
|
|
|
|
if ((d = dirname(s)))
|
2024-11-19 01:05:25 +05:00
|
|
|
setenv("EXEDIR", s, 1);
|
2024-11-14 12:57:02 +05:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
return bOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-14 16:58:31 +05:00
|
|
|
static void mask_from_preflen6_make(uint8_t plen, struct in6_addr *a)
|
|
|
|
{
|
|
|
|
if (plen >= 128)
|
2024-11-19 01:05:25 +05:00
|
|
|
memset(a->s6_addr, 0xFF, 16);
|
2024-10-14 16:58:31 +05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
uint8_t n = plen >> 3;
|
2024-11-19 01:05:25 +05:00
|
|
|
memset(a->s6_addr, 0xFF, n);
|
|
|
|
memset(a->s6_addr + n, 0x00, 16 - n);
|
2024-10-14 16:58:31 +05:00
|
|
|
a->s6_addr[n] = (uint8_t)(0xFF00 >> (plen & 7));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct in6_addr ip6_mask[129];
|
|
|
|
void mask_from_preflen6_prepare(void)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
for (int plen = 0; plen <= 128; plen++) mask_from_preflen6_make(plen, ip6_mask + plen);
|
2024-10-14 16:58:31 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__GNUC__) && !defined(__llvm__)
|
2024-11-19 01:05:25 +05:00
|
|
|
__attribute__((optimize("no-strict-aliasing")))
|
2024-10-14 16:58:31 +05:00
|
|
|
#endif
|
|
|
|
void ip6_and(const struct in6_addr * restrict a, const struct in6_addr * restrict b, struct in6_addr * restrict result)
|
|
|
|
{
|
2024-10-16 13:21:31 +05:00
|
|
|
// int 128 can cause alignment segfaults because sin6_addr in struct sockaddr_in6 is 8-byte aligned, not 16-byte
|
2024-10-14 16:58:31 +05:00
|
|
|
((uint64_t*)result->s6_addr)[0] = ((uint64_t*)a->s6_addr)[0] & ((uint64_t*)b->s6_addr)[0];
|
|
|
|
((uint64_t*)result->s6_addr)[1] = ((uint64_t*)a->s6_addr)[1] & ((uint64_t*)b->s6_addr)[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void str_cidr4(char *s, size_t s_len, const struct cidr4 *cidr)
|
|
|
|
{
|
|
|
|
char s_ip[16];
|
2024-11-19 01:05:25 +05:00
|
|
|
*s_ip = 0;
|
2024-10-14 16:58:31 +05:00
|
|
|
inet_ntop(AF_INET, &cidr->addr, s_ip, sizeof(s_ip));
|
2024-11-19 01:05:25 +05:00
|
|
|
snprintf(s, s_len, cidr->preflen < 32 ? "%s/%u" : "%s", s_ip, cidr->preflen);
|
2024-10-14 16:58:31 +05:00
|
|
|
}
|
|
|
|
void print_cidr4(const struct cidr4 *cidr)
|
|
|
|
{
|
|
|
|
char s[19];
|
2024-11-19 01:05:25 +05:00
|
|
|
str_cidr4(s, sizeof(s), cidr);
|
|
|
|
printf("%s", s);
|
2024-10-14 16:58:31 +05:00
|
|
|
}
|
|
|
|
void str_cidr6(char *s, size_t s_len, const struct cidr6 *cidr)
|
|
|
|
{
|
|
|
|
char s_ip[40];
|
2024-11-19 01:05:25 +05:00
|
|
|
*s_ip = 0;
|
2024-10-14 16:58:31 +05:00
|
|
|
inet_ntop(AF_INET6, &cidr->addr, s_ip, sizeof(s_ip));
|
2024-11-19 01:05:25 +05:00
|
|
|
snprintf(s, s_len, cidr->preflen < 128 ? "%s/%u" : "%s", s_ip, cidr->preflen);
|
2024-10-14 16:58:31 +05:00
|
|
|
}
|
|
|
|
void print_cidr6(const struct cidr6 *cidr)
|
|
|
|
{
|
|
|
|
char s[44];
|
2024-11-19 01:05:25 +05:00
|
|
|
str_cidr6(s, sizeof(s), cidr);
|
|
|
|
printf("%s", s);
|
2024-10-14 16:58:31 +05:00
|
|
|
}
|
|
|
|
bool parse_cidr4(char *s, struct cidr4 *cidr)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
char *p, d;
|
2024-10-14 16:58:31 +05:00
|
|
|
bool b;
|
|
|
|
unsigned int plen;
|
|
|
|
|
|
|
|
if ((p = strchr(s, '/')))
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
if (sscanf(p + 1, "%u", &plen) != 1 || plen > 32)
|
2024-10-14 16:58:31 +05:00
|
|
|
return false;
|
|
|
|
cidr->preflen = (uint8_t)plen;
|
2024-11-19 01:05:25 +05:00
|
|
|
d = *p; *p = 0; // backup char
|
2024-10-14 16:58:31 +05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
cidr->preflen = 32;
|
2024-11-19 01:05:25 +05:00
|
|
|
b = (inet_pton(AF_INET, s, &cidr->addr) == 1);
|
|
|
|
if (p) *p = d; // restore char
|
2024-10-14 16:58:31 +05:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
bool parse_cidr6(char *s, struct cidr6 *cidr)
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
char *p, d;
|
2024-10-14 16:58:31 +05:00
|
|
|
bool b;
|
|
|
|
unsigned int plen;
|
|
|
|
|
|
|
|
if ((p = strchr(s, '/')))
|
|
|
|
{
|
2024-11-19 01:05:25 +05:00
|
|
|
if (sscanf(p + 1, "%u", &plen) != 1 || plen > 128)
|
2024-10-14 16:58:31 +05:00
|
|
|
return false;
|
|
|
|
cidr->preflen = (uint8_t)plen;
|
2024-11-19 01:05:25 +05:00
|
|
|
d = *p; *p = 0; // backup char
|
2024-10-14 16:58:31 +05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
cidr->preflen = 128;
|
2024-11-19 01:05:25 +05:00
|
|
|
b = (inet_pton(AF_INET6, s, &cidr->addr) == 1);
|
|
|
|
if (p) *p = d; // restore char
|
2024-10-14 16:58:31 +05:00
|
|
|
return b;
|
|
|
|
}
|
2024-11-19 01:05:25 +05:00
|
|
|
|
|
|
|
|
|
|
|
void msleep(unsigned int ms)
|
|
|
|
{
|
|
|
|
struct timespec time = {
|
|
|
|
.tv_nsec = (ms % 1000) * 1000000,
|
|
|
|
.tv_sec = ms / 1000
|
|
|
|
};
|
|
|
|
nanosleep(&time, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
2024-11-19 15:59:46 +05:00
|
|
|
bool socket_supports_notsent()
|
|
|
|
{
|
|
|
|
int sfd;
|
|
|
|
struct tcp_info tcpi;
|
|
|
|
|
|
|
|
sfd = socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
if (sfd<0) return false;
|
|
|
|
|
|
|
|
socklen_t ts = sizeof(tcpi);
|
|
|
|
if (getsockopt(sfd, IPPROTO_TCP, TCP_INFO, (char *)&tcpi, &ts) < 0)
|
|
|
|
{
|
|
|
|
close(sfd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
close(sfd);
|
|
|
|
|
|
|
|
return ts>=((char *)&tcpi.tcpi_notsent_bytes - (char *)&tcpi.tcpi_state + sizeof(tcpi.tcpi_notsent_bytes));
|
|
|
|
}
|
2024-11-19 01:05:25 +05:00
|
|
|
bool socket_has_notsent(int sfd)
|
|
|
|
{
|
|
|
|
struct tcp_info tcpi;
|
|
|
|
socklen_t ts = sizeof(tcpi);
|
|
|
|
|
|
|
|
if (getsockopt(sfd, IPPROTO_TCP, TCP_INFO, (char *)&tcpi, &ts) < 0)
|
|
|
|
return false;
|
2024-11-19 11:51:32 +05:00
|
|
|
if (tcpi.tcpi_state != 1) // TCP_ESTABLISHED
|
2024-11-19 01:05:25 +05:00
|
|
|
return false;
|
2024-11-19 15:59:46 +05:00
|
|
|
size_t s = (char *)&tcpi.tcpi_notsent_bytes - (char *)&tcpi + sizeof(tcpi.tcpi_notsent_bytes);
|
2024-11-19 01:05:25 +05:00
|
|
|
if (ts < s)
|
2024-11-19 11:51:32 +05:00
|
|
|
// old structure version
|
2024-11-19 01:05:25 +05:00
|
|
|
return false;
|
|
|
|
return !!tcpi.tcpi_notsent_bytes;
|
|
|
|
}
|
|
|
|
bool socket_wait_notsent(int sfd, unsigned int delay_ms, unsigned int *wasted_ms)
|
|
|
|
{
|
|
|
|
struct timespec tres;
|
|
|
|
unsigned int mtick;
|
|
|
|
|
|
|
|
if (wasted_ms) *wasted_ms=0;
|
|
|
|
if (!socket_has_notsent(sfd)) return true;
|
|
|
|
|
|
|
|
if (clock_getres(CLOCK_MONOTONIC,&tres))
|
|
|
|
{
|
|
|
|
tres.tv_nsec = 10000000;
|
|
|
|
tres.tv_sec = 0;
|
|
|
|
}
|
|
|
|
mtick = (unsigned int)(tres.tv_sec*1000) + (unsigned int)(tres.tv_nsec/1000000);
|
|
|
|
if (mtick<1) mtick=1;
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
msleep(mtick);
|
|
|
|
if (wasted_ms) *wasted_ms+=mtick;
|
|
|
|
if (!socket_has_notsent(sfd)) return true;
|
|
|
|
if (delay_ms<=mtick) break;
|
|
|
|
delay_ms-=mtick;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|