Loading...
Searching...
No Matches
coap_ws_internal.h
Go to the documentation of this file.
/*
* coap_ws_internal.h -- WebSockets Transport Layer Support for libcoap
*
* Copyright (C) 2023-2024 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2023-2024 Jon Shallow <supjps-libcoap@jpshallow.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_WS_INTERNAL_H_
#define COAP_WS_INTERNAL_H_
#include "coap_internal.h"
/* Frame size: Min Header + (Opt) Ext payload length + (Opt) Masking key */
#define COAP_MAX_FS (2 + 8 + 4)
typedef struct coap_ws_state_t {
coap_session_type_t state;
uint8_t up;
uint8_t seen_first;
uint8_t seen_host;
uint8_t seen_upg;
uint8_t seen_conn;
uint8_t seen_key;
uint8_t seen_proto;
uint8_t seen_ver;
uint8_t sent_close;
uint8_t recv_close;
uint16_t close_reason;
int all_hdr_in;
int hdr_ofs;
uint8_t rd_header[COAP_MAX_FS];
uint8_t mask_key[4];
uint32_t http_ofs;
uint8_t http_hdr[160];
size_t data_ofs;
size_t data_size;
uint8_t key[16];
} coap_ws_state_t;
/*
* WebSockets Frame
*
* 0 1 2 3
* 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
* +-+-+-+-+-------+-+-------------+-------------------------------+
* |F|R|R|R| opcode|M| Payload len | Extended payload length |
* |I|S|S|S| (4) |A| (7) | (16/64) |
* |N|V|V|V| |S| | (if payload len==126/127) |
* | |1|2|3| |K| | |
* +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
* | Extended payload length continued, if payload len == 127 |
* + - - - - - - - - - - - - - - - +-------------------------------+
* | |Masking-key, if MASK set to 1 |
* +-------------------------------+-------------------------------+
* | Masking-key (continued) | Payload Data |
* +-------------------------------- - - - - - - - - - - - - - - - +
* : Payload Data continued ... :
* + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
* | Payload Data continued ... |
* +---------------------------------------------------------------+
*/
#define WS_B0_FIN_BIT 0x80
#define WS_B0_RSV_MASK 0x70
#define WS_B0_OP_MASK 0x0f
#define WS_B1_MASK_BIT 0x80
#define WS_B1_LEN_MASK 0x7f
typedef enum {
WS_OP_CONT = 0x0,
WS_OP_TEXT,
WS_OP_BINARY,
WS_OP_CLOSE = 0x8,
WS_OP_PING,
WS_OP_PONG
} coap_ws_opcode_t;
ssize_t coap_ws_write(coap_session_t *session,
const uint8_t *data, size_t datalen);
ssize_t coap_ws_read(coap_session_t *session, uint8_t *data,
size_t datalen);
void coap_ws_establish(coap_session_t *session);
void coap_ws_close(coap_session_t *session);
#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:44
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::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::http_hdr
uint8_t http_hdr[160]
(Partial) HTTP header
Definition coap_ws_internal.h:55
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