]> git.sesse.net Git - jam/blob - jam.c
1da8fc84f282189882fe5a4eddc6ff732bbd50df
[jam] / jam.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <getopt.h>
6 #include <netdb.h>
7 #include <unistd.h>
8 #include <pthread.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11
12 unsigned short port = 2007;
13
14 struct in_addr *destinations = NULL;
15 unsigned num_destinations = 0;
16 unsigned room_destinations = 0;
17
18 struct in_addr *sources = NULL;
19 unsigned num_sources = 0;
20 unsigned room_sources = 0;
21
22 unsigned num_senders = 128;
23 unsigned do_listen = 1;
24
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' },
31         { NULL, 0, NULL, 0 }
32 };
33
34 // generates from [0,1>
35 double gen_uniform_random()
36 {
37         return rand() / (RAND_MAX+1.0);
38 }
39
40 double gen_pareto_random(double min, double k)
41 {
42         double u = gen_uniform_random();
43         return min * pow(u, -1.0 / k);
44 }
45
46 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
47 {
48         char buf[256];
49         FILE *in = fopen(filename, "r");
50         if (in == NULL) {
51                 perror(filename);
52                 exit(1);
53         }
54
55         for ( ;; ) {
56                 char *ptr;
57                 struct in_addr addr;
58                 struct hostent *he;
59
60                 if (fgets(buf, 256, in) == NULL)
61                         break;
62
63                 ptr = strchr(buf, '\n');
64                 if (ptr != NULL)
65                         *ptr = 0;
66
67                 ptr = strchr(buf, '\r');
68                 if (ptr != NULL)
69                         *ptr = 0;
70                 
71                 ptr = buf + strspn(buf, " \t");
72
73                 if (ptr[0] == '#' || ptr[0] == 0)
74                         continue;
75
76                 he = gethostbyname(ptr);
77                 if (he == NULL) {
78                         perror(ptr);
79                         exit(1);
80                 }
81
82                 // just pick the first for now
83                 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
84
85                 if (*num >= *room) {
86                         if (*room == 0) {
87                                 *room = 16;
88                         } else {
89                                 *room <<= 1;
90                         }
91                         *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
92                 }
93
94                 (*addr_list)[*num] = addr;
95                 ++*num;
96         }
97
98         fclose(in);
99 }
100
101 void parse_options(int argc, char **argv)
102 {
103         int option_index = 0;
104
105         for ( ;; ) {
106                 int c = getopt_long(argc, argv, "s:d:n:p:o", longopts, &option_index); 
107                 switch (c) {
108                 case 's':
109                         read_ip_list(optarg, &sources, &num_sources, &room_sources);
110                         break;
111                 case 'd':
112                         read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
113                         break;
114                 case 'n':
115                         num_senders = atoi(optarg);
116                         break;
117                 case 'p':
118                         port = atoi(optarg);
119                         break;
120                 case 'o':
121                         do_listen = 0;
122                         break;
123                 case -1:
124                         return;       // end of argument list
125                 default:
126                         fprintf(stderr, "Invalid option\n");
127                         exit(1);
128                 }
129         }
130 }
131
132 void *sender_worker(void *arg)
133 {
134         unsigned i;
135         int sock;
136         char buf[65536];
137
138         for (i = 0; i < 65536; ++i)
139                 buf[i] = rand() & 0xff;
140
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;
146
147                 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
148                 if (sock == -1) {
149                         perror("socket()");
150                         exit(1);
151                 }
152                 
153                 // FIXME: bind to the right source
154                 
155                 sin.sin_family = AF_INET;
156                 sin.sin_port = htons(port);
157                 sin.sin_addr = destinations[dst_num];
158         
159                 fprintf(stderr, "connecting %u\n", i);
160
161                 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
162                         perror("connect()");
163                         exit(1);
164                 }
165                 
166                 fprintf(stderr, "connected\n");
167
168                 while (num_bytes > 0) {
169                         unsigned bytes_to_send = num_bytes;
170                         unsigned ret;
171
172                         if (bytes_to_send > 65536) {
173                                 bytes_to_send = 65536;  
174                         }
175
176                         ret = send(sock, buf, bytes_to_send, MSG_NOSIGNAL);
177                         if (ret == -1) {
178                                 perror("send()");
179                                 exit(1);
180                         }
181
182                         num_bytes -= ret;
183                 }
184                 fprintf(stderr, "sent\n");
185
186                 close(sock);
187         }
188
189         pthread_exit(0);
190 }
191
192 void *receiver_worker(void *arg)
193 {
194         int sock = (int)arg;
195         char buf[65536];
196
197         for ( ;; ) {
198                 int ret = read(sock, buf, 65536);
199                 if (ret == 0)
200                         break;
201
202                 // FIXME: update stats here
203         }
204         
205         if (close(sock) == -1) {
206                 perror("close()");
207                 exit(1);
208         }
209
210         pthread_exit(0);
211 }
212
213 int get_server_socket(unsigned short port)
214 {
215         int server_sock;
216         struct sockaddr_in sin;
217         unsigned one = 1;
218
219         server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
220         if (server_sock == -1) {
221                 perror("socket()");
222                 exit(1);
223         }
224
225         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
226                 perror("setsocket(SO_REUSEADDR)");
227                 exit(1);
228         }
229
230         sin.sin_family = AF_INET;
231         sin.sin_port = htons(port);
232         sin.sin_addr.s_addr = INADDR_ANY;
233
234         if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
235                 perror("bind()");
236                 exit(1);
237         }
238
239         if (listen(server_sock, 255) == -1) {
240                 perror("listen()");
241                 exit(1);
242         }
243
244         return server_sock;
245 }
246
247 int main(int argc, char **argv)
248 {
249         int server_sock;
250         unsigned i;
251         pthread_attr_t attr;
252
253         parse_options(argc, argv);
254
255         if (destinations == NULL || sources == NULL) {
256                 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
257         }
258
259         if (do_listen) {
260                 server_sock = get_server_socket(port);
261         }
262         
263         printf("Sending data on port %u from %u sources to %u destinations.\n\n",
264                 port, num_sources, num_destinations);
265                 
266         // FIXME: these do not really set errno
267         if (pthread_attr_init(&attr) != 0) {
268                 perror("pthread_attr_init()");
269                 exit(1);
270         }
271
272         if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
273                 perror("pthread_attr_setstacksize");
274                 exit(1);
275         }
276
277         if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
278                 perror("pthread_attr_setdetachstate");
279                 exit(1);
280         }
281
282         // Fire off sender workers.
283         for (i = 0; i < num_senders; ++i) {
284                 pthread_t thread;
285                 
286                 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
287                         perror("pthread_create()");
288                         exit(1);
289                 }
290         }
291
292         /*
293          * Listen for incoming connections, spawning off one receiver
294          * thread for each (which will just gobble up the data until
295          * we're done).
296          */
297         if (!do_listen) {
298                 sleep(3600);
299                 exit(0);
300         }
301
302         for ( ;; ) {
303                 struct sockaddr_in addr;
304                 socklen_t addr_len = sizeof(addr);
305                 pthread_t thread;
306
307                 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
308                 if (sock == -1) {
309                         perror("accept()");
310                         exit(1);
311                 }
312
313                 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
314                         perror("pthread_create()");
315                         exit(1);
316                 }
317         }
318
319         exit(0);
320 }