allow to specify maximum number of redirections with -r

For example, -r/--redirect 3 specifies that the maximum number of redirections
per client is 3.

if -r/--redirect is specified with no argument, then the argument specified by
-n/--max-clients is used.

If -n/--max-clients is not specified and -r/--redirect is specifed with no
argument, then default value is 3.
master
tiptorrent development team 2022-02-08 11:58:53 +01:00
parent 28d7a25a89
commit f6f3984be7
3 changed files with 16 additions and 8 deletions

View File

@ -222,7 +222,7 @@ int tip_client_redirect_create(const struct tip_client *cli)
struct tip_client_redirect *redir; struct tip_client_redirect *redir;
bool found = false; bool found = false;
if (!redirect || !cli->allow_redirect) if (!max_redirect || !cli->allow_redirect)
return 0; return 0;
list_for_each_entry(redir, &client_redirect_list, list) { list_for_each_entry(redir, &client_redirect_list, list) {
@ -247,7 +247,7 @@ int tip_client_redirect_create(const struct tip_client *cli)
redir->addr = cli->addr; redir->addr = cli->addr;
redir->addr.sin_port = htons(9999); redir->addr.sin_port = htons(9999);
redir->uri = strdup(cli->uri); redir->uri = strdup(cli->uri);
redir->users = max_clients; redir->users = max_redirect;
list_add_tail(&redir->list, &client_redirect_list); list_add_tail(&redir->list, &client_redirect_list);
ev_timer_init(&redir->timer, tip_client_redirect_timer_cb, TIP_CLIENT_REDIRECT_TIMEOUT, 0.); ev_timer_init(&redir->timer, tip_client_redirect_timer_cb, TIP_CLIENT_REDIRECT_TIMEOUT, 0.);
@ -380,7 +380,7 @@ bool tip_client_redirect(struct tip_client *cli)
struct tip_client_redirect *redir, *next; struct tip_client_redirect *redir, *next;
char addr[INET_ADDRSTRLEN + 1]; char addr[INET_ADDRSTRLEN + 1];
if (!redirect) if (!max_redirect)
return false; return false;
inet_ntop(AF_INET, &cli->addr.sin_addr, addr, INET_ADDRSTRLEN); inet_ntop(AF_INET, &cli->addr.sin_addr, addr, INET_ADDRSTRLEN);

View File

@ -13,8 +13,8 @@
extern const char *root; extern const char *root;
#define DEFAULT_MAX_CLIENTS 3 #define DEFAULT_MAX_CLIENTS 3
extern int max_clients; extern int max_clients;
extern int max_redirect;
extern int num_clients; extern int num_clients;
extern bool redirect;
enum tip_client_state { enum tip_client_state {
TIP_CLIENT_PENDING = 0, TIP_CLIENT_PENDING = 0,

View File

@ -29,11 +29,11 @@
int max_clients = DEFAULT_MAX_CLIENTS; int max_clients = DEFAULT_MAX_CLIENTS;
const char *root = "."; const char *root = ".";
bool redirect; int max_redirect = 0;
static struct option tip_repo_opts[] = { static struct option tip_repo_opts[] = {
{ "max-clients", 1, 0, 'n' }, { "max-clients", 1, 0, 'n' },
{ "redirect", 0, 0, 'r' }, { "redirect", 2, 0, 'r' },
{ "root", 1, 0, 't' }, { "root", 1, 0, 't' },
{ "daemon", 0, 0, 'd' }, { "daemon", 0, 0, 'd' },
{ NULL }, { NULL },
@ -54,7 +54,7 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
while (1) { while (1) {
val = getopt_long(argc, argv, "n:rd", tip_repo_opts, NULL); val = getopt_long(argc, argv, "n:r::d", tip_repo_opts, NULL);
if (val < 0) if (val < 0)
break; break;
@ -67,7 +67,15 @@ int main(int argc, char *argv[])
} }
break; break;
case 'r': case 'r':
redirect = true; if (optarg) {
max_redirect = atoi(optarg);
if (max_redirect <= 0) {
syslog(LOG_ERR, "Invalid number for redirections");
return EXIT_FAILURE;
}
} else {
max_redirect = max_clients;
}
break; break;
case 't': case 't':
root = strdup(optarg); root = strdup(optarg);