Template
1
0
mirror of https://github.com/bol-van/zapret2.git synced 2026-03-13 22:03:09 +00:00

mdig: --eagain, --eagain-delay

This commit is contained in:
bol-van
2026-01-09 11:42:33 +03:00
parent fdb9c9be60
commit 801328dc02
3 changed files with 45 additions and 10 deletions

View File

@@ -168,3 +168,4 @@ v0.8.1
* nfqws2, zapret-lib: gzip compression and decompression * nfqws2, zapret-lib: gzip compression and decompression
* nfqws2: ignore trailing spaces and tabs in hostlists and ipsets. "host.com " or "1.2.3.4 " are ok now * nfqws2: ignore trailing spaces and tabs in hostlists and ipsets. "host.com " or "1.2.3.4 " are ok now
* init.d: 99-lan-filter custom script * init.d: 99-lan-filter custom script
* mdig: --eagain, --eagain-delay

View File

@@ -4324,8 +4324,10 @@ ip2net фильтрует входные данные, выкидывая неп
Она берет из stdin список доменов и выводит в stdout результат ресолвинга. Ошибки выводятся в stderr. Она берет из stdin список доменов и выводит в stdout результат ресолвинга. Ошибки выводятся в stderr.
``` ```
--threads=<threads_number> ; количество потоков. по умолчанию 1.
--family=<4|6|46> ; выбор семейства IP адресов : ipv4, ipv6, ipv4+ipv6 --family=<4|6|46> ; выбор семейства IP адресов : ipv4, ipv6, ipv4+ipv6
--threads=<threads_number> ; количество потоков. по умолчанию 1.
--eagain=<eagain_retries> ; количество попыток повтора после EAGAIN. по умолчанию 10
--eagain-delay=<ms> ; время ожидания в мсек между повторами по EAGAIN. по умолчанию 500.
--verbose ; дебаг-лог на консоль --verbose ; дебаг-лог на консоль
--stats=N ; выводить статистику каждые N доменов --stats=N ; выводить статистику каждые N доменов
--log-resolved=<file> ; сохранять успешно отресолвленные домены в файл --log-resolved=<file> ; сохранять успешно отресолвленные домены в файл

View File

@@ -30,7 +30,8 @@
#endif #endif
#include <time.h> #include <time.h>
#define RESOLVER_EAGAIN_ATTEMPTS 3 #define RESOLVER_EAGAIN_ATTEMPTS 10
#define RESOLVER_EAGAIN_DELAY 500
static void trimstr(char *s) static void trimstr(char *s)
{ {
@@ -97,7 +98,7 @@ static struct
{ {
char verbose; char verbose;
char family; char family;
int threads; int threads, eagain, eagain_delay;
time_t start_time; time_t start_time;
pthread_mutex_t flock; pthread_mutex_t flock;
pthread_mutex_t slock; // stats lock pthread_mutex_t slock; // stats lock
@@ -193,11 +194,12 @@ static void *t_resolver(void *arg)
int i, r; int i, r;
char dom[256]; char dom[256];
bool is_ok; bool is_ok;
struct addrinfo hints; struct addrinfo hints, *result;
struct addrinfo *result; struct timespec ts_eagain = { .tv_sec = glob.eagain_delay/1000, .tv_nsec=glob.eagain_delay%1000*1000000 };
VLOG("started"); VLOG("started");
memset(&hints, 0, sizeof(struct addrinfo)); memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = (glob.family == FAMILY4) ? AF_INET : (glob.family == FAMILY6) ? AF_INET6 : AF_UNSPEC; hints.ai_family = (glob.family == FAMILY4) ? AF_INET : (glob.family == FAMILY6) ? AF_INET6 : AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
@@ -244,12 +246,16 @@ static void *t_resolver(void *arg)
else if (dom_valid(dom)) else if (dom_valid(dom))
{ {
VLOG("resolving %s", dom); VLOG("resolving %s", dom);
for (i = 0; i < RESOLVER_EAGAIN_ATTEMPTS; i++) for (i = 0; i < glob.eagain; i++)
{ {
if ((r = getaddrinfo(dom, NULL, &hints, &result))) if ((r = getaddrinfo(dom, NULL, &hints, &result)))
{ {
VLOG("failed to resolve %s : result %d (%s)", dom, r, eai_str(r)); VLOG("failed to resolve %s : result %d (%s)", dom, r, eai_str(r));
if (r == EAI_AGAIN) continue; // temporary failure. should retry if (r == EAI_AGAIN)
{
nanosleep(&ts_eagain, NULL);
continue; // temporary failure. should retry
}
} }
else else
{ {
@@ -447,14 +453,18 @@ int dns_parse_query()
static void exithelp(void) static void exithelp(void)
{ {
printf( printf(
" --threads=<threads_number>\n"
" --family=<4|6|46>\t\t; ipv4, ipv6, ipv4+ipv6\n" " --family=<4|6|46>\t\t; ipv4, ipv6, ipv4+ipv6\n"
" --threads=<threads_number>\n"
" --eagain=<eagain_retries>\t; how many times to retry if EAGAIN received. default %u\n"
" --eagain-delay=<ms>\t\t; time in msec to wait between EAGAIN attempts. default %u\n"
" --verbose\t\t\t; print query progress to stderr\n" " --verbose\t\t\t; print query progress to stderr\n"
" --stats=N\t\t\t; print resolve stats to stderr every N domains\n" " --stats=N\t\t\t; print resolve stats to stderr every N domains\n"
" --log-resolved=<file>\t\t; log successfully resolved domains to a file\n" " --log-resolved=<file>\t\t; log successfully resolved domains to a file\n"
" --log-failed=<file>\t\t; log failed domains to a file\n" " --log-failed=<file>\t\t; log failed domains to a file\n"
" --dns-make-query=<domain>\t; output to stdout binary blob with DNS query. use --family to specify ip version.\n" " --dns-make-query=<domain>\t; output to stdout binary blob with DNS query. use --family to specify ip version.\n"
" --dns-parse-query\t\t; read from stdin binary DNS answer blob and parse it to ipv4/ipv6 addresses\n" " --dns-parse-query\t\t; read from stdin binary DNS answer blob and parse it to ipv4/ipv6 addresses\n",
RESOLVER_EAGAIN_ATTEMPTS,
RESOLVER_EAGAIN_DELAY
); );
exit(1); exit(1);
} }
@@ -469,6 +479,8 @@ static void exithelp(void)
enum opt_indices { enum opt_indices {
IDX_HELP, IDX_HELP,
IDX_EAGAIN,
IDX_EAGAIN_DELAY,
IDX_THREADS, IDX_THREADS,
IDX_FAMILY, IDX_FAMILY,
IDX_VERBOSE, IDX_VERBOSE,
@@ -483,6 +495,8 @@ enum opt_indices {
static const struct option long_options[] = { static const struct option long_options[] = {
[IDX_HELP] = {"help", no_argument, 0, 0}, [IDX_HELP] = {"help", no_argument, 0, 0},
[IDX_THREADS] = {"threads", required_argument, 0, 0}, [IDX_THREADS] = {"threads", required_argument, 0, 0},
[IDX_EAGAIN] = {"eagain", required_argument, 0, 0},
[IDX_EAGAIN_DELAY] = {"eagain-delay", required_argument, 0, 0},
[IDX_FAMILY] = {"family", required_argument, 0, 0}, [IDX_FAMILY] = {"family", required_argument, 0, 0},
[IDX_VERBOSE] = {"verbose", no_argument, 0, 0}, [IDX_VERBOSE] = {"verbose", no_argument, 0, 0},
[IDX_STATS] = {"stats", required_argument, 0, 0}, [IDX_STATS] = {"stats", required_argument, 0, 0},
@@ -503,6 +517,8 @@ int main(int argc, char **argv)
*fn1 = *fn2 = *dom = 0; *fn1 = *fn2 = *dom = 0;
glob.family = FAMILY4; glob.family = FAMILY4;
glob.threads = 1; glob.threads = 1;
glob.eagain = RESOLVER_EAGAIN_ATTEMPTS;
glob.eagain_delay = RESOLVER_EAGAIN_DELAY;
while ((v = getopt_long_only(argc, argv, "", long_options, &option_index)) != -1) while ((v = getopt_long_only(argc, argv, "", long_options, &option_index)) != -1)
{ {
if (v) exithelp(); if (v) exithelp();
@@ -513,13 +529,29 @@ int main(int argc, char **argv)
exithelp(); exithelp();
break; break;
case IDX_THREADS: case IDX_THREADS:
glob.threads = optarg ? atoi(optarg) : 0; glob.threads = atoi(optarg);
if (glob.threads <= 0 || glob.threads > 100) if (glob.threads <= 0 || glob.threads > 100)
{ {
fprintf(stderr, "thread number must be within 1..100\n"); fprintf(stderr, "thread number must be within 1..100\n");
return 1; return 1;
} }
break; break;
case IDX_EAGAIN:
glob.eagain = atoi(optarg);
if (glob.eagain <= 0 || glob.eagain > 1000)
{
fprintf(stderr, "eagain must be within 1..1000\n");
return 1;
}
break;
case IDX_EAGAIN_DELAY:
glob.eagain_delay = atoi(optarg);
if (glob.eagain_delay < 0 || glob.eagain_delay > 100000)
{
fprintf(stderr, "eagain-delay must be within 0..100000\n");
return 1;
}
break;
case IDX_FAMILY: case IDX_FAMILY:
if (!strcmp(optarg, "4")) if (!strcmp(optarg, "4"))
glob.family = FAMILY4; glob.family = FAMILY4;