9 #include <sys/socket.h>
10 #include <netinet/in.h>
12 unsigned short port = 2007;
14 struct in_addr *destinations = NULL;
15 unsigned num_destinations = 0;
16 unsigned room_destinations = 0;
18 struct in_addr *sources = NULL;
19 unsigned num_sources = 0;
20 unsigned room_sources = 0;
22 unsigned num_senders = 128;
23 unsigned do_listen = 1;
25 const static struct option longopts[] = {
26 { "source-list", required_argument, NULL, 's' },
27 { "destination-list", required_argument, NULL, 'd' },
28 { "num-senders", required_argument, NULL, 'n' },
29 { "port", required_argument, NULL, 'p' },
30 { "sender-only", no_argument, NULL, 'o' },
34 // generates from [0,1>
35 double gen_uniform_random()
37 return rand() / (RAND_MAX+1.0);
40 double gen_pareto_random(double min, double k)
42 double u = gen_uniform_random();
43 return min * pow(u, -1.0 / k);
46 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
49 FILE *in = fopen(filename, "r");
60 if (fgets(buf, 256, in) == NULL)
63 ptr = strchr(buf, '\n');
67 ptr = strchr(buf, '\r');
71 ptr = buf + strspn(buf, " \t");
73 if (ptr[0] == '#' || ptr[0] == 0)
76 he = gethostbyname(ptr);
82 // just pick the first for now
83 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
91 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
94 (*addr_list)[*num] = addr;
101 void parse_options(int argc, char **argv)
103 int option_index = 0;
106 int c = getopt_long(argc, argv, "s:d:n:p:o", longopts, &option_index);
109 read_ip_list(optarg, &sources, &num_sources, &room_sources);
112 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
115 num_senders = atoi(optarg);
124 return; // end of argument list
126 fprintf(stderr, "Invalid option\n");
132 void *sender_worker(void *arg)
138 for (i = 0; i < 65536; ++i)
139 buf[i] = rand() & 0xff;
141 for (i = 0; i < 1000; ++i) {
142 unsigned src_num = (unsigned)(num_sources * gen_uniform_random());
143 unsigned dst_num = (unsigned)(num_destinations * gen_uniform_random());
144 unsigned num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
145 struct sockaddr_in sin;
147 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
153 // FIXME: bind to the right source
155 sin.sin_family = AF_INET;
156 sin.sin_port = htons(port);
157 sin.sin_addr = destinations[dst_num];
159 fprintf(stderr, "connecting %u\n", i);
161 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
166 fprintf(stderr, "connected\n");
168 while (num_bytes > 0) {
169 unsigned bytes_to_send = num_bytes;
172 if (bytes_to_send > 65536) {
173 bytes_to_send = 65536;
176 ret = send(sock, buf, bytes_to_send, MSG_NOSIGNAL);
184 fprintf(stderr, "sent\n");
192 void *receiver_worker(void *arg)
197 fprintf(stderr, "Receiver started\n");
200 int ret = read(sock, buf, 65536);
204 // FIXME: update stats here
207 if (close(sock) == -1) {
215 int get_server_socket(unsigned short port)
218 struct sockaddr_in sin;
221 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
222 if (server_sock == -1) {
227 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
228 perror("setsocket(SO_REUSEADDR)");
232 sin.sin_family = AF_INET;
233 sin.sin_port = htons(port);
234 sin.sin_addr.s_addr = INADDR_ANY;
236 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
241 if (listen(server_sock, 255) == -1) {
249 int main(int argc, char **argv)
255 parse_options(argc, argv);
257 if (destinations == NULL || sources == NULL) {
258 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
262 server_sock = get_server_socket(port);
265 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
266 port, num_sources, num_destinations);
268 // FIXME: these do not really set errno
269 if (pthread_attr_init(&attr) != 0) {
270 perror("pthread_attr_init()");
274 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
275 perror("pthread_attr_setstacksize");
279 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
280 perror("pthread_attr_setdetachstate");
284 // Fire off sender workers.
285 for (i = 0; i < num_senders; ++i) {
288 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
289 perror("pthread_create()");
295 * Listen for incoming connections, spawning off one receiver
296 * thread for each (which will just gobble up the data until
305 struct sockaddr_in addr;
306 socklen_t addr_len = sizeof(addr);
309 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
315 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
316 perror("pthread_create()");