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 unsigned num_senders = 128;
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' },
30 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
33 FILE *in = fopen(filename, "r");
44 if (fgets(buf, 256, in) == NULL)
47 ptr = strchr(buf, '\n');
51 ptr = strchr(buf, '\r');
55 ptr = buf + strspn(buf, " \t");
57 if (ptr[0] == '#' || ptr[0] == 0)
60 he = gethostbyname(ptr);
66 // just pick the first for now
67 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
75 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
78 (*addr_list)[*num] = addr;
85 void parse_options(int argc, char **argv)
90 int c = getopt_long(argc, argv, "s:d:p:", longopts, &option_index);
93 read_ip_list(optarg, &sources, &num_sources, &room_sources);
96 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
102 return; // end of argument list
104 fprintf(stderr, "Invalid option\n");
110 void *sender_worker(void *arg)
112 printf("Dummy sender worker\n");
116 void *receiver_worker(void *arg)
121 printf("Received worker for socket %u\n", sock);
124 int ret = read(sock, buf, 65536);
128 // FIXME: update stats here
131 printf("Socket %u done\n", sock);
133 if (close(sock) == -1) {
141 int get_server_socket(unsigned short port)
144 struct sockaddr_in sin;
147 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
148 if (server_sock == -1) {
153 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
154 perror("setsocket(SO_REUSEADDR)");
158 sin.sin_family = AF_INET;
159 sin.sin_port = htons(port);
160 sin.sin_addr.s_addr = INADDR_ANY;
162 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
167 if (listen(server_sock, 255) == -1) {
175 int main(int argc, char **argv)
181 parse_options(argc, argv);
183 if (destinations == NULL || sources == NULL) {
184 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
187 server_sock = get_server_socket(port);
189 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
190 port, num_sources, num_destinations);
192 // FIXME: these do not really set errno
193 if (pthread_attr_init(&attr) != 0) {
194 perror("pthread_attr_init()");
198 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
199 perror("pthread_attr_setstacksize");
203 // Fire off sender workers.
204 for (i = 0; i < num_senders; ++i) {
207 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
208 perror("pthread_create()");
214 * Listen for incoming connections, spawning off one receiver
215 * thread for each (which will just gobble up the data until
219 struct sockaddr_in addr;
220 socklen_t addr_len = sizeof(addr);
223 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
229 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
230 perror("pthread_create()");