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 4915 2021-12-01 18:26:55Z wini $
15  *
16  * COPYRIGHT: Real Time Logic LLC, 2003 - 2020
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 
45 struct 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 */
69 typedef 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 /*
82 The 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 
96 struct SoDispCon;
97 struct SharkSsl;
98 
99 typedef int (*SoDispCon_Execute)(
100  struct SoDispCon* con, ThreadMutex* m,SoDispCon_ExType s,void* d1,int d2);
101 
102 
103 typedef void (*SoDispCon_DispRecEv)(struct SoDispCon* con);
104 typedef void (*SoDispCon_DispSendEv)(struct SoDispCon* con);
105 
106 
111 typedef 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 
130  int connect(const char* host,
131  U16 port,
132  const void* bindIntfName=0,
133  U16 bindPort=0,
134  U32 timeout=1500,
135  BaBool dgram=false, BaBool ipv6=false,
136  char** errinfo=0);
137 
140  bool isSecure();
141 
145  bool getSharkSslCon(SharkSslCon** sc);
146 
147 
150  bool isValid();
151 
153  bool isIP6();
154 
158  int getPeerName(HttpSockaddr* addr,U16* port=0);
159 
163  int getSockName(HttpSockaddr* addr,U16* port=0);
164 
167  char* addr2String(HttpSockaddr* addr, char* buf, int len);
168 
171  bool cmpAddr(HttpSockaddr* addr2);
172 
178  void setTCPNoDelay(bool enable);
179 
182  struct SoDisp* getDispatcher();
183 
186  bool hasMoreData();
187 
190  bool dispatcherHasCon();
191 
195  bool recEvActive();
196 
199  bool sendEvActive();
200 
201  void setDispSendEvent(SoDispCon_DispSendEv ev);
202  void setDispRecEvent(SoDispCon_DispRecEv ev);
203  int readData(void* data, int len, bool relmutex=false);
204  int setNonblocking();
205  int setBlocking();
206  int sendData(const void* data, int len);
207  int asyncSend(int len);
208  int sendChunkData(const void* data, int len);
209  int asyncReady();
210  void* allocAsynchBuf(int* size);
211 #endif
212  CONNECTION_DISPATCHER_OBJ /*Owned and handled by the SoDisp obj.*/
213 
214  /* Owned and handled by SSL. */
215  void* sslData;
216  SoDispCon_Execute exec;
217 
218  HttpSocket httpSocket;
219  SoDispCon_DispRecEv dispRecEv;
220  SoDispCon_DispSendEv dispSendEv;
221  struct SoDisp* dispatcher;
222  BaBool* sendTermPtr;
223  BaBool* recTermPtr;
224  U16 rtmo; /* Read timeout in 50 millisec intervals */
225  U8 dataBits;
226  U8 isSending;
227 } SoDispCon;
228 
229 #define SoDispCon_socketHasNonBlockDataBitMask 0x01
230 #define SoDispCon_hasMoreDataDataBitMask 0x02
231 
232 #define SoDispCon_dispatcherHasConDataBitMask 0x04
233 #define SoDispCon_recEvActiveDataBitMask 0x08
234 #define SoDispCon_sendEvActiveDataBitMask 0x10
235 #define SoDispCon_isNonBlockingDataBitMask 0x20
236 #define SoDispCon_IP6DataBitMask 0x40
237 #define SoDispCon_DGramBitMask 0x80
238 
239 #define SoDispCon_setDispHasRecData(o)\
240  ((o)->dataBits |= \
241  (SoDispCon_hasMoreDataDataBitMask | SoDispCon_socketHasNonBlockDataBitMask))
242 #define SoDispCon_setDispatcherHasCon(o)\
243  ((o)->dataBits |= SoDispCon_dispatcherHasConDataBitMask)
244 #define SoDispCon_setRecEvActive(o)\
245  ((o)->dataBits |= SoDispCon_recEvActiveDataBitMask)
246 #define SoDispCon_setSendEvActive(o)\
247  ((o)->dataBits |= SoDispCon_sendEvActiveDataBitMask)
248 #define SoDispCon_setIP6(o)\
249  ((o)->dataBits |= SoDispCon_IP6DataBitMask)
250 
251 #define SoDispCon_clearSocketHasNonBlockData(o)\
252  ((o)->dataBits &= ~(U8)SoDispCon_socketHasNonBlockDataBitMask)
253 #define SoDispCon_clearHasMoreData(o)\
254  ((o)->dataBits &= ~(U8)SoDispCon_hasMoreDataDataBitMask)
255 #define SoDispCon_clearDispatcherHasCon(o)\
256  ((o)->dataBits &= ~(U8)SoDispCon_dispatcherHasConDataBitMask)
257 #define SoDispCon_setRecEvInactive(o)\
258  ((o)->dataBits &= ~(U8)SoDispCon_recEvActiveDataBitMask)
259 #define SoDispCon_setSendEvInactive(o)\
260  ((o)->dataBits &= ~(U8)SoDispCon_sendEvActiveDataBitMask)
261 #define SoDispCon_clearIP6(o)\
262  ((o)->dataBits &= ~(U8)SoDispCon_IP6DataBitMask)
263 
264 #define SoDispCon_socketHasNonBlockData(o)\
265  (((o)->dataBits & SoDispCon_socketHasNonBlockDataBitMask) ? TRUE : FALSE)
266 #define SoDispCon_hasMoreData(o)\
267  (((o)->dataBits & SoDispCon_hasMoreDataDataBitMask) ? TRUE : FALSE)
268 #define SoDispCon_dispatcherHasCon(o)\
269  (((o)->dataBits & SoDispCon_dispatcherHasConDataBitMask)?TRUE:FALSE)
270 #define SoDispCon_recEvActive(o)\
271  (((o)->dataBits & SoDispCon_recEvActiveDataBitMask)?TRUE:FALSE)
272 #define SoDispCon_sendEvActive(o)\
273  (((o)->dataBits & SoDispCon_sendEvActiveDataBitMask)?TRUE:FALSE)
274 #define SoDispCon_isNonBlocking(o)\
275  (((o)->dataBits & SoDispCon_isNonBlockingDataBitMask) ? TRUE : FALSE)
276 #ifndef SoDispCon_isIP6
277 #define SoDispCon_isIP6(o)\
278  (((o)->dataBits & SoDispCon_IP6DataBitMask) ? TRUE : FALSE)
279 #define SoDispCon_isDGRAM(o) \
280  (((o)->dataBits & SoDispCon_DGramBitMask) ? TRUE : FALSE)
281 #endif
282 #define SoDispCon_isSecure(o) \
283  ((o)->exec(o,0,SoDispCon_GetSharkSslCon,0,0) > 0)
284 #define SoDispCon_getSharkSslCon(o,sharkSslConPtrPtr) \
285  (o)->exec(o,0,SoDispCon_GetSharkSslCon,sharkSslConPtrPtr,0)
286 #define SoDispCon_isValid(o) HttpSocket_isValid(&(o)->httpSocket)
287 #define SoDispCon_invalidate(o) HttpSocket_invalidate(&(o)->httpSocket)
288 #define SoDispCon_getDispatcher(o) (o)->dispatcher
289 #define SoDispCon_dispRecEvent(o) (o)->dispRecEv(o)
290 #define SoDispCon_dispSendEvent(o) (o)->dispSendEv(o)
291 #define SoDispCon_setDispSendEvent(o, dispSendEvFp) \
292  (o)->dispSendEv=dispSendEvFp
293 
294 #define SoDispCon_setDispRecEvent(o, dispRecEvFp) \
295  (o)->dispRecEv=dispRecEvFp
296 
297 #define SoDispCon_readData(o, data, len, relmutex) \
298  (o)->exec(o, (relmutex) ? SoDisp_getMutex((o)->dispatcher) : 0, \
299  SoDispCon_ExTypeRead,data,len)
300 
301 /* The HttpServCon (and classes inherited from HttpServCon) calls the
302  * newConnectionIsReady function after a successful call to
303  * HttpSocket_accept and after the new connection is installed in the
304  * web-server. This is a platform dependent function and is, by default,
305  * disabled as few platforms need this.
306  */
307 #ifndef SoDispCon_newConnectionIsReady
308 #define SoDispCon_newConnectionIsReady(con)
309 #endif
310 
311 BA_API int SoDispCon_getSharkAlert(SoDispCon* o, U8* alertLevel, U8* alertDescription);
312 
313 #ifdef __cplusplus
314 extern "C" {
315 #endif
316 BA_API void SoDispCon_constructor(
317  SoDispCon* o, struct SoDisp* dispatcher, SoDispCon_DispRecEv e);
318 BA_API int SoDispCon_upgrade(SoDispCon* o, struct SharkSsl* ssl,
319  const char* alpn, const char* host, int port);
320 BA_API int SoDispCon_blockRead(SoDispCon* o,void* data,int len);
321 BA_API int SoDispCon_sendData(SoDispCon* o, const void* data, int len);
322 BA_API int SoDispCon_sendDataNT(SoDispCon* o, const void* data, int len);
323 BA_API int SoDispCon_sendDataX(SoDispCon* o, const void* data, int len);
324 BA_API int SoDispCon_sendChunkData(SoDispCon* o,const void* data, int len);
325 BA_API int SoDispCon_asyncReadyF(SoDispCon* o);
326 #define SoDispCon_asyncReady(o)\
327  (o)->exec(o,0,SoDispCon_ExTypeAsyncReady,0,0)
328 #define SoDispCon_asyncSend(o, len) \
329  (o)->exec(o,0,SoDispCon_ExTypeAsyncReady,0,len)
330 struct AllocatorIntf;
331 
332 #ifndef __DOXYGEN__
333 typedef struct
334 {
335  void* retVal;
336  int size;
337 } AllocAsynchBufArgs;
338 #endif
339 
340 BA_API void* SoDispCon_allocAsynchBuf(SoDispCon* o, int* size);
341 BA_API void SoDispCon_releaseAsyncBuf(SoDispCon* con);
342 BA_API void SoDispCon_internalAllocAsynchBuf(SoDispCon* con, AllocAsynchBufArgs* args);
343 BA_API int SoDispCon_setNonblocking(SoDispCon* o);
344 BA_API int SoDispCon_setBlocking(SoDispCon* o);
345 BA_API int SoDispCon_peek(SoDispCon* o);
346 BA_API int SoDispCon_moveCon(SoDispCon* o, SoDispCon* newCon);
347 #define SoDispCon_destructor(o) SoDispCon_zzCloseCon(o,1)
348 #define SoDispCon_hardClose(o) SoDispCon_zzCloseCon(o, 2)
349 #define SoDispCon_shutdown(o) SoDispCon_zzCloseCon(o, 1)
350 #define SoDispCon_closeCon(o) SoDispCon_zzCloseCon(o, 0)
351 #define SoDispCon_setReadTmo(o,timeout) (o)->rtmo = (U16)((timeout)/50)
352 BA_API void SoDispCon_zzCloseCon(SoDispCon* o, int shutdown);
353 BA_API void SoDispCon_setTCPNoDelay(SoDispCon* o, int enable);
354 BA_API int SoDispCon_getPeerName(SoDispCon* o,HttpSockaddr* addr,U16* port);
355 BA_API int SoDispCon_getSockName(SoDispCon* o, HttpSockaddr* addr, U16* port);
356 BA_API char* SoDispCon_addr2String(
357  SoDispCon* o, HttpSockaddr* addr, char* buf, int len);
358 BA_API BaBool SoDispCon_cmpAddr(SoDispCon* o, HttpSockaddr* addr2);
359 BA_API void SoDispCon_printSockErr(
360  SoDispCon* o,const char* type,HttpSocket* s,int status);
361 #define SoDispCon_getId(o) HttpSocket_getId(&(o)->httpSocket)
362 /* Internal */
363 int SoDispCon_platReadData(SoDispCon* o, ThreadMutex* m, BaBool* isTerminated,
364  void* data, int len);
365 
366 BA_API int
367 SoDispCon_connect(SoDispCon* o,
368  const char* host,
369  U16 port,
370  const void* bindIntfName,
371  U16 bindPort,
372  U32 timeout,
373  BaBool dgram, BaBool ipv6,
374  char** errinfo);
375 #ifndef NO_ASYNCH_RESP
376 BA_API int
377 SoDispCon_asyncConnect(SoDispCon* o,
378  const char* host,
379  U16 port,
380  const void* bindIntfName,
381  BaBool ipv6,
382  char** errinfo);
383 BA_API int SoDispCon_asyncConnectNext(SoDispCon* o);
384 #ifdef USE_ADDRINFO
385 BA_API void SoDispCon_asyncConnectRelease(SoDispCon* o);
386 #else
387 #define SoDispCon_asyncConnectRelease(o)
388 #endif
389 
390 #endif
391 #ifdef __cplusplus
392 }
393 inline int SoDispCon::readData(void* data, int len, bool relmutex) {
394  return SoDispCon_readData(this, data, len, relmutex ? TRUE : FALSE); }
395 inline int SoDispCon::setNonblocking() {
396  return SoDispCon_setNonblocking(this); }
397 inline int SoDispCon::setBlocking() {
398  return SoDispCon_setBlocking(this); }
399 inline bool SoDispCon::isSecure() {
400  return SoDispCon_isSecure(this) ? true : false; }
401 inline bool SoDispCon::getSharkSslCon(SharkSslCon** sc) {
402  return SoDispCon_getSharkSslCon(this,sc) ? true : false; }
403 inline bool SoDispCon::isIP6() {
404  return SoDispCon_isIP6(this) ? true : false; }
405 inline bool SoDispCon::isValid() {
406  return SoDispCon_isValid(this) ? true : false; }
407 inline int SoDispCon::getPeerName(HttpSockaddr* addr,U16* port){
408  return SoDispCon_getPeerName(this,addr,port); }
409 inline int SoDispCon::getSockName(HttpSockaddr* addr,U16* port){
410  return SoDispCon_getSockName(this, addr,port); }
411 inline char* SoDispCon::addr2String(HttpSockaddr* addr,char* buf,int len){
412  return SoDispCon_addr2String(this, addr, buf, len); }
413 inline bool SoDispCon::cmpAddr(HttpSockaddr* addr2) {
414  return SoDispCon_cmpAddr(this, addr2) ? true : false;
415 }
416 inline void SoDispCon::setTCPNoDelay(bool enable) {
417  SoDispCon_setTCPNoDelay(this, enable); }
418 inline bool SoDispCon::hasMoreData() {
419  return SoDispCon_hasMoreData(this) ? true : false;
420 }
422  return SoDispCon_dispatcherHasCon(this) ? true : false; }
423 inline bool SoDispCon::recEvActive() {
424  return SoDispCon_recEvActive(this) ? true : false; }
425 inline bool SoDispCon::sendEvActive() {
426  return SoDispCon_sendEvActive(this) ? true : false; }
428  return SoDispCon_getDispatcher(this); }
429 inline void SoDispCon::setDispSendEvent(SoDispCon_DispSendEv ev) {
430  SoDispCon_setDispSendEvent(this, ev); }
431 inline void SoDispCon::setDispRecEvent(SoDispCon_DispRecEv ev) {
432  SoDispCon_setDispRecEvent(this, ev); }
433 inline int SoDispCon::sendData(const void* data, int len) {
434  return SoDispCon_sendData(this,data, len); }
435 inline int SoDispCon::asyncSend(int len) {
436  return SoDispCon_asyncSend(this, len); }
437 inline int SoDispCon::sendChunkData(const void* data, int len) {
438  return SoDispCon_sendChunkData(this, data, len); }
439 inline int SoDispCon::asyncReady() {
440  return SoDispCon_asyncReadyF(this) ? true : false; }
441 inline void* SoDispCon::allocAsynchBuf(int* size){
442  return SoDispCon_allocAsynchBuf(this, size); }
443 inline int SoDispCon::connect(const char* host,
444  U16 port,
445  const void* bindIntfName,
446  U16 bindPort,
447  U32 timeout,
448  BaBool dgram, BaBool ipv6,
449  char** errinfo) {
450  return SoDispCon_connect(this,host,port, bindIntfName,bindPort,timeout,
451  dgram?TRUE:FALSE, dgram?TRUE:FALSE,errinfo);
452 }
453 #endif
454 
455 
456 #endif
SoDisp
The SoDisp dispatches any socket connection that contains data by calling the SoDispCon::execute memb...
Definition: SoDisp.h:86
SoDispCon::sendEvActive
bool sendEvActive()
Used for non blocking send.
Definition: SoDispCon.h:425
SoDispCon::setTCPNoDelay
void setTCPNoDelay(bool enable)
Disable the TCP delay.
Definition: SoDispCon.h:416
SoDispCon::recEvActive
bool recEvActive()
Returns true if the connection is active in the SoDisp object.
Definition: SoDispCon.h:423
SoDispCon::hasMoreData
bool hasMoreData()
Returns true if more data on the socket.
Definition: SoDispCon.h:418
AllocatorIntf
Memory allocation and deallocation Interface class.
Definition: AllocatorIntf.h:83
SoDispCon::isValid
bool isValid()
Returns true if the socket connection is valid.
Definition: SoDispCon.h:405
SoDispCon::cmpAddr
bool cmpAddr(HttpSockaddr *addr2)
Compare addr with address returned by getPeerName.
Definition: SoDispCon.h:413
SoDispCon::getDispatcher
struct SoDisp * getDispatcher()
Fetch the SoDisp object.
Definition: SoDispCon.h:427
SoDispCon::connect
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:443
SoDispCon::dispatcherHasCon
bool dispatcherHasCon()
Returns true if connection is in the SoDisp object.
Definition: SoDispCon.h:421
SoDispCon::getPeerName
int getPeerName(HttpSockaddr *addr, U16 *port=0)
Returns the 'peer' IP address.
Definition: SoDispCon.h:407
SoDispCon::getSockName
int getSockName(HttpSockaddr *addr, U16 *port=0)
Returns the 'sock' IP address.
Definition: SoDispCon.h:409
SoDispCon::getSharkSslCon
bool getSharkSslCon(SharkSslCon **sc)
Returns true if this is an SSL connection.
Definition: SoDispCon.h:401
SoDispCon::isSecure
bool isSecure()
Deprecated: Use getSharkSslCon(NULL).
Definition: SoDispCon.h:399
SoDispCon
Contains information about the physical socket connection.
Definition: SoDispCon.h:112
SharkSsl
Definition: selib.h:257
ThreadMutex
A mutual exclusion class.
Definition: ThreadLib.h:186
SoDispCon::isIP6
bool isIP6()
Returns true if this is an IP version 6 connection.
Definition: SoDispCon.h:403
SoDispCon::addr2String
char * addr2String(HttpSockaddr *addr, char *buf, int len)
Converts IP address to string.
Definition: SoDispCon.h:411