Sponge
CS144's user-space TCP library
util.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_UTIL_HH
2 #define SPONGE_LIBSPONGE_UTIL_HH
3 
4 #include <algorithm>
5 #include <cerrno>
6 #include <cstddef>
7 #include <cstdint>
8 #include <iterator>
9 #include <ostream>
10 #include <random>
11 #include <string>
12 #include <system_error>
13 #include <vector>
14 
17  private:
19 
20  public:
25  tagged_error(const std::error_category &category, const std::string &attempt, const int error_code)
26  : system_error(error_code, category), _attempt_and_error(attempt + ": " + std::system_error::what()) {}
27 
29  const char *what() const noexcept override { return _attempt_and_error.c_str(); }
30 };
31 
33 class unix_error : public tagged_error {
34  public:
38  explicit unix_error(const std::string &attempt, const int error = errno)
39  : tagged_error(std::system_category(), attempt, error) {}
40 };
41 
43 int SystemCall(const char *attempt, const int return_value, const int errno_mask = 0);
44 
46 int SystemCall(const std::string &attempt, const int return_value, const int errno_mask = 0);
47 
50 
52 uint64_t timestamp_ms();
53 
56  private:
57  uint32_t _sum;
58  bool _parity{};
59 
60  public:
61  InternetChecksum(const uint32_t initial_sum = 0);
62  void add(std::string_view data);
63  uint16_t value() const;
64 };
65 
67 void hexdump(const char *data, const size_t len, const size_t indent = 0);
68 
70 void hexdump(const uint8_t *data, const size_t len, const size_t indent = 0);
71 
72 #endif // SPONGE_LIBSPONGE_UTIL_HH
get_random_generator
std::mt19937 get_random_generator()
Seed a fast random generator.
Definition: util.cc:64
std::string
tagged_error::what
const char * what() const noexcept override
Returns a C string describing the error.
Definition: util.hh:29
hexdump
void hexdump(const char *data, const size_t len, const size_t indent=0)
Hexdump the contents of a packet (or any other sequence of bytes)
std::system_error
len
constexpr size_t len
Definition: tcp_benchmark.cc:12
std::error_category
InternetChecksum::add
void add(std::string_view data)
Definition: util.cc:89
tagged_error
std::system_error plus the name of what was being attempted
Definition: util.hh:16
InternetChecksum::value
uint16_t value() const
Definition: util.cc:100
std::mt19937
unix_error::unix_error
unix_error(const std::string &attempt, const int error=errno)
Definition: util.hh:38
tagged_error::_attempt_and_error
std::string _attempt_and_error
What was attempted, and what happened.
Definition: util.hh:18
timestamp_ms
uint64_t timestamp_ms()
Get the time in milliseconds since the program began.
Definition: util.cc:14
std::string::c_str
T c_str(T... args)
InternetChecksum::InternetChecksum
InternetChecksum(const uint32_t initial_sum=0)
Definition: util.cc:87
SystemCall
int SystemCall(const char *attempt, const int return_value, const int errno_mask=0)
Error-checking wrapper for most syscalls.
Definition: util.cc:42
tagged_error::tagged_error
tagged_error(const std::error_category &category, const std::string &attempt, const int error_code)
Construct from a category, an attempt, and an error code.
Definition: util.hh:25
InternetChecksum::_parity
bool _parity
Definition: util.hh:58
unix_error
a tagged_error for syscalls
Definition: util.hh:33
std
InternetChecksum
The internet checksum algorithm.
Definition: util.hh:55
InternetChecksum::_sum
uint32_t _sum
Definition: util.hh:57