SharkSSL™ Embedded SSL/TLS Stack
MSLib.h
1 /*
2  * ____ _________ __ _
3  * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4  * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5  * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6  * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7  * /____/
8  *
9  * SharkSSL Embedded SSL/TLS Stack
10  ****************************************************************************
11  * HEADER
12  *
13  * $Id: MSLib.h 4827 2021-08-23 20:44:04Z wini $
14  *
15  * COPYRIGHT: Real Time Logic LLC, 2013 - 2020
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 
44 struct 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  * since it's not really used and the complexity is not something you
53  * want in a tiny device.
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 
132 typedef int (*MSFetchPage)(void* hndl, struct MST* mst,U8* path);
133 
141 typedef struct {
156  const U8* b64Credent;
157 
160  const U8* realm;
161 
176 
179 
186  U8* request;
187 
191  U8* origin;
192 
195  U8* hKeys[MAX_HTTP_H_SIZE];
196 
199  U8* hVals[MAX_HTTP_H_SIZE];
201 
202 
206 typedef 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 */
212  U8* maskPtr;
213  U8 frameHeader[8]; /*[0] FIN+opcode, [1] Payload len, [2-3] Ext payload len*/
214 
217  int frameLen;
218 
222 
226 } WssReadState;
227 
228 
229 /* Minnow Server Transmission Interface
230  */
231 
237 typedef struct {
238  U8* recBuf;
239  U8* sendBuf;
240  U16 recBufSize;
241  U16 sendBufSize;
242 } MSTBuf;
243 
244 
249 typedef struct MST
250 {
251  SOCKET* sock;
252 #ifdef MS_SEC
253  union {
254  SharkSslCon* sc;
255  MSTBuf b;
256  } u;
257 #else
258  MSTBuf b;
259 #endif
260  BaBool isSecure;
261 } MST;
262 
266 
270 
278 int MST_read(MST* o,U8 **buf,U32 timeout);
279 
286 int MST_write(MST* o,U8* buf, int len);
287 
288 
291 typedef struct
292 {
293  WssReadState rs;
294  MST mst;
295 } MS;
296 
297 
298 
299 #ifdef __cplusplus
300 extern "C" {
301 #endif
302 
311 /* Similar to the standard ANSI function strstr, except for that it
312  does not rely on str being a zero terminated string. The compare is
313  also case insensitive.
314 
315  \param str string to search.
316  \param slen str len
317  \param substrs substring to find in str.
318 */
319 
320 U8* msstrstrn(U8* str, int slen, const U8* substr);
321 
329 U8* msCpAndInc(U8* dest, int* dlen, const U8* src, int slen);
330 
331 
339 U8* msB64Encode(U8* dest, int* dlen, const U8* src, int slen);
340 
341 
348 U8* msi2a(U8* dest, int* dlen, U32 n);
349 
350 
363 U8* msRespCT(U8* dest, int* dlen, int contentLen, const U8* extHeader);
364  /* end group MsHelperFunc */
366 
367 
371 #define MS_constructor(o) memset(o,0,sizeof(MS))
372 
382 #define MS_setSharkCon(o, sharkCon, socket) \
383  (o)->mst.u.sc=sharkCon, \
384  (o)->mst.sock=socket, \
385  (o)->mst.isSecure=TRUE
386 
387 #ifdef MS_SEC
388 #define MS_setSocket(o,socket,rec,recSize,send,sendSize) \
389  (o)->mst.sock=socket, \
390  (o)->mst.u.b.recBuf=rec, \
391  (o)->mst.u.b.recBufSize=recSize, \
392  (o)->mst.u.b.sendBuf=send, \
393  (o)->mst.u.b.sendBufSize=sendSize, \
394  (o)->mst.isSecure=FALSE
395 #else
396 
410 #define MS_setSocket(o,socket,rec,recSize,send,sendSize) \
411  (o)->mst.sock=socket, \
412  (o)->mst.b.recBuf=rec, \
413  (o)->mst.b.recBufSize=recSize, \
414  (o)->mst.b.sendBuf=send, \
415  (o)->mst.b.sendBufSize=sendSize, \
416  (o)->mst.isSecure=FALSE
417 #endif
418 
428 U8* MS_respCT(MS* o, int* dlen, int contentLen, const U8* extHeader);
429 
449 
467 U8* MS_prepSend(MS* o, int extSize, int* maxSize);
468 
469 int MS_send(MS* o, U8 opCode, int len);
470 
471 
484 #define MS_sendBin(o, len) MS_send(o, WSOP_Binary, len)
485 
486 
499 #define MS_sendText(o, len) MS_send(o, WSOP_Text, len)
500 
501 
515 int MS_write(MS *o, U8 opCode,const void* data,int len);
516 
529 #define MS_writeBin(o,data,len) MS_write(o,WSOP_Binary,data,len)
530 
543 #define MS_writeText(o,data,len) MS_write(o,WSOP_Text,data,len)
544 
557 int MS_close(MS *o, int statusCode);
558 
581 int MS_read(MS *o,U8 **buf,U32 timeout);
582 
583 #ifdef __cplusplus
584 }
585 #endif
586  /* end group MSLib */
588 
589 #endif
MS_close
int MS_close(MS *o, int statusCode)
Sends a WebSocket close frame command to the peer and close the socket.
msi2a
U8 * msi2a(U8 *dest, int *dlen, U32 n)
Formats 'n' as a string and adds the string to 'dest'.
WssProtocolHandshake::realm
const U8 * realm
In param: Set the HTTP basic authentication's 'realm' name.
Definition: MSLib.h:160
MSFetchPage
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
WssProtocolHandshake::request
U8 * request
Out param: Set to the initial HTTP header request line.
Definition: MSLib.h:186
WssReadState::frameLen
int frameLen
The WebSocket frame length.
Definition: MSLib.h:217
WssReadState::isTimeout
U8 isTimeout
Set when function msRead returns due to a timeout.
Definition: MSLib.h:225
MST_getSendBufPtr
U8 * MST_getSendBufPtr(MST *o)
Get the send buffer pointer.
MST_write
int MST_write(MST *o, U8 *buf, int len)
Write data to a secure layer or non secure (standard socket) layer.
WssProtocolHandshake::fetchPageHndl
void * fetchPageHndl
In param: The MSFetchPage handle, if any.
Definition: MSLib.h:178
SharkSslCon
struct SharkSslCon SharkSslCon
SharkSslCon is an opaque handle returned by function SharkSsl_createCon.
Definition: SharkSSL.h:534
WssProtocolHandshake::origin
U8 * origin
Out param: Set to the HTTP WebSocket header 'origin', if sent by the client.
Definition: MSLib.h:191
MS_webServer
int MS_webServer(MS *o, WssProtocolHandshake *wph)
The Web Server function is responsible for parsing incoming HTTP requests and upgrading HTTP requests...
WssReadState
The WS protocol is frame based, and struct WssReadState keeps state information for the Minnow Server...
Definition: MSLib.h:207
WssProtocolHandshake::b64Credent
const U8 * b64Credent
In param: Enable HTTP basic authentication by setting 'b64Credent' to a B64 encoded string of 'userna...
Definition: MSLib.h:156
MST_getSendBufSize
U16 MST_getSendBufSize(MST *o)
Get the send buffer size.
WssReadState::bytesRead
int bytesRead
Read frame data until: frameLen - bytesRead = 0.
Definition: MSLib.h:221
MST
struct MST MST
The Minnow Server Transmission Class defines a set of functions for reading and writing socket data e...
SOCKET
#define SOCKET
Infinite wait time option for socket read functions.
Definition: selib.h:102
msRespCT
U8 * msRespCT(U8 *dest, int *dlen, int contentLen, const U8 *extHeader)
Adds the following HTTP response to buffer 'dest'.
MST
The Minnow Server Transmission Class defines a set of functions for reading and writing socket data e...
Definition: MSLib.h:250
MS_write
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.
WssProtocolHandshake::fetchPage
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
MS_read
int MS_read(MS *o, U8 **buf, U32 timeout)
Waits for WebSocket frames sent by the peer side.
msB64Encode
U8 * msB64Encode(U8 *dest, int *dlen, const U8 *src, int slen)
Encodes 'src' as B64 and adds the string to 'dest'.
MSTBuf
The Minnow Server Transmission Buffer is used by the Minnow Server Transmission (MST) class when used...
Definition: MSLib.h:237
WssProtocolHandshake
The WssProtocolHandshake structure keeps state information for the web server function MS_ebServer.
Definition: MSLib.h:141
msCpAndInc
U8 * msCpAndInc(U8 *dest, int *dlen, const U8 *src, int slen)
Copies 'src' to 'dest'.
MS_respCT
U8 * MS_respCT(MS *o, int *dlen, int contentLen, const U8 *extHeader)
Format a HTTP 200 OK response with Content Length.
MS
MS: Minnow Server HTTP(S) and (secure) WebSocket Server.
Definition: MSLib.h:292
MST_read
int MST_read(MST *o, U8 **buf, U32 timeout)
Write data to a secure layer or non secure (standard socket) layer.
MS_prepSend
U8 * MS_prepSend(MS *o, int extSize, int *maxSize)
Prepare sending a WebSocket frame using one of MS_sendBin or MS_sendText.