Sponge
CS144's user-space TCP library
Functions
util.cc File Reference
#include "util.hh"
#include <array>
#include <cctype>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <sys/socket.h>
Include dependency graph for util.cc:

Go to the source code of this file.

Functions

mt19937 get_random_generator ()
 Seed a fast random generator. More...
 
void hexdump (const char *data, const size_t len, const size_t indent)
 
void hexdump (const uint8_t *data, const size_t len, const size_t indent)
 
int SystemCall (const char *attempt, const int return_value, const int errno_mask)
 Error-checking wrapper for most syscalls. More...
 
int SystemCall (const string &attempt, const int return_value, const int errno_mask)
 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()

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 
)
Parameters
[in]datais a pointer to the bytes to show
[in]lenis the number of bytes to show
[in]indentis the number of spaces to indent

Definition at line 142 of file util.cc.

◆ hexdump() [2/2]

void hexdump ( const uint8_t data,
const size_t  len,
const size_t  indent 
)
Parameters
[in]datais a pointer to the bytes to show
[in]lenis the number of bytes to show
[in]indentis the number of spaces to indent

Definition at line 113 of file util.cc.

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