8 #include <sys/socket.h>
9 #include <netinet/in.h>
11 unsigned short port = 2007;
12 struct in_addr *destinations = NULL;
13 unsigned num_destinations = 0;
14 unsigned room_destinations = 0;
16 const static struct option longopts[] = {
17 { "destination-file", required_argument, NULL, 'd' },
18 { "port", required_argument, NULL, 'p' },
22 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
25 FILE *in = fopen(filename, "r");
36 if (fgets(buf, 256, in) == NULL)
39 ptr = strchr(buf, '\n');
43 ptr = strchr(buf, '\r');
47 ptr = buf + strspn(buf, " \t");
49 if (ptr[0] == '#' || ptr[0] == 0)
52 he = gethostbyname(ptr);
58 // just pick the first for now
59 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
67 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
70 (*addr_list)[*num] = addr;
77 void parse_options(int argc, char **argv)
82 int c = getopt_long(argc, argv, "d:p:", longopts, &option_index);
85 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
91 return; // end of argument list
93 fprintf(stderr, "Invalid option\n");
99 void *receiver_worker(void *arg)
104 printf("Received worker for socket %u\n", sock);
107 int ret = read(sock, buf, 65536);
111 // FIXME: update stats here
114 printf("Socket %u done\n", sock);
116 if (close(sock) == -1) {
124 int get_server_socket(unsigned short port)
127 struct sockaddr_in sin;
130 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
131 if (server_sock == -1) {
136 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
137 perror("setsocket(SO_REUSEADDR)");
141 sin.sin_family = AF_INET;
142 sin.sin_port = htons(port);
143 sin.sin_addr.s_addr = INADDR_ANY;
145 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
150 if (listen(server_sock, 255) == -1) {
158 int main(int argc, char **argv)
162 parse_options(argc, argv);
163 server_sock = get_server_socket(port);
165 // FIXME: fire off sender workers here
168 * Listen for incoming connections, spawning off one receiver
169 * thread for each (which will just gobble up the data until
173 struct sockaddr_in addr;
174 socklen_t addr_len = sizeof(addr);
178 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
184 // FIXME: these do not really set errno
185 if (pthread_attr_init(&attr) != 0) {
186 perror("pthread_attr_init()");
190 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
191 perror("pthread_attr_setstacksize");
195 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
196 perror("pthread_create()");