~ubuntu-branches/ubuntu/karmic/ncbi-tools6/karmic

« back to all changes in this revision

Viewing changes to connect/ncbi_socket.h

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2006-10-22 19:32:02 UTC
  • mfrom: (1.2.1 upstream) (2.1.4 dapper)
  • Revision ID: james.westby@ubuntu.com-20061022193202-6svrvb6l52n0uhe4
Tags: 6.1.20061015-1
* New upstream release.
* Don't bother linking against indirectly used system libraries.
* Update man pages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#ifndef CONNECT___NCBI_SOCKET__H
2
2
#define CONNECT___NCBI_SOCKET__H
3
3
 
4
 
/*  $Id: ncbi_socket.h,v 6.50 2004/07/23 19:04:38 lavr Exp $
 
4
/*  $Id: ncbi_socket.h,v 6.61 2006/03/16 19:12:21 ucko Exp $
5
5
 * ===========================================================================
6
6
 *
7
7
 *                            PUBLIC DOMAIN NOTICE
64
64
 *  SOCK_GetReadTimeout
65
65
 *  SOCK_GetWriteTimeout
66
66
 *  SOCK_Read (including "peek" and "persistent read")
 
67
 *  SOCK_ReadLine
67
68
 *  SOCK_PushBack
68
69
 *  SOCK_Status
69
70
 *  SOCK_Write
77
78
 *  SOCK_SetInterruptOnSignal
78
79
 *  SOCK_SetReuseAddressAPI
79
80
 *  SOCK_SetReuseAddress
 
81
 *  SOCK_DisableOSSendDelay
80
82
 *
81
83
 * Datagram Socket:
82
84
 *
94
96
 *  SOCK_IsDatagram
95
97
 *  SOCK_IsClientSide
96
98
 *  SOCK_IsServerSide
 
99
 *  SOCK_IsUNIX
97
100
 *
98
101
 * Data logging:
99
102
 *
110
113
 *  SOCK_NetToHostLong
111
114
 *  SOCK_gethostbyname
112
115
 *  SOCK_gethostbyaddr
 
116
 *  SOCK_GetLoopbackAddress
 
117
 *  SOCK_StringToHostPort
 
118
 *  SOCK_HostPortToString
113
119
 *
114
120
 */
115
121
 
136
142
 */
137
143
 
138
144
 
139
 
/* Network and host byte order enumeration type
 
145
/** Network and host byte order enumeration type
140
146
 */
141
147
typedef enum {
142
148
    eNH_HostByteOrder,
144
150
} ENH_ByteOrder;
145
151
 
146
152
 
147
 
/* Forward declarations of the hidden socket internal structure, and
 
153
/** Forward declarations of the hidden socket internal structure, and
148
154
 * their upper-level handles to use by the LSOCK_*() and SOCK_*() API
149
155
 */
150
156
struct LSOCK_tag;                /* listening socket:  internal storage */
178
184
/******************************************************************************
179
185
 *   Error & Data Logging
180
186
 *
181
 
 * NOTE:  Use CORE_SetLOG() from "ncbi_core.h" to setup the log handler.
 
187
 * @li <b>NOTE:</b>  Use CORE_SetLOG() from "ncbi_core.h" to setup the log handler.
182
188
 */
183
189
 
184
190
 
185
 
/* By default ("log" == eDefault, which is eOff), the data is not logged.
186
 
 * To start logging the data, call this func with "log" == eOn.
187
 
 * To stop  logging the data, call this func with "log" == eOff.
188
 
 * Return prior setting.
 
191
/** By default ("log" == eDefault, which is eOff), the data is not logged.
 
192
 * @param log
 
193
 *  To start logging the data, call this func with "log" == eOn.
 
194
 *  To stop  logging the data, call this func with "log" == eOff.
 
195
 * @return
 
196
 *  Prior setting.
189
197
 */
190
198
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetDataLoggingAPI
191
199
(ESwitch log
192
200
 );
193
201
 
194
202
 
195
 
/* Control the data logging for socket "sock" individually.
196
 
 * To reset to the global default behavior (as set by SOCK_SetDataLoggingAPI),
197
 
 * call this function with "log" == eDefault.
198
 
 * Return prior setting.
 
203
/** Control the data logging for socket "sock" individually.
 
204
 * @param sock
 
205
 *  [in]  socket handle 
 
206
 * @param log
 
207
 *  To reset to the global default behavior (as set by SOCK_SetDataLoggingAPI),
 
208
 *  call this function with "log" == eDefault.
 
209
 * @return
 
210
 *  Return prior setting.
199
211
 */
200
212
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetDataLogging
201
213
(SOCK    sock,
208
220
 *   I/O restart on signals
209
221
 */
210
222
 
211
 
 
212
 
/* By default ("on_off" == eDefault,eOff), I/O is restartable if interrupted.
213
 
 * Return prior setting.
 
223
/** By default ("on_off" == eDefault,eOff), I/O is restartable if interrupted.
 
224
 * @param on_off
 
225
 * 
 
226
 * @return
 
227
 *  Prior setting.
214
228
 */
215
229
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetInterruptOnSignalAPI
216
230
(ESwitch on_off
217
231
 );
218
232
 
219
233
 
220
 
/* Control sockets individually. eDefault causes the use of global API flag.
221
 
 * Return prior setting.
 
234
/** Control sockets individually. eDefault causes the use of global API flag.
 
235
 * @param sock
 
236
 *  [in]  socket handle 
 
237
 * @param on_off
 
238
 *
 
239
 * @return
 
240
 *  Prior setting.
222
241
 */
223
242
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetInterruptOnSignal
224
243
(SOCK    sock,
234
253
 
235
254
/* By default ("on_off" == eDefault,eOff), address is not marked for reuse
236
255
 * in SOCK, but is always reused for LSOCK.
237
 
 * Return prior setting.
 
256
 * @param on_off
 
257
 *
 
258
 * @return
 
259
 *  Prior setting.
238
260
 */
239
261
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetReuseAddressAPI
240
262
(ESwitch on_off
241
263
 );
242
264
 
243
265
 
244
 
/* Control sockets individually (Note: only boolean value is available here.)
245
 
 * No return value is available.
 
266
/** Control sockets individually (Note: only boolean value is available here.)
 
267
 * @param sock
 
268
 *  [in]  socket handle 
 
269
 * @param on_off
 
270
 *   
 
271
 * @return
 
272
 *  No return value is available.
246
273
 */
247
274
extern NCBI_XCONNECT_EXPORT void SOCK_SetReuseAddress
248
 
(SOCK        sock,
249
 
 int/*bool*/ on_off);
 
275
(SOCK         sock,
 
276
 int/**bool*/ on_off);
250
277
 
251
278
 
252
279
 
255
282
 */
256
283
 
257
284
 
258
 
/*
 
285
/**
 
286
 * @param timeout
 
287
 *
259
288
 */
260
289
extern NCBI_XCONNECT_EXPORT const STimeout*SOCK_SetSelectInternalRestartTimeout
261
290
(const STimeout* timeout);
262
291
 
263
292
 
264
 
/* By default (on UNIX platforms) the SOCK API functions automagically call
 
293
/** By default (on UNIX platforms) the SOCK API functions automagically call
265
294
 * "signal(SIGPIPE, SIG_IGN)" on initialization.  To prohibit this feature,
266
295
 * you must call SOCK_AllowSigPipeAPI() before you call any other
267
296
 * function from the SOCK API.
269
298
extern NCBI_XCONNECT_EXPORT void SOCK_AllowSigPipeAPI(void);
270
299
 
271
300
 
272
 
/* Initialize all internal/system data & resources to be used by the SOCK API.
273
 
 * NOTE:
 
301
/** Initialize all internal/system data & resources to be used by the SOCK API.
 
302
 * @li <b>NOTE:</b>
274
303
 *  You can safely call it more than once; just, all calls after the first
275
304
 *  one will have no result. 
276
 
 * NOTE:
 
305
 * @li <b>NOTE:</b>
277
306
 *  Usually, SOCK API does not require an explicit initialization -- as it is
278
307
 *  guaranteed to initialize itself automagically, in one of API functions,
279
308
 *  when necessary. Yet, see the "Multi Thread safety" remark above.
280
 
 * NOTE:
 
309
 * @li <b>NOTE:</b>
281
310
 *  This call, when used for the very first time in the application, enqueues
282
311
 *  SOCK_ShutdownAPI() to be called upon application exit on plaftorms that
283
312
 *  provide this functionality. In any case, the application can opt for
286
315
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_InitializeAPI(void);
287
316
 
288
317
 
289
 
/* Cleanup; destroy all internal/system data & resources used by the SOCK API.
 
318
/** Cleanup; destroy all internal/system data & resources used by the SOCK API.
290
319
 * ATTENTION:  no function from the SOCK API should be called after this call!
291
 
 * NOTE: you can safely call it more than once; just, all calls after the first
 
320
 * @li <b>NOTE:</b> you can safely call it more than once; just, all calls after the first
292
321
 *       one will have no result. 
293
322
 */
294
323
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_ShutdownAPI(void);
308
337
} FLSCE_Flags;
309
338
typedef unsigned int TLSCE_Flags;
310
339
 
311
 
/* [SERVER-side]  Create and initialize the server-side(listening) socket
 
340
/** [SERVER-side]  Create and initialize the server-side(listening) socket
312
341
 * (socket() + bind() + listen())
313
 
 * NOTE: on some systems, "backlog" can be silently limited down to 128 (or 5).
 
342
 * @li <b>NOTE:</b> on some systems, "backlog" can be silently limited down to 128 (or 5).
 
343
 * @param port
 
344
 *  [in] the port to listen at
 
345
 * @param backlog
 
346
 *  [in] maximal # of pending connections
 
347
 * @param lsock
 
348
 *  [out] handle of the created listening socket
 
349
 * @param flags
 
350
 *  [in] special modifiers
314
351
 */
315
352
extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_CreateEx
316
 
(unsigned short port,    /* [in]  the port to listen at                  */
317
 
 unsigned short backlog, /* [in]  maximal # of pending connections       */
318
 
 LSOCK*         lsock,   /* [out] handle of the created listening socket */
319
 
 TLSCE_Flags    flags    /* [in]  special modifiers                      */
 
353
(unsigned short port,    
 
354
 unsigned short backlog, 
 
355
 LSOCK*         lsock,   
 
356
 TLSCE_Flags    flags    
320
357
 );
321
358
 
 
359
/**
 
360
 * @param port
 
361
 *  [in] the port to listen at
 
362
 * @param backlog
 
363
 *  [in] maximal # of pending connections
 
364
 * @param lsock
 
365
 *  [out] handle of the created listening socket
 
366
 */
322
367
extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_Create
323
 
(unsigned short port,    /* [in]  the port to listen at                  */
324
 
 unsigned short backlog, /* [in]  maximal # of pending connections       */
325
 
 LSOCK*         lsock    /* [out] handle of the created listening socket */
 
368
(unsigned short port,   
 
369
 unsigned short backlog, 
 
370
 LSOCK*         lsock    
326
371
 );
327
372
 
328
373
 
329
 
/* [SERVER-side]  Accept connection from a client.
330
 
 * NOTE: the "*timeout" is for this accept() only.  To set I/O timeout,
 
374
/** [SERVER-side]  Accept connection from a client.
 
375
 * @li <b>NOTE:</b> the "*timeout" is for this accept() only.  To set I/O timeout,
331
376
 *       use SOCK_SetTimeout();  all I/O timeouts are infinite by default.
 
377
 * @param lsock
 
378
 *  [in] handle of a listening socket
 
379
 * @param timeout
 
380
 *  [in] timeout (infinite if NULL)
 
381
 * @param sock
 
382
 *  [out] handle of the created socket
332
383
 */
333
384
extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_Accept
334
 
(LSOCK           lsock,    /* [in]  handle of a listening socket */
335
 
 const STimeout* timeout,  /* [in]  timeout (infinite if NULL)   */
336
 
 SOCK*           sock      /* [out] handle of the created socket */
 
385
(LSOCK           lsock,    
 
386
 const STimeout* timeout, 
 
387
 SOCK*           sock      
337
388
 );
338
389
 
339
390
 
340
 
/* [SERVER-side]  Close the listening socket, destroy relevant internal data.
 
391
/** [SERVER-side]  Close the listening socket, destroy relevant internal data.
 
392
 * @param lsock
 
393
 * 
341
394
 */
342
395
extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_Close(LSOCK lsock);
343
396
 
344
397
 
345
398
/* Get an OS-dependent native socket handle to use by platform-specific API.
346
399
 * FYI:  on MS-Windows it will be "SOCKET", on other platforms -- "int".
 
400
 * @param lsock
 
401
 *  [in]  socket handle 
 
402
 * @param handle_buf
 
403
 *  Pointer to a memory area to put the socket's native OS handle at
 
404
 * @param handle_size
 
405
 *  The exact(!) size of the expected OS handle
347
406
 */
348
407
extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_GetOSHandle
349
408
(LSOCK  lsock,
350
 
 void*  handle_buf,  /* pointer to a memory area to put the OS handle at */
351
 
 size_t handle_size  /* the exact(!) size of the expected OS handle */
 
409
 void*  handle_buf, 
 
410
 size_t  
352
411
 );
353
412
 
354
413
 
358
417
 */
359
418
 
360
419
 
361
 
/* [CLIENT-side]  Connect client to another(server-side, listening) socket
 
420
/** [CLIENT-side]  Connect client to another(server-side, listening) socket
362
421
 * (socket() + connect() [+ select()])
 
422
 * Equivalent to SOCK_CreateEx(host, port, timeout, sock, 0, 0, eDefault).
 
423
 *
 
424
 * @param host
 
425
 *  [in] host to connect to 
 
426
 * @param port
 
427
 *  [in] port to connect to 
 
428
 * @param timeout
 
429
 *  [in] the connect timeout (infinite if NULL)
 
430
 * @param sock
 
431
 *  [out] handle of the created socket
 
432
 * @sa SOCK_CreateEx()
363
433
 */
364
 
/* SOCK_CreateEx(host, port, timeout, sock, 0, 0, eDefault) */
365
434
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Create
366
 
(const char*     host,    /* [in]  host to connect to                        */
367
 
 unsigned short  port,    /* [in]  port to connect to                        */
368
 
 const STimeout* timeout, /* [in]  the connect timeout (infinite if NULL)    */
369
 
 SOCK*           sock     /* [out] handle of the created socket              */
 
435
(const char*     host,   
 
436
 unsigned short  port,   
 
437
 const STimeout* timeout, 
 
438
 SOCK*           sock     
370
439
 );
371
440
 
 
441
 
 
442
/** [CLIENT-side]  Connect client to another(server-side, listening) socket
 
443
 * (socket() + connect() [+ select()])
 
444
 *
 
445
 * @param host
 
446
 *  [in] Host to connect to 
 
447
 * @param port
 
448
 *  [in] port to connect to 
 
449
 * @param timeout
 
450
 *  [in] the connect timeout (infinite if NULL)
 
451
 * @param sock
 
452
 *  [out] handle of the created socket
 
453
 * @param init_data
 
454
 *  [in] initial output data segment (may be NULL)
 
455
 * @param init_size
 
456
 *  [in] size of initial data segment (may be 0) 
 
457
 * @param log
 
458
 *  [in] whether to do logging on this socket
 
459
 * @sa SOCK_Create()
 
460
 */
372
461
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_CreateEx
373
 
(const char*     host,     /* [in]  host to connect to                       */
374
 
 unsigned short  port,     /* [in]  port to connect to                       */
375
 
 const STimeout* timeout,  /* [in]  the connect timeout (infinite if NULL)   */
376
 
 SOCK*           sock,     /* [out] handle of the created socket             */
377
 
 const void*     init_data,/* [in]  initial output data segment (may be NULL)*/
378
 
 size_t          init_size,/* [in]  size of initial data segment (may be 0)  */
379
 
 ESwitch         log       /* [in]  whether to do logging on this socket     */
380
 
 );
381
 
 
382
 
 
383
 
/* [SERVER-side]  Create a socket on top of OS-dependent "handle"
384
 
 * (file descriptor on Unix). Returned socket is not reopenable to its
385
 
 * default peer (SOCK_Reconnect may not specify zeros for the connection
386
 
 * point).  All timeouts are set to its default [infinite] values.
 
462
(const char*     host,    
 
463
 unsigned short  port,    
 
464
 const STimeout* timeout,  
 
465
 SOCK*           sock,     
 
466
 const void*     init_data,
 
467
 size_t          init_size,
 
468
 ESwitch         log       
 
469
 );
 
470
 
 
471
/**
 
472
 * @sa SOCK_CreateOnTopEx()
 
473
 */
 
474
typedef enum {
 
475
    eSCOT_KeepOnClose,    /** Do not close "handle" on SOCK_Close() */
 
476
    eSCOT_CloseOnClose    /** Do close "handle" on SOCK_Close() */
 
477
} ESCOT_OnClose;
 
478
 
 
479
 
 
480
/** [SERVER-side]  Create a socket on top of OS-dependent "handle".
 
481
 *
 
482
 * Equivalent of SOCK_CreateOnTopEx(handle, handle_size, sock,
 
483
 *                                   0, 0, eDefault, eSCOT_CloseOnClose).
 
484
 * @param handle
 
485
 *  [in] OS-dependent "handle" to be converted
 
486
 * @param handle_size
 
487
 *  [in] "handle" size
 
488
 * @param sock
 
489
 *  [out] SOCK built on top of OS "handle"
 
490
 * @sa SOCK_CreateOnTopEx()
 
491
 */
 
492
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_CreateOnTop
 
493
(const void*   handle,      
 
494
 size_t        handle_size,                       
 
495
 SOCK*         sock         
 
496
 );
 
497
 
 
498
 
 
499
/** [SERVER-side]  Create a socket on top of OS-dependent "handle"
 
500
 * (file descriptor on Unix, SOCKET on MS-Windows). Returned socket
 
501
 * is not reopenable to its default peer (SOCK_Reconnect may not specify
 
502
 * zeros for the connection point).
 
503
 * All timeouts are set to default [infinite] values.
387
504
 * SOCK_Close() will close the "handle" only if the "close_on_close"
388
505
 * parameter is passed non-zero (eSCOT_CloseOnClose).
389
 
 * Return eIO_Success on success; otherwise: eIO_Closed if the "handle" does
390
 
 * not refer to an open socket [but e.g. to a normal file or a pipe];
391
 
 * other error codes in case of other errors.
 
506
 * @param handle
 
507
 *  [in] OS-dependent "handle" to be converted
 
508
 * @param handle_size
 
509
 *  [in] "handle" size
 
510
 * @param sock
 
511
 *  [out] SOCK built on top of OS "handle"
 
512
 * @param init_data
 
513
 *  [in] initial output data segment (ok NULL)
 
514
 * @param init_size
 
515
 *  [in] size of initial data segment (ok 0)  
 
516
 * @param log
 
517
 *  [in] data logging for the resulting SOCK 
 
518
 * @param on_close
 
519
 *  [in] if to keep "handle" in SOCK_Close()
 
520
 * @return 
 
521
 *  Return eIO_Success on success; otherwise: eIO_Closed if the "handle" does
 
522
 *  not refer to an open socket [but e.g. to a normal file or a pipe];
 
523
 *  other error codes in case of other errors.
 
524
 * @sa SOCK_CreateOnTop()
392
525
 */
393
 
typedef enum {
394
 
    eSCOT_KeepOnClose,    /* Do not close "handle" on SOCK_Close() */
395
 
    eSCOT_CloseOnClose    /* Do close "handle" on SOCK_Close() */
396
 
} ESCOT_OnClose;
397
 
 
398
 
/* SOCK_CreateOnTopEx(handle, handle_size, sock,
399
 
                      0, 0, eDefault, eSCOT_CloseOnClose) */
400
 
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_CreateOnTop
401
 
(const void*   handle,      /* [in]  OS-dependent "handle" to be converted */
402
 
 size_t        handle_size, /* [in]  "handle" size                         */
403
 
 SOCK*         sock         /* [out] SOCK built on top of OS "handle"      */
404
 
 );
405
 
 
406
526
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_CreateOnTopEx
407
 
(const void*   handle,      /* [in]  OS-dependent "handle" to be converted */
408
 
 size_t        handle_size, /* [in]  "handle" size                         */
409
 
 SOCK*         sock,        /* [out] SOCK built on top of OS "handle"      */
410
 
 const void*   init_data,   /* [in]  initial output data segment (ok NULL) */
411
 
 size_t        init_size,   /* [in]  size of initial data segment (ok 0)   */
412
 
 ESwitch       log,         /* [in]  data logging for the resulting SOCK   */
413
 
 ESCOT_OnClose on_close     /* [in]  if to keep "handle" in SOCK_Close()   */
 
527
(const void*   handle,      
 
528
 size_t        handle_size, 
 
529
 SOCK*         sock,       
 
530
 const void*   init_data,   
 
531
 size_t        init_size,   
 
532
 ESwitch       log,         
 
533
 ESCOT_OnClose on_close     
414
534
 );
415
535
 
416
536
 
417
 
/* [CLIENT-side]  Close the socket referred to by "sock" and then connect
 
537
/** [CLIENT-side]  Close the socket referred to by "sock" and then connect
418
538
 * it to another "host:port";  fail if it takes more than "timeout"
419
539
 * (close() + connect() [+ select()])
420
540
 *
421
541
 * HINT:  if "host" is NULL then connect to the same host address as before;
422
542
 *        if "port" is zero then connect to the same port # as before.
423
543
 *
424
 
 * NOTE1: "new" socket inherits the old I/O timeouts.
425
 
 * NOTE2: the call is applicable to stream [not datagram] sockets only.
426
 
 * NOTE3: "timeout"==NULL is infinite; "timeout"=={0,0} causes no wait for
 
544
 * @li <b>NOTE 1:</b> "new" socket inherits the old I/O timeouts.
 
545
 * @li <b>NOTE 2:</b> the call is applicable to stream [not datagram] sockets only.
 
546
 * @li <b>NOTE 3:</b> "timeout"==NULL is infinite; "timeout"=={0,0} causes no wait for
427
547
 *        connection to be established and to return immediately.
 
548
 * @li <b>NOTE 4:</b> UNIX sockets can only be reconnected to the same file thus both
 
549
 *        host and port have to be passed as 0s.
 
550
 * @param sock
 
551
 *  [in] handle of the socket to reconnect
 
552
 * @param host
 
553
 *  [in] host to connect to  (can be NULL)
 
554
 * @param port
 
555
 *  [in] port to connect to  (can be 0)
 
556
 * @param timeout
 
557
 *  [in] the connect timeout (infinite if NULL)
428
558
 */
429
559
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Reconnect
430
 
(SOCK            sock,    /* [in] handle of the socket to reconnect      */
431
 
 const char*     host,    /* [in] host to connect to  (can be NULL)      */
432
 
 unsigned short  port,    /* [in] port to connect to  (can be 0)         */
433
 
 const STimeout* timeout  /* [in] the connect timeout (infinite if NULL) */
 
560
(SOCK            sock,   
 
561
 const char*     host,    
 
562
 unsigned short  port,    
 
563
 const STimeout* timeout  
434
564
 );
435
565
 
436
566
 
437
 
/* Shutdown the connection in only one direction (specified by "direction").
 
567
/** Shutdown the connection in only one direction (specified by "direction").
438
568
 * Later attempts to I/O (or to wait) in the shutdown direction will
439
569
 * do nothing, and immediately return with "eIO_Closed" status.
440
570
 * Pending data output can cause data transfer to the remote end (subject
441
571
 * for eIO_Close timeout as previously set by SOCK_SetTimeout()).
442
572
 * Cannot be applied to datagram sockets (eIO_InvalidArg results).
 
573
 * @param sock
 
574
 *  [in] handle of the socket to shutdown  
 
575
 * @param how
 
576
 *  [in] one of:  eIO_Read, eIO_Write, eIO_ReadWrite
443
577
 */
444
578
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Shutdown
445
 
(SOCK      sock, /* [in] handle of the socket to shutdown            */
446
 
 EIO_Event how   /* [in] one of:  eIO_Read, eIO_Write, eIO_ReadWrite */
 
579
(SOCK      sock, 
 
580
 EIO_Event how   
447
581
 );
448
582
 
449
583
 
450
 
/* Close the connection, destroy relevant internal data.
 
584
/** Close the connection, destroy relevant internal data.
451
585
 * The "sock" handle goes invalid after this function call, regardless
452
586
 * of whether the call was successful or not.
453
 
 * NOTE1:  if eIO_Close timeout was specified (or NULL) then it blocks until
 
587
 * @li <b>NOTE 1:</b>  if eIO_Close timeout was specified (or NULL) then it blocks until
454
588
 *         either all unsent data are sent, error flagged, or the timeout
455
589
 *         expires.
456
 
 * NOTE2:  if there is output pending, that output will be flushed.
 
590
 * @li <b>NOTE 2:</b>  if there is output pending, that output will be flushed.
 
591
 * @param sock
 
592
 *  [in] Socket
457
593
 */
458
594
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Close(SOCK sock);
459
595
 
460
596
 
461
 
/* Close the connection, and conditionally destroy relevant internal data.
462
 
 * NOTE1:  if eIO_Close timeout was specified (or NULL) then it blocks until
 
597
/** Close the connection, and conditionally destroy relevant internal data.
 
598
 * @li <b>NOTE 1:</b>  if eIO_Close timeout was specified (or NULL) then it blocks until
463
599
 *         either all unsent data are sent, error flagged, or the timeout
464
600
 *         expires.
465
 
 * NOTE2:  if there is output pending, that output will be flushed.
466
 
 * NOTE3:  SOCK_CloseEx(sock, 1) is equivalent to SOCK_Close(sock);
 
601
 * @li <b>NOTE 2:</b>  if there is output pending, that output will be flushed.
 
602
 * @li <b>NOTE 3:</b>  SOCK_CloseEx(sock, 1) is equivalent to SOCK_Close(sock);
 
603
 * @param sock
 
604
 *  [in] handle of the socket to close
 
605
 * @param destroy 
 
606
 *  [in] =1 to destroy handle; =0 to keep handle
467
607
 */
468
608
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_CloseEx
469
 
(SOCK        sock,    /* [in] handle of the socket to close           */
470
 
 int/*bool*/ destroy  /* [in] =1 to destroy handle; =0 to keep handle */
 
609
(SOCK         sock,    
 
610
 int/**bool*/ destroy  
471
611
 );
472
612
 
473
613
 
474
 
/* Block on the socket until either read/write (dep. on the "event" arg) is
 
614
/** Block on the socket until either read/write (dep. on the "event" arg) is
475
615
 * available or timeout expires (if "timeout" is NULL then assume it infinite).
476
616
 * For a datagram socket, eIO_Closed is returned if the internally latched
477
617
 * message was entirely read out, and eIO_Read was requested as the "event".
478
618
 * Both eIO_Write and eIO_ReadWrite events always immediately succeed for
479
619
 * the datagram socket.
 
620
 * @param sock
 
621
 *  [in]  socket handle 
 
622
 * @param event
 
623
 *  [in] one of:  eIO_Read, eIO_Write, eIO_ReadWrite 
 
624
 * @param timeout
 
625
 *  [in] Maximum time to wait for an event
480
626
 */
481
627
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Wait
482
628
(SOCK            sock,
483
 
 EIO_Event       event,  /* [in] one of:  eIO_Read, eIO_Write, eIO_ReadWrite */
 
629
 EIO_Event       event, 
484
630
 const STimeout* timeout
485
631
 );
486
632
 
487
633
 
488
 
/* Block until at least one of the sockets enlisted in "polls" array
 
634
/**
 
635
 * @sa SOCK_Poll()
 
636
 */
 
637
typedef struct {
 
638
    SOCK      sock;   /** [in]          SOCK to poll (NULL if not to poll)  */
 
639
    EIO_Event event;  /** [in]  one of: eIO_Read, eIO_Write, eIO_ReadWrite  */
 
640
    EIO_Event revent; /** [out] one of: eIO_Open/Read/Write/ReadWrite/Close */
 
641
} SSOCK_Poll;
 
642
 
 
643
 
 
644
/** Block until at least one of the sockets enlisted in "polls" array
489
645
 * (of size "n") becomes available for requested operation (event),
490
646
 * or until timeout expires (wait indefinitely if timeout is passed NULL).
491
647
 * Return eIO_Success if at least one socket was found ready; eIO_Timeout
492
648
 * if timeout expired; eIO_Unknown if underlying system call(s) failed.
493
 
 * NOTE1: For a socket found not ready for an operation, eIO_Open is returned
 
649
 * @li <b>NOTE 1:</b> For a socket found not ready for an operation, eIO_Open is returned
494
650
 *        in its "revent"; for a failing socket, eIO_Close is returned;
495
 
 * NOTE2: This call may return eIO_InvalidArg if
 
651
 * @li <b>NOTE 2:</b> This call may return eIO_InvalidArg if
496
652
 *        - parameters to the call are inconsistent;
497
653
 *        - a non-NULL socket polled with a bad "event" (eIO_Open, eIO_Close).
498
654
 *        With this return code, the calling program cannot rely on "revent"
499
655
 *        fields the "polls" array as they might not be properly updated.
500
 
 * NOTE3: If either both "n" and "polls" are NULL, or all sockets in "polls"
 
656
 * @li <b>NOTE 3:</b> If either both "n" and "polls" are NULL, or all sockets in "polls"
501
657
 *        are NULL, then the returned result is either
502
658
 *        eIO_Timeout (after the specified amount of time was spent idle), or
503
659
 *        eIO_Interrupted (if signal came while the waiting was in progress).
504
 
 * NOTE4: For datagram sockets, the readiness for reading is determined by
 
660
 * @li <b>NOTE 4:</b> For datagram sockets, the readiness for reading is determined by
505
661
 *        message data latched since last message receive call (DSOCK_RecvMsg).
506
 
 * NOTE5: This call allows intermixture of stream and datagram sockets.
507
 
 * NOTE6: This call can cause some socket I/O in those sockets marked for
 
662
 * @li <b>NOTE 5:</b> This call allows intermixture of stream and datagram sockets.
 
663
 * @li <b>NOTE 6:</b> This call can cause some socket I/O in those sockets marked for
508
664
 *        read-on-write and those with pending connection or output data.
 
665
 * @param n
 
666
 *  [in] # of SSOCK_Poll elems in "polls"  
 
667
 * @param polls[]
 
668
 *  [in|out] array of query/result structures   
 
669
 * @param timeout
 
670
 *  [in] max time to wait (infinite if NULL)
 
671
 * @param n_ready
 
672
 *  [out] # of ready sockets  (may be NULL) 
509
673
 */
510
 
 
511
 
typedef struct {
512
 
    SOCK      sock;   /* [in]           SOCK to poll (NULL if not to poll)   */
513
 
    EIO_Event event;  /* [in]  one of:  eIO_Read, eIO_Write, eIO_ReadWrite   */
514
 
    EIO_Event revent; /* [out] one of:  eIO_Open/Read/Write/ReadWrite/Close  */
515
 
} SSOCK_Poll;
516
 
 
517
674
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Poll
518
 
(size_t          n,         /* [in]      # of SSOCK_Poll elems in "polls"    */
519
 
 SSOCK_Poll      polls[],   /* [in|out]  array of query/result structures    */
520
 
 const STimeout* timeout,   /* [in]      max time to wait (infinite if NULL) */
521
 
 size_t*         n_ready    /* [out]     # of ready sockets  (may be NULL)   */
 
675
(size_t          n,         
 
676
 SSOCK_Poll      polls[],   
 
677
 const STimeout* timeout,   
 
678
 size_t*         n_ready    
522
679
 );
523
680
 
524
681
 
525
682
 
526
 
/* GENERIC POLLABLE INTERFACE, please see above for explanations
 
683
/** GENERIC POLLABLE INTERFACE, please see above for explanations
527
684
 */
528
685
struct SPOLLABLE_tag;
529
686
typedef struct SPOLLABLE_tag* POLLABLE;
534
691
    EIO_Event revent;
535
692
} SPOLLABLE_Poll;
536
693
 
 
694
/**
 
695
 * @param n
 
696
 * 
 
697
 * @param polls[]
 
698
 *   
 
699
 * @param timeout
 
700
 * 
 
701
 * @param n_ready
 
702
 * 
 
703
 */
537
704
extern NCBI_XCONNECT_EXPORT EIO_Status POLLABLE_Poll
538
705
(size_t          n,
539
706
 SPOLLABLE_Poll  polls[],
542
709
 );
543
710
 
544
711
 
545
 
/* Return 0 if conversion cannot be made; otherwise converted handle */
 
712
/**
 
713
 * @return
 
714
 *  Return 0 if conversion cannot be made; otherwise converted handle 
 
715
 */
546
716
extern NCBI_XCONNECT_EXPORT POLLABLE POLLABLE_FromSOCK (SOCK);
547
717
extern NCBI_XCONNECT_EXPORT POLLABLE POLLABLE_FromLSOCK(LSOCK);
548
718
extern NCBI_XCONNECT_EXPORT SOCK     POLLABLE_ToSOCK   (POLLABLE);
549
719
extern NCBI_XCONNECT_EXPORT LSOCK    POLLABLE_ToLSOCK  (POLLABLE);
550
720
 
551
721
 
552
 
/* Specify timeout for the connection i/o (see SOCK_[Read|Write|Close] funcs).
 
722
/** Specify timeout for the connection i/o (see SOCK_[Read|Write|Close] funcs).
553
723
 * If "timeout" is NULL then set the timeout to be infinite;
554
 
 * NOTE: the default timeout is infinite (wait "ad infinitum" on I/O).
 
724
 * @li <b>NOTE:</b> the default timeout is infinite (wait "ad infinitum" on I/O).
 
725
 * @param sock
 
726
 *  [in]  socket handle 
 
727
 * @param event
 
728
 *  [in] one of:  eIO_[Read/Write/ReadWrite/Close]
 
729
 * @param timeout
 
730
 *  [in] new timeout value to set  
555
731
 */
556
732
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_SetTimeout
557
733
(SOCK            sock,
558
 
 EIO_Event       event,   /* [in]  one of:  eIO_[Read/Write/ReadWrite/Close] */
559
 
 const STimeout* timeout  /* [in]  new timeout value to set                  */
 
734
 EIO_Event       event,   
 
735
 const STimeout* timeout  
560
736
 );
561
737
 
562
738
 
563
 
/* Get the connection's i/o timeout (or NULL, if the timeout is infinite).
564
 
 * NOTE1:  the returned timeout is guaranteed to be pointing to a valid
 
739
/** Get the connection's i/o timeout (or NULL, if the timeout is infinite).
 
740
 * @li <b>NOTE 1:</b>  the returned timeout is guaranteed to be pointing to a valid
565
741
 *         (and correct) structure in memory at least until the SOCK is closed
566
742
 *         or SOCK_SetTimeout is called for this "sock".
567
 
 * NOTE2:  eIO_ReadWrite timeout is the least of eIO_Read and eIO_Write ones.
 
743
 * @li <b>NOTE 2:</b>  eIO_ReadWrite timeout is the least of eIO_Read and eIO_Write ones.
 
744
 * @param sock
 
745
 *  [in]  socket handle 
 
746
 * @param event
 
747
 *  [in]  one of:  eIO_[Read/Write/Close] 
568
748
 */
569
749
extern NCBI_XCONNECT_EXPORT const STimeout* SOCK_GetTimeout
570
750
(SOCK      sock,
571
 
 EIO_Event event  /* [in]  one of:  eIO_[Read/Write/Close] */
 
751
 EIO_Event event  
572
752
 );
573
753
 
574
754
 
575
 
/* Read/peek up to "size" bytes from "sock" to the mem.buffer pointed by "buf".
 
755
/** Read/peek up to "size" bytes from "sock" to the mem.buffer pointed by "buf".
576
756
 * In "*n_read", return the number of successfully read bytes.
577
757
 * Read method "how" can be either of the following:
578
758
 * eIO_ReadPlain   -- read as many as "size" bytes and return (eIO_Success);
605
785
 *   eIO_Read[Persist] -- discard up to "size" bytes from internal buffer
606
786
 *                        and socket (check "*n_read" to know how many).
607
787
 *
608
 
 * NOTE1: "Read" and "peek" methods differ:  if "read" is performed and not
 
788
 * @li <b>NOTE 1:</b> "Read" and "peek" methods differ:  if "read" is performed and not
609
789
 *        enough but some data available immediately from the internal buffer,
610
790
 *        then the call completes with eIO_Success status.  For "peek", if
611
791
 *        not all requested data were available, the real I/O occurs to pick up
612
792
 *        additional data (if any) from the system. Keep this difference in
613
793
 *        mind when programming loops that heavily use "peek"s without "read"s.
614
 
 * NOTE2: If on input "size" == 0, then "*n_read" is set to 0, and the
 
794
 * @li <b>NOTE 2:</b> If on input "size" == 0, then "*n_read" is set to 0, and the
615
795
 *        return value can be either of eIO_Success, eIO_Closed or
616
796
 *        eIO_Unknown depending on connection status of the socket.
 
797
 * @param sock
 
798
 *  [in]  socket handle 
 
799
 * @param buf
 
800
 *  [out] data buffer to read to 
 
801
 * @param size
 
802
 *  [in] max # of bytes to read to "buf" 
 
803
 * @param n_read
 
804
 *  [out] # of bytes read  (can be NULL)
 
805
 * @param how
 
806
 *  [in] how to read the data 
617
807
 */
618
808
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Read
619
809
(SOCK           sock,
620
 
 void*          buf,    /* [out] data buffer to read to          */
621
 
 size_t         size,   /* [in]  max # of bytes to read to "buf" */
622
 
 size_t*        n_read, /* [out] # of bytes read  (can be NULL)  */
623
 
 EIO_ReadMethod how     /* [in]  how to read the data            */
624
 
 );
625
 
 
626
 
 
627
 
/* Push the specified data back to the socket input queue (in the socket's
 
810
 void*          buf,   
 
811
 size_t         size,   
 
812
 size_t*        n_read, 
 
813
 EIO_ReadMethod how    
 
814
 );
 
815
 
 
816
 
 
817
/**
 
818
 * Read a line from SOCK.  A line is terminated by either '\n' (with
 
819
 * optional preceding '\r') or '\0'.  Returned result is always '\0'-
 
820
 * terminated and having '\r'(if any)'\n' stripped. *n_read (if 'n_read'
 
821
 * passed non-NULL) contains the numbed of characters written into
 
822
 * 'buf' (not counting the terminating '\0').  If 'buf', whose size is
 
823
 * specified via 'size' parameter, is not big enough to contain the
 
824
 * line, all 'size' bytes will be filled, with *n_read == size upon
 
825
 * return.  Note that there will be no terminating '\0' in this
 
826
 * (and the only) case, which the caller can easily distinguish.
 
827
 * @param sock
 
828
 *  [in]  socket handle 
 
829
 * @param buf
 
830
 *  [out] data buffer to read to 
 
831
 * @param size
 
832
 *  [in] max # of bytes to read to "buf"
 
833
 * @param n_read
 
834
 *  [out] # of bytes read  (can be NULL) 
 
835
 * @return
 
836
 *  Return code eIO_Success upon successful completion, other - upon
 
837
 *  an error.  Note that *n_read must be analyzed prior to return code,
 
838
 *  because the buffer could have received some contents before
 
839
 *  the indicated error occurred (especially when connection closed).
 
840
 */
 
841
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_ReadLine
 
842
(SOCK           sock,
 
843
 char*          buf,    
 
844
 size_t         size,   
 
845
 size_t*        n_read  
 
846
 );
 
847
 
 
848
 
 
849
/** Push the specified data back to the socket input queue (in the socket's
628
850
 * internal read buffer). These can be any data, not necessarily the data
629
851
 * previously read from the socket.
 
852
 * @param sock
 
853
 *  [in]  socket handle 
 
854
 * @param buf
 
855
 *  [in] data to push back to the socket's local buffer 
 
856
 * @param size
 
857
 *  [in] # of bytes (starting at "buf") to push back 
630
858
 */
631
859
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_PushBack
632
860
(SOCK        sock,
633
 
 const void* buf,  /* [in] data to push back to the socket's local buffer */
634
 
 size_t      size  /* [in] # of bytes (starting at "buf") to push back    */
 
861
 const void* buf,  
 
862
 size_t      size  
635
863
 );
636
864
 
637
865
 
638
 
/* Return (for the specified "direction" [eIO_Open to check for closed sock]):
 
866
/**
 
867
 * @param sock
 
868
 *  [in]  socket handle 
 
869
 * @param direction
 
870
 *  [in] one of:  eIO_Open, eIO_Read, eIO_Write
 
871
 * @return
 
872
 *  Return (for the specified "direction" [eIO_Open to check for closed sock]):
639
873
 *   eIO_Closed     -- if the connection was shutdown by SOCK_Shutdown(), or
640
874
 *                     (for "eIO_Read" only) if EOF was detected
641
875
 *                     if "direction"==eIO_Open, this code means socket closed
644
878
 *   eIO_Timeout    -- if the socket is not yet actually connected
645
879
 *   eIO_Success    -- otherwise (incl. eIO_Timeout on last I/O)
646
880
 *
647
 
 * NOTE:  The SOCK_Read() and SOCK_Wait(eIO_Read) will not return any error
 
881
 * @li <b>NOTE:</b>  The SOCK_Read() and SOCK_Wait(eIO_Read) will not return any error
648
882
 *        as long as there is any unread (buffered) data left.
649
883
 *        Thus, when you are "peeking" data instead of actually reading it,
650
884
 *        then this is the only "non-destructive" way to check whether EOF
652
886
 */
653
887
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Status
654
888
(SOCK      sock,
655
 
 EIO_Event direction  /* [in] one of:  eIO_Open, eIO_Read, eIO_Write */
 
889
 EIO_Event direction  
656
890
 );
657
891
 
658
892
 
659
 
/* Write "size" bytes from buffer "buf" to "sock".
660
 
 * In "*n_written", return the number of bytes actually written.
661
 
 * eIO_WritePlain   --  write as many bytes as possible at once and return
 
893
/** Write "size" bytes from buffer "buf" to "sock".
 
894
 * @param sock
 
895
 *  [in]  socket handle 
 
896
 * @param buf
 
897
 *  [in] data to write to the socket 
 
898
 * @param size
 
899
 *  [in] # of bytes (starting at "buf") to write 
 
900
 * @param n_written
 
901
 *  [out] # of written bytes (can be NULL)
 
902
 * @param how
 
903
 *  [in] eIO_WritePlain | eIO_WritePersist
 
904
 * @return
 
905
 *  In "*n_written", return the number of bytes actually written.
 
906
 *  eIO_WritePlain   --  write as many bytes as possible at once and return
662
907
 *                      immediately; if no bytes can be written then wait
663
908
 *                      at most WRITE timeout, try again and return.
664
 
 * eIO_WritePersist --  write all data (doing an internal retry loop
 
909
 *  eIO_WritePersist --  write all data (doing an internal retry loop
665
910
 *                      if necessary); if any single write attempt times out
666
911
 *                      or fails then stop writing and return (error code).
667
 
 * Return status: eIO_Success -- some bytes were written successfully  [Plain]
 
912
 *  Return status: eIO_Success -- some bytes were written successfully  [Plain]
668
913
 *                            -- all bytes were written successfully [Persist]
669
914
 *                other code denotes an error, but some bytes might have
670
915
 *                been sent nevertheless (always check *n_written to know).
671
916
 *
672
 
 * NOTE1: With eIO_WritePlain the call returns eIO_Success iff some data
 
917
 * @li <b>NOTE 1:</b> With eIO_WritePlain the call returns eIO_Success iff some data
673
918
 *        were actually written to the socket. If no data could be written
674
919
 *        (and perhaps timeout expired) this call always returns an error.
675
 
 * NOTE2: eIO_WritePlain and eIO_WritePersist differs that the latter can
 
920
 * @li <b>NOTE 2:</b> eIO_WritePlain and eIO_WritePersist differs that the latter can
676
921
 *        flag an error condition even if some data were actually written
677
922
 *        (see "the rule of thumb" in the comments for SOCK_Read() above).
678
 
 * NOTE3: if "size"==0, return value can be eIO_Success if no pending data
 
923
 * @li <b>NOTE 3:</b> if "size"==0, return value can be eIO_Success if no pending data
679
924
 *        left in the socket, or eIO_Timeout if there are still data pending.
680
925
 *        In either case, "*n_written" is set to 0 on return.
681
926
 */
682
927
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Write
683
928
(SOCK            sock,
684
 
 const void*     buf,       /* [in]  data to write to the socket             */
685
 
 size_t          size,      /* [in]  # of bytes (starting at "buf") to write */
686
 
 size_t*         n_written, /* [out] # of written bytes (can be NULL)        */
687
 
 EIO_WriteMethod how        /* [in]  eIO_WritePlain | eIO_WritePersist       */
 
929
 const void*     buf,       
 
930
 size_t          size,      
 
931
 size_t*         n_written, 
 
932
 EIO_WriteMethod how        
688
933
 );
689
934
 
690
935
 
691
 
/* If there is outstanding connection or output data pending, cancel it.
 
936
/** If there is outstanding connection or output data pending, cancel it.
692
937
 * Mark the socket as if it has been shut down for both reading and writing.
693
938
 * Break actual connection if any was established.
694
939
 * Do not attempt to send anything upon SOCK_Close().
695
940
 * This call is available for stream sockets only.
 
941
 * @param sock
 
942
 * Socket handle
696
943
 */
697
944
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Abort
698
945
(SOCK sock
699
946
 );
700
947
 
701
948
 
702
 
/* Get host and port of the socket's peer.
703
 
 * If "network_byte_order" is true(non-zero) then return the host/port in the
704
 
 * network byte order; otherwise return them in the local host byte order.
 
949
/** Get host and port of the socket's peer.
 
950
 * @param sock
 
951
 *  [in]  socket handle 
 
952
 * @param host
 
953
 *  [out] the peer's host (can be NULL) 
 
954
 * @param port
 
955
 *  [out] the peer's port (can be NULL)
 
956
 * @param byte_order
 
957
 *  [in] host/port byte order
 
958
 * @return
 
959
 *  If "network_byte_order" is true(non-zero) then return the host/port in the
 
960
 *  network byte order; otherwise return them in the local host byte order.
705
961
 */
706
962
extern NCBI_XCONNECT_EXPORT void SOCK_GetPeerAddress
707
963
(SOCK            sock,
708
 
 unsigned int*   host,                /* [out] the peer's host (can be NULL) */
709
 
 unsigned short* port,                /* [out] the peer's port (can be NULL) */
710
 
 ENH_ByteOrder   byte_order           /* [in]  host/port byte order          */
 
964
 unsigned int*   host,               
 
965
 unsigned short* port,                
 
966
 ENH_ByteOrder   byte_order          
711
967
 );
712
968
 
713
969
 
714
 
/* Get textual representation of the socket's peer.
 
970
/** Get textual representation of the socket's peer.
715
971
 * For INET domain sockets, the result is of the form "aaa.bbb.ccc.ddd:ppppp";
716
972
 * for UNIX domain socket, the result is the name of the socket's file.
717
 
 * On success, return its "buf" argument; return 0 on error.
718
 
 */
 
973
 * @param sock
 
974
 *  [in]  socket handle 
 
975
 * @param buf
 
976
 *  [out] pointer to provided buffer to store the text to
 
977
 * @param buflen
 
978
 *  [in] usable size of the buffer above 
 
979
 * @return
 
980
 *  On success, return its "buf" argument; return 0 on error.
 
981
*/
719
982
extern NCBI_XCONNECT_EXPORT char* SOCK_GetPeerAddressString
720
983
(SOCK   sock,
721
 
 char*  buf,        /* [out] pointer to provided buffer to store the text to */
722
 
 size_t buflen      /* [in]  usable size of the buffer above                 */
 
984
 char*  buf,       
 
985
 size_t buflen     
723
986
 );
724
987
 
725
988
 
726
 
/* Get an OS-dependent native socket handle to use by platform-specific API.
 
989
/** Get an OS-dependent native socket handle to use by platform-specific API.
727
990
 * FYI:  on MS-Windows it will be "SOCKET", on other platforms -- "int".
 
991
 * @param sock
 
992
 *   Socket handle
 
993
 * @param handle_buf
 
994
 *  pointer to a memory area to put the OS handle at
 
995
 * @param handle_size
 
996
 *  the exact(!) size of the expected OS handle
728
997
 */
729
998
extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_GetOSHandle
730
999
(SOCK   sock,
731
 
 void*  handle_buf,  /* pointer to a memory area to put the OS handle at */
732
 
 size_t handle_size  /* the exact(!) size of the expected OS handle      */
 
1000
 void*  handle_buf,  
 
1001
 size_t handle_size  
733
1002
 );
734
1003
 
735
1004
 
736
 
/* By default ("on_off" == eDefault,eOff), sockets will not try to read data
 
1005
/** By default ("on_off" == eDefault,eOff), sockets will not try to read data
737
1006
 * from inside SOCK_Write(). If you want to automagically upread the data
738
1007
 * (and cache it in the internal socket buffer) when the write operation
739
1008
 * is not immediately available, call this func with "on_off" == eOn.
740
 
 * Return prior setting.
741
 
 */
 
1009
 * @param on_off
 
1010
 * 
 
1011
 * @return
 
1012
 *  Prior setting.
 
1013
*/
742
1014
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetReadOnWriteAPI
743
1015
(ESwitch on_off
744
1016
 );
745
1017
 
746
1018
 
747
 
/* Control the reading-while-writing feature for socket "sock" individually.
 
1019
/** Control the reading-while-writing feature for socket "sock" individually.
748
1020
 * To reset to the global default behavior (as set by
749
1021
 * SOCK_SetReadOnWriteAPI), call this function with "on_off" == eDefault.
750
 
 * Return prior setting.
 
1022
 * @param sock
 
1023
 *  [in]  socket handle 
 
1024
 * @param on_off
 
1025
 *  
 
1026
 * @return
 
1027
 *  Prior setting.
751
1028
 */
752
1029
extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetReadOnWrite
753
1030
(SOCK    sock,
755
1032
 );
756
1033
 
757
1034
 
 
1035
/** Control OS-defined send strategy by disabling/enabling TCP
 
1036
 * Nagle algorithm that packs multiple requests into a single
 
1037
 * frame and thus transferring data in fewer transactions,
 
1038
 * miminizing the network traffic and bursting the throughput.
 
1039
 * Some applications, however, may find it useful to disable this
 
1040
 * default behavior for the sake of their performance increase
 
1041
 * (like in case of short transactions otherwise held by the system
 
1042
 * to be possibly coalesced into larger chunks).
 
1043
 * @param sock
 
1044
 *  [in]  socket handle 
 
1045
 * @param on_off
 
1046
 *  
 
1047
 * NB: use true to disable; false to enable
 
1048
 */
 
1049
extern NCBI_XCONNECT_EXPORT void SOCK_DisableOSSendDelay
 
1050
(SOCK         sock,
 
1051
 int/**bool*/ on_off  
 
1052
 );
 
1053
 
 
1054
 
758
1055
 
759
1056
/******************************************************************************
760
1057
 *  Connectionless (datagram) sockets
798
1095
 *  buffers correspondingly.
799
1096
 */
800
1097
 
801
 
 
 
1098
/**
 
1099
 * @param sock
 
1100
 *  [out] socket created
 
1101
 */
802
1102
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_Create
803
 
(SOCK*           sock                   /* [out] socket created              */
 
1103
(SOCK*           sock                                
804
1104
 );
805
1105
 
806
 
 
 
1106
/**
 
1107
 * @param sock
 
1108
 *  [out] socket created
 
1109
 * @param log
 
1110
 *  [in] whether to log data activity
 
1111
 */
807
1112
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_CreateEx
808
 
(SOCK*           sock,                  /* [out] socket created              */
809
 
 ESwitch         log                    /* [in]  whether to log data activity*/
 
1113
(SOCK*           sock,               
 
1114
 ESwitch         log                 
810
1115
 );
811
1116
 
812
 
 
 
1117
/**
 
1118
 * @param sock
 
1119
 *  [in] SOCK from DSOCK_Create[Ex]()
 
1120
 * @param port
 
1121
 *  [in] port to bind to (!=0)
 
1122
 */
813
1123
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_Bind
814
 
(SOCK            sock,                  /* [in]  SOCK from DSOCK_Create[Ex]()*/
815
 
 unsigned short  port                   /* [in]  port to bind to (!=0)       */
 
1124
(SOCK            sock,                  
 
1125
 unsigned short  port                  
816
1126
 );
817
1127
 
818
 
 
 
1128
/**
 
1129
 * @param sock
 
1130
 *  [in] SOCK from DSOCK_Create[Ex]()
 
1131
 * @param host
 
1132
 *  [in] peer host
 
1133
 * @param port
 
1134
 *  [in] peer port
 
1135
 */
819
1136
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_Connect
820
 
(SOCK            sock,                  /* [in]  SOCK from DSOCK_Create[Ex]()*/
821
 
 const char*     host,                  /* [in]  peer host                   */
822
 
 unsigned short  port                   /* [in]  peer port                   */
 
1137
(SOCK            sock,                  
 
1138
 const char*     host,                 
 
1139
 unsigned short  port                   
823
1140
 );
824
1141
 
825
 
 
 
1142
/**
 
1143
 * @param sock
 
1144
 *  [in] SOCK from DSOCK_Create[Ex]()
 
1145
 * @param timeout
 
1146
 *  [in] time to wait for message
 
1147
 */
826
1148
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_WaitMsg
827
 
(SOCK            sock,                  /* [in]  SOCK from DSOCK_Create[Ex]()*/
828
 
 const STimeout* timeout                /* [in]  time to wait for message    */
 
1149
(SOCK            sock,                 
 
1150
 const STimeout* timeout            
829
1151
 );
830
1152
 
831
 
 
 
1153
/**
 
1154
 * @param sock
 
1155
 *  [in] SOCK from DSOCK_Create[Ex]()
 
1156
 * @param host
 
1157
 *  [in] hostname or dotted IP
 
1158
 * @param port
 
1159
 *  [in] port number, host byte order
 
1160
 * @param data
 
1161
 *  [in] additional data to send
 
1162
 * @param datalen
 
1163
 *  [in] size of additional data (bytes)
 
1164
 */
832
1165
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_SendMsg
833
 
(SOCK            sock,                  /* [in]  SOCK from DSOCK_Create[Ex]()*/
834
 
 const char*     host,                  /* [in]  hostname or dotted IP       */
835
 
 unsigned short  port,                  /* [in]  port number, host byte order*/
836
 
 const void*     data,                  /* [in]  additional data to send     */
837
 
 size_t          datalen                /* [in]  size of addtl data (bytes)  */
 
1166
(SOCK            sock,                
 
1167
 const char*     host,                 
 
1168
 unsigned short  port,           
 
1169
 const void*     data,                 
 
1170
 size_t          datalen               
838
1171
 );
839
1172
 
840
 
 
 
1173
/**
 
1174
 * @param sock
 
1175
 *  [in] SOCK from DSOCK_Create[Ex]()
 
1176
 * @param buf
 
1177
 *  [in] buf to store msg at,m.b.NULL
 
1178
 * @param buflen
 
1179
 *  [in]  buf length provided 
 
1180
 * @param maxmsglen
 
1181
 *  [in] maximal expected message len
 
1182
 * @param msglen
 
1183
 *  [out] actual msg size, may be NULL
 
1184
 * @param sender_addr
 
1185
 *  [out] net byte order, may be NULL
 
1186
 * @param sender_port
 
1187
 *  [out] host byte order, may be NULL
 
1188
 */
841
1189
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_RecvMsg
842
 
(SOCK            sock,                  /* [in]  SOCK from DSOCK_Create[Ex]()*/
843
 
 void*           buf,                   /* [in]  buf to store msg at,m.b.NULL*/
844
 
 size_t          buflen,                /* [in]  buf length provided         */
845
 
 size_t          maxmsglen,             /* [in]  maximal expected message len*/
846
 
 size_t*         msglen,                /* [out] actual msg size, may be NULL*/
847
 
 unsigned int*   sender_addr,           /* [out] net byte order, may be NULL */
848
 
 unsigned short* sender_port            /* [out] host byte order, may be NULL*/
 
1190
(SOCK            sock,                 
 
1191
 void*           buf,                  
 
1192
 size_t          buflen,                
 
1193
 size_t          maxmsglen,             
 
1194
 size_t*         msglen,                
 
1195
 unsigned int*   sender_addr,           
 
1196
 unsigned short* sender_port            
849
1197
);
850
1198
 
851
 
 
 
1199
/**
 
1200
 * @param sock
 
1201
 *  [in] SOCK from DSOCK_Create[Ex]()
 
1202
 * @param direction
 
1203
 *  [in] either of eIO_Read|eIO_Write
 
1204
 */
852
1205
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_WipeMsg
853
 
(SOCK            sock,                  /* [in]  SOCK from DSOCK_Create[Ex]()*/
854
 
 EIO_Event       direction              /* [in]  either of eIO_Read|eIO_Write*/
 
1206
(SOCK            sock,                 
 
1207
 EIO_Event       direction             
855
1208
 );
856
1209
 
857
 
 
 
1210
/**
 
1211
 * @param sock
 
1212
 *  [in] SOCK from DSOCK_Create[Ex]()
 
1213
 * @param broadcast
 
1214
 *  [in] set(1)/unset(0) bcast capab
 
1215
 */
858
1216
extern NCBI_XCONNECT_EXPORT EIO_Status DSOCK_SetBroadcast
859
 
(SOCK            sock,                  /* [in]  SOCK from DSOCK_Create[Ex]()*/
860
 
 int/*bool*/     broadcast              /* [in]  set(1)/unset(0) bcast capab.*/
 
1217
(SOCK            sock,                  
 
1218
 int/**bool*/    broadcast             
861
1219
 );
862
1220
 
863
1221
 
867
1225
 */
868
1226
 
869
1227
 
870
 
/* Return non-zero value if socket "sock" was created by DSOCK_Create[Ex]().
871
 
 * Return zero otherwise.
872
 
 */
873
 
extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_IsDatagram(SOCK sock);
874
 
 
875
 
 
876
 
/* Return non-zero value if socket "sock" was created by SOCK_Create[Ex]().
877
 
 * Return zero otherwise.
878
 
 */
879
 
extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_IsClientSide(SOCK sock);
880
 
 
881
 
 
882
 
/* Return non-zero value if socket "sock" was created by LSOCK_Accept().
883
 
 * Return zero otherwise.
884
 
 */
885
 
extern NCBI_XCONNECT_EXPORT int/*bool*/ SOCK_IsServerSide(SOCK sock);
 
1228
/**
 
1229
 * @param sock
 
1230
 *  [in]  socket handle 
 
1231
 * @return 
 
1232
 *  Non-zero value if socket "sock" was created by DSOCK_Create[Ex]().
 
1233
 *  Return zero otherwise.
 
1234
 */
 
1235
extern NCBI_XCONNECT_EXPORT int/**bool*/ SOCK_IsDatagram(SOCK sock);
 
1236
 
 
1237
 
 
1238
/**
 
1239
 * @param sock
 
1240
 *  [in]  socket handle 
 
1241
 * @return
 
1242
 *  Non-zero value if socket "sock" was created by SOCK_Create[Ex]().
 
1243
 *  Return zero otherwise.
 
1244
 */
 
1245
extern NCBI_XCONNECT_EXPORT int/**bool*/ SOCK_IsClientSide(SOCK sock);
 
1246
 
 
1247
 
 
1248
/**
 
1249
 * @param sock
 
1250
 *  [in]  socket handle 
 
1251
 * @return
 
1252
 *  Non-zero value if socket "sock" was created by LSOCK_Accept().
 
1253
 *  Return zero otherwise.
 
1254
 */
 
1255
extern NCBI_XCONNECT_EXPORT int/**bool*/ SOCK_IsServerSide(SOCK sock);
 
1256
 
 
1257
 
 
1258
/**
 
1259
 * @param sock
 
1260
 *  [in]  socket handle 
 
1261
 * @return
 
1262
 *  Non-zero value if socket "sock" was created by LSOCK_Accept().
 
1263
 *  Return zero otherwise.
 
1264
 */
 
1265
extern NCBI_XCONNECT_EXPORT int/**bool*/ SOCK_IsUNIX(SOCK sock);
886
1266
 
887
1267
 
888
1268
 
891
1271
 */
892
1272
 
893
1273
 
894
 
/* Return zero on success, non-zero on error.  See BSD gethostname().
895
 
 * On error "name" returned emptied (name[0] == '\0').
 
1274
/**
 
1275
 * @param name
 
1276
 *  [out] (guaranteed to be '\0'-terminated)
 
1277
 * @param namelen
 
1278
 *  [in] max # of bytes allowed to put to "name" 
 
1279
 * @return
 
1280
 *  Zero on success, non-zero on error.  See BSD gethostname().
 
1281
 *  On error "name" returned emptied (name[0] == '\0').
896
1282
 */
897
1283
extern NCBI_XCONNECT_EXPORT int SOCK_gethostname
898
 
(char*  name,          /* [out] (guaranteed to be '\0'-terminated)           */
899
 
 size_t namelen        /* [in]  max # of bytes allowed to put to "name"      */
 
1284
(char*  name,          
 
1285
 size_t namelen        
900
1286
 );
901
1287
 
902
1288
 
903
 
/* Return zero on success, non-zero on error.  Vaguely related to BSD's
904
 
 * inet_ntoa(). On error "buf" returned emptied (buf[0] == '\0').
 
1289
/**
 
1290
 * @param addr
 
1291
 *  [in] must be in the network byte-order
 
1292
 * @param buf
 
1293
 *  [out] to be filled by smth. like "123.45.67.89\0"
 
1294
 * @param buflen
 
1295
 *  [in] max # of bytes to put to "buf"
 
1296
 * @return
 
1297
 *  Zero on success, non-zero on error.  Vaguely related to BSD's
 
1298
 *  inet_ntoa(). On error "buf" returned emptied (buf[0] == '\0').
905
1299
 */
906
1300
extern NCBI_XCONNECT_EXPORT int SOCK_ntoa
907
 
(unsigned int addr,      /* [in]  must be in the network byte-order          */
908
 
 char*        buf,       /* [out] to be filled by smth. like "123.45.67.89\0"*/
909
 
 size_t       buflen     /* [in]  max # of bytes to put to "buf"             */
 
1301
(unsigned int addr,     
 
1302
 char*        buf,      
 
1303
 size_t       buflen    
910
1304
 );
911
1305
 
912
1306
 
913
 
/* See man for the BSDisms, htonl() and htons().
 
1307
/** See man for the BSDisms, htonl() and htons().
 
1308
 * @param value
 
1309
 * 
914
1310
 */
915
1311
extern NCBI_XCONNECT_EXPORT unsigned int SOCK_HostToNetLong
916
1312
(unsigned int value
918
1314
 
919
1315
#define SOCK_NetToHostLong SOCK_HostToNetLong
920
1316
 
 
1317
/** 
 
1318
 * @param value
 
1319
 * 
 
1320
 */
921
1321
extern NCBI_XCONNECT_EXPORT unsigned short SOCK_HostToNetShort
922
1322
(unsigned short value
923
1323
 );
924
1324
 
925
1325
#define SOCK_NetToHostShort SOCK_HostToNetShort
926
1326
 
927
 
/* Deprecated */
928
 
#define SOCK_htonl SOCK_HostToNetLong
929
 
#define SOCK_ntohl SOCK_NetToHostLong
930
 
#define SOCK_htons SOCK_HostToNetShort
931
 
#define SOCK_ntohs SOCK_NetToHostShort
932
 
 
933
 
 
934
 
/* Return INET host address (in network byte order) of the
935
 
 * specified host (or local host, if hostname is passed as NULL),
936
 
 * which can be either domain name or an IP address in
937
 
 * dotted notation (e.g. "123.45.67.89\0"). Return 0 on error.
938
 
 * NOTE: "0.0.0.0" and "255.255.255.255" are considered invalid.
 
1327
 
 
1328
/* Deprecated:  Use SOCK_{Host|Net}To{Net|Host}{Long|Short}() instead */
 
1329
#ifndef NCBI_DEPRECATED
 
1330
#  define NCBI_SOCK_DEPRECATED
 
1331
#else
 
1332
#  define NCBI_SOCK_DEPRECATED NCBI_DEPRECATED
 
1333
#endif
 
1334
extern NCBI_XCONNECT_EXPORT NCBI_SOCK_DEPRECATED
 
1335
unsigned int   SOCK_htonl(unsigned int);
 
1336
#define        SOCK_ntohl SOCK_htonl
 
1337
extern NCBI_XCONNECT_EXPORT NCBI_SOCK_DEPRECATED
 
1338
unsigned short SOCK_htons(unsigned short);
 
1339
#define        SOCK_ntohs SOCK_htons
 
1340
 
 
1341
 
 
1342
/** 
 
1343
 * @param hostname
 
1344
 *  [in] return current host address if hostname is 0 
 
1345
 * @return
 
1346
 *  INET host address (in network byte order) of the
 
1347
 *  specified host (or local host, if hostname is passed as NULL),
 
1348
 *  which can be either domain name or an IP address in
 
1349
 *  dotted notation (e.g. "123.45.67.89\0"). Return 0 on error.
 
1350
 *  @li <b>NOTE:</b> "0.0.0.0" and "255.255.255.255" are considered invalid.
939
1351
 */
940
1352
extern NCBI_XCONNECT_EXPORT unsigned int SOCK_gethostbyname
941
 
(const char* hostname  /* [in]  return current host address if hostname is 0 */
 
1353
(const char* hostname \
942
1354
 );
943
1355
 
944
1356
 
945
 
/* Take INET host address (in network byte order) and fill out the
 
1357
/** Take INET host address (in network byte order) and fill out the
946
1358
 * the provided buffer with the name, which the address corresponds to
947
 
 * (in case of multiple names the primary name is used). Return value 0
948
 
 * means error, while success is denoted by the 'name' argument returned.
949
 
 * Note that on error the name returned emptied (name[0] == '\0').
 
1359
 * (in case of multiple names the primary name is used).
 
1360
 * @param addr
 
1361
 *  [in] host address in network byte order
 
1362
 * @param name
 
1363
 *  [out] buffer to put the name to 
 
1364
 * @param namelen
 
1365
 *  [in] size (bytes) of the buffer above 
 
1366
 * @return
 
1367
 *  Value 0
 
1368
 *  means error, while success is denoted by the 'name' argument returned.
 
1369
 *  Note that on error the name returned emptied (name[0] == '\0').
950
1370
 */
951
1371
extern NCBI_XCONNECT_EXPORT char* SOCK_gethostbyaddr
952
 
(unsigned int addr,    /* [in]  host address in network byte order           */
953
 
 char*        name,    /* [out] buffer to put the name to                    */
954
 
 size_t       namelen  /* [in]  size (bytes) of the buffer above             */
 
1372
(unsigned int addr,  
 
1373
 char*        name,   
 
1374
 size_t       namelen  
 
1375
 );
 
1376
 
 
1377
 
 
1378
/**
 
1379
 * @return
 
1380
 *  Loopback address (in network byte order).
 
1381
 */
 
1382
extern NCBI_XCONNECT_EXPORT unsigned int SOCK_GetLoopbackAddress(void);
 
1383
 
 
1384
 
 
1385
/** Read (skipping leading blanks) "[host][:port]" from a string.
 
1386
 * @param str
 
1387
 *  must not be NULL
 
1388
 * @param host
 
1389
 *  must not be NULL
 
1390
 * @param port
 
1391
 *  must not be NULL
 
1392
 * @return
 
1393
 *  On success, return the advanced pointer past the host/port read.
 
1394
 *  If no host/port detected, return 'str'.
 
1395
 *  On format error, return 0.
 
1396
 *  If host and/or port fragments are missing,
 
1397
 *  then corresponding 'host'/'port' value returned as 0.
 
1398
 *  Note that 'host' returned is in network byte order,
 
1399
 *  unlike 'port', which always comes out in host (native) byte order.
 
1400
 */
 
1401
extern NCBI_XCONNECT_EXPORT const char* SOCK_StringToHostPort
 
1402
(const char*     str,  
 
1403
 unsigned int*   host,  
 
1404
 unsigned short* port   
 
1405
 );
 
1406
 
 
1407
 
 
1408
/** Print host:port into provided buffer string, not to exceed 'buflen' size.
 
1409
 * Suppress printing host if parameter 'host' is zero.
 
1410
 * @param host
 
1411
 * 
 
1412
 * @param port
 
1413
 * 
 
1414
 * @param buf
 
1415
 * 
 
1416
 * @param buflen
 
1417
 * 
 
1418
 * @return
 
1419
 *  Number of bytes printed.
 
1420
 */
 
1421
extern NCBI_XCONNECT_EXPORT size_t SOCK_HostPortToString
 
1422
(unsigned int   host,
 
1423
 unsigned short port,
 
1424
 char*          buf,
 
1425
 size_t         buflen
955
1426
 );
956
1427
 
957
1428
 
966
1437
/*
967
1438
 * ---------------------------------------------------------------------------
968
1439
 * $Log: ncbi_socket.h,v $
 
1440
 * Revision 6.61  2006/03/16 19:12:21  ucko
 
1441
 * Fix SOCK_CreateOnTopEx's prototype, which accidentally lost one
 
1442
 * argument in the previous commit.
 
1443
 *
 
1444
 * Revision 6.60  2006/03/16 18:15:34  serovav
 
1445
 * added markup for Doxygen
 
1446
 *
 
1447
 * Revision 6.58  2006/01/27 19:17:41  lavr
 
1448
 * Fix NCBI_DEPRECATED placement to satify MSVC compiler
 
1449
 *
 
1450
 * Revision 6.57  2006/01/27 16:57:15  lavr
 
1451
 * Added SOCK_StringHostToPort() and SOCK_HostPortToString() [from ncbi_connutil]
 
1452
 *
 
1453
 * Revision 6.56  2005/07/19 19:54:56  lavr
 
1454
 * +SOCK_GetLoopbackAddress()
 
1455
 *
 
1456
 * Revision 6.55  2005/05/20 11:39:55  lavr
 
1457
 * Correct documentation on SOCK_ReadLine()
 
1458
 *
 
1459
 * Revision 6.54  2005/01/05 17:37:20  lavr
 
1460
 * SOCK_ReadLine() documented in details
 
1461
 *
 
1462
 * Revision 6.53  2004/11/09 21:13:00  lavr
 
1463
 * +ReadLine
 
1464
 *
 
1465
 * Revision 6.52  2004/10/26 14:46:06  lavr
 
1466
 * <ncbi_socket.h> -> <ncbi_socket_unix.h>
 
1467
 *
 
1468
 * Revision 6.51  2004/10/19 18:05:07  lavr
 
1469
 * +SOCK_DisableOSSendDelay
 
1470
 *
969
1471
 * Revision 6.50  2004/07/23 19:04:38  lavr
970
1472
 * LSOCK_CreateEx(): last parameter to become flags (bitmask)
971
1473
 *