libcoap 4.3.2rc2

coap_ws_internal.h
Go to the documentation of this file.
1/*
2 * coap_ws_internal.h -- WebSockets Transport Layer Support for libcoap
3 *
4 * Copyright (C) 2023 Olaf Bergmann <bergmann@tzi.org>
5 * Copyright (C) 2023 Jon Shallow <supjps-libcoap@jpshallow.com>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
18#ifndef COAP_WS_INTERNAL_H_
19#define COAP_WS_INTERNAL_H_
20
21#include "coap_internal.h"
22
31/* Frame size: Min Header + (Opt) Ext payload length + (Opt) Masking key */
32#define COAP_MAX_FS (2 + 8 + 4)
33
37 typedef struct coap_ws_state_t {
38 coap_session_type_t state;
39 uint8_t up;
40 uint8_t seen_first;
41 uint8_t seen_host;
42 uint8_t seen_upg;
43 uint8_t seen_conn;
44 uint8_t seen_key;
45 uint8_t seen_proto;
46 uint8_t seen_ver;
47 uint8_t sent_close;
48 uint8_t recv_close;
49 uint16_t close_reason;
50 int all_hdr_in;
51 int hdr_ofs;
52 uint8_t rd_header[COAP_MAX_FS];
53 uint8_t mask_key[4];
54 uint32_t http_ofs;
55 uint8_t http_hdr[80];
56 size_t data_ofs;
57 size_t data_size;
58 uint8_t key[16];
59} coap_ws_state_t;
60
61/*
62 * WebSockets Frame
63 *
64 * 0 1 2 3
65 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
66 * +-+-+-+-+-------+-+-------------+-------------------------------+
67 * |F|R|R|R| opcode|M| Payload len | Extended payload length |
68 * |I|S|S|S| (4) |A| (7) | (16/64) |
69 * |N|V|V|V| |S| | (if payload len==126/127) |
70 * | |1|2|3| |K| | |
71 * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
72 * | Extended payload length continued, if payload len == 127 |
73 * + - - - - - - - - - - - - - - - +-------------------------------+
74 * | |Masking-key, if MASK set to 1 |
75 * +-------------------------------+-------------------------------+
76 * | Masking-key (continued) | Payload Data |
77 * +-------------------------------- - - - - - - - - - - - - - - - +
78 * : Payload Data continued ... :
79 * + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
80 * | Payload Data continued ... |
81 * +---------------------------------------------------------------+
82 */
83
84#define WS_B0_FIN_BIT 0x80
85#define WS_B0_RSV_MASK 0x70
86#define WS_B0_OP_MASK 0x0f
87
88#define WS_B1_MASK_BIT 0x80
89#define WS_B1_LEN_MASK 0x7f
90
91 typedef enum {
92 WS_OP_CONT = 0x0,
93 WS_OP_TEXT,
94 WS_OP_BINARY,
95 WS_OP_CLOSE = 0x8,
96 WS_OP_PING,
97 WS_OP_PONG
98} coap_ws_opcode_t;
99
113ssize_t coap_ws_write(coap_session_t *session,
114 const uint8_t *data, size_t datalen);
115
129ssize_t coap_ws_read(coap_session_t *session, uint8_t *data,
130 size_t datalen);
131
144 void coap_ws_establish(coap_session_t *session);
145
151 void coap_ws_close(coap_session_t *session);
152
155#endif /* COAP_WS_INTERNAL_H */
coap_internal.h
Pulls together all the internal only header files.
coap_session_type_t
coap_session_type_t
coap_session_type_t values
Definition: coap_session.h:43
coap_ws_state_t
struct coap_ws_state_t coap_ws_state_t
WebSockets session state.
coap_ws_establish
void coap_ws_establish(coap_session_t *session)
Layer function interface for layer below WebSockets accept/connect being established.
coap_ws_opcode_t
coap_ws_opcode_t
Definition: coap_ws_internal.h:91
coap_ws_write
ssize_t coap_ws_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for websockets data transmission.
coap_ws_close
void coap_ws_close(coap_session_t *session)
Layer function interface for WebSockets close for a session.
COAP_MAX_FS
#define COAP_MAX_FS
Definition: coap_ws_internal.h:32
coap_ws_read
ssize_t coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen)
Function interface for websockets data receiving.
WS_OP_TEXT
@ WS_OP_TEXT
Definition: coap_ws_internal.h:93
WS_OP_CLOSE
@ WS_OP_CLOSE
Definition: coap_ws_internal.h:95
WS_OP_PONG
@ WS_OP_PONG
Definition: coap_ws_internal.h:97
WS_OP_PING
@ WS_OP_PING
Definition: coap_ws_internal.h:96
WS_OP_BINARY
@ WS_OP_BINARY
Definition: coap_ws_internal.h:94
WS_OP_CONT
@ WS_OP_CONT
Definition: coap_ws_internal.h:92
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition: coap_session_internal.h:68
coap_ws_state_t
WebSockets session state.
Definition: coap_ws_internal.h:37
coap_ws_state_t::http_hdr
uint8_t http_hdr[80]
(Partial) HTTP header
Definition: coap_ws_internal.h:55
coap_ws_state_t::up
uint8_t up
WebSockets established.
Definition: coap_ws_internal.h:39
coap_ws_state_t::recv_close
uint8_t recv_close
Close has been received.
Definition: coap_ws_internal.h:48
coap_ws_state_t::key
uint8_t key[16]
Random, but agreed key value.
Definition: coap_ws_internal.h:58
coap_ws_state_t::seen_host
uint8_t seen_host
Seen Host: HTTP header (server)
Definition: coap_ws_internal.h:41
coap_ws_state_t::seen_ver
uint8_t seen_ver
Seen version: HTTP header (server)
Definition: coap_ws_internal.h:46
coap_ws_state_t::seen_key
uint8_t seen_key
Seen Key: HTTP header.
Definition: coap_ws_internal.h:44
coap_ws_state_t::close_reason
uint16_t close_reason
Reason for closing.
Definition: coap_ws_internal.h:49
coap_ws_state_t::data_ofs
size_t data_ofs
Offset into user provided buffer.
Definition: coap_ws_internal.h:56
coap_ws_state_t::seen_conn
uint8_t seen_conn
Seen Connection: HTTP header.
Definition: coap_ws_internal.h:43
coap_ws_state_t::data_size
size_t data_size
Data size as indicated by WebSocket frame.
Definition: coap_ws_internal.h:57
coap_ws_state_t::seen_upg
uint8_t seen_upg
Seen Upgrade: HTTP header.
Definition: coap_ws_internal.h:42
coap_ws_state_t::http_ofs
uint32_t http_ofs
Current offset into http_hdr.
Definition: coap_ws_internal.h:54
coap_ws_state_t::all_hdr_in
int all_hdr_in
Frame header available.
Definition: coap_ws_internal.h:50
coap_ws_state_t::hdr_ofs
int hdr_ofs
Current offset into rd_header.
Definition: coap_ws_internal.h:51
coap_ws_state_t::sent_close
uint8_t sent_close
Close has been sent.
Definition: coap_ws_internal.h:47
coap_ws_state_t::state
coap_session_type_t state
Client or Server.
Definition: coap_ws_internal.h:38
coap_ws_state_t::rd_header
uint8_t rd_header[COAP_MAX_FS]
(Partial) frame
Definition: coap_ws_internal.h:52
coap_ws_state_t::seen_proto
uint8_t seen_proto
Seen Protocol: HTTP header.
Definition: coap_ws_internal.h:45
coap_ws_state_t::mask_key
uint8_t mask_key[4]
Masking key.
Definition: coap_ws_internal.h:53
coap_ws_state_t::seen_first
uint8_t seen_first
Seen first request/response HTTP header.
Definition: coap_ws_internal.h:40