]> git.sesse.net Git - jam/blob - jam.c
Make a special "listen-only" mode.
[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
137         for (i = 0; i < 1000; ++i) {
138                 unsigned src_num = (unsigned)(num_sources * gen_uniform_random());
139                 unsigned dst_num = (unsigned)(num_destinations * gen_uniform_random());
140                 unsigned num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
141                 struct sockaddr_in sin;
142
143                 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
144                 if (sock == -1) {
145                         perror("socket()");
146                         exit(1);
147                 }
148                 
149                 // FIXME: bind to the right source
150                 
151                 sin.sin_family = AF_INET;
152                 sin.sin_port = htons(port);
153                 sin.sin_addr = destinations[dst_num];
154         
155                 fprintf(stderr, "connecting %u\n", i);
156
157                 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
158                         perror("connect()");
159                         exit(1);
160                 }
161                 
162                 fprintf(stderr, "connected\n");
163
164                 // FIXME: send data here
165
166                 close(sock);
167         }
168
169         pthread_exit(0);
170 }
171
172 void *receiver_worker(void *arg)
173 {
174         int sock = (int)arg;
175         char buf[65536];
176
177         for ( ;; ) {
178                 int ret = read(sock, buf, 65536);
179                 if (ret == 0)
180                         break;
181
182                 // FIXME: update stats here
183         }
184         
185         if (close(sock) == -1) {
186                 perror("close()");
187                 exit(1);
188         }
189
190         pthread_exit(0);
191 }
192
193 int get_server_socket(unsigned short port)
194 {
195         int server_sock;
196         struct sockaddr_in sin;
197         unsigned one = 1;
198
199         server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
200         if (server_sock == -1) {
201                 perror("socket()");
202                 exit(1);
203         }
204
205         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
206                 perror("setsocket(SO_REUSEADDR)");
207                 exit(1);
208         }
209
210         sin.sin_family = AF_INET;
211         sin.sin_port = htons(port);
212         sin.sin_addr.s_addr = INADDR_ANY;
213
214         if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
215                 perror("bind()");
216                 exit(1);
217         }
218
219         if (listen(server_sock, 255) == -1) {
220                 perror("listen()");
221                 exit(1);
222         }
223
224         return server_sock;
225 }
226
227 int main(int argc, char **argv)
228 {
229         int server_sock;
230         unsigned i;
231         pthread_attr_t attr;
232
233         parse_options(argc, argv);
234
235         if (destinations == NULL || sources == NULL) {
236                 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
237         }
238
239         if (do_listen) {
240                 server_sock = get_server_socket(port);
241         }
242         
243         printf("Sending data on port %u from %u sources to %u destinations.\n\n",
244                 port, num_sources, num_destinations);
245                 
246         // FIXME: these do not really set errno
247         if (pthread_attr_init(&attr) != 0) {
248                 perror("pthread_attr_init()");
249                 exit(1);
250         }
251
252         if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
253                 perror("pthread_attr_setstacksize");
254                 exit(1);
255         }
256
257         if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
258                 perror("pthread_attr_setdetachstate");
259                 exit(1);
260         }
261
262         // Fire off sender workers.
263         for (i = 0; i < num_senders; ++i) {
264                 pthread_t thread;
265                 
266                 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
267                         perror("pthread_create()");
268                         exit(1);
269                 }
270         }
271
272         /*
273          * Listen for incoming connections, spawning off one receiver
274          * thread for each (which will just gobble up the data until
275          * we're done).
276          */
277         if (!do_listen) {
278                 sleep(3600);
279                 exit(0);
280         }
281
282         for ( ;; ) {
283                 struct sockaddr_in addr;
284                 socklen_t addr_len = sizeof(addr);
285                 pthread_t thread;
286
287                 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
288                 if (sock == -1) {
289                         perror("accept()");
290                         exit(1);
291                 }
292
293                 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
294                         perror("pthread_create()");
295                         exit(1);
296                 }
297         }
298
299         exit(0);
300 }