|
#define _GNU_SOURCE // for splice() |
|
#include <arpa/inet.h> |
|
#include <assert.h> |
|
#include <err.h> |
|
#include <errno.h> |
|
#include <fcntl.h> |
|
#include <netinet/in.h> |
|
#include <netinet/tcp.h> |
|
#include <signal.h> |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <sys/sendfile.h> |
|
#include <sys/socket.h> |
|
#include <sys/stat.h> |
|
#include <sys/time.h> |
|
#include <sys/types.h> |
|
#include <unistd.h> |
|
|
|
void do_tcp(int port) { |
|
int srv = socket(AF_INET, SOCK_STREAM, 0); |
|
if (srv == -1) |
|
err(EXIT_FAILURE, "socket"); |
|
const int on = 1; |
|
const int off = 0; |
|
if (setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
|
err(EXIT_FAILURE, "setsockopt(SO_REUSEADDR)"); |
|
if (setsockopt(srv, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) != 0) |
|
err(EXIT_FAILURE, "setsockopt(srv, TCP_NODELAY)"); |
|
struct sockaddr_in bindaddr = { |
|
.sin_family = AF_INET, |
|
}; |
|
bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
|
bindaddr.sin_port = htons(port); |
|
if (bind(srv, (struct sockaddr*)&bindaddr, sizeof(bindaddr)) != 0) |
|
err(EXIT_FAILURE, "bind"); |
|
if (listen(srv, 1) != 0) |
|
err(EXIT_FAILURE, "listen"); |
|
|
|
struct sockaddr_in cliaddr; |
|
socklen_t cliaddrlen = sizeof(cliaddr); |
|
int client = accept(srv, (struct sockaddr*)&cliaddr, &cliaddrlen); |
|
if (client == -1) |
|
err(EXIT_FAILURE, "client"); |
|
printf("Got client from %s:%d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); |
|
if (setsockopt(client, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) != 0) |
|
err(EXIT_FAILURE, "setsockopt(client, TCP_NODELAY)"); |
|
close(srv); |
|
|
|
for (;;) { |
|
static const char *magic = "MTU\0RTT\0""\0\0\0\0""\0\0\0\0"; |
|
const ssize_t mlen = 16, moff = 8; |
|
char buf[14600*2]; // RFC6928 initcwnd * 2 |
|
|
|
const ssize_t got = recv(client, buf, sizeof(buf), 0); |
|
if (got == -1) |
|
err(EXIT_FAILURE, "recv"); |
|
else if (got == 0) |
|
errx(EXIT_SUCCESS, "EOF from client"); |
|
else if (got > mlen && memcmp(buf, magic, mlen) == 0) { |
|
struct timeval tv; |
|
if (gettimeofday(&tv, NULL) != 0) |
|
err(EXIT_FAILURE, "gettimeofday"); |
|
*(uint32_t*)(buf+moff) = htonl(tv.tv_sec); |
|
*(uint32_t*)(buf+moff+4) = htonl(tv.tv_usec); |
|
} |
|
|
|
usleep(5000); |
|
|
|
if (setsockopt(client, IPPROTO_TCP, TCP_CORK, &on, sizeof(on)) != 0) |
|
err(EXIT_FAILURE, "setsockopt(client, TCP_CORK, on)"); |
|
const ssize_t sent = send(client, buf, got, 0); |
|
if (sent != got) |
|
err(EXIT_FAILURE, "send: sent (%zd) != got (%zd)", sent, got); |
|
if (setsockopt(client, IPPROTO_TCP, TCP_CORK, &off, sizeof(off)) != 0) |
|
err(EXIT_FAILURE, "setsockopt(client, TCP_CORK, off)"); |
|
} |
|
} |
|
|
|
void do_udp(int port) { |
|
int srv = socket(AF_INET, SOCK_DGRAM, 0); |
|
if (srv == -1) |
|
err(EXIT_FAILURE, "socket"); |
|
const int on = 1; |
|
if (setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) |
|
err(EXIT_FAILURE, "setsockopt(SO_REUSEADDR)"); |
|
struct sockaddr_in bindaddr = { |
|
.sin_family = AF_INET, |
|
}; |
|
bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
|
bindaddr.sin_port = htons(port); |
|
if (bind(srv, (struct sockaddr*)&bindaddr, sizeof(bindaddr)) != 0) |
|
err(EXIT_FAILURE, "bind"); |
|
|
|
for (;;) { |
|
struct sockaddr_in cliaddr; |
|
socklen_t cliaddrlen = sizeof(cliaddr); |
|
|
|
static const char *magic = "MTU\0RTT\0""\0\0\0\0""\0\0\0\0"; |
|
const ssize_t mlen = 16, moff = 8; |
|
char buf[0x20000]; // ui16*2 |
|
|
|
const ssize_t got = recvfrom(srv, buf, sizeof(buf), 0, (struct sockaddr*)&cliaddr, &cliaddrlen); |
|
if (got == -1) |
|
err(EXIT_FAILURE, "recv"); |
|
else if (got == 0) |
|
errx(EXIT_SUCCESS, "EOF from client"); |
|
else if (got > mlen && memcmp(buf, magic, mlen) == 0) { |
|
struct timeval tv; |
|
if (gettimeofday(&tv, NULL) != 0) |
|
err(EXIT_FAILURE, "gettimeofday"); |
|
|
|
*(uint32_t*)(buf+moff) = htonl(tv.tv_sec); |
|
*(uint32_t*)(buf+moff+4) = htonl(tv.tv_usec); |
|
|
|
const ssize_t sent = sendto(srv, buf, got, 0, (struct sockaddr*)&cliaddr, cliaddrlen); |
|
if (sent != got) |
|
err(EXIT_FAILURE, "send: sent (%zd) != got (%zd)", sent, got); |
|
} else if (got <= 50) { |
|
const ssize_t sent = sendto(srv, buf, got, 0, (struct sockaddr*)&cliaddr, cliaddrlen); |
|
if (sent != got) |
|
err(EXIT_FAILURE, "send: sent (%zd) != got (%zd)", sent, got); |
|
} else { |
|
errx(EXIT_FAILURE, "Got strange packet %zd bytes long, die to avoid flood", got); |
|
} |
|
} |
|
} |
|
|
|
int main(int argc, const char* argv[]) { |
|
if (argc != 3) |
|
errx(EXIT_SUCCESS, "Usage: %s <tcp|udp> port", argv[0]); |
|
|
|
int port = atoi(argv[2]); |
|
if (port <= 0 || 0xffff < port) |
|
errx(EXIT_SUCCESS, "bad port value <%s>", argv[2]); |
|
|
|
if (signal(SIGPIPE, SIG_IGN) != 0) |
|
err(EXIT_FAILURE, "signal"); |
|
|
|
if (strcmp(argv[1], "tcp") == 0) { |
|
do_tcp(port); |
|
} else if (strcmp(argv[1], "udp") == 0) { |
|
do_udp(port); |
|
} else { |
|
errx(EXIT_SUCCESS, "bad protocol <%s>", argv[1]); |
|
} |
|
|
|
return EXIT_SUCCESS; |
|
} |