Barracuda Application Server C/C++ Reference
NO
SoDispCon.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Embedded Web-Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: SoDispCon.h 5387 2023-02-20 22:50:13Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2006 - 2023
17 *
18 * This software is copyrighted by and is the sole property of Real
19 * Time Logic LLC. All rights, title, ownership, or other interests in
20 * the software remain the property of Real Time Logic LLC. This
21 * software may only be used in accordance with the terms and
22 * conditions stipulated in the corresponding license agreement under
23 * which the software has been supplied. Any unauthorized use,
24 * duplication, transmission, distribution, or disclosure of this
25 * software is expressly forbidden.
26 *
27 * This Copyright notice may not be removed or modified without prior
28 * written consent of Real Time Logic LLC.
29 *
30 * Real Time Logic LLC. reserves the right to modify this software
31 * without notice.
32 *
33 * http://www.realtimelogic.com
34 ****************************************************************************
35 *
36 *
37 */
38
39#ifndef __SoDispCon_h
40#define __SoDispCon_h
41
42#include <ThreadLib.h>
43#include <SoDisp.h>
44
45struct SharkSslCon;
46
47/*===========================================================================
48 *
49 * SoDispCon
50 *---------------------------------------------------------------------------
51 */
52
53
54
55/* SoDispCon_ExType -> SoDispCon Execute Type.
56 The callback function SoDispCon_Execute in SoDispCon is
57 like having 6 callback functions. SoDispCon_ExType specifies the
58 current callback type.
59 This construction preserves memory as we have 1 callback function pointer
60 instead of 5.
61
62 ExStateRead: read socket data
63 ExStateWrite: write socket data
64 GetSharkSslCon: return the internal SharkSsslCon object
65 ExStateClose: Close connection.
66 ExStateMoveCon: Moving the active connection to another SoDispCon obj.
67 ExStateDispEv: The socket dispatcher signals that data is ready.
68*/
69typedef enum {
70 SoDispCon_ExTypeRead=1,
71 SoDispCon_ExTypeWrite,
72 SoDispCon_GetSharkSslCon,
73 SoDispCon_ExTypeClose,
74 SoDispCon_ExTypeMoveCon,
75 SoDispCon_ExTypeAllocAsynchBuf,
76 SoDispCon_ExTypeAsyncReady,
77 SoDispCon_ExTypeIdle
78} SoDispCon_ExType;
79
80
81/*
82The arguments to the SoDispCon_Execute callback function:
83 SoDispCon_ExTypeRead:
84 d1 pointer to data, d2 length of data, ret #of bytes read, < 0 if failed
85 or 0 if no data.
86 SoDispCon_ExTypeWrite:
87 d1 pointer to data, d2 length, ret < 0 if failed or #of bytes written.
88 SoDispCon_GetSharkSslCon:
89 d1 is pointer pointer to SharkSslCon, d2 not used, ret TRUE of FALSE.
90 SoDispCon_ExTypeClose:
91 d1 not used, d2 not used, ret is void.
92 SoDispCon_ExTypeMoveCon:
93 d1 is a pointer to a SoDispCon object.
94*/
95
96struct SoDispCon;
97struct SharkSsl;
98
99typedef int (*SoDispCon_Execute)(
100 struct SoDispCon* con, ThreadMutex* m,SoDispCon_ExType s,void* d1,int d2);
101
102
103typedef void (*SoDispCon_DispRecEv)(struct SoDispCon* con);
104typedef void (*SoDispCon_DispSendEv)(struct SoDispCon* con);
105
106
111typedef struct SoDispCon
112{
113#ifdef __cplusplus
114 void *operator new(size_t, void *place) { return place; }
115 void operator delete(void*, void *) { }
116 SoDispCon() {}
117
118
132 int connect(const char* host,
133 U16 port,
134 const void* bindIntfName=0,
135 U16 bindPort=0,
136 U32 timeout=1500,
137 BaBool dgram=false, BaBool ipv6=false,
138 char** errinfo=0);
139
142 bool isSecure();
143
147 bool getSharkSslCon(SharkSslCon** sc);
148
149
152 bool isValid();
153
155 bool isIP6();
156
160 int getPeerName(HttpSockaddr* addr,U16* port=0);
161
165 int getSockName(HttpSockaddr* addr,U16* port=0);
166
169 char* addr2String(HttpSockaddr* addr, char* buf, int len);
170
173 bool cmpAddr(HttpSockaddr* addr2);
174
180 void setTCPNoDelay(bool enable);
181
184 struct SoDisp* getDispatcher();
185
188 bool hasMoreData();
189
192 bool dispatcherHasCon();
193
197 bool recEvActive();
198
201 bool sendEvActive();
202
203 void setDispSendEvent(SoDispCon_DispSendEv ev);
204 void setDispRecEvent(SoDispCon_DispRecEv ev);
205 int readData(void* data, int len, bool relmutex=false);
206 int setNonblocking();
207 int setBlocking();
208 int sendData(const void* data, int len);
209 int asyncSend(int len);
210 int sendChunkData(const void* data, int len);
211 int asyncReady();
212 void* allocAsynchBuf(int* size);
213#endif
214 CONNECTION_DISPATCHER_OBJ /*Owned and handled by the SoDisp obj.*/
215
216 /* Owned and handled by SSL. */
217 void* sslData;
218 SoDispCon_Execute exec;
219
220 HttpSocket httpSocket;
221 SoDispCon_DispRecEv dispRecEv;
222 SoDispCon_DispSendEv dispSendEv;
223 struct SoDisp* dispatcher;
224 BaBool* sendTermPtr;
225 BaBool* recTermPtr;
226 U16 rtmo; /* Read timeout in 50 millisec intervals */
227 U8 dataBits;
228 U8 isSending;
229} SoDispCon;
230
231#define SoDispCon_socketHasNonBlockDataBitMask 0x01
232#define SoDispCon_hasMoreDataDataBitMask 0x02
233
234#define SoDispCon_dispatcherHasConDataBitMask 0x04
235#define SoDispCon_recEvActiveDataBitMask 0x08
236#define SoDispCon_sendEvActiveDataBitMask 0x10
237#define SoDispCon_isNonBlockingDataBitMask 0x20
238#define SoDispCon_IP6DataBitMask 0x40
239#define SoDispCon_DGramBitMask 0x80
240
241#define SoDispCon_setDispHasRecData(o)\
242 ((o)->dataBits |= \
243 (SoDispCon_hasMoreDataDataBitMask | SoDispCon_socketHasNonBlockDataBitMask))
244#define SoDispCon_setDispatcherHasCon(o)\
245 ((o)->dataBits |= SoDispCon_dispatcherHasConDataBitMask)
246#define SoDispCon_setRecEvActive(o)\
247 ((o)->dataBits |= SoDispCon_recEvActiveDataBitMask)
248#define SoDispCon_setSendEvActive(o)\
249 ((o)->dataBits |= SoDispCon_sendEvActiveDataBitMask)
250#define SoDispCon_setIP6(o)\
251 ((o)->dataBits |= SoDispCon_IP6DataBitMask)
252
253#define SoDispCon_clearSocketHasNonBlockData(o)\
254 ((o)->dataBits &= ~(U8)SoDispCon_socketHasNonBlockDataBitMask)
255#define SoDispCon_clearHasMoreData(o)\
256 ((o)->dataBits &= ~(U8)SoDispCon_hasMoreDataDataBitMask)
257#define SoDispCon_clearDispatcherHasCon(o)\
258 ((o)->dataBits &= ~(U8)SoDispCon_dispatcherHasConDataBitMask)
259#define SoDispCon_setRecEvInactive(o)\
260 ((o)->dataBits &= ~(U8)SoDispCon_recEvActiveDataBitMask)
261#define SoDispCon_setSendEvInactive(o)\
262 ((o)->dataBits &= ~(U8)SoDispCon_sendEvActiveDataBitMask)
263#define SoDispCon_clearIP6(o)\
264 ((o)->dataBits &= ~(U8)SoDispCon_IP6DataBitMask)
265
266#define SoDispCon_socketHasNonBlockData(o)\
267 (((o)->dataBits & SoDispCon_socketHasNonBlockDataBitMask) ? TRUE : FALSE)
268#define SoDispCon_hasMoreData(o)\
269 (((o)->dataBits & SoDispCon_hasMoreDataDataBitMask) ? TRUE : FALSE)
270#define SoDispCon_dispatcherHasCon(o)\
271 (((o)->dataBits & SoDispCon_dispatcherHasConDataBitMask)?TRUE:FALSE)
272#define SoDispCon_recEvActive(o)\
273 (((o)->dataBits & SoDispCon_recEvActiveDataBitMask)?TRUE:FALSE)
274#define SoDispCon_sendEvActive(o)\
275 (((o)->dataBits & SoDispCon_sendEvActiveDataBitMask)?TRUE:FALSE)
276#define SoDispCon_isNonBlocking(o)\
277 (((o)->dataBits & SoDispCon_isNonBlockingDataBitMask) ? TRUE : FALSE)
278#ifndef SoDispCon_isIP6
279#define SoDispCon_isIP6(o)\
280 (((o)->dataBits & SoDispCon_IP6DataBitMask) ? TRUE : FALSE)
281#define SoDispCon_isDGRAM(o) \
282 (((o)->dataBits & SoDispCon_DGramBitMask) ? TRUE : FALSE)
283#endif
284#define SoDispCon_isSecure(o) \
285 ((o)->exec(o,0,SoDispCon_GetSharkSslCon,0,0) > 0)
286#define SoDispCon_getSharkSslCon(o,sharkSslConPtrPtr) \
287 (o)->exec(o,0,SoDispCon_GetSharkSslCon,sharkSslConPtrPtr,0)
288#define SoDispCon_isValid(o) HttpSocket_isValid(&(o)->httpSocket)
289#define SoDispCon_invalidate(o) HttpSocket_invalidate(&(o)->httpSocket)
290#define SoDispCon_getDispatcher(o) (o)->dispatcher
291#define SoDispCon_dispRecEvent(o) (o)->dispRecEv(o)
292#define SoDispCon_dispSendEvent(o) (o)->dispSendEv(o)
293#define SoDispCon_setDispSendEvent(o, dispSendEvFp) \
294 (o)->dispSendEv=dispSendEvFp
295
296#define SoDispCon_setDispRecEvent(o, dispRecEvFp) \
297 (o)->dispRecEv=dispRecEvFp
298
299#define SoDispCon_readData(o, data, len, relmutex) \
300 (o)->exec(o, (relmutex) ? SoDisp_getMutex((o)->dispatcher) : 0, \
301 SoDispCon_ExTypeRead,data,len)
302
303/* The HttpServCon (and classes inherited from HttpServCon) calls the
304 * newConnectionIsReady function after a successful call to
305 * HttpSocket_accept and after the new connection is installed in the
306 * web-server. This is a platform dependent function and is, by default,
307 * disabled as few platforms need this.
308 */
309#ifndef SoDispCon_newConnectionIsReady
310#define SoDispCon_newConnectionIsReady(con)
311#endif
312
313BA_API int SoDispCon_getSharkAlert(
314 SoDispCon* o, U8* alertLevel, U8* alertDescription);
315
316#ifdef __cplusplus
317extern "C" {
318#endif
319BA_API void SoDispCon_constructor(
320 SoDispCon* o, struct SoDisp* dispatcher, SoDispCon_DispRecEv e);
321BA_API int SoDispCon_upgrade(SoDispCon* o, struct SharkSsl* ssl,
322 const char* alpn, const char* host, int port);
323BA_API int SoDispCon_blockRead(SoDispCon* o,void* data,int len);
324BA_API int SoDispCon_sendData(SoDispCon* o, const void* data, int len);
325BA_API int SoDispCon_sendDataNT(SoDispCon* o, const void* data, int len);
326BA_API int SoDispCon_sendDataX(SoDispCon* o, const void* data, int len);
327BA_API int SoDispCon_sendChunkData(SoDispCon* o,const void* data, int len);
328BA_API int SoDispCon_asyncReadyF(SoDispCon* o);
329#define SoDispCon_asyncReady(o)\
330 (o)->exec(o,0,SoDispCon_ExTypeAsyncReady,0,0)
331#define SoDispCon_asyncSend(o, len) \
332 (o)->exec(o,0,SoDispCon_ExTypeAsyncReady,0,len)
333struct AllocatorIntf;
334
335#ifndef __DOXYGEN__
336typedef struct
337{
338 void* retVal;
339 int size;
340} AllocAsynchBufArgs;
341#endif
342
343BA_API void* SoDispCon_allocAsynchBuf(SoDispCon* o, int* size);
344BA_API void SoDispCon_releaseAsyncBuf(SoDispCon* con);
345BA_API void SoDispCon_internalAllocAsynchBuf(
346 SoDispCon* con, AllocAsynchBufArgs* args);
347BA_API int SoDispCon_setNonblocking(SoDispCon* o);
348BA_API int SoDispCon_setBlocking(SoDispCon* o);
349BA_API int SoDispCon_peek(SoDispCon* o);
350BA_API int SoDispCon_moveCon(SoDispCon* o, SoDispCon* newCon);
351#define SoDispCon_destructor(o) SoDispCon_zzCloseCon(o,1)
352#define SoDispCon_hardClose(o) SoDispCon_zzCloseCon(o, 2)
353#define SoDispCon_shutdown(o) SoDispCon_zzCloseCon(o, 1)
354#define SoDispCon_closeCon(o) SoDispCon_zzCloseCon(o, 0)
355#define SoDispCon_setReadTmo(o,timeout) (o)->rtmo = (U16)((timeout)/50)
356BA_API void SoDispCon_zzCloseCon(SoDispCon* o, int shutdown);
357BA_API void SoDispCon_setTCPNoDelay(SoDispCon* o, int enable);
358BA_API int SoDispCon_getPeerName(SoDispCon* o,HttpSockaddr* addr,U16* port);
359BA_API int SoDispCon_getSockName(SoDispCon* o, HttpSockaddr* addr, U16* port);
360BA_API char* SoDispCon_addr2String(
361 SoDispCon* o, HttpSockaddr* addr, char* buf, int len);
362BA_API BaBool SoDispCon_cmpAddr(SoDispCon* o, HttpSockaddr* addr2);
363BA_API void SoDispCon_printSockErr(
364 SoDispCon* o,const char* type,HttpSocket* s,int status);
365#define SoDispCon_getId(o) HttpSocket_getId(&(o)->httpSocket)
366/* Internal */
367#ifndef SoDispCon_platReadData
368int SoDispCon_platReadData(SoDispCon* o, ThreadMutex* m, BaBool* isTerminated,
369 void* data, int len);
370#endif
371
372BA_API int
373SoDispCon_connect(SoDispCon* o,
374 const char* host,
375 U16 port,
376 const void* bindIntfName,
377 U16 bindPort,
378 U32 timeout,
379 BaBool dgram, BaBool ipv6,
380 char** errinfo);
381#ifndef NO_ASYNCH_RESP
382BA_API int
383SoDispCon_asyncConnect(SoDispCon* o,
384 const char* host,
385 U16 port,
386 const void* bindIntfName,
387 BaBool ipv6,
388 char** errinfo);
389BA_API int SoDispCon_asyncConnectNext(SoDispCon* o);
390#ifdef USE_ADDRINFO
391BA_API void SoDispCon_asyncConnectRelease(SoDispCon* o);
392#else
393#define SoDispCon_asyncConnectRelease(o)
394#endif
395
396#endif
397#ifdef __cplusplus
398}
399inline int SoDispCon::readData(void* data, int len, bool relmutex) {
400 return SoDispCon_readData(this, data, len, relmutex ? TRUE : FALSE); }
401inline int SoDispCon::setNonblocking() {
402 return SoDispCon_setNonblocking(this); }
403inline int SoDispCon::setBlocking() {
404 return SoDispCon_setBlocking(this); }
405inline bool SoDispCon::isSecure() {
406 return SoDispCon_isSecure(this) ? true : false; }
407inline bool SoDispCon::getSharkSslCon(SharkSslCon** sc) {
408 return SoDispCon_getSharkSslCon(this,sc) ? true : false; }
409inline bool SoDispCon::isIP6() {
410 return SoDispCon_isIP6(this) ? true : false; }
411inline bool SoDispCon::isValid() {
412 return SoDispCon_isValid(this) ? true : false; }
413inline int SoDispCon::getPeerName(HttpSockaddr* addr,U16* port){
414 return SoDispCon_getPeerName(this,addr,port); }
415inline int SoDispCon::getSockName(HttpSockaddr* addr,U16* port){
416 return SoDispCon_getSockName(this, addr,port); }
417inline char* SoDispCon::addr2String(HttpSockaddr* addr,char* buf,int len){
418 return SoDispCon_addr2String(this, addr, buf, len); }
419inline bool SoDispCon::cmpAddr(HttpSockaddr* addr2) {
420 return SoDispCon_cmpAddr(this, addr2) ? true : false;
421}
422inline void SoDispCon::setTCPNoDelay(bool enable) {
423 SoDispCon_setTCPNoDelay(this, enable); }
425 return SoDispCon_hasMoreData(this) ? true : false;
426}
428 return SoDispCon_dispatcherHasCon(this) ? true : false; }
430 return SoDispCon_recEvActive(this) ? true : false; }
432 return SoDispCon_sendEvActive(this) ? true : false; }
434 return SoDispCon_getDispatcher(this); }
435inline void SoDispCon::setDispSendEvent(SoDispCon_DispSendEv ev) {
436 SoDispCon_setDispSendEvent(this, ev); }
437inline void SoDispCon::setDispRecEvent(SoDispCon_DispRecEv ev) {
438 SoDispCon_setDispRecEvent(this, ev); }
439inline int SoDispCon::sendData(const void* data, int len) {
440 return SoDispCon_sendData(this,data, len); }
441inline int SoDispCon::asyncSend(int len) {
442 return SoDispCon_asyncSend(this, len); }
443inline int SoDispCon::sendChunkData(const void* data, int len) {
444 return SoDispCon_sendChunkData(this, data, len); }
445inline int SoDispCon::asyncReady() {
446 return SoDispCon_asyncReadyF(this) ? true : false; }
447inline void* SoDispCon::allocAsynchBuf(int* size){
448 return SoDispCon_allocAsynchBuf(this, size); }
449inline int SoDispCon::connect(const char* host,
450 U16 port,
451 const void* bindIntfName,
452 U16 bindPort,
453 U32 timeout,
454 BaBool dgram, BaBool ipv6,
455 char** errinfo) {
456 return SoDispCon_connect(this,host,port, bindIntfName,bindPort,timeout,
457 dgram?TRUE:FALSE, dgram?TRUE:FALSE,errinfo);
458}
459#endif
460
461
462#endif
Memory allocation and deallocation Interface class.
Definition: AllocatorIntf.h:83
Contains information about the physical socket connection.
Definition: SoDispCon.h:112
int connect(const char *host, U16 port, const void *bindIntfName=0, U16 bindPort=0, U32 timeout=1500, BaBool dgram=false, BaBool ipv6=false, char **errinfo=0)
Connect to host/IP address.
Definition: SoDispCon.h:449
struct SoDisp * getDispatcher()
Fetch the SoDisp object.
Definition: SoDispCon.h:433
bool isIP6()
Returns true if this is an IP version 6 connection.
Definition: SoDispCon.h:409
bool isValid()
Returns true if the socket connection is valid.
Definition: SoDispCon.h:411
bool isSecure()
Deprecated: Use getSharkSslCon(NULL).
Definition: SoDispCon.h:405
bool sendEvActive()
Used for non blocking send.
Definition: SoDispCon.h:431
char * addr2String(HttpSockaddr *addr, char *buf, int len)
Converts IP address to string.
Definition: SoDispCon.h:417
bool cmpAddr(HttpSockaddr *addr2)
Compare addr with address returned by getPeerName.
Definition: SoDispCon.h:419
bool getSharkSslCon(SharkSslCon **sc)
Returns true if this is an SSL connection.
Definition: SoDispCon.h:407
void setTCPNoDelay(bool enable)
Disable the TCP delay.
Definition: SoDispCon.h:422
bool hasMoreData()
Returns true if more data on the socket.
Definition: SoDispCon.h:424
bool recEvActive()
Returns true if the connection is active in the SoDisp object.
Definition: SoDispCon.h:429
int getSockName(HttpSockaddr *addr, U16 *port=0)
Returns the 'sock' IP address.
Definition: SoDispCon.h:415
bool dispatcherHasCon()
Returns true if connection is in the SoDisp object.
Definition: SoDispCon.h:427
int getPeerName(HttpSockaddr *addr, U16 *port=0)
Returns the 'peer' IP address.
Definition: SoDispCon.h:413
The SoDisp dispatches any socket connection that contains data by calling the SoDispCon::execute memb...
Definition: SoDisp.h:86
A mutual exclusion class.
Definition: ThreadLib.h:186