|
Sponge
CS144's user-space TCP library
|
Go to the documentation of this file.
19 static void show_usage(
const char *argv0,
const char *msg) {
20 cout <<
"Usage: " << argv0 <<
" [options] <host> <port>\n\n"
21 <<
" Option Default\n"
24 <<
" -l Server (listen) mode. (client mode)\n"
25 <<
" In server mode, <host>:<port> is the address to bind.\n\n"
28 <<
" -s <port> Set source port (client mode only) (random)\n\n"
35 <<
" -d <tundev> Connect to tun <tundev> " <<
TUN_DFLT <<
"\n\n"
37 <<
" -Lu <loss> Set uplink loss to <rate> (float in 0..1) (no loss)\n"
38 <<
" -Ld <loss> Set downlink loss to <rate> (float in 0..1) (no loss)\n\n"
40 <<
" -h Show this message.\n\n";
48 static void check_argc(
int argc,
char **argv,
int curr,
const char *err) {
49 if (curr + 3 >= argc) {
58 char *tundev =
nullptr;
66 while (argc - curr > 2) {
67 if (
strncmp(
"-l", argv[curr], 3) == 0) {
71 }
else if (
strncmp(
"-a", argv[curr], 3) == 0) {
72 check_argc(argc, argv, curr,
"ERROR: -a requires one argument.");
73 source_address = argv[curr + 1];
76 }
else if (
strncmp(
"-s", argv[curr], 3) == 0) {
77 check_argc(argc, argv, curr,
"ERROR: -s requires one argument.");
78 source_port = argv[curr + 1];
81 }
else if (
strncmp(
"-w", argv[curr], 3) == 0) {
82 check_argc(argc, argv, curr,
"ERROR: -w requires one argument.");
83 c_fsm.recv_capacity =
strtol(argv[curr + 1],
nullptr, 0);
86 }
else if (
strncmp(
"-t", argv[curr], 3) == 0) {
87 check_argc(argc, argv, curr,
"ERROR: -t requires one argument.");
88 c_fsm.rt_timeout =
strtol(argv[curr + 1],
nullptr, 0);
91 }
else if (
strncmp(
"-d", argv[curr], 3) == 0) {
92 check_argc(argc, argv, curr,
"ERROR: -t requires one argument.");
93 tundev = argv[curr + 1];
96 }
else if (
strncmp(
"-Lu", argv[curr], 3) == 0) {
97 check_argc(argc, argv, curr,
"ERROR: -Lu requires one argument.");
98 float lossrate =
strtof(argv[curr + 1],
nullptr);
99 using LossRateUpT = decltype(c_filt.loss_rate_up);
100 c_filt.loss_rate_up =
104 }
else if (
strncmp(
"-Ld", argv[curr], 3) == 0) {
105 check_argc(argc, argv, curr,
"ERROR: -Lu requires one argument.");
106 float lossrate =
strtof(argv[curr + 1],
nullptr);
107 using LossRateDnT = decltype(c_filt.loss_rate_dn);
108 c_filt.loss_rate_dn =
112 }
else if (
strncmp(
"-h", argv[curr], 3) == 0) {
117 show_usage(argv[0],
string(
"ERROR: unrecognized option " +
string(argv[curr])).c_str());
124 c_filt.source = {
"0", argv[curr + 1]};
125 if (c_filt.source.port() == 0) {
126 show_usage(argv[0],
"ERROR: listen port cannot be zero in server mode.");
130 c_filt.destination = {argv[curr], argv[curr + 1]};
131 c_filt.source = {source_address, source_port};
137 int main(
int argc,
char **argv) {
140 show_usage(argv[0],
"ERROR: required arguments are missing.");
151 tcp_socket.
connect(c_fsm, c_filt);
static constexpr size_t MAX_PAYLOAD_SIZE
Max TCP payload that fits in either IPv4 or UDP datagram.
constexpr const char * TUN_DFLT
static void show_usage(const char *argv0, const char *msg)
const string LOCAL_ADDRESS_DFLT
Config for classes derived from FdAdapter.
LossyFdAdapter< TCPOverIPv4OverTunFdAdapter > LossyTCPOverIPv4OverTunFdAdapter
Typedef for TCPOverIPv4OverTunFdAdapter.
void connect(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad)
Connect using the specified configurations; blocks until connect succeeds or fails.
Config for TCP sender and receiver.
void listen_and_accept(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad)
Listen and accept using the specified configurations; blocks until accept succeeds or fails.
void bidirectional_stream_copy(Socket &socket)
Copy socket input/output to stdin/stdout until finished.
A FD adapter for IPv4 datagrams read from and written to a TUN device.
int main(int argc, char **argv)
static constexpr uint16_t TIMEOUT_DFLT
Default re-transmit timeout is 1 second.
static tuple< TCPConfig, FdAdapterConfig, bool, char * > get_config(int argc, char **argv)
A FileDescriptor to a Linux TUN device.
static void check_argc(int argc, char **argv, int curr, const char *err)
Multithreaded wrapper around TCPConnection that approximates the Unix sockets API.