summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortiptorrent development team <tiptorrent@soleta.eu>2022-11-30 12:15:35 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2022-11-30 12:29:07 +0100
commit161a49b683e7aab1d8384b5c74d1c9b1d1ff300d (patch)
treedc8265584e4c4deee614344fc720169d5187d49f /src
parent9e80376cb843be991faf19f8302c215b3adf4072 (diff)
open two simultaneous connections using two processesmultiprocess
Diffstat (limited to 'src')
-rw-r--r--src/main.c57
1 files changed, 40 insertions, 17 deletions
diff --git a/src/main.c b/src/main.c
index 984d9b4..e494c58 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,6 +25,7 @@
#include <sys/time.h>
#include <limits.h>
#include <syslog.h>
+#include <sys/wait.h>
#include <netinet/tcp.h>
#include <fcntl.h>
@@ -593,23 +594,23 @@ static int tip_client_run(struct tip_client *cli, int fd, const char *addr,
}
static int tip_client_report(struct tip_client *cli, const struct timeval *tv,
- uint64_t data_len)
+ uint64_t data_len, pid_t pid)
{
if (cli->state == TIP_CLIENT_DONE) {
- printf("OK.\n");
- syslog(LOG_INFO, "Done in %lu.%06lu seconds (%lu Mbytes/second). "
+ printf("(%u) OK.\n", pid);
+ syslog(LOG_INFO, "(%u) Done in %lu.%06lu seconds (%lu Mbytes/second). "
"Direct from server: %u Redirected: %u\n",
- tv->tv_sec, tv->tv_usec,
+ pid, tv->tv_sec, tv->tv_usec,
tv->tv_sec > 0 ? data_len / 1024000 / tv->tv_sec : data_len / 1024000,
tip_client_stats.direct_from_server,
tip_client_stats.redirects);
return EXIT_SUCCESS;
}
- printf("Failure, see syslog for details.\n");
- syslog(LOG_INFO, "Failure after %lu.%06lu seconds (%lu Mbytes/second). "
+ printf("(%u) Failure, see syslog for details.\n", pid);
+ syslog(LOG_INFO, "(%u) Failure after %lu.%06lu seconds (%lu Mbytes/second). "
"Direct from server: %u Redirected: %u\n",
- tv->tv_sec, tv->tv_usec,
+ pid, tv->tv_sec, tv->tv_usec,
tv->tv_sec > 0 ? data_len / 1024000 / tv->tv_sec : data_len / 1024000,
tip_client_stats.direct_from_server,
tip_client_stats.redirects);
@@ -617,14 +618,17 @@ static int tip_client_report(struct tip_client *cli, const struct timeval *tv,
return EXIT_FAILURE;
}
+#define NUM_CONN 2
+
int main(int argc, char *argv[])
{
+ uint32_t chunk_array[NUM_CONN][MAX_CHUNKS / NUM_CONN] = {};
struct timeval tv_start, tv_stop, tv;
- uint64_t data_len = 0, file_size = 0;
bool file_chunk[MAX_CHUNKS] = {};
char filename[PATH_MAX + 1];
+ uint64_t file_size = 0;
+ int i, j, k, fd, ret;
uint64_t chunk_size;
- int i, k, fd, ret;
if (argc != 3) {
printf("%s [ip] [file]\n", argv[0]);
@@ -653,21 +657,40 @@ int main(int argc, char *argv[])
file_size = _cli.content_len;
chunk_size = file_size / MAX_CHUNKS;
- for (i = 0; i < MAX_CHUNKS; i++) {
- k = select_file_chunk(file_chunk);
- snprintf(filename, sizeof(filename), "%s.%u", argv[2], k);
+ for (i = 0; i < NUM_CONN; i++) {
+ for (j = 0; j < MAX_CHUNKS / NUM_CONN; j++) {
+ k = select_file_chunk(file_chunk);
+ chunk_array[i][j] = k;
+ file_chunk[k] = true;
+ }
+ }
- if (tip_client_run(&_cli, fd, addr, filename, k, chunk_size) < 0)
- goto err_bailout;
+ for (i = 0; i < NUM_CONN; i++) {
+ ret = fork();
+ if (ret > 0) {
+ for (j = 0; j < MAX_CHUNKS / NUM_CONN; j++) {
+ k = chunk_array[i][j];
+ snprintf(filename, sizeof(filename), "%s.%u", argv[2], k);
- file_chunk[k] = true;
- data_len += _cli.data_len;
+ if (tip_client_run(&_cli, fd, addr, filename, k, chunk_size) < 0)
+ goto err_bailout;
+ }
+ break;
+ } else if (ret < 0) {
+ syslog(LOG_ERR, "failed to fork process");
+ break;
+ }
}
+ if (ret == 0) {
+ while (wait(NULL) > 0);
+ return EXIT_SUCCESS;
+ }
+
close(fd);
gettimeofday(&tv_stop, NULL);
timersub(&tv_stop, &tv_start, &tv);
- return tip_client_report(&_cli, &tv, data_len);
+ return tip_client_report(&_cli, &tv, file_size / 2, ret);
err_bailout:
if (_cli.fatal)