summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2022-11-30 11:49:56 +0100
committertiptorrent development team <tiptorrent@soleta.eu>2022-11-30 12:13:36 +0100
commitd4753d6c3e41aa227809ca6adbc6bc663c1411ec (patch)
tree89c15bd8c906bd2fe346fa5d195e63612bbbdfa9
parent9d13b48925547fb1f0deaa0b257d490a16f47f92 (diff)
add tip_client_run()
-rw-r--r--src/main.c72
1 files changed, 42 insertions, 30 deletions
diff --git a/src/main.c b/src/main.c
index b156e1b..be30150 100644
--- a/src/main.c
+++ b/src/main.c
@@ -126,7 +126,7 @@ static void tip_client_progress(struct tip_client *cli, bool now)
}
}
-static int tip_client_connect(const char *addr);
+static int tip_client_connect(struct tip_client *cli, const char *addr);
static int tip_client_get_hdr(struct tip_client *cli)
{
@@ -165,7 +165,7 @@ static int tip_client_get_hdr(struct tip_client *cli)
cli->redirected = true;
tip_client_close(cli);
- tip_client_connect(redirect_addr);
+ tip_client_connect(cli, redirect_addr);
cli->state = TIP_CLIENT_GET_HEADER;
return 0;
@@ -230,7 +230,7 @@ static int tip_client_get_payload(struct tip_client *cli)
if (cli->data_len >= cli->content_len) {
if (cli->redirected) {
tip_client_close(cli);
- tip_client_connect(addr);
+ tip_client_connect(cli, addr);
cli->state = TIP_CLIENT_POST_REDIRECT;
return 1;
@@ -444,11 +444,10 @@ static void tip_client_connect_cb(struct ev_loop *loop, struct ev_io *io, int ev
#define TIP_TCP_KEEPALIVE_INTL 30
#define TIP_TCP_KEEPALIVE_CNT 4
-static int tip_client_connect(const char *addr)
+static int tip_client_connect(struct tip_client *cli, const char *addr)
{
int intl = TIP_TCP_KEEPALIVE_INTL, cnt = TIP_TCP_KEEPALIVE_CNT;
int on = 1, idle = TIP_TCP_KEEPALIVE_IDLE;
- struct tip_client *cli = &_cli;
int remote_fd;
int flags;
int len;
@@ -497,7 +496,7 @@ static int tip_client_connect(const char *addr)
static int tip_client_request_file(struct tip_client *cli,
const char *server, const char *filename)
{
- tip_client_connect(server);
+ tip_client_connect(cli, server);
while (cli->state != TIP_CLIENT_DONE && !cli->error)
ev_loop(tip_main_loop, 0);
@@ -555,6 +554,42 @@ static void tip_client_reset_state(struct tip_client *cli, int fd,
cli->num_retries = num_retries;
}
+static int tip_client_run(struct tip_client *cli, int fd, const char *addr,
+ const char *filename, uint32_t chunk,
+ uint64_t chunk_size)
+{
+ int ret;
+
+ do {
+ tip_client_reset_state(cli, fd, chunk_size * chunk);
+ syslog(LOG_INFO, "Requesting file %s to server\n", filename);
+ cli->state = TIP_CLIENT_GET_HEADER;
+ ret = tip_client_request_file(cli, addr, filename);
+ } while (ret > 0);
+
+ if (ret < 0) {
+ do {
+ tip_client_reset_state(cli, fd, chunk_size * chunk);
+ cli->server_only = true;
+ syslog(LOG_INFO, "Requesting file %s to server only\n", filename);
+ cli->state = TIP_CLIENT_GET_HEADER;
+ ret = tip_client_request_file(cli, addr, filename);
+ } while (ret > 0);
+ }
+
+ if (ret < 0)
+ return ret;
+
+ if (cli->redirected)
+ tip_client_stats.redirects++;
+ else
+ tip_client_stats.direct_from_server++;
+
+ tip_client_progress(cli, true);
+
+ return 0;
+}
+
static char _filename[PATH_MAX + 1];
int main(int argc, char *argv[])
@@ -598,32 +633,9 @@ int main(int argc, char *argv[])
filename = _filename;
chunk_size = file_size / MAX_CHUNKS;
- do {
- tip_client_reset_state(&_cli, fd, chunk_size * k);
- syslog(LOG_INFO, "Requesting file %s to server\n", filename);
- _cli.state = TIP_CLIENT_GET_HEADER;
- ret = tip_client_request_file(&_cli, addr, filename);
- } while (ret > 0);
-
- if (ret < 0) {
- do {
- tip_client_reset_state(&_cli, fd, chunk_size * k);
- _cli.server_only = true;
- syslog(LOG_INFO, "Requesting file %s to server only\n", filename);
- _cli.state = TIP_CLIENT_GET_HEADER;
- ret = tip_client_request_file(&_cli, addr, filename);
- } while (ret > 0);
- }
-
- if (ret < 0)
+ if (tip_client_run(&_cli, fd, addr, filename, k, chunk_size) < 0)
goto err_bailout;
- if (_cli.redirected)
- tip_client_stats.redirects++;
- else
- tip_client_stats.direct_from_server++;
-
- tip_client_progress(&_cli, true);
file_chunk[k] = true;
data_len += _cli.data_len;
}