Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
selib.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * SharkSSL Embedded SSL/TLS Stack
10 ****************************************************************************
11 * PROGRAM MODULE
12 *
13 * $Id: selib.h 6686 2026-10-29 23:59:57Z wini $
14 *
15 * COPYRIGHT: Real Time Logic LLC, 2014 - 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 * https://realtimelogic.com
33 ****************************************************************************
34 *
35 */
36
37#ifndef _selib_h
38#define _selib_h
39
40/*
41 The SharkSSL compatibility API makes it easy to write code that can
42 later be upgraded to a secure version, if needed.
43*/
44#ifndef SHARKSSL_COMPAT
45#define SHARKSSL_COMPAT 1
46#endif
47
48#ifndef SE_SHA1
49#define SE_SHA1 1
50#endif
51#ifndef SE_SHA256
52#define SE_SHA256 1
53#endif
54
55
60#ifndef INTEGRAL_TYPES
61#define INTEGRAL_TYPES
62#ifndef XTYPES
63#include <stdint.h>
64typedef uint8_t U8;
65#ifndef __MBED__
66typedef int8_t S8;
67#endif
68typedef uint16_t U16;
69typedef int16_t S16;
70typedef uint32_t U32;
71typedef int32_t S32;
72typedef uint64_t U64;
73typedef int64_t S64;
74#endif
75#endif
76
77#ifndef BaBool
78#define BaBool U8
79#endif
80
83#define INFINITE_TMO (~((U32)0))
84
85#include "selibplat.h"
86
87#ifndef SE_CTX
88#define SeCtx void
89#endif
90
91#ifndef XPRINTF
92#define XPRINTF 0
93#endif
94
95#ifndef TRUE
96#define TRUE 1
97#endif
98
99#ifndef FALSE
100#define FALSE 0
101#endif
102
103#ifndef baAssert
104#ifdef NDEBUG
105#define baAssert(x)
106#else
107#ifdef PRINT_ASSERT
108#define baAssert(x) if(x) xprintf(("failed assertion %s %d\n",__FILE__, __LINE__))
109#else
110#include <assert.h>
111#define baAssert assert
112#endif
113#endif
114#endif
115
116#include <string.h>
117#if XPRINTF
118#include <stdarg.h>
119#endif
120
121#ifdef UMM_MALLOC
122#include <umm_malloc.h>
123#define baMalloc(s) umm_malloc(s)
124#define baRealloc(m, s) umm_realloc(m, s)
125#define baFree(m) umm_free(m)
126#endif
127
128#ifndef NO_BSD_SOCK
134#ifndef SOCKET
135#define SOCKET int
136#endif
137#endif
138
139#ifndef SE_CTX
140#define SeCtx void
141#endif
142
143#ifndef NO_BSD_SOCK
144#ifndef INVALID_SOCKET
145#define INVALID_SOCKET ((SOCKET)-1)
146#endif
147#endif
148#ifndef SOCKET_constructor
149#ifdef NO_BSD_SOCK
150#define SOCKET_constructor(o, ctx) ((void)(ctx), memset((o), 0, sizeof(SOCKET)))
151#else
152#define SOCKET_constructor(o, ctx) ((void)(ctx), (*(o) = INVALID_SOCKET))
153#endif
154#endif
155
156
157#ifdef __cplusplus
158extern "C" {
159#endif
160
172int se_connect(SOCKET* sock, const char* address, U16 port);
173
184int se_bind(SOCKET* sock, U16 port);
185
198int se_accept(SOCKET** listenSock, U32 timeout, SOCKET** outSock);
199
203void se_close(SOCKET* sock);
204
211
218S32 se_send(SOCKET* sock, const void* buf, U32 len);
219
229S32 se_recv(SOCKET* sock, void* buf, U32 len, U32 timeout);
230
231/* Macro function designed for IPv4
232 sock: a pointer to SOCKET
233 buf: a buf large enough to hold 4 bytes
234 status: int pointer: out value is negative on error and 4 (len) on success
235*/
236#ifndef se_getSockName
237#define se_getSockName(sock, buf, status) do { \
238 struct sockaddr_in in; int size=sizeof(struct sockaddr_in); \
239 *(status) = getsockname(*(sock), (struct sockaddr *)&in, &size); \
240 memcpy((buf), &in.sin_addr.s_addr, 4); \
241 if(*(status) == 0) *(status) = 4; \
242} while(0)
243#endif
244
245
246#if XPRINTF == 1
252#define xprintf(data) _xprintf data
258#ifndef _xprintf /* to handle #define _xprintf printf */
259void _xprintf(const char* fmt, ...);
260#endif
261#else
262#ifndef xprintf
263#define xprintf(data)
264#endif
265#endif
266
270void mainTask(SeCtx* ctx);
271
272#ifdef __cplusplus
273}
274#endif
275
276
277#if SHARKSSL_COMPAT
278typedef struct {void* x; } SharkSsl;
279#define SharkSsl_constructor(sharkSsl, type, cache, inSize,outSize)
280#define SharkSsl_setCAList(sharkSsl, sharkSslCAList)
281#define SharkSsl_setCAListEx(sharkSsl, sharkSslCAList, caListLen) 1
282#define sharkssl_entropy(entropy)
283#define SharkSsl_createCon(sharkSsl) ((void*)~0)
284#define SharkSsl_terminateCon(sharkSsl, scon)
285#define SharkSsl_destructor(sharkSsl)
286#define SharkSsl_addCertificate(sharkSsl, cert)
287
288#define SharkSslCon void
289
290#if SE_SHA256
291/* Standalone compatibility implementation; set SE_SHA256=0 when unused. */
292#define SHARKSSL_SHA256_HASH_LEN 32
293typedef struct SharkSslSha256Ctx
294{
295 U32 total[2];
296 U32 state[8];
297 U8 buffer[64];
298} SharkSslSha256Ctx;
299#ifdef __cplusplus
300extern "C" {
301#endif
302int seSha256Ctx_constructor(SharkSslSha256Ctx* ctx);
303int seSha256Ctx_append(SharkSslSha256Ctx* ctx, const U8* data, U32 len);
304int seSha256Ctx_finish(SharkSslSha256Ctx* ctx, U8 digest[SHARKSSL_SHA256_HASH_LEN]);
305#define SharkSslSha256Ctx_constructor seSha256Ctx_constructor
306#define SharkSslSha256Ctx_append seSha256Ctx_append
307#define SharkSslSha256Ctx_finish seSha256Ctx_finish
308#ifdef __cplusplus
309}
310#endif
311#endif
312
313#if SE_SHA1
314#define SHARKSSL_SHA1_HASH_LEN 20
315typedef struct SharkSslSha1Ctx
316{
317 U32 total[2];
318 U32 state[5];
319 U8 buffer[64];
320} SharkSslSha1Ctx;
321void SharkSslSha1Ctx_constructor(SharkSslSha1Ctx* ctx);
322void SharkSslSha1Ctx_append(SharkSslSha1Ctx* ctx, const U8* data, U32 len);
323void SharkSslSha1Ctx_finish(SharkSslSha1Ctx*,U8 digest[SHARKSSL_SHA1_HASH_LEN]);
324#endif
325
326
327
328#endif
329
330 /* end group selib */
332
333#endif
uint16_t U16
Unsigned 16-bit integer.
Definition: GenPrimT.h:94
int64_t S64
Signed 64-bit integer.
Definition: GenPrimT.h:90
uint64_t U64
Unsigned 64-bit integer.
Definition: GenPrimT.h:98
int32_t S32
Signed 32-bit integer.
Definition: GenPrimT.h:88
uint32_t U32
Unsigned 32-bit integer.
Definition: GenPrimT.h:96
int16_t S16
Signed 16-bit integer.
Definition: GenPrimT.h:86
uint8_t U8
Unsigned 8-bit integer.
Definition: GenPrimT.h:92
int8_t S8
Signed 8-bit integer.
Definition: GenPrimT.h:84
void se_close(SOCKET *sock)
Close a connected socket connection.
int se_accept(SOCKET **listenSock, U32 timeout, SOCKET **outSock)
Waits for remote connections on the server SOCKET object 'listenSock', initialized by function se_bin...
S32 se_send(SOCKET *sock, const void *buf, U32 len)
Sends data to the connected peer.
int se_connect(SOCKET *sock, const char *address, U16 port)
Initializes a SOCKET object connected to a remote host/address at a given port.
int se_sockValid(SOCKET *sock)
Check the socket representation using the selected port.
void mainTask(SeCtx *ctx)
Main entry for all example programs.
int se_bind(SOCKET *sock, U16 port)
Initializes a SOCKET object bound to a local port, ready to accept client connections.
S32 se_recv(SOCKET *sock, void *buf, U32 len, U32 timeout)
Waits for data sent by peer.
#define SOCKET
The SOCKET object/handle is an 'int' when using a BSD compatible TCP/IP stack.
Definition: selib.h:135
SeCtx structure: See Context Manager and Bare Metal Systems for details.
Definition: SeCtx.h:75