Lines Matching full:ctx

82 static int websocket_context_ref(struct websocket_context *ctx)  in websocket_context_ref()  argument
84 int old_rc = atomic_inc(&ctx->refcount); in websocket_context_ref()
89 static int websocket_context_unref(struct websocket_context *ctx) in websocket_context_unref() argument
91 int old_rc = atomic_dec(&ctx->refcount); in websocket_context_unref()
100 static inline bool websocket_context_is_used(struct websocket_context *ctx) in websocket_context_is_used() argument
102 return !!atomic_get(&ctx->refcount); in websocket_context_is_used()
107 struct websocket_context *ctx = NULL; in websocket_get() local
118 ctx = &contexts[i]; in websocket_get()
124 return ctx; in websocket_get()
129 struct websocket_context *ctx = NULL; in websocket_find() local
143 ctx = &contexts[i]; in websocket_find()
149 return ctx; in websocket_find()
156 struct websocket_context *ctx = user_data; in response_cb() local
159 NET_DBG("[%p] Partial data received (%zd bytes)", ctx, in response_cb()
161 ctx->all_received = false; in response_cb()
163 NET_DBG("[%p] All the data received (%zd bytes)", ctx, in response_cb()
165 ctx->all_received = true; in response_cb()
175 struct websocket_context *ctx = req->internal.user_data; in on_header_field() local
181 ctx->sec_accept_present = true; in on_header_field()
184 if (ctx->http_cb && ctx->http_cb->on_header_field) { in on_header_field()
185 ctx->http_cb->on_header_field(parser, at, length); in on_header_field()
199 struct websocket_context *ctx = req->internal.user_data; in on_header_value() local
202 if (ctx->sec_accept_present) { in on_header_value()
206 ctx->sec_accept_ok = false; in on_header_value()
207 ctx->sec_accept_present = false; in on_header_value()
210 ctx->sec_accept_key, in on_header_value()
215 "%s vs %s", ctx, str, at); in on_header_value()
217 ctx->sec_accept_ok = true; in on_header_value()
222 if (ctx->http_cb && ctx->http_cb->on_header_value) { in on_header_value()
223 ctx->http_cb->on_header_value(parser, at, length); in on_header_value()
233 * pointer to this in ctx but the value is only used for the duration in websocket_connect()
239 struct websocket_context *ctx; in websocket_connect() local
262 ctx = websocket_find(sock); in websocket_connect()
263 if (ctx) { in websocket_connect()
264 NET_DBG("[%p] Websocket for sock %d already exists!", ctx, in websocket_connect()
269 ctx = websocket_get(); in websocket_connect()
270 if (!ctx) { in websocket_connect()
274 ctx->real_sock = sock; in websocket_connect()
275 ctx->recv_buf.buf = wreq->tmp_buf; in websocket_connect()
276 ctx->recv_buf.size = wreq->tmp_buf_len; in websocket_connect()
277 ctx->sec_accept_key = sec_accept_key; in websocket_connect()
278 ctx->http_cb = wreq->http_cb; in websocket_connect()
279 ctx->is_client = 1; in websocket_connect()
293 NET_DBG("[%p] Cannot encode base64 (%d)", ctx, ret); in websocket_connect()
298 NET_DBG("[%p] Too long message (%zd > %zd)", ctx, in websocket_connect()
326 if (ctx->http_cb) { in websocket_connect()
327 memcpy(&http_parser_settings, ctx->http_cb, in websocket_connect()
347 ret = http_client_req(sock, &req, timeout, ctx); in websocket_connect()
349 NET_DBG("[%p] Cannot connect to Websocket host %s", ctx, in websocket_connect()
355 if (!(ctx->all_received && ctx->sec_accept_ok)) { in websocket_connect()
356 NET_DBG("[%p] WS handshake failed (%d/%d)", ctx, in websocket_connect()
357 ctx->all_received, ctx->sec_accept_ok); in websocket_connect()
362 ctx->user_data = user_data; in websocket_connect()
370 ctx->sock = fd; in websocket_connect()
371 zvfs_finalize_typed_fd(fd, ctx, (const struct fd_op_vtable *)&websocket_fd_op_vtable, in websocket_connect()
380 NET_DBG("[%p] Connection aborted (%d)", ctx, ret); in websocket_connect()
385 NET_DBG("[%p] WS connection to peer established (fd %d)", ctx, fd); in websocket_connect()
390 ctx->recv_buf.count = 0; in websocket_connect()
393 ctx->parser_state = WEBSOCKET_PARSER_STATE_OPCODE; in websocket_connect()
395 (void)sock_obj_core_alloc_find(ctx->real_sock, fd, SOCK_STREAM); in websocket_connect()
404 websocket_context_unref(ctx); in websocket_connect()
413 static int websocket_interal_disconnect(struct websocket_context *ctx) in websocket_interal_disconnect() argument
417 if (ctx == NULL) { in websocket_interal_disconnect()
421 NET_DBG("[%p] Disconnecting", ctx); in websocket_interal_disconnect()
423 ret = websocket_send_msg(ctx->sock, NULL, 0, WEBSOCKET_OPCODE_CLOSE, in websocket_interal_disconnect()
426 NET_DBG("[%p] Failed to send close message (err %d).", ctx, ret); in websocket_interal_disconnect()
429 (void)sock_obj_core_dealloc(ctx->sock); in websocket_interal_disconnect()
431 websocket_context_unref(ctx); in websocket_interal_disconnect()
438 struct websocket_context *ctx = obj; in websocket_close_vmeth() local
441 ret = websocket_interal_disconnect(ctx); in websocket_close_vmeth()
462 void *ctx; in websocket_poll_offload() local
470 ctx = zvfs_get_fd_obj(fds[i].fd, in websocket_poll_offload()
474 if (ctx == NULL) { in websocket_poll_offload()
478 fds[i].fd = ((struct websocket_context *)ctx)->real_sock; in websocket_poll_offload()
482 ctx = zvfs_get_fd_obj_and_vtable(fds[0].fd, in websocket_poll_offload()
485 if (ctx == NULL) { in websocket_poll_offload()
491 ret = zvfs_fdtable_call_ioctl(vtable, ctx, ZFD_IOCTL_POLL_OFFLOAD, in websocket_poll_offload()
505 struct websocket_context *ctx = obj; in websocket_ioctl_vmeth() local
529 ctx->real_sock, in websocket_ioctl_vmeth()
604 static int websocket_prepare_and_send(struct websocket_context *ctx, in websocket_prepare_and_send() argument
646 return sendmsg_all(ctx->real_sock, &msg, in websocket_prepare_and_send()
656 struct websocket_context *ctx; in websocket_send_msg() local
670 ctx = zvfs_get_fd_obj(ws_sock, NULL, 0); in websocket_send_msg()
671 if (ctx == NULL) { in websocket_send_msg()
680 if (!PART_OF_ARRAY(contexts, ctx)) { in websocket_send_msg()
685 NET_DBG("[%p] Len %zd %s/%d/%s", ctx, payload_len, opcode2str(opcode), in websocket_send_msg()
723 ctx->masking_value = sys_rand32_get(); in websocket_send_msg()
725 header[hdr_len++] |= ctx->masking_value >> 24; in websocket_send_msg()
726 header[hdr_len++] |= ctx->masking_value >> 16; in websocket_send_msg()
727 header[hdr_len++] |= ctx->masking_value >> 8; in websocket_send_msg()
728 header[hdr_len++] |= ctx->masking_value; in websocket_send_msg()
739 data_to_send[i] ^= ctx->masking_value >> (8 * (3 - i % 4)); in websocket_send_msg()
744 ret = websocket_prepare_and_send(ctx, header, hdr_len, in websocket_send_msg()
783 static int websocket_parse(struct websocket_context *ctx, struct websocket_buffer *payload) in websocket_parse() argument
790 if (parsed_count >= ctx->recv_buf.count) { in websocket_parse()
793 if (ctx->parser_state != WEBSOCKET_PARSER_STATE_PAYLOAD) { in websocket_parse()
794 data = ctx->recv_buf.buf[parsed_count++]; in websocket_parse()
796 switch (ctx->parser_state) { in websocket_parse()
798 ctx->message_type = websocket_opcode2flag(data); in websocket_parse()
800 ctx->message_type |= WEBSOCKET_FLAG_FINAL; in websocket_parse()
802 ctx->parser_state = WEBSOCKET_PARSER_STATE_LENGTH; in websocket_parse()
805 ctx->masked = (data & 0x80) != 0; in websocket_parse()
808 ctx->message_len = len; in websocket_parse()
809 if (ctx->masked) { in websocket_parse()
810 ctx->masking_value = 0; in websocket_parse()
811 ctx->parser_remaining = 4; in websocket_parse()
812 ctx->parser_state = WEBSOCKET_PARSER_STATE_MASK; in websocket_parse()
814 ctx->parser_remaining = ctx->message_len; in websocket_parse()
815 ctx->parser_state = in websocket_parse()
816 (ctx->parser_remaining == 0) in websocket_parse()
821 ctx->message_len = 0; in websocket_parse()
822 ctx->parser_remaining = (len < 127) ? 2 : 8; in websocket_parse()
823 ctx->parser_state = WEBSOCKET_PARSER_STATE_EXT_LEN; in websocket_parse()
827 ctx->parser_remaining--; in websocket_parse()
828 ctx->message_len |= ((uint64_t)data << (ctx->parser_remaining * 8)); in websocket_parse()
829 if (ctx->parser_remaining == 0) { in websocket_parse()
830 if (ctx->masked) { in websocket_parse()
831 ctx->masking_value = 0; in websocket_parse()
832 ctx->parser_remaining = 4; in websocket_parse()
833 ctx->parser_state = WEBSOCKET_PARSER_STATE_MASK; in websocket_parse()
835 ctx->parser_remaining = ctx->message_len; in websocket_parse()
836 ctx->parser_state = WEBSOCKET_PARSER_STATE_PAYLOAD; in websocket_parse()
841 ctx->parser_remaining--; in websocket_parse()
842 ctx->masking_value |= (data << (ctx->parser_remaining * 8)); in websocket_parse()
843 if (ctx->parser_remaining == 0) { in websocket_parse()
844 if (ctx->message_len == 0) { in websocket_parse()
845 ctx->parser_remaining = 0; in websocket_parse()
846 ctx->parser_state = WEBSOCKET_PARSER_STATE_OPCODE; in websocket_parse()
848 ctx->parser_remaining = ctx->message_len; in websocket_parse()
849 ctx->parser_state = WEBSOCKET_PARSER_STATE_PAYLOAD; in websocket_parse()
858 if ((ctx->parser_state == WEBSOCKET_PARSER_STATE_PAYLOAD) || in websocket_parse()
859 ((ctx->parser_state == WEBSOCKET_PARSER_STATE_OPCODE) && in websocket_parse()
860 (ctx->message_len == 0))) { in websocket_parse()
861 NET_DBG("[%p] %smasked, mask 0x%08x, type 0x%02x, msg %zd", ctx, in websocket_parse()
862 ctx->masked ? "" : "un", in websocket_parse()
863 ctx->masked ? ctx->masking_value : 0, ctx->message_type, in websocket_parse()
864 (size_t)ctx->message_len); in websocket_parse()
868 size_t remaining_in_recv_buf = ctx->recv_buf.count - parsed_count; in websocket_parse()
870 MIN(remaining_in_recv_buf, ctx->parser_remaining); in websocket_parse()
878 memcpy(&payload->buf[payload->count], &ctx->recv_buf.buf[parsed_count], in websocket_parse()
882 ctx->parser_remaining -= ready_to_copy; in websocket_parse()
883 if (ctx->parser_remaining == 0) { in websocket_parse()
884 ctx->parser_remaining = 0; in websocket_parse()
885 ctx->parser_state = WEBSOCKET_PARSER_STATE_OPCODE; in websocket_parse()
889 } while (ctx->parser_state != WEBSOCKET_PARSER_STATE_OPCODE); in websocket_parse()
940 struct websocket_context *ctx; in websocket_recv_msg() local
963 ctx = test_data->ctx; in websocket_recv_msg()
965 ctx = zvfs_get_fd_obj(ws_sock, NULL, 0); in websocket_recv_msg()
966 if (ctx == NULL) { in websocket_recv_msg()
970 if (!PART_OF_ARRAY(contexts, ctx)) { in websocket_recv_msg()
978 if (ctx->recv_buf.count == 0) { in websocket_recv_msg()
980 size_t input_len = MIN(ctx->recv_buf.size, in websocket_recv_msg()
984 memcpy(ctx->recv_buf.buf, in websocket_recv_msg()
995 ret = wait_rx(ctx->real_sock, timeout_to_ms(&tout)); in websocket_recv_msg()
997 ret = zsock_recv(ctx->real_sock, ctx->recv_buf.buf, in websocket_recv_msg()
998 ctx->recv_buf.size, ZSOCK_MSG_DONTWAIT); in websocket_recv_msg()
1018 ctx->recv_buf.count = ret; in websocket_recv_msg()
1020 NET_DBG("[%p] Received %d bytes", ctx, ret); in websocket_recv_msg()
1023 ret = websocket_parse(ctx, &payload); in websocket_recv_msg()
1029 if ((ctx->parser_state == WEBSOCKET_PARSER_STATE_OPCODE) || in websocket_recv_msg()
1032 *remaining = ctx->parser_remaining; in websocket_recv_msg()
1035 *message_type = ctx->message_type; in websocket_recv_msg()
1038 size_t left = ctx->recv_buf.count - parsed_count; in websocket_recv_msg()
1041 memmove(ctx->recv_buf.buf, &ctx->recv_buf.buf[parsed_count], left); in websocket_recv_msg()
1043 ctx->recv_buf.count = left; in websocket_recv_msg()
1047 ctx->recv_buf.count -= parsed_count; in websocket_recv_msg()
1052 if (ctx->masked) { in websocket_recv_msg()
1053 uint8_t *mask_as_bytes = (uint8_t *)&ctx->masking_value; in websocket_recv_msg()
1054 size_t data_buf_offset = ctx->message_len - ctx->parser_remaining - payload.count; in websocket_recv_msg()
1067 static int websocket_send(struct websocket_context *ctx, const uint8_t *buf, in websocket_send() argument
1072 NET_DBG("[%p] Sending %zd bytes", ctx, buf_len); in websocket_send()
1074 ret = websocket_send_msg(ctx->sock, buf, buf_len, WEBSOCKET_OPCODE_DATA_TEXT, in websocket_send()
1075 ctx->is_client, true, timeout); in websocket_send()
1081 NET_DBG("[%p] Sent %d bytes", ctx, ret); in websocket_send()
1083 sock_obj_core_update_send_stats(ctx->sock, ret); in websocket_send()
1088 static int websocket_recv(struct websocket_context *ctx, uint8_t *buf, in websocket_recv() argument
1095 NET_DBG("[%p] Waiting data, buf len %zd bytes", ctx, buf_len); in websocket_recv()
1100 ret = websocket_recv_msg(ctx->sock, buf, buf_len, &message_type, in websocket_recv()
1111 NET_DBG("[%p] Received %d bytes", ctx, ret); in websocket_recv()
1113 sock_obj_core_update_recv_stats(ctx->sock, ret); in websocket_recv()
1134 struct websocket_context *ctx = obj; in websocket_sendto_ctx() local
1144 return (ssize_t)websocket_send(ctx, buf, len, timeout); in websocket_sendto_ctx()
1151 struct websocket_context *ctx = obj; in websocket_recvfrom_ctx() local
1161 return (ssize_t)websocket_recv(ctx, buf, max_len, timeout); in websocket_recvfrom_ctx()
1166 struct websocket_context *ctx; in websocket_register() local
1173 ctx = websocket_find(sock); in websocket_register()
1174 if (ctx) { in websocket_register()
1175 NET_DBG("[%p] Websocket for sock %d already exists!", ctx, sock); in websocket_register()
1179 ctx = websocket_get(); in websocket_register()
1180 if (!ctx) { in websocket_register()
1184 ctx->real_sock = sock; in websocket_register()
1185 ctx->recv_buf.buf = recv_buf; in websocket_register()
1186 ctx->recv_buf.size = recv_buf_len; in websocket_register()
1187 ctx->is_client = 0; in websocket_register()
1195 ctx->sock = fd; in websocket_register()
1196 zvfs_finalize_typed_fd(fd, ctx, (const struct fd_op_vtable *)&websocket_fd_op_vtable, in websocket_register()
1199 NET_DBG("[%p] WS connection to peer established (fd %d)", ctx, fd); in websocket_register()
1201 ctx->recv_buf.count = 0; in websocket_register()
1202 ctx->parser_state = WEBSOCKET_PARSER_STATE_OPCODE; in websocket_register()
1204 (void)sock_obj_core_alloc_find(ctx->real_sock, fd, SOCK_STREAM); in websocket_register()
1209 websocket_context_unref(ctx); in websocket_register()
1216 struct websocket_context *ctx = NULL; in websocket_search() local
1230 ctx = &contexts[i]; in websocket_search()
1236 return ctx; in websocket_search()
1241 struct websocket_context *ctx; in websocket_unregister() local
1247 ctx = websocket_search(sock); in websocket_unregister()
1248 if (ctx == NULL) { in websocket_unregister()
1249 NET_DBG("[%p] Real socket for websocket sock %d not found!", ctx, sock); in websocket_unregister()
1253 if (ctx->real_sock < 0) { in websocket_unregister()
1258 (void)zsock_close(ctx->real_sock); in websocket_unregister()
1260 ctx->real_sock = -1; in websocket_unregister()
1261 ctx->sock = -1; in websocket_unregister()