8 #include <sys/socket.h>
9 #include <netinet/in.h>
11 unsigned short port = 2007;
13 struct in_addr *destinations = NULL;
14 unsigned num_destinations = 0;
15 unsigned room_destinations = 0;
17 struct in_addr *sources = NULL;
18 unsigned num_sources = 0;
19 unsigned room_sources = 0;
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' },
28 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
31 FILE *in = fopen(filename, "r");
42 if (fgets(buf, 256, in) == NULL)
45 ptr = strchr(buf, '\n');
49 ptr = strchr(buf, '\r');
53 ptr = buf + strspn(buf, " \t");
55 if (ptr[0] == '#' || ptr[0] == 0)
58 he = gethostbyname(ptr);
64 // just pick the first for now
65 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
73 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
76 (*addr_list)[*num] = addr;
83 void parse_options(int argc, char **argv)
88 int c = getopt_long(argc, argv, "s:d:p:", longopts, &option_index);
91 read_ip_list(optarg, &sources, &num_sources, &room_sources);
94 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
100 return; // end of argument list
102 fprintf(stderr, "Invalid option\n");
108 void *receiver_worker(void *arg)
113 printf("Received worker for socket %u\n", sock);
116 int ret = read(sock, buf, 65536);
120 // FIXME: update stats here
123 printf("Socket %u done\n", sock);
125 if (close(sock) == -1) {
133 int get_server_socket(unsigned short port)
136 struct sockaddr_in sin;
139 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
140 if (server_sock == -1) {
145 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
146 perror("setsocket(SO_REUSEADDR)");
150 sin.sin_family = AF_INET;
151 sin.sin_port = htons(port);
152 sin.sin_addr.s_addr = INADDR_ANY;
154 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
159 if (listen(server_sock, 255) == -1) {
167 int main(int argc, char **argv)
171 parse_options(argc, argv);
173 if (destinations == NULL || sources == NULL) {
174 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
177 server_sock = get_server_socket(port);
179 // FIXME: fire off sender workers here
182 * Listen for incoming connections, spawning off one receiver
183 * thread for each (which will just gobble up the data until
187 struct sockaddr_in addr;
188 socklen_t addr_len = sizeof(addr);
192 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
198 // FIXME: these do not really set errno
199 if (pthread_attr_init(&attr) != 0) {
200 perror("pthread_attr_init()");
204 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
205 perror("pthread_attr_setstacksize");
209 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
210 perror("pthread_create()");