Sponge
CS144's user-space TCP library
tun.cc
Go to the documentation of this file.
1 #include "tun.hh"
2 
3 #include "ipv4_datagram.hh"
4 #include "parser.hh"
5 #include "tcp_segment.hh"
6 #include "util.hh"
7 
8 #include <cstdlib>
9 #include <exception>
10 #include <iostream>
11 #include <vector>
12 
13 using namespace std;
14 
15 int main() {
16  try {
17  TunFD tun("tun144");
18  while (true) {
19  auto buffer = tun.read();
20  cout << "\n\n***\n*** Got packet:\n***\n";
22 
23  IPv4Datagram ip_dgram;
24 
25  cout << "attempting to parse as ipv4 datagram... ";
26  if (ip_dgram.parse(move(buffer)) != ParseResult::NoError) {
27  cout << "failed.\n";
28  continue;
29  }
30 
31  cout << "success! totlen=" << ip_dgram.header().len << ", IPv4 header contents:\n";
32  cout << ip_dgram.header().to_string();
33 
34  if (ip_dgram.header().proto != IPv4Header::PROTO_TCP) {
35  cout << "\nNot TCP, skipping.\n";
36  continue;
37  }
38 
39  cout << "\nAttempting to parse as a TCP segment... ";
40 
41  TCPSegment tcp_seg;
42 
43  if (tcp_seg.parse(ip_dgram.payload(), ip_dgram.header().pseudo_cksum()) != ParseResult::NoError) {
44  cout << "failed.\n";
45  continue;
46  }
47 
48  cout << "success! payload len=" << tcp_seg.payload().size() << ", TCP header contents:\n";
49  cout << tcp_seg.header().to_string() << endl;
50  }
51  } catch (const exception &e) {
52  cout << "Exception: " << e.what() << endl;
53  return EXIT_FAILURE;
54  }
55 
56  return EXIT_SUCCESS;
57 }
ParseResult::NoError
@ NoError
Success.
util.hh
std::exception
tcp_segment.hh
std::move
T move(T... args)
main
int main()
Definition: tun.cc:15
std::string::size
T size(T... args)
IPv4Datagram
IPv4 Internet datagram
Definition: ipv4_datagram.hh:8
FileDescriptor::read
std::string read(const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes.
buffer
std::string buffer
Definition: parser_example.cc:7
ipv4_datagram.hh
std::cout
tun.hh
IPv4Header::to_string
std::string to_string() const
Return a string containing a header in human-readable format.
Definition: ipv4_header.cc:134
IPv4Header::PROTO_TCP
static constexpr uint8_t PROTO_TCP
Protocol number for tcp.
Definition: ipv4_header.hh:11
TCPSegment::parse
ParseResult parse(const Buffer buffer, const uint32_t datagram_layer_checksum=0)
Parse the segment from a string.
Definition: tcp_segment.cc:12
IPv4Datagram::parse
ParseResult parse(const Buffer buffer)
Parse the segment from a string.
Definition: ipv4_datagram.cc:11
IPv4Header::pseudo_cksum
uint32_t pseudo_cksum() const
pseudo-header's contribution to the TCP checksum
Definition: ipv4_header.cc:125
TCPHeader::to_string
std::string to_string() const
Return a string containing a header in human-readable format.
Definition: tcp_header.cc:81
std::endl
T endl(T... args)
std
IPv4Header::len
uint16_t len
total length of packet
Definition: ipv4_header.hh:37
Buffer::size
size_t size() const
Size of the string.
Definition: buffer.hh:41
TCPSegment::payload
const Buffer & payload() const
Definition: tcp_segment.hh:27
TunFD
A FileDescriptor to a Linux TUN device.
Definition: tun.hh:16
IPv4Header::proto
uint8_t proto
protocol field
Definition: ipv4_header.hh:43
hexdump
void hexdump(const uint8_t *data, const size_t len, const size_t indent)
Definition: util.cc:113
TCPSegment
TCP segment
Definition: tcp_segment.hh:10
IPv4Datagram::header
const IPv4Header & header() const
Definition: ipv4_datagram.hh:22
std::string::data
T data(T... args)
TCPSegment::header
const TCPHeader & header() const
Definition: tcp_segment.hh:24
std::exception::what
T what(T... args)
parser.hh
IPv4Datagram::payload
const BufferList & payload() const
Definition: ipv4_datagram.hh:25