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 // generates from [0,1>
33 double gen_uniform_random()
35 return rand() / (RAND_MAX+1.0);
38 double gen_pareto_random(double min, double k)
40 double u = gen_uniform_random();
41 return min * pow(u, -1.0 / k);
44 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
47 FILE *in = fopen(filename, "r");
58 if (fgets(buf, 256, in) == NULL)
61 ptr = strchr(buf, '\n');
65 ptr = strchr(buf, '\r');
69 ptr = buf + strspn(buf, " \t");
71 if (ptr[0] == '#' || ptr[0] == 0)
74 he = gethostbyname(ptr);
80 // just pick the first for now
81 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
89 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
92 (*addr_list)[*num] = addr;
99 void parse_options(int argc, char **argv)
101 int option_index = 0;
104 int c = getopt_long(argc, argv, "s:d:n:p:", longopts, &option_index);
107 read_ip_list(optarg, &sources, &num_sources, &room_sources);
110 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
113 num_senders = atoi(optarg);
119 return; // end of argument list
121 fprintf(stderr, "Invalid option\n");
127 void *sender_worker(void *arg)
132 for (i = 0; i < 1000; ++i) {
133 unsigned src_num = (unsigned)(num_sources * gen_uniform_random());
134 unsigned dst_num = (unsigned)(num_destinations * gen_uniform_random());
135 unsigned num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
136 struct sockaddr_in sin;
138 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
144 // FIXME: bind to the right source
146 sin.sin_family = AF_INET;
147 sin.sin_port = htons(port);
148 sin.sin_addr = destinations[dst_num];
150 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
155 // FIXME: send data here
163 void *receiver_worker(void *arg)
168 printf("Received worker for socket %u\n", sock);
171 int ret = read(sock, buf, 65536);
175 // FIXME: update stats here
178 printf("Socket %u done\n", sock);
180 if (close(sock) == -1) {
188 int get_server_socket(unsigned short port)
191 struct sockaddr_in sin;
194 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
195 if (server_sock == -1) {
200 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
201 perror("setsocket(SO_REUSEADDR)");
205 sin.sin_family = AF_INET;
206 sin.sin_port = htons(port);
207 sin.sin_addr.s_addr = INADDR_ANY;
209 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
214 if (listen(server_sock, 255) == -1) {
222 int main(int argc, char **argv)
228 parse_options(argc, argv);
230 if (destinations == NULL || sources == NULL) {
231 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
234 server_sock = get_server_socket(port);
236 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
237 port, num_sources, num_destinations);
239 // FIXME: these do not really set errno
240 if (pthread_attr_init(&attr) != 0) {
241 perror("pthread_attr_init()");
245 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
246 perror("pthread_attr_setstacksize");
250 // Fire off sender workers.
251 for (i = 0; i < num_senders; ++i) {
254 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
255 perror("pthread_create()");
261 * Listen for incoming connections, spawning off one receiver
262 * thread for each (which will just gobble up the data until
266 struct sockaddr_in addr;
267 socklen_t addr_len = sizeof(addr);
270 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
276 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
277 perror("pthread_create()");