SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
MSLib.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * SharkSSL Embedded SSL/TLS Stack
10 ****************************************************************************
11 * HEADER
12 *
13 * $Id: MSLib.h 6673 2026-10-29 16:58:52Z wini $
14 *
15 * COPYRIGHT: Real Time Logic LLC, 2013 - 2026
16 *
17 * This software is copyrighted by and is the sole property of Real
18 * Time Logic LLC. All rights, title, ownership, or other interests in
19 * the software remain the property of Real Time Logic LLC. This
20 * software may only be used in accordance with the terms and
21 * conditions stipulated in the corresponding license agreement under
22 * which the software has been supplied. Any unauthorized use,
23 * duplication, transmission, distribution, or disclosure of this
24 * software is expressly forbidden.
25 *
26 * This Copyright notice may not be removed or modified without prior
27 * written consent of Real Time Logic LLC.
28 *
29 * Real Time Logic LLC. reserves the right to modify this software
30 * without notice.
31 *
32 * http://sharkssl.com
33 ****************************************************************************
34 *
35 *
36 * Minnow Server: SharkSSL WebSocket Server
37 */
38
39#ifndef _MSLib_h
40#define _MSLib_h
41
42#include <selib.h>
43
44struct MST;
45
50/* RFC6455 Page 29: Opcode: 4 bits.
51 * WS Opcodes with FIN=1. We do not manage WS fragments (FIN=0/1)
52 * to keep the implementation small. Payloads are limited to 65535 bytes;
53 * UTF-8 is not validated. Ordinary stream reads may split a frame.
54 */
55#define WSOP_Text 0x81
56#define WSOP_Binary 0x82
57/* RFC6455 5.5. Control Frames */
58#define WSOP_Close 0x88
59#define WSOP_Ping 0x89
60#define WSOP_Pong 0x8A
61
62
75#define MS_ERR_ALLOC -10
76
80#define MS_ERR_AUTHENTICATION -11
81
86#define MS_ERR_HTTP_HEADER_OVERFLOW -12
87
89#define MS_ERR_INVALID_HTTP -13
90
92#define MS_ERR_NOT_WEBSOCKET -14
93
95#define MS_ERR_READ -15
96
98#define MS_ERR_READ_TMO -16
99
101#define MS_ERR_SSL_HANDSHAKE -17
102
104#define MS_ERR_WRITE -18
105
107#define MS_ERR_ENCRYPTED_ZIP -30
108
110#define MS_ERR_FILE_IO -31
111
115#define MS_ERR_BUF_OVERFLOW -40
116
120#define MS_ERR_BUF_UNDERFLOW -41
121 /* end group MSLibErrCodes */
123
124
125#define MAX_HTTP_H_SIZE 20
126
132typedef int (*MSFetchPage)(void* hndl, struct MST* mst,U8* path);
133
141typedef struct {
156 const U8* b64Credent;
157
160 const U8* realm;
161
176
179
187
192
195 U8* hKeys[MAX_HTTP_H_SIZE];
196
199 U8* hVals[MAX_HTTP_H_SIZE];
201
202
206typedef struct
207{
208 /* Private members */
209 U8* overflowPtr; /* Set if: consumed more data from stream than frame len */
210 int overflowLen; /* overflowPtr len is used internally in wsRawRead */
211 int frameHeaderIx; /* Cursor used when reading frameHeader from socket */
213 U8 frameHeader[8]; /*[0] FIN+opcode, [1] Payload len, [2-3] Ext payload len*/
214
218
222
227
228
229/* Minnow Server Transmission Interface
230 */
231
237typedef struct {
242} MSTBuf;
243
244
249typedef struct MST
250{
252#ifdef MS_SEC
253 union {
254 SharkSslCon* sc;
255 MSTBuf b;
256 } u;
257#else
259#endif
260 BaBool isSecure;
262
266
270
276int MST_read(MST* o,U8 **buf,U32 timeout);
277
283int MST_write(MST* o,U8* buf, int len);
284
285
288typedef struct
289{
292} MS;
293
294
295
296#ifdef __cplusplus
297extern "C" {
298#endif
299
308/* Similar to the standard ANSI function strstr, except for that it
309 does not rely on str being a zero terminated string. The compare is
310 also case insensitive.
311
312 \param str string to search.
313 \param slen str len
314 \param substrs substring to find in str.
315*/
316
317U8* msstrstrn(U8* str, int slen, const U8* substr);
318
326U8* msCpAndInc(U8* dest, int* dlen, const U8* src, int slen);
327
328
336U8* msB64Encode(U8* dest, int* dlen, const U8* src, int slen);
337
338
345U8* msi2a(U8* dest, int* dlen, U32 n);
346
347
360U8* msRespCT(U8* dest, int* dlen, int contentLen, const U8* extHeader);
361 /* end group MsHelperFunc */
363
364
368#define MS_constructor(o) memset(o,0,sizeof(MS))
369
379#define MS_setSharkCon(o, sharkCon, socket) \
380 (o)->mst.u.sc=sharkCon, \
381 (o)->mst.sock=socket, \
382 (o)->mst.isSecure=TRUE
383
384#ifdef MS_SEC
385#define MS_setSocket(o,socket,rec,recSize,send,sendSize) \
386 (o)->mst.sock=socket, \
387 (o)->mst.u.b.recBuf=rec, \
388 (o)->mst.u.b.recBufSize=recSize, \
389 (o)->mst.u.b.sendBuf=send, \
390 (o)->mst.u.b.sendBufSize=sendSize, \
391 (o)->mst.isSecure=FALSE
392#else
393
407#define MS_setSocket(o,socket,rec,recSize,send,sendSize) \
408 (o)->mst.sock=socket, \
409 (o)->mst.b.recBuf=rec, \
410 (o)->mst.b.recBufSize=recSize, \
411 (o)->mst.b.sendBuf=send, \
412 (o)->mst.b.sendBufSize=sendSize, \
413 (o)->mst.isSecure=FALSE
414#endif
415
425U8* MS_respCT(MS* o, int* dlen, int contentLen, const U8* extHeader);
426
451
469U8* MS_prepSend(MS* o, int extSize, int* maxSize);
470
471int MS_send(MS* o, U8 opCode, int len);
472
473
486#define MS_sendBin(o, len) MS_send(o, WSOP_Binary, len)
487
488
501#define MS_sendText(o, len) MS_send(o, WSOP_Text, len)
502
503
517int MS_write(MS *o, U8 opCode,const void* data,int len);
518
531#define MS_writeBin(o,data,len) MS_write(o,WSOP_Binary,data,len)
532
545#define MS_writeText(o,data,len) MS_write(o,WSOP_Text,data,len)
546
559int MS_close(MS *o, int statusCode);
560
585int MS_read(MS *o,U8 **buf,U32 timeout);
586
587#ifdef __cplusplus
588}
589#endif
590 /* end group MSLib */
592
593#endif
U8 * MS_prepSend(MS *o, int extSize, int *maxSize)
Prepare sending a WebSocket frame using one of MS_sendBin or MS_sendText.
U16 MST_getSendBufSize(MST *o)
Get the send buffer size.
struct MST MST
The Minnow Server Transmission Class defines a set of functions for reading and writing socket data e...
int MS_close(MS *o, int statusCode)
Sends a WebSocket close frame command to the peer and close the socket.
int MS_send(MS *o, U8 opCode, int len)
int MS_webServer(MS *o, WssProtocolHandshake *wph)
The Web Server function is responsible for parsing incoming HTTP requests and upgrading HTTP requests...
int MST_read(MST *o, U8 **buf, U32 timeout)
Read data to a secure layer or non secure (standard socket) layer.
U8 * MS_respCT(MS *o, int *dlen, int contentLen, const U8 *extHeader)
Format a HTTP 200 OK response with Content Length.
int MS_read(MS *o, U8 **buf, U32 timeout)
Waits for WebSocket frames sent by the peer side.
int MST_write(MST *o, U8 *buf, int len)
Write data to a secure layer or non secure (standard socket) layer.
int(* MSFetchPage)(void *hndl, struct MST *mst, U8 *path)
A page fetch callback function used by function MS_ebServer is typically installed when the web appli...
Definition: MSLib.h:132
U8 * MST_getSendBufPtr(MST *o)
Get the send buffer pointer.
#define MAX_HTTP_H_SIZE
Definition: MSLib.h:125
int MS_write(MS *o, U8 opCode, const void *data, int len)
Function used by the two inline functions (macros) MS_writeBin and MS_writeText.
U8 * msi2a(U8 *dest, int *dlen, U32 n)
Formats 'n' as a string and adds the string to 'dest'.
U8 * msstrstrn(U8 *str, int slen, const U8 *substr)
U8 * msRespCT(U8 *dest, int *dlen, int contentLen, const U8 *extHeader)
Adds the following HTTP response to buffer 'dest'.
U8 * msB64Encode(U8 *dest, int *dlen, const U8 *src, int slen)
Encodes 'src' as B64 and adds the string to 'dest'.
U8 * msCpAndInc(U8 *dest, int *dlen, const U8 *src, int slen)
Copies 'src' to 'dest'.
struct SharkSslCon SharkSslCon
SharkSslCon is an opaque handle returned by function SharkSsl_createCon.
Definition: SharkSSL.h:496
#define SOCKET
Infinite wait time option for socket read functions.
Definition: selib.h:103
The Minnow Server Transmission Buffer is used by the Minnow Server Transmission (MST) class when used...
Definition: MSLib.h:237
U8 * recBuf
Definition: MSLib.h:238
U16 recBufSize
Definition: MSLib.h:240
U8 * sendBuf
Definition: MSLib.h:239
U16 sendBufSize
Definition: MSLib.h:241
The Minnow Server Transmission Class defines a set of functions for reading and writing socket data e...
Definition: MSLib.h:250
SOCKET * sock
Definition: MSLib.h:251
MSTBuf b
Definition: MSLib.h:258
BaBool isSecure
Definition: MSLib.h:260
MS: Minnow Server HTTP(S) and (secure) WebSocket Server.
Definition: MSLib.h:289
MST mst
Definition: MSLib.h:291
WssReadState rs
Definition: MSLib.h:290
The WssProtocolHandshake structure keeps state information for the web server function MS_ebServer.
Definition: MSLib.h:141
U8 * origin
Out param: Set to the HTTP WebSocket header 'origin', if sent by the client.
Definition: MSLib.h:191
MSFetchPage fetchPage
In param: set the page fetch callback function if you plan on storing the web application in the devi...
Definition: MSLib.h:175
U8 * request
Out param: Set to the initial HTTP header request line.
Definition: MSLib.h:186
void * fetchPageHndl
In param: The MSFetchPage handle, if any.
Definition: MSLib.h:178
const U8 * b64Credent
In param: Enable HTTP basic authentication by setting 'b64Credent' to a B64 encoded string of 'userna...
Definition: MSLib.h:156
const U8 * realm
In param: Set the HTTP basic authentication's 'realm' name.
Definition: MSLib.h:160
The WS protocol is frame based, and struct WssReadState keeps state information for the Minnow Server...
Definition: MSLib.h:207
U8 * overflowPtr
Definition: MSLib.h:209
int frameLen
The WebSocket frame length.
Definition: MSLib.h:217
int overflowLen
Definition: MSLib.h:210
U8 * maskPtr
Definition: MSLib.h:212
int frameHeaderIx
Definition: MSLib.h:211
int bytesRead
Read frame data until: frameLen - bytesRead = 0.
Definition: MSLib.h:221
U8 isTimeout
Set when function msRead returns due to a timeout.
Definition: MSLib.h:225