]> git.sesse.net Git - jam/blob - jam.c
Add an option for setting the number of senders.
[jam] / jam.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <getopt.h>
5 #include <netdb.h>
6 #include <unistd.h>
7 #include <pthread.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10
11 unsigned short port = 2007;
12
13 struct in_addr *destinations = NULL;
14 unsigned num_destinations = 0;
15 unsigned room_destinations = 0;
16
17 struct in_addr *sources = NULL;
18 unsigned num_sources = 0;
19 unsigned room_sources = 0;
20
21 unsigned num_senders = 128;
22
23 const static struct option longopts[] = {
24         { "source-list", required_argument, NULL, 's' },
25         { "destination-list", required_argument, NULL, 'd' },
26         { "num-senders", required_argument, NULL, 'n' },
27         { "port", required_argument, NULL, 'p' },
28         { NULL, 0, NULL, 0 }
29 };
30
31 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
32 {
33         char buf[256];
34         FILE *in = fopen(filename, "r");
35         if (in == NULL) {
36                 perror(filename);
37                 exit(1);
38         }
39
40         for ( ;; ) {
41                 char *ptr;
42                 struct in_addr addr;
43                 struct hostent *he;
44
45                 if (fgets(buf, 256, in) == NULL)
46                         break;
47
48                 ptr = strchr(buf, '\n');
49                 if (ptr != NULL)
50                         *ptr = 0;
51
52                 ptr = strchr(buf, '\r');
53                 if (ptr != NULL)
54                         *ptr = 0;
55                 
56                 ptr = buf + strspn(buf, " \t");
57
58                 if (ptr[0] == '#' || ptr[0] == 0)
59                         continue;
60
61                 he = gethostbyname(ptr);
62                 if (he == NULL) {
63                         perror(ptr);
64                         exit(1);
65                 }
66
67                 // just pick the first for now
68                 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
69
70                 if (*num >= *room) {
71                         if (*room == 0) {
72                                 *room = 16;
73                         } else {
74                                 *room <<= 1;
75                         }
76                         *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
77                 }
78
79                 (*addr_list)[*num] = addr;
80                 ++*num;
81         }
82
83         fclose(in);
84 }
85
86 void parse_options(int argc, char **argv)
87 {
88         int option_index = 0;
89
90         for ( ;; ) {
91                 int c = getopt_long(argc, argv, "s:d:n:p:", longopts, &option_index); 
92                 switch (c) {
93                 case 's':
94                         read_ip_list(optarg, &sources, &num_sources, &room_sources);
95                         break;
96                 case 'd':
97                         read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
98                         break;
99                 case 'n':
100                         num_senders = atoi(optarg);
101                         break;
102                 case 'p':
103                         port = atoi(optarg);
104                         break;
105                 case -1:
106                         return;       // end of argument list
107                 default:
108                         fprintf(stderr, "Invalid option\n");
109                         exit(1);
110                 }
111         }
112 }
113
114 void *sender_worker(void *arg)
115 {
116         printf("Dummy sender worker\n");
117         pthread_exit(0);
118 }
119
120 void *receiver_worker(void *arg)
121 {
122         int sock = (int)arg;
123         char buf[65536];
124
125         printf("Received worker for socket %u\n", sock);
126
127         for ( ;; ) {
128                 int ret = read(sock, buf, 65536);
129                 if (ret == 0)
130                         break;
131
132                 // FIXME: update stats here
133         }
134
135         printf("Socket %u done\n", sock);
136         
137         if (close(sock) == -1) {
138                 perror("close()");
139                 exit(1);
140         }
141
142         pthread_exit(0);
143 }
144
145 int get_server_socket(unsigned short port)
146 {
147         int server_sock;
148         struct sockaddr_in sin;
149         unsigned one = 1;
150
151         server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
152         if (server_sock == -1) {
153                 perror("socket()");
154                 exit(1);
155         }
156
157         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
158                 perror("setsocket(SO_REUSEADDR)");
159                 exit(1);
160         }
161
162         sin.sin_family = AF_INET;
163         sin.sin_port = htons(port);
164         sin.sin_addr.s_addr = INADDR_ANY;
165
166         if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
167                 perror("bind()");
168                 exit(1);
169         }
170
171         if (listen(server_sock, 255) == -1) {
172                 perror("listen()");
173                 exit(1);
174         }
175
176         return server_sock;
177 }
178
179 int main(int argc, char **argv)
180 {
181         int server_sock;
182         unsigned i;
183         pthread_attr_t attr;
184
185         parse_options(argc, argv);
186
187         if (destinations == NULL || sources == NULL) {
188                 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
189         }
190
191         server_sock = get_server_socket(port);
192         
193         printf("Sending data on port %u from %u sources to %u destinations.\n\n",
194                 port, num_sources, num_destinations);
195                 
196         // FIXME: these do not really set errno
197         if (pthread_attr_init(&attr) != 0) {
198                 perror("pthread_attr_init()");
199                 exit(1);
200         }
201
202         if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
203                 perror("pthread_attr_setstacksize");
204                 exit(1);
205         }
206
207         // Fire off sender workers.
208         for (i = 0; i < num_senders; ++i) {
209                 pthread_t thread;
210                 
211                 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
212                         perror("pthread_create()");
213                         exit(1);
214                 }
215         }
216
217         /*
218          * Listen for incoming connections, spawning off one receiver
219          * thread for each (which will just gobble up the data until
220          * we're done).
221          */
222         for ( ;; ) {
223                 struct sockaddr_in addr;
224                 socklen_t addr_len = sizeof(addr);
225                 pthread_t thread;
226
227                 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
228                 if (sock == -1) {
229                         perror("accept()");
230                         exit(1);
231                 }
232
233                 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
234                         perror("pthread_create()");
235                         exit(1);
236                 }
237         }
238
239         exit(0);
240 }