|
Sponge
CS144's user-space TCP library
|
Go to the documentation of this file.
20 static void show_usage(
const char *argv0,
const char *msg) {
21 cout <<
"Usage: " << argv0 <<
" [options] <host> <port>\n\n"
22 <<
" Option Default\n"
25 <<
" -a <addr> Set IP source address (client mode only) " <<
LOCAL_ADDRESS_DFLT <<
"\n"
26 <<
" -s <port> Set TCP source port (client mode only) (random)\n\n"
27 <<
" -n <addr> Set IP next-hop address " <<
GATEWAY_DFLT <<
"\n"
34 <<
" -d <tapdev> Connect to tap <tapdev> " <<
TAP_DFLT <<
"\n\n"
36 <<
" -h Show this message.\n\n";
44 static void check_argc(
int argc,
char **argv,
int curr,
const char *err) {
45 if (curr + 3 >= argc) {
62 while (argc - curr > 2) {
63 if (
strncmp(
"-a", argv[curr], 3) == 0) {
64 check_argc(argc, argv, curr,
"ERROR: -a requires one argument.");
65 source_address = argv[curr + 1];
68 }
else if (
strncmp(
"-s", argv[curr], 3) == 0) {
69 check_argc(argc, argv, curr,
"ERROR: -s requires one argument.");
70 source_port = argv[curr + 1];
73 }
else if (
strncmp(
"-n", argv[curr], 3) == 0) {
74 check_argc(argc, argv, curr,
"ERROR: -n requires one argument.");
75 next_hop_address = argv[curr + 1];
78 }
else if (
strncmp(
"-w", argv[curr], 3) == 0) {
79 check_argc(argc, argv, curr,
"ERROR: -w requires one argument.");
80 c_fsm.recv_capacity =
strtol(argv[curr + 1],
nullptr, 0);
83 }
else if (
strncmp(
"-t", argv[curr], 3) == 0) {
84 check_argc(argc, argv, curr,
"ERROR: -t requires one argument.");
85 c_fsm.rt_timeout =
strtol(argv[curr + 1],
nullptr, 0);
88 }
else if (
strncmp(
"-d", argv[curr], 3) == 0) {
89 check_argc(argc, argv, curr,
"ERROR: -t requires one argument.");
90 tapdev = argv[curr + 1];
93 }
else if (
strncmp(
"-h", argv[curr], 3) == 0) {
98 show_usage(argv[0],
string(
"ERROR: unrecognized option " +
string(argv[curr])).c_str());
104 c_filt.destination = {argv[curr], argv[curr + 1]};
105 c_filt.source = {source_address, source_port};
107 Address next_hop{next_hop_address,
"0"};
109 return make_tuple(c_fsm, c_filt, next_hop, tapdev);
112 int main(
int argc,
char **argv) {
115 show_usage(argv[0],
"ERROR: required arguments are missing.");
121 for (
auto &
byte : local_ethernet_address) {
124 local_ethernet_address.at(0) |= 0x02;
125 local_ethernet_address.at(0) &= 0xfe;
127 auto [c_fsm, c_filt, next_hop, tap_dev_name] =
get_config(argc, argv);
132 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.
const string GATEWAY_DFLT
static void check_argc(int argc, char **argv, int curr, const char *err)
Wrapper around IPv4 addresses and DNS operations.
const string LOCAL_ADDRESS_DFLT
Config for classes derived from FdAdapter.
void connect(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad)
Connect using the specified configurations; blocks until connect succeeds or fails.
A FD adapter for IPv4 datagrams read from and written to a TAP device.
static tuple< TCPConfig, FdAdapterConfig, Address, string > get_config(int argc, char **argv)
Config for TCP sender and receiver.
void bidirectional_stream_copy(Socket &socket)
Copy socket input/output to stdin/stdout until finished.
int main(int argc, char **argv)
static constexpr uint16_t TIMEOUT_DFLT
Default re-transmit timeout is 1 second.
constexpr const char * TAP_DFLT
A FileDescriptor to a Linux TAP device.
static void show_usage(const char *argv0, const char *msg)
Multithreaded wrapper around TCPConnection that approximates the Unix sockets API.