summaryrefslogtreecommitdiff
path: root/latency.c
blob: c1782cf8527d14d5ce405e463f37b2ec24ed9149 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "proxy.h"

static int parse_host(const char *host, const char *port, struct addrinfo **addr)
{
	struct addrinfo hints = {
		.ai_family   = AF_UNSPEC,
		.ai_socktype = SOCK_STREAM,
		.ai_flags    = AI_PASSIVE,
	};

	int err;
	if ((err = getaddrinfo(host, port, &hints, addr))) {
		fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(err));
		return -1;
	}

	return 0;
}

static int listen_source(struct addrinfo *source)
{
	int fd = -1;
	for (struct addrinfo *a = source; a; a = a->ai_next) {
		if ((fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) == -1)
			continue;
		if (!bind(fd, a->ai_addr, a->ai_addrlen) && !listen(fd, 1))
			break;
		close(fd);
		fd = -1;
	}
	return fd;
}

static int connect_target(struct addrinfo *target)
{
	int fd = -1;
	for (struct addrinfo *a = target; a; a = a->ai_next) {
		if ((fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) == -1)
			continue;
		if (connect(fd, a->ai_addr, a->ai_addrlen) != -1)
			break;
		close(fd);
		fd = -1;
	}
	return fd;
}

struct args {
	struct addrinfo *source;
	struct addrinfo *target;
	unsigned delay;
};

static void usage(const char *progname)
{
	int width = 20;
	const char *args[] = {
		"-d DELAY",     "millisecond delay to add (default: 500ms)",
		"-p LOCALPORT", "port to listen on (default: 1234)",
		NULL
	};
	fprintf(stderr, "usage: %s [-d DELAY] [-p LOCALPORT] HOST PORT\n\n", progname);
	for (const char **a = args; *a; a += 2)
		fprintf(stderr, "\t%-*s %s\n", width, a[0], a[1]);
}

static int parse_unsigned(const char *arg, unsigned *out)
{
	char *end;
	errno = 0;
	unsigned long val = strtoul(arg, &end, 10);
	if (*end) return -1;
	if (errno && !val) return -1;
	if (val == ULONG_MAX && errno == ERANGE) return -1;
	if (val > UINT_MAX) return -1;
	*out = val;
	return 0;
}

static int parse_args(struct args *args, int argc, char **argv)
{
	args->delay = 500;

	const char *localport = "1234";
	const char *host = NULL, *port = NULL;

	extern char *optarg;
	extern int optind;
	int opt, error = 0;

	while ((opt = getopt(argc, argv, "+d:p:")) != -1) {
		switch (opt) {
		case 'd':
			if (parse_unsigned(optarg, &args->delay)) {
				fprintf(stderr, "invalid delay\n");
				error = 1;
			}
			break;

		case 'p':
			localport = optarg;
			break;

		default:
			error = 1;
		}
	}

	if (optind <= argc - 2) {
		host = argv[optind++];
		port = argv[optind++];
	} else error = 1;

	if (error) {
		usage(argv[0]);
		return -1;
	}
	if (parse_host(NULL, localport, &args->source))
		return -1;
	if (parse_host(host, port, &args->target))
		return -1;

	return 0;
}

int main(int argc, char **argv)
{
	struct args args;
	if (parse_args(&args, argc, argv))
		return -1;

	int listenfd = listen_source(args.source);
	if (listenfd < 0) {
		perror("listen_source");
		return -1;
	}

	for (;;) {
		int sourcefd = accept(listenfd, NULL, NULL);
		if (sourcefd == -1) break;

		int targetfd = connect_target(args.target);
		if (targetfd == -1) {
			perror("connect_target");
			close(sourcefd);
			break;
		}

		proxy(sourcefd, targetfd, args.delay);

		close(targetfd);
		close(sourcefd);
	}

	close(listenfd);
	return 0;
}