File: repl_comm.c

package info (click to toggle)
fis-gtm 6.3-014-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 36,680 kB
  • sloc: ansic: 333,039; asm: 5,180; csh: 4,956; sh: 1,924; awk: 291; makefile: 66; sed: 13
file content (698 lines) | stat: -rw-r--r-- 26,110 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/****************************************************************
 *								*
 * Copyright (c) 2001-2020 Fidelity National Information	*
 * Services, Inc. and/or its subsidiaries. All rights reserved.	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"

#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#ifdef USE_POLL
# include <sys/poll.h>
#endif

#include "gtm_stdio.h"
#include "gtm_string.h"
#include "gtm_socket.h"
#include "gtm_inet.h"
#include "gtm_netdb.h"
#include "gtm_fcntl.h"
#include "gtm_unistd.h"
#include "gtm_stat.h"
#include "gtm_select.h"
#include "gtm_time.h"
#include "eintr_wrappers.h"

#include "gtmio.h"
#include "repl_msg.h"
#include "repl_errno.h"
#include "repl_dbg.h"
#include "iosp.h"
#include "repl_comm.h"
#include "repl_sp.h"
#include "min_max.h"
#include "rel_quant.h"
#include "repl_log.h"
#include "gtmmsg.h"
#include "gt_timer.h"
#include "have_crit.h"
#ifdef GTM_TLS
#include "error.h"	/* For MAKE_MSG_WARNING macro. */
#include "lv_val.h"
#include "fgncalsp.h"
#include "gtm_tls.h"
#include "gtm_repl.h"
#endif
#ifdef DEBUG
#include "wbox_test_init.h"
#endif

/* These statistics are useful and should perhaps be collected - Vinaya 2003/08/18
 *
 * Common:
 *
 * Send:
 * # calls to repl_send
 * # calls to repl_send with unaligned buffer
 * # bytes repl_send called with, distribute into buckets
 * % of input bytes repl_send actually sent, or # bytes actually sent distributed into buckets
 * # calls to select, and timeout distributed into buckets
 * # calls to send
 * # calls to select that were interrupted (EINTR)
 * # calls to select that were unsuccessful due to system resource shortage (EAGAIN)
 * # calls to select that timed out
 * # calls to send that were interrupted (EINTR)
 * # calls to send that failed due to the message size being too big (EMSGSIZE)
 * # calls to send that would have blocked (EWOULDBLOCK)
 *
 * Receive:
 * # calls to repl_recv
 * # calls to repl_recv with unaligned buffer
 * # bytes repl_recv called with, distribute into buckets
 * % of input length repl_recv actually received, or # bytes actuall received distributed into buckets
 * # calls to select, and timeout distributed into buckets
 * # calls to recv
 * # calls to select that were interrupted (EINTR)
 * # calls to select that were unsuccessful due to system resource shortage (EAGAIN)
 * # calls to select that timed out
 * # calls to recv that were interrupted (EINTR)
 * # calls to recv that failed due to the connection reset (bytes received == 0)
 * # calls to recv that would have blocked (EWOULDBLOCK)
 */

GBLDEF	int			repl_max_send_buffsize, repl_max_recv_buffsize;
GBLREF  boolean_t               is_rcvr_server;
GBLREF  boolean_t               is_src_server;
#ifdef GTM_TLS
GBLREF	gtm_tls_ctx_t		*tls_ctx;
GBLREF	char			dl_err[MAX_ERRSTR_LEN];
#endif

#if defined(__hppa) || defined(__vms)
#define REPL_SEND_TRACE_BUFF_SIZE 65536
#define REPL_RECV_TRACE_BUFF_SIZE 65536
#else
#define REPL_SEND_TRACE_BUFF_SIZE (MAX_REPL_MSGLEN * 2) /* For symmetry with the below */
#define REPL_RECV_TRACE_BUFF_SIZE (MAX_REPL_MSGLEN * 2) /* To debug problems in the Receiver's msgbuff (gtmrecv_msgp) management */
#endif
#define REPL_SEND_SIZE_TRACE_SIZE 1024
#define REPL_RECV_SIZE_TRACE_SIZE 1024
STATICDEF int repl_send_trace_buff_pos = 0;
STATICDEF unsigned char * repl_send_trace_buff = 0;
STATICDEF int repl_send_size_trace_pos = 0;
STATICDEF int repl_send_size_trace[REPL_SEND_SIZE_TRACE_SIZE];
STATICDEF int repl_recv_trace_buff_pos = 0;
STATICDEF unsigned char * repl_recv_trace_buff = 0;
STATICDEF int repl_recv_size_trace_pos = 0;
STATICDEF int repl_recv_size_trace[REPL_RECV_SIZE_TRACE_SIZE];

error_def(ERR_GETADDRINFO);
error_def(ERR_GETNAMEINFO);
error_def(ERR_GETSOCKNAMERR);
error_def(ERR_TEXT);
error_def(ERR_TLSCLOSE);
error_def(ERR_TLSDLLNOOPEN);
error_def(ERR_TLSINIT);
error_def(ERR_TLSIOERROR);
error_def(ERR_TLSCONNINFO);

#define REPL_TRACE_BUFF(TRACE_BUFF, TRACE_BUFF_POS, IO_BUFF, IO_SIZE, MAX_TRACE_SIZE)			\
{													\
	if (IO_SIZE > MAX_TRACE_SIZE)									\
	{												\
		memcpy(TRACE_BUFF, IO_BUFF + IO_SIZE - MAX_TRACE_SIZE, MAX_TRACE_SIZE);			\
		TRACE_BUFF_POS = 0;									\
	} else												\
	{												\
		int space_to_end = MAX_TRACE_SIZE - TRACE_BUFF_POS;					\
		if (IO_SIZE > space_to_end)								\
		{											\
			memcpy(TRACE_BUFF + TRACE_BUFF_POS, IO_BUFF, space_to_end);			\
			memcpy(TRACE_BUFF, IO_BUFF + space_to_end, IO_SIZE - space_to_end);		\
		} else											\
			memcpy(TRACE_BUFF + TRACE_BUFF_POS, IO_BUFF, IO_SIZE);				\
		TRACE_BUFF_POS = (TRACE_BUFF_POS + IO_SIZE) % MAX_TRACE_SIZE;				\
	}												\
}

int fd_ioready(int sock_fd, int poll_direction, int timeout)
{
	int		save_errno, status, EAGAIN_cnt = 0;
#	ifdef USE_POLL
	struct pollfd	fds;
#	else
	fd_set		fds, *readfds, *writefds;
	struct timeval	timeout_spec;
#	endif

	assert(timeout < MILLISECS_IN_SEC);
	SELECT_ONLY(timeout = timeout * 1000);		/* Convert to microseconds (~ 1sec) */
	assert((timeout >= 0) && (timeout < POLL_ONLY(MILLISECS_IN_SEC) SELECT_ONLY(MICROSECS_IN_SEC)));
#	ifdef USE_POLL
	fds.fd = sock_fd;
	fds.events = (REPL_POLLIN == poll_direction) ? POLLIN : POLLOUT;
#	else
	readfds = writefds = NULL;
	timeout_spec.tv_sec = 0;
	timeout_spec.tv_usec = timeout;
	assertpro(FD_SETSIZE > sock_fd);
	FD_ZERO(&fds);
	FD_SET(sock_fd, &fds);
	writefds = (REPL_POLLOUT == poll_direction) ? &fds : NULL;
	readfds = (REPL_POLLIN == poll_direction) ? &fds : NULL;
#	endif
	POLL_ONLY(while (-1 == (status = poll(&fds, 1, timeout))))
	SELECT_ONLY(while (-1 == (status = select(sock_fd + 1, readfds, writefds, NULL, &timeout_spec))))
	{
		save_errno = ERRNO;
		if (EINTR == save_errno)
		{	/* Give it another shot. But, halve the timeout so we don't keep doing this forever. */
			timeout = timeout >> 1;
		} else if (EAGAIN == save_errno)
		{	/* Resource starved system; relinquish the processor in the hope that we may get the required resources
			 * next time around.
			 */
			if (0 == ++EAGAIN_cnt % REPL_COMM_LOG_EAGAIN_INTERVAL)
			{
				repl_log(stderr, TRUE, TRUE, "Communication subsytem warning: System appears to be resource "
						"starved. EAGAIN returned from select()/poll() %d times\n", EAGAIN_cnt);
			}
			rel_quant();	/* this seems legit */
		} else
			return -1;
		/* Just in case select() modifies the incoming arguments, restore fd_set and timeout_spec */
		SELECT_ONLY(
			assert(0 == timeout_spec.tv_sec);
			timeout_spec.tv_usec = timeout;	/* Note: timeout is the reduced value (in case of EINTR) */
			FD_SET(sock_fd, &fds);
		)
	}
	return status;
}

int repl_send(int sock_fd, unsigned char *buff, int *send_len, int timeout GTMTLS_ONLY_COMMA(int *poll_direction))
{
	int		send_size, status, io_ready, save_errno, EMSGSIZE_cnt = 0, EWOULDBLOCK_cnt = 0;
  	ssize_t		bytes_sent;
	char const	*errptr;

	if (!repl_send_trace_buff)
		repl_send_trace_buff = malloc(REPL_SEND_TRACE_BUFF_SIZE);
	/* Note: there is no corresponding free for this malloc since it is only done once per process and will not
	 * accumulate across multiple process invocations. It will be "freed" when the mupip process exits.
	 */
	assert(FD_INVALID != sock_fd);
	send_size = *send_len;
	/* VMS returns SYSTEM-F-INVBUFLEN if send_size is larger than the hard limit VMS_MAX_TCP_SEND_SIZE (64K - 1 on some
	 * impelementations, 64K - 512 on some others). VMS_MAX_TCP_SEND_SIZE may be larger than repl_max_send_buffsize, and
	 * empirically we have noticed send() successfully sending repl_max_send_buffsize or more bytes.
	 */
	VMS_ONLY(send_size = MIN(send_size, VMS_MAX_TCP_SEND_SIZE));
	*send_len = 0;
#	ifdef GTM_TLS
	if (repl_tls.enabled && (REPL_INVALID_POLL_DIRECTION != *poll_direction))
	{
		if (0 >= (io_ready = fd_ioready(sock_fd, *poll_direction, timeout)))
		{
			if (!io_ready)
				return SS_NORMAL;
			save_errno = ERRNO;
			repl_errno = EREPL_SELECT;
			return save_errno;
		}
		/* Fall-through */
	}
#	endif
	if (0 < (io_ready = fd_ioready(sock_fd, REPL_POLLOUT, timeout)))
	{
		/* Trace last REPL_SEND_SIZE_TRACE_SIZE sizes of what was sent */
		repl_send_size_trace[repl_send_size_trace_pos++] = send_size;
		repl_send_size_trace_pos %= ARRAYSIZE(repl_send_size_trace);
		/* Trace last REPL_SEND_TRACE_BUFF_SIZE bytes sent. */
		assert(0 < send_size);
		REPL_TRACE_BUFF(repl_send_trace_buff, repl_send_trace_buff_pos, buff, send_size, REPL_SEND_TRACE_BUFF_SIZE);
		/* The check for EINTR below is valid and should not be converted to an EINTR wrapper macro, because other errno
		 * values are being checked.
		 */
		while (TRUE)
		{
			bytes_sent = GTMTLS_ONLY(repl_tls.enabled ? gtm_tls_send(repl_tls.sock, (char *)buff, send_size)
							: ) send(sock_fd, (char *)buff, send_size, 0);	/* BYPASSOK(send) */
			if (WBTEST_ENABLED(WBTEST_REPLCOMM_SEND_SRC) && is_src_server)
			{
				GTM_WHITE_BOX_TEST(WBTEST_REPLCOMM_SEND_SRC, bytes_sent, -1);
			}
			if (0 <= bytes_sent)
			{
				assert(0 < bytes_sent);
				*send_len = (int)bytes_sent;
				REPL_DPRINT2("repl_send: returning with send_len %ld\n", bytes_sent);
				return SS_NORMAL;
			}
#			ifdef GTM_TLS
			if (repl_tls.enabled && ((GTMTLS_WANT_READ == bytes_sent) || (GTMTLS_WANT_WRITE == bytes_sent)))
			{	/* TLS/SSL implementation couldn't complete the send() without reading or writing more data. Treat
				 * as if nothing was sent. But, mark the poll direction so that then next call to repl_recv waits
				 * for the TCP/IP pipe to be ready for an I/O.
				 */
				*poll_direction = (GTMTLS_WANT_READ == bytes_sent) ? REPL_POLLIN : REPL_POLLOUT;
				return SS_NORMAL;
			}
			/* handle error */
			save_errno = repl_tls.enabled ? gtm_tls_errno() : ERRNO;
			if (-1 == save_errno)
			{	/* This indicates an error from TLS/SSL layer and not from a system call. */
				assert(repl_tls.enabled);
				errptr = gtm_tls_get_error();
				rts_error_csa(CSA_ARG(NULL) VARLSTCNT(8) ERR_TLSIOERROR, 2, LEN_AND_LIT("send"), ERR_TEXT, 2,
						LEN_AND_STR(errptr));
			}
#			else
			save_errno = ERRNO;
#			endif
			if (WBTEST_ENABLED(WBTEST_REPLCOMM_SEND_SRC) && is_src_server)
			{
				repl_log(stderr, TRUE, TRUE,"Changing save errno\n");
				save_errno = 70;
			}
			assert((EMSGSIZE != save_errno) && (EWOULDBLOCK != save_errno));
			if (EINTR == save_errno)
				continue;
			if (EMSGSIZE == save_errno)
			{
				if (send_size > REPL_COMM_MIN_SEND_SIZE)
				{
					if ((send_size >> 1) <= REPL_COMM_MIN_SEND_SIZE)
						send_size = REPL_COMM_MIN_SEND_SIZE;
					else
						send_size >>= 1;
				}
				if (0 == ++EMSGSIZE_cnt % REPL_COMM_LOG_EMSGSIZE_INTERVAL)
				{
					repl_log(stderr, TRUE, TRUE, "Communication subsystem warning: System appears to be "
							"clogged; EMSGSIZE returned from send %d times\n", EMSGSIZE_cnt);
				}
			} else if (EWOULDBLOCK == save_errno)
			{
				if (0 == ++EWOULDBLOCK_cnt % REPL_COMM_LOG_EWDBLCK_INTERVAL)
				{
					repl_log(stderr, TRUE, TRUE, "Communication subsystem warning: System appears to be "
							"running slow; EWOULDBLOCK returned from send %d times\n", EWOULDBLOCK_cnt);
				}	/* the rel_quant below might be legit */
				rel_quant(); /* Relinquish our quanta in the hope that things get cleared next time around */
			} else
				break;
		}
		repl_errno = EREPL_SEND;
		repl_log(stderr, TRUE, TRUE, "Returning err: %d\n",save_errno);
		return save_errno;
	} else if (!io_ready)
		return SS_NORMAL;
	save_errno = ERRNO;
	repl_errno = EREPL_SELECT;
	return save_errno;
}

int repl_recv(int sock_fd, unsigned char *buff, int *recv_len, int timeout GTMTLS_ONLY_COMMA(int *poll_direction))
{
	int		status, max_recv_len, io_ready, save_errno;
	ssize_t		bytes_recvd;
	char const	*errptr;

	if (!repl_recv_trace_buff)
		repl_recv_trace_buff = malloc(REPL_RECV_TRACE_BUFF_SIZE);
	/* Note: there is no corresponding free for this malloc since it is only done once per process and will not
	 * accumulate across multiple process invocations. It will be "freed" when the mupip process exits.
	 */
	assert(FD_INVALID != sock_fd);
	max_recv_len = *recv_len;
	/* VMS returns SYSTEM-F-INVBUFLEN if send_size is larger than the hard limit VMS_MAX_TCP_RECV_SIZE (64K - 1 on some
	 * impelementations, 64K - 512 on some others). VMS_MAX_TCP_RECV_SIZE may be larger than repl_max_send_buffsize, and
	 * empirically we have noticed send() successfully sending repl_max_send_buffsize or more bytes.
	 */
	VMS_ONLY(max_recv_len = MIN(max_recv_len, VMS_MAX_TCP_RECV_SIZE));
	*recv_len = 0;
#	ifdef GTM_TLS
	if (repl_tls.enabled && (REPL_INVALID_POLL_DIRECTION != *poll_direction))
	{
		if (0 >= (io_ready = fd_ioready(sock_fd, *poll_direction, timeout)))
		{
			if (!io_ready)
				return SS_NORMAL;
			save_errno = ERRNO;
			repl_errno = EREPL_SELECT;
			return save_errno;
		}
		/* Fall-through */
	}
#	endif
	if ((0 < (io_ready = fd_ioready(sock_fd, REPL_POLLIN, timeout)))
		GTMTLS_ONLY( || (repl_tls.enabled && gtm_tls_cachedbytes(repl_tls.sock))))
	{
		while (TRUE)
		{
			if ((WBTEST_ENABLED(WBTEST_REPL_TLS_RECONN)) && is_rcvr_server)
			{
				GTM_WHITE_BOX_TEST(WBTEST_REPL_TLS_RECONN, bytes_recvd, -1);
				if (-1 == bytes_recvd)
					gtm_wbox_input_test_case_count = 11; /* Do not go to white box again*/
			}
			bytes_recvd = GTMTLS_ONLY(repl_tls.enabled ? gtm_tls_recv(repl_tls.sock, (char *)buff, max_recv_len)
							 : ) recv(sock_fd, (char *)buff, max_recv_len, 0);	/* BYPASSOK */
			if (((WBTEST_ENABLED(WBTEST_FETCHCOMM_ERR)) || (WBTEST_ENABLED(WBTEST_FETCHCOMM_HISTINFO)))
				 && !(is_rcvr_server || is_src_server))
			{	/* Induce the test only in mupip rollback process*/
				repl_log(stderr, TRUE, TRUE, "Inducing error\n");
				repl_log(stdout, TRUE, TRUE,"gtm_wbox_input_test_case_count is : %d\n",
								gtm_wbox_input_test_case_count);
				GTM_WHITE_BOX_TEST(WBTEST_FETCHCOMM_ERR, bytes_recvd, -1);
				GTM_WHITE_BOX_TEST(WBTEST_FETCHCOMM_HISTINFO, bytes_recvd, -1);
				LONG_SLEEP(5);
				repl_log(stdout, TRUE, TRUE, "bytes_recvd: %d\n",bytes_recvd);
			}
			if (0 < bytes_recvd)
			{
				*recv_len = (int)bytes_recvd;
				REPL_DPRINT2("repl_recv: returning with recv_len %ld\n", bytes_recvd);
				/* Trace last REPL_RECV_SIZE_TRACE_SIZE sizes of what was received */
				repl_recv_size_trace[repl_recv_size_trace_pos++] = bytes_recvd;
				repl_recv_size_trace_pos %= ARRAYSIZE(repl_recv_size_trace);
				/* Trace last REPL_RECV_TRACE_BUFF_SIZE bytes received. */
				REPL_TRACE_BUFF(repl_recv_trace_buff, repl_recv_trace_buff_pos, buff, bytes_recvd,
							REPL_RECV_TRACE_BUFF_SIZE);
				return (SS_NORMAL); /* always process the received buffer before dealing with any errno */
			}
#			ifdef GTM_TLS
			if (repl_tls.enabled && ((GTMTLS_WANT_READ == bytes_recvd) || (GTMTLS_WANT_WRITE == bytes_recvd)))
			{	/* TLS/SSL implementation couldn't complete the recv() without reading or writing more data. Treat
				 * as if nothing was read. But, mark the poll direction so that then next call to repl_recv waits
				 * for the TCP/IP pipe to be ready for an I/O.
				 */
				*poll_direction = (GTMTLS_WANT_READ == bytes_recvd) ? REPL_POLLIN : REPL_POLLOUT;
				return SS_NORMAL;
			}
			/* handle error */
			save_errno = repl_tls.enabled ? gtm_tls_errno() : ERRNO;
			if ((WBTEST_ENABLED(WBTEST_REPL_TLS_RECONN)) &&
				(ECONNRESET == save_errno))
				repl_log(stderr, TRUE, TRUE, "WBTEST_REPL_TLS_RECONN induced\n");
			if (-1 == save_errno)
			{
				assert(repl_tls.enabled);
				errptr = gtm_tls_get_error();
				rts_error_csa(CSA_ARG(NULL) VARLSTCNT(8) ERR_TLSIOERROR, 2, LEN_AND_LIT("recv"), ERR_TEXT, 2,
						LEN_AND_STR(errptr));
			}
#			else
			save_errno = ERRNO;
#			endif
			if (0 == bytes_recvd)
				save_errno = ECONNRESET;
			if ((EINTR == save_errno) && !((WBTEST_ENABLED(WBTEST_FETCHCOMM_ERR)) ||
					(WBTEST_ENABLED(WBTEST_FETCHCOMM_HISTINFO))))
				continue;
			else if (ETIMEDOUT == save_errno)
			{
				repl_log(stderr, TRUE, TRUE, "Communication subsystem warning: network may be down;"
								" socket recv() returned ETIMEDOUT\n");		/* BYPASSOK(recv) */
			} else if (EWOULDBLOCK == save_errno)
			{	/* NOTE: Although we use blocking sockets, it is possible to get EWOULDBLOCK error status if
				 * receive timeout has been set and the timeout expired before data was received (from man recv on
				 * RH 8 Linux). Some systems return ETIMEDOUT for the timeout condition.
				 */
				assert(EWOULDBLOCK != save_errno);
				repl_log(stderr, TRUE, TRUE, "Communication subsystem warning: network I/O failed to complete; "
								" Socket recv() returned EWOULDBLOCK\n");	/* BYPASSOK(recv) */
				save_errno = errno = ETIMEDOUT; /* will be treated as a bad connection and the connection closed */
			}
			break;
		}
		repl_errno = EREPL_RECV;
		if ((WBTEST_ENABLED(WBTEST_REPLCOMM_ERR) && is_rcvr_server) ||
                 (WBTEST_ENABLED(WBTEST_REPLCOMM_ERR_SRC) && is_src_server))
		{
			repl_log(stderr, TRUE, TRUE, "Changing save_errno\n");
			save_errno = 113;
		}
		if (((WBTEST_ENABLED(WBTEST_FETCHCOMM_ERR)) || (WBTEST_ENABLED(WBTEST_FETCHCOMM_HISTINFO)))
								&& !(is_rcvr_server || is_src_server))
		{
			repl_log(stdout, TRUE, TRUE, "Changing save_errno\n");
			GTM_WHITE_BOX_TEST(WBTEST_FETCHCOMM_ERR, save_errno, ECONNRESET);
			if (WBTEST_ENABLED(WBTEST_FETCHCOMM_HISTINFO) && (-1 == bytes_recvd))
			{
				save_errno = ECONNRESET;
				gtm_wbox_input_test_case_count = 9; /* Do not go to white box again*/
			}
		}
		return save_errno;
	} else if (!io_ready)	/* Select call timedout with no errors */
		return SS_NORMAL;
	save_errno = ERRNO;
	repl_errno = EREPL_SELECT;
	return save_errno;
}

int repl_close(int *sock_fd)
{
	int		status = 0;
	boolean_t	close_tls;

	/* Before closing the underlying transport, close the TLS/SSL connection */
#	ifdef GTM_TLS
	close_tls = (NULL != repl_tls.sock) && (NULL != repl_tls.sock->ssl);
	if (close_tls)
	{
		assert(REPL_TLS_REQUESTED);
		gtm_tls_socket_close(repl_tls.sock);
		assert(NULL == repl_tls.sock->ssl);
	}
	CLEAR_REPL_TLS_ENABLED;
	repl_tls.renegotiate_state = REPLTLS_RENEG_STATE_NONE;
#	endif
	if (FD_INVALID != *sock_fd)
		CLOSEFILE_RESET(*sock_fd, status);	/* resets "*sock_fd" to FD_INVALID */
	return (0 == status ? 0 : ERRNO);
}

static int get_sock_buff_size(int sockfd, int *buffsize, int which_buf)
{
	int			status;
	GTM_SOCKLEN_TYPE 	optlen;

	optlen = SIZEOF(*buffsize);
        status = getsockopt(sockfd, SOL_SOCKET, which_buf, (void *)buffsize, (GTM_SOCKLEN_TYPE *)&optlen);
	return (0 == status) ? 0 : ERRNO;
}

int get_send_sock_buff_size(int sockfd, int *send_buffsize)
{
	return get_sock_buff_size(sockfd, send_buffsize, SO_SNDBUF);
}

int get_recv_sock_buff_size(int sockfd, int *recv_buffsize)
{
	return get_sock_buff_size(sockfd, recv_buffsize, SO_RCVBUF);
}

static int set_sock_buff_size(int sockfd, int buflen, int which_buf)
{
	int	status;
#	ifndef sun
	size_t	optlen;
#	else
	int	optlen;
#	endif
	optlen = SIZEOF(buflen);
        status = setsockopt(sockfd, SOL_SOCKET, which_buf, (void *)&buflen, (GTM_SOCKLEN_TYPE)optlen);
	return (0 == status) ? 0 : ERRNO;
}

int set_send_sock_buff_size(int sockfd, int buflen)
{
	return set_sock_buff_size(sockfd, buflen, SO_SNDBUF);
}

int set_recv_sock_buff_size(int sockfd, int buflen)
{
	return set_sock_buff_size(sockfd, buflen, SO_RCVBUF);
}

void repl_log_conn_info(int sock_fd, FILE *log_fp,boolean_t debug)
{
	struct sockaddr_storage	local, remote;
	struct sockaddr		*local_sa_ptr, *remote_sa_ptr;
	GTM_SOCKLEN_TYPE	len;
	int			save_errno;
	char			*errptr, local_ip[SA_MAXLEN], remote_ip[SA_MAXLEN], *debpr;
	char			port_buffer[NI_MAXSERV];
	char			local_port_buffer[NI_MAXSERV], remote_port_buffer[NI_MAXSERV];
	int			errcode;

	debug ? (debpr = "Debug ") : (debpr = "");
	len = SIZEOF(local);
	local_sa_ptr = (struct sockaddr *)&local;
	remote_sa_ptr = (struct sockaddr *)&remote;
	if (0 == getsockname(sock_fd, local_sa_ptr, (GTM_SOCKLEN_TYPE *)&len))
	{
		/* translate internal address to numeric ip address */
		GETNAMEINFO(local_sa_ptr, len, local_ip, SA_MAXLEN, local_port_buffer, NI_MAXSERV, NI_NUMERICSERV, errcode);
		if (0 != errcode)
		{
			repl_log(log_fp, TRUE, TRUE, "Error getting local name info: %s\n", gai_strerror(errcode));
			strcpy(local_port_buffer, "*UNKNOWN*");
			strcpy(local_ip, "*UNKNOWN*");
		}
	} else
	{
		save_errno = errno;
		errptr = (char *)STRERROR(save_errno);
		repl_log(log_fp, TRUE, TRUE, "Error getting local name: %s\n", errptr);
		strcpy(local_port_buffer, "*UNKNOWN*");
		strcpy(local_ip, "*UNKNOWN*");
	}
	len = SIZEOF(remote);
	if (0 == getpeername(sock_fd, remote_sa_ptr, (GTM_SOCKLEN_TYPE *)&len))
	{
		GETNAMEINFO(remote_sa_ptr, len, remote_ip, SA_MAXLEN, remote_port_buffer, NI_MAXSERV, NI_NUMERICSERV, errcode);
		if (0 != errcode)
		{
			repl_log(log_fp, TRUE, TRUE, "Error getting remote name info: %s\n", gai_strerror(errcode));
			strcpy(remote_port_buffer, "*UNKNOWN*");
			strcpy(remote_ip, "*UNKNOWN*");
		}
	} else
	{
		save_errno = errno;
		errptr = (char *)STRERROR(save_errno);
		repl_log(log_fp, TRUE, TRUE, "Error getting remote name: %s\n", errptr);
		strcpy(remote_port_buffer, "*UNKNOWN*");
		strcpy(remote_ip, "*UNKNOWN*");
	}
	repl_log(log_fp, TRUE, TRUE, "%sConnection information:: Local: %s:%s Remote: %s:%s\n", debpr,
				local_ip, local_port_buffer, remote_ip, remote_port_buffer);
	return;
}

#ifdef GTM_TLS
void repl_log_tls_info(FILE *logfp, gtm_tls_socket_t *socket)
{
	char			*expiry;
	struct tm		*localtm;
	gtm_tls_conn_info	conn_info;

	if (0 != gtm_tls_get_conn_info(repl_tls.sock, &conn_info))
	{
		gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(6) ERR_TLSCONNINFO, 0, ERR_TEXT, 2, LEN_AND_STR(gtm_tls_get_error()));
		return;
	}
	repl_log(logfp, FALSE, TRUE, "TLS/SSL Session:\n");
	repl_log(logfp, FALSE, TRUE, "  Protocol Version: %s\n", conn_info.protocol);
	repl_log(logfp, FALSE, TRUE, "  Session Algorithm: %s\n", conn_info.session_algo);
	repl_log(logfp, FALSE, TRUE, "  Compression: %s\n", conn_info.compression);
	repl_log(logfp, FALSE, TRUE, "  Session Reused: %s\n", conn_info.reused ? "YES" : "NO");
	if ('\0' != conn_info.session_id[0])
		repl_log(logfp, FALSE, TRUE, "  Session-ID: %s\n", conn_info.session_id);
	if (-1 == conn_info.session_expiry_timeout)
		expiry = "NEVER\n";	/* '\n' is added because asctime always appends a '\n' at the end. */
	else
	{
		GTM_LOCALTIME(localtm, (time_t *)&conn_info.session_expiry_timeout);
		expiry = asctime(localtm);	/* BYPASSOK to prevent workcheck wanting to convert *time(..) to GTM_*TIME(..)*/
	}
	repl_log(logfp, FALSE, TRUE, "  Session Expiry: %s", expiry);
	repl_log(logfp, FALSE, TRUE, "  Secure Renegotiation %s supported\n", conn_info.secure_renegotiation ? "IS" : "IS NOT");
	repl_log(logfp, FALSE, TRUE, "Peer Certificate Information:\n");
	repl_log(logfp, FALSE, TRUE, "  Asymmetric Algorithm: %s (%d bit)\n", conn_info.cert_algo, conn_info.cert_nbits);
	repl_log(logfp, FALSE, TRUE, "  Subject: %s\n", conn_info.subject);
	repl_log(logfp, FALSE, TRUE, "  Issuer: %s\n", conn_info.issuer);
	repl_log(logfp, FALSE, TRUE, "  Validity:\n");
	repl_log(logfp, FALSE, TRUE, "    Not Before: %s\n", conn_info.not_before);
	repl_log(logfp, FALSE, TRUE, "    Not After: %s\n", conn_info.not_after);
}

void repl_do_tls_init(FILE *logfp)
{
	boolean_t	issue_fallback_warning, status;

	assert(REPL_TLS_REQUESTED);
	assert(NULL == tls_ctx);
	assert(!repl_tls.enabled);
	issue_fallback_warning = FALSE;
	if (SS_NORMAL != (status = gtm_tls_loadlibrary()))
	{
		if (!PLAINTEXT_FALLBACK)
			rts_error_csa(CSA_ARG(NULL) VARLSTCNT(6) ERR_TLSDLLNOOPEN, 0, ERR_TEXT, 2, LEN_AND_STR(dl_err));
		else
			gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(6) MAKE_MSG_WARNING(ERR_TLSDLLNOOPEN), 0, ERR_TEXT, 2,
					LEN_AND_STR(dl_err));
		issue_fallback_warning = TRUE;
	} else if (NULL == (tls_ctx = gtm_tls_init(GTM_TLS_API_VERSION, 0)))
	{
		if (!PLAINTEXT_FALLBACK)
			rts_error_csa(CSA_ARG(NULL) VARLSTCNT(6) ERR_TLSINIT, 0, ERR_TEXT, 2, LEN_AND_STR(gtm_tls_get_error()));
		else
			gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(6) MAKE_MSG_WARNING(ERR_TLSINIT), 0, ERR_TEXT, 2,
					LEN_AND_STR(gtm_tls_get_error()));
		issue_fallback_warning = TRUE;
	}
	if (issue_fallback_warning)
	{
		repl_log(logfp, TRUE, TRUE, "Plaintext fallback enabled. TLS/SSL communication will not be attempted again.\n");
		CLEAR_REPL_TLS_REQUESTED;	/* As if -tlsid qualifier was never specified. */
	} else
	{
		assert(NULL != tls_ctx);
		repl_log(logfp, TRUE, FALSE, "TLS/SSL library for secure communication successfully loaded. ");
		repl_log(logfp, FALSE, TRUE, "Runtime library version: 0x%08x; FIPS Mode: %s\n",
				GTMTLS_RUNTIME_LIB_VERSION(tls_ctx),  GTMTLS_IS_FIPS_MODE(tls_ctx) ? "ON" : "OFF");
	}
}

int repl_do_tls_handshake(FILE *logfp, int sock_fd, boolean_t do_accept, int *poll_direction)
{
	int			io_ready, save_errno, status;

	assert(REPL_TLS_REQUESTED);
	assert(NULL != tls_ctx);
	assert(NULL != repl_tls.sock);
	assert(!repl_tls.enabled);
	if (REPL_INVALID_POLL_DIRECTION != *poll_direction)
	{
		if (0 >= (io_ready = fd_ioready(sock_fd, *poll_direction, REPL_POLL_WAIT)))
		{
			if (!io_ready)
				return REPL_POLLIN == *poll_direction ? GTMTLS_WANT_READ : GTMTLS_WANT_WRITE;
			save_errno = errno;
			repl_errno = EREPL_SELECT;
			return save_errno;
		}
		/* Fall-through */
	}
	/* Do the TLS/SSL handshake */
	save_errno = SS_NORMAL;
	if (-1 == (status = do_accept ? gtm_tls_accept(repl_tls.sock) : gtm_tls_connect(repl_tls.sock)))
		save_errno = gtm_tls_errno();
	else if ((GTMTLS_WANT_READ == status) || (GTMTLS_WANT_WRITE == status))
	{
		*poll_direction = GTMTLS_WANT_READ ? REPL_POLLIN : REPL_POLLOUT;
		return status;
	} else
	{	/* TLS/SSL handshake succeeded. Log information about the peer's certificate and the TLS/SSL connection. */
		repl_log(logfp, TRUE, TRUE, "Secure communication enabled using TLS/SSL protocol\n");
		repl_log_tls_info(logfp, repl_tls.sock);
		return SS_NORMAL;
	}
	assert(SS_NORMAL != save_errno);
	repl_errno = do_accept ? EREPL_RECV : EREPL_SEND;
	return save_errno;
}
#endif