Lines Matching refs: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()
177 struct websocket_context *ctx = req->internal.user_data; in on_header_field() local
183 ctx->sec_accept_present = true; in on_header_field()
186 if (ctx->http_cb && ctx->http_cb->on_header_field) { in on_header_field()
187 ctx->http_cb->on_header_field(parser, at, length); in on_header_field()
201 struct websocket_context *ctx = req->internal.user_data; in on_header_value() local
204 if (ctx->sec_accept_present) { in on_header_value()
208 ctx->sec_accept_ok = false; in on_header_value()
209 ctx->sec_accept_present = false; in on_header_value()
212 ctx->sec_accept_key, in on_header_value()
217 "%s vs %s", ctx, str, at); in on_header_value()
219 ctx->sec_accept_ok = true; in on_header_value()
224 if (ctx->http_cb && ctx->http_cb->on_header_value) { in on_header_value()
225 ctx->http_cb->on_header_value(parser, at, length); in on_header_value()
241 struct websocket_context *ctx; in websocket_connect() local
264 ctx = websocket_find(sock); in websocket_connect()
265 if (ctx) { in websocket_connect()
266 NET_DBG("[%p] Websocket for sock %d already exists!", ctx, in websocket_connect()
271 ctx = websocket_get(); in websocket_connect()
272 if (!ctx) { in websocket_connect()
276 ctx->real_sock = sock; in websocket_connect()
277 ctx->recv_buf.buf = wreq->tmp_buf; in websocket_connect()
278 ctx->recv_buf.size = wreq->tmp_buf_len; in websocket_connect()
279 ctx->sec_accept_key = sec_accept_key; in websocket_connect()
280 ctx->http_cb = wreq->http_cb; in websocket_connect()
281 ctx->is_client = 1; in websocket_connect()
295 NET_DBG("[%p] Cannot encode base64 (%d)", ctx, ret); in websocket_connect()
300 NET_DBG("[%p] Too long message (%zd > %zd)", ctx, in websocket_connect()
328 if (ctx->http_cb) { in websocket_connect()
329 memcpy(&http_parser_settings, ctx->http_cb, in websocket_connect()
349 ret = http_client_req(sock, &req, timeout, ctx); in websocket_connect()
351 NET_DBG("[%p] Cannot connect to Websocket host %s", ctx, in websocket_connect()
357 if (!(ctx->all_received && ctx->sec_accept_ok)) { in websocket_connect()
358 NET_DBG("[%p] WS handshake failed (%d/%d)", ctx, in websocket_connect()
359 ctx->all_received, ctx->sec_accept_ok); in websocket_connect()
364 ctx->user_data = user_data; in websocket_connect()
372 ctx->sock = fd; in websocket_connect()
373 zvfs_finalize_typed_fd(fd, ctx, (const struct fd_op_vtable *)&websocket_fd_op_vtable, in websocket_connect()
382 NET_DBG("[%p] Connection aborted (%d)", ctx, ret); in websocket_connect()
387 NET_DBG("[%p] WS connection to peer established (fd %d)", ctx, fd); in websocket_connect()
393 ctx->recv_buf.count = req.data_len; in websocket_connect()
396 ctx->parser_state = WEBSOCKET_PARSER_STATE_OPCODE; in websocket_connect()
398 (void)sock_obj_core_alloc_find(ctx->real_sock, fd, SOCK_STREAM); in websocket_connect()
407 websocket_context_unref(ctx); in websocket_connect()
416 static int websocket_interal_disconnect(struct websocket_context *ctx) in websocket_interal_disconnect() argument
420 if (ctx == NULL) { in websocket_interal_disconnect()
424 NET_DBG("[%p] Disconnecting", ctx); in websocket_interal_disconnect()
426 ret = websocket_send_msg(ctx->sock, NULL, 0, WEBSOCKET_OPCODE_CLOSE, in websocket_interal_disconnect()
427 ctx->is_client, true, SYS_FOREVER_MS); in websocket_interal_disconnect()
429 NET_DBG("[%p] Failed to send close message (err %d).", ctx, ret); in websocket_interal_disconnect()
432 (void)sock_obj_core_dealloc(ctx->sock); in websocket_interal_disconnect()
434 websocket_context_unref(ctx); in websocket_interal_disconnect()
441 struct websocket_context *ctx = obj; in websocket_close_vmeth() local
444 ret = websocket_interal_disconnect(ctx); in websocket_close_vmeth()
465 void *ctx; in websocket_poll_offload() local
473 ctx = zvfs_get_fd_obj(fds[i].fd, in websocket_poll_offload()
477 if (ctx == NULL) { in websocket_poll_offload()
481 fds[i].fd = ((struct websocket_context *)ctx)->real_sock; in websocket_poll_offload()
485 ctx = zvfs_get_fd_obj_and_vtable(fds[0].fd, in websocket_poll_offload()
488 if (ctx == NULL) { in websocket_poll_offload()
494 ret = zvfs_fdtable_call_ioctl(vtable, ctx, ZFD_IOCTL_POLL_OFFLOAD, in websocket_poll_offload()
508 struct websocket_context *ctx = obj; in websocket_ioctl_vmeth() local
532 ctx->real_sock, in websocket_ioctl_vmeth()
607 static int websocket_prepare_and_send(struct websocket_context *ctx, in websocket_prepare_and_send() argument
649 return sendmsg_all(ctx->real_sock, &msg, in websocket_prepare_and_send()
659 struct websocket_context *ctx; in websocket_send_msg() local
673 ctx = zvfs_get_fd_obj(ws_sock, NULL, 0); in websocket_send_msg()
674 if (ctx == NULL) { in websocket_send_msg()
683 if (!PART_OF_ARRAY(contexts, ctx)) { in websocket_send_msg()
688 NET_DBG("[%p] Len %zd %s/%d/%s", ctx, payload_len, opcode2str(opcode), in websocket_send_msg()
726 ctx->masking_value = sys_rand32_get(); in websocket_send_msg()
728 header[hdr_len++] |= ctx->masking_value >> 24; in websocket_send_msg()
729 header[hdr_len++] |= ctx->masking_value >> 16; in websocket_send_msg()
730 header[hdr_len++] |= ctx->masking_value >> 8; in websocket_send_msg()
731 header[hdr_len++] |= ctx->masking_value; in websocket_send_msg()
742 data_to_send[i] ^= ctx->masking_value >> (8 * (3 - i % 4)); in websocket_send_msg()
747 ret = websocket_prepare_and_send(ctx, header, hdr_len, in websocket_send_msg()
786 static int websocket_parse(struct websocket_context *ctx, struct websocket_buffer *payload) in websocket_parse() argument
793 if (parsed_count >= ctx->recv_buf.count) { in websocket_parse()
796 if (ctx->parser_state != WEBSOCKET_PARSER_STATE_PAYLOAD) { in websocket_parse()
797 data = ctx->recv_buf.buf[parsed_count++]; in websocket_parse()
799 switch (ctx->parser_state) { in websocket_parse()
801 ctx->message_type = websocket_opcode2flag(data); in websocket_parse()
803 ctx->message_type |= WEBSOCKET_FLAG_FINAL; in websocket_parse()
805 ctx->parser_state = WEBSOCKET_PARSER_STATE_LENGTH; in websocket_parse()
808 ctx->masked = (data & 0x80) != 0; in websocket_parse()
811 ctx->message_len = len; in websocket_parse()
812 if (ctx->masked) { in websocket_parse()
813 ctx->masking_value = 0; in websocket_parse()
814 ctx->parser_remaining = 4; in websocket_parse()
815 ctx->parser_state = WEBSOCKET_PARSER_STATE_MASK; in websocket_parse()
817 ctx->parser_remaining = ctx->message_len; in websocket_parse()
818 ctx->parser_state = in websocket_parse()
819 (ctx->parser_remaining == 0) in websocket_parse()
824 ctx->message_len = 0; in websocket_parse()
825 ctx->parser_remaining = (len < 127) ? 2 : 8; in websocket_parse()
826 ctx->parser_state = WEBSOCKET_PARSER_STATE_EXT_LEN; in websocket_parse()
830 ctx->parser_remaining--; in websocket_parse()
831 ctx->message_len |= ((uint64_t)data << (ctx->parser_remaining * 8)); in websocket_parse()
832 if (ctx->parser_remaining == 0) { in websocket_parse()
833 if (ctx->masked) { in websocket_parse()
834 ctx->masking_value = 0; in websocket_parse()
835 ctx->parser_remaining = 4; in websocket_parse()
836 ctx->parser_state = WEBSOCKET_PARSER_STATE_MASK; in websocket_parse()
838 ctx->parser_remaining = ctx->message_len; in websocket_parse()
839 ctx->parser_state = WEBSOCKET_PARSER_STATE_PAYLOAD; in websocket_parse()
844 ctx->parser_remaining--; in websocket_parse()
845 ctx->masking_value |= in websocket_parse()
846 (uint32_t)((uint64_t)data << (ctx->parser_remaining * 8)); in websocket_parse()
847 if (ctx->parser_remaining == 0) { in websocket_parse()
848 if (ctx->message_len == 0) { in websocket_parse()
849 ctx->parser_remaining = 0; in websocket_parse()
850 ctx->parser_state = WEBSOCKET_PARSER_STATE_OPCODE; in websocket_parse()
852 ctx->parser_remaining = ctx->message_len; in websocket_parse()
853 ctx->parser_state = WEBSOCKET_PARSER_STATE_PAYLOAD; in websocket_parse()
862 if ((ctx->parser_state == WEBSOCKET_PARSER_STATE_PAYLOAD) || in websocket_parse()
863 ((ctx->parser_state == WEBSOCKET_PARSER_STATE_OPCODE) && in websocket_parse()
864 (ctx->message_len == 0))) { in websocket_parse()
865 NET_DBG("[%p] %smasked, mask 0x%08x, type 0x%02x, msg %zd", ctx, in websocket_parse()
866 ctx->masked ? "" : "un", in websocket_parse()
867 ctx->masked ? ctx->masking_value : 0, ctx->message_type, in websocket_parse()
868 (size_t)ctx->message_len); in websocket_parse()
872 size_t remaining_in_recv_buf = ctx->recv_buf.count - parsed_count; in websocket_parse()
874 MIN(remaining_in_recv_buf, ctx->parser_remaining); in websocket_parse()
882 memcpy(&payload->buf[payload->count], &ctx->recv_buf.buf[parsed_count], in websocket_parse()
886 ctx->parser_remaining -= ready_to_copy; in websocket_parse()
887 if (ctx->parser_remaining == 0) { in websocket_parse()
888 ctx->parser_remaining = 0; in websocket_parse()
889 ctx->parser_state = WEBSOCKET_PARSER_STATE_OPCODE; in websocket_parse()
893 } while (ctx->parser_state != WEBSOCKET_PARSER_STATE_OPCODE); in websocket_parse()
944 struct websocket_context *ctx; in websocket_recv_msg() local
967 ctx = test_data->ctx; in websocket_recv_msg()
969 ctx = zvfs_get_fd_obj(ws_sock, NULL, 0); in websocket_recv_msg()
970 if (ctx == NULL) { in websocket_recv_msg()
974 if (!PART_OF_ARRAY(contexts, ctx)) { in websocket_recv_msg()
982 if (ctx->recv_buf.count == 0) { in websocket_recv_msg()
984 size_t input_len = MIN(ctx->recv_buf.size, in websocket_recv_msg()
988 memcpy(ctx->recv_buf.buf, in websocket_recv_msg()
999 ret = wait_rx(ctx->real_sock, timeout_to_ms(&tout)); in websocket_recv_msg()
1001 ret = zsock_recv(ctx->real_sock, ctx->recv_buf.buf, in websocket_recv_msg()
1002 ctx->recv_buf.size, ZSOCK_MSG_DONTWAIT); in websocket_recv_msg()
1022 ctx->recv_buf.count = ret; in websocket_recv_msg()
1024 NET_DBG("[%p] Received %d bytes", ctx, ret); in websocket_recv_msg()
1027 ret = websocket_parse(ctx, &payload); in websocket_recv_msg()
1033 if ((ctx->parser_state == WEBSOCKET_PARSER_STATE_OPCODE) || in websocket_recv_msg()
1036 *remaining = ctx->parser_remaining; in websocket_recv_msg()
1039 *message_type = ctx->message_type; in websocket_recv_msg()
1042 size_t left = ctx->recv_buf.count - parsed_count; in websocket_recv_msg()
1045 memmove(ctx->recv_buf.buf, &ctx->recv_buf.buf[parsed_count], left); in websocket_recv_msg()
1047 ctx->recv_buf.count = left; in websocket_recv_msg()
1051 ctx->recv_buf.count -= parsed_count; in websocket_recv_msg()
1056 if (ctx->masked) { in websocket_recv_msg()
1057 uint8_t *mask_as_bytes = (uint8_t *)&ctx->masking_value; in websocket_recv_msg()
1058 size_t data_buf_offset = ctx->message_len - ctx->parser_remaining - payload.count; in websocket_recv_msg()
1071 static int websocket_send(struct websocket_context *ctx, const uint8_t *buf, in websocket_send() argument
1076 NET_DBG("[%p] Sending %zd bytes", ctx, buf_len); in websocket_send()
1078 ret = websocket_send_msg(ctx->sock, buf, buf_len, WEBSOCKET_OPCODE_DATA_TEXT, in websocket_send()
1079 ctx->is_client, true, timeout); in websocket_send()
1085 NET_DBG("[%p] Sent %d bytes", ctx, ret); in websocket_send()
1087 sock_obj_core_update_send_stats(ctx->sock, ret); in websocket_send()
1092 static int websocket_recv(struct websocket_context *ctx, uint8_t *buf, in websocket_recv() argument
1099 NET_DBG("[%p] Waiting data, buf len %zd bytes", ctx, buf_len); in websocket_recv()
1104 ret = websocket_recv_msg(ctx->sock, buf, buf_len, &message_type, in websocket_recv()
1115 NET_DBG("[%p] Received %d bytes", ctx, ret); in websocket_recv()
1117 sock_obj_core_update_recv_stats(ctx->sock, ret); in websocket_recv()
1138 struct websocket_context *ctx = obj; in websocket_sendto_ctx() local
1148 return (ssize_t)websocket_send(ctx, buf, len, timeout); in websocket_sendto_ctx()
1155 struct websocket_context *ctx = obj; in websocket_recvfrom_ctx() local
1165 return (ssize_t)websocket_recv(ctx, buf, max_len, timeout); in websocket_recvfrom_ctx()
1170 struct websocket_context *ctx; in websocket_register() local
1177 ctx = websocket_find(sock); in websocket_register()
1178 if (ctx) { in websocket_register()
1179 NET_DBG("[%p] Websocket for sock %d already exists!", ctx, sock); in websocket_register()
1183 ctx = websocket_get(); in websocket_register()
1184 if (!ctx) { in websocket_register()
1188 ctx->real_sock = sock; in websocket_register()
1189 ctx->recv_buf.buf = recv_buf; in websocket_register()
1190 ctx->recv_buf.size = recv_buf_len; in websocket_register()
1191 ctx->is_client = 0; in websocket_register()
1199 ctx->sock = fd; in websocket_register()
1200 zvfs_finalize_typed_fd(fd, ctx, (const struct fd_op_vtable *)&websocket_fd_op_vtable, in websocket_register()
1203 NET_DBG("[%p] WS connection to peer established (fd %d)", ctx, fd); in websocket_register()
1205 ctx->recv_buf.count = 0; in websocket_register()
1206 ctx->parser_state = WEBSOCKET_PARSER_STATE_OPCODE; in websocket_register()
1208 (void)sock_obj_core_alloc_find(ctx->real_sock, fd, SOCK_STREAM); in websocket_register()
1213 websocket_context_unref(ctx); in websocket_register()
1220 struct websocket_context *ctx = NULL; in websocket_search() local
1234 ctx = &contexts[i]; in websocket_search()
1240 return ctx; in websocket_search()
1245 struct websocket_context *ctx; in websocket_unregister() local
1251 ctx = websocket_search(sock); in websocket_unregister()
1252 if (ctx == NULL) { in websocket_unregister()
1253 NET_DBG("[%p] Real socket for websocket sock %d not found!", ctx, sock); in websocket_unregister()
1257 if (ctx->real_sock < 0) { in websocket_unregister()
1262 (void)zsock_close(ctx->real_sock); in websocket_unregister()
1264 ctx->real_sock = -1; in websocket_unregister()
1265 ctx->sock = -1; in websocket_unregister()