9 #include <sys/socket.h>
10 #include <netinet/in.h>
12 unsigned short port = 2007;
14 struct in_addr *destinations = NULL;
15 unsigned num_destinations = 0;
16 unsigned room_destinations = 0;
18 struct in_addr *sources = NULL;
19 unsigned num_sources = 0;
20 unsigned room_sources = 0;
22 unsigned num_senders = 128;
24 const static struct option longopts[] = {
25 { "source-list", required_argument, NULL, 's' },
26 { "destination-list", required_argument, NULL, 'd' },
27 { "num-senders", required_argument, NULL, 'n' },
28 { "port", required_argument, NULL, 'p' },
32 double gen_pareto_random(double min, double k)
34 double u = rand() / (RAND_MAX+1.0);
35 return min * pow(u, -1.0 / k);
38 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
41 FILE *in = fopen(filename, "r");
52 if (fgets(buf, 256, in) == NULL)
55 ptr = strchr(buf, '\n');
59 ptr = strchr(buf, '\r');
63 ptr = buf + strspn(buf, " \t");
65 if (ptr[0] == '#' || ptr[0] == 0)
68 he = gethostbyname(ptr);
74 // just pick the first for now
75 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
83 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
86 (*addr_list)[*num] = addr;
93 void parse_options(int argc, char **argv)
98 int c = getopt_long(argc, argv, "s:d:n:p:", longopts, &option_index);
101 read_ip_list(optarg, &sources, &num_sources, &room_sources);
104 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
107 num_senders = atoi(optarg);
113 return; // end of argument list
115 fprintf(stderr, "Invalid option\n");
121 void *sender_worker(void *arg)
125 printf("Dummy sender worker\n");
127 for (i = 0; i < 1000; ++i) {
128 unsigned bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
129 printf("Sending %u bytes\n", bytes);
135 void *receiver_worker(void *arg)
140 printf("Received worker for socket %u\n", sock);
143 int ret = read(sock, buf, 65536);
147 // FIXME: update stats here
150 printf("Socket %u done\n", sock);
152 if (close(sock) == -1) {
160 int get_server_socket(unsigned short port)
163 struct sockaddr_in sin;
166 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
167 if (server_sock == -1) {
172 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
173 perror("setsocket(SO_REUSEADDR)");
177 sin.sin_family = AF_INET;
178 sin.sin_port = htons(port);
179 sin.sin_addr.s_addr = INADDR_ANY;
181 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
186 if (listen(server_sock, 255) == -1) {
194 int main(int argc, char **argv)
200 parse_options(argc, argv);
202 if (destinations == NULL || sources == NULL) {
203 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
206 server_sock = get_server_socket(port);
208 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
209 port, num_sources, num_destinations);
211 // FIXME: these do not really set errno
212 if (pthread_attr_init(&attr) != 0) {
213 perror("pthread_attr_init()");
217 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
218 perror("pthread_attr_setstacksize");
222 // Fire off sender workers.
223 for (i = 0; i < num_senders; ++i) {
226 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
227 perror("pthread_create()");
233 * Listen for incoming connections, spawning off one receiver
234 * thread for each (which will just gobble up the data until
238 struct sockaddr_in addr;
239 socklen_t addr_len = sizeof(addr);
242 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
248 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
249 perror("pthread_create()");