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;
23 unsigned do_listen = 1;
25 const static struct option longopts[] = {
26 { "source-list", required_argument, NULL, 's' },
27 { "destination-list", required_argument, NULL, 'd' },
28 { "num-senders", required_argument, NULL, 'n' },
29 { "port", required_argument, NULL, 'p' },
30 { "sender-only", no_argument, NULL, 'o' },
34 // generates from [0,1>
35 double gen_uniform_random()
37 return rand() / (RAND_MAX+1.0);
40 double gen_pareto_random(double min, double k)
42 double u = gen_uniform_random();
43 return min * pow(u, -1.0 / k);
46 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
49 FILE *in = fopen(filename, "r");
60 if (fgets(buf, 256, in) == NULL)
63 ptr = strchr(buf, '\n');
67 ptr = strchr(buf, '\r');
71 ptr = buf + strspn(buf, " \t");
73 if (ptr[0] == '#' || ptr[0] == 0)
76 he = gethostbyname(ptr);
82 // just pick the first for now
83 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
91 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
94 (*addr_list)[*num] = addr;
101 void parse_options(int argc, char **argv)
103 int option_index = 0;
106 int c = getopt_long(argc, argv, "s:d:n:p:o", longopts, &option_index);
109 read_ip_list(optarg, &sources, &num_sources, &room_sources);
112 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
115 num_senders = atoi(optarg);
124 return; // end of argument list
126 fprintf(stderr, "Invalid option\n");
132 void *sender_worker(void *arg)
137 for (i = 0; i < 1000; ++i) {
138 unsigned src_num = (unsigned)(num_sources * gen_uniform_random());
139 unsigned dst_num = (unsigned)(num_destinations * gen_uniform_random());
140 unsigned num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
141 struct sockaddr_in sin;
143 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
149 // FIXME: bind to the right source
151 sin.sin_family = AF_INET;
152 sin.sin_port = htons(port);
153 sin.sin_addr = destinations[dst_num];
155 fprintf(stderr, "connecting %u\n", i);
157 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
162 fprintf(stderr, "connected\n");
164 // FIXME: send data here
172 void *receiver_worker(void *arg)
178 int ret = read(sock, buf, 65536);
182 // FIXME: update stats here
185 if (close(sock) == -1) {
193 int get_server_socket(unsigned short port)
196 struct sockaddr_in sin;
199 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
200 if (server_sock == -1) {
205 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
206 perror("setsocket(SO_REUSEADDR)");
210 sin.sin_family = AF_INET;
211 sin.sin_port = htons(port);
212 sin.sin_addr.s_addr = INADDR_ANY;
214 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
219 if (listen(server_sock, 255) == -1) {
227 int main(int argc, char **argv)
233 parse_options(argc, argv);
235 if (destinations == NULL || sources == NULL) {
236 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
240 server_sock = get_server_socket(port);
243 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
244 port, num_sources, num_destinations);
246 // FIXME: these do not really set errno
247 if (pthread_attr_init(&attr) != 0) {
248 perror("pthread_attr_init()");
252 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
253 perror("pthread_attr_setstacksize");
257 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
258 perror("pthread_attr_setdetachstate");
262 // Fire off sender workers.
263 for (i = 0; i < num_senders; ++i) {
266 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
267 perror("pthread_create()");
273 * Listen for incoming connections, spawning off one receiver
274 * thread for each (which will just gobble up the data until
283 struct sockaddr_in addr;
284 socklen_t addr_len = sizeof(addr);
287 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
293 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
294 perror("pthread_create()");