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