NetBurner 3.5.6
PDF Version
CryptoSocket.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5#ifndef _CRYPTO_SOCKET_H_
6#define _CRYPTO_SOCKET_H_
7
8#include <basictypes.h>
9#include <buffers.h>
10#include <netinterface.h>
11#include <iointernal.h>
12
13#define NUM_WOLF_CRYPTO_SOCKETS (16)
14#define NUM_WOLF_CRYPTO_SESSIONS (16)
15#define CRYPTO_BUFFER_SEGMENTS TCP_BUFFER_SEGMENTS
16
17#define CRYPTO_OBJ_FLAG_WNOTIFY_IGNORE (0x0001)
18#define CRYPTO_OBJ_FLAG_USER_CLOSED (0x0002) // User program called close on the SSL/ExtraFd.
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 \
25 (0x0080) // If we fail during negotiation, we need to go ahead and reset the full socket as the user won't get
26 // a valid FD to close, which will result in a socket leak.
27#define CRYPTO_OBJ_FLAG_ERROR_RETURNED \
28 (0x0100) // If we are resetting the socket in the asynch task, we want to make sure we don't do so until the
29 // connection has returned any errors.
30#define CRYPTO_OBJ_FLAG_TCP_RESET (0x0200) // We recieved a reset or abort on the tcp conneciton.
31
32// Time stuff
33#define SHUTDOWN_RETRY_INTERVAL_IMMEDIATE \
34 (1) // Because of our asynchronous connections, we will generally have to try at least twice to completely
35 // shutdown with WolfSSL. However, the time between these calls can be extremely short.
36#define SHUTDOWN_RETRY_INTERVAL (TICKS_PER_SECOND)
37#define CONNECT_RETRY_INTERVAL (TICKS_PER_SECOND)
38
39enum SocketType_t : unsigned char
40{
41 SocketType_Default = 0,
42 SocketType_Tls = 1,
43 SocketType_Ssh = 2,
44 SocketType_UNKNOWN = 0xFF
45};
46
47enum SocketHasData_t : int
48{
49 SocketHasData_NoData = 0,
50 SocketHasData_HasData = 1,
51 SocketHasData_PendingData = 2
52};
53
54class CryptoSocket
55{
56protected:
57 class Pool;
58public:
59 // Member variables
60 Pool *m_socketPool = NULL;
61
62 OS_CRIT m_Crit; // Critical seciton to keep wolf multithread safe
63 int m_error = 0; // The error value
64 int m_options = 0; // Options like push etc ...
65
66 PoolPtr m_NoPushbuffer;
67 fifo_buffer_storage m_dataTcpOverflow; // Used to hold overflow on outbound data to tcp
68
69 // Will either be a WOLFSSL* or WOLFSSH*
70 void *m_wolfCtx = nullptr;
71
72 // Volatile becasue they can be touched from multiple places
73 volatile int m_tcpFd = 0; // The underlyign tcp socket
74 volatile int m_cryptoExtraFd = 0; // The ExtraFd the user code uses to interact with thei socket
75 OS_FLAGS m_OS_Flags; //State flags
76
77 volatile uint32_t m_TimeTickOfNextAction = 0; // Time tick to do some time releated thing
78
79 // This is an index that gets incrmented every time a socket is reset.
80 // This is used to detect race conditions on the worker queue to keep
81 // messages from sockets already closed from getting reused.
82 volatile uint8_t m_IndexOfReuse;
83 volatile uint8_t m_RetryCount;
84
85 CryptoSocket();
86 virtual ~CryptoSocket();
87
88 SocketType_t GetSocketType();
89 int GetPoolNum();
90 bool InUse();
91
92 // Virtual Fuctions
93 // when push gets turned off or the socket gets closed with data to be written we cleanup here
94 void CleanupUnWritten();
95
96 // Called from wolf loop mostly to handle notifications and
97 // time out stuff for all async process except read. Needs to be
98 // defined for each socket type.
99 virtual void ProcessAsyncStuff() = 0;
100
101 virtual int CheckSocketRecv() = 0;
102
103 // Final clean up when we are done usign this socket.
104 virtual void ResetSocket();
105
106 // Called when the underlying TCP socket got closed
107 virtual void CleanupTcpClose();
108
109 // Flag Functions
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);};
117
118 // Options functions
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; }
122
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;
127
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; }
134
135 // Buffer Handling
136 inline fifo_buffer_storage &GetTcpOverflow() { return m_dataTcpOverflow; }
137 inline PoolPtr GetNoPushBuffer() { return m_NoPushbuffer; }
138 inline void SetNewNoPushBuffer() { m_NoPushbuffer = GetBuffer(); }
139 inline bool IsNoPushBufferNull() { return (m_NoPushbuffer == 0); }
140
141 void SetCryptoExtraFd(int cpExFd) { m_cryptoExtraFd = cpExFd; }
142
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);
148
149 protected:
150 void InitSocket(int tcpFd, IoExpandStruct *ioExpStr);
151
152 bool CloseTcpFd(IPADDR &remoteAddr, int &remotePort);
153
154 // Static Member Functions
155
156 // The following should be defined for each class type
157 virtual void WriteUnwrittenData() = 0;
158 virtual bool PendOnHandshake() { return false; }
159
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);
164
165 protected:
166 class Pool {
167
168 Pool *pNextPool;
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;
176 public:
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
188 {
189 if (!Contains(sck))
190 return 0xFFFFFFFFul;
191 return (((uint8_t*)sck)-pPool)/elemSize;
192 }
193 inline const IoExpandStruct * GetIoExpandStruct() const { return pIoExp; }
194
195 bool SocketsAvail() const;
196 int FreeSockets() const;
197 };
198 static CryptoSocket *FindNextEmptySocket(Pool &pool);
199 static Pool *poolList;
200};
201
202inline SocketType_t CryptoSocket::GetSocketType()
203{
204 return (m_socketPool ? m_socketPool->SocketType() : SocketType_UNKNOWN);
205}
206inline int CryptoSocket::GetPoolNum()
207{ return (m_socketPool ? m_socketPool->PoolNum() : -1); }
208
209#endif // _CRYPTO_SOCKET_H_
NetBurner Buffers API.
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
Extra File Descriptors.
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