Sponge
CS144's user-space TCP library
|
#include <algorithm>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <ostream>
#include <random>
#include <string>
#include <system_error>
#include <vector>
Go to the source code of this file.
Classes | |
class | InternetChecksum |
The internet checksum algorithm. More... | |
class | tagged_error |
std::system_error plus the name of what was being attempted More... | |
class | unix_error |
a tagged_error for syscalls More... | |
Functions | |
std::mt19937 | get_random_generator () |
Seed a fast random generator. More... | |
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) More... | |
void | hexdump (const uint8_t *data, const size_t len, const size_t indent=0) |
Hexdump the contents of a packet (or any other sequence of bytes) More... | |
int | SystemCall (const char *attempt, const int return_value, const int errno_mask=0) |
Error-checking wrapper for most syscalls. More... | |
int | SystemCall (const std::string &attempt, const int return_value, const int errno_mask=0) |
Version of SystemCall that takes a C++ std::string. More... | |
uint64_t | timestamp_ms () |
Get the time in milliseconds since the program began. More... | |
std::mt19937 get_random_generator | ( | ) |
Seed a fast random generator.
A properly seeded mt19937 generator takes a lot of entropy!
This code borrows from the following:
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)
void hexdump | ( | const uint8_t * | data, |
const size_t | len, | ||
const size_t | indent = 0 |
||
) |
Hexdump the contents of a packet (or any other sequence of bytes)
int SystemCall | ( | const char * | attempt, |
const int | return_value, | ||
const int | errno_mask | ||
) |
Error-checking wrapper for most syscalls.
[in] | attempt | is the name of the syscall to try (for error reporting) |
[in] | return_value | is the return value of the syscall |
[in] | errno_mask | is any errno value that is acceptable, e.g., EAGAIN when reading a non-blocking fd |
This function works for any syscall that returns less than 0 on error and sets errno:
For example, to wrap a call to open(2), you might say:
If you don't have permission to open the file, SystemCall will throw a std::runtime_error. If you don't want to throw in that case, you can pass EACCESS
in errno_mask
:
int SystemCall | ( | const string & | attempt, |
const int | return_value, | ||
const int | errno_mask | ||
) |
Version of SystemCall that takes a C++ std::string.
[in] | attempt | is the name of the syscall to try (for error reporting) |
[in] | return_value | is the return value of the syscall |
[in] | errno_mask | is any errno value that is acceptable, e.g., EAGAIN when reading a non-blocking fd |
see the other SystemCall() documentation for more details