2009-11-02 22:43:19 +00:00
|
|
|
/*
|
2011-04-20 20:33:09 +00:00
|
|
|
* Copyright (c) 2009-2011, The Regents of the University of California,
|
|
|
|
* through Lawrence Berkeley National Laboratory (subject to receipt of any
|
|
|
|
* required approvals from the U.S. Dept. of Energy). All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is distributed under a BSD style license, see the LICENSE file
|
|
|
|
* for complete information.
|
2009-11-02 22:43:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <sys/time.h>
|
2010-07-22 23:26:38 +00:00
|
|
|
#include <sys/select.h>
|
2009-11-02 22:43:19 +00:00
|
|
|
|
|
|
|
#include "iperf.h"
|
|
|
|
#include "iperf_api.h"
|
|
|
|
#include "iperf_tcp.h"
|
|
|
|
#include "net.h"
|
|
|
|
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* iperf_tcp_recv
|
2009-11-02 22:43:19 +00:00
|
|
|
*
|
2010-07-22 18:57:08 +00:00
|
|
|
* receives the data for TCP
|
2009-11-02 22:43:19 +00:00
|
|
|
*/
|
|
|
|
int
|
2010-07-22 23:26:38 +00:00
|
|
|
iperf_tcp_recv(struct iperf_stream *sp)
|
2009-11-02 22:43:19 +00:00
|
|
|
{
|
2013-02-22 15:54:05 -08:00
|
|
|
int r;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
r = Nread(sp->socket, sp->buffer, sp->settings->blksize, Ptcp);
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
sp->result->bytes_received += r;
|
|
|
|
sp->result->bytes_received_this_interval += r;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
return r;
|
2009-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* iperf_tcp_send
|
2009-11-02 22:43:19 +00:00
|
|
|
*
|
2010-07-22 18:57:08 +00:00
|
|
|
* sends the data for TCP
|
2009-11-02 22:43:19 +00:00
|
|
|
*/
|
|
|
|
int
|
2010-07-22 23:26:38 +00:00
|
|
|
iperf_tcp_send(struct iperf_stream *sp)
|
2009-11-02 22:43:19 +00:00
|
|
|
{
|
2013-02-22 15:54:05 -08:00
|
|
|
int r;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
r = Nwrite(sp->socket, sp->buffer, sp->settings->blksize, Ptcp);
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2009-11-02 23:56:55 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
sp->result->bytes_sent += r;
|
|
|
|
sp->result->bytes_sent_this_interval += r;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
return r;
|
2009-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
|
|
|
|
/* iperf_tcp_accept
|
2009-11-02 22:43:19 +00:00
|
|
|
*
|
2010-07-22 18:57:08 +00:00
|
|
|
* accept a new TCP stream connection
|
2009-11-02 22:43:19 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
iperf_tcp_accept(struct iperf_test * test)
|
|
|
|
{
|
2010-07-22 18:57:08 +00:00
|
|
|
int s;
|
|
|
|
int rbuf = ACCESS_DENIED;
|
|
|
|
char cookie[COOKIE_SIZE];
|
2009-11-02 22:43:19 +00:00
|
|
|
socklen_t len;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
|
|
len = sizeof(addr);
|
2010-07-22 23:26:38 +00:00
|
|
|
if ((s = accept(test->listener, (struct sockaddr *) &addr, &len)) < 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
i_errno = IESTREAMCONNECT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-06-21 15:08:47 +00:00
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
if (Nread(s, cookie, COOKIE_SIZE, Ptcp) < 0) {
|
|
|
|
i_errno = IERECVCOOKIE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-23 18:39:14 +00:00
|
|
|
if (strcmp(test->cookie, cookie) != 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
if (Nwrite(s, &rbuf, sizeof(char), Ptcp) < 0) {
|
|
|
|
i_errno = IESENDMESSAGE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return s;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* iperf_tcp_listen
|
|
|
|
*
|
|
|
|
* start up a listener for TCP stream connections
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iperf_tcp_listen(struct iperf_test *test)
|
|
|
|
{
|
|
|
|
int s, opt;
|
2010-07-27 17:32:21 +00:00
|
|
|
struct addrinfo hints, *res;
|
|
|
|
char portstr[6];
|
2010-07-22 23:26:38 +00:00
|
|
|
s = test->listener;
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
if (test->no_delay || test->settings->mss || test->settings->socket_bufsize) {
|
2010-07-22 18:57:08 +00:00
|
|
|
FD_CLR(s, &test->read_set);
|
|
|
|
close(s);
|
2010-07-27 20:27:34 +00:00
|
|
|
if ((s = socket(test->settings->domain, SOCK_STREAM, 0)) < 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
i_errno = IESTREAMLISTEN;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
if (test->no_delay) {
|
|
|
|
opt = 1;
|
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IESETNODELAY;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// XXX: Setting MSS is very buggy!
|
2010-07-23 18:39:14 +00:00
|
|
|
if ((opt = test->settings->mss)) {
|
2010-07-22 18:57:08 +00:00
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_MAXSEG, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IESETMSS;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
if ((opt = test->settings->socket_bufsize)) {
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IESETBUF;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IESETBUF;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-22 18:57:08 +00:00
|
|
|
opt = 1;
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IEREUSEADDR;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-07-27 17:32:21 +00:00
|
|
|
snprintf(portstr, 6, "%d", test->server_port);
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2010-07-27 20:27:34 +00:00
|
|
|
hints.ai_family = test->settings->domain;
|
2010-07-27 17:32:21 +00:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
// XXX: Check getaddrinfo for errors!
|
|
|
|
if (getaddrinfo(test->bind_address, portstr, &hints, &res) != 0) {
|
|
|
|
i_errno = IESTREAMLISTEN;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-26 21:30:34 +00:00
|
|
|
}
|
2010-06-23 19:13:37 +00:00
|
|
|
|
2010-07-27 17:32:21 +00:00
|
|
|
if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
close(s);
|
|
|
|
i_errno = IESTREAMLISTEN;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
2010-06-21 15:08:47 +00:00
|
|
|
|
2010-07-27 17:32:21 +00:00
|
|
|
freeaddrinfo(res);
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
if (listen(s, 5) < 0) {
|
|
|
|
i_errno = IESTREAMLISTEN;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 23:26:38 +00:00
|
|
|
test->listener = s;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return s;
|
2009-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* iperf_tcp_connect
|
|
|
|
*
|
|
|
|
* connect to a TCP stream listener
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iperf_tcp_connect(struct iperf_test *test)
|
|
|
|
{
|
|
|
|
int s, opt;
|
2010-07-27 17:32:21 +00:00
|
|
|
struct addrinfo hints, *res;
|
|
|
|
char portstr[6];
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2010-07-27 20:27:34 +00:00
|
|
|
if ((s = socket(test->settings->domain, SOCK_STREAM, 0)) < 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
i_errno = IESTREAMCONNECT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
2010-07-26 21:30:34 +00:00
|
|
|
|
|
|
|
if (test->bind_address) {
|
2010-07-27 17:32:21 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2010-07-27 20:27:34 +00:00
|
|
|
hints.ai_family = test->settings->domain;
|
2010-07-27 17:32:21 +00:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
// XXX: Check getaddrinfo for errors!
|
|
|
|
if (getaddrinfo(test->bind_address, NULL, &hints, &res) != 0) {
|
2010-07-26 21:30:34 +00:00
|
|
|
i_errno = IESTREAMCONNECT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-26 21:30:34 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 17:32:21 +00:00
|
|
|
if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
|
2010-07-26 21:30:34 +00:00
|
|
|
i_errno = IESTREAMCONNECT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-26 21:30:34 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 17:32:21 +00:00
|
|
|
freeaddrinfo(res);
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
/* Set socket options */
|
2010-07-22 18:57:08 +00:00
|
|
|
if (test->no_delay) {
|
|
|
|
opt = 1;
|
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IESETNODELAY;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-23 18:39:14 +00:00
|
|
|
if ((opt = test->settings->mss)) {
|
2010-07-22 18:57:08 +00:00
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_MAXSEG, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IESETMSS;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
if ((opt = test->settings->socket_bufsize)) {
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IESETBUF;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) {
|
|
|
|
i_errno = IESETBUF;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2010-07-27 17:32:21 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2010-07-27 20:27:34 +00:00
|
|
|
hints.ai_family = test->settings->domain;
|
2010-07-27 17:32:21 +00:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
snprintf(portstr, 6, "%d", test->server_port);
|
|
|
|
// XXX: Check getaddrinfo for errors!
|
|
|
|
if (getaddrinfo(test->server_hostname, portstr, &hints, &res) != 0) {
|
|
|
|
i_errno = IESTREAMCONNECT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-27 17:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0 && errno != EINPROGRESS) {
|
2010-07-22 18:57:08 +00:00
|
|
|
i_errno = IESTREAMCONNECT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 17:32:21 +00:00
|
|
|
freeaddrinfo(res);
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* Send cookie for verification */
|
2010-07-23 18:39:14 +00:00
|
|
|
if (Nwrite(s, test->cookie, COOKIE_SIZE, Ptcp) < 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
i_errno = IESENDCOOKIE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return s;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|