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