+ unsigned i;
+ int sock;
+ char buf[65536];
+ unsigned long long bytes_sent = 0;
+
+ for (i = 0; i < 65536; ++i)
+ buf[i] = rand() & 0xff;
+
+ for (i = 0; i < 1000; ++i) {
+ unsigned src_num = (unsigned)(num_sources * gen_uniform_random());
+ unsigned dst_num = (unsigned)(num_destinations * gen_uniform_random());
+ unsigned num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
+ struct sockaddr_in sin;
+
+ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sock == -1) {
+ perror("socket()");
+ exit(1);
+ }
+
+ // FIXME: bind to the right source
+
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(port);
+ sin.sin_addr = destinations[dst_num];
+
+ fprintf(stderr, "connecting %u\n", i);
+
+ if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
+ perror("connect()");
+ exit(1);
+ }
+
+ fprintf(stderr, "connected\n");
+
+ while (num_bytes > 0) {
+ unsigned bytes_to_send = num_bytes;
+ unsigned ret;
+
+ if (bytes_to_send > 65536) {
+ bytes_to_send = 65536;
+ }
+
+ ret = send(sock, buf, bytes_to_send, MSG_NOSIGNAL);
+ if (ret == -1) {
+ perror("send()");
+ exit(1);
+ }
+
+ num_bytes -= ret;
+ bytes_sent += ret;
+
+ // update the central counter after every 1MB (8ms
+ // at gigabit speeds, should be enough) of sent data
+ if (bytes_sent > 1048576) {
+ pthread_mutex_lock(&send_mutex);
+ total_bytes_sent += bytes_sent;
+ pthread_mutex_unlock(&send_mutex);
+
+ bytes_sent = 0;
+ }
+ }
+
+ close(sock);
+ }
+
+ pthread_mutex_lock(&send_mutex);
+ total_bytes_sent += bytes_sent;
+ pthread_mutex_unlock(&send_mutex);
+
+ pthread_exit(0);
+}
+
+void *receiver_worker(void *arg)
+{
+ int sock = (int)arg;
+ char buf[65536];
+ unsigned long long bytes_received = 0;
+
+ fprintf(stderr, "Receiver started\n");
+
+ for ( ;; ) {
+ int ret = read(sock, buf, 65536);
+ if (ret == 0)
+ break;
+
+ // update the central counter after every 1MB (8ms
+ // at gigabit speeds, should be enough) of received data
+ if (bytes_received > 1048576) {
+ pthread_mutex_lock(&receive_mutex);
+ total_bytes_sent += bytes_received;
+ pthread_mutex_unlock(&receive_mutex);
+
+ bytes_received = 0;
+ }
+ }
+
+ if (close(sock) == -1) {
+ perror("close()");
+ exit(1);
+ }
+
+ pthread_mutex_lock(&receive_mutex);
+ total_bytes_received += bytes_received;
+ pthread_mutex_unlock(&receive_mutex);
+
+ pthread_exit(0);
+}
+
+void *receiver_dispatcher(void *arg)
+{
+ int server_sock = (int)arg;
+ pthread_attr_t attr;
+
+ // FIXME: these do not really set errno
+ if (pthread_attr_init(&attr) != 0) {
+ perror("pthread_attr_init()");
+ exit(1);
+ }
+
+ if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
+ perror("pthread_attr_setstacksize");
+ exit(1);
+ }
+
+ if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
+ perror("pthread_attr_setdetachstate");
+ exit(1);
+ }
+
+ /*
+ * Listen for incoming connections, spawning off one receiver
+ * thread for each (which will just gobble up the data until
+ * we're done).
+ */
+ for ( ;; ) {
+ struct sockaddr_in addr;
+ socklen_t addr_len = sizeof(addr);
+ pthread_t thread;
+
+ int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
+ if (sock == -1) {
+ perror("accept()");
+ exit(1);
+ }
+
+ if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
+ perror("pthread_create()");
+ exit(1);
+ }
+ }
+}
+
+int get_server_socket(unsigned short port)
+{
+ int server_sock;
+ struct sockaddr_in sin;
+ unsigned one = 1;
+
+ server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);