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