use phosg namespace

This commit is contained in:
Martin Michelsen
2024-07-28 11:54:41 -07:00
parent 5523388ad4
commit 1bfbf09891
116 changed files with 2969 additions and 3088 deletions
+50 -51
View File
@@ -22,7 +22,7 @@ using namespace std;
static const size_t DEFAULT_RESEND_PUSH_USECS = 200000; // 200ms
static string unescape_hdlc_frame(const void* data, size_t size) {
StringReader r(data, size);
phosg::StringReader r(data, size);
if (r.get_u8(data) != 0x7E) {
throw runtime_error("HDLC frame does not begin with 7E");
}
@@ -53,7 +53,7 @@ static string escape_hdlc_frame(const void* data, size_t size, uint32_t escape_c
throw runtime_error("HDLC frame too small for start and end sentinels");
}
StringReader r(data, size);
phosg::StringReader r(data, size);
if (r.pget_u8(size - 1) != 0x7E) {
throw runtime_error("HDLC frame does not end with 7E");
}
@@ -104,9 +104,9 @@ string IPStackSimulator::str_for_ipv4_netloc(uint32_t addr, uint16_t port) {
be_uint32_t be_addr = addr;
char addr_str[INET_ADDRSTRLEN];
if (!inet_ntop(AF_INET, &be_addr, addr_str, INET_ADDRSTRLEN)) {
return string_printf("<UNKNOWN>:%hu", port);
return phosg::string_printf("<UNKNOWN>:%hu", port);
} else {
return string_printf("%s:%hu", addr_str, port);
return phosg::string_printf("%s:%hu", addr_str, port);
}
}
@@ -115,7 +115,7 @@ string IPStackSimulator::str_for_tcp_connection(shared_ptr<const IPClient> c, co
string server_netloc_str = str_for_ipv4_netloc(conn.server_addr, conn.server_port);
string client_netloc_str = str_for_ipv4_netloc(c->ipv4_addr, conn.client_port);
int fd = bufferevent_getfd(c->bev.get());
return string_printf("%d+%016" PRIX64 " (%s -> %s)",
return phosg::string_printf("%d+%016" PRIX64 " (%s -> %s)",
fd, key, client_netloc_str.c_str(), server_netloc_str.c_str());
}
@@ -137,7 +137,7 @@ IPStackSimulator::~IPStackSimulator() {
}
void IPStackSimulator::listen(const string& name, const string& socket_path, Protocol proto) {
int fd = ::listen(socket_path, 0, SOMAXCONN);
int fd = phosg::listen(socket_path, 0, SOMAXCONN);
ip_stack_simulator_log.info("Listening on Unix socket %s on fd %d as %s", socket_path.c_str(), fd, name.c_str());
this->add_socket(name, fd, proto);
}
@@ -146,8 +146,8 @@ void IPStackSimulator::listen(const string& name, const string& addr, int port,
if (port == 0) {
this->listen(name, addr, proto);
} else {
int fd = ::listen(addr, port, SOMAXCONN);
string netloc_str = render_netloc(addr, port);
int fd = phosg::listen(addr, port, SOMAXCONN);
string netloc_str = phosg::render_netloc(addr, port);
ip_stack_simulator_log.info("Listening on TCP interface %s on fd %d as %s", netloc_str.c_str(), fd, name.c_str());
this->add_socket(name, fd, proto);
}
@@ -195,7 +195,7 @@ IPStackSimulator::IPClient::IPClient(
ipv4_addr(0),
idle_timeout_event(event_new(sim->base.get(), -1, EV_TIMEOUT, &IPStackSimulator::IPClient::dispatch_on_idle_timeout, this), event_free) {
uint64_t idle_timeout_usecs = sim->state->client_idle_timeout_usecs;
struct timeval tv = usecs_to_timeval(idle_timeout_usecs);
struct timeval tv = phosg::usecs_to_timeval(idle_timeout_usecs);
event_add(this->idle_timeout_event.get(), &tv);
}
@@ -247,7 +247,7 @@ void IPStackSimulator::dispatch_on_listen_accept(
void IPStackSimulator::on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr*, int) {
struct sockaddr_storage remote_addr;
get_socket_addresses(fd, nullptr, &remote_addr);
phosg::get_socket_addresses(fd, nullptr, &remote_addr);
if (this->state->banned_ipv4_ranges->check(remote_addr)) {
close(fd);
return;
@@ -304,7 +304,7 @@ void IPStackSimulator::IPClient::on_client_input(struct bufferevent* bev) {
}
uint64_t idle_timeout_usecs = sim ? sim->state->client_idle_timeout_usecs : 60000000;
struct timeval tv = usecs_to_timeval(idle_timeout_usecs);
struct timeval tv = phosg::usecs_to_timeval(idle_timeout_usecs);
event_add(this->idle_timeout_event.get(), &tv);
switch (this->protocol) {
@@ -325,7 +325,7 @@ void IPStackSimulator::IPClient::on_client_input(struct bufferevent* bev) {
sim->on_client_frame(this->shared_from_this(), frame);
} catch (const exception& e) {
if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) {
print_data(stderr, frame);
phosg::print_data(stderr, frame);
}
}
}
@@ -360,7 +360,7 @@ void IPStackSimulator::IPClient::on_client_input(struct bufferevent* bev) {
sim->on_client_frame(this->shared_from_this(), frame);
} catch (const exception& e) {
if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) {
print_data(stderr, frame);
phosg::print_data(stderr, frame);
}
}
}
@@ -416,7 +416,7 @@ void IPStackSimulator::send_layer3_frame(shared_ptr<IPClient> c, FrameInfo::Prot
evbuffer_add(out_buf, &ether, sizeof(ether));
evbuffer_add(out_buf, data, size);
if (this->pcap_text_log_file) {
StringWriter w;
phosg::StringWriter w;
w.write(&ether, sizeof(ether));
w.write(data, size);
this->log_frame(w.str());
@@ -451,7 +451,7 @@ void IPStackSimulator::send_layer3_frame(shared_ptr<IPClient> c, FrameInfo::Prot
throw logic_error("unknown layer 3 protocol");
}
StringWriter w;
phosg::StringWriter w;
w.put(hdlc);
w.write(data, size);
w.put_u16l(FrameInfo::computed_hdlc_checksum(w.str().data() + 1, w.size() - 1));
@@ -459,7 +459,7 @@ void IPStackSimulator::send_layer3_frame(shared_ptr<IPClient> c, FrameInfo::Prot
string escaped = escape_hdlc_frame(w.str(), c->hdlc_escape_control_character_flags);
if (ip_stack_simulator_log.debug("Sending HDLC frame to virtual network (escaped to %zX bytes)", escaped.size())) {
print_data(stderr, w.str());
phosg::print_data(stderr, w.str());
}
if (c->protocol == Protocol::HDLC_TAPSERVER) {
@@ -490,12 +490,12 @@ void IPStackSimulator::on_client_frame(shared_ptr<IPClient> c, const string& fra
effective_data = &hdlc_unescaped_data;
}
if (ip_stack_simulator_log.debug("Virtual network sent frame")) {
print_data(stderr, *effective_data);
phosg::print_data(stderr, *effective_data);
}
this->log_frame(*effective_data);
FrameInfo fi(link_type, *effective_data);
if (ip_stack_simulator_log.should_log(LogLevel::DEBUG)) {
if (ip_stack_simulator_log.should_log(phosg::LogLevel::DEBUG)) {
string fi_header = fi.header_str();
ip_stack_simulator_log.debug("Frame header: %s", fi_header.c_str());
}
@@ -510,7 +510,7 @@ void IPStackSimulator::on_client_frame(shared_ptr<IPClient> c, const string& fra
uint16_t expected_checksum = fi.computed_hdlc_checksum();
uint16_t stored_checksum = fi.stored_hdlc_checksum();
if (expected_checksum != stored_checksum) {
throw runtime_error(string_printf(
throw runtime_error(phosg::string_printf(
"HDLC checksum is incorrect (%04hX expected, %04hX received)",
expected_checksum, stored_checksum));
}
@@ -533,7 +533,7 @@ void IPStackSimulator::on_client_frame(shared_ptr<IPClient> c, const string& fra
} else if (fi.ipv4) {
uint16_t expected_ipv4_checksum = fi.computed_ipv4_header_checksum();
if (fi.ipv4->checksum != expected_ipv4_checksum) {
throw runtime_error(string_printf(
throw runtime_error(phosg::string_printf(
"IPv4 header checksum is incorrect (%04hX expected, %04hX received)",
expected_ipv4_checksum, fi.ipv4->checksum.load()));
}
@@ -545,7 +545,7 @@ void IPStackSimulator::on_client_frame(shared_ptr<IPClient> c, const string& fra
if (fi.udp) {
uint16_t expected_udp_checksum = fi.computed_udp4_checksum();
if (fi.udp->checksum != expected_udp_checksum) {
throw runtime_error(string_printf(
throw runtime_error(phosg::string_printf(
"UDP checksum is incorrect (%04hX expected, %04hX received)",
expected_udp_checksum, fi.udp->checksum.load()));
}
@@ -554,7 +554,7 @@ void IPStackSimulator::on_client_frame(shared_ptr<IPClient> c, const string& fra
} else if (fi.tcp) {
uint16_t expected_tcp_checksum = fi.computed_tcp4_checksum();
if (fi.tcp->checksum != expected_tcp_checksum) {
throw runtime_error(string_printf(
throw runtime_error(phosg::string_printf(
"TCP checksum is incorrect (%04hX expected, %04hX received)",
expected_tcp_checksum, fi.tcp->checksum.load()));
}
@@ -576,7 +576,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr<IPClient> c, const FrameIn
while (!opts_r.eof()) {
uint8_t opt = opts_r.get_u8();
string opt_data = opts_r.read(opts_r.get_u8() - 2);
StringReader opt_data_r(opt_data);
phosg::StringReader opt_data_r(opt_data);
switch (opt) {
case 0x01: // Maximum receive unit
// TODO: Currently we ignore this, but we probably should use it.
@@ -593,7 +593,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr<IPClient> c, const FrameIn
case 0x04: // Quality protocol
case 0x07: // Protocol field compression
case 0x08: // Address and control field compression
throw runtime_error(string_printf("unimplemented LCP option %02hhX (%zu bytes)", opt, opt_data.size()));
throw runtime_error(phosg::string_printf("unimplemented LCP option %02hhX (%zu bytes)", opt, opt_data.size()));
default:
throw runtime_error("unknown LCP option");
}
@@ -602,7 +602,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr<IPClient> c, const FrameIn
// lazy to do this right now. In our situation, it should suffice to
// simply always send a Configure-Request to the client with a magic
// number not equal to the one we received.
StringWriter opts_w;
phosg::StringWriter opts_w;
opts_w.put_u8(0x01); // Maximum receive unit
opts_w.put_u8(0x04);
opts_w.put_u16b(1500);
@@ -615,7 +615,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr<IPClient> c, const FrameIn
opts_w.put_u8(0x05); // Magic number (bitwise inverse of the remote end's)
opts_w.put_u8(0x06);
opts_w.put_u32b(~c->hdlc_remote_magic_number);
StringWriter request_w;
phosg::StringWriter request_w;
request_w.put<LCPHeader>(LCPHeader{
.command = 0x01, // Configure-Request
.request_id = fi.lcp->request_id,
@@ -624,7 +624,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr<IPClient> c, const FrameIn
request_w.write(opts_w.str());
this->send_layer3_frame(c, FrameInfo::Protocol::LCP, request_w.str());
StringWriter ack_w;
phosg::StringWriter ack_w;
ack_w.put<LCPHeader>(LCPHeader{
.command = 0x02, // Configure-Ack
.request_id = fi.lcp->request_id,
@@ -679,7 +679,7 @@ void IPStackSimulator::on_client_pap_frame(shared_ptr<IPClient> c, const FrameIn
ip_stack_simulator_log.info("Client logged in with username \"%s\" and password", username.c_str());
static const string login_message = "newserv PPP simulator";
StringWriter w;
phosg::StringWriter w;
w.put<PAPHeader>(PAPHeader{
.command = 0x02, // Authenticate-Ack
.request_id = fi.pap->request_id,
@@ -698,11 +698,11 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPClient> c, const FrameI
uint32_t remote_ip = 0;
uint32_t remote_primary_dns = 0;
uint32_t remote_secondary_dns = 0;
StringWriter rejected_opts_w;
phosg::StringWriter rejected_opts_w;
while (!opts_r.eof()) {
uint8_t opt = opts_r.get_u8();
string opt_data = opts_r.read(opts_r.get_u8() - 2);
StringReader opt_data_r(opt_data);
phosg::StringReader opt_data_r(opt_data);
switch (opt) {
case 0x01: // IP addresses (deprecated as of 1992; we don't support it at all)
throw runtime_error("IPCP client sent IP-Addresses option");
@@ -723,7 +723,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPClient> c, const FrameI
case 0x82: // Primary NBNS server address
case 0x84: // Secondary NBNS server address
case 0x04: // Mobile IP address
throw runtime_error(string_printf("unimplemented IPCP option %02hhX (%zu bytes)", opt, opt_data.size()));
throw runtime_error(phosg::string_printf("unimplemented IPCP option %02hhX (%zu bytes)", opt, opt_data.size()));
default:
throw runtime_error("unknown IPCP option");
}
@@ -731,7 +731,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPClient> c, const FrameI
if (!rejected_opts_w.str().empty()) {
// Send a Configure-Reject if the client specified IP header compression
StringWriter reject_w;
phosg::StringWriter reject_w;
reject_w.put<IPCPHeader>(IPCPHeader{
.command = 0x04, // Configure-Reject
.request_id = fi.ipcp->request_id,
@@ -745,7 +745,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPClient> c, const FrameI
(remote_secondary_dns != 0x24242424)) {
// Send a Configure-Nak if the client's request doesn't exactly match
// what we want them to use.
StringWriter opts_w;
phosg::StringWriter opts_w;
opts_w.put_u8(0x03); // IP address
opts_w.put_u8(0x06);
opts_w.put_u32b(0x1E1E1E1E);
@@ -756,7 +756,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPClient> c, const FrameI
opts_w.put_u8(0x06);
opts_w.put_u32b(0x24242424);
StringWriter nak_w;
phosg::StringWriter nak_w;
nak_w.put<IPCPHeader>(IPCPHeader{
.command = 0x03, // Configure-Nak
.request_id = fi.ipcp->request_id,
@@ -770,7 +770,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPClient> c, const FrameI
// As with LCP, we technically should implement the state machine, but I
// continue to be lazy.
StringWriter opts_w;
phosg::StringWriter opts_w;
opts_w.put_u8(0x03); // IP address
opts_w.put_u8(0x06);
opts_w.put_u32b(0x39393939);
@@ -781,7 +781,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPClient> c, const FrameI
opts_w.put_u8(0x06);
opts_w.put_u32b(0x24242424);
StringWriter request_w;
phosg::StringWriter request_w;
request_w.put<IPCPHeader>(IPCPHeader{
.command = 0x01, // Configure-Request
.request_id = fi.ipcp->request_id,
@@ -790,7 +790,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPClient> c, const FrameI
request_w.write(opts_w.str());
this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, request_w.str());
StringWriter ack_w;
phosg::StringWriter ack_w;
ack_w.put<IPCPHeader>(IPCPHeader{
.command = 0x02, // Configure-Ack
.request_id = fi.ipcp->request_id,
@@ -841,7 +841,7 @@ void IPStackSimulator::on_client_arp_frame(
reinterpret_cast<const uint8_t*>(fi.payload) + 6);
}
StringWriter w;
phosg::StringWriter w;
w.put<ARPHeader>(ARPHeader{
.hardware_type = fi.arp->hardware_type,
.protocol_type = fi.arp->protocol_type,
@@ -942,7 +942,7 @@ void IPStackSimulator::on_client_udp_frame(shared_ptr<IPClient> c, const FrameIn
throw runtime_error("client sent unknown DHCP command option");
}
StringWriter w;
phosg::StringWriter w;
DHCPHeader r_dhcp;
r_dhcp.opcode = 2; // Response
r_dhcp.hardware_type = 1; // Ethernet
@@ -1027,13 +1027,13 @@ void IPStackSimulator::on_client_udp_frame(shared_ptr<IPClient> c, const FrameIn
r_udp.checksum = FrameInfo::computed_udp4_checksum(
r_ipv4, r_udp, r_data.data(), r_data.size());
if (ip_stack_simulator_log.should_log(LogLevel::DEBUG)) {
if (ip_stack_simulator_log.should_log(phosg::LogLevel::DEBUG)) {
string remote_str = this->str_for_ipv4_netloc(fi.ipv4->src_addr, fi.udp->src_port);
ip_stack_simulator_log.debug("Sending UDP response to %s", remote_str.c_str());
print_data(stderr, r_data);
phosg::print_data(stderr, r_data);
}
StringWriter w;
phosg::StringWriter w;
w.put(r_ipv4);
w.put(r_udp);
w.write(r_data);
@@ -1079,7 +1079,7 @@ void IPStackSimulator::on_client_tcp_frame(
throw runtime_error("TCP SYN contains extra flags");
}
StringReader options_r(fi.tcp + 1, fi.tcp_options_size);
phosg::StringReader options_r(fi.tcp + 1, fi.tcp_options_size);
size_t max_frame_size = 1400;
while (!options_r.eof()) {
uint8_t option = options_r.get_u8();
@@ -1135,7 +1135,7 @@ void IPStackSimulator::on_client_tcp_frame(
conn.server_port = fi.tcp->dest_port;
conn.client_port = fi.tcp->src_port;
conn.next_client_seq = fi.tcp->seq_num + 1;
conn.acked_server_seq = random_object<uint32_t>();
conn.acked_server_seq = phosg::random_object<uint32_t>();
conn.resend_push_usecs = DEFAULT_RESEND_PUSH_USECS;
conn.next_push_max_frame_size = max_frame_size;
conn.awaiting_first_ack = true;
@@ -1240,7 +1240,7 @@ void IPStackSimulator::on_client_tcp_frame(
// server immediately.
} else if (fi.payload_size != 0) {
string conn_str = ip_stack_simulator_log.should_log(LogLevel::WARNING)
string conn_str = ip_stack_simulator_log.should_log(phosg::LogLevel::WARNING)
? this->str_for_tcp_connection(c, *conn)
: "";
@@ -1285,7 +1285,7 @@ void IPStackSimulator::on_client_tcp_frame(
conn_str.c_str());
}
if (was_logged) {
print_data(stderr, payload, payload_size);
phosg::print_data(stderr, payload, payload_size);
}
// Send the new data to the server
@@ -1388,7 +1388,7 @@ void IPStackSimulator::send_pending_push_frame(
conn.acked_server_seq, bytes_to_send, pending_bytes);
this->send_tcp_frame(c, conn, TCPHeader::Flag::PSH, conn.pending_data.get(), bytes_to_send);
struct timeval resend_push_timeout = usecs_to_timeval(conn.resend_push_usecs);
struct timeval resend_push_timeout = phosg::usecs_to_timeval(conn.resend_push_usecs);
event_add(conn.resend_push_event.get(), &resend_push_timeout);
// If the client isn't responding to our PSHes, back off exponentially up to
@@ -1441,7 +1441,7 @@ void IPStackSimulator::send_tcp_frame(
const void* linear_data = src_bytes ? evbuffer_pullup(src_buf, src_bytes) : nullptr;
tcp.checksum = FrameInfo::computed_tcp4_checksum(ipv4, tcp, linear_data, src_bytes);
StringWriter w;
phosg::StringWriter w;
w.put(ipv4);
w.put(tcp);
if (src_bytes) {
@@ -1488,7 +1488,7 @@ void IPStackSimulator::on_server_input(shared_ptr<IPClient> c, IPClient::TCPConn
auto sim = c->sim.lock();
uint64_t idle_timeout_usecs = sim ? sim->state->client_idle_timeout_usecs : 60000000;
struct timeval tv = usecs_to_timeval(idle_timeout_usecs);
struct timeval tv = phosg::usecs_to_timeval(idle_timeout_usecs);
event_add(c->idle_timeout_event.get(), &tv);
evbuffer_add_buffer(conn.pending_data.get(), buf);
@@ -1534,8 +1534,7 @@ void IPStackSimulator::on_server_error(
void IPStackSimulator::log_frame(const string& data) const {
if (this->pcap_text_log_file) {
print_data(this->pcap_text_log_file, data, 0, nullptr,
PrintDataFlags::SKIP_SEPARATOR);
phosg::print_data(this->pcap_text_log_file, data, 0, nullptr, phosg::PrintDataFlags::SKIP_SEPARATOR);
fputc('\n', this->pcap_text_log_file);
fflush(this->pcap_text_log_file);
}