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 { "num-senders", required_argument, NULL, 'n' },
27 { "port", required_argument, NULL, 'p' },
31 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
34 FILE *in = fopen(filename, "r");
45 if (fgets(buf, 256, in) == NULL)
48 ptr = strchr(buf, '\n');
52 ptr = strchr(buf, '\r');
56 ptr = buf + strspn(buf, " \t");
58 if (ptr[0] == '#' || ptr[0] == 0)
61 he = gethostbyname(ptr);
67 // just pick the first for now
68 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
76 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
79 (*addr_list)[*num] = addr;
86 void parse_options(int argc, char **argv)
91 int c = getopt_long(argc, argv, "s:d:n:p:", longopts, &option_index);
94 read_ip_list(optarg, &sources, &num_sources, &room_sources);
97 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
100 num_senders = atoi(optarg);
106 return; // end of argument list
108 fprintf(stderr, "Invalid option\n");
114 void *sender_worker(void *arg)
116 printf("Dummy sender worker\n");
120 void *receiver_worker(void *arg)
125 printf("Received worker for socket %u\n", sock);
128 int ret = read(sock, buf, 65536);
132 // FIXME: update stats here
135 printf("Socket %u done\n", sock);
137 if (close(sock) == -1) {
145 int get_server_socket(unsigned short port)
148 struct sockaddr_in sin;
151 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
152 if (server_sock == -1) {
157 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
158 perror("setsocket(SO_REUSEADDR)");
162 sin.sin_family = AF_INET;
163 sin.sin_port = htons(port);
164 sin.sin_addr.s_addr = INADDR_ANY;
166 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
171 if (listen(server_sock, 255) == -1) {
179 int main(int argc, char **argv)
185 parse_options(argc, argv);
187 if (destinations == NULL || sources == NULL) {
188 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
191 server_sock = get_server_socket(port);
193 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
194 port, num_sources, num_destinations);
196 // FIXME: these do not really set errno
197 if (pthread_attr_init(&attr) != 0) {
198 perror("pthread_attr_init()");
202 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
203 perror("pthread_attr_setstacksize");
207 // Fire off sender workers.
208 for (i = 0; i < num_senders; ++i) {
211 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
212 perror("pthread_create()");
218 * Listen for incoming connections, spawning off one receiver
219 * thread for each (which will just gobble up the data until
223 struct sockaddr_in addr;
224 socklen_t addr_len = sizeof(addr);
227 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
233 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
234 perror("pthread_create()");