]> git.sesse.net Git - jam/blob - jam.c
Add a new printf, for debugging.
[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         fprintf(stderr, "Receiver started\n");
198
199         for ( ;; ) {
200                 int ret = read(sock, buf, 65536);
201                 if (ret == 0)
202                         break;
203
204                 // FIXME: update stats here
205         }
206         
207         if (close(sock) == -1) {
208                 perror("close()");
209                 exit(1);
210         }
211
212         pthread_exit(0);
213 }
214
215 int get_server_socket(unsigned short port)
216 {
217         int server_sock;
218         struct sockaddr_in sin;
219         unsigned one = 1;
220
221         server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
222         if (server_sock == -1) {
223                 perror("socket()");
224                 exit(1);
225         }
226
227         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
228                 perror("setsocket(SO_REUSEADDR)");
229                 exit(1);
230         }
231
232         sin.sin_family = AF_INET;
233         sin.sin_port = htons(port);
234         sin.sin_addr.s_addr = INADDR_ANY;
235
236         if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
237                 perror("bind()");
238                 exit(1);
239         }
240
241         if (listen(server_sock, 255) == -1) {
242                 perror("listen()");
243                 exit(1);
244         }
245
246         return server_sock;
247 }
248
249 int main(int argc, char **argv)
250 {
251         int server_sock;
252         unsigned i;
253         pthread_attr_t attr;
254
255         parse_options(argc, argv);
256
257         if (destinations == NULL || sources == NULL) {
258                 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
259         }
260
261         if (do_listen) {
262                 server_sock = get_server_socket(port);
263         }
264         
265         printf("Sending data on port %u from %u sources to %u destinations.\n\n",
266                 port, num_sources, num_destinations);
267                 
268         // FIXME: these do not really set errno
269         if (pthread_attr_init(&attr) != 0) {
270                 perror("pthread_attr_init()");
271                 exit(1);
272         }
273
274         if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
275                 perror("pthread_attr_setstacksize");
276                 exit(1);
277         }
278
279         if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
280                 perror("pthread_attr_setdetachstate");
281                 exit(1);
282         }
283
284         // Fire off sender workers.
285         for (i = 0; i < num_senders; ++i) {
286                 pthread_t thread;
287                 
288                 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
289                         perror("pthread_create()");
290                         exit(1);
291                 }
292         }
293
294         /*
295          * Listen for incoming connections, spawning off one receiver
296          * thread for each (which will just gobble up the data until
297          * we're done).
298          */
299         if (!do_listen) {
300                 sleep(3600);
301                 exit(0);
302         }
303
304         for ( ;; ) {
305                 struct sockaddr_in addr;
306                 socklen_t addr_len = sizeof(addr);
307                 pthread_t thread;
308
309                 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
310                 if (sock == -1) {
311                         perror("accept()");
312                         exit(1);
313                 }
314
315                 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
316                         perror("pthread_create()");
317                         exit(1);
318                 }
319         }
320
321         exit(0);
322 }