2021-03-04 16:30:38 +05:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include "helpers.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
2024-03-24 00:57:05 +05:00
|
|
|
#include <sys/stat.h>
|
2021-03-04 16:30:38 +05:00
|
|
|
|
2024-04-20 22:59:45 +05:00
|
|
|
|
|
|
|
#include "params.h"
|
|
|
|
|
2021-03-04 16:30:38 +05:00
|
|
|
void hexdump_limited_dlog(const uint8_t *data, size_t size, size_t limit)
|
|
|
|
{
|
|
|
|
size_t k;
|
2022-03-25 18:59:58 +05:00
|
|
|
bool bcut = false;
|
|
|
|
if (size > limit)
|
2021-03-04 16:30:38 +05:00
|
|
|
{
|
2022-03-25 18:59:58 +05:00
|
|
|
size = limit;
|
2021-03-04 16:30:38 +05:00
|
|
|
bcut = true;
|
|
|
|
}
|
|
|
|
if (!size) return;
|
2022-03-25 18:59:58 +05:00
|
|
|
for (k = 0; k < size; k++) DLOG("%02X ", data[k]);
|
2021-03-04 16:30:38 +05:00
|
|
|
DLOG(bcut ? "... : " : ": ");
|
2022-03-25 18:59:58 +05:00
|
|
|
for (k = 0; k < size; k++) DLOG("%c", data[k] >= 0x20 && data[k] <= 0x7F ? (char)data[k] : '.');
|
2021-03-04 16:30:38 +05:00
|
|
|
if (bcut) DLOG(" ...");
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:59:58 +05:00
|
|
|
char *strncasestr(const char *s, const char *find, size_t slen)
|
2021-03-04 16:30:38 +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;
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:59:58 +05:00
|
|
|
bool load_file(const char *filename, void *buffer, size_t *buffer_size)
|
2021-03-04 16:30:38 +05:00
|
|
|
{
|
|
|
|
FILE *F;
|
|
|
|
|
2022-03-25 18:59:58 +05:00
|
|
|
F = fopen(filename, "rb");
|
2021-03-04 16:30:38 +05:00
|
|
|
if (!F) return false;
|
|
|
|
|
2022-03-25 18:59:58 +05:00
|
|
|
*buffer_size = fread(buffer, 1, *buffer_size, F);
|
2021-03-04 16:30:38 +05:00
|
|
|
if (ferror(F))
|
|
|
|
{
|
|
|
|
fclose(F);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(F);
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-25 18:59:58 +05:00
|
|
|
bool load_file_nonempty(const char *filename, void *buffer, size_t *buffer_size)
|
2021-03-04 16:30:38 +05:00
|
|
|
{
|
2022-03-25 18:59:58 +05:00
|
|
|
bool b = load_file(filename, buffer, buffer_size);
|
2021-03-04 16:30:38 +05:00
|
|
|
return b && *buffer_size;
|
|
|
|
}
|
2022-03-25 18:59:58 +05:00
|
|
|
bool save_file(const char *filename, const void *buffer, size_t buffer_size)
|
|
|
|
{
|
|
|
|
FILE *F;
|
|
|
|
|
|
|
|
F = fopen(filename, "wb");
|
|
|
|
if (!F) return false;
|
2021-03-04 16:30:38 +05:00
|
|
|
|
2022-03-25 18:59:58 +05:00
|
|
|
fwrite(buffer, 1, buffer_size, F);
|
|
|
|
if (ferror(F))
|
|
|
|
{
|
|
|
|
fclose(F);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(F);
|
|
|
|
return true;
|
|
|
|
}
|
2023-10-26 17:12:32 +05:00
|
|
|
bool append_to_list_file(const char *filename, const char *s)
|
|
|
|
{
|
|
|
|
FILE *F = fopen(filename,"at");
|
|
|
|
if (!F) return false;
|
|
|
|
bool bOK = fprintf(F,"%s\n",s)>0;
|
|
|
|
fclose(F);
|
|
|
|
return bOK;
|
|
|
|
}
|
2021-03-04 16:30:38 +05:00
|
|
|
|
|
|
|
|
2021-03-05 22:15:56 +05:00
|
|
|
void ntop46(const struct sockaddr *sa, char *str, size_t len)
|
2021-03-04 16:30:38 +05:00
|
|
|
{
|
2021-03-05 22:15:56 +05:00
|
|
|
if (!len) return;
|
2022-03-25 18:59:58 +05:00
|
|
|
*str = 0;
|
2021-03-04 16:30:38 +05:00
|
|
|
switch (sa->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2021-03-05 22:15:56 +05:00
|
|
|
inet_ntop(sa->sa_family, &((struct sockaddr_in*)sa)->sin_addr, str, len);
|
2021-03-04 16:30:38 +05:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2021-03-05 22:15:56 +05:00
|
|
|
inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, str, len);
|
2021-03-04 16:30:38 +05:00
|
|
|
break;
|
|
|
|
default:
|
2022-03-25 18:59:58 +05:00
|
|
|
snprintf(str, len, "UNKNOWN_FAMILY_%d", sa->sa_family);
|
2021-03-04 16:30:38 +05:00
|
|
|
}
|
|
|
|
}
|
2021-03-05 22:15:56 +05:00
|
|
|
void ntop46_port(const struct sockaddr *sa, char *str, size_t len)
|
|
|
|
{
|
|
|
|
char ip[40];
|
2022-03-25 18:59:58 +05:00
|
|
|
ntop46(sa, ip, sizeof(ip));
|
2021-03-05 22:15:56 +05:00
|
|
|
switch (sa->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2022-03-25 18:59:58 +05:00
|
|
|
snprintf(str, len, "%s:%u", ip, ntohs(((struct sockaddr_in*)sa)->sin_port));
|
2021-03-05 22:15:56 +05:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2022-03-25 18:59:58 +05:00
|
|
|
snprintf(str, len, "[%s]:%u", ip, ntohs(((struct sockaddr_in6*)sa)->sin6_port));
|
2021-03-05 22:15:56 +05:00
|
|
|
break;
|
|
|
|
default:
|
2022-03-25 18:59:58 +05:00
|
|
|
snprintf(str, len, "%s", ip);
|
2021-03-05 22:15:56 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void print_sockaddr(const struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
char ip_port[48];
|
|
|
|
|
2022-03-25 18:59:58 +05:00
|
|
|
ntop46_port(sa, ip_port, sizeof(ip_port));
|
|
|
|
printf("%s", ip_port);
|
2021-03-05 22:15:56 +05:00
|
|
|
}
|
2021-03-04 16:30:38 +05:00
|
|
|
|
|
|
|
void dbgprint_socket_buffers(int fd)
|
|
|
|
{
|
|
|
|
if (params.debug)
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
socklen_t sz;
|
2022-03-25 18:59:58 +05:00
|
|
|
sz = sizeof(int);
|
|
|
|
if (!getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &v, &sz))
|
|
|
|
DLOG("fd=%d SO_RCVBUF=%d\n", fd, v)
|
|
|
|
sz = sizeof(int);
|
|
|
|
if (!getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &v, &sz))
|
|
|
|
DLOG("fd=%d SO_SNDBUF=%d\n", fd, v)
|
2021-03-04 16:30:38 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool set_socket_buffers(int fd, int rcvbuf, int sndbuf)
|
|
|
|
{
|
2022-03-25 18:59:58 +05:00
|
|
|
DLOG("set_socket_buffers fd=%d rcvbuf=%d sndbuf=%d\n", fd, rcvbuf, sndbuf)
|
2023-11-15 21:36:34 +05:00
|
|
|
if (rcvbuf && setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int)) < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt (SO_RCVBUF)");
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-25 18:59:58 +05:00
|
|
|
if (sndbuf && setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(int)) < 0)
|
2021-03-04 16:30:38 +05:00
|
|
|
{
|
2021-03-05 22:08:49 +05:00
|
|
|
perror("setsockopt (SO_SNDBUF)");
|
2021-03-04 16:30:38 +05:00
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dbgprint_socket_buffers(fd);
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-25 18:59:58 +05:00
|
|
|
|
|
|
|
uint64_t pntoh64(const void *p)
|
|
|
|
{
|
|
|
|
return (uint64_t)*((const uint8_t *)(p)+0) << 56 |
|
|
|
|
(uint64_t)*((const uint8_t *)(p)+1) << 48 |
|
|
|
|
(uint64_t)*((const uint8_t *)(p)+2) << 40 |
|
|
|
|
(uint64_t)*((const uint8_t *)(p)+3) << 32 |
|
|
|
|
(uint64_t)*((const uint8_t *)(p)+4) << 24 |
|
|
|
|
(uint64_t)*((const uint8_t *)(p)+5) << 16 |
|
|
|
|
(uint64_t)*((const uint8_t *)(p)+6) << 8 |
|
|
|
|
(uint64_t)*((const uint8_t *)(p)+7) << 0;
|
|
|
|
}
|
|
|
|
void phton64(uint8_t *p, uint64_t v)
|
|
|
|
{
|
|
|
|
p[0] = (uint8_t)(v >> 56);
|
|
|
|
p[1] = (uint8_t)(v >> 48);
|
|
|
|
p[2] = (uint8_t)(v >> 40);
|
|
|
|
p[3] = (uint8_t)(v >> 32);
|
|
|
|
p[4] = (uint8_t)(v >> 24);
|
|
|
|
p[5] = (uint8_t)(v >> 16);
|
|
|
|
p[6] = (uint8_t)(v >> 8);
|
|
|
|
p[7] = (uint8_t)(v >> 0);
|
|
|
|
}
|
2023-09-07 15:41:25 +05:00
|
|
|
|
2023-11-15 21:36:34 +05:00
|
|
|
bool seq_within(uint32_t s, uint32_t s1, uint32_t s2)
|
|
|
|
{
|
|
|
|
return s2>=s1 && s>=s1 && s<=s2 || s2<s1 && (s<=s2 || s>=s1);
|
|
|
|
}
|
|
|
|
|
2023-11-09 14:08:09 +05:00
|
|
|
bool ipv6_addr_is_zero(const struct in6_addr *a)
|
|
|
|
{
|
|
|
|
return !memcmp(a,"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-07 15:41:25 +05:00
|
|
|
#define INVALID_HEX_DIGIT ((uint8_t)-1)
|
|
|
|
static inline uint8_t parse_hex_digit(char c)
|
|
|
|
{
|
|
|
|
return (c>='0' && c<='9') ? c-'0' : (c>='a' && c<='f') ? c-'a'+0xA : (c>='A' && c<='F') ? c-'A'+0xA : INVALID_HEX_DIGIT;
|
|
|
|
}
|
|
|
|
static inline bool parse_hex_byte(const char *s, uint8_t *pbyte)
|
|
|
|
{
|
|
|
|
uint8_t u,l;
|
|
|
|
u = parse_hex_digit(s[0]);
|
|
|
|
l = parse_hex_digit(s[1]);
|
|
|
|
if (u==INVALID_HEX_DIGIT || l==INVALID_HEX_DIGIT)
|
|
|
|
{
|
|
|
|
*pbyte=0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pbyte=(u<<4) | l;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool parse_hex_str(const char *s, uint8_t *pbuf, size_t *size)
|
|
|
|
{
|
|
|
|
uint8_t *pe = pbuf+*size;
|
|
|
|
*size=0;
|
|
|
|
while(pbuf<pe && *s)
|
|
|
|
{
|
|
|
|
if (!parse_hex_byte(s,pbuf))
|
|
|
|
return false;
|
|
|
|
pbuf++; s+=2; (*size)++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fill_pattern(uint8_t *buf,size_t bufsize,const void *pattern,size_t patsize)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
while (bufsize)
|
|
|
|
{
|
|
|
|
size = bufsize>patsize ? patsize : bufsize;
|
|
|
|
memcpy(buf,pattern,size);
|
|
|
|
buf += size;
|
|
|
|
bufsize -= size;
|
|
|
|
}
|
|
|
|
}
|
2023-11-09 14:08:09 +05:00
|
|
|
|
|
|
|
int fprint_localtime(FILE *F)
|
|
|
|
{
|
|
|
|
struct tm t;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
localtime_r(&now,&t);
|
|
|
|
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);
|
|
|
|
}
|
2024-03-24 00:57:05 +05:00
|
|
|
|
|
|
|
time_t file_mod_time(const char *filename)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
return stat(filename,&st)==-1 ? 0 : st.st_mtime;
|
|
|
|
}
|