171
182
/** Forward declarations of the hidden socket internal structures, and
172
* their upper-level handles to use by the LSOCK_*() and SOCK_*() API
183
* their upper-level handles to use by the LSOCK_*() and SOCK_*() API
174
185
struct LSOCK_tag; /* listening socket: internal storage */
175
typedef struct LSOCK_tag* LSOCK; /* listening socket: handle */
186
typedef struct LSOCK_tag* LSOCK; /* listening socket: handle, opaque */
177
188
struct SOCK_tag; /* socket: internal storage */
178
typedef struct SOCK_tag* SOCK; /* socket: handle */
189
typedef struct SOCK_tag* SOCK; /* socket: handle, opaque */
180
191
struct TRIGGER_tag;
181
192
typedef struct TRIGGER_tag* TRIGGER;
184
196
/******************************************************************************
185
* Multi-Thread safety
197
* Multi-Thread safety NOTICE
187
199
* If you are using this API in a multi-threaded application, and there is
188
200
* more than one thread using this API, it is safe to call SOCK_InitializeAPI()
189
* explicitly in the beginning of your main thread, before you run any other
190
* threads, and to call SOCK_ShutdownAPI() after all threads are exited.
201
* explicitly at the beginning of your main thread, before you run any other
202
* threads, and to call SOCK_ShutdownAPI() after all threads have exited.
192
204
* As soon as the API is initialized it becomes relatively MT-safe, however
193
205
* you still must not operate on same LSOCK or SOCK objects from different
194
* threads simultaneously.
206
* threads simultaneously. Any entry point of this API will attempt to
207
* initialize the API implicitly if that has not yet been previously done.
208
* However, the implicit initialization gets disabled by SOCK_ShutdownAPI()
209
* (explicit re-init with SOCK_InitializeAPI() is always allowed).
196
211
* A MUCH BETTER WAY of dealing with this issue is to provide your own MT
197
* locking callback (see CORE_SetLOCK in "ncbi_core.h"). This will also
198
* guarantee the proper MT protection should some other SOCK functions
199
* start to access any static data in the future.
207
/******************************************************************************
208
* Error & Data Logging
210
* @li <b>NOTE:</b> Use CORE_SetLOG() from "ncbi_core.h" to set log handler.
216
/** By default ("log" == eDefault, which is eOff), data are not logged.
218
* To start logging the data, call this func with "log" == eOn.
219
* To stop logging the data, call this func with "log" == eOff.
223
* SOCK_SetDataLogging
225
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetDataLoggingAPI
230
/** Control the data logging for socket "sock" individually.
234
* [in] requested data logging
235
* To reset to the global default behavior (as set by SOCK_SetDataLoggingAPI),
236
* call this function with "log" == eDefault.
240
* SOCK_SetDataLoggingAPI, SOCK_Create, DSOCK_Create
242
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetDataLogging
249
/******************************************************************************
250
* I/O restart on signals
253
/** Control restartability of I/O interrupted by signals.
254
* By default ("on_off" == eDefault,eOff), I/O is restartable if interrupted.
256
* [in] eOn to cancel I/O on signals; eOff to restart
260
* SOCK_SetInterruptOnSignal
262
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetInterruptOnSignalAPI
267
/** Control restartability of I/O interrupted by signals on a per-socket basis.
268
* eDefault causes the use of global API flag.
272
* [in] per-socket I/O restart behavior on signals
276
* SOCK_SetInterruptOnSignalAPI, SOCK_Create, DSOCK_Create
278
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetInterruptOnSignal
285
/******************************************************************************
286
* Address reuse: EXPERIMENTAL and may be removed in the upcoming releases!
289
/** Control address reuse for socket addresses taken by the API.
290
* By default ("on_off" == eDefault,eOff), address is not marked
291
* for reuse in SOCK, but is always reused for LSOCK.
293
* [in] whether to turn on (eOn), turn off (eOff), or use default (eDefault)
297
* SOCK_SetReuseAddress
299
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetReuseAddressAPI
304
/** Control reuse of socket addresses on per-socket basis
305
* Note: only a boolean parameter value is can be used here.
309
* [in] whether to reuse the address (true, non-zero) or not (false, zero)
311
* SOCK_SetReuseAddressAPI, SOCK_Create, DSOCK_Create
313
extern NCBI_XCONNECT_EXPORT void SOCK_SetReuseAddress
315
int/**bool*/ on_off);
319
/******************************************************************************
320
* API Initialization and Shutdown/Cleanup
212
* locking callback (see CORE_SetLOCK() in "ncbi_core.h"). This will also
213
* ensure proper MT protection should some SOCK functions start accessing
214
* any intrinsic static data (such as in case of SSL).
216
* The MT lock as well as other library-wide settings are also provided
217
* (in most cases automatically) by CONNECT_Init() API: for C Toolkit it gets
218
* always called before [Nlm_]Main(); in C++ Toolkit it gets called by
219
* most of C++ classes' ctors, except for sockets; so if your application
220
* does not use any C++ classes besides sockets, it has to set CORE_LOCK
221
* explicitly, as described above.
224
* CORE_SetLOCK, CONNECT_Init
229
/******************************************************************************
230
* API Initialization, Shutdown/Cleanup, and Utility
233
/** Initialize all internal/system data & resources to be used by the SOCK API.
235
* You can safely call it more than once; just, all calls after the first
236
* one will have no result.
238
* Usually, SOCK API does not require an explicit initialization -- as it is
239
* guaranteed to initialize itself automagically, in one of API functions,
240
* when necessary. Yet, see the "Multi Thread safety" remark above.
242
* This call, when used for the very first time in the application, enqueues
243
* SOCK_ShutdownAPI() to be called upon application exit on plaftorms that
244
* provide this functionality. In any case, the application can opt for
245
* explicit SOCK_ShutdownAPI() call when it is done with all sockets.
249
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_InitializeAPI(void);
252
/** Cleanup; destroy all internal/system data & resources used by the SOCK API.
253
* ATTENTION: no function from the SOCK API should be called after this call!
255
* You can safely call it more than once; just, all calls after the first
256
* one will have no result.
260
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_ShutdownAPI(void);
263
/** By default (on UNIX platforms) the SOCK API functions automagically call
264
* "signal(SIGPIPE, SIG_IGN)" on initialization. To prohibit this feature,
265
* you must call SOCK_AllowSigPipeAPI() before you call any other
266
* function from the SOCK API.
268
extern NCBI_XCONNECT_EXPORT void SOCK_AllowSigPipeAPI(void);
272
* Get size of OS-dependent native socket handle
274
* OS-dependent handle size or 0 in case of an error
278
extern NCBI_XCONNECT_EXPORT size_t SOCK_OSHandleSize(void);
323
281
/** This is a helper call that can improve I/O behavior.
325
* [in] Break down long waits on I/O into smaller chunks of at most "timeout"
326
* duration each. This can help recover "hanging" sockets from indefinite
327
* wait and allow them to report an exceptional I/O condition.
283
* [in] Break down long waits on I/O into smaller intervals of at most
284
* "timeslice" duration each. This can help recover "hanging" sockets from
285
* indefinite wait and allow them to report an exceptional I/O condition.
329
* Previous value of the timeout
287
* Previous value of the timeslice
331
289
* SOCK_Wait, SOCK_Poll
333
291
extern NCBI_XCONNECT_EXPORT const STimeout*
334
292
SOCK_SetSelectInternalRestartTimeout
335
(const STimeout* timeout);
293
(const STimeout* timeslice);
338
296
/** Selector of I/O wait system API: auto, poll(), or select().
362
320
(ESOCK_IOWaitSysAPI api);
365
/** By default (on UNIX platforms) the SOCK API functions automagically call
366
* "signal(SIGPIPE, SIG_IGN)" on initialization. To prohibit this feature,
367
* you must call SOCK_AllowSigPipeAPI() before you call any other
368
* function from the SOCK API.
370
extern NCBI_XCONNECT_EXPORT void SOCK_AllowSigPipeAPI(void);
373
/** Initialize all internal/system data & resources to be used by the SOCK API.
375
* You can safely call it more than once; just, all calls after the first
376
* one will have no result.
378
* Usually, SOCK API does not require an explicit initialization -- as it is
379
* guaranteed to initialize itself automagically, in one of API functions,
380
* when necessary. Yet, see the "Multi Thread safety" remark above.
382
* This call, when used for the very first time in the application, enqueues
383
* SOCK_ShutdownAPI() to be called upon application exit on plaftorms that
384
* provide this functionality. In any case, the application can opt for
385
* explicit SOCK_ShutdownAPI() call when it is done with all sockets.
389
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_InitializeAPI(void);
392
/** Cleanup; destroy all internal/system data & resources used by the SOCK API.
393
* ATTENTION: no function from the SOCK API should be called after this call!
395
* You can safely call it more than once; just, all calls after the first
396
* one will have no result.
400
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_ShutdownAPI(void);
404
324
/******************************************************************************
1346
1245
/** Control OS-defined send strategy by disabling/enabling TCP
1246
* layer to send incomplete network frames (packets).
1247
* With the "cork" set on, data gets always buffered until a complete
1248
* hardware packet is full (or connection is about to close), and only
1249
* then is sent out to the medium.
1250
* The setting cancels any effects of SOCK_DisableOSSendDelay().
1252
* [in] socket handle [stream socket only]
1254
* [in] 1 to set the cork; 0 to remove the cork
1256
* SOCK_DisableOSSendDelay
1258
extern NCBI_XCONNECT_EXPORT void SOCK_SetCork
1264
/** Control OS-defined send strategy by disabling/enabling TCP
1347
1265
* Nagle algorithm that packs multiple requests into a single
1348
* frame and thus transferring data in fewer transactions,
1266
* packet and thus transferring data in fewer transactions,
1349
1267
* miminizing the network traffic and bursting the throughput.
1350
1268
* Some applications, however, may find it useful to disable this
1351
1269
* default behavior for the sake of their performance increase
1352
1270
* (like in case of short transactions otherwise held by the system
1353
1271
* to be possibly coalesced into larger chunks).
1272
* The setting cancels any effects of SOCK_SetCork().
1355
* [in] socket handle
1274
* [in] socket handle [stream socket only]
1356
1275
* @param on_off
1358
* NB: use true to disable; false to enable
1276
* [in] 1 to disable the send delay; 0 to enable the send delay
1360
1280
extern NCBI_XCONNECT_EXPORT void SOCK_DisableOSSendDelay
1586
1527
extern NCBI_XCONNECT_EXPORT int/**bool*/ SOCK_IsSecure(SOCK sock);
1532
* [in] socket handle
1534
* [in] either eIO_Read or eIO_Write
1536
* Current read or write logical position, which takes any pending
1537
* (i.e. unread or unwritten) data into account.
1539
extern NCBI_XCONNECT_EXPORT TNCBI_BigCount SOCK_GetPosition
1547
* [in] socket handle
1549
* [in] either eIO_Read or eIO_Write
1551
* Count of bytes actually read or written through this socket in the current
1552
* session. For datagram sockets the count applies for the last message only;
1553
* for stream sockets it counts only since last accept or connect event.
1555
extern NCBI_XCONNECT_EXPORT TNCBI_BigCount SOCK_GetCount
1563
* [in] socket handle
1565
* [in] either eIO_Read or eIO_Write
1567
* Total number of bytes transferred through the socket in its lifetime.
1569
extern NCBI_XCONNECT_EXPORT TNCBI_BigCount SOCK_GetTotalCount
1575
/******************************************************************************
1576
* I/O restart on signals
1579
/** Control restartability of I/O interrupted by signals.
1580
* By default I/O is restartable if interrupted.
1581
* Pass "on_off" as eDefault to get the current setting.
1583
* [in] eOn to cancel I/O on signals; eOff to restart
1587
* SOCK_SetInterruptOnSignal
1589
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetInterruptOnSignalAPI
1594
/** Control restartability of I/O interrupted by signals on a per-socket basis.
1595
* eDefault causes the use of global API flag.
1597
* [in] socket handle
1599
* [in] per-socket I/O restart behavior on signals
1603
* SOCK_SetInterruptOnSignalAPI, SOCK_Create, DSOCK_Create
1605
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetInterruptOnSignal
1611
/******************************************************************************
1612
* Address reuse: EXPERIMENTAL and may be removed in the upcoming releases!
1615
/** Control address reuse for socket addresses taken by the API.
1616
* By default address is not marked for reuse in SOCK,
1617
* but is always reused for LSOCK.
1618
* Pass "on_off" as eDefault to get the current setting.
1620
* [in] whether to turn on (eOn), turn off (eOff) or get current (eDefault)
1624
* SOCK_SetReuseAddress
1626
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetReuseAddressAPI
1631
/** Control reuse of socket addresses on per-socket basis
1632
* Note: only a boolean parameter value is can be used here.
1634
* [in] socket handle
1636
* [in] whether to reuse the address (true, non-zero) or not (false, zero)
1638
* SOCK_SetReuseAddressAPI, SOCK_Create, DSOCK_Create
1640
extern NCBI_XCONNECT_EXPORT void SOCK_SetReuseAddress
1642
int/**bool*/ on_off);
1645
/******************************************************************************
1646
* Error & Data Logging
1648
* @li <b>NOTE:</b> Use CORE_SetLOG() from "ncbi_core.h" to set log handler.
1654
/** By default data are not logged.
1656
* To start logging the data, call this func with "log" == eOn.
1657
* To stop logging the data, call this func with "log" == eOff.
1658
* To get current log switch, call this func with "log" == eDefault.
1662
* SOCK_SetDataLogging
1664
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetDataLoggingAPI
1669
/** Control the data logging for socket "sock" individually.
1671
* [in] socket handle
1673
* [in] requested data logging
1674
* To reset to the global default behavior (as set by SOCK_SetDataLoggingAPI),
1675
* call this function with "log" == eDefault.
1679
* SOCK_SetDataLoggingAPI, SOCK_Create, DSOCK_Create
1681
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetDataLogging
1688
/******************************************************************************
1689
* GENERIC POLLABLE INTERFACE, please see SOCK_Poll() above for explanations
1694
struct SPOLLABLE_tag;
1695
typedef struct SPOLLABLE_tag* POLLABLE;
1713
extern NCBI_XCONNECT_EXPORT EIO_Status POLLABLE_Poll
1715
SPOLLABLE_Poll polls[],
1716
const STimeout* timeout,
1723
* Return 0 if conversion cannot be made; otherwise the converted handle
1725
extern NCBI_XCONNECT_EXPORT POLLABLE POLLABLE_FromSOCK (SOCK);
1726
extern NCBI_XCONNECT_EXPORT POLLABLE POLLABLE_FromLSOCK (LSOCK);
1727
extern NCBI_XCONNECT_EXPORT POLLABLE POLLABLE_FromTRIGGER(TRIGGER);
1728
extern NCBI_XCONNECT_EXPORT SOCK POLLABLE_ToSOCK (POLLABLE);
1729
extern NCBI_XCONNECT_EXPORT LSOCK POLLABLE_ToLSOCK (POLLABLE);
1730
extern NCBI_XCONNECT_EXPORT TRIGGER POLLABLE_ToTRIGGER(POLLABLE);
1590
1734
/******************************************************************************
1591
1735
* AUXILIARY network-specific functions (added for the portability reasons)