From d543f6894738ec60a5e2aeeb0d9e39082a4fbb6d Mon Sep 17 00:00:00 2001 From: hkc Date: Mon, 8 Jan 2024 16:54:53 +0300 Subject: [PATCH] Cleanup --- wsvpn.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/wsvpn.c b/wsvpn.c index 16980e6..6a3a6dc 100644 --- a/wsvpn.c +++ b/wsvpn.c @@ -106,7 +106,6 @@ static void handle_client(struct mg_connection *connection, int event_type, void if (connection->rem.port == 0) return; memset(connection->data, 0, 32); } else if (event_type == MG_EV_HTTP_MSG) { - printf("http: %p\n", connection); struct mg_http_message *http_message = (struct mg_http_message *) event_data; if (mg_http_match_uri(http_message, "/open")) { mg_ws_upgrade(connection, http_message, NULL); @@ -148,21 +147,18 @@ static void handle_client(struct mg_connection *connection, int event_type, void } } else if (event_type == MG_EV_WS_OPEN) { struct mg_http_message *http_message = (struct mg_http_message *) event_data; - printf("ws open: %p\n", connection); on_ws_connect(connection, http_message, fn_data); } else if (event_type == MG_EV_WS_MSG) { - printf("websocket: %p\n", connection); struct mg_ws_message *ws_message = (struct mg_ws_message *)event_data; on_ws_message(connection, ws_message, fn_data); } else if (event_type == MG_EV_CLOSE) { - printf("closed: %p\n", connection); if (connection->is_websocket) { on_ws_disconnect(connection, fn_data); } } } -void ws_send_error(struct mg_connection *connection, uint16_t request_id, const char *fmt, ...) { +void ws_send_error(struct client *client, uint16_t request_id, const char *fmt, ...) { static char buffer[1024]; memset(buffer, 0, 1024); @@ -181,10 +177,10 @@ void ws_send_error(struct mg_connection *connection, uint16_t request_id, const metrics.sent_bytes += 5 + text_size; metrics.sent_messages++; metrics.errors++; - mg_ws_send(connection, buffer, 5 + text_size, WEBSOCKET_OP_BINARY); + mg_ws_send(client->connection, buffer, 5 + text_size, WEBSOCKET_OP_BINARY); } -void ws_send_info(struct mg_connection *connection, const char *fmt, ...) { +void ws_send_info(struct client *client, const char *fmt, ...) { static char buffer[1024]; memset(buffer, 0, 1024); @@ -200,10 +196,10 @@ void ws_send_info(struct mg_connection *connection, const char *fmt, ...) { metrics.sent_bytes += 3 + text_size; metrics.sent_messages++; - mg_ws_send(connection, buffer, 3 + text_size, WEBSOCKET_OP_BINARY); + mg_ws_send(client->connection, buffer, 3 + text_size, WEBSOCKET_OP_BINARY); } -void ws_respond(struct mg_connection *connection, uint16_t request_id, void *data, uint32_t size) { +void ws_respond(struct client *client, uint16_t request_id, void *data, uint32_t size) { static char buffer[MAX_PACKET_SIZE]; assert(size < MAX_PACKET_SIZE); buffer[0] = 'R'; @@ -212,10 +208,12 @@ void ws_respond(struct mg_connection *connection, uint16_t request_id, void *dat if (size != 0) memcpy(&buffer[3], data, size); metrics.sent_bytes += 3 + size; metrics.sent_messages++; - mg_ws_send(connection, buffer, size + 3, WEBSOCKET_OP_BINARY); + mg_ws_send(client->connection, buffer, size + 3, WEBSOCKET_OP_BINARY); } static void on_ws_connect(struct mg_connection *connection, struct mg_http_message *message, void *data) { + (void)message; + (void)data; struct client *client = malloc(sizeof(struct client)); memcpy(&connection->data[0], &client, sizeof(struct client *)); client->connection = connection; @@ -229,8 +227,11 @@ static void on_ws_connect(struct mg_connection *connection, struct mg_http_messa } static void on_ws_message(struct mg_connection *connection, struct mg_ws_message *message, void *data) { + (void)data; + if ((message->flags & 15) != WEBSOCKET_OP_BINARY) { - ws_send_error(connection, -1, "This text could've been a binary."); + const char *err_str = "This server only works in binary mode. Sorry!"; + mg_ws_send(connection, err_str, strlen(err_str), WEBSOCKET_OP_TEXT); connection->is_draining = 1; return; } @@ -252,7 +253,7 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message { metrics.method_calls[0]++; uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]); - printf("%p[%04x] modem.open(%d)\n", client, request_id, channel); + printf("%p[%04x] modem.open(%d)\n", (void*)client, request_id, channel); modem_open(client, request_id, channel); } return; @@ -260,7 +261,7 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message { metrics.method_calls[1]++; uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]); - printf("%p[%04x] modem.isOpen(%d)\n", client, request_id, channel); + printf("%p[%04x] modem.isOpen(%d)\n", (void*)client, request_id, channel); modem_isOpen(client, request_id, channel); } return; @@ -268,14 +269,14 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message { metrics.method_calls[2]++; uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]); - printf("%p[%04x] modem.close(%d)\n", client, request_id, channel); + printf("%p[%04x] modem.close(%d)\n", (void*)client, request_id, channel); modem_close(client, request_id, channel); } return; case 'C': // closeAll { metrics.method_calls[3]++; - printf("%p[%04x] modem.closeAll()\n", client, request_id); + printf("%p[%04x] modem.closeAll()\n", (void*)client, request_id); modem_closeAll(client, request_id); } return; @@ -289,13 +290,15 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message } return; default: - ws_send_error(connection, request_id, "Unknown opcode: 0x%02x", message->data.ptr[0]); + ws_send_error(client, request_id, "Unknown opcode: 0x%02x", message->data.ptr[0]); connection->is_draining = 1; return; } } static void on_ws_disconnect(struct mg_connection *connection, void *data) { + (void)data; + struct client *client = *(struct client **)&connection->data[0]; if (client->connection == connection) { free(client); @@ -305,22 +308,22 @@ static void on_ws_disconnect(struct mg_connection *connection, void *data) { static void modem_open(struct client *client, uint16_t request_id, uint16_t channel) { if (client_is_open(client, channel)) { - ws_respond(client->connection, request_id, NULL, 0); + ws_respond(client, request_id, NULL, 0); } if (client->next_open_channel_index == MAX_OPEN_CHANNELS) { - ws_send_error(client->connection, request_id, "Too many open channels"); + ws_send_error(client, request_id, "Too many open channels"); return; } client->open_channels[client->next_open_channel_index] = channel; client->next_open_channel_index++; - ws_respond(client->connection, request_id, NULL, 0); + ws_respond(client, request_id, NULL, 0); } static void modem_isOpen(struct client *client, uint16_t request_id, uint16_t channel) { unsigned char is_open = client_is_open(client, channel) ? 42 : 0; - ws_respond(client->connection, request_id, &is_open, 1); + ws_respond(client, request_id, &is_open, 1); } static void modem_close(struct client *client, uint16_t request_id, uint16_t channel) { @@ -331,20 +334,20 @@ static void modem_close(struct client *client, uint16_t request_id, uint16_t cha break; } } - ws_respond(client->connection, request_id, NULL, 0); + ws_respond(client, request_id, NULL, 0); } static void modem_closeAll(struct client *client, uint16_t request_id) { client->next_open_channel_index = 0; memset(client->open_channels, 0, sizeof(uint16_t) * MAX_OPEN_CHANNELS); - ws_respond(client->connection, request_id, NULL, 0); + ws_respond(client, request_id, NULL, 0); } static void modem_transmit(struct client *client, uint16_t request_id, uint16_t channel, uint16_t reply_channel, void *data, uint16_t size) { static uint8_t buffer[MAX_PACKET_SIZE + 7]; if (size > MAX_PACKET_SIZE) { - ws_send_error(client->connection, request_id, "Packet too big: %d > %d", size, MAX_PACKET_SIZE); + ws_send_error(client, request_id, "Packet too big: %d > %d", size, MAX_PACKET_SIZE); return; } @@ -369,7 +372,7 @@ static void modem_transmit(struct client *client, uint16_t request_id, uint16_t } } } - ws_respond(client->connection, request_id, NULL, 0); + ws_respond(client, request_id, NULL, 0); } bool client_is_open(struct client *client, uint16_t channel) {