5#ifndef _CRYPTO_SOCKET_H_
6#define _CRYPTO_SOCKET_H_
13#define NUM_WOLF_CRYPTO_SOCKETS (16)
14#define NUM_WOLF_CRYPTO_SESSIONS (16)
15#define CRYPTO_BUFFER_SEGMENTS TCP_BUFFER_SEGMENTS
17#define CRYPTO_OBJ_FLAG_WNOTIFY_IGNORE (0x0001)
18#define CRYPTO_OBJ_FLAG_USER_CLOSED (0x0002)
19#define CRYPTO_OBJ_FLAG_DOINGSHUTDOWN (0x0004)
20#define CRYPTO_OBJ_FLAG_DOINGCONNECT (0x0008)
21#define CRYPTO_OBJ_FLAG_DOINGACCEPT (0x0010)
22#define CRYPTO_OBJ_FLAG_HAVEERROR (0x0020)
23#define CRYPTO_OBJ_FLAG_OTHER_SIDE_CLOSED (0x0040)
24#define CRYPTO_OBJ_FLAG_RESET_SOCKET \
27#define CRYPTO_OBJ_FLAG_ERROR_RETURNED \
30#define CRYPTO_OBJ_FLAG_TCP_RESET (0x0200)
33#define SHUTDOWN_RETRY_INTERVAL_IMMEDIATE \
36#define SHUTDOWN_RETRY_INTERVAL (TICKS_PER_SECOND)
37#define CONNECT_RETRY_INTERVAL (TICKS_PER_SECOND)
39enum SocketType_t :
unsigned char
41 SocketType_Default = 0,
44 SocketType_UNKNOWN = 0xFF
47enum SocketHasData_t :
int
49 SocketHasData_NoData = 0,
50 SocketHasData_HasData = 1,
51 SocketHasData_PendingData = 2
60 Pool *m_socketPool = NULL;
70 void *m_wolfCtx =
nullptr;
73 volatile int m_tcpFd = 0;
74 volatile int m_cryptoExtraFd = 0;
77 volatile uint32_t m_TimeTickOfNextAction = 0;
82 volatile uint8_t m_IndexOfReuse;
83 volatile uint8_t m_RetryCount;
86 virtual ~CryptoSocket();
88 SocketType_t GetSocketType();
94 void CleanupUnWritten();
99 virtual void ProcessAsyncStuff() = 0;
101 virtual int CheckSocketRecv() = 0;
104 virtual void ResetSocket();
107 virtual void CleanupTcpClose();
110 inline void SetFlag(uint32_t flag) { m_OS_Flags.
Set(flag); }
111 inline void ClrFlag(uint32_t flag) { m_OS_Flags.
Clear(flag); }
112 inline bool GetFlag(uint32_t flag) {
return (m_OS_Flags.
State()&flag)!=0;}
113 inline uint32_t GetFlags() {
return m_OS_Flags.
State();}
114 inline void ClrAllFlags() { m_OS_Flags.
Clear(0xFFFFFFFF); }
115 inline bool PendFlagUntil(uint32_t flag, uint32_t Time) {
return (m_OS_Flags.
PendAnyUntil(flag,Time)!=
OS_TIMEOUT);}
116 inline bool PendFlagDly(uint32_t flag, uint32_t Delay){
return (m_OS_Flags.
PendAny(flag,Delay)!=
OS_TIMEOUT);};
119 inline int GetOptions() {
return m_options; }
120 inline void SetOptions(
int option) { m_options |= option; }
121 inline void ClrOptions(
int option) { m_options &= ~option; }
123 uint32_t GetCheckableIndexFromSocket();
124 virtual uint32_t SocketRead(
char *buf, uint32_t len) = 0;
125 virtual uint32_t SocketWrite(
const char *buf, uint32_t len) = 0;
126 virtual SocketHasData_t SocketHasData() = 0;
128 inline int GetTcpFd() {
return m_tcpFd; }
129 inline int GetCryptoExtraFd() {
return m_cryptoExtraFd; }
130 inline int GetError() {
return m_error; }
131 inline void SetError(
int err) { m_error = err; }
132 inline uint32_t GetTimeOfNextAction() {
return m_TimeTickOfNextAction; }
133 inline OS_CRIT &GetSockCrit() {
return m_Crit; }
137 inline PoolPtr GetNoPushBuffer() {
return m_NoPushbuffer; }
138 inline void SetNewNoPushBuffer() { m_NoPushbuffer =
GetBuffer(); }
139 inline bool IsNoPushBufferNull() {
return (m_NoPushbuffer == 0); }
141 void SetCryptoExtraFd(
int cpExFd) { m_cryptoExtraFd = cpExFd; }
143 static CryptoSocket *GetSocketFromCheckableIndex(uint32_t index);
144 static CryptoSocket *GetSocketFromTcpFd(
int tcpFd);
145 static CryptoSocket *GetSocketFromCryptoFd(
int cryptoFd,
const Pool *usePool = NULL);
146 static bool IsCryptoSocket(
int cryptoFd,
const Pool *usePool = NULL);
147 static void ProcessSockets(uint32_t &lowest_next);
150 void InitSocket(
int tcpFd, IoExpandStruct *ioExpStr);
152 bool CloseTcpFd(
IPADDR &remoteAddr,
int &remotePort);
157 virtual void WriteUnwrittenData() = 0;
158 virtual bool PendOnHandshake() {
return false; }
160 void ClearNextActionTimeTick() { m_TimeTickOfNextAction = 0; };
161 void SetNextActionTimeTick(uint32_t time);
162 void SetSocketError(
int error,
bool setTcpClose,
bool resetSocket);
163 void ResetSequence(
bool success);
169 const uint8_t *pPool;
170 const size_t poolCount;
171 const size_t elemSize;
172 const size_t poolNum;
173 const IoExpandStruct *pIoExp;
174 const SocketType_t type;
175 static size_t nextPoolNum;
177 Pool(CryptoSocket *pool,
size_t sockCount,
size_t sockSize, SocketType_t sockType, IoExpandStruct *iox);
178 inline CryptoSocket &GetSock(
int idx)
const {
return *((CryptoSocket*)(pPool+idx*elemSize)); }
179 inline CryptoSocket &operator[](
int idx)
const {
return GetSock(idx); }
180 inline CryptoSocket *operator+(
int idx)
const {
return ((CryptoSocket*)(pPool+idx*elemSize)); }
181 inline size_t Size()
const {
return poolCount; }
182 inline size_t PoolNum()
const {
return poolNum; }
183 inline SocketType_t SocketType()
const {
return type; }
184 inline const Pool *GetNext()
const {
return pNextPool; }
185 inline bool Contains(
const CryptoSocket *sck)
const
186 {
return (((uint8_t*)sck) >= pPool) && (((uint8_t*)sck) < (pPool+poolCount*elemSize)); }
187 inline uint32_t SockIdx(CryptoSocket *sck)
const
191 return (((uint8_t*)sck)-pPool)/elemSize;
193 inline const IoExpandStruct * GetIoExpandStruct()
const {
return pIoExp; }
195 bool SocketsAvail()
const;
196 int FreeSockets()
const;
198 static CryptoSocket *FindNextEmptySocket(Pool &pool);
199 static Pool *poolList;
202inline SocketType_t CryptoSocket::GetSocketType()
204 return (m_socketPool ? m_socketPool->SocketType() : SocketType_UNKNOWN);
206inline int CryptoSocket::GetPoolNum()
207{
return (m_socketPool ? m_socketPool->PoolNum() : -1); }
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
FIFO buffer storage using linked pool buffers.
Definition buffers.h:443
PoolPtr GetBuffer()
Allocate a software buffer for general use.
#define OS_TIMEOUT
Timeout.
Definition nbrtos.h:64
NetBurner Network Interface Header File.
An OS_CRIT object is used to establish critical sections of code that can only be run by one task at ...
Definition nbrtos.h:1110
OSFlags enables a function or task to pend on multiple flags or events.
Definition nbrtos.h:1289
uint8_t PendAny(uint32_t bit_mask, uint16_t timeout=WAIT_FOREVER)
Wait the specified number of system TimeTicks for any of the flags in the bit mask to be set.
Definition nbrtos.h:1363
uint32_t State()
Returns the current values of the flags stored in the OS_FLAGS object.
void Set(uint32_t bits_to_set)
Sets the specified flag bits.
void Clear(uint32_t bits_to_clr)
Clear the specified flag bits.
uint8_t PendAnyUntil(uint32_t bit_mask, uint32_t end_time)
Wait until the specified system TimeTicks value for any of the flags in the bit mask to be set.
Definition nbrtos.h:1393
Main buffer structure for network and serial communication.
Definition buffers.h:90