Sponge
CS144's user-space TCP library
Classes | Functions
util.hh File Reference
#include <algorithm>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <ostream>
#include <random>
#include <string>
#include <system_error>
#include <vector>
Include dependency graph for util.hh:
This graph shows which files directly or indirectly include this file:

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...
 

Function Documentation

◆ get_random_generator()

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:

Definition at line 64 of file util.cc.

◆ hexdump() [1/2]

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)

◆ hexdump() [2/2]

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)

◆ SystemCall() [1/2]

int SystemCall ( const char *  attempt,
const int  return_value,
const int  errno_mask 
)

Error-checking wrapper for most syscalls.

Parameters
[in]attemptis the name of the syscall to try (for error reporting)
[in]return_valueis the return value of the syscall
[in]errno_maskis 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:

const int foo_fd = SystemCall("open", ::open("/tmp/foo", O_RDWR));
SystemCall("socketpair", ::socketpair(AF_UNIX, SOCK_STREAM, 0, fds.data()))

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:

// open a file, or print an error if permission was denied
const int foo_fd = SystemCall("open", ::open("/tmp/foo", O_RDWR), EACCESS);
if (foo_fd < 0) {
std::cerr << "Access to /tmp/foo was denied." << std::endl;
}
T endl(T... args)

Definition at line 42 of file util.cc.

◆ SystemCall() [2/2]

int SystemCall ( const string attempt,
const int  return_value,
const int  errno_mask 
)

Version of SystemCall that takes a C++ std::string.

Parameters
[in]attemptis the name of the syscall to try (for error reporting)
[in]return_valueis the return value of the syscall
[in]errno_maskis any errno value that is acceptable, e.g., EAGAIN when reading a non-blocking fd

see the other SystemCall() documentation for more details

Definition at line 54 of file util.cc.

◆ timestamp_ms()

uint64_t timestamp_ms ( )

Get the time in milliseconds since the program began.

Returns
the number of milliseconds since the program started

Definition at line 14 of file util.cc.