diff --git a/other/bootstrap_node_packets.c b/other/bootstrap_node_packets.c index 2ca711a32b..55908c136e 100644 --- a/other/bootstrap_node_packets.c +++ b/other/bootstrap_node_packets.c @@ -21,7 +21,7 @@ static uint8_t bootstrap_motd[MAX_MOTD_LENGTH]; static uint16_t bootstrap_motd_length; /* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH - * with the first byte being BOOTSTRAP_INFO_PACKET_ID + * with the first byte being NET_PACKET_BOOTSTRAP_INFO */ static int handle_info_request(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata) @@ -33,7 +33,7 @@ static int handle_info_request(void *object, const IP_Port *source, const uint8_ const Networking_Core *nc = (const Networking_Core *)object; uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH]; - data[0] = BOOTSTRAP_INFO_PACKET_ID; + data[0] = NET_PACKET_BOOTSTRAP_INFO; memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version)); const uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length; memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length); @@ -55,6 +55,6 @@ int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, const uint8_ memcpy(bootstrap_motd, motd, motd_length); bootstrap_motd_length = motd_length; - networking_registerhandler(net, BOOTSTRAP_INFO_PACKET_ID, &handle_info_request, net); + networking_registerhandler(net, NET_PACKET_BOOTSTRAP_INFO, &handle_info_request, net); return 0; } diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel index b8c1a2ba54..4f6f6af431 100644 --- a/testing/BUILD.bazel +++ b/testing/BUILD.bazel @@ -14,7 +14,6 @@ sh_test( args = ["$(locations %s)" % f for f in CIMPLE_FILES] + [ "-Wno-boolean-return", "-Wno-callback-names", - "-Wno-enum-from-int", "+RTS", "-N4", "-RTS", diff --git a/toxcore/network.c b/toxcore/network.c index d2e8165571..e678f63c58 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -741,170 +741,373 @@ static int net_getsockopt(const Network *ns, Socket sock, int level, int optname } non_null() -static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) -{ - uint32_t data = 0; - - if (buflen > 4) { - net_unpack_u32(buffer + 1, &data); - } - - return data; -} -non_null() -static uint32_t data_1(uint16_t buflen, const uint8_t *buffer) -{ - uint32_t data = 0; - - if (buflen > 8) { - net_unpack_u32(buffer + 5, &data); +static bool net_packet_type_from_int(uint8_t value, Net_Packet_Type *out_enum) +{ + // Clang compiles these to jump tables. Not amazing, but not too terrible. + // It's necessary because the enum values are not contiguous and casting + // the int directly to the enum is UB. + switch (value) { + case NET_PACKET_PING_REQUEST: { + *out_enum = NET_PACKET_PING_REQUEST; + return true; + } + case NET_PACKET_PING_RESPONSE: { + *out_enum = NET_PACKET_PING_RESPONSE; + return true; + } + case NET_PACKET_GET_NODES: { + *out_enum = NET_PACKET_GET_NODES; + return true; + } + case NET_PACKET_SEND_NODES_IPV6: { + *out_enum = NET_PACKET_SEND_NODES_IPV6; + return true; + } + case NET_PACKET_COOKIE_REQUEST: { + *out_enum = NET_PACKET_COOKIE_REQUEST; + return true; + } + case NET_PACKET_COOKIE_RESPONSE: { + *out_enum = NET_PACKET_COOKIE_RESPONSE; + return true; + } + case NET_PACKET_CRYPTO_HS: { + *out_enum = NET_PACKET_CRYPTO_HS; + return true; + } + case NET_PACKET_CRYPTO_DATA: { + *out_enum = NET_PACKET_CRYPTO_DATA; + return true; + } + case NET_PACKET_CRYPTO: { + *out_enum = NET_PACKET_CRYPTO; + return true; + } + case NET_PACKET_LAN_DISCOVERY: { + *out_enum = NET_PACKET_LAN_DISCOVERY; + return true; + } + case NET_PACKET_GC_HANDSHAKE: { + *out_enum = NET_PACKET_GC_HANDSHAKE; + return true; + } + case NET_PACKET_GC_LOSSLESS: { + *out_enum = NET_PACKET_GC_LOSSLESS; + return true; + } + case NET_PACKET_GC_LOSSY: { + *out_enum = NET_PACKET_GC_LOSSY; + return true; + } + case NET_PACKET_ONION_SEND_INITIAL: { + *out_enum = NET_PACKET_ONION_SEND_INITIAL; + return true; + } + case NET_PACKET_ONION_SEND_1: { + *out_enum = NET_PACKET_ONION_SEND_1; + return true; + } + case NET_PACKET_ONION_SEND_2: { + *out_enum = NET_PACKET_ONION_SEND_2; + return true; + } + case NET_PACKET_ANNOUNCE_REQUEST_OLD: { + *out_enum = NET_PACKET_ANNOUNCE_REQUEST_OLD; + return true; + } + case NET_PACKET_ANNOUNCE_RESPONSE_OLD: { + *out_enum = NET_PACKET_ANNOUNCE_RESPONSE_OLD; + return true; + } + case NET_PACKET_ONION_DATA_REQUEST: { + *out_enum = NET_PACKET_ONION_DATA_REQUEST; + return true; + } + case NET_PACKET_ONION_DATA_RESPONSE: { + *out_enum = NET_PACKET_ONION_DATA_RESPONSE; + return true; + } + case NET_PACKET_ANNOUNCE_REQUEST: { + *out_enum = NET_PACKET_ANNOUNCE_REQUEST; + return true; + } + case NET_PACKET_ANNOUNCE_RESPONSE: { + *out_enum = NET_PACKET_ANNOUNCE_RESPONSE; + return true; + } + case NET_PACKET_ONION_RECV_3: { + *out_enum = NET_PACKET_ONION_RECV_3; + return true; + } + case NET_PACKET_ONION_RECV_2: { + *out_enum = NET_PACKET_ONION_RECV_2; + return true; + } + case NET_PACKET_ONION_RECV_1: { + *out_enum = NET_PACKET_ONION_RECV_1; + return true; + } + case NET_PACKET_FORWARD_REQUEST: { + *out_enum = NET_PACKET_FORWARD_REQUEST; + return true; + } + case NET_PACKET_FORWARDING: { + *out_enum = NET_PACKET_FORWARDING; + return true; + } + case NET_PACKET_FORWARD_REPLY: { + *out_enum = NET_PACKET_FORWARD_REPLY; + return true; + } + case NET_PACKET_DATA_SEARCH_REQUEST: { + *out_enum = NET_PACKET_DATA_SEARCH_REQUEST; + return true; + } + case NET_PACKET_DATA_SEARCH_RESPONSE: { + *out_enum = NET_PACKET_DATA_SEARCH_RESPONSE; + return true; + } + case NET_PACKET_DATA_RETRIEVE_REQUEST: { + *out_enum = NET_PACKET_DATA_RETRIEVE_REQUEST; + return true; + } + case NET_PACKET_DATA_RETRIEVE_RESPONSE: { + *out_enum = NET_PACKET_DATA_RETRIEVE_RESPONSE; + return true; + } + case NET_PACKET_STORE_ANNOUNCE_REQUEST: { + *out_enum = NET_PACKET_STORE_ANNOUNCE_REQUEST; + return true; + } + case NET_PACKET_STORE_ANNOUNCE_RESPONSE: { + *out_enum = NET_PACKET_STORE_ANNOUNCE_RESPONSE; + return true; + } + case NET_PACKET_BOOTSTRAP_INFO: { + *out_enum = NET_PACKET_BOOTSTRAP_INFO; + return true; + } + case NET_PACKET_MAX: { + *out_enum = NET_PACKET_MAX; + return true; + } + default: { + *out_enum = NET_PACKET_PING_REQUEST; + return false; + } } - - return data; } -static const char *net_packet_type_name(Net_Packet_Type type) +static const char *net_packet_type_to_string(Net_Packet_Type type) { switch (type) { case NET_PACKET_PING_REQUEST: - return "PING_REQUEST"; + return "NET_PACKET_PING_REQUEST"; case NET_PACKET_PING_RESPONSE: - return "PING_RESPONSE"; + return "NET_PACKET_PING_RESPONSE"; case NET_PACKET_GET_NODES: - return "GET_NODES"; + return "NET_PACKET_GET_NODES"; case NET_PACKET_SEND_NODES_IPV6: - return "SEND_NODES_IPV6"; + return "NET_PACKET_SEND_NODES_IPV6"; case NET_PACKET_COOKIE_REQUEST: - return "COOKIE_REQUEST"; + return "NET_PACKET_COOKIE_REQUEST"; case NET_PACKET_COOKIE_RESPONSE: - return "COOKIE_RESPONSE"; + return "NET_PACKET_COOKIE_RESPONSE"; case NET_PACKET_CRYPTO_HS: - return "CRYPTO_HS"; + return "NET_PACKET_CRYPTO_HS"; case NET_PACKET_CRYPTO_DATA: - return "CRYPTO_DATA"; + return "NET_PACKET_CRYPTO_DATA"; case NET_PACKET_CRYPTO: - return "CRYPTO"; + return "NET_PACKET_CRYPTO"; + + case NET_PACKET_LAN_DISCOVERY: + return "NET_PACKET_LAN_DISCOVERY"; case NET_PACKET_GC_HANDSHAKE: - return "GC_HANDSHAKE"; + return "NET_PACKET_GC_HANDSHAKE"; case NET_PACKET_GC_LOSSLESS: - return "GC_LOSSLESS"; + return "NET_PACKET_GC_LOSSLESS"; case NET_PACKET_GC_LOSSY: - return "GC_LOSSY"; - - case NET_PACKET_LAN_DISCOVERY: - return "LAN_DISCOVERY"; + return "NET_PACKET_GC_LOSSY"; case NET_PACKET_ONION_SEND_INITIAL: - return "ONION_SEND_INITIAL"; + return "NET_PACKET_ONION_SEND_INITIAL"; case NET_PACKET_ONION_SEND_1: - return "ONION_SEND_1"; + return "NET_PACKET_ONION_SEND_1"; case NET_PACKET_ONION_SEND_2: - return "ONION_SEND_2"; + return "NET_PACKET_ONION_SEND_2"; case NET_PACKET_ANNOUNCE_REQUEST_OLD: - return "ANNOUNCE_REQUEST_OLD"; + return "NET_PACKET_ANNOUNCE_REQUEST_OLD"; case NET_PACKET_ANNOUNCE_RESPONSE_OLD: - return "ANNOUNCE_RESPONSE_OLD"; + return "NET_PACKET_ANNOUNCE_RESPONSE_OLD"; case NET_PACKET_ONION_DATA_REQUEST: - return "ONION_DATA_REQUEST"; + return "NET_PACKET_ONION_DATA_REQUEST"; case NET_PACKET_ONION_DATA_RESPONSE: - return "ONION_DATA_RESPONSE"; + return "NET_PACKET_ONION_DATA_RESPONSE"; case NET_PACKET_ANNOUNCE_REQUEST: - return "ANNOUNCE_REQUEST"; + return "NET_PACKET_ANNOUNCE_REQUEST"; case NET_PACKET_ANNOUNCE_RESPONSE: - return "ANNOUNCE_RESPONSE"; + return "NET_PACKET_ANNOUNCE_RESPONSE"; case NET_PACKET_ONION_RECV_3: - return "ONION_RECV_3"; + return "NET_PACKET_ONION_RECV_3"; case NET_PACKET_ONION_RECV_2: - return "ONION_RECV_2"; + return "NET_PACKET_ONION_RECV_2"; case NET_PACKET_ONION_RECV_1: - return "ONION_RECV_1"; + return "NET_PACKET_ONION_RECV_1"; case NET_PACKET_FORWARD_REQUEST: - return "FORWARD_REQUEST"; + return "NET_PACKET_FORWARD_REQUEST"; case NET_PACKET_FORWARDING: - return "FORWARDING"; + return "NET_PACKET_FORWARDING"; case NET_PACKET_FORWARD_REPLY: - return "FORWARD_REPLY"; + return "NET_PACKET_FORWARD_REPLY"; case NET_PACKET_DATA_SEARCH_REQUEST: - return "DATA_SEARCH_REQUEST"; + return "NET_PACKET_DATA_SEARCH_REQUEST"; case NET_PACKET_DATA_SEARCH_RESPONSE: - return "DATA_SEARCH_RESPONSE"; + return "NET_PACKET_DATA_SEARCH_RESPONSE"; case NET_PACKET_DATA_RETRIEVE_REQUEST: - return "DATA_RETRIEVE_REQUEST"; + return "NET_PACKET_DATA_RETRIEVE_REQUEST"; case NET_PACKET_DATA_RETRIEVE_RESPONSE: - return "DATA_RETRIEVE_RESPONSE"; + return "NET_PACKET_DATA_RETRIEVE_RESPONSE"; case NET_PACKET_STORE_ANNOUNCE_REQUEST: - return "STORE_ANNOUNCE_REQUEST"; + return "NET_PACKET_STORE_ANNOUNCE_REQUEST"; case NET_PACKET_STORE_ANNOUNCE_RESPONSE: - return "STORE_ANNOUNCE_RESPONSE"; + return "NET_PACKET_STORE_ANNOUNCE_RESPONSE"; - case BOOTSTRAP_INFO_PACKET_ID: - return "BOOTSTRAP_INFO"; + case NET_PACKET_BOOTSTRAP_INFO: + return "NET_PACKET_BOOTSTRAP_INFO"; case NET_PACKET_MAX: - return "MAX"; + return "NET_PACKET_MAX"; } - return ""; + return ""; +} + +typedef enum Net_Direction { + NET_DIRECTION_TCP_INCOMING, + NET_DIRECTION_TCP_OUTGOING, + NET_DIRECTION_UDP_INCOMING, + NET_DIRECTION_UDP_OUTGOING, +} Net_Direction; + +static const char *net_direction_str(Net_Direction direction) +{ + switch (direction) { + case NET_DIRECTION_TCP_INCOMING: + return "=>T"; + case NET_DIRECTION_TCP_OUTGOING: + return "T=>"; + case NET_DIRECTION_UDP_INCOMING: + return "=>O"; + case NET_DIRECTION_UDP_OUTGOING: + return "O=>"; + } + + return "???"; } non_null() -static void loglogdata(const Logger *log, const char *message, const uint8_t *buffer, - uint16_t buflen, const IP_Port *ip_port, long res) +static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) { - if (res < 0) { /* Windows doesn't necessarily know `%zu` */ - Ip_Ntoa ip_str; + uint32_t data = 0; + + if (buflen > 4) { + net_unpack_u32(buffer + 1, &data); + } + + return data; +} +non_null() +static uint32_t data_1(uint16_t buflen, const uint8_t *buffer) +{ + uint32_t data = 0; + + if (buflen > 8) { + net_unpack_u32(buffer + 5, &data); + } + + return data; +} + +non_null() +static void loglogdata(const Logger *log, Net_Direction direction, const uint8_t *buffer, + uint16_t buflen, const IP_Port *ip_port, int res) +{ + assert(buflen >= 1); + const char *message = net_direction_str(direction); + + const uint8_t first_byte = buffer[0]; + const uint8_t last_byte = buffer[buflen - 1]; + Net_Packet_Type packet_type; + if (!net_packet_type_from_int(first_byte, &packet_type)) { + // Pick something invalid. + packet_type = NET_PACKET_MAX; + } + const char *packet_type_name = strip_prefix("NET_PACKET_", net_packet_type_to_string(packet_type)); + + Ip_Ntoa ip_str; + net_ip_ntoa(&ip_port->ip, &ip_str); + + const uint16_t port = net_ntohs(ip_port->port); + + // These cases all look *very* similar, but it seems not worth actually + // unifying them, so we've made them look as similar as possible. + if (res < 0) { const int error = net_error(); - Net_Strerror error_str; + Net_Strerror error_buf; + const char *error_str = net_strerror(error, &error_buf); + const char res_char = 'E'; + const uint16_t sent_count = min_u16(buflen, 999); LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x", - buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, - min_u16(buflen, 999), 'E', - net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, - net_strerror(error, &error_str), data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); + first_byte, packet_type_name, message, sent_count, res_char, + ip_str.buf, port, error, error_str, data_0(buflen, buffer), data_1(buflen, buffer), last_byte); } else if ((res > 0) && ((size_t)res <= buflen)) { - Ip_Ntoa ip_str; + const int error = 0; + const char *error_str = "OK"; + const char res_char = (size_t)res < buflen ? '<' : '='; + const uint16_t sent_count = min_u16(res, 999); LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x", - buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, - min_u16(res, 999), (size_t)res < buflen ? '<' : '=', - net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK", - data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); + first_byte, packet_type_name, message, sent_count, res_char, + ip_str.buf, port, error, error_str, data_0(buflen, buffer), data_1(buflen, buffer), last_byte); } else { /* empty or overwrite */ - Ip_Ntoa ip_str; - LOGGER_TRACE(log, "[%02x = %-21s] %s %lu%c%u %s:%u (%u: %s) | %08x%08x...%02x", - buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, - res, res == 0 ? '!' : '>', buflen, - net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK", - data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); + const int error = 0; + const char *error_str = "OK"; + const char res_char = res == 0 ? '!' : '>'; + assert(res <= UINT16_MAX); + const uint16_t sent_count = (uint16_t)res; + LOGGER_TRACE(log, "[%02x = %-21s] %s %u%c%u %s:%u (%u: %s) | %08x%08x...%02x", + first_byte, packet_type_name, message, sent_count, res_char, buflen, + ip_str.buf, port, error, error_str, data_0(buflen, buffer), data_1(buflen, buffer), last_byte); } } @@ -917,7 +1120,7 @@ int net_send(const Network *ns, const Logger *log, netprof_record_packet(net_profile, buf[0], res, PACKET_DIRECTION_SEND); } - loglogdata(log, "T=>", buf, len, ip_port, res); + loglogdata(log, NET_DIRECTION_TCP_OUTGOING, buf, len, ip_port, res); return res; } @@ -933,7 +1136,7 @@ int net_recv(const Network *ns, const Logger *log, Socket sock, uint8_t *buf, size_t len, const IP_Port *ip_port) { const int res = ns->funcs->recv(ns->obj, sock, buf, len); - loglogdata(log, "=>T", buf, len, ip_port, res); + loglogdata(log, NET_DIRECTION_TCP_INCOMING, buf, len, ip_port, res); return res; } @@ -1037,8 +1240,10 @@ uint16_t net_port(const Networking_Core *net) /* Basic network functions: */ -int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packet) +int send_packet(const Networking_Core *net, const IP_Port *ip_port, const Packet packet) { + assert(packet.data != nullptr); + IP_Port ipp_copy = *ip_port; if (net_family_is_unspec(ip_port->ip.family)) { @@ -1103,12 +1308,12 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe return -1; } - const long res = net_sendto(net->ns, net->sock, packet.data, packet.length, &addr, &ipp_copy); - loglogdata(net->log, "O=>", packet.data, packet.length, ip_port, res); + const int res = net_sendto(net->ns, net->sock, packet.data, packet.length, &addr, &ipp_copy); + loglogdata(net->log, NET_DIRECTION_UDP_OUTGOING, packet.data, packet.length, ip_port, res); assert(res <= INT_MAX); - if (res == packet.length && packet.data != nullptr) { + if (res == packet.length) { netprof_record_packet(net->udp_net_profile, packet.data[0], packet.length, PACKET_DIRECTION_SEND); } @@ -1188,7 +1393,7 @@ static int receivepacket(const Network *ns, const Logger *log, Socket sock, IP_P return -1; } - loglogdata(log, "=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length); + loglogdata(log, NET_DIRECTION_UDP_INCOMING, data, MAX_UDP_PACKET_SIZE, ip_port, *length); return 0; } @@ -2365,7 +2570,7 @@ int net_error(void) char *net_strerror(int error, Net_Strerror *buf) { FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, - error, 0, buf->data, NET_STRERROR_SIZE, nullptr); + error, 0, buf->data, sizeof(buf->data), nullptr); return buf->data; } #else @@ -2398,9 +2603,9 @@ char *net_strerror(int error, Net_Strerror *buf) { errno = 0; - const char *retstr = net_strerror_r(error, buf->data, NET_STRERROR_SIZE); + const char *retstr = net_strerror_r(error, buf->data, sizeof(buf->data)); const size_t retstr_len = strlen(retstr); - assert(retstr_len < NET_STRERROR_SIZE); + assert(retstr_len < sizeof(buf->data)); buf->size = (uint16_t)retstr_len; return buf->data; diff --git a/toxcore/network.h b/toxcore/network.h index 2bab9f2935..b9df6c7b71 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -111,41 +111,41 @@ Family net_family_tox_tcp_ipv6(void); #define MAX_UDP_PACKET_SIZE 2048 typedef enum Net_Packet_Type { - NET_PACKET_PING_REQUEST = 0x00, /* Ping request packet ID. */ - NET_PACKET_PING_RESPONSE = 0x01, /* Ping response packet ID. */ - NET_PACKET_GET_NODES = 0x02, /* Get nodes request packet ID. */ - NET_PACKET_SEND_NODES_IPV6 = 0x04, /* Send nodes response packet ID for other addresses. */ - NET_PACKET_COOKIE_REQUEST = 0x18, /* Cookie request packet */ - NET_PACKET_COOKIE_RESPONSE = 0x19, /* Cookie response packet */ - NET_PACKET_CRYPTO_HS = 0x1a, /* Crypto handshake packet */ - NET_PACKET_CRYPTO_DATA = 0x1b, /* Crypto data packet */ - NET_PACKET_CRYPTO = 0x20, /* Encrypted data packet ID. */ - NET_PACKET_LAN_DISCOVERY = 0x21, /* LAN discovery packet ID. */ - - NET_PACKET_GC_HANDSHAKE = 0x5a, /* Group chat handshake packet ID */ - NET_PACKET_GC_LOSSLESS = 0x5b, /* Group chat lossless packet ID */ - NET_PACKET_GC_LOSSY = 0x5c, /* Group chat lossy packet ID */ + NET_PACKET_PING_REQUEST = 0x00, /* Ping request packet ID. */ + NET_PACKET_PING_RESPONSE = 0x01, /* Ping response packet ID. */ + NET_PACKET_GET_NODES = 0x02, /* Get nodes request packet ID. */ + NET_PACKET_SEND_NODES_IPV6 = 0x04, /* Send nodes response packet ID for other addresses. */ + NET_PACKET_COOKIE_REQUEST = 0x18, /* Cookie request packet */ + NET_PACKET_COOKIE_RESPONSE = 0x19, /* Cookie response packet */ + NET_PACKET_CRYPTO_HS = 0x1a, /* Crypto handshake packet */ + NET_PACKET_CRYPTO_DATA = 0x1b, /* Crypto data packet */ + NET_PACKET_CRYPTO = 0x20, /* Encrypted data packet ID. */ + NET_PACKET_LAN_DISCOVERY = 0x21, /* LAN discovery packet ID. */ + + NET_PACKET_GC_HANDSHAKE = 0x5a, /* Group chat handshake packet ID */ + NET_PACKET_GC_LOSSLESS = 0x5b, /* Group chat lossless packet ID */ + NET_PACKET_GC_LOSSY = 0x5c, /* Group chat lossy packet ID */ /* See: `docs/Prevent_Tracking.txt` and `onion.{c,h}` */ - NET_PACKET_ONION_SEND_INITIAL = 0x80, - NET_PACKET_ONION_SEND_1 = 0x81, - NET_PACKET_ONION_SEND_2 = 0x82, + NET_PACKET_ONION_SEND_INITIAL = 0x80, + NET_PACKET_ONION_SEND_1 = 0x81, + NET_PACKET_ONION_SEND_2 = 0x82, - NET_PACKET_ANNOUNCE_REQUEST_OLD = 0x83, /* TODO: DEPRECATE */ - NET_PACKET_ANNOUNCE_RESPONSE_OLD = 0x84, /* TODO: DEPRECATE */ + NET_PACKET_ANNOUNCE_REQUEST_OLD = 0x83, /* TODO: DEPRECATE */ + NET_PACKET_ANNOUNCE_RESPONSE_OLD = 0x84, /* TODO: DEPRECATE */ - NET_PACKET_ONION_DATA_REQUEST = 0x85, - NET_PACKET_ONION_DATA_RESPONSE = 0x86, - NET_PACKET_ANNOUNCE_REQUEST = 0x87, - NET_PACKET_ANNOUNCE_RESPONSE = 0x88, + NET_PACKET_ONION_DATA_REQUEST = 0x85, + NET_PACKET_ONION_DATA_RESPONSE = 0x86, + NET_PACKET_ANNOUNCE_REQUEST = 0x87, + NET_PACKET_ANNOUNCE_RESPONSE = 0x88, - NET_PACKET_ONION_RECV_3 = 0x8c, - NET_PACKET_ONION_RECV_2 = 0x8d, - NET_PACKET_ONION_RECV_1 = 0x8e, + NET_PACKET_ONION_RECV_3 = 0x8c, + NET_PACKET_ONION_RECV_2 = 0x8d, + NET_PACKET_ONION_RECV_1 = 0x8e, - NET_PACKET_FORWARD_REQUEST = 0x90, - NET_PACKET_FORWARDING = 0x91, - NET_PACKET_FORWARD_REPLY = 0x92, + NET_PACKET_FORWARD_REQUEST = 0x90, + NET_PACKET_FORWARDING = 0x91, + NET_PACKET_FORWARD_REPLY = 0x92, NET_PACKET_DATA_SEARCH_REQUEST = 0x93, NET_PACKET_DATA_SEARCH_RESPONSE = 0x94, @@ -154,9 +154,9 @@ typedef enum Net_Packet_Type { NET_PACKET_STORE_ANNOUNCE_REQUEST = 0x97, NET_PACKET_STORE_ANNOUNCE_RESPONSE = 0x98, - BOOTSTRAP_INFO_PACKET_ID = 0xf0, /* Only used for bootstrap nodes */ + NET_PACKET_BOOTSTRAP_INFO = 0xf0, /* Only used for bootstrap nodes */ - NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */ + NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */ } Net_Packet_Type; #define TOX_PORTRANGE_FROM 33445 diff --git a/toxcore/util.c b/toxcore/util.c index 455e513cef..9ee03628f5 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -104,6 +104,27 @@ void memzero(uint8_t *data, size_t data_size) memset(data, 0, data_size); } +/** + * @brief Strips a prefix from a string. + * + * @param prefix The prefix to strip. + * @param str The string to strip the prefix from. + * + * @return A pointer to the first character after the prefix in `str`, or the + * original `str` if the prefix is not found. + */ +const char *strip_prefix(const char *prefix, const char *str) +{ + const char *p = str; + + while (*prefix != '\0' && *prefix == *p) { + ++prefix; + ++p; + } + + return *prefix == '\0' ? p : str; +} + int16_t max_s16(int16_t a, int16_t b) { return a > b ? a : b; diff --git a/toxcore/util.h b/toxcore/util.h index 9be721318b..adc4e5b5e2 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -58,6 +58,17 @@ non_null(1) nullable(2) uint8_t *memdup(const Memory *mem, const uint8_t *data, */ nullable(1) void memzero(uint8_t *data, size_t data_size); +/** + * @brief Strips a prefix from a string. + * + * @param prefix The prefix to strip. + * @param str The string to strip the prefix from. + * + * @return A pointer to the first character after the prefix in `str`, or the + * original `str` if the prefix is not found. + */ +non_null() const char *strip_prefix(const char *prefix, const char *str); + // Safe min/max functions with specific types. This forces the conversion to the // desired type before the comparison expression, giving the choice of // conversion to the caller. Use these instead of inline comparisons or MIN/MAX diff --git a/toxcore/util_test.cc b/toxcore/util_test.cc index 0a80ff5b28..54a26516b1 100644 --- a/toxcore/util_test.cc +++ b/toxcore/util_test.cc @@ -20,4 +20,18 @@ TEST(Cmp, OrdersNumbersCorrectly) EXPECT_EQ(cmp_uint(UINT64_MAX, UINT64_MAX), 0); } +TEST(StripPrefix, StripsPrefix) +{ + const char *prefix = "prefix"; + const char *str = "prefixsuffix"; + EXPECT_STREQ(strip_prefix(prefix, str), str + strlen(prefix)); +} + +TEST(StripPrefix, ReturnsOriginalStringIfPrefixNotFound) +{ + const char *prefix = "prefix"; + const char *str = "suffix"; + EXPECT_STREQ(strip_prefix(prefix, str), str); +} + } // namespace