int32_t krad_interweb_stream_in_client_handle(kr_iws_client_t *client) { printk("fake reading stream in %zu bytes", client->in->len); kr_io2_pulled(client->in, client->in->len); return 0; }
int32_t interweb_ws_parse_frame_data(kr_web_client *client) { kr_websocket_client *ws; int32_t ret; int32_t pos; int32_t max; uint8_t output[4096]; ws = &client->ws; ws->input_len = client->in->len; ws->input = client->in->rd_buf; if (ws->input_len < ws->len) { printk("Incomplete WS frame: %u / %"PRIu64"", ws->input_len, ws->len); return 0; } ws->output = output; ws->output_len = sizeof(output); pos = 0; if ((ws->len == 0) || (ws->pos == ws->len) || (ws->input_len == 0) || (ws->output_len == 0)) { return 0; } max = MIN(MIN((ws->len - ws->pos), ws->input_len), ws->output_len); //printk ("max is %d", max); for (pos = 0; pos < max; pos++) { ws->output[pos] = ws->input[ws->pos] ^ ws->mask[ws->pos % 4]; ws->pos++; } output[pos] = '\0'; //printk("unmasked %d bytes %s", pos, (char *)output); ret = handle_json(client, (char *)output, pos); if (ret != 0) return -1; kr_io2_pulled (client->in, pos); if (ws->pos == ws->len) { ws->len = 0; ws->pos = 0; } return pos; }
int32_t interweb_ws_parse_frame_header(kr_web_client *client) { kr_websocket_client *ws; uint8_t *size_bytes; uint8_t payload_sz_8; uint64_t payload_sz_64; uint16_t payload_sz_16; int32_t bytes_read; uint8_t frame_type; bytes_read = 0; ws = &client->ws; ws->input_len = client->in->len; ws->input = client->in->rd_buf; if (ws->input_len < 6) { return 0; } frame_type = ws->input[0]; //printk("pframe type = %2X", frame_type); if (frame_type & WS_FIN_FRM) { //printk ("We have a fin frame!"); frame_type ^= WS_FIN_FRM; } //printk("poframe type = %2X", frame_type); if (frame_type == WS_PING_FRM) { printke("We have a ping frame but we are not dealing with it right!"); } else { if (frame_type == WS_CLOSE_FRM) { printk("Websocket close!"); return -1; } else { if (frame_type == WS_BIN_FRM) { //printk ("We have a bin frame!"); } else { if (frame_type == WS_TEXT_FRM) { //printk ("We have a text frame!"); } else { if (frame_type == WS_CONT_FRM) { //printk ("We have a CONT frame!"); } else { printke ("Unknown frame type!"); return -9; } } } } } payload_sz_8 = ws->input[1]; if (payload_sz_8 & WS_MASK_BIT) { payload_sz_8 ^= WS_MASK_BIT; } else { printke("Mask Bit is NOT set"); return -4; } if (payload_sz_8 < 126) { //printk("payload size is %u", payload_sz_8); ws->mask[0] = ws->input[2]; ws->mask[1] = ws->input[3]; ws->mask[2] = ws->input[4]; ws->mask[3] = ws->input[5]; ws->len = payload_sz_8; bytes_read = 6; } else { if (ws->input_len < 8) { return 0; } if (payload_sz_8 == 126) { size_bytes = (uint8_t *)&payload_sz_16; size_bytes[1] = ws->input[2]; size_bytes[0] = ws->input[3]; ws->mask[0] = ws->input[4]; ws->mask[1] = ws->input[5]; ws->mask[2] = ws->input[6]; ws->mask[3] = ws->input[7]; ws->len = payload_sz_16; bytes_read = 8; } else { if (ws->input_len < 14) { return 0; } size_bytes = (uint8_t *)&payload_sz_64; size_bytes[7] = ws->input[2]; size_bytes[6] = ws->input[3]; size_bytes[5] = ws->input[4]; size_bytes[4] = ws->input[5]; size_bytes[3] = ws->input[6]; size_bytes[2] = ws->input[7]; size_bytes[1] = ws->input[8]; size_bytes[0] = ws->input[9]; ws->mask[0] = ws->input[10]; ws->mask[1] = ws->input[11]; ws->mask[2] = ws->input[12]; ws->mask[3] = ws->input[13]; ws->len = payload_sz_64; bytes_read = 14; } } if (ws->len > 8192 * 6) { printke("input ws frame size too big"); ws->len = 0; ws->pos = 0; return -10; } ws->pos = 0; ws->frames++; //printk("payload size is %"PRIu64"", ws->len); kr_io2_pulled (client->in, bytes_read); return bytes_read; }