ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
protocol.cpp
Go to the documentation of this file.
1#include <emscripten/bind.h>
2#include "client_bytes.h"
3extern "C" {
4#include "../client.h"
7#include <ascii-chat/common.h>
9}
10#include <sodium.h>
11#include <cstdint>
12#include <optional>
13#include <string>
14#include <vector>
15
16static std::optional<std::string> parse_packet(ClientBytes packet) {
17 auto input = binding_copy_input(packet);
18 if (input.size() < sizeof(packet_header_t)) {
19 SET_ERRNO(ERROR_INVALID_PARAM, "Client packet is shorter than its header");
20 return std::nullopt;
21 }
22 packet_header_t header;
23 if (packet_validate(input.data(), input.size(), 0, &header) != ASCIICHAT_OK)
24 return std::nullopt;
25 emscripten::val result = emscripten::val::object();
26 result.set("type", NET_TO_HOST_U16(header.type));
27 result.set("length", NET_TO_HOST_U32(header.length));
28 result.set("client_id", NET_TO_HOST_U32(header.client_id));
29 result.set("crc32", NET_TO_HOST_U32(header.crc32));
30 return emscripten::val::global("JSON").call<std::string>("stringify", result);
31}
32static ClientPacketResult serialize_packet(unsigned type, ClientBytes payload, unsigned client_id) {
33 if (type > UINT16_MAX) {
34 const int code = SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client packet type: %u", type);
35 return packet_result(code, nullptr, 0);
36 }
37 auto input = binding_copy_input(payload);
38 std::vector<uint8_t> output(input.size() + 256);
39 size_t length = 0;
40 const int code = packet_encode(static_cast<packet_type_t>(type), input.empty() ? nullptr : input.data(), input.size(), client_id, output.data(), output.size(), &length);
41 if (code)
42 SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Cannot serialize client packet");
43 return packet_result(code, output.data(), code == 0 ? length : 0);
44}
45static int handle_handshake(int stage, ClientBytes packet) {
46 auto bytes = binding_copy_input(packet);
47 if (stage >= 0 && stage <= 3 && bytes.size() < sizeof(packet_header_t)) {
48 if (!bytes.empty())
49 sodium_memzero(bytes.data(), bytes.size());
50 return SET_ERRNO(ERROR_INVALID_PARAM, "Client handshake packet is shorter than its header");
51 }
52 int result = -1;
53 switch (stage) {
54 case 0:
55 result = client_handle_crypto_parameters(bytes.data(), bytes.size());
56 break;
57 case 1:
58 result = client_handle_key_exchange_init(bytes.data(), bytes.size());
59 break;
60 case 2:
61 result = client_handle_auth_challenge(bytes.data(), bytes.size());
62 break;
63 case 3:
64 result = client_handle_handshake_complete(bytes.data(), bytes.size());
65 break;
66 default:
67 result = SET_ERRNO(ERROR_INVALID_PARAM, "Invalid handshake stage: %d", stage);
68 break;
69 }
70 if (!bytes.empty())
71 sodium_memzero(bytes.data(), bytes.size());
72 if (result && stage >= 0 && stage <= 3)
73 SET_ERRNO(ERROR_CRYPTO_HANDSHAKE, "Client handshake stage %d failed", stage);
74 return result;
75}
76static int receive_handshake(bool discovery, unsigned type, ClientBytes payload) {
77 auto bytes = binding_copy_input(payload);
78 uint8_t empty = 0;
79 const void *data = bytes.empty() ? &empty : bytes.data();
80 const int result = discovery ? client_acds_handshake_receive(type, data, bytes.size()) : client_handshake_receive(type, data, bytes.size());
81 if (!bytes.empty())
82 sodium_memzero(bytes.data(), bytes.size());
83 return result;
84}
85static ClientPacketResult crypt_packet(bool encrypt, ClientBytes packet) {
86 auto input = binding_copy_input(packet);
87 std::vector<uint8_t> output(input.size() + (encrypt ? 72 : 0));
88 size_t length = 0;
89 const int code =
90 encrypt ? client_encrypt_packet(input.data(), input.size(), output.data(), output.size(), &length) : client_decrypt_packet(input.data(), input.size(), output.data(), output.size(), &length);
91 ClientPacketResult result = packet_result(code, output.data(), code == 0 ? length : 0);
92 if (!input.empty())
93 sodium_memzero(input.data(), input.size());
94 if (!output.empty())
95 sodium_memzero(output.data(), output.size());
96 return result;
97}
98
99EMSCRIPTEN_BINDINGS(ascii_chat_client_protocol) {
100 emscripten::register_type<ClientBytes>("ClientBytes", "Uint8Array");
101 emscripten::register_type<ClientPacketResult>("ClientPacketResult", "{ code: number; bytes: Uint8Array }");
102 emscripten::function("handleHandshake", &handle_handshake);
103 emscripten::function("receiveHandshake", &receive_handshake);
104 emscripten::function("cryptPacket", &crypt_packet);
105
106 emscripten::function("parsePacketJson", &parse_packet);
107 emscripten::function("serializePacket", &serialize_packet);
108}
#define SET_ERRNO(code,...)
std::vector< uint8_t > binding_copy_input(emscripten::val bytes)
Definition byte_arrays.h:6
ClientPacketResult packet_result(int code, const uint8_t *data, size_t length)
🔄 Network byte order conversion helpers
#define NET_TO_HOST_U16(val)
Definition endian.h:115
#define NET_TO_HOST_U32(val)
Definition endian.h:85
unsigned char uint8_t
@ ERROR_CRYPTO_HANDSHAKE
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
asciichat_error_t packet_validate(const void *packet, size_t length, size_t limit, packet_header_t *output)
Definition packet.c:81
asciichat_error_t packet_encode(packet_type_t type, const void *payload, size_t length, uint32_t client_id, void *output, size_t capacity, size_t *written)
Definition packet.c:63
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Network error handling utilities.
EMSCRIPTEN_BINDINGS(ascii_chat_client_protocol)
Definition protocol.cpp:99
asciichat_error_t client_handshake_receive(uint32_t type, const void *payload, size_t length)
int client_handle_handshake_complete(const uint8_t *packet, size_t packet_len)
int client_handle_auth_challenge(const uint8_t *packet, size_t packet_len)
asciichat_error_t client_acds_handshake_receive(uint32_t type, const void *payload, size_t length)
int client_decrypt_packet(const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t plaintext_size, size_t *out_len)
int client_encrypt_packet(const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext, size_t ciphertext_size, size_t *out_len)
int client_handle_crypto_parameters(const uint8_t *packet, size_t packet_len)
int client_handle_key_exchange_init(const uint8_t *packet, size_t packet_len)
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641