mirror of
https://github.com/bol-van/zapret2.git
synced 2026-03-14 06:13:09 +00:00
nfqws2: EINTR safety
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "helpers.h"
|
||||||
|
|
||||||
#define ZCHUNK 16384
|
#define ZCHUNK 16384
|
||||||
#define BUFMIN 128
|
#define BUFMIN 128
|
||||||
@@ -25,7 +26,7 @@ int z_readfile(FILE *F, char **buf, size_t *size, size_t extra_alloc)
|
|||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
zs.avail_in = fread(in, 1, sizeof(in), F);
|
zs.avail_in = fread_safe(in, 1, sizeof(in), F);
|
||||||
if (ferror(F))
|
if (ferror(F))
|
||||||
{
|
{
|
||||||
r = Z_ERRNO;
|
r = Z_ERRNO;
|
||||||
@@ -78,7 +79,7 @@ zerr:
|
|||||||
bool is_gzip(FILE* F)
|
bool is_gzip(FILE* F)
|
||||||
{
|
{
|
||||||
unsigned char magic[2];
|
unsigned char magic[2];
|
||||||
bool b = !fseek(F, 0, SEEK_SET) && fread(magic, 1, 2, F) == 2 && magic[0] == 0x1F && magic[1] == 0x8B;
|
bool b = !fseek(F, 0, SEEK_SET) && fread_safe(magic, 1, 2, F) == 2 && magic[0] == 0x1F && magic[1] == 0x8B;
|
||||||
fseek(F, 0, SEEK_SET);
|
fseek(F, 0, SEEK_SET);
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|||||||
335
nfq2/helpers.c
335
nfq2/helpers.c
@@ -33,7 +33,7 @@ static int cmp_size_t(const void * a, const void * b)
|
|||||||
}
|
}
|
||||||
void qsort_size_t(size_t *array, int ct)
|
void qsort_size_t(size_t *array, int ct)
|
||||||
{
|
{
|
||||||
qsort(array,ct,sizeof(*array),cmp_size_t);
|
qsort(array, ct, sizeof(*array), cmp_size_t);
|
||||||
}
|
}
|
||||||
static int cmp_ssize_t(const void * a, const void * b)
|
static int cmp_ssize_t(const void * a, const void * b)
|
||||||
{
|
{
|
||||||
@@ -41,14 +41,14 @@ static int cmp_ssize_t(const void * a, const void * b)
|
|||||||
}
|
}
|
||||||
void qsort_ssize_t(ssize_t *array, int ct)
|
void qsort_ssize_t(ssize_t *array, int ct)
|
||||||
{
|
{
|
||||||
qsort(array,ct,sizeof(*array),cmp_ssize_t);
|
qsort(array, ct, sizeof(*array), cmp_ssize_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int str_index(const char **strs, int count, const char *str)
|
int str_index(const char **strs, int count, const char *str)
|
||||||
{
|
{
|
||||||
for(int i=0;i<count;i++)
|
for (int i = 0; i < count; i++)
|
||||||
if (!strcmp(strs[i],str)) return i;
|
if (!strcmp(strs[i], str)) return i;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ void rtrim(char *s)
|
|||||||
|
|
||||||
void replace_char(char *s, char from, char to)
|
void replace_char(char *s, char from, char to)
|
||||||
{
|
{
|
||||||
for(;*s;s++) if (*s==from) *s=to;
|
for (; *s; s++) if (*s == from) *s = to;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *strncasestr(const char *s, const char *find, size_t slen)
|
const char *strncasestr(const char *s, const char *find, size_t slen)
|
||||||
@@ -88,18 +88,18 @@ const char *strncasestr(const char *s, const char *find, size_t slen)
|
|||||||
|
|
||||||
static inline bool is_letter(char c)
|
static inline bool is_letter(char c)
|
||||||
{
|
{
|
||||||
return (c>='a' && c<='z') || (c>='A' && c<='Z');
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
||||||
}
|
}
|
||||||
static inline bool is_digit(char c)
|
static inline bool is_digit(char c)
|
||||||
{
|
{
|
||||||
return c>='0' && c<='9';
|
return c >= '0' && c <= '9';
|
||||||
}
|
}
|
||||||
bool is_identifier(const char *p)
|
bool is_identifier(const char *p)
|
||||||
{
|
{
|
||||||
if (*p!='_' && !is_letter(*p))
|
if (*p != '_' && !is_letter(*p))
|
||||||
return false;
|
return false;
|
||||||
for(++p;*p;p++)
|
for (++p; *p; p++)
|
||||||
if (!is_letter(*p) && !is_digit(*p) && *p!='_')
|
if (!is_letter(*p) && !is_digit(*p) && *p != '_')
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ bool load_file(const char *filename, off_t offset, void *buffer, size_t *buffer_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*buffer_size = fread(buffer, 1, *buffer_size, F);
|
*buffer_size = fread_safe(buffer, 1, *buffer_size, F);
|
||||||
if (ferror(F))
|
if (ferror(F))
|
||||||
{
|
{
|
||||||
fclose(F);
|
fclose(F);
|
||||||
@@ -143,7 +143,7 @@ bool save_file(const char *filename, const void *buffer, size_t buffer_size)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fclose(F);
|
fclose(F);
|
||||||
if (wr!=buffer_size)
|
if (wr != buffer_size)
|
||||||
{
|
{
|
||||||
errno = EIO;
|
errno = EIO;
|
||||||
return false;
|
return false;
|
||||||
@@ -152,22 +152,22 @@ bool save_file(const char *filename, const void *buffer, size_t buffer_size)
|
|||||||
}
|
}
|
||||||
bool append_to_list_file(const char *filename, const char *s)
|
bool append_to_list_file(const char *filename, const char *s)
|
||||||
{
|
{
|
||||||
FILE *F = fopen(filename,"at");
|
FILE *F = fopen(filename, "at");
|
||||||
if (!F) return false;
|
if (!F) return false;
|
||||||
bool bOK = fprintf(F,"%s\n",s)>0;
|
bool bOK = fprintf(F, "%s\n", s) > 0;
|
||||||
fclose(F);
|
fclose(F);
|
||||||
return bOK;
|
return bOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void expand_bits(void *target, const void *source, unsigned int source_bitlen, unsigned int target_bytelen)
|
void expand_bits(void *target, const void *source, unsigned int source_bitlen, unsigned int target_bytelen)
|
||||||
{
|
{
|
||||||
unsigned int target_bitlen = target_bytelen<<3;
|
unsigned int target_bitlen = target_bytelen << 3;
|
||||||
unsigned int bitlen = target_bitlen<source_bitlen ? target_bitlen : source_bitlen;
|
unsigned int bitlen = target_bitlen < source_bitlen ? target_bitlen : source_bitlen;
|
||||||
unsigned int bytelen = bitlen>>3;
|
unsigned int bytelen = bitlen >> 3;
|
||||||
|
|
||||||
if ((target_bytelen-bytelen)>=1) memset((uint8_t*)target+bytelen,0,target_bytelen-bytelen);
|
if ((target_bytelen - bytelen) >= 1) memset((uint8_t*)target + bytelen, 0, target_bytelen - bytelen);
|
||||||
memcpy(target,source,bytelen);
|
memcpy(target, source, bytelen);
|
||||||
if ((bitlen &= 7)) ((uint8_t*)target)[bytelen] = ((uint8_t*)source)[bytelen] & (~((1 << (8-bitlen)) - 1));
|
if ((bitlen &= 7)) ((uint8_t*)target)[bytelen] = ((uint8_t*)source)[bytelen] & (~((1 << (8 - bitlen)) - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
// " [fd00::1]" => "fd00::1"
|
// " [fd00::1]" => "fd00::1"
|
||||||
@@ -179,53 +179,53 @@ void expand_bits(void *target, const void *source, unsigned int source_bitlen, u
|
|||||||
bool strip_host_to_ip(char *host)
|
bool strip_host_to_ip(char *host)
|
||||||
{
|
{
|
||||||
size_t l;
|
size_t l;
|
||||||
char *h,*p;
|
char *h, *p;
|
||||||
uint8_t addr[16];
|
uint8_t addr[16];
|
||||||
|
|
||||||
for (h = host ; *h==' ' || *h=='\t' ; h++);
|
for (h = host; *h == ' ' || *h == '\t'; h++);
|
||||||
l = strlen(h);
|
l = strlen(h);
|
||||||
if (l>=2)
|
if (l >= 2)
|
||||||
{
|
{
|
||||||
if (*h=='[')
|
if (*h == '[')
|
||||||
{
|
{
|
||||||
// ipv6 ?
|
// ipv6 ?
|
||||||
for (p=++h ; *p && *p!=']' ; p++);
|
for (p = ++h; *p && *p != ']'; p++);
|
||||||
if (*p==']')
|
if (*p == ']')
|
||||||
{
|
{
|
||||||
l = p-h;
|
l = p - h;
|
||||||
memmove(host,h,l);
|
memmove(host, h, l);
|
||||||
host[l]=0;
|
host[l] = 0;
|
||||||
return inet_pton(AF_INET6, host, addr)>0;
|
return inet_pton(AF_INET6, host, addr) > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (inet_pton(AF_INET6, h, addr)>0)
|
if (inet_pton(AF_INET6, h, addr) > 0)
|
||||||
{
|
{
|
||||||
// ipv6 ?
|
// ipv6 ?
|
||||||
if (host!=h)
|
if (host != h)
|
||||||
{
|
{
|
||||||
l = strlen(h);
|
l = strlen(h);
|
||||||
memmove(host,h,l);
|
memmove(host, h, l);
|
||||||
host[l]=0;
|
host[l] = 0;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// ipv4 ?
|
// ipv4 ?
|
||||||
for (p=h ; *p && *p!=':' ; p++);
|
for (p = h; *p && *p != ':'; p++);
|
||||||
l = p-h;
|
l = p - h;
|
||||||
if (host!=h) memmove(host,h,l);
|
if (host != h) memmove(host, h, l);
|
||||||
host[l]=0;
|
host[l] = 0;
|
||||||
return inet_pton(AF_INET, host, addr)>0;
|
return inet_pton(AF_INET, host, addr) > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ntopa46(const struct in_addr *ip, const struct in6_addr *ip6,char *str, size_t len)
|
void ntopa46(const struct in_addr *ip, const struct in6_addr *ip6, char *str, size_t len)
|
||||||
{
|
{
|
||||||
if (!len) return;
|
if (!len) return;
|
||||||
*str = 0;
|
*str = 0;
|
||||||
@@ -235,8 +235,8 @@ void ntopa46(const struct in_addr *ip, const struct in6_addr *ip6,char *str, siz
|
|||||||
}
|
}
|
||||||
void ntop46(const struct sockaddr *sa, char *str, size_t len)
|
void ntop46(const struct sockaddr *sa, char *str, size_t len)
|
||||||
{
|
{
|
||||||
ntopa46(sa->sa_family==AF_INET ? &((struct sockaddr_in*)sa)->sin_addr : NULL,
|
ntopa46(sa->sa_family == AF_INET ? &((struct sockaddr_in*)sa)->sin_addr : NULL,
|
||||||
sa->sa_family==AF_INET6 ? &((struct sockaddr_in6*)sa)->sin6_addr : NULL,
|
sa->sa_family == AF_INET6 ? &((struct sockaddr_in6*)sa)->sin6_addr : NULL,
|
||||||
str, len);
|
str, len);
|
||||||
}
|
}
|
||||||
void ntop46_port(const struct sockaddr *sa, char *str, size_t len)
|
void ntop46_port(const struct sockaddr *sa, char *str, size_t len)
|
||||||
@@ -265,32 +265,32 @@ void print_sockaddr(const struct sockaddr *sa)
|
|||||||
|
|
||||||
uint16_t saport(const struct sockaddr *sa)
|
uint16_t saport(const struct sockaddr *sa)
|
||||||
{
|
{
|
||||||
return ntohs(sa->sa_family==AF_INET ? ((struct sockaddr_in*)sa)->sin_port :
|
return ntohs(sa->sa_family == AF_INET ? ((struct sockaddr_in*)sa)->sin_port :
|
||||||
sa->sa_family==AF_INET6 ? ((struct sockaddr_in6*)sa)->sin6_port : 0);
|
sa->sa_family == AF_INET6 ? ((struct sockaddr_in6*)sa)->sin6_port : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sa_has_addr(const struct sockaddr *sa)
|
bool sa_has_addr(const struct sockaddr *sa)
|
||||||
{
|
{
|
||||||
switch(sa->sa_family)
|
switch (sa->sa_family)
|
||||||
{
|
{
|
||||||
case AF_INET:
|
case AF_INET:
|
||||||
return ((struct sockaddr_in*)sa)->sin_addr.s_addr!=INADDR_ANY;
|
return ((struct sockaddr_in*)sa)->sin_addr.s_addr != INADDR_ANY;
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
return memcmp(((struct sockaddr_in6*)sa)->sin6_addr.s6_addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
|
return memcmp(((struct sockaddr_in6*)sa)->sin6_addr.s6_addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool seq_within(uint32_t s, uint32_t s1, uint32_t s2)
|
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));
|
return (s2 >= s1 && s >= s1 && s <= s2) || (s2 < s1 && (s <= s2 || s >= s1));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ipv6_addr_is_zero(const struct in6_addr *a)
|
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);
|
return !memcmp(a, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -309,8 +309,8 @@ uint32_t pntoh24(const uint8_t *p)
|
|||||||
}
|
}
|
||||||
void phton24(uint8_t *p, uint32_t v)
|
void phton24(uint8_t *p, uint32_t v)
|
||||||
{
|
{
|
||||||
p[0] = (uint8_t)(v>>16);
|
p[0] = (uint8_t)(v >> 16);
|
||||||
p[1] = (uint8_t)(v>>8);
|
p[1] = (uint8_t)(v >> 8);
|
||||||
p[2] = (uint8_t)v;
|
p[2] = (uint8_t)v;
|
||||||
}
|
}
|
||||||
uint32_t pntoh32(const uint8_t *p)
|
uint32_t pntoh32(const uint8_t *p)
|
||||||
@@ -319,9 +319,9 @@ uint32_t pntoh32(const uint8_t *p)
|
|||||||
}
|
}
|
||||||
void phton32(uint8_t *p, uint32_t v)
|
void phton32(uint8_t *p, uint32_t v)
|
||||||
{
|
{
|
||||||
p[0] = (uint8_t)(v>>24);
|
p[0] = (uint8_t)(v >> 24);
|
||||||
p[1] = (uint8_t)(v>>16);
|
p[1] = (uint8_t)(v >> 16);
|
||||||
p[2] = (uint8_t)(v>>8);
|
p[2] = (uint8_t)(v >> 8);
|
||||||
p[3] = (uint8_t)v;
|
p[3] = (uint8_t)v;
|
||||||
}
|
}
|
||||||
uint64_t pntoh48(const uint8_t *p)
|
uint64_t pntoh48(const uint8_t *p)
|
||||||
@@ -330,11 +330,11 @@ uint64_t pntoh48(const uint8_t *p)
|
|||||||
}
|
}
|
||||||
void phton48(uint8_t *p, uint64_t v)
|
void phton48(uint8_t *p, uint64_t v)
|
||||||
{
|
{
|
||||||
p[0] = (uint8_t)(v>>40);
|
p[0] = (uint8_t)(v >> 40);
|
||||||
p[1] = (uint8_t)(v>>32);
|
p[1] = (uint8_t)(v >> 32);
|
||||||
p[2] = (uint8_t)(v>>24);
|
p[2] = (uint8_t)(v >> 24);
|
||||||
p[3] = (uint8_t)(v>>16);
|
p[3] = (uint8_t)(v >> 16);
|
||||||
p[4] = (uint8_t)(v>>8);
|
p[4] = (uint8_t)(v >> 8);
|
||||||
p[5] = (uint8_t)v;
|
p[5] = (uint8_t)v;
|
||||||
}
|
}
|
||||||
uint64_t pntoh64(const uint8_t *p)
|
uint64_t pntoh64(const uint8_t *p)
|
||||||
@@ -343,24 +343,24 @@ uint64_t pntoh64(const uint8_t *p)
|
|||||||
}
|
}
|
||||||
void phton64(uint8_t *p, uint64_t v)
|
void phton64(uint8_t *p, uint64_t v)
|
||||||
{
|
{
|
||||||
p[0] = (uint8_t)(v>>56);
|
p[0] = (uint8_t)(v >> 56);
|
||||||
p[1] = (uint8_t)(v>>48);
|
p[1] = (uint8_t)(v >> 48);
|
||||||
p[2] = (uint8_t)(v>>40);
|
p[2] = (uint8_t)(v >> 40);
|
||||||
p[3] = (uint8_t)(v>>32);
|
p[3] = (uint8_t)(v >> 32);
|
||||||
p[4] = (uint8_t)(v>>24);
|
p[4] = (uint8_t)(v >> 24);
|
||||||
p[5] = (uint8_t)(v>>16);
|
p[5] = (uint8_t)(v >> 16);
|
||||||
p[6] = (uint8_t)(v>>8);
|
p[6] = (uint8_t)(v >> 8);
|
||||||
p[7] = (uint8_t)v;
|
p[7] = (uint8_t)v;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t bswap16(uint16_t u)
|
uint16_t bswap16(uint16_t u)
|
||||||
{
|
{
|
||||||
// __builtin_bswap16 is absent in ancient lexra gcc 4.6
|
// __builtin_bswap16 is absent in ancient lexra gcc 4.6
|
||||||
return (u>>8) | ((u&0xFF)<<8);
|
return (u >> 8) | ((u & 0xFF) << 8);
|
||||||
}
|
}
|
||||||
uint32_t bswap24(uint32_t u)
|
uint32_t bswap24(uint32_t u)
|
||||||
{
|
{
|
||||||
return (u>>16) & 0xFF | u & 0xFF00 | (u<<16) & 0xFF0000;
|
return (u >> 16) & 0xFF | u & 0xFF00 | (u << 16) & 0xFF0000;
|
||||||
}
|
}
|
||||||
uint64_t bswap48(uint64_t u)
|
uint64_t bswap48(uint64_t u)
|
||||||
{
|
{
|
||||||
@@ -371,39 +371,39 @@ uint64_t bswap48(uint64_t u)
|
|||||||
#define INVALID_HEX_DIGIT ((uint8_t)-1)
|
#define INVALID_HEX_DIGIT ((uint8_t)-1)
|
||||||
static inline uint8_t parse_hex_digit(char c)
|
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;
|
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)
|
static inline bool parse_hex_byte(const char *s, uint8_t *pbyte)
|
||||||
{
|
{
|
||||||
uint8_t u,l;
|
uint8_t u, l;
|
||||||
u = parse_hex_digit(s[0]);
|
u = parse_hex_digit(s[0]);
|
||||||
l = parse_hex_digit(s[1]);
|
l = parse_hex_digit(s[1]);
|
||||||
if (u==INVALID_HEX_DIGIT || l==INVALID_HEX_DIGIT)
|
if (u == INVALID_HEX_DIGIT || l == INVALID_HEX_DIGIT)
|
||||||
{
|
{
|
||||||
*pbyte=0;
|
*pbyte = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*pbyte=(u<<4) | l;
|
*pbyte = (u << 4) | l;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool parse_hex_str(const char *s, uint8_t *pbuf, size_t *size)
|
bool parse_hex_str(const char *s, uint8_t *pbuf, size_t *size)
|
||||||
{
|
{
|
||||||
uint8_t *pe = pbuf+*size;
|
uint8_t *pe = pbuf + *size;
|
||||||
*size=0;
|
*size = 0;
|
||||||
while(pbuf<pe && *s)
|
while (pbuf < pe && *s)
|
||||||
{
|
{
|
||||||
if (!parse_hex_byte(s,pbuf))
|
if (!parse_hex_byte(s, pbuf))
|
||||||
return false;
|
return false;
|
||||||
pbuf++; s+=2; (*size)++;
|
pbuf++; s += 2; (*size)++;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
char hex_digit(uint8_t v)
|
char hex_digit(uint8_t v)
|
||||||
{
|
{
|
||||||
return v<=9 ? '0'+v : (v<=0xF) ? v+'A'-0xA : '?';
|
return v <= 9 ? '0' + v : (v <= 0xF) ? v + 'A' - 0xA : '?';
|
||||||
}
|
}
|
||||||
|
|
||||||
int fprint_localtime(FILE *F)
|
int fprint_localtime(FILE *F)
|
||||||
@@ -412,39 +412,39 @@ int fprint_localtime(FILE *F)
|
|||||||
time_t now;
|
time_t now;
|
||||||
|
|
||||||
time(&now);
|
time(&now);
|
||||||
localtime_r(&now,&t);
|
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);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_size(const char *filename, off_t *size)
|
bool file_size(const char *filename, off_t *size)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(filename,&st)==-1) return false;
|
if (stat(filename, &st) == -1) return false;
|
||||||
*size = st.st_size;
|
*size = st.st_size;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
time_t file_mod_time(const char *filename)
|
time_t file_mod_time(const char *filename)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
return stat(filename,&st)==-1 ? 0 : st.st_mtime;
|
return stat(filename, &st) == -1 ? 0 : st.st_mtime;
|
||||||
}
|
}
|
||||||
bool file_mod_signature(const char *filename, file_mod_sig *ms)
|
bool file_mod_signature(const char *filename, file_mod_sig *ms)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(filename,&st)==-1)
|
if (stat(filename, &st) == -1)
|
||||||
{
|
{
|
||||||
FILE_MOD_RESET(ms);
|
FILE_MOD_RESET(ms);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ms->mod_time=st.st_mtime;
|
ms->mod_time = st.st_mtime;
|
||||||
ms->size=st.st_size;
|
ms->size = st.st_size;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_open_test(const char *filename, int flags)
|
bool file_open_test(const char *filename, int flags)
|
||||||
{
|
{
|
||||||
int fd = open(filename,flags);
|
int fd = open(filename, flags);
|
||||||
if (fd>=0)
|
if (fd >= 0)
|
||||||
{
|
{
|
||||||
close(fd);
|
close(fd);
|
||||||
return true;
|
return true;
|
||||||
@@ -453,54 +453,54 @@ bool file_open_test(const char *filename, int flags)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void fill_random_bytes(uint8_t *p,size_t sz)
|
void fill_random_bytes(uint8_t *p, size_t sz)
|
||||||
{
|
{
|
||||||
size_t k;
|
size_t k;
|
||||||
if (sz)
|
if (sz)
|
||||||
{
|
{
|
||||||
// alignment
|
// alignment
|
||||||
if ((size_t)p & 1) { *p=(uint8_t)random(); sz--; p++; }
|
if ((size_t)p & 1) { *p = (uint8_t)random(); sz--; p++; }
|
||||||
// random has only 31 bits of entropy. not 32 bits
|
// random has only 31 bits of entropy. not 32 bits
|
||||||
for (k=0 ; (k+1)<sz ; k+=2) *(uint16_t*)(p+k) = (uint16_t)random();
|
for (k = 0; (k + 1) < sz; k += 2) *(uint16_t*)(p + k) = (uint16_t)random();
|
||||||
if (sz & 1) p[sz-1]=(uint8_t)random();
|
if (sz & 1) p[sz - 1] = (uint8_t)random();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void fill_random_az(uint8_t *p,size_t sz)
|
void fill_random_az(uint8_t *p, size_t sz)
|
||||||
{
|
{
|
||||||
size_t k;
|
size_t k;
|
||||||
for(k=0;k<sz;k++) p[k] = 'a'+(random() % ('z'-'a'+1));
|
for (k = 0; k < sz; k++) p[k] = 'a' + (random() % ('z' - 'a' + 1));
|
||||||
}
|
}
|
||||||
void fill_random_az09(uint8_t *p,size_t sz)
|
void fill_random_az09(uint8_t *p, size_t sz)
|
||||||
{
|
{
|
||||||
size_t k;
|
size_t k;
|
||||||
uint8_t rnd;
|
uint8_t rnd;
|
||||||
for(k=0;k<sz;k++)
|
for (k = 0; k < sz; k++)
|
||||||
{
|
{
|
||||||
rnd = random() % (10 + 'z'-'a'+1);
|
rnd = random() % (10 + 'z' - 'a' + 1);
|
||||||
p[k] = rnd<10 ? rnd+'0' : 'a'+rnd-10;
|
p[k] = rnd < 10 ? rnd + '0' : 'a' + rnd - 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#if defined(__FreeBSD__) && __FreeBSD_version <= 1200000
|
#if defined(__FreeBSD__) && __FreeBSD_version <= 1200000
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
int getentropy(void *buf, size_t len)
|
int getentropy(void *buf, size_t len)
|
||||||
{
|
{
|
||||||
int mib[2];
|
int mib[2];
|
||||||
size_t size = len;
|
size_t size = len;
|
||||||
|
|
||||||
// Check for reasonable length (getentropy limits to 256)
|
// Check for reasonable length (getentropy limits to 256)
|
||||||
if (len > 256) {
|
if (len > 256) {
|
||||||
errno = EIO;
|
errno = EIO;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mib[0] = CTL_KERN;
|
mib[0] = CTL_KERN;
|
||||||
mib[1] = KERN_ARND;
|
mib[1] = KERN_ARND;
|
||||||
|
|
||||||
if (sysctl(mib, 2, buf, &size, NULL, 0) == -1) {
|
if (sysctl(mib, 2, buf, &size, NULL, 0) == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (size == len) ? 0 : -1;
|
return (size == len) ? 0 : -1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -508,48 +508,89 @@ int getentropy(void *buf, size_t len)
|
|||||||
ssize_t read_intr(int fd, void *buf, size_t count)
|
ssize_t read_intr(int fd, void *buf, size_t count)
|
||||||
{
|
{
|
||||||
ssize_t rd;
|
ssize_t rd;
|
||||||
while ((rd=read(fd,buf,count))<0 && errno==EINTR);
|
while ((rd = read(fd, buf, count)) < 0 && errno == EINTR);
|
||||||
return rd;
|
return rd;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fill_crypto_random_bytes(uint8_t *p,size_t sz)
|
size_t fread_safe(void *ptr, size_t size, size_t nmemb, FILE *F)
|
||||||
|
{
|
||||||
|
size_t total_read = 0;
|
||||||
|
while (total_read < nmemb)
|
||||||
|
{
|
||||||
|
size_t result = fread((uint8_t*)ptr + (total_read * size), size, nmemb - total_read, F);
|
||||||
|
if (result < (nmemb - total_read))
|
||||||
|
{
|
||||||
|
if (errno == EINTR)
|
||||||
|
{
|
||||||
|
clearerr(F);
|
||||||
|
total_read += result;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
total_read += result;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
total_read += result;
|
||||||
|
}
|
||||||
|
return total_read;
|
||||||
|
}
|
||||||
|
char* fgets_safe(char *s, int size, FILE *stream)
|
||||||
|
{
|
||||||
|
char *result;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if ((result = fgets(s, size, stream))) return result;
|
||||||
|
if (ferror(stream))
|
||||||
|
{
|
||||||
|
if (errno == EINTR)
|
||||||
|
{
|
||||||
|
clearerr(stream);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (feof(stream)) return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fill_crypto_random_bytes(uint8_t *p, size_t sz)
|
||||||
{
|
{
|
||||||
ssize_t rd;
|
ssize_t rd;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
#if defined(__linux__) || defined(__CYGWIN__)
|
#if defined(__linux__) || defined(__CYGWIN__)
|
||||||
for(; sz && (rd=getrandom(p,sz,GRND_NONBLOCK))>0 ; p+=rd, sz-=rd);
|
for (; sz && (rd = getrandom(p, sz, GRND_NONBLOCK)) > 0; p += rd, sz -= rd);
|
||||||
if (sz)
|
if (sz)
|
||||||
#elif defined(BSD)
|
#elif defined(BSD)
|
||||||
while(sz)
|
while (sz)
|
||||||
{
|
{
|
||||||
rd = sz<256 ? sz : 256; // BSD limitation
|
rd = sz < 256 ? sz : 256; // BSD limitation
|
||||||
if (getentropy(p,rd)) break;
|
if (getentropy(p, rd)) break;
|
||||||
p+=rd; sz-=rd;
|
p += rd; sz -= rd;
|
||||||
}
|
}
|
||||||
if (sz)
|
if (sz)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
if ((fd = open("/dev/random",O_NONBLOCK))>=0)
|
if ((fd = open("/dev/random", O_NONBLOCK)) >= 0)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if ((rd=read_intr(fd,p,sz))>0)
|
if ((rd = read_intr(fd, p, sz)) > 0)
|
||||||
{
|
{
|
||||||
p+=rd; sz-=rd;
|
p += rd; sz -= rd;
|
||||||
}
|
}
|
||||||
} while(sz && rd>0);
|
} while (sz && rd > 0);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
if (sz && (fd = open("/dev/urandom",0))>=0)
|
if (sz && (fd = open("/dev/urandom", 0)) >= 0)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if ((rd=read_intr(fd,p,sz))>0)
|
if ((rd = read_intr(fd, p, sz)) > 0)
|
||||||
{
|
{
|
||||||
p+=rd; sz-=rd;
|
p += rd; sz -= rd;
|
||||||
}
|
}
|
||||||
} while(sz && rd>0);
|
} while (sz && rd > 0);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -557,33 +598,33 @@ bool fill_crypto_random_bytes(uint8_t *p,size_t sz)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__GNUC__) && !defined(__llvm__)
|
#if defined(__GNUC__) && !defined(__llvm__)
|
||||||
__attribute__((optimize ("no-strict-aliasing")))
|
__attribute__((optimize("no-strict-aliasing")))
|
||||||
#endif
|
#endif
|
||||||
void bxor(const uint8_t *x1, const uint8_t *x2, uint8_t *result, size_t sz)
|
void bxor(const uint8_t *x1, const uint8_t *x2, uint8_t *result, size_t sz)
|
||||||
{
|
{
|
||||||
for (; sz>=8 ; x1+=8, x2+=8, result+=8, sz-=8)
|
for (; sz >= 8; x1 += 8, x2 += 8, result += 8, sz -= 8)
|
||||||
*(uint64_t*)result = *(uint64_t*)x1 ^ *(uint64_t*)x2;
|
*(uint64_t*)result = *(uint64_t*)x1 ^ *(uint64_t*)x2;
|
||||||
for (; sz ; x1++, x2++, result++, sz--)
|
for (; sz; x1++, x2++, result++, sz--)
|
||||||
*result = *x1 ^ *x2;
|
*result = *x1 ^ *x2;
|
||||||
}
|
}
|
||||||
#if defined(__GNUC__) && !defined(__llvm__)
|
#if defined(__GNUC__) && !defined(__llvm__)
|
||||||
__attribute__((optimize ("no-strict-aliasing")))
|
__attribute__((optimize("no-strict-aliasing")))
|
||||||
#endif
|
#endif
|
||||||
void bor(const uint8_t *x1, const uint8_t *x2, uint8_t *result, size_t sz)
|
void bor(const uint8_t *x1, const uint8_t *x2, uint8_t *result, size_t sz)
|
||||||
{
|
{
|
||||||
for (; sz>=8 ; x1+=8, x2+=8, result+=8, sz-=8)
|
for (; sz >= 8; x1 += 8, x2 += 8, result += 8, sz -= 8)
|
||||||
*(uint64_t*)result = *(uint64_t*)x1 | *(uint64_t*)x2;
|
*(uint64_t*)result = *(uint64_t*)x1 | *(uint64_t*)x2;
|
||||||
for (; sz ; x1++, x2++, result++, sz--)
|
for (; sz; x1++, x2++, result++, sz--)
|
||||||
*result = *x1 | *x2;
|
*result = *x1 | *x2;
|
||||||
}
|
}
|
||||||
#if defined(__GNUC__) && !defined(__llvm__)
|
#if defined(__GNUC__) && !defined(__llvm__)
|
||||||
__attribute__((optimize ("no-strict-aliasing")))
|
__attribute__((optimize("no-strict-aliasing")))
|
||||||
#endif
|
#endif
|
||||||
void band(const uint8_t *x1, const uint8_t *x2, uint8_t *result, size_t sz)
|
void band(const uint8_t *x1, const uint8_t *x2, uint8_t *result, size_t sz)
|
||||||
{
|
{
|
||||||
for (; sz>=8 ; x1+=8, x2+=8, result+=8, sz-=8)
|
for (; sz >= 8; x1 += 8, x2 += 8, result += 8, sz -= 8)
|
||||||
*(uint64_t*)result = *(uint64_t*)x1 & *(uint64_t*)x2;
|
*(uint64_t*)result = *(uint64_t*)x1 & *(uint64_t*)x2;
|
||||||
for (; sz ; x1++, x2++, result++, sz--)
|
for (; sz; x1++, x2++, result++, sz--)
|
||||||
*result = *x1 & *x2;
|
*result = *x1 & *x2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -608,12 +649,12 @@ void close_std_and_exit(int code)
|
|||||||
|
|
||||||
bool set_env_exedir(const char *argv0)
|
bool set_env_exedir(const char *argv0)
|
||||||
{
|
{
|
||||||
char *s,*d;
|
char *s, *d;
|
||||||
bool bOK=false;
|
bool bOK = false;
|
||||||
if ((s = strdup(argv0)))
|
if ((s = strdup(argv0)))
|
||||||
{
|
{
|
||||||
if ((d = dirname(s)))
|
if ((d = dirname(s)))
|
||||||
bOK = !setenv("EXEDIR",d,1);
|
bOK = !setenv("EXEDIR", d, 1);
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
return bOK;
|
return bOK;
|
||||||
@@ -632,28 +673,28 @@ bool parse_int16(const char *p, int16_t *v)
|
|||||||
|
|
||||||
uint32_t mask_from_bitcount(uint32_t zct)
|
uint32_t mask_from_bitcount(uint32_t zct)
|
||||||
{
|
{
|
||||||
return zct<32 ? ~((1u << zct) - 1) : 0;
|
return zct < 32 ? ~((1u << zct) - 1) : 0;
|
||||||
}
|
}
|
||||||
static void mask_from_bitcount6_make(uint32_t zct, struct in6_addr *a)
|
static void mask_from_bitcount6_make(uint32_t zct, struct in6_addr *a)
|
||||||
{
|
{
|
||||||
if (zct >= 128)
|
if (zct >= 128)
|
||||||
memset(a->s6_addr,0x00,16);
|
memset(a->s6_addr, 0x00, 16);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int32_t n = (127 - zct) >> 3;
|
int32_t n = (127 - zct) >> 3;
|
||||||
memset(a->s6_addr,0xFF,n);
|
memset(a->s6_addr, 0xFF, n);
|
||||||
memset(a->s6_addr+n,0x00,16-n);
|
memset(a->s6_addr + n, 0x00, 16 - n);
|
||||||
a->s6_addr[n] = ~((1u << (zct & 7)) - 1);
|
a->s6_addr[n] = ~((1u << (zct & 7)) - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static struct in6_addr ip6_mask[129];
|
static struct in6_addr ip6_mask[129];
|
||||||
void mask_from_bitcount6_prepare(void)
|
void mask_from_bitcount6_prepare(void)
|
||||||
{
|
{
|
||||||
for (int zct=0;zct<=128;zct++) mask_from_bitcount6_make(zct, ip6_mask+zct);
|
for (int zct = 0; zct <= 128; zct++) mask_from_bitcount6_make(zct, ip6_mask + zct);
|
||||||
}
|
}
|
||||||
const struct in6_addr *mask_from_bitcount6(uint32_t zct)
|
const struct in6_addr *mask_from_bitcount6(uint32_t zct)
|
||||||
{
|
{
|
||||||
return ip6_mask+zct;
|
return ip6_mask + zct;
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t boottime(void)
|
time_t boottime(void)
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ const char *strncasestr(const char *s,const char *find, size_t slen);
|
|||||||
bool is_identifier(const char *p);
|
bool is_identifier(const char *p);
|
||||||
|
|
||||||
ssize_t read_intr(int fd, void *buf, size_t count);
|
ssize_t read_intr(int fd, void *buf, size_t count);
|
||||||
|
size_t fread_safe(void *ptr, size_t size, size_t nmemb, FILE *F);
|
||||||
|
char* fgets_safe(char *s, int size, FILE *stream);
|
||||||
|
|
||||||
bool load_file(const char *filename, off_t offset, void *buffer, size_t *buffer_size);
|
bool load_file(const char *filename, off_t offset, void *buffer, size_t *buffer_size);
|
||||||
bool save_file(const char *filename, const void *buffer, size_t buffer_size);
|
bool save_file(const char *filename, const void *buffer, size_t buffer_size);
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ bool AppendHostList(hostlist_pool **hostlist, const char *filename)
|
|||||||
{
|
{
|
||||||
DLOG_CONDUP("loading plain text list\n");
|
DLOG_CONDUP("loading plain text list\n");
|
||||||
|
|
||||||
while (fgets(s, sizeof(s), F))
|
while (fgets_safe(s, sizeof(s), F))
|
||||||
{
|
{
|
||||||
p = s;
|
p = s;
|
||||||
if (!addpool(hostlist,&p,p+strlen(p),&ct))
|
if (!addpool(hostlist,&p,p+strlen(p),&ct))
|
||||||
|
|||||||
@@ -625,7 +625,7 @@ static int dvt_main(void)
|
|||||||
if (FD_ISSET(fd[i], &fdset))
|
if (FD_ISSET(fd[i], &fdset))
|
||||||
{
|
{
|
||||||
socklen = sizeof(sa_from);
|
socklen = sizeof(sa_from);
|
||||||
rd = recvfrom(fd[i], buf, sizeof(buf), 0, (struct sockaddr*)&sa_from, &socklen);
|
while ((rd = recvfrom(fd[i], buf, sizeof(buf), 0, (struct sockaddr*)&sa_from, &socklen))<0 && errno==EINTR);
|
||||||
if (rd < 0)
|
if (rd < 0)
|
||||||
{
|
{
|
||||||
DLOG_PERROR("recvfrom");
|
DLOG_PERROR("recvfrom");
|
||||||
|
|||||||
Reference in New Issue
Block a user