// SPDX-License-Identifier: Apache-2.0 // Minimal WebSocket client (RFC 6455, text frames only). Port of led-driver/main/ws_client.cpp. #include "ws_client.h" #include #include #include #include #include "esp_log.h" #include "esp_random.h" #include "lwip/netdb.h" #include "lwip/sockets.h" static const char *TAG = "ws_client"; #define WS_TIMEOUT_MS 2000 // Read until the buffer holds the end of the handshake reply. Returns the bytes read, or -1. static int read_handshake(int fd, char *buf, size_t cap) { size_t got = 0; while (got < cap - 1) { int r = recv(fd, buf + got, cap - 1 - got, 0); if (r <= 0) { return -1; } got += r; buf[got] = '\0'; if (strstr(buf, "\r\n\r\n")) { return (int)got; } } return -1; } int ws_client_connect(const char *host, const char *path) { struct addrinfo hints = {}; struct addrinfo *res = NULL; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(host, "80", &hints, &res) != 0 || !res) { ESP_LOGW(TAG, "cannot resolve %s", host); return -1; } int fd = socket(res->ai_family, res->ai_socktype, 0); if (fd < 0) { freeaddrinfo(res); return -1; } struct timeval tv = { .tv_sec = WS_TIMEOUT_MS / 1000, .tv_usec = (WS_TIMEOUT_MS % 1000) * 1000 }; setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); int one = 1; setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); if (connect(fd, res->ai_addr, res->ai_addrlen) != 0) { freeaddrinfo(res); close(fd); return -1; } freeaddrinfo(res); char req[256]; int n = snprintf(req, sizeof(req), "GET %s HTTP/1.1\r\nHost: %s\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n" "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n", path, host); if (n <= 0 || n >= (int)sizeof(req) || send(fd, req, n, 0) != n) { close(fd); return -1; } char reply[256]; if (read_handshake(fd, reply, sizeof(reply)) < 0 || strncmp(reply, "HTTP/1.1 101", 12) != 0) { ESP_LOGW(TAG, "handshake with %s failed", host); close(fd); return -1; } return fd; } // Client frames are masked (RFC 6455). The mask only has to vary, not be secret. esp_err_t ws_client_send_text(int fd, const char *data, size_t len) { if (len > 2048) { return ESP_ERR_INVALID_SIZE; } size_t hdr = len < 126 ? 2 : 4; size_t total = hdr + 4 + len; uint8_t *frame = (uint8_t *)malloc(total); if (!frame) { return ESP_ERR_NO_MEM; } uint8_t mask[4]; esp_fill_random(mask, sizeof(mask)); frame[0] = 0x81; // FIN, text if (len < 126) { frame[1] = 0x80 | (uint8_t)len; } else { frame[1] = 0x80 | 126; frame[2] = (uint8_t)(len >> 8); frame[3] = (uint8_t)len; } memcpy(&frame[hdr], mask, 4); for (size_t i = 0; i < len; i++) { frame[hdr + 4 + i] = (uint8_t)data[i] ^ mask[i & 3]; } esp_err_t err = send(fd, frame, total, 0) == (ssize_t)total ? ESP_OK : ESP_FAIL; free(frame); return err; } static bool read_n(int fd, uint8_t *dst, size_t n) { size_t got = 0; while (got < n) { int r = recv(fd, dst + got, n - got, 0); if (r <= 0) { return false; } got += r; } return true; } // Server frames are not masked. Only text frames are returned; other opcodes are skipped. int ws_client_recv_text(int fd, char *buf, size_t cap) { for (;;) { uint8_t h[2]; if (!read_n(fd, h, 2)) { return -1; } size_t len = h[1] & 0x7F; if (len == 126) { uint8_t ext[2]; if (!read_n(fd, ext, 2)) { return -1; } len = (ext[0] << 8) | ext[1]; } else if (len == 127) { return -1; } bool masked = (h[1] & 0x80) != 0; uint8_t mask[4] = {}; if (masked && !read_n(fd, mask, 4)) { return -1; } if (len + 1 > cap) { return -1; } if (!read_n(fd, (uint8_t *)buf, len)) { return -1; } for (size_t i = 0; masked && i < len; i++) { buf[i] ^= mask[i & 3]; } if ((h[0] & 0x0F) == 0x1) { buf[len] = '\0'; return (int)len; } if ((h[0] & 0x0F) == 0x8) { return -1; } } } void ws_client_close(int fd) { if (fd >= 0) { close(fd); } }