NCBI C++ ToolKit
ncbi_socket.h
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CONNECT___NCBI_SOCKET__H
2 #define CONNECT___NCBI_SOCKET__H
3 
4 /* $Id: ncbi_socket.h 101456 2023-12-15 16:43:56Z lavr $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Denis Vakatov, Anton Lavrentiev
30  *
31  * @file
32  * File Description:
33  * Plain portable TCP/IP socket API for: UNIX, MS-Win, MacOS
34  * Platform-specific library requirements:
35  * [UNIX ] -DNCBI_OS_UNIX -lresolv -lsocket -lnsl
36  * [MSWIN] -DNCBI_OS_MSWIN ws2_32.lib
37  *
38  *********************************
39  * Generic:
40  *
41  * SOCK_InitializeAPI
42  * SOCK_ShutdownAPI
43  * SOCK_OSHandleSize
44  * SOCK_AllowSigPipeAPI
45  * SOCK_SetApproveHookAPI
46  *
47  * Event trigger (handle TRIGGER):
48  *
49  * TRIGGER_Create
50  * TRIGGER_Close
51  * TRIGGER_Set
52  * TRIGGER_IsSet
53  * TRIGGER_Reset
54  *
55  * Listening socket (handle LSOCK):
56  *
57  * LSOCK_Create[Ex]
58  * LSOCK_Accept[Ex]
59  * LSOCK_Close
60  * LSOCK_GetOSHandle[Ex]
61  * LSOCK_GetPort
62  *
63  * I/O Socket (handle SOCK):
64  *
65  * SOCK_Create[Ex] (see also LSOCK_Accept)
66  * SOCK_CreateOnTop[Ex]
67  * SOCK_Reconnect
68  * SOCK_Shutdown
69  * SOCK_Close[Ex]
70  * SOCK_Destroy
71  * SOCK_CloseOSHandle
72  * SOCK_Wait
73  * SOCK_Poll
74  * SOCK_SetTimeout
75  * SOCK_GetTimeout
76  * SOCK_Read (including "peek" and "persistent read")
77  * SOCK_ReadLine
78  * SOCK_Pushback
79  * SOCK_Status
80  * SOCK_Write
81  * SOCK_Abort
82  * SOCK_GetLocalPort[Ex]
83  * SOCK_GetRemotePort
84  * SOCK_GetPeerAddress
85  * SOCK_GetPeerAddressString[Ex]
86  * SOCK_GetOSHandle[Ex]
87  * SOCK_SetReadOnWriteAPI
88  * SOCK_SetReadOnWrite
89  * SOCK_SetCork
90  * SOCK_DisableOSSendDelay
91  *
92  * Datagram Socket (handle SOCK):
93  *
94  * DSOCK_Create[Ex]
95  * DSOCK_Bind
96  * DSOCK_Connect
97  * DSOCK_WaitMsg
98  * DSOCK_RecvMsg
99  * DSOCK_SendMsg
100  * DSOCK_WipeMsg
101  * DSOCK_SetBroadcast
102  * DSOCK_GetMessageCount
103  *
104  * Socket classification & statistics:
105  *
106  * SOCK_IsDatagram
107  * SOCK_IsClientSide
108  * SOCK_IsServerSide
109  * SOCK_IsUNIX
110  * SOCK_IsSecure
111  * SOCK_GetPosition
112  * SOCK_GetCount
113  * SOCK_GetTotalCount
114  *
115  * Settings:
116  *
117  * SOCK_SetInterruptOnSignalAPI
118  * SOCK_SetInterruptOnSignal
119  * SOCK_SetReuseAddressAPI
120  * SOCK_SetReuseAddress
121  *
122  * Data logging / error reporting:
123  *
124  * SOCK_SetDataLoggingAPI
125  * SOCK_SetDataLogging
126  * SOCK_SetErrHookAPI
127  *
128  * Generic POLLABLE API:
129  *
130  * POLLABLE_Poll
131  * POLLABLE_What
132  * POLLABLE_FromSOCK
133  * POLLABLE_FromLSOCK
134  * POLLABLE_FromTrigger
135  * POLLABLE_ToSOCK
136  * POLLABLE_ToLSOCK
137  * POLLABLE_ToTrigger
138  *
139  * Auxiliary:
140  *
141  * SOCK_ntoa
142  * SOCK_isip[Ex]
143  * SOCK_IsLoopbackAddress
144  * SOCK_HostToNetShort
145  * SOCK_HostToNetLong
146  * SOCK_NetToHostShort
147  * SOCK_NetToHostLong
148  * SOCK_gethostname[Ex]
149  * SOCK_gethostbyname[Ex]
150  * SOCK_gethostbyaddr[Ex]
151  * SOCK_GetLoopbackAddress
152  * SOCK_GetLocalHostAddress
153  * SOCK_StringToHostPort
154  * SOCK_HostPortToString
155  *
156  * Utility:
157  *
158  * SOCK_SetSelectInternalRestartTimeout
159  * SOCK_SetIOWaitSysAPI
160  *
161  * Secure Socket Layer (SSL):
162  *
163  * SOCK_SetupSSL[Ex]
164  * SOCK_SSLName
165  *
166  */
167 
168 #include <connect/ncbi_core.h>
169 
170 
171 /** @addtogroup Sockets
172  *
173  * @{
174  */
175 
176 
177 #ifdef __cplusplus
178 extern "C" {
179 #endif
180 
181 
182 /******************************************************************************
183  * TYPEDEFS & MACROS
184  */
185 
186 typedef unsigned EBIO_Event;
187 typedef unsigned EBIO_Status;
188 
189 
190 /** Network and host byte order enumeration type
191  */
192 typedef enum {
196 
197 
198 /** Socket type (internal)
199  */
200 typedef enum {
204  eSOCK_Datagram = 3/*2|1*/
206 
207 
208 /** Sides of socket
209  */
210 typedef enum {
212  eSOCK_Client = 1
214 
215 
216 /** Forward declarations of the hidden socket internal structures, and
217  * their upper-level handles to use by the SOCK API.
218  */
219 struct LSOCK_tag; /* listening socket: internal storage */
220 typedef struct LSOCK_tag* LSOCK; /* listening socket: handle, opaque */
221 
222 struct SOCK_tag; /* socket: internal storage */
223 typedef struct SOCK_tag* SOCK; /* socket: handle, opaque */
224 
225 struct TRIGGER_tag; /* trigger: internal storage */
226 typedef struct TRIGGER_tag* TRIGGER; /* trigger: handle, opaque */
227 
228 
229 
230 /******************************************************************************
231  * Multi-Thread safety NOTICE
232  *
233  * If you are using this API in a multi-threaded application, and there is
234  * more than one thread using this API, it is safe to call SOCK_InitializeAPI()
235  * explicitly at the beginning of your main thread, before you run any other
236  * threads, and to call SOCK_ShutdownAPI() after all threads have exited.
237  *
238  * As soon as the API is initialized it becomes relatively MT-safe, however
239  * you still must not operate on same LSOCK or SOCK objects from different
240  * threads simultaneously. Any entry point of this API will attempt to
241  * initialize the API implicitly if that has not yet been previously done.
242  * However, the implicit initialization gets disabled by SOCK_ShutdownAPI()
243  * (explicit re-init with SOCK_InitializeAPI() is always allowed).
244  *
245  * A MUCH BETTER WAY of dealing with this issue is to provide your own MT
246  * locking callback (see CORE_SetLOCK() in "ncbi_core.h"). This will also
247  * ensure proper MT protection should some SOCK functions start accessing
248  * any intrinsic static data (such as in case of SSL).
249  *
250  * The MT lock as well as other library-wide settings are also provided
251  * (in most cases automatically) by CONNECT_Init() API: for C Toolkit it gets
252  * always called before [Nlm_]Main(); in C++ Toolkit it gets called by
253  * most of C++ classes' ctors, except for sockets; so if your application
254  * does not use any C++ classes besides sockets, it has to set CORE_SetLOCK
255  * explicitly, as described above.
256  *
257  * @sa
258  * CORE_SetLOCK, CONNECT_Init
259  */
260 
261 
262 
263 /******************************************************************************
264  * API INITIALIZATION, SHUTDOWN/CLEANUP, AND UTILITY
265  */
266 
267 /** Initialize all internal/system data & resources to be used by the SOCK API.
268  * @note
269  * You can safely call it more than once; just, all calls after the first
270  * one will have no result.
271  * @note
272  * Usually, SOCK API does not require an explicit initialization -- as it is
273  * guaranteed to initialize itself automagically, in one of API functions,
274  * when necessary. Yet, see the "Multi Thread safety" remark above.
275  * @note
276  * This call, when used for the very first time in the application, enqueues
277  * SOCK_ShutdownAPI() to be called upon application exit on plaftorms that
278  * provide this functionality. In any case, the application can opt for
279  * explicit SOCK_ShutdownAPI() call when it is done with all sockets.
280  * @sa
281  * SOCK_ShutdownAPI
282  */
284 
285 
286 /** Cleanup; destroy all internal/system data & resources used by the SOCK API.
287  * @attention No function from the SOCK API should be called after this call!
288  * @note
289  * You can safely call it more than once; just, all calls after the first
290  * one will have no effect.
291  * @warning
292  * Once the API has been shut down with this call, it cannot be reactivated
293  * with SOCK_InitializeAPI() -- it will always return eIO_NotSupported.
294  * @sa
295  * SOCK_InitializeAPI
296  */
298 
299 
300 /** Get size of OS-dependent native socket handle.
301  * @return
302  * OS-dependent handle size or 0 in case of an error
303  * @sa
304  * SOCK_GetOSHandle
305  */
306 extern NCBI_XCONNECT_EXPORT size_t SOCK_OSHandleSize(void);
307 
308 
309 /** By default (on UNIX platforms) the SOCK API functions automagically call
310  * "signal(SIGPIPE, SIG_IGN)" on initialization. To prohibit this feature you
311  * must call SOCK_AllowSigPipeAPI() before you call any other function from the
312  * SOCK API.
313  */
315 
316 
317 /** User-level connection approval hook.
318  * The hook (when installed) gets called for any attempt to establish an
319  * outgoing(eSOCK_Client) or incoming(eSOCK_Server) connection(eSOCK_Socket),
320  * or to send(eSOCK_Client) or receive(eSOCK_Server) a message packet in a
321  * datagram socket(eSOCK_Datagram). The peer end is always identified by the
322  * "addr:port" pair (which is non-0 in both parts). Optionally for outgoing
323  * transaction, a textual hostname("host") can be provided (to which "addr"
324  * corresponds). If the action was requested by a plain IP address, the "host"
325  * field is set to NULL.
326  *
327  * The hook is expected to return eIO_Success to approve the action, or any
328  * other status code to deny. Note that either eIO_Timeout or eIO_Closed
329  * returned from the hook, will get converted to eIO_Unknown; and other codes
330  * will get passed through to the caller "as is".
331  * @warning
332  * Although returning eIO_Interrupt may be helpful to prevent the upper-lever
333  * connection (CONN) layer from processing further I/O on this communication
334  * channel, it may also be just an undesireable side-effect -- so it should
335  * be used with caution.
336  * @note
337  * When the hook is not installed, all the actions described above are
338  * treated as allowed (as if eIO_Success was returned).
339  * @sa
340  * SOCK_Create, SOCK_Reconnect, LSOCK_Accept, DSOCK_SendMsg, DSOCK_RecvMsg
341  */
342 
343 typedef struct {
344  const char* host; /**< Textual hostname if provided for outgoing */
345  unsigned int addr; /**< IPv4 (or -1 if unknown / broadcast), never 0 */
346  unsigned short port; /**< Port number, host byte order, never 0 */
347  ESOCK_Side side; /**< eSOCK_Client (out) / eSOCK_Server (in) */
348  ESOCK_Type type; /**< eSOCK_Socket (conn) / eSOCK_Datagram (packet) */
350 
351 
352 /** The approve hook is global per API and gets called with an arbitrary
353  * user "data", if so desired as a second argument, which was specified when
354  * the hook was installed with the SOCK_SetApproveHookAPI() call.
355  */
357  void* data);
358 
359 /** The hook is installed when non-NULL, and de-installed otherwise. */
361 void SOCK_SetApproveHookAPI(FSOCK_ApproveHook hook, /**< the hook function */
362  void* data); /**< optional user data */
363 
364 
365 /** This is a helper call that can improve I/O behavior.
366  * @param timeslice
367  * [in] Break down long waits for I/O into smaller intervals of at most
368  * "timeslice" duration each. This can help recover "hanging" sockets from
369  * indefinite wait and allow them to report an exceptional I/O condition.
370  * @return
371  * Previous value of the timeslice
372  * @sa
373  * SOCK_Wait, SOCK_Poll
374  */
376 (const STimeout* timeslice
377  );
378 
379 
380 /** Selector of I/O wait system API: auto, poll(), or select().
381  * @sa
382  * SOCK_SetIOWaitSysAPI
383  */
384 typedef enum {
385  eSOCK_IOWaitSysAPIAuto, /**< default; use some heuristics to choose API*/
386  eSOCK_IOWaitSysAPIPoll, /**< always use poll() */
387  eSOCK_IOWaitSysAPISelect /**< always use select() */
389 
390 /** This is a helper call that can improve I/O performance (ignored for MSVC).
391  * @param api
392  * [in] Default behavior is to wait for I/O such a way that accomodates the
393  * requested sockets accordingly. There is a known limitation of the select()
394  * API that requires all sockets to have numeric values of their low-level I/O
395  * handles less than (as little as) 1024, but works faster than the poll() API
396  * that does not have limits on the number or numeric values of the handles.
397  * Either API can be enforced here.
398  * @return
399  * Previous value of the API selector
400  * @sa
401  * SOCK_Wait, SOCK_Poll
402  */
405  );
406 
407 
408 
409 /******************************************************************************
410  * EVENT TRIGGER
411  */
412 
413 /** Create an event trigger.
414  * @param trigger
415  * [in|out] a pointer to a location where to store handle of the new trigger
416  * @return
417  * eIO_Success on success; other status on error
418  * @sa
419  * TRIGGER_Close, TRIGGER_Set
420  */
422 (TRIGGER* trigger,
423  ESwitch log
424  );
425 
426 
427 /** Close an event trigger.
428  * @param trigger
429  * [in] a handle returned by TRIGGER_Create()
430  * @return
431  * eIO_Success on success; other status on error
432  * @sa
433  * TRIGGER_Create
434  */
436 (TRIGGER trigger
437  );
438 
439 
440 /** Set an event trigger. Can be used from many threads concurrently.
441  * @param trigger
442  * [in] a handle returned by TRIGGER_Create()
443  * @return
444  * eIO_Success on success; other status on error
445  * @sa
446  * TRIGGER_Create, TRIGGER_IsSet
447  */
449 (TRIGGER trigger
450  );
451 
452 
453 /** Check whether the trigger has been set. Should not be used from multiple
454  * threads concurrently at a time, including along with TRIGGER_Reset().
455  * @param trigger
456  * [in] a handle returned by TRIGGER_Create()
457  * @return
458  * eIO_Success if the trigger has been set;
459  * eIO_Closed if the trigger has not yet been set;
460  * other status on error
461  * @sa
462  * TRIGGER_Create, TRIGGER_Set, TRIGGER_Reset
463  */
465 (TRIGGER trigger
466  );
467 
468 
469 /** Reset the trigger. Should not be used from multiple threads concurrently,
470  * including along with TRIGGER_IsSet().
471  * @param trigger
472  * [in] a handle returned by TRIGGER_Create()
473  * @return
474  * eIO_Success if the trigger has been reset; other status on error
475  * @sa
476  * TRIGGER_Create, TRIGGER_Set, TRIGGER_IsSet
477  */
479 (TRIGGER trigger
480  );
481 
482 
483 
484 /******************************************************************************
485  * SOCKET FLAGS
486  */
487 
488 typedef enum {
489  fSOCK_LogOff = eOff, /**< NB: logging is inherited in accepted SOCK*/
492  fSOCK_KeepAlive = 8, /**< keep socket alive (if supported by OS) */
493  fSOCK_BindAny = 0, /**< bind to 0.0.0.0 (i.e. any), default */
494  fSOCK_BindLocal = 0x10, /**< bind to 127.0.0.1 only */
495  fSOCK_KeepOnExec = 0x20, /**< can be applied to all sockets */
496  fSOCK_CloseOnExec = 0, /**< can be applied to all sockets, default */
497  fSOCK_Secure = 0x40, /**< subsumes CloseOnExec regardless of Keep */
498  fSOCK_KeepOnClose = 0x80, /**< retain OS handle in SOCK_Close[Ex]() */
499  fSOCK_CloseOnClose = 0, /**< close OS handle in SOCK_Close[Ex]() */
503 typedef unsigned int TSOCK_Flags; /**< bitwise "OR" of ESOCK_Flags */
504 
505 
506 
507 /******************************************************************************
508  * LISTENING SOCKET [SERVER-side]
509  */
510 
511 /** [SERVER-side] Create and initialize the server-side(listening) socket
512  * (socket() + bind() + listen())
513  * @param port
514  * [in] the port to listen at (0 to choose first available)
515  * @param backlog
516  * [in] maximal # of pending connections
517  * @note On some systems, "backlog" may be silently limited down to 128
518  (or even 5), or completely ignored whatsoever.
519  * @param lsock
520  * [out] handle of the created listening socket
521  * @param flags
522  * [in] special modifiers
523  * @sa
524  * LSOCK_Create, LSOCK_Close, LSOCK_GetPort
525  */
527 (unsigned short port,
528  unsigned short backlog,
529  LSOCK* lsock,
531  );
532 
533 
534 /** [SERVER-side] Create and initialize the server-side(listening) socket
535  * Same as LSOCK_CreateEx() called with the last argument provided as
536  * fSOCK_LogDefault.
537  * @param port
538  * [in] the port to listen at
539  * @param backlog
540  * [in] maximal # of pending connections
541  * @note On some systems, "backlog" can be silently limited down to 128
542  * (or even 5), or completely ignored whatsoever.
543  * @param lsock
544  * [out] handle of the created listening socket
545  * @sa
546  * LSOCK_CreateEx, LSOCK_Close
547  */
549 (unsigned short port,
550  unsigned short backlog,
551  LSOCK* lsock
552  );
553 
554 
555 /** [SERVER-side] Accept connection from a client.
556  * @param lsock
557  * [in] handle of a listening socket
558  * @param timeout
559  * [in] timeout (infinite if NULL)
560  * @param sock
561  * [out] handle of the accepted socket
562  * @param flags
563  * [in] properties for the accepted socket to have
564  * @note
565  * The provided "timeout" is for this Accept() only. To set I/O timeout on
566  * the resulted socket use SOCK_SetTimeout();
567  * all I/O timeouts are infinite by default.
568  * @sa
569  * SOCK_Create, SOCK_Close, TSOCK_Flags
570  */
572 (LSOCK lsock,
573  const STimeout* timeout,
574  SOCK* sock,
576  );
577 
578 
579 /** [SERVER-side] Accept connection from a client.
580  * Same as LSOCK_AcceptEx(.,.,.,fSOCK_LogDefault)
581  * @sa
582  * LSOCK_AcceptEx
583  */
585 (LSOCK lsock,
586  const STimeout* timeout,
587  SOCK* sock
588  );
589 
590 
591 /** [SERVER-side] Close the listening socket, destroy relevant internal data.
592  * @param lsock
593  * [in] listening socket handle to close
594  * The call invalidates the handle, so its further is not allowed.
595  * @sa
596  * LSOCK_Create
597  */
599 
600 
601 /** Get an OS-dependent native socket handle to use by platform-specific API.
602  * FYI: on MS-Windows it will be "SOCKET", on other platforms -- "int".
603  * @param lsock
604  * [in] socket handle
605  * @param handle_buf
606  * [in] pointer to a memory location to store the OS-dependent handle at
607  * @param handle_size
608  * The exact(!) size of the expected OS handle
609  * @param ownership
610  * eTakeOwnership removes the handle from LSOCK; eNoOwnership retains it
611  * @note
612  * If handle ownership is taken, all operations with this LSOCK (except for
613  * LSOCK_Close()) will fail.
614  * @sa
615  * SOCK_OSHandleSize, SOCK_GetOSHandle, SOCK_CloseOSHandle, LSOCK_GetOSHandle,
616  * LSOCK_Close
617  */
619 (LSOCK lsock,
620  void* handle_buf,
621  size_t handle_size,
622  EOwnership owndership
623  );
624 
625 /** Same as LSOCK_GetOSHandleEx(lsock, handle_buf, handle_size, eNoOwnership).
626  * @sa
627  * LSOCK_GetOSHandleEx
628  */
630 (LSOCK lsock,
631  void* handle_buf,
632  size_t handle_size
633  );
634 
635 
636 /** Get socket port number, which it listens on.
637  * The returned port is either one specified when the socket was created, or
638  * an automatically assigned number if LSOCK_Create() provided the port as 0.
639  * @param lsock
640  * [in] socket handle
641  * @param byte_order
642  * [in] byte order for port on return
643  * @return
644  * Listening port number in requested byte order, or 0 in case of an error.
645  * @sa
646  * LSOCK_Create
647  */
648 extern NCBI_XCONNECT_EXPORT unsigned short LSOCK_GetPort
649 (LSOCK lsock,
650  ENH_ByteOrder byte_order
651  );
652 
653 
654 
655 /******************************************************************************
656  * SOCKET (connection-oriented)
657  */
658 
659 /** [CLIENT-side] Connect client to another(server-side, listening) socket
660  * (socket() + connect() [+ select()])
661  * @note
662  * SOCK_Close[Ex]() will not close the underlying OS handle if
663  * fSOCK_KeepOnClose is set in "flags".
664  * @param host
665  * [in] host to connect to
666  * @param port
667  * [in] port to connect to
668  * @param timeout
669  * [in] the connect timeout (infinite if NULL)
670  * @param sock
671  * [out] handle of socket created
672  * @param data
673  * [in] initial output data block (may be NULL)
674  * @param size
675  * [in] size of the initial data block (may be 0)
676  * @param flags
677  * [in] additional socket requirements
678  * @sa
679  * SOCK_Create, SOCK_Reconnect, SOCK_Close
680  */
682 (const char* host,
683  unsigned short port,
684  const STimeout* timeout,
685  SOCK* sock,
686  const void* data,
687  size_t size,
689  );
690 
691 
692 /** [CLIENT-side] Connect client to another(server-side, listening) socket
693  * (socket() + connect() [+ select()])
694  * Equivalent to
695  * SOCK_CreateEx(host, port, timeout, sock, 0, 0, fSOCK_LogDefault).
696  *
697  * @param host
698  * [in] host to connect to
699  * @param port
700  * [in] port to connect to
701  * @param timeout
702  * [in] the connect timeout (infinite if NULL)
703  * @param sock
704  * [out] handle of the created socket
705  * @sa
706  * SOCK_CreateEx, SOCK_Reconnect, SOCK_Close
707  */
709 (const char* host,
710  unsigned short port,
711  const STimeout* timeout,
712  SOCK* sock
713  );
714 
715 
716 /** [SERVER-side] Create a SOCKet on top of either an OS-dependent "handle"
717  * (file descriptor on Unix, SOCKET on MS-Windows) or an existing SOCK object.
718  * In the former case, the returned socket is not reopenable to its default
719  * peer (SOCK_Reconnect() may not specify zeros for the connection point).
720  * In the latter case, the returned socket is reopenable to its default peer
721  * only if the original existing socket is a client-side one (connected to a
722  * remote server end), not a server-side socket (accepted at the server end).
723  * All timeouts are set to default [infinite] values.
724  * The call does *not* destroy either the OS handle or the SOCK passed in the
725  * arguments, regardless of the return status code.
726  * When a socket is being created on top of a SOCK handle, the original SOCK
727  * gets emptied (and the underlying OS handle removed from it) upon the call
728  * return (whether successfully or not), yet it will still need SOCK_Destroy()
729  * in the caller's code to free up the memory it occupies.
730  * Any secure session that may have existed in the original SOCK will have
731  * been migrated to the new socket iff "flags" indicate fSOCK_Secure (if no
732  * session existed, a new session may have been initiated in the new SOCK).
733  * Otherwise, the original secure session (if any) will have been closed.
734  * Any pending output will have been flushed (if switching secure / insecure
735  * contexts; otherwise, it will have simply migrated to the created SOCK), and
736  * any pending input still in the original SOCK will migrate to the new socket
737  * object returned. If this behavior is undesireable, one can use
738  * SOCK_GetOSHandleEx() on the original socket, taking the ownership of the
739  * underlying OS handle, and then create a SOCK on top of the bare "handle",
740  * specifying its (non-zero) "handle_size".
741  * @warning
742  * It is not recommended to use this call on not fully connected sockets
743  * (either bare OS handle or SOCK) in native MS-Windows builds (e.g. Cygwin is
744  * okay).
745  * @note
746  * SOCK_Close[Ex]() on the resultant socket will not close the underlying OS
747  * handle if fSOCK_KeepOnClose is set in "flags".
748  * @note
749  * (SOCK_IsClientSide() | SOCK_IsServerSide()) can be used to determine if the
750  * original SOCK was stripped off the underlying handle: the expression would
751  * evaluate to 0(false) iff the SOCK object does no longer have the OS handle.
752  * @warning
753  * If a new SOCK is being built from an existing SOCK, then it is assumed that
754  * the underlying OS handle's properies have not been modified outside that
755  * old SOCK object (as all system calls to adjust the handle will be skipped).
756  * @param handle
757  * [in] OS-dependent "handle" or SOCK to be converted
758  * @param handle_size
759  * [in] "handle" size (or 0 if a SOCK passed in "handle")
760  * @param sock
761  * [out] SOCK built on top of the "handle"
762  * @param data
763  * [in] initial output data block (may be NULL)
764  * @param size
765  * [in] size of the initial data block (may be 0)
766  * @param flags
767  * [in] additional socket requirements
768  * @return
769  * Return eIO_Success on success; otherwise: eIO_InvalidArg if the "handle"
770  * does not refer to an open socket [but e.g. to a normal file or a pipe];
771  * eIO_Closed when the original socket is not connected; other error codes
772  * in case of other errors.
773  * @sa
774  * SOCK_GetOSHandleEx, SOCK_CreateOnTop, SOCK_Reconnect, SOCK_Close
775  */
777 (const void* handle,
778  size_t handle_size,
779  SOCK* sock,
780  const void* data,
781  size_t size,
783  );
784 
785 
786 /** [SERVER-side] Create a socket on top of a "handle".
787  * Equivalent to SOCK_CreateOnTopEx(handle, handle_size, sock,
788  * 0, 0, fSOCK_LogDefault|fSOCK_CloseOnClose).
789  * @param handle
790  * [in] OS-dependent "handle" or "SOCK" to be converted
791  * @param handle_size
792  * [in] "handle" size (0 if a "SOCK" passed in "handle")
793  * @param sock
794  * [out] SOCK built on top of the OS "handle"
795  * @sa
796  * SOCK_GetOSHandleEx, SOCK_CreateOnTopEx, SOCK_Close
797  */
799 (const void* handle,
800  size_t handle_size,
801  SOCK* sock
802  );
803 
804 
805 /** [CLIENT-side] Close the socket referred to by "sock" and then connect
806  * it to another "host:port"; fail if it takes more than "timeout"
807  * (close() + connect() [+ select()])
808  *
809  * HINT: if "host" is NULL then connect to the same host address as before;
810  * if "port" is zero then connect to the same port # as before.
811  *
812  * @note The "new" socket inherits old I/O timeouts;
813  * @note The call is only applicable to stream [not datagram] sockets.
814  * @note "timeout"==NULL is infinite (also as kInfiniteTimeout);
815  * "timeout"=={0,0} causes no wait for connection to be established,
816  * and to return immediately.
817  * @note UNIX sockets can only be reconnected to the same file,
818  * thus both host and port have to be passed as 0s.
819  * @param sock
820  * [in] handle of the socket to reconnect
821  * @param host
822  * [in] host to connect to (can be NULL)
823  * @param port
824  * [in] port to connect to (can be 0)
825  * @param timeout
826  * [in] the connect timeout (infinite if NULL)
827  */
829 (SOCK sock,
830  const char* host,
831  unsigned short port,
832  const STimeout* timeout
833  );
834 
835 
836 /** Shutdown the connection in only one direction (specified by "direction").
837  * Later attempts to I/O (or to wait) in the shutdown direction will
838  * do nothing, and immediately return with "eIO_Closed" status.
839  * Pending data output can cause data transfer to the remote end (subject
840  * for eIO_Close timeout as previously set by SOCK_SetTimeout()).
841  * Cannot be applied to datagram sockets (eIO_InvalidArg results).
842  * @param sock
843  * [in] handle of the socket to shutdown
844  * @param how
845  * [in] one of: eIO_Read, eIO_Write, eIO_ReadWrite
846  * @sa
847  * SOCK_SetTimeout
848  */
850 (SOCK sock,
851  EIO_Event how
852  );
853 
854 
855 /** Close the SOCK handle, and destroy all relevant internal data.
856  * The "sock" handle goes invalid after this function call, regardless of
857  * whether the call was successful or not. If eIO_Close timeout was specified
858  * (or NULL) then it blocks until either all unsent data are sent, an error
859  * flagged, or the timeout expires; if there was any output pending, that
860  * output will be flushed.
861  * Connection may remain in the system if SOCK was created with the
862  * fSOCK_KeepOnClose flag set; otherwise, it gets closed as well.
863  * @warning
864  * On MS-Windows closing a SOCK whose OS handle has been used elsewhere
865  * (e.g. in SOCK_CreateOnTop()) may render the OS handle unresponsive, so
866  * always make sure to close the current SOCK first (assuming
867  * fSOCK_KeepOnClose), and only then use the extracted handle to build
868  * another socket.
869  * @param sock
870  * [in] socket handle to close(if not yet closed) and destroy(always)
871  * @sa
872  * SOCK_Create, SOCK_CreateOnTop, DSOCK_Create, SOCK_SetTimeout, SOCK_CloseEx
873  */
875 #define SOCK_Destroy(s) SOCK_Close(s)
876 
877 
878 /** Close the SOCK handle, and conditionally destroy relevant internal data.
879  * If eIO_Close timeout was specified (or NULL) then it blocks until either all
880  * unsent data are sent, an error flagged, or the timeout expires; if there is
881  * any output pending, that output will be flushed.
882  * Connection may remain in the system if the socket was created with the
883  * fSOCK_KeepOnClose flag set; otherwise, it gets closed as well.
884  * @note
885  * On MS-Windows closing a SOCK whose OS handle has been used elsewhere
886  * (e.g. in SOCK_CreateOnTop()) may render the OS handle unresponsive, so
887  * always make sure to close the current SOCK first (assuming
888  * fSOCK_KeepOnClose), and only then use the extracted handle to build
889  * another socket.
890  * @param sock
891  * [in] handle of the socket to close
892  * @param destroy
893  * [in] =1 to destroy the SOCK handle; =0 to keep the handle
894  * @note
895  * The kept SOCK handle can be freed/destroyed by SOCK_Destroy().
896  * @note
897  * SOCK_CloseEx(sock, 1) is equivalent to SOCK_Destroy(sock).
898  * @sa
899  * SOCK_Close, SOCK_Create, SOCK_CreateOnTop, DSOCK_Create, SOCK_SetTimeout
900  */
902 (SOCK sock,
903  int/*bool*/ destroy
904  );
905 
906 
907 /** Close socket OS handle (ungracefully aborting the connection if necessary).
908  * The call retries repeatedly if interrupted by a singal (so no eIO_Interrupt
909  * should be expected). Return eIO_Success when the handle has been closed
910  * successfully, eIO_Closed if the handle has been passed already closed,
911  * eIO_InvalidArg if passed arguments are not valid, eIO_Unknown if the
912  * handle cannot be closed (per an error returned by the system, see errno).
913  * @warning
914  * Using this call on a handle that belongs to an active [LD]SOCK object is
915  * undefined.
916  * @sa
917  * SOCK_GetOSHandleEx
918  */
920 (const void* handle,
921  size_t handle_size
922 );
923 
924 
925 /** Block on the socket until either the specified "event" is available or
926  * "timeout" expires (if "timeout" is NULL then assume it infinite).
927  * eIO_Open (as "event") can be used to check whether the socket has been
928  * connected. When eIO_Read is requested as an "event" for a datagram socket,
929  * then eIO_Closed results if the internally latched message has been entirely
930  * read out. Either of eIO_Open, eIO_Write and eIO_ReadWrite always succeeds
931  * immediately for a datagram socket.
932  * @param sock
933  * [in] socket handle
934  * @param event
935  * [in] one of: eIO_Open, eIO_Read, eIO_Write, eIO_ReadWrite
936  * @param timeout
937  * [in] maximal time to wait for the event to occur (not kDefaultTimeout!)
938  * @return
939  * eIO_Closed -- if the socket has been closed (in the specified
940  * direction when a read/write "event" requested);
941  * eIO_Success -- if the socket is ready;
942  * eIO_Unknown -- if partially closed with eIO_Open requested,
943  * or an I/O error occurred;
944  * eIO_Timeout -- if socket is not ready for the "event" and the allotted
945  * timeout has expired (for eIO_Open means the socket is
946  * still connecting);
947  * eIO_Interrupt -- if the call had been interrupted by a signal before
948  * any other verifiable status was available;
949  * eIO_InvalidArg -- if the "event" is not one of those mentioned above.
950  * @note It is allowed to use a non-NULL zeroed STimeout to poll
951  * on the socket for the immediately available event and
952  * return it (or eIO_Timeout, otherwise) without blocking.
953  * @sa
954  * SOCK_Poll, POLLABLE_Poll
955  */
957 (SOCK sock,
958  EIO_Event event,
959  const STimeout* timeout
960  );
961 
962 
963 /** I/O polling structure.
964  * @sa SOCK_Poll()
965  */
966 typedef struct {
967  SOCK sock; /** [in] SOCK to poll (NULL if not to poll) */
968  EIO_Event event; /** [in] one of: eIO_Open/Read/Write/ReadWrite */
969  EIO_Event revent; /** [out] one of: eIO_Open/Read/Write/ReadWrite/Close */
970 } SSOCK_Poll;
971 
972 
973 /** Block until at least one of the sockets enlisted in "polls" array
974  * (of size "n") becomes available for requested operation (SSOCK_Poll::event),
975  * or until timeout expires (wait indefinitely if timeout is passed as NULL).
976  *
977  * @note To lower overhead, use SOCK_Wait() to wait for I/O on a single socket.
978  *
979  * Return eIO_Success if at least one socket was found ready; eIO_Timeout
980  * if timeout expired; eIO_Unknown if underlying system call(s) failed.
981  *
982  * @note NULL sockets (without any verification for the contents of
983  * the "event" field) as well as non-NULL sockets with eIO_Open
984  * requested in their "event" do not get polled (yet the corresponding
985  * "revent" gets updated to indicate eIO_Open, for no I/O event ready);
986  * @note For a socket found not ready for an operation, eIO_Open gets returned
987  * in its "revent"; for a failing / closed socket -- eIO_Close;
988  * @note This call may return eIO_InvalidArg if:
989  * - parameters to the call are incomplete / inconsistent;
990  * - a non-NULL socket polled with a bad "event" (e.g. eIO_Close).
991  * With this return code, the caller cannot rely on "revent" fields in
992  * the "polls" array as they might not have been updated properly.
993  * @note If either both "n" and "polls" are NULL, or all sockets in the
994  * "polls" array are either NULL or without any events requested
995  * (eIO_Open), then the returned status is either:
996  * - eIO_Timeout (after the specified amount of time was spent idle), or
997  * - eIO_Interrupt (if a signal came while the waiting was in progress).
998  * @note For datagram sockets, the readiness for reading is determined by the
999  * message data latched since the last message receive call,
1000  * DSOCK_RecvMsg().
1001  * @note This call allows the intermixture of stream, datagram, and listening
1002  * sockets (cast to SOCK), as well as triggers (also cast to SOCK), but
1003  * for the sake of readability, it is recommended to use POLLABLE_Poll()
1004  * in such circumstances.
1005  * @note This call may cause some socket I/O in those sockets marked for
1006  * read-on-write and those with pending connection or output data.
1007  * @param n
1008  * [in] # of SSOCK_Poll elems in "polls"
1009  * @param polls[]
1010  * [in|out] array of query/result structures
1011  * @param timeout
1012  * [in] max time to wait (infinite if NULL)
1013  * @param n_ready
1014  * [out] # of ready sockets (may be NULL)
1015  * @sa
1016  * SOCK_Wait, POLLABLE_Poll
1017  */
1019 (size_t n,
1020  SSOCK_Poll polls[],
1021  const STimeout* timeout,
1022  size_t* n_ready
1023  );
1024 
1025 
1026 /** Specify timeout for the connection I/O (see SOCK_[Read|Write|Close]()).
1027  * If "timeout" is NULL then set the timeout to be infinite;
1028  * @note The default timeout is infinite (to wait indefinitely).
1029  * @param sock
1030  * [in] socket handle
1031  * @param event
1032  * [in] one of: eIO_[Read/Write/ReadWrite/Close]
1033  * @param timeout
1034  * [in] new timeout value to set (kDefaultTimeout is not accepted)
1035  * @sa
1036  * SOCK_Read, SOCK_Write, SOCK_Close
1037  */
1039 (SOCK sock,
1040  EIO_Event event,
1041  const STimeout* timeout
1042  );
1043 
1044 
1045 /** Get the connection's i/o timeout (or NULL, if the timeout is infinite).
1046  * @note The returned timeout is guaranteed to be pointing to
1047  * a valid structure in memory at least until the SOCK is closed
1048  * or SOCK_SetTimeout() is called for this "sock".
1049  * @note eIO_ReadWrite timeout is the least of eIO_Read and eIO_Write ones.
1050  * @param sock
1051  * [in] socket handle
1052  * @param event
1053  * [in] one of: eIO_[Read/Write/Close]
1054  * @sa
1055  * SOCK_Read, SOCK_Write, SOCK_Close
1056  */
1058 (SOCK sock,
1059  EIO_Event event
1060  );
1061 
1062 
1063 /** Read/peek up to "size" bytes from "sock" to a buffer pointed to by "buf".
1064  * In "*n_read", return the number of successfully read bytes.
1065  * Read method "how" can be either of the following:
1066  * eIO_ReadPlain -- read as many as "size" bytes and return (eIO_Success);
1067  * if no data are readily available then wait at most
1068  * read timeout and return (eIO_Timeout) if no data still
1069  * could be got; eIO_Success if some data were obtained.
1070  * eIO_ReadPeek -- same as "eIO_ReadPlain" but do not extract the data from
1071  * the socket (so that the next read operation will see the
1072  * data again), with one important exception noted below.
1073  * eIO_ReadPersist -- read exactly "size" bytes and return eIO_Success; if less
1074  * data received then return an error condition (including
1075  * eIO_Timeout).
1076  *
1077  * If there is no data available to read (also, if eIO_ReadPersist and cannot
1078  * read exactly "size" bytes) and the timeout(see SOCK_SetTimeout()) is expired
1079  * then return eIO_Timeout.
1080  *
1081  * Both eIO_ReadPlain and eIO_ReadPeek return eIO_Success iff some data have
1082  * been actually read (within the time allotted by the eIO_Read timeout).
1083  * Both methods return any other code when no data at all were available.
1084  * In that respect, eIO_ReadPersist differs from the other two methods as it
1085  * can return an error condition even if some (but not all) data were actually
1086  * obtained from the socket.
1087  *
1088  * Hence, as the *rule of thumb*, an application should always check the number
1089  * of read bytes BEFORE checking the return status, which merely advises as to
1090  * whether it is okay to read again.
1091  *
1092  * As a special case, "buf" may passed as NULL:
1093  * eIO_ReadPeek -- read up to "size" bytes and store them in internal
1094  * buffer;
1095  * eIO_Read[Persist] -- discard up to "size" bytes from internal buffer
1096  * and socket (check "*n_read" to know how many).
1097  *
1098  * @note "Read" and "peek" methods differ: when "read" is performed and not
1099  * enough but only some data are available immediately from the internal
1100  * buffer, then the call still completes with the eIO_Success status.
1101  * For "peek", if not all requested data were available right away, the
1102  * real I/O occurs to try to pick up additional data from the system.
1103  * Keep this difference in mind when programming loops that heavily use
1104  * "peek"s without "read"s.
1105  * @note If on input "size" == 0, then "*n_read" is set to 0 on return,
1106  * and the return status value can be either of eIO_Success, eIO_Closed,
1107  * eIO_Timeout (if still connecting), or eIO_Unknown depending on the
1108  * _current_ state of the socket regardless of any pending read data.
1109  * @param sock
1110  * [in] socket handle
1111  * @param buf
1112  * [out] data buffer to read to
1113  * @param size
1114  * [in] max # of bytes to read to "buf"
1115  * @param n_read
1116  * [out] # of bytes read (can be NULL)
1117  * @param how
1118  * [in] how to read the data
1119  * @sa
1120  * SOCK_SetTimeout, SOCK_PushBack
1121  */
1123 (SOCK sock,
1124  void* buf,
1125  size_t size,
1126  size_t* n_read,
1127  EIO_ReadMethod how
1128  );
1129 
1130 
1131 /** Read a line from SOCK. A line is terminated by either '\n' (with an
1132  * optional preceding '\r') or '\0', and is stored in the buffer "line" of
1133  * "size" characters long. "*n_read" (if "n_read" passed non-NULL) receives
1134  * the number of characters written into "line", not counting the terminating
1135  * '\0'. Returned result is '\0'-terminated (and has '\r'(if any)'\n'
1136  * stripped) but only if "size" is big enough to contain the entire line.
1137  * Otherwise, all "size" bytes are filled, and "*n_read" == "size" upon return,
1138  * and there is no terminating '\0' in this (and the only!) case, which the
1139  * caller can easily distinguish. The remainder of the line can be read with
1140  * successive call(s) to SOCK_ReadLine() until the '\0' terminator is received
1141  * in the buffer.
1142  * @param sock
1143  * [in] socket handle
1144  * @param line
1145  * [out] line buffer to read to, non-NULL
1146  * @param size
1147  * [in] max # of bytes to read to "line", non-0
1148  * @param n_read
1149  * [out] # of bytes read (optional, can be NULL)
1150  * @return
1151  * Return eIO_Success upon successful completion: either a line separator is
1152  * encountered or the buffer is filled up completely, and any excess read has
1153  * been successfully saved for further I/O. Other code, otherwise.
1154  * @note that "*n_read" should be analyzed prior to the return status, because
1155  * the buffer could have received some contents before the indicated error
1156  * occurred (e.g. when the connection closes before a line separator is seen).
1157  * @sa
1158  * SOCK_SetTimeout, SOCK_PushBack
1159  */
1161 (SOCK sock,
1162  char* line,
1163  size_t size,
1164  size_t* n_read
1165  );
1166 
1167 
1168 /** Push the specified data back to the socket's input queue (in the socket's
1169  * internal read buffer). These can be any data, not necessarily the data
1170  * previously read from the socket. The most recently pushed back data are the
1171  * data that will be read from the socket first (by either SOCK_ReadLine() or
1172  * SOCK_Read()). The presence of pushed back data makes the socket ready
1173  * for read immediately (SOCK_Wait(sock, eIO_Read) succeeds without waiting).
1174  * @param sock
1175  * [in] socket handle
1176  * @param data
1177  * [in] data to push back to the socket's local buffer
1178  * @param size
1179  * [in] # of bytes (starting at "data") to push back
1180  * @sa
1181  * SOCK_Read, SOCK_ReadLine, SOCK_Wait
1182  */
1184 (SOCK sock,
1185  const void* data,
1186  size_t size
1187  );
1188 
1189 
1190 /** Return low-level socket I/O status of *last* socket operation.
1191  * This call does not perform any I/O or socket-related system calls.
1192  * @param sock
1193  * [in] socket handle
1194  * @param direction
1195  * [in] one of: eIO_Open, eIO_Read, eIO_Write
1196  * @return
1197  * - eIO_Closed - if either the connection has been closed / shut down
1198  * (in corresponding direction for eIO_Read or eIO_Write),
1199  * or the socket does not exist (for eIO_Open);
1200  * - eIO_Timeout - if connection request has been submitted but not
1201  * completed (i.e. it was still pending during last I/O);
1202  * - eIO_Interrupt - if last data I/O was interrupted by a signal
1203  * (applicable only to eIO_Read or eIO_Write);
1204  * - eIO_Unknown - if an error has been detected during last data I/O
1205  * (applicable only to eIO_Read or eIO_Write);
1206  * - eIO_InvalidArg - if "direction" is not one of the allowed above;
1207  * - eIO_Success - otherwise.
1208  *
1209  * @note eIO_Open merely checks whether the socket still exists (i.e. open as
1210  * a system object), and that SOCK_CloseEx() has not been called on it.
1211  *
1212  * @note SOCK_Read(eIO_ReadPeek) and SOCK_Wait(eIO_Read) will not
1213  * return any error as long as there is unread buffered
1214  * data left. Thus, when you are "peeking" data (instead of
1215  * reading it out), then the only "non-destructive" way to
1216  * check whether an EOF or an error has actually occurred,
1217  * it is to use this call.
1218  */
1220 (SOCK sock,
1221  EIO_Event direction
1222  );
1223 
1224 
1225 /** Write "size" bytes of "data" to "sock".
1226  * @param sock
1227  * [in] socket handle
1228  * @param data
1229  * [in] data to write to the socket
1230  * @param size
1231  * [in] # of bytes (starting at "data") to write
1232  * @param n_written
1233  * [out] # of written bytes (can be NULL)
1234  * @param how
1235  * [in] either eIO_WritePlain or eIO_WritePersist
1236  * @return
1237  * In "*n_written", return the number of bytes actually written.
1238  * eIO_WritePlain -- write as many bytes as possible at once and return
1239  * immediately; if no bytes can be written then wait
1240  * at most WRITE timeout, try again and return.
1241  * eIO_WritePersist -- write all data (doing an internal retry loop
1242  * if necessary); if any single write attempt times out
1243  * or fails then stop writing and return (error code).
1244  * Return status: eIO_Success -- some bytes were written successfully [Plain]
1245  * -- all bytes were written successfully [Persist]
1246  * other code denotes an error, but some bytes might have
1247  * been sent nevertheless (always check "*n_written" to know).
1248  *
1249  * @note With eIO_WritePlain the call returns eIO_Success if and only if
1250  * some data were actually written to the socket. If no data could
1251  * be written (and perhaps timeout expired) this call always returns
1252  * an error.
1253  * @note eIO_WritePlain and eIO_WritePersist differ that
1254  * the latter can flag an error condition even if some data were
1255  * actually written
1256  * (see "the rule of thumb" in the comments for SOCK_Read() above).
1257  * @note If "size"==0, return value can be eIO_Success if no
1258  * pending data left in the socket, or eIO_Timeout if there are still
1259  * data pending. In either case, "*n_written" is set to 0 on return.
1260  * @sa
1261  * SOCK_SetTimeout
1262  */
1264 (SOCK sock,
1265  const void* data,
1266  size_t size,
1267  size_t* n_written,
1268  EIO_WriteMethod how
1269  );
1270 
1271 
1272 /** If there is outstanding connection or output data pending, cancel it.
1273  * Mark the socket as if it has been shut down for both reading and writing.
1274  * Break actual connection if any was established.
1275  * Do not attempt to send anything upon SOCK_Close().
1276  * This call is available for stream sockets only.
1277  * @note Even though the underlying OS socket handle may have been marked for
1278  * preservation via fSOCK_KeepOnClose, this call always and
1279  * unconditionally closes and destroys the actual OS handle.
1280  * @param sock
1281  * [in] socket handle
1282  */
1284 (SOCK sock
1285  );
1286 
1287 
1288 /** Get local port of the socket (true or cached / stored).
1289  * For most users, a simpler SOCK_GetLocalPort() call is going to be the
1290  * most suitable. This call allows to inquire the network level about
1291  * a temporary port number assigned when a socket was created as a result
1292  * of accepting the connection (otherwise, the listening socket port number
1293  * gets returned as the local port). For connecting sockets, both "trueport"
1294  * and no "trueport" results are identical (with the exception that "trueport"
1295  * causes an additional system call, and the result is not stored).
1296  * @param sock
1297  * [in] socket handle
1298  * @param trueport
1299  * [in] non-zero causes to refetch / no-cache port from the network layer
1300  * @param byte_order
1301  * [in] byte order for port on return
1302  * @return
1303  * The port number in requested byte order, or 0 in case of an error.
1304  * @sa
1305  * SOCK_GetLocalPort
1306  */
1307 extern NCBI_XCONNECT_EXPORT unsigned short SOCK_GetLocalPortEx
1308 (SOCK sock,
1309  int/*bool*/ trueport,
1310  ENH_ByteOrder byte_order
1311  );
1312 
1313 
1314 /** Get local port of the socket.
1315  * The returned port number is also cached within "sock" so any further
1316  * inquires for the local port do not cause any system calls to occur.
1317  * The call is exactly equavalent to SOCK_GetLocalPortEx(sock, 0, byte_order).
1318  * @param sock
1319  * [in] socket handle
1320  * @param byte_order
1321  * [in] byte order for port on return
1322  * @return
1323  * Local port number in requested byte order, or 0 in case of an error.
1324  * @sa
1325  * SOCK_GetLocalPortEx
1326  */
1327 extern NCBI_XCONNECT_EXPORT unsigned short SOCK_GetLocalPort
1328 (SOCK sock,
1329  ENH_ByteOrder byte_order
1330  );
1331 
1332 
1333 /** Get host and port of the socket's peer (remote end).
1334  * @param sock
1335  * [in] socket handle
1336  * @param host
1337  * [out] the peer's host (can be NULL, then not filled in)
1338  * @param port
1339  * [out] the peer's port (can be NULL, then not filled in)
1340  * @param byte_order
1341  * [in] byte order for either host or port, or both, on return
1342  * @return
1343  * Host/port addresses in requested byte order, or 0 in case of an error.
1344  * @sa
1345  * SOCK_GetLocalPort
1346  */
1348 (SOCK sock,
1349  unsigned int* host,
1350  unsigned short* port,
1351  ENH_ByteOrder byte_order
1352  );
1353 
1354 
1355 /** Get remote port of the socket (the port it is connected to).
1356  * The call is provided as a counterpart for SOCK_GetLocalPort(), and is
1357  * equivalent to calling SOCK_GetPeerAddress(sock, 0, &port, byte_order).
1358  * @param sock
1359  * [in] socket handle
1360  * @param byte_order
1361  * [in] byte order for port on return
1362  * @return
1363  * Remote port number in requested byte order, or 0 in case of an error.
1364  * @sa
1365  * SOCK_GetPeerAddress, SOCK_GetLocalPort
1366  */
1367 extern NCBI_XCONNECT_EXPORT unsigned short SOCK_GetRemotePort
1368 (SOCK sock,
1369  ENH_ByteOrder byte_order
1370  );
1371 
1372 
1373 /** Get textual representation of the socket's peer.
1374  * For INET domain sockets, the result is of the form "aaa.bbb.ccc.ddd:ppppp";
1375  * for UNIX domain socket, the result is the name of the socket's file.
1376  * @param sock
1377  * [in] socket handle
1378  * @param buf
1379  * [out] pointer to provided buffer to store the text to
1380  * @param bufsize
1381  * [in] usable size of the buffer above
1382  * @param format
1383  * [in] what parts of address to include
1384  * @return
1385  * On success, return its "buf" argument; return 0 on error.
1386  * @sa
1387  * SOCK_HostPortToString
1388  */
1389 typedef enum {
1390  eSAF_Full = 0, /**< address in full, native form */
1391  eSAF_Port, /**< only numeric port if INET socket, empty otherwise */
1392  eSAF_IP /**< only numeric IP if INET socket, empty otherwise */
1394 
1396 (SOCK sock,
1397  char* buf,
1398  size_t bufsize,
1400  );
1401 
1402 
1403 /** Equivalent to SOCK_GetPeerAddressStringEx(.,.,.,eSAF_Full) */
1405 (SOCK sock,
1406  char* buf,
1407  size_t bufsize
1408  );
1409 
1410 
1411 /** Get an OS-dependent native socket handle to use by platform-specific API.
1412  * FYI: on MS-Windows it will be "SOCKET", on other platforms -- "int".
1413  * @param sock
1414  * [in] socket handle
1415  * @param handle_buf
1416  * [out] pointer to a memory area to put the OS handle at
1417  * @param handle_size
1418  * [in] the exact(!) size of the expected OS handle
1419  * @param ownership
1420  * eTakeOwnership removes the handle from SOCK; eNoOwnership retains it
1421  * @note
1422  * If handle ownership is taken, all operations with this SOCK (except for
1423  * SOCK_Close[Ex]()) will fail.
1424  * @sa
1425  * SOCK_OSHandleSize, SOCK_GetOSHandle, SOCK_CloseOSHandle, SOCK_CloseEx
1426  */
1428 (SOCK sock,
1429  void* handle_buf,
1430  size_t handle_size,
1431  EOwnership ownership
1432  );
1433 
1434 
1435 /** Same as SOCK_GetOSHandleEx(sock, handle_buf, handle_size, eNoOwnership).
1436  * @sa
1437  * SOCK_GetOSHandleEx
1438  */
1440 (SOCK sock,
1441  void* handle_buf,
1442  size_t handle_size
1443  );
1444 
1445 
1446 /** By default, sockets will not try to read data from inside SOCK_Write().
1447  * If you want to automagically upread the data (and cache it in the internal
1448  * socket buffer) when the write operation is not immediately available,
1449  * call this func with "on_off" == eOn.
1450  * Pass "on_off" as eDefault to get current setting.
1451  * @param on_off
1452  *
1453  * @return
1454  * Prior setting
1455  */
1457 (ESwitch on_off
1458  );
1459 
1460 
1461 /** Control the reading-while-writing feature for socket "sock" individually.
1462  * To reset to the global default behavior (as set by
1463  * SOCK_SetReadOnWriteAPI()), call this function with "on_off" == eDefault.
1464  * @param sock
1465  * [in] socket handle
1466  * @param on_off
1467  * [in] R-on-W setting for this socket
1468  * @return
1469  * Prior setting
1470  * @sa
1471  * SOCK_Read
1472  */
1474 (SOCK sock,
1475  ESwitch on_off
1476  );
1477 
1478 
1479 /** Control OS-defined send strategy by disabling/enabling the TCP layer to
1480  * send incomplete network frames (packets). With the "cork" set on, data gets
1481  * always buffered until a complete hardware packet is full (or connection is
1482  * about to close), and only then is sent out to the wire.
1483  * @note The setting cancels the effects of SOCK_DisableOSSendDelay().
1484  * @param sock
1485  * [in] socket handle [stream socket only]
1486  * @param on_off
1487  * [in] 1 to set the cork; 0 to remove the cork
1488  * @sa
1489  * SOCK_Write, SOCK_DisableOSSendDelay
1490  */
1492 (SOCK sock,
1493  int/*bool*/ on_off
1494  );
1495 
1496 
1497 /** Control OS-defined send strategy by disabling/enabling the TCP Nagle
1498  * algorithm (which is on by default) that packs multiple requests into a
1499  * single packet and thus transfers the data in fewer transactions, miminizing
1500  * the network traffic and generally bursting the throughput. However, some
1501  * applications may find it useful to disable this default behavior for the
1502  * sake of their performance increase (like in case of short transactions
1503  * otherwise held off by the system in anticipation of coalescing into larger
1504  * chunks -- a typical example is an interactive transmission of keystrokes).
1505  * Disabling the Nagle algorithm causes all internally pending yet
1506  * untransmitted data to flush down to the hardware.
1507  * @note The setting is overridden by SOCK_SetCork() but it still performs the
1508  * flush, if set to disable.
1509  * @param sock
1510  * [in] socket handle [stream socket only]
1511  * @param on_off
1512  * [in] 1 to disable the send delay; 0 to enable the send delay
1513  * @sa
1514  * SOCK_Write, SOCK_SetCork
1515  */
1517 (SOCK sock,
1518  int/*bool*/ on_off
1519  );
1520 
1521 
1522 
1523 /******************************************************************************
1524  * DATAGRAM SOCKETS (connectionless)
1525  *
1526  * How the datagram exchange API works:
1527  *
1528  * Datagram socket is created with special DSOCK_Create[Ex]() calls but the
1529  * resulting object is still a SOCK handle. That is, almost all SOCK routines
1530  * may be applied to the handle. There are few exceptions, though.
1531  * In datagram sockets the I/O differs from how it is done in stream sockets:
1532  *
1533  * SOCK_Write() writes data into an internal message buffer, appending new
1534  * data as they come with each SOCK_Write(). When the message is complete,
1535  * SOCK_SendMsg() should be called (optionally with an additional last,
1536  * or the only [if no SOCK_Write() preceded the call] message fragment)
1537  * to actually send the message down the wire. If successful, SOCK_SendMsg()
1538  * clears the internal buffer, and the process may repeat. If unsuccessful,
1539  * SOCK_SendMsg() can be repeated with restiction that no additional data are
1540  * provided in the call. This way, the entire message will be attempted to
1541  * be sent again. On the other hand, if after any SOCK_SendMsg() new data
1542  * are added [regardless of whether previous data were successfully sent
1543  * or not], all previously written [and kept in the internal send buffer]
1544  * data get dropped and replaced with the new data.
1545  *
1546  * DSOCK_WaitMsg() can be used to learn whether there is a new message
1547  * available for read by DSOCK_RecvMsg() immediately.
1548  *
1549  * SOCK_RecvMsg() receives the message into an internal receive buffer,
1550  * and optionally can return the initial datagram fragment via provided
1551  * buffer [this initial fragment is then stripped from what remains unread
1552  * in the internal buffer]. Optimized version can supply a maximal message
1553  * size (if known in advance), or 0 to get a message of any allowed size.
1554  * The actual size of the received message can be obtained via a
1555  * pointer-type argument "msgsize". The message kept in the internal buffer
1556  * can be read out in several SOCK_Read() calls, last returning eIO_Closed,
1557  * when all data have been taken out. SOCK_Wait() returns eIO_Success while
1558  * there are data in the internal message buffer that SOCK_Read() can read.
1559  *
1560  * SOCK_WipeMsg() can be used to clear the internal message buffers in
1561  * either eIO_Read or eIO_Write directions, meaning receive and send
1562  * buffers correspondingly.
1563  */
1564 
1565 
1566 /** Create a datagram socket.
1567  * @param sock
1568  * [out] socket created
1569  * @param flags
1570  * [in] additional socket properties
1571  */
1573 (SOCK* sock,
1575  );
1576 
1577 
1578 /** Same as DSOCK_CreateEx(, fSOCK_LogDefault)
1579  * @param sock
1580  * [out] socket created
1581  */
1583 (SOCK* sock
1584  );
1585 
1586 
1587 /** Assosiate a datagram socket with a local port.
1588  * All other attempts to use the same port will result in eIO_Closed (for "port
1589  * busy") unless SOCK_SetReuseAddress() is called, which then allows multiple
1590  * sockets to bind to the same port, and receive messages, in undefined order,
1591  * arriving at that port.
1592  * Passing 0 will ask the OS to automatically choose an unused port, which then
1593  * can be obtained via SOCK_GetLocalPort().
1594  * @param sock
1595  * [in] SOCK from DSOCK_Create[Ex]()
1596  * @param port
1597  * [in] port to bind to (0 to auto-choose)
1598  * @sa
1599  * SOCK_SetReuseAddress, SOCK_GetLocalPort
1600  */
1602 (SOCK sock,
1603  unsigned short port
1604  );
1605 
1606 
1607 /** Associate a datagram socket with a destination address.
1608  * @param sock
1609  * [in] SOCK from DSOCK_Create[Ex]()
1610  * @param host
1611  * [in] peer host
1612  * @param port
1613  * [in] peer port
1614  */
1616 (SOCK sock,
1617  const char* host,
1618  unsigned short port
1619  );
1620 
1621 
1622 /** Wait for a datagram in a datagram socket.
1623  * @param sock
1624  * [in] SOCK from DSOCK_Create[Ex]()
1625  * @param timeout
1626  * [in] time to wait for message
1627  */
1629 (SOCK sock,
1630  const STimeout* timeout
1631  );
1632 
1633 
1634 /** Send a datagram to a datagram socket.
1635  * @param sock
1636  * [in] SOCK from DSOCK_Create[Ex]()
1637  * @param host
1638  * [in] hostname or dotted IP
1639  * @param port
1640  * [in] port number, host byte order
1641  * @param data
1642  * [in] additional data to send
1643  * @param datalen
1644  * [in] size of additional data (bytes)
1645  * @sa
1646  * SOCK_Write, SOCK_SetTimeout
1647  */
1649 (SOCK sock,
1650  const char* host,
1651  unsigned short port,
1652  const void* data,
1653  size_t datalen
1654  );
1655 
1656 
1657 /** Receive a datagram from a datagram socket.
1658  * @param sock
1659  * [in] SOCK from DSOCK_Create[Ex]()
1660  * @param buf
1661  * [in] buf to store msg at, may be NULL
1662  * @param bufsize
1663  * [in] buf length provided
1664  * @param maxmsglen
1665  * [in] maximal expected message len
1666  * @param msglen
1667  * [out] actual msg size, may be NULL
1668  * @param sender_addr
1669  * [out] net byte order, may be NULL
1670  * @param sender_port
1671  * [out] host byte order, may be NULL
1672  * @sa
1673  * SOCK_Read, SOCK_SetTimeout
1674  */
1676 (SOCK sock,
1677  void* buf,
1678  size_t bufsize,
1679  size_t maxmsglen,
1680  size_t* msglen,
1681  unsigned int* sender_addr,
1682  unsigned short* sender_port
1683 );
1684 
1685 
1686 /** Clear message froma datagram socket.
1687  * @param sock
1688  * [in] SOCK from DSOCK_Create[Ex]()
1689  * @param direction
1690  * [in] either of eIO_Read (incoming), eIO_Write (outgoing)
1691  */
1693 (SOCK sock,
1694  EIO_Event direction
1695  );
1696 
1697 
1698 /** Set a datagram socket for broadcast.
1699  * @param sock
1700  * [in] SOCK from DSOCK_Create[Ex]()
1701  * @param broadcast
1702  * [in] set(1)/unset(0) broadcast capability
1703  */
1705 (SOCK sock,
1706  int/*bool*/ broadcast
1707  );
1708 
1709 
1710 /** Get message count.
1711  * @param sock
1712  * [in] socket handle (datagram socket only)
1713  * @param direction
1714  * [in] either eIO_Read (in) or eIO_Write (out)
1715  * @return
1716  * Total number of messages sent or received through this datagram socket.
1717  */
1719 (SOCK sock,
1720  EIO_Event direction
1721  );
1722 
1723 
1724 
1725 /******************************************************************************
1726  * TYPE & STATISTICS INFORMATION FOR [D]SOCK SOCKETS
1727  */
1728 
1729 
1730 /** Check whether a socket is a datagram one.
1731  * @param sock
1732  * [in] socket handle
1733  * @return
1734  * Non-zero value if socket "sock" was created by DSOCK_Create[Ex]().
1735  * Return zero otherwise.
1736  */
1737 extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_IsDatagram(SOCK sock);
1738 
1739 
1740 /** Check whether a socket is client-side.
1741  * @param sock
1742  * [in] socket handle
1743  * @return
1744  * Non-zero value if socket "sock" was created by SOCK_Create[Ex]().
1745  * Return zero otherwise.
1746  */
1747 extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_IsClientSide(SOCK sock);
1748 
1749 
1750 /** Check whether a socket is server-side.
1751  * @param sock
1752  * [in] socket handle
1753  * @return
1754  * Non-zero value if socket "sock" was created by LSOCK_Accept().
1755  * Return zero otherwise.
1756  */
1757 extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_IsServerSide(SOCK sock);
1758 
1759 
1760 /** Check whether a socket is UNIX type.
1761  * @param sock
1762  * [in] socket handle
1763  * @return
1764  * Non-zero value if socket "sock" is a UNIX family named socket
1765  * Return zero otherwise.
1766  */
1767 extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_IsUNIX(SOCK sock);
1768 
1769 
1770 /** Check whether a socket is using SSL (Secure Socket Layer).
1771  * @param sock
1772  * [in] socket handle
1773  * @return
1774  * Non-zero value if socket "sock" is using Secure Socket Layer (SSL).
1775  * Return zero otherwise.
1776  */
1777 extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_IsSecure(SOCK sock);
1778 
1779 
1780 /** Get current read or write position within a socket.
1781  * @param sock
1782  * [in] socket handle
1783  * @param direction
1784  * [in] either eIO_Read or eIO_Write
1785  * @return
1786  * Current read or write logical position, which takes any pending (i.e. still
1787  * unwritten or yet unread) data into account, by reporting / not reporting
1788  * that, respectively, in the position returned. For datagram sockets, the
1789  * position is always relative to the beginning of the current message, and
1790  * gets reset with every new message (i.e. does not span across the messages).
1791  */
1793 (SOCK sock,
1794  EIO_Event direction
1795  );
1796 
1797 
1798 /** Get counts of read or written bytes.
1799  * @param sock
1800  * [in] socket handle
1801  * @param direction
1802  * [in] either eIO_Read or eIO_Write
1803  * @return
1804  * Count of bytes actually read or written through this socket in the current
1805  * session. For datagram sockets the count applies for the last message only;
1806  * for stream sockets it counts only since last accept or connect event.
1807  */
1809 (SOCK sock,
1810  EIO_Event direction
1811  );
1812 
1813 
1814 /** Get the total volume of data transferred by a socket.
1815  * @param sock
1816  * [in] socket handle
1817  * @param direction
1818  * [in] either eIO_Read or eIO_Write
1819  * @return
1820  * Total number of bytes transferred through the socket in its lifetime.
1821  */
1823 (SOCK sock,
1824  EIO_Event direction
1825  );
1826 
1827 
1828 /******************************************************************************
1829  * I/O RESTART ON SIGNALS
1830  */
1831 
1832 /** Control restartability of I/O interrupted by signals.
1833  * By default I/O is restartable if interrupted.
1834  * Pass "on_off" as eDefault to get the current setting.
1835  * @param on_off
1836  * [in] eOn to cancel I/O on signals; eOff to restart
1837  * @return
1838  * Prior setting
1839  * @sa
1840  * SOCK_SetInterruptOnSignal
1841  */
1843 (ESwitch on_off
1844  );
1845 
1846 
1847 /** Control restartability of I/O interrupted by signals on a per-socket basis.
1848  * eDefault causes the use of global API flag.
1849  * @param sock
1850  * [in] socket handle
1851  * @param on_off
1852  * [in] per-socket I/O restart behavior on signals
1853  * @return
1854  * Prior setting
1855  * @sa
1856  * SOCK_SetInterruptOnSignalAPI, SOCK_Create, DSOCK_Create
1857  */
1859 (SOCK sock,
1860  ESwitch on_off
1861  );
1862 
1863 
1864 /******************************************************************************
1865  * ADDRESS REUSE
1866  */
1867 
1868 /** Control address reuse for socket addresses taken by the API.
1869  * By default address is not marked for reuse in either SOCK or DSOCK,
1870  * but is always reused for LSOCK (upon socket closure).
1871  * Pass "on_off" as eDefault to get the current setting.
1872  * @param on_off
1873  * [in] whether to turn on (eOn), turn off (eOff) or get current (eDefault)
1874  * @return
1875  * Prior setting
1876  * @sa
1877  * SOCK_SetReuseAddress
1878  */
1880 (ESwitch on_off
1881  );
1882 
1883 
1884 /** Control reuse of socket addresses on per-socket basis
1885  * Note: only a boolean parameter value is can be used here.
1886  * @param sock
1887  * [in] socket handle
1888  * @param on_off
1889  * [in] whether to reuse the address (true, non-zero) or not (false, zero)
1890  * @sa
1891  * SOCK_SetReuseAddressAPI, SOCK_Create, DSOCK_Create
1892  */
1894 (SOCK sock,
1895  int/*bool*/ on_off
1896  );
1897 
1898 
1899 /******************************************************************************
1900  * DATA LOGGING & ERROR REPORTING
1901  *
1902  * @note Use CORE_SetLOG() from "ncbi_core.h" to set log handler.
1903  *
1904  * @sa
1905  * CORE_SetLOG
1906  */
1907 
1908 /** By default data are not logged.
1909  * @param log
1910  * To start logging the data, call this func with "log" == eOn.
1911  * To stop logging the data, call this func with "log" == eOff.
1912  * To get current log switch, call this func with "log" == eDefault.
1913  * @return
1914  * Prior (or current, if "log" is eDefault) setting
1915  * @sa
1916  * SOCK_SetDataLogging
1917  */
1919 (ESwitch log
1920  );
1921 
1922 
1923 /** Control the data logging for socket "sock" individually.
1924  * @param sock
1925  * [in] socket handle
1926  * @param log
1927  * [in] requested data logging
1928  * To reset to the global default behavior (as set by SOCK_SetDataLoggingAPI),
1929  * call this function with "log" == eDefault.
1930  * @return
1931  * Prior setting
1932  * @sa
1933  * SOCK_SetDataLoggingAPI, SOCK_Create, DSOCK_Create
1934  */
1936 (SOCK sock,
1937  ESwitch log
1938  );
1939 
1940 
1941 /** User-level error hook.
1942  */
1943 
1944 typedef enum {
1945  eSOCK_ErrInit = 1, /**< Socket layer initialization error */
1946  eSOCK_ErrDns, /**< DNS-related error (unresolvable hostname) */
1947  eSOCK_ErrIO /**< I/O-related error */
1949 
1950 typedef struct {
1951  ESOCK_ErrType type; /**< See ESOCK_ErrType */
1952  SOCK sock; /**< Non-NULL when SOCK-related */
1953  const char* host; /**< Host name/IP (or path for non-IP SOCK) */
1954  unsigned short port; /**< Port (host byte order), 0 for non-IP SOCK */
1955  EIO_Event event; /**< Meaningful only for the eSOCK_ErrIO type */
1956  EIO_Status status; /**< Status code about to be returned (if known) */
1957 } SSOCK_ErrInfo;
1958 
1959 
1960 typedef void (*FSOCK_ErrHook)(const SSOCK_ErrInfo* info,
1961  void* data);
1962 
1963 extern NCBI_XCONNECT_EXPORT
1965  void* data);
1966 
1967 
1968 
1969 /******************************************************************************
1970  * GENERIC POLLABLE INTERFACE (please see SOCK_Poll() above for explanations)
1971  */
1972 
1973 
1974 /*fwdecl; opaque*/
1975 struct SPOLLABLE_tag;
1976 typedef struct SPOLLABLE_tag* POLLABLE;
1977 
1978 typedef struct {
1982 } SPOLLABLE_Poll;
1983 
1984 /** Poll for I/O readiness.
1985  * @param n
1986  * [in] how many elements to scan in the "polls" array parameter
1987  * @param polls[]
1988  * [in/out] array of handles and event masks to check for I/O, and return
1989  * @param timeout
1990  * [in] how long to wait for at least one handle to get ready.
1991  * @param n_ready
1992  * [out] how many elements of the "polls" array returned with their
1993  * I/O marked ready
1994  * @return
1995  * eIO_Success if at least one element was found ready, eIO_Timeout if none
1996  * and the specified time interval had elapsed, other error code for some
1997  * other error condition (in which case the "revent" fields in the array may
1998  * not have been updated with valid values).
1999  * @sa
2000  * SOCK_Wait, SOCK_Poll
2001  */
2003 (size_t n,
2004  SPOLLABLE_Poll polls[],
2005  const STimeout* timeout,
2006  size_t* n_ready
2007  );
2008 
2009 
2010 /** Identifies a non-NULL POLLABLE. */
2012 
2013 
2014 /** Conversion utilities from handles to POLLABLEs, and back.
2015  * @return
2016  * Return 0 if conversion cannot be made; otherwise the converted handle.
2017  */
2024 
2025 
2026 
2027 /******************************************************************************
2028  * AUXILIARY NETWORK-SPECIFIC FUNCTIONS (added for the portability reasons)
2029  */
2030 
2031 
2032 /** Convert IP address to a string in dotted notation.
2033  * @param addr
2034  * [in] must be in the network byte-order
2035  * @param buf
2036  * [out] to be filled by smth. like "123.45.67.89\0"
2037  * @param bufsize
2038  * [in] max # of bytes to put into "buf" (including the terminating '\0')
2039  * @return
2040  * Zero on success, non-zero on error. Vaguely related to BSD's inet_ntoa().
2041  * On error (including insufficient room in "buf" to store the result) "buf"
2042  * is returned empty (buf[0] == '\0').
2043  */
2045 (unsigned int addr,
2046  char* buf,
2047  size_t bufsize
2048  );
2049 
2050 
2051 /** Check whether the given string represents a valid IPv4 address.
2052  * @param host
2053  * [in] '\0'-terminated string to check against being a plain IPv4 address
2054  * @param fullquad
2055  * [in] non-zero to only accept "host" if it is a full-quad IPv4 notation
2056  * @return
2057  * Non-zero (true) if given string is an IPv4 address, zero (false) otherwise.
2058  * @note
2059  * Unless "fullquad" is requested, valid IPv4 addresses may have various
2060  * formats (e.g. legal to be 32-bit integers, or have fewer than 3 dots).
2061  */
2062 extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_isipEx
2063 (const char* host,
2064  int/*bool*/ fullquad
2065  );
2066 
2067 
2068 /** Equivalent of SOCK_isip(host, 0)
2069  * @param host
2070  * [in] '\0'-terminated string to check against being a plain IPv4 address
2071  * @return
2072  * Non-zero (true) if given string is an IPv4 address, zero (false) otherwise.
2073  */
2074 extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_isip
2075 (const char* host
2076  );
2077 
2078 
2079 /** Check whether an address is a loopback one.
2080  * Return non-zero (true) if the IPv4 address (in network byte order) given
2081  * in the agrument, is a loopback one; zero otherwise.
2082  */
2084 (unsigned int ip
2085  );
2086 
2087 
2088 /** See man for the BSDisms, htonl() and htons().
2089  * @param value
2090  * The value to convert from host to network byte order.
2091  */
2092 extern NCBI_XCONNECT_EXPORT unsigned int SOCK_HostToNetLong
2093 (unsigned int value
2094  );
2095 
2096 #define SOCK_NetToHostLong SOCK_HostToNetLong
2097 
2098 /** See man for the BSDisms, htonl() and htons().
2099  * @param value
2100  * The value to convert from host to network byte order.
2101  */
2102 extern NCBI_XCONNECT_EXPORT unsigned short SOCK_HostToNetShort
2103 (unsigned short value
2104  );
2105 
2106 #define SOCK_NetToHostShort SOCK_HostToNetShort
2107 
2108 
2109 /* Deprecated: Use SOCK_{Host|Net}To{Net|Host}{Long|Short}() instead */
2110 #ifndef NCBI_DEPRECATED
2111 # define NCBI_SOCK_DEPRECATED
2112 #else
2113 # define NCBI_SOCK_DEPRECATED NCBI_DEPRECATED
2114 #endif
2116 unsigned int SOCK_htonl(unsigned int);
2117 #define SOCK_ntohl SOCK_htonl
2119 unsigned short SOCK_htons(unsigned short);
2120 #define SOCK_ntohs SOCK_htons
2121 
2122 
2123 /** Get the local host name.
2124  * @param name
2125  * [out] (guaranteed to be '\0'-terminated)
2126  * @param namelen
2127  * [in] max # of bytes allowed to put into "name"
2128  * @param log
2129  * [in] whether to log failures
2130  * @return
2131  * Zero on success, non-zero on error. See BSD gethostname().
2132  * On error (incl. insufficient room) "name" returned empty (name[0] == '\0').
2133  * @sa
2134  * SOCK_gethostname, SOCK_gethostbyaddr
2135  */
2137 (char* name,
2138  size_t namelen,
2139  ESwitch log
2140  );
2141 
2142 
2143 /** Same as SOCK_gethostnameEx(,,<current API data logging>)
2144  * @sa
2145  * SOCK_gethostnameEx, SOCK_gethostbyaddrEx, SOCK_SetDataLoggingAPI
2146  */
2148 (char* name,
2149  size_t namelen
2150  );
2151 
2152 
2153 /** Find and return IPv4 address of a named host. The call also accepts dotted
2154  * IPv4 notation, in which case the conversion is done without consulting the
2155  * name resolver (DNS).
2156  * @param hostname
2157  * [in] specified host, or the current host if hostname is either 0 or ""
2158  * @param log
2159  * [in] whether to log failures
2160  * @return
2161  * IPv4 host address (in network byte order) of the specified host (or of the
2162  * local host, if hostname is passed as NULL / empty), which can be specified
2163  * as either a domain name or an IPv4 address in the dotted notation (e.g.
2164  * "123.45.67.89\0"). Return 0 on error.
2165  * @note "0.0.0.0" and "255.255.255.255" are both considered invalid
2166  * @sa
2167  * SOCK_gethostbyname, SOCK_gethostname, NcbiStringToIPv4
2168  */
2169 extern NCBI_XCONNECT_EXPORT unsigned int SOCK_gethostbynameEx
2170 (const char* hostname,
2171  ESwitch log
2172  );
2173 
2174 
2175 /** Same as SOCK_gethostbynameEx(,<current API data logging>)
2176  * @sa
2177  * SOCK_gethostbynameEx, SOCK_SetDataLoggingAPI
2178  */
2179 extern NCBI_XCONNECT_EXPORT unsigned int SOCK_gethostbyname
2180 (const char* hostname
2181  );
2182 
2183 
2184 /** Take IPv4 host address (in network byte order) or 0 for current host, and
2185  * fill out the provided buffer with the name, which the address corresponds to
2186  * (in case of multiple names the primary name is used).
2187  * @param addr
2188  * [in] IPv4 host address (0 means current host) in network byte order
2189  * @param name
2190  * [out] buffer to store the name to
2191  * @param namelen
2192  * [in] size (bytes) of the buffer above
2193  * @param log
2194  * [in] whether to log failures
2195  * @return
2196  * Value 0 means error; success is denoted by the 'name' argument returned.
2197  * Note that on error the name gets emptied (name[0] == '\0').
2198  * @sa
2199  * SOCK_gethostbyaddr, SOCK_gethostname
2200  */
2201 extern NCBI_XCONNECT_EXPORT const char* SOCK_gethostbyaddrEx
2202 (unsigned int addr,
2203  char* name,
2204  size_t namelen,
2205  ESwitch log
2206  );
2207 
2208 
2209 /** Same as SOCK_gethostbyaddrEx(,,<current API data logging>)
2210  * @sa
2211  * SOCK_gethostbyaddrEx, SOCK_gethostnameEx, SOCK_SetDataLoggingAPI
2212  */
2213 extern NCBI_XCONNECT_EXPORT const char* SOCK_gethostbyaddr
2214 (unsigned int addr,
2215  char* name,
2216  size_t namelen
2217  );
2218 
2219 
2220 /** Get loopback IPv4 address.
2221  * @return
2222  * Loopback address (in network byte order).
2223  */
2224 extern NCBI_XCONNECT_EXPORT unsigned int SOCK_GetLoopbackAddress(void);
2225 
2226 
2227 /** Get (and cache for faster follow-up retrievals) IPv4 address of local host
2228  * @param reget
2229  * eOn to forcibly re-cache and return the address;
2230  * eDefault to re-cache only if unknown, return the [new] cached value;
2231  * eOff not to re-cache even if unknown, return whatever is available.
2232  * @return
2233  * Local address (in network byte order).
2234  * @sa
2235  * SOCK_gethostname
2236  */
2238 (ESwitch reget
2239  );
2240 
2241 
2242 /** Read (skipping leading blanks) "[host][:port]" from a string stopping
2243  * at either EOL or a blank character.
2244  * @param str
2245  * must not be NULL
2246  * @param host
2247  * may be NULL for no assignment
2248  * @param port
2249  * may be NULL for no assignment
2250  * @return
2251  * On success, return the advanced pointer past the host/port read.
2252  * If no host/port detected, return 'str'. On format error, return 0.
2253  * If either host or port fragment is missing, then the corresponding 'host'/
2254  * 'port' parameters get a value of 0.
2255  * @note "0.0.0.0" for the host part gets the host returned as 0 as well.
2256  * @note 'host' gets returned in network byte order, unlike 'port', which
2257  * always comes out in host (native) byte order.
2258  * @note ":0" is accepted to denote no-host:zero-port.
2259  * @sa
2260  * SOCK_HostPortToString, SOCK_isip, SOCK_gethostbyname
2261  */
2263 (const char* str,
2264  unsigned int* host,
2265  unsigned short* port
2266  );
2267 
2268 
2269 /** Print numeric string "host:port" into a buffer provided, not to exceed
2270  * 'bufsize' bytes (including the teminating '\0' character). Suppress
2271  * printing the "host" if the 'host' parameter is zero. Suppress printing the
2272  * ":port" part if 'port' passed as zero, but if both the 'host' and the 'port'
2273  * parameters are zero, output is the literal ":0".
2274  * @param host
2275  * IPv4 in network byte order
2276  * @param port
2277  * in host byte order
2278  * @param buf
2279  * must not be NULL
2280  * @param bufsize
2281  * must be large enough
2282  * @return
2283  * Number of bytes printed, or 0 on error (e.g. buffer is too small).
2284  * @sa
2285  * SOCK_StringToHostPort, SOCK_ntoa, SOCK_gethostbyaddr
2286  */
2288 (unsigned int host,
2289  unsigned short port,
2290  char* buf,
2291  size_t bufsize
2292  );
2293 
2294 
2295 
2296 /******************************************************************************
2297  * SECURE SOCKET LAYER (SSL) SUPPORT
2298  */
2299 
2300 /*fwdecl*/
2301 struct SNcbiCred;
2302 /** Opaque type for credentials */
2303 typedef struct SNcbiCred* NCBI_CRED;
2304 
2305 /*fwdecl*/
2306 struct SOCKSSL_struct;
2307 /** Opaque type for SSL implementation */
2308 typedef const struct SOCKSSL_struct* SOCKSSL;
2309 
2310 
2311 /** SSL setup callback */
2312 typedef SOCKSSL (*FSSLSetup)(void);
2313 
2314 
2315 /** Store SSL setup callback until actual initialization.
2316  * @param setup
2317  * non-NULL SSL setup routine, or NULL to shut the SSL down
2318  * @warning
2319  * Do _not_ use this function unless you know what you're doing.
2320  * Use other means of initialization such as CONNECT_Init() or CConnIniter.
2321  * @sa
2322  * SOCK_SetupSSLEx, CONNECT_Init, CConnIniter
2323  */
2325 
2326 
2327 /** Take SSL setup callback, and then init the entire API (SOCK + SSL).
2328  * @param setup
2329  * non-NULL SSL setup routine, or NULL to shut the SSL down
2330  * @return
2331  * eIO_Success if successful, other code on error.
2332  * @warning
2333  * Do _not_ use this function unless you know what you're doing.
2334  * Use other means of initialization such as CONNECT_Init() or CConnIniter.
2335  * @sa
2336  * SOCK_SetupSSL, CONNECT_Init, CConnIniter
2337  */
2339 
2340 
2341 /** Return an SSL provider name (in a const static storage), with special
2342  * considerations: return NULL if SSL has not been set up (yet), and return
2343  * "" if SSL has not been (properly) initialized for use.
2344  * @note
2345  * "NONE" is returned as a name if SSL has been explicitly disabled.
2346  * @sa
2347  * SOCK_SetupSSL, SOCK_SetupSSLEx
2348  */
2349 extern NCBI_XCONNECT_EXPORT const char* SOCK_SSLName(void);
2350 
2351 
2352 #ifdef __cplusplus
2353 } /* extern "C" */
2354 #endif
2355 
2356 
2357 /* @} */
2358 
2359 #endif /* CONNECT___NCBI_SOCKET__H */
static uch flags
static const char ip[]
Definition: des.c:75
static const char * str(char *buf, int n)
Definition: stats.c:84
char data[12]
Definition: iconv.c:80
@ eOff
Definition: ncbi_types.h:110
@ eDefault
Definition: ncbi_types.h:112
@ eOn
Definition: ncbi_types.h:111
void destroy(P pT)
TRIGGER POLLABLE_ToTRIGGER(POLLABLE)
Definition: ncbi_socket.c:8634
struct SNcbiCred * NCBI_CRED
Opaque type for credentials.
Definition: ncbi_socket.h:2303
LSOCK POLLABLE_ToLSOCK(POLLABLE)
Definition: ncbi_socket.c:8641
const char * SOCK_StringToHostPort(const char *str, unsigned int *host, unsigned short *port)
Read (skipping leading blanks) "[host][:port]" from a string stopping at either EOL or a blank charac...
Definition: ncbi_socket.c:8901
void SOCK_GetPeerAddress(SOCK sock, unsigned int *host, unsigned short *port, ENH_ByteOrder byte_order)
Get host and port of the socket's peer (remote end).
Definition: ncbi_socket.c:7612
char * SOCK_GetPeerAddressStringEx(SOCK sock, char *buf, size_t bufsize, ESOCK_AddressFormat format)
Definition: ncbi_socket.c:7652
int SOCK_isipEx(const char *host, int fullquad)
Check whether the given string represents a valid IPv4 address.
Definition: ncbi_socket.c:8680
ESwitch SOCK_SetReadOnWrite(SOCK sock, ESwitch on_off)
Control the reading-while-writing feature for socket "sock" individually.
Definition: ncbi_socket.c:7761
EIO_Status TRIGGER_Set(TRIGGER trigger)
Set an event trigger.
Definition: ncbi_socket.c:6377
struct SOCK_tag * SOCK
Definition: ncbi_socket.h:223
int SOCK_IsSecure(SOCK sock)
Check whether a socket is using SSL (Secure Socket Layer).
Definition: ncbi_socket.c:8464
EIO_Status SOCK_SetTimeout(SOCK sock, EIO_Event event, const STimeout *timeout)
Specify timeout for the connection I/O (see SOCK_[Read|Write|Close]()).
Definition: ncbi_socket.c:7194
void SOCK_DisableOSSendDelay(SOCK sock, int on_off)
Control OS-defined send strategy by disabling/enabling the TCP Nagle algorithm (which is on by defaul...
Definition: ncbi_socket.c:7815
unsigned short port
Port (host byte order), 0 for non-IP SOCK.
Definition: ncbi_socket.h:1954
unsigned int SOCK_HostToNetLong(unsigned int value)
See man for the BSDisms, htonl() and htons().
Definition: ncbi_socket.c:8736
EIO_Status SOCK_Shutdown(SOCK sock, EIO_Event how)
Shutdown the connection in only one direction (specified by "direction").
Definition: ncbi_socket.c:6797
unsigned short SOCK_GetRemotePort(SOCK sock, ENH_ByteOrder byte_order)
Get remote port of the socket (the port it is connected to).
Definition: ncbi_socket.c:7635
EIO_Event event
[in] SOCK to poll (NULL if not to poll)
Definition: ncbi_socket.h:968
EIO_Status SOCK_Close(SOCK sock)
Close the SOCK handle, and destroy all relevant internal data.
Definition: ncbi_socket.c:6851
size_t SOCK_HostPortToString(unsigned int host, unsigned short port, char *buf, size_t bufsize)
Print numeric string "host:port" into a buffer provided, not to exceed 'bufsize' bytes (including the...
Definition: ncbi_socket.c:8945
EIO_Status DSOCK_SetBroadcast(SOCK sock, int broadcast)
Set a datagram socket for broadcast.
Definition: ncbi_socket.c:8361
EIO_Status LSOCK_Accept(LSOCK lsock, const STimeout *timeout, SOCK *sock)
[SERVER-side] Accept connection from a client.
Definition: ncbi_socket.c:6538
ESOCK_AddressFormat
Get textual representation of the socket's peer.
Definition: ncbi_socket.h:1389
EIO_Status DSOCK_RecvMsg(SOCK sock, void *buf, size_t bufsize, size_t maxmsglen, size_t *msglen, unsigned int *sender_addr, unsigned short *sender_port)
Receive a datagram from a datagram socket.
Definition: ncbi_socket.c:8219
void(* FSOCK_ErrHook)(const SSOCK_ErrInfo *info, void *data)
Definition: ncbi_socket.h:1960
EIO_Event revent
Definition: ncbi_socket.h:1981
const char * SOCK_SSLName(void)
Return an SSL provider name (in a const static storage), with special considerations: return NULL if ...
Definition: ncbi_socket.c:9017
EIO_Status DSOCK_Connect(SOCK sock, const char *host, unsigned short port)
Associate a datagram socket with a destination address.
Definition: ncbi_socket.c:8073
ESwitch SOCK_SetReuseAddressAPI(ESwitch on_off)
Control address reuse for socket addresses taken by the API.
Definition: ncbi_socket.c:8549
EIO_Status SOCK_Read(SOCK sock, void *buf, size_t size, size_t *n_read, EIO_ReadMethod how)
Read/peek up to "size" bytes from "sock" to a buffer pointed to by "buf".
Definition: ncbi_socket.c:7270
ESwitch SOCK_SetInterruptOnSignal(SOCK sock, ESwitch on_off)
Control restartability of I/O interrupted by signals on a per-socket basis.
Definition: ncbi_socket.c:8541
EIO_Status SOCK_Status(SOCK sock, EIO_Event direction)
Return low-level socket I/O status of *last* socket operation.
Definition: ncbi_socket.c:7459
EIO_Status SOCK_CloseOSHandle(const void *handle, size_t handle_size)
Close socket OS handle (ungracefully aborting the connection if necessary).
Definition: ncbi_socket.c:6890
unsigned int SOCK_gethostbynameEx(const char *hostname, ESwitch log)
Find and return IPv4 address of a named host.
Definition: ncbi_socket.c:8780
EIO_Status POLLABLE_Poll(size_t n, SPOLLABLE_Poll polls[], const STimeout *timeout, size_t *n_ready)
Poll for I/O readiness.
Definition: ncbi_socket.c:8597
EIO_Status TRIGGER_Reset(TRIGGER trigger)
Reset the trigger.
Definition: ncbi_socket.c:6477
EIO_Status SOCK_InitializeAPI(void)
Initialize all internal/system data & resources to be used by the SOCK API.
Definition: ncbi_socket.c:991
#define NCBI_SOCK_DEPRECATED
Definition: ncbi_socket.h:2113
unsigned int SOCK_htonl(unsigned int)
Definition: ncbi_socket.c:8748
int SOCK_gethostname(char *name, size_t namelen)
Same as SOCK_gethostnameEx(,,<current API data logging>)
Definition: ncbi_socket.c:8774
unsigned short port
Port number, host byte order, never 0.
Definition: ncbi_socket.h:346
void SOCK_SetupSSL(FSSLSetup setup)
Store SSL setup callback until actual initialization.
Definition: ncbi_socket.c:8998
POLLABLE POLLABLE_FromSOCK(SOCK)
Conversion utilities from handles to POLLABLEs, and back.
Definition: ncbi_socket.c:8627
void SOCK_SetCork(SOCK sock, int on_off)
Control OS-defined send strategy by disabling/enabling the TCP layer to send incomplete network frame...
Definition: ncbi_socket.c:7773
void SOCK_AllowSigPipeAPI(void)
By default (on UNIX platforms) the SOCK API functions automagically call "signal(SIGPIPE,...
Definition: ncbi_socket.c:1060
ENH_ByteOrder
Network and host byte order enumeration type.
Definition: ncbi_socket.h:192
ESOCK_Type POLLABLE_What(POLLABLE)
Identifies a non-NULL POLLABLE.
Definition: ncbi_socket.c:8606
EIO_Status SOCK_CloseEx(SOCK sock, int destroy)
Close the SOCK handle, and conditionally destroy relevant internal data.
Definition: ncbi_socket.c:6857
void SOCK_SetApproveHookAPI(FSOCK_ApproveHook hook, void *data)
The hook is installed when non-NULL, and de-installed otherwise.
Definition: ncbi_socket.c:1623
ESOCK_IOWaitSysAPI SOCK_SetIOWaitSysAPI(ESOCK_IOWaitSysAPI api)
This is a helper call that can improve I/O performance (ignored for MSVC).
Definition: ncbi_socket.c:1086
unsigned int SOCK_GetLoopbackAddress(void)
Get loopback IPv4 address.
Definition: ncbi_socket.c:8821
int SOCK_IsServerSide(SOCK sock)
Check whether a socket is server-side.
Definition: ncbi_socket.c:8446
unsigned short SOCK_HostToNetShort(unsigned short value)
See man for the BSDisms, htonl() and htons().
Definition: ncbi_socket.c:8742
ESOCK_Side
Sides of socket.
Definition: ncbi_socket.h:210
EIO_Event event
Definition: ncbi_socket.h:1980
ESOCK_Type type
eSOCK_Socket (conn) / eSOCK_Datagram (packet)
Definition: ncbi_socket.h:348
int SOCK_IsLoopbackAddress(unsigned int ip)
Check whether an address is a loopback one.
Definition: ncbi_socket.c:8718
TNCBI_BigCount SOCK_GetPosition(SOCK sock, EIO_Event direction)
Get current read or write position within a socket.
Definition: ncbi_socket.c:8471
unsigned int SOCK_gethostbyname(const char *hostname)
Same as SOCK_gethostbynameEx(,<current API data logging>)
Definition: ncbi_socket.c:8790
void SOCK_SetErrHookAPI(FSOCK_ErrHook hook, void *data)
Definition: ncbi_socket.c:384
EIO_Status SOCK_Pushback(SOCK sock, const void *data, size_t size)
Push the specified data back to the socket's input queue (in the socket's internal read buffer).
Definition: ncbi_socket.c:7439
EIO_Status SOCK_Wait(SOCK sock, EIO_Event event, const STimeout *timeout)
Block on the socket until either the specified "event" is available or "timeout" expires (if "timeout...
Definition: ncbi_socket.c:6953
EIO_Status SOCK_CreateEx(const char *host, unsigned short port, const STimeout *timeout, SOCK *sock, const void *data, size_t size, TSOCK_Flags flags)
[CLIENT-side] Connect client to another(server-side, listening) socket (socket() + connect() [+ selec...
Definition: ncbi_socket.c:6648
ESwitch SOCK_SetDataLoggingAPI(ESwitch log)
By default data are not logged.
Definition: ncbi_socket.c:8574
EIO_Status SOCK_Abort(SOCK sock)
If there is outstanding connection or output data pending, cancel it.
Definition: ncbi_socket.c:7557
EIO_Status SOCK_GetOSHandleEx(SOCK sock, void *handle_buf, size_t handle_size, EOwnership ownership)
Get an OS-dependent native socket handle to use by platform-specific API.
Definition: ncbi_socket.c:7706
EIO_Status LSOCK_AcceptEx(LSOCK lsock, const STimeout *timeout, SOCK *sock, TSOCK_Flags flags)
[SERVER-side] Accept connection from a client.
Definition: ncbi_socket.c:6546
int SOCK_ntoa(unsigned int addr, char *buf, size_t bufsize)
Convert IP address to a string in dotted notation.
Definition: ncbi_socket.c:8661
struct LSOCK_tag * LSOCK
Definition: ncbi_socket.h:220
EIO_Status TRIGGER_Create(TRIGGER *trigger, ESwitch log)
Create an event trigger.
Definition: ncbi_socket.c:6180
ESwitch SOCK_SetReadOnWriteAPI(ESwitch on_off)
By default, sockets will not try to read data from inside SOCK_Write().
Definition: ncbi_socket.c:7752
ESOCK_IOWaitSysAPI
Selector of I/O wait system API: auto, poll(), or select().
Definition: ncbi_socket.h:384
EIO_Status DSOCK_Create(SOCK *sock)
Same as DSOCK_CreateEx(, fSOCK_LogDefault)
Definition: ncbi_socket.c:7857
unsigned int SOCK_GetLocalHostAddress(ESwitch reget)
Get (and cache for faster follow-up retrievals) IPv4 address of local host.
Definition: ncbi_socket.c:8827
const struct SOCKSSL_struct * SOCKSSL
Opaque type for SSL implementation.
Definition: ncbi_socket.h:2308
EIO_Status TRIGGER_Close(TRIGGER trigger)
Close an event trigger.
Definition: ncbi_socket.c:6341
EIO_Status SOCK_ReadLine(SOCK sock, char *line, size_t size, size_t *n_read)
Read a line from SOCK.
Definition: ncbi_socket.c:7329
int SOCK_IsDatagram(SOCK sock)
Check whether a socket is a datagram one.
Definition: ncbi_socket.c:8432
EIO_Status LSOCK_GetOSHandle(LSOCK lsock, void *handle_buf, size_t handle_size)
Same as LSOCK_GetOSHandleEx(lsock, handle_buf, handle_size, eNoOwnership).
Definition: ncbi_socket.c:6608
const char * host
Textual hostname if provided for outgoing.
Definition: ncbi_socket.h:344
EIO_Status SOCK_Write(SOCK sock, const void *data, size_t size, size_t *n_written, EIO_WriteMethod how)
Write "size" bytes of "data" to "sock".
Definition: ncbi_socket.c:7486
EIO_Status DSOCK_WaitMsg(SOCK sock, const STimeout *timeout)
Wait for a datagram in a datagram socket.
Definition: ncbi_socket.c:8170
TNCBI_BigCount SOCK_GetTotalCount(SOCK sock, EIO_Event direction)
Get the total volume of data transferred by a socket.
Definition: ncbi_socket.c:8509
unsigned int addr
IPv4 (or -1 if unknown / broadcast), never 0.
Definition: ncbi_socket.h:345
EIO_Status SOCK_Reconnect(SOCK sock, const char *host, unsigned short port, const STimeout *timeout)
[CLIENT-side] Close the socket referred to by "sock" and then connect it to another "host:port"; fail...
Definition: ncbi_socket.c:6739
const char * host
Host name/IP (or path for non-IP SOCK)
Definition: ncbi_socket.h:1953
int SOCK_gethostnameEx(char *name, size_t namelen, ESwitch log)
Get the local host name.
Definition: ncbi_socket.c:8760
int SOCK_IsClientSide(SOCK sock)
Check whether a socket is client-side.
Definition: ncbi_socket.c:8439
EIO_Status DSOCK_SendMsg(SOCK sock, const char *host, unsigned short port, const void *data, size_t datalen)
Send a datagram to a datagram socket.
Definition: ncbi_socket.c:8272
ESOCK_ErrType
User-level error hook.
Definition: ncbi_socket.h:1944
SOCK sock
Non-NULL when SOCK-related.
Definition: ncbi_socket.h:1952
char * SOCK_GetPeerAddressString(SOCK sock, char *buf, size_t bufsize)
Equivalent to SOCK_GetPeerAddressStringEx(.,.,.,eSAF_Full)
Definition: ncbi_socket.c:7644
EIO_Status LSOCK_CreateEx(unsigned short port, unsigned short backlog, LSOCK *lsock, TSOCK_Flags flags)
[SERVER-side] Create and initialize the server-side(listening) socket (socket() + bind() + listen())
Definition: ncbi_socket.c:6516
ESOCK_ErrType type
See ESOCK_ErrType.
Definition: ncbi_socket.h:1951
EIO_Status LSOCK_GetOSHandleEx(LSOCK lsock, void *handle_buf, size_t handle_size, EOwnership owndership)
Get an OS-dependent native socket handle to use by platform-specific API.
Definition: ncbi_socket.c:6570
struct TRIGGER_tag * TRIGGER
Definition: ncbi_socket.h:226
EIO_Status TRIGGER_IsSet(TRIGGER trigger)
Check whether the trigger has been set.
Definition: ncbi_socket.c:6452
unsigned short SOCK_htons(unsigned short)
Definition: ncbi_socket.c:8754
const char * SOCK_gethostbyaddrEx(unsigned int addr, char *name, size_t namelen, ESwitch log)
Take IPv4 host address (in network byte order) or 0 for current host, and fill out the provided buffe...
Definition: ncbi_socket.c:8796
int SOCK_IsUNIX(SOCK sock)
Check whether a socket is UNIX type.
Definition: ncbi_socket.c:8453
EIO_Status SOCK_SetupSSLEx(FSSLSetup setup)
Take SSL setup callback, and then init the entire API (SOCK + SSL).
Definition: ncbi_socket.c:9011
TNCBI_BigCount DSOCK_GetMessageCount(SOCK sock, EIO_Event direction)
Get message count.
Definition: ncbi_socket.c:8409
EIO_Status SOCK_ShutdownAPI(void)
Cleanup; destroy all internal/system data & resources used by the SOCK API.
Definition: ncbi_socket.c:1019
ESwitch SOCK_SetDataLogging(SOCK sock, ESwitch log)
Control the data logging for socket "sock" individually.
Definition: ncbi_socket.c:8583
void SOCK_SetReuseAddress(SOCK sock, int on_off)
Control reuse of socket addresses on per-socket basis Note: only a boolean parameter value is can be ...
Definition: ncbi_socket.c:8558
EIO_Status DSOCK_CreateEx(SOCK *sock, TSOCK_Flags flags)
Create a datagram socket.
Definition: ncbi_socket.c:7863
EIO_Status LSOCK_Create(unsigned short port, unsigned short backlog, LSOCK *lsock)
[SERVER-side] Create and initialize the server-side(listening) socket Same as LSOCK_CreateEx() called...
Definition: ncbi_socket.c:6507
unsigned short LSOCK_GetPort(LSOCK lsock, ENH_ByteOrder byte_order)
Get socket port number, which it listens on.
Definition: ncbi_socket.c:6616
POLLABLE POLLABLE_FromLSOCK(LSOCK)
Definition: ncbi_socket.c:8620
unsigned EBIO_Status
Definition: ncbi_socket.h:187
EIO_Status DSOCK_Bind(SOCK sock, unsigned short port)
Assosiate a datagram socket with a local port.
Definition: ncbi_socket.c:8001
EIO_Status SOCK_Create(const char *host, unsigned short port, const STimeout *timeout, SOCK *sock)
[CLIENT-side] Connect client to another(server-side, listening) socket (socket() + connect() [+ selec...
Definition: ncbi_socket.c:6664
POLLABLE POLLABLE_FromTRIGGER(TRIGGER)
Definition: ncbi_socket.c:8613
size_t SOCK_OSHandleSize(void)
Get size of OS-dependent native socket handle.
Definition: ncbi_socket.c:1069
ESOCK_Flags
Definition: ncbi_socket.h:488
EIO_Status LSOCK_Close(LSOCK lsock)
[SERVER-side] Close the listening socket, destroy relevant internal data.
Definition: ncbi_socket.c:6555
struct SPOLLABLE_tag * POLLABLE
Definition: ncbi_socket.h:1976
EIO_Status SOCK_CreateOnTop(const void *handle, size_t handle_size, SOCK *sock)
[SERVER-side] Create a socket on top of a "handle".
Definition: ncbi_socket.c:6730
ESwitch SOCK_SetInterruptOnSignalAPI(ESwitch on_off)
Control restartability of I/O interrupted by signals.
Definition: ncbi_socket.c:8532
EIO_Status SOCK_CreateOnTopEx(const void *handle, size_t handle_size, SOCK *sock, const void *data, size_t size, TSOCK_Flags flags)
[SERVER-side] Create a SOCKet on top of either an OS-dependent "handle" (file descriptor on Unix,...
Definition: ncbi_socket.c:6714
EIO_Status SOCK_Poll(size_t n, SSOCK_Poll polls[], const STimeout *timeout, size_t *n_ready)
Block until at least one of the sockets enlisted in "polls" array (of size "n") becomes available for...
Definition: ncbi_socket.c:7083
EIO_Status DSOCK_WipeMsg(SOCK sock, EIO_Event direction)
Clear message froma datagram socket.
Definition: ncbi_socket.c:8315
TNCBI_BigCount SOCK_GetCount(SOCK sock, EIO_Event direction)
Get counts of read or written bytes.
Definition: ncbi_socket.c:8492
unsigned EBIO_Event
Definition: ncbi_socket.h:186
const STimeout * SOCK_GetTimeout(SOCK sock, EIO_Event event)
Get the connection's i/o timeout (or NULL, if the timeout is infinite).
Definition: ncbi_socket.c:7230
EIO_Event event
Meaningful only for the eSOCK_ErrIO type.
Definition: ncbi_socket.h:1955
EIO_Event revent
[in] one of: eIO_Open/Read/Write/ReadWrite
Definition: ncbi_socket.h:969
ESOCK_Side side
eSOCK_Client (out) / eSOCK_Server (in)
Definition: ncbi_socket.h:347
const char * SOCK_gethostbyaddr(unsigned int addr, char *name, size_t namelen)
Same as SOCK_gethostbyaddrEx(,,<current API data logging>)
Definition: ncbi_socket.c:8813
EIO_Status status
Status code about to be returned (if known)
Definition: ncbi_socket.h:1956
int SOCK_isip(const char *host)
Equivalent of SOCK_isip(host, 0)
Definition: ncbi_socket.c:8712
const STimeout * SOCK_SetSelectInternalRestartTimeout(const STimeout *timeslice)
This is a helper call that can improve I/O behavior.
Definition: ncbi_socket.c:1075
ESOCK_Type
Socket type (internal)
Definition: ncbi_socket.h:200
EIO_Status(* FSOCK_ApproveHook)(const SSOCK_ApproveInfo *info, void *data)
The approve hook is global per API and gets called with an arbitrary user "data", if so desired as a ...
Definition: ncbi_socket.h:356
SOCKSSL(* FSSLSetup)(void)
SSL setup callback.
Definition: ncbi_socket.h:2312
SOCK POLLABLE_ToSOCK(POLLABLE)
Definition: ncbi_socket.c:8648
unsigned short SOCK_GetLocalPort(SOCK sock, ENH_ByteOrder byte_order)
Get local port of the socket.
Definition: ncbi_socket.c:7605
unsigned short SOCK_GetLocalPortEx(SOCK sock, int trueport, ENH_ByteOrder byte_order)
Get local port of the socket (true or cached / stored).
Definition: ncbi_socket.c:7581
unsigned int TSOCK_Flags
bitwise "OR" of ESOCK_Flags
Definition: ncbi_socket.h:503
EIO_Status SOCK_GetOSHandle(SOCK sock, void *handle_buf, size_t handle_size)
Same as SOCK_GetOSHandleEx(sock, handle_buf, handle_size, eNoOwnership).
Definition: ncbi_socket.c:7744
@ eSAF_IP
only numeric IP if INET socket, empty otherwise
Definition: ncbi_socket.h:1392
@ eSAF_Full
address in full, native form
Definition: ncbi_socket.h:1390
@ eSAF_Port
only numeric port if INET socket, empty otherwise
Definition: ncbi_socket.h:1391
@ eNH_NetworkByteOrder
Definition: ncbi_socket.h:194
@ eNH_HostByteOrder
Definition: ncbi_socket.h:193
@ eSOCK_Client
Definition: ncbi_socket.h:212
@ eSOCK_Server
Definition: ncbi_socket.h:211
@ eSOCK_IOWaitSysAPISelect
always use select()
Definition: ncbi_socket.h:387
@ eSOCK_IOWaitSysAPIAuto
default; use some heuristics to choose API
Definition: ncbi_socket.h:385
@ eSOCK_IOWaitSysAPIPoll
always use poll()
Definition: ncbi_socket.h:386
@ eSOCK_ErrDns
DNS-related error (unresolvable hostname)
Definition: ncbi_socket.h:1946
@ eSOCK_ErrIO
I/O-related error.
Definition: ncbi_socket.h:1947
@ eSOCK_ErrInit
Socket layer initialization error.
Definition: ncbi_socket.h:1945
@ fSOCK_CloseOnClose
close OS handle in SOCK_Close[Ex]()
Definition: ncbi_socket.h:499
@ fSOCK_LogDefault
Definition: ncbi_socket.h:491
@ fSOCK_KeepAlive
keep socket alive (if supported by OS)
Definition: ncbi_socket.h:492
@ fSOCK_BindLocal
bind to 127.0.0.1 only
Definition: ncbi_socket.h:494
@ fSOCK_Secure
subsumes CloseOnExec regardless of Keep
Definition: ncbi_socket.h:497
@ fSOCK_KeepOnClose
retain OS handle in SOCK_Close[Ex]()
Definition: ncbi_socket.h:498
@ fSOCK_BindAny
bind to 0.0.0.0 (i.e.
Definition: ncbi_socket.h:493
@ fSOCK_ReadOnWrite
Definition: ncbi_socket.h:500
@ fSOCK_KeepOnExec
can be applied to all sockets
Definition: ncbi_socket.h:495
@ fSOCK_CloseOnExec
can be applied to all sockets, default
Definition: ncbi_socket.h:496
@ fSOCK_LogOff
NB: logging is inherited in accepted SOCK.
Definition: ncbi_socket.h:489
@ fSOCK_InterruptOnSignal
Definition: ncbi_socket.h:501
@ fSOCK_LogOn
Definition: ncbi_socket.h:490
@ eSOCK_Trigger
Definition: ncbi_socket.h:202
@ eSOCK_Listening
Definition: ncbi_socket.h:201
@ eSOCK_Socket
Definition: ncbi_socket.h:203
@ eSOCK_Datagram
Definition: ncbi_socket.h:204
enum ENcbiSwitch ESwitch
Aux.
EIO_Status
I/O status.
Definition: ncbi_core.h:132
EIO_ReadMethod
I/O read method.
Definition: ncbi_core.h:88
enum ENcbiOwnership EOwnership
Ownership relations between objects.
EIO_WriteMethod
I/O write method.
Definition: ncbi_core.h:99
EIO_Event
I/O event (or direction).
Definition: ncbi_core.h:118
uint64_t TNCBI_BigCount
Big unsigned integer for file size and position.
Definition: ncbi_types.h:164
#define NCBI_XCONNECT_EXPORT
char * buf
yy_size_t n
static MDB_envinfo info
Definition: mdb_load.c:37
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
static Format format
Definition: njn_ioutil.cpp:53
static int bufsize
Definition: pcregrep.c:162
User-level connection approval hook.
Definition: ncbi_socket.h:343
I/O polling structure.
Definition: ncbi_socket.h:966
Timeout structure.
Definition: ncbi_types.h:76
static void setup(void)
Definition: tvp.c:114
Modified on Wed May 15 15:04:24 2024 by modify_doxy.py rev. 669887