void Acceptor::do_work()
{
while (!should_stop()) {
- if (!wait_for_activity(server_sock, POLLIN, NULL)) {
+ if (!wait_for_activity(server_sock, POLLIN, nullptr)) {
continue;
}
AccessLogThread::AccessLogThread()
{
- pthread_mutex_init(&mutex, NULL);
+ pthread_mutex_init(&mutex, nullptr);
}
AccessLogThread::AccessLogThread(const string &filename)
: filename(filename) {
- pthread_mutex_init(&mutex, NULL);
+ pthread_mutex_init(&mutex, nullptr);
}
void AccessLogThread::write(const ClientStats& client)
{
// Open the file.
if (filename.empty()) {
- logfp = NULL;
+ logfp = nullptr;
} else {
logfp = fopen(filename.c_str(), "a+");
- if (logfp == NULL) {
+ if (logfp == nullptr) {
log_perror(filename.c_str());
// Continue as before.
}
swap(pending_writes, writes);
}
- if (logfp != NULL) {
+ if (logfp != nullptr) {
// Do the actual writes.
timespec now_monotonic;
timespec now_realtime;
// Wait until we are being woken up, either to quit or because
// there is material in pending_writes.
- wait_for_wakeup(NULL);
+ wait_for_wakeup(nullptr);
}
- if (logfp != NULL) {
+ if (logfp != nullptr) {
if (fclose(logfp) == EOF) {
log_perror("fclose");
}
}
- logfp = NULL;
+ logfp = nullptr;
}
private:
virtual void do_work();
- // The file we are logging to. If NULL, do not log.
+ // The file we are logging to. If nullptr, do not log.
FILE *logfp;
std::string filename;
Client::Client(int sock)
: sock(sock),
state(Client::READING_REQUEST),
- stream(NULL),
+ stream(nullptr),
header_or_short_response_bytes_sent(0),
stream_pos(0),
bytes_sent(0),
bytes_lost(0),
num_loss_events(0),
- tls_context(NULL),
- tls_data_to_send(NULL),
+ tls_context(nullptr),
+ tls_data_to_send(nullptr),
tls_data_left_to_send(0),
in_ktls_mode(false)
{
char buf[INET6_ADDRSTRLEN];
if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) {
// IPv4 address, really.
- if (inet_ntop(AF_INET, &addr.sin6_addr.s6_addr32[3], buf, sizeof(buf)) == NULL) {
+ if (inet_ntop(AF_INET, &addr.sin6_addr.s6_addr32[3], buf, sizeof(buf)) == nullptr) {
log_perror("inet_ntop");
remote_addr = "";
} else {
remote_addr = buf;
}
} else {
- if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
+ if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == nullptr) {
log_perror("inet_ntop");
remote_addr = "";
} else {
bytes_lost(serialized.bytes_lost()),
num_loss_events(serialized.num_loss_events())
{
- if (stream != NULL) {
+ if (stream != nullptr) {
if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &stream->pacing_rate, sizeof(stream->pacing_rate)) == -1) {
if (stream->pacing_rate != ~0U) {
log_perror("setsockopt(SO_MAX_PACING_RATE)");
tls_context = tls_import_context(
reinterpret_cast<const unsigned char *>(serialized.tls_context().data()),
serialized.tls_context().size());
- if (tls_context == NULL) {
+ if (tls_context == nullptr) {
log(WARNING, "tls_import_context() failed, TLS client might not survive across restart");
} else {
tls_data_to_send = tls_get_write_buffer(tls_context, &tls_data_left_to_send);
assert(serialized.tls_output_bytes_already_consumed() <= tls_data_left_to_send);
if (serialized.tls_output_bytes_already_consumed() >= tls_data_left_to_send) {
tls_buffer_clear(tls_context);
- tls_data_to_send = NULL;
+ tls_data_to_send = nullptr;
} else {
tls_data_to_send += serialized.tls_output_bytes_already_consumed();
tls_data_left_to_send -= serialized.tls_output_bytes_already_consumed();
in_ktls_mode = serialized.in_ktls_mode();
}
} else {
- tls_context = NULL;
+ tls_context = nullptr;
}
}
serialized.set_bytes_lost(bytes_lost);
serialized.set_num_loss_events(num_loss_events);
- if (tls_context != NULL) {
+ if (tls_context != nullptr) {
bool small_version = false;
- int required_size = tls_export_context(tls_context, NULL, 0, small_version);
+ int required_size = tls_export_context(tls_context, nullptr, 0, small_version);
if (required_size <= 0) {
// Can happen if we're in the middle of the key exchange, unfortunately.
// We'll get an error fairly fast, and this client hasn't started playing
// currently serialize in-progress key exchanges.
unsigned base_tls_data_left_to_send;
const unsigned char *base_tls_data_to_send = tls_get_write_buffer(tls_context, &base_tls_data_left_to_send);
- if (base_tls_data_to_send == NULL) {
- assert(tls_data_to_send == NULL);
+ if (base_tls_data_to_send == nullptr) {
+ assert(tls_data_to_send == nullptr);
} else {
assert(tls_data_to_send + tls_data_left_to_send == base_tls_data_to_send + base_tls_data_left_to_send);
}
bool read_config(const string &filename, vector<ConfigLine> *lines)
{
FILE *fp = fopen(filename.c_str(), "r");
- if (fp == NULL) {
+ if (fp == nullptr) {
log_perror(filename.c_str());
return false;
}
char buf[4096];
while (!feof(fp)) {
- if (fgets(buf, sizeof(buf), fp) == NULL) {
+ if (fgets(buf, sizeof(buf), fp) == nullptr) {
break;
}
contents->clear();
FILE *fp = fopen(filename.c_str(), "r");
- if (fp == NULL) {
+ if (fp == nullptr) {
log_perror(filename.c_str());
return false;
}
num_connection_attempts(0),
suppress_logging(false)
{
- pthread_mutex_init(&stats_mutex, NULL);
+ pthread_mutex_init(&stats_mutex, nullptr);
stats.url = url;
stats.bytes_received = 0;
stats.data_bytes_received = 0;
string protocol, user;
parse_url(url, &protocol, &user, &host, &port, &path); // Don't care if it fails.
- pthread_mutex_init(&stats_mutex, NULL);
+ pthread_mutex_init(&stats_mutex, nullptr);
stats.url = url;
stats.bytes_received = serialized.bytes_received();
stats.data_bytes_received = serialized.data_bytes_received();
if (serialized.has_connect_time()) {
stats.connect_time = serialized.connect_time();
} else {
- stats.connect_time = time(NULL);
+ stats.connect_time = time(nullptr);
}
if (serialized.has_latency_sec()) {
stats.latency_sec = serialized.latency_sec();
int HTTPInput::lookup_and_connect(const string &host, const string &port)
{
addrinfo *ai;
- int err = getaddrinfo(host.c_str(), port.c_str(), NULL, &ai);
+ int err = getaddrinfo(host.c_str(), port.c_str(), nullptr, &ai);
if (err != 0) {
if (!suppress_logging) {
log(WARNING, "[%s] Lookup of '%s' failed (%s).",
// Wait for the connect to complete, or an error to happen.
for ( ;; ) {
- bool complete = wait_for_activity(sock, POLLIN | POLLOUT, NULL);
+ bool complete = wait_for_activity(sock, POLLIN | POLLOUT, nullptr);
if (should_stop()) {
safe_close(sock);
return -1;
}
MutexLock lock(&stats_mutex);
- stats.connect_time = time(NULL);
+ stats.connect_time = time(nullptr);
clock_gettime(CLOCK_MONOTONIC_COARSE, &last_activity);
}
break;
if (status == RP_EXTRA_DATA) {
char *ptr = static_cast<char *>(
memmem(response.data(), response.size(), "\r\n\r\n", 4));
- assert(ptr != NULL);
+ assert(ptr != nullptr);
extra_data = string(ptr + 4, &response[0] + response.size());
response.resize(ptr - response.data());
}
char *ptr = static_cast<char *>(
memmem(pending_data.data(), pending_data.size(),
METACUBE2_SYNC, strlen(METACUBE2_SYNC)));
- if (ptr == NULL) {
+ if (ptr == nullptr) {
// OK, so we didn't find the sync marker. We know then that
// we do not have the _full_ marker in the buffer, but we
// could have N-1 bytes. Drop everything before that,
{
string protocol, user, host, port, path;
if (!parse_url(url, &protocol, &user, &host, &port, &path)) {
- return NULL;
+ return nullptr;
}
if (protocol == "http") {
return new HTTPInput(url, encoding);
// encoding is ignored; it's never Metacube.
return new UDPInput(url);
}
- return NULL;
+ return nullptr;
}
Input *create_input(const InputProto &serialized)
{
string protocol, user, host, port, path;
if (!parse_url(serialized.url(), &protocol, &user, &host, &port, &path)) {
- return NULL;
+ return nullptr;
}
if (protocol == "http") {
return new HTTPInput(serialized);
if (protocol == "udp") {
return new UDPInput(serialized);
}
- return NULL;
+ return nullptr;
}
Input::~Input() {}
bool parse_url(const std::string &url, std::string *protocol, std::string *user, std::string *host, std::string *port, std::string *path);
// Figure out the right type of input based on the URL, and create a new Input of the right type.
-// Will return NULL if unknown.
+// Will return nullptr if unknown.
Input *create_input(const std::string &url, Input::Encoding encoding);
Input *create_input(const InputProto &serialized);
}
fp = fdopen(fd, "w");
- if (fp == NULL) {
+ if (fp == nullptr) {
log_perror("fdopen");
safe_close(fd);
if (unlink(filename) == -1) {
goto sleep;
}
- now = time(NULL);
+ now = time(nullptr);
for (size_t i = 0; i < inputs.size(); ++i) {
InputStats stats = inputs[i]->get_stats();
fprintf(fp, "%s %llu %llu", stats.url.c_str(),
using namespace std;
// Yes, it's a bit ugly.
-#define SYSLOG_FAKE_FILE (static_cast<FILE *>(NULL))
+#define SYSLOG_FAKE_FILE (static_cast<FILE *>(nullptr))
bool logging_started = false;
vector<FILE *> log_destinations;
void add_log_destination_file(const string &filename)
{
FILE *fp = fopen(filename.c_str(), "a");
- if (fp == NULL) {
+ if (fp == nullptr) {
perror(filename.c_str());
return;
}
vsnprintf(formatted_msg, sizeof(formatted_msg), fmt, ap);
va_end(ap);
- time_t now = time(NULL);
+ time_t now = time(nullptr);
struct tm lt;
struct tm *ltime = localtime_r(&now, <);
char timestamp[1024];
- if (ltime == NULL) {
+ if (ltime == nullptr) {
strcpy(timestamp, "???");
} else {
strftime(timestamp, sizeof(timestamp), "%a, %d %b %Y %T %z", ltime);
using namespace std;
-AccessLogThread *access_log = NULL;
-ServerPool *servers = NULL;
+AccessLogThread *access_log = nullptr;
+ServerPool *servers = nullptr;
volatile bool hupped = false;
volatile bool stopped = false;
{
vector<Acceptor *> acceptors;
for (const AcceptorConfig &acceptor_config : config.acceptors) {
- Acceptor *acceptor = NULL;
+ Acceptor *acceptor = nullptr;
const auto deserialized_acceptor_it = deserialized_acceptors->find(acceptor_config);
if (deserialized_acceptor_it != deserialized_acceptors->end()) {
acceptor = deserialized_acceptor_it->second;
InputWithRefcount iwr;
iwr.input = create_input(src, encoding);
- if (iwr.input == NULL) {
+ if (iwr.input == nullptr) {
log(ERROR, "did not understand URL '%s' or source encoding was invalid, clients will not get any data.",
src.c_str());
return;
return false;
case 0:
// Child.
- execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, NULL);
+ execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, nullptr);
log_perror(argv0_copy);
_exit(1);
default:
char argv0_canon[PATH_MAX];
char config_filename_canon[PATH_MAX];
- if (realpath("/proc/self/exe", argv0_canon) == NULL) {
+ if (realpath("/proc/self/exe", argv0_canon) == nullptr) {
log_perror(argv[0]);
exit(1);
}
- if (realpath(config_filename.c_str(), config_filename_canon) == NULL) {
+ if (realpath(config_filename.c_str(), config_filename_canon) == nullptr) {
log_perror(config_filename.c_str());
exit(1);
}
err = clock_gettime(CLOCK_MONOTONIC, &serialize_start);
assert(err != -1);
- if (input_stats_thread != NULL) {
+ if (input_stats_thread != nullptr) {
input_stats_thread->stop();
input_stats_thread.reset();
}
- if (stats_thread != NULL) {
+ if (stats_thread != nullptr) {
stats_thread->stop();
stats_thread.reset();
}
sprintf(buf, "%d", state_fd);
for ( ;; ) {
- execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, NULL);
+ execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, nullptr);
open_logs(config.log_destinations);
log_perror("execlp");
log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv0_canon);
const char *ptr = reinterpret_cast<char *>(
memmem(existing_data->data() + start_at, existing_data->size() - start_at,
"\r\n\r\n", 4));
- if (ptr == NULL) {
+ if (ptr == nullptr) {
return RP_NOT_FINISHED_YET;
}
if (ptr != existing_data->data() + existing_data->size() - 4) {
Server::Server()
{
- pthread_mutex_init(&mutex, NULL);
- pthread_mutex_init(&queued_clients_mutex, NULL);
+ pthread_mutex_init(&mutex, nullptr);
+ pthread_mutex_init(&queued_clients_mutex, nullptr);
epoll_fd = epoll_create(1024); // Size argument is ignored.
if (epoll_fd == -1) {
if (is_tls) {
assert(tls_server_contexts.count(acceptor));
client_ptr->tls_context = tls_accept(tls_server_contexts[acceptor]);
- if (client_ptr->tls_context == NULL) {
+ if (client_ptr->tls_context == nullptr) {
log(ERROR, "tls_accept() failed");
close_client(client_ptr);
return;
int stream_index = lookup_stream_by_url(client.url());
if (stream_index == -1) {
assert(client.state() != Client::SENDING_DATA);
- stream = NULL;
+ stream = nullptr;
} else {
stream = streams[stream_index].get();
}
{
switch (client->state) {
case Client::READING_REQUEST: {
- if (client->tls_context != NULL) {
+ if (client->tls_context != nullptr) {
if (send_pending_tls_data(client)) {
// send_pending_tls_data() hit postconditions #1 or #4.
return;
// Try to read more of the request.
char buf[1024];
int ret;
- if (client->tls_context == NULL) {
+ if (client->tls_context == nullptr) {
ret = read_nontls_data(client, buf, sizeof(buf));
if (ret == -1) {
// read_nontls_data() hit postconditions #1 or #2.
bool Server::send_pending_tls_data(Client *client)
{
// See if there's data from the TLS library to write.
- if (client->tls_data_to_send == NULL) {
+ if (client->tls_data_to_send == nullptr) {
client->tls_data_to_send = tls_get_write_buffer(client->tls_context, &client->tls_data_left_to_send);
- if (client->tls_data_to_send == NULL) {
+ if (client->tls_data_to_send == nullptr) {
// Really no data to send.
return false;
}
if (ret > 0 && size_t(ret) == client->tls_data_left_to_send) {
// All data has been sent, so we don't need to go to sleep.
tls_buffer_clear(client->tls_context);
- client->tls_data_to_send = NULL;
+ client->tls_data_to_send = nullptr;
return false;
}
void Server::skip_lost_data(Client *client)
{
Stream *stream = client->stream;
- if (stream == NULL) {
+ if (stream == nullptr) {
return;
}
size_t bytes_to_send = stream->bytes_received - client->stream_pos;
void Server::close_client(Client *client)
{
- if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
+ if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, nullptr) == -1) {
log_perror("epoll_ctl(EPOLL_CTL_DEL)");
exit(1);
}
// This client could be sleeping, so we'll need to fix that. (Argh, O(n).)
- if (client->stream != NULL) {
+ if (client->stream != nullptr) {
delete_from(&client->stream->sleeping_clients, client);
delete_from(&client->stream->to_process, client);
}
int ServerPool::lookup_stream_by_url(const string &url) const
{
- assert(servers != NULL);
+ assert(servers != nullptr);
return servers[0].lookup_stream_by_url(url);
}
}
fp = fdopen(fd, "w");
- if (fp == NULL) {
+ if (fp == nullptr) {
log_perror("fdopen");
safe_close(fd);
if (unlink(filename) == -1) {
exit(1);
}
- pthread_mutex_init(&queued_data_mutex, NULL);
+ pthread_mutex_init(&queued_data_mutex, nullptr);
}
Stream::~Stream()
suitable_starting_points.push_back(point);
}
- pthread_mutex_init(&queued_data_mutex, NULL);
+ pthread_mutex_init(&queued_data_mutex, nullptr);
}
StreamProto Stream::serialize()
void Thread::run()
{
- pthread_mutex_init(&should_stop_mutex, NULL);
+ pthread_mutex_init(&should_stop_mutex, nullptr);
should_stop_status = false;
- pthread_create(&worker_thread, NULL, &Thread::do_work_thunk, this);
+ pthread_create(&worker_thread, nullptr, &Thread::do_work_thunk, this);
}
void Thread::stop()
should_stop_status = true;
}
wakeup();
- if (pthread_join(worker_thread, NULL) == -1) {
+ if (pthread_join(worker_thread, nullptr) == -1) {
log_perror("pthread_join");
exit(1);
}
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGHUP);
- int err = pthread_sigmask(SIG_BLOCK, &set, NULL);
+ int err = pthread_sigmask(SIG_BLOCK, &set, nullptr);
if (err != 0) {
errno = err;
log_perror("pthread_sigmask");
// Call the right thunk.
thread->do_work();
- return NULL;
+ return nullptr;
}
bool Thread::wait_for_activity(int fd, short events, const struct timespec *timeout_ts)
// the file descriptor.
//
// If fd is -1, wait until a wakeup or timeout.
- // if timeout_ts is NULL, there is no timeout.
+ // if timeout_ts is nullptr, there is no timeout.
bool wait_for_activity(int fd, short events, const timespec *timeout_ts);
// Wait until a wakeup.
construct_header();
- pthread_mutex_init(&stats_mutex, NULL);
+ pthread_mutex_init(&stats_mutex, nullptr);
stats.url = url;
stats.bytes_received = 0;
stats.data_bytes_received = 0;
stats.metadata_bytes_received = 0;
- stats.connect_time = time(NULL);
+ stats.connect_time = time(nullptr);
stats.latency_sec = HUGE_VAL;
}
construct_header();
- pthread_mutex_init(&stats_mutex, NULL);
+ pthread_mutex_init(&stats_mutex, nullptr);
stats.url = url;
stats.bytes_received = serialized.bytes_received();
stats.data_bytes_received = serialized.data_bytes_received();
if (serialized.has_connect_time()) {
stats.connect_time = serialized.connect_time();
} else {
- stats.connect_time = time(NULL);
+ stats.connect_time = time(nullptr);
}
}
}
// Wait for a packet, or a wakeup.
- bool activity = wait_for_activity(sock, POLLIN, NULL);
+ bool activity = wait_for_activity(sock, POLLIN, nullptr);
if (!activity) {
// Most likely, should_stop was set.
continue;