summaryrefslogtreecommitdiff
path: root/usr/src/cmd/vscan/vscand/vs_eng.c
blob: f92ffc750d701c5b6fabf6989311c0c632866c2a (plain)
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 */

/*
 * vs_eng.c manages the vs_engines array of scan engine.
 * Access to the array and other private data is protected by vs_eng_mutex.
 * A caller can wait for an available engine connection on vs_eng_cv
 *
 */

#include <sys/types.h>
#include <sys/synch.h>
#include <sys/socket.h>
#include <sys/filio.h>
#include <sys/ioctl.h>
#include <sys/debug.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <time.h>

#include <signal.h>
#include <thread.h>

#include "vs_incl.h"

/* max connections per scan engine */
#define	VS_CXN_MAX	VS_VAL_SE_MAXCONN_MAX

/*
 * vs_eng_state_t - connection state
 *
 * Each configured scan engine supports up to vse_cfg.vep_maxconn
 * connections. These connections are represented by a vs_connection_t
 * which defines the connection state, associated socket descriptor
 * and how long the connection has been available. A connection
 * that has been available but unused for vs_inactivity_timeout
 * seconds will be closed by the housekeeper thread.
 *
 * When a scan engine is reconfigured to have less connections
 * (or is disabled) any of the superflous connections which are in
 * AVAILABLE state are closed (DISCONNECTED). Others are set to
 * CLOSE_PENDING to be closed (DISCONNECTED) when the engine is
 * released (when the current request completes).
 *
 *              +---------------------+
 *  |---------->| VS_ENG_DISCONNECTED |<-----------------|
 *  |           +---------------------+                  |
 *  |              |                                     |
 *  |              | eng_get                             |
 *  |              v                                     | release/
 *  | shutdown  +---------------------+   reconfig       | shutdown
 *  |<----------| VS_ENG_RESERVED     | -----------|     |
 *  |           +---------------------+            |     |
 *  |              |                               v     |
 *  |              |                       +----------------------+
 *  |              | connect               | VS_ENG_CLOSE_PENDING |
 *  |              |                       +----------------------+
 *  |              v                               ^
 *  | shutdown  +---------------------+            |
 *  |<----------| VS_ENG_INUSE        |------------|
 *  |           +---------------------+  reconfig/error
 *  |              |           ^
 *  |              | release   | eng_get
 *  | reconfig/    |           |
 *  | timeout/     v           |
 *  | shutdown  +---------------------+
 *  |<----------| VS_ENG_AVAILABLE    |
 *              +---------------------+
 *
 */

typedef enum {
	VS_ENG_DISCONNECTED = 0,
	VS_ENG_RESERVED,
	VS_ENG_INUSE,
	VS_ENG_AVAILABLE,
	VS_ENG_CLOSE_PENDING
} vs_eng_state_t;

typedef struct vs_connection {
	vs_eng_state_t vsc_state;
	int vsc_sockfd;
	struct timeval vsc_avail_time;
} vs_connection_t;

typedef struct vs_engine {
	vs_props_se_t vse_cfg;	/* host, port, maxconn */
	int vse_inuse;		/* # connections in use */
	boolean_t vse_error;
	vs_connection_t vse_cxns[VS_CXN_MAX];
} vs_engine_t;

static vs_engine_t vs_engines[VS_SE_MAX];

static int vs_eng_next;		/* round-robin "finger" */
static int vs_eng_count;	/* how many configured engines */
static int vs_eng_total_maxcon;	/* total configured connections */
static int vs_eng_total_inuse;	/* total connections in use */
static int vs_eng_wait_count;	/* # threads waiting for connection */

static pthread_mutex_t vs_eng_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t vs_eng_cv;
int vs_inactivity_timeout = 60; /* seconds */
int vs_reuse_connection = 1;

time_t vs_eng_wait = VS_ENG_WAIT_DFLT;

/* local functions */
static int vs_eng_connect(char *, int);
static boolean_t vs_eng_check_errors(void);
static int vs_eng_find_connection(int *, int *, boolean_t);
static int vs_eng_find_next(boolean_t);
static int vs_eng_compare(int, char *, int);
static void vs_eng_config_close(vs_engine_t *, int);
static void *vs_eng_housekeeper(void *);


#ifdef FIONBIO
/* non-blocking connect */
static int nbio_connect(int, const struct sockaddr *, int);
int vs_connect_timeout = 5000; /* milliseconds */
#endif /* FIONBIO */


/*
 * vs_eng_init
 */
void
vs_eng_init()
{
	pthread_t tid;

	(void) pthread_cond_init(&vs_eng_cv, NULL);
	(void) pthread_mutex_lock(&vs_eng_mutex);

	(void) memset(vs_engines, 0, sizeof (vs_engine_t) * VS_SE_MAX);

	vs_eng_total_maxcon = 0;
	vs_eng_total_inuse = 0;
	vs_eng_count = 0;
	vs_eng_next = 0;

	(void) pthread_mutex_unlock(&vs_eng_mutex);

	(void) pthread_create(&tid, NULL, vs_eng_housekeeper, NULL);
}


/*
 * vs_eng_config
 *
 * Configure scan engine connections.
 *
 * If a scan engine has been reconfigured (different host or port)
 * the scan engine's error count is reset.
 *
 * If the host/port has changed, the engine has been disabled
 * or less connections are configured now, connections need
 * to be closed or placed in CLOSE_PENDING state (vs_eng_config_close)
 *
 * vs_icap_config is invoked to reset engine-specific data stored
 * in vs_icap.
 *
 */
void
vs_eng_config(vs_props_all_t *config)
{
	int i;
	vs_props_se_t *cfg;
	vs_engine_t *eng;

	(void) pthread_mutex_lock(&vs_eng_mutex);

	vs_eng_count = 0;
	vs_eng_total_maxcon = 0;

	for (i = 0; i < VS_SE_MAX; i++) {
		cfg = &config->va_se[i];
		eng = &vs_engines[i];

		if (vs_eng_compare(i, cfg->vep_host, cfg->vep_port) != 0) {
			vs_eng_config_close(eng, 0);
			eng->vse_error = B_FALSE;
		}

		if (cfg->vep_enable) {
			if (cfg->vep_maxconn < eng->vse_cfg.vep_maxconn)
				vs_eng_config_close(eng, cfg->vep_maxconn);

			eng->vse_cfg = *cfg;
			vs_eng_total_maxcon += cfg->vep_maxconn;
			vs_eng_count++;
		} else {
			vs_eng_config_close(eng, 0);
			(void) memset(&eng->vse_cfg, 0, sizeof (vs_props_se_t));
		}

		vs_icap_config(i, eng->vse_cfg.vep_host, eng->vse_cfg.vep_port);
	}

	if ((vs_eng_total_maxcon <= 0) || (vs_eng_count == 0))
		syslog(LOG_NOTICE, "Scan Engine - no engines configured");

	(void) pthread_mutex_unlock(&vs_eng_mutex);
}


/*
 * vs_eng_config_close
 *
 *	If the host/port has changed, the engine has been disabled
 *	or less connections are configured now, connections need
 *	to be closed or placed in CLOSE_PENDING state
 */
static void
vs_eng_config_close(vs_engine_t *eng, int start_idx)
{
	int i;
	vs_connection_t *cxn;

	for (i = start_idx; i < eng->vse_cfg.vep_maxconn; i++) {
		cxn = &(eng->vse_cxns[i]);

		switch (cxn->vsc_state) {
		case VS_ENG_RESERVED:
		case VS_ENG_INUSE:
			cxn->vsc_state = VS_ENG_CLOSE_PENDING;
			break;
		case VS_ENG_AVAILABLE:
			(void) close(cxn->vsc_sockfd);
			cxn->vsc_sockfd = -1;
			cxn->vsc_state = VS_ENG_DISCONNECTED;
			break;
		case VS_ENG_CLOSE_PENDING:
		case VS_ENG_DISCONNECTED:
			break;
		}
	}
}


/*
 * vs_eng_fini
 */
void
vs_eng_fini()
{
	(void) pthread_cond_destroy(&vs_eng_cv);
}


/*
 * vs_eng_housekeeper
 *
 * Wakeup every (vs_inactivity_timeout / 2) seconds and close
 * any connections that are in AVAILABLE state but have not
 * been used for vs_inactivity_timeout seconds.
 */
/* ARGSUSED */
static void *
vs_eng_housekeeper(void *arg)
{
	struct timeval now;
	long expire;
	int i, j;
	vs_engine_t *eng;
	vs_connection_t *cxn;

	for (;;) {
		(void) sleep(vs_inactivity_timeout / 2);

		if (vscand_get_state() == VS_STATE_SHUTDOWN)
			break;

		(void) gettimeofday(&now, NULL);
		expire = now.tv_sec - vs_inactivity_timeout;

		(void) pthread_mutex_lock(&vs_eng_mutex);
		for (i = 0; i < VS_SE_MAX; i++) {
			eng = &(vs_engines[i]);
			for (j = 0; j < eng->vse_cfg.vep_maxconn; j++) {
				cxn = &(eng->vse_cxns[j]);

				if ((cxn->vsc_state == VS_ENG_AVAILABLE) &&
				    (cxn->vsc_avail_time.tv_sec < expire)) {
					(void) close(cxn->vsc_sockfd);
					cxn->vsc_sockfd = -1;
					cxn->vsc_state = VS_ENG_DISCONNECTED;
				}
			}
		}
		(void) pthread_mutex_unlock(&vs_eng_mutex);
	}

	return (NULL);
}


/*
 * vs_eng_set_error
 *
 * If the engine identified in conn (host, port) matches the
 * engine in vs_engines set or clear the error state of the
 * engine and update the error statistics.
 *
 * If error == 0, clear the error state(B_FALSE), else set
 * the error state (B_TRUE) and increment engine error stats
 */
void
vs_eng_set_error(vs_eng_ctx_t *eng_ctx, int error)
{
	int eidx = eng_ctx->vse_eidx;
	int cidx =  eng_ctx->vse_cidx;
	vs_engine_t *eng;

	(void) pthread_mutex_lock(&vs_eng_mutex);

	eng = &(vs_engines[eidx]);

	if (vs_eng_compare(eidx, eng_ctx->vse_host, eng_ctx->vse_port) == 0)
		eng->vse_error = (error == 0) ? B_FALSE : B_TRUE;

	if (error != 0) {
		eng->vse_cxns[cidx].vsc_state = VS_ENG_CLOSE_PENDING;
		vs_stats_eng_err(eng_ctx->vse_engid);
	}

	(void) pthread_mutex_unlock(&vs_eng_mutex);
}


/*
 * vs_eng_get
 * Get next available scan engine connection.
 * If retry == B_TRUE look for a scan engine with no errors.
 *
 * Returns: 0 - success
 *         -1 - error
 */
int
vs_eng_get(vs_eng_ctx_t *eng_ctx, boolean_t retry)
{
	struct timespec tswait;
	int eidx, cidx, sockfd;
	vs_engine_t *eng;
	vs_connection_t *cxn;

	(void) pthread_mutex_lock(&vs_eng_mutex);

	/*
	 * If no engines connections configured OR
	 * retry and only one engine configured, give up
	 */
	if ((vs_eng_total_maxcon <= 0) ||
	    ((retry == B_TRUE) && (vs_eng_count <= 1))) {
		(void) pthread_mutex_unlock(&vs_eng_mutex);
		return (-1);
	}

	tswait.tv_sec = vs_eng_wait;
	tswait.tv_nsec = 0;

	while ((vscand_get_state() != VS_STATE_SHUTDOWN) &&
	    (vs_eng_find_connection(&eidx, &cidx, retry) == -1)) {
		/* If retry and all configured engines have errors, give up */
		if (retry && vs_eng_check_errors() == B_TRUE) {
			(void) pthread_mutex_unlock(&vs_eng_mutex);
			return (-1);
		}

		/* wait for a connection to become available */
		vs_eng_wait_count++;
		if (pthread_cond_reltimedwait_np(&vs_eng_cv, &vs_eng_mutex,
		    &tswait) < 0) {
			syslog(LOG_NOTICE, "Scan Engine "
			    "- timeout waiting for available engine");
			vs_eng_wait_count--;
			(void) pthread_mutex_unlock(&vs_eng_mutex);
			return (-1);
		}
		vs_eng_wait_count--;
	}

	if (vscand_get_state() == VS_STATE_SHUTDOWN) {
		(void) pthread_mutex_unlock(&vs_eng_mutex);
		return (-1);
	}

	eng = &(vs_engines[eidx]);
	cxn = &(eng->vse_cxns[cidx]);

	/* update in use counts */
	eng->vse_inuse++;
	vs_eng_total_inuse++;

	/* update round-robin index */
	if (!retry)
		vs_eng_next = (eidx == VS_SE_MAX) ? 0 : eidx + 1;

	/* populate vs_eng_ctx_t */
	eng_ctx->vse_eidx = eidx;
	eng_ctx->vse_cidx = cidx;
	(void) strlcpy(eng_ctx->vse_engid, eng->vse_cfg.vep_engid,
	    sizeof (eng_ctx->vse_engid));
	(void) strlcpy(eng_ctx->vse_host, eng->vse_cfg.vep_host,
	    sizeof (eng_ctx->vse_host));
	eng_ctx->vse_port = eng->vse_cfg.vep_port;
	eng_ctx->vse_sockfd = cxn->vsc_sockfd;

	if (cxn->vsc_state == VS_ENG_INUSE) {
		(void) pthread_mutex_unlock(&vs_eng_mutex);
		return (0);
	}

	/* state == VS_ENG_RESERVED, need to connect */

	(void) pthread_mutex_unlock(&vs_eng_mutex);

	sockfd = vs_eng_connect(eng_ctx->vse_host, eng_ctx->vse_port);

	/* retry a failed connection once */
	if (sockfd == -1) {
		(void) sleep(1);
		sockfd = vs_eng_connect(eng_ctx->vse_host, eng_ctx->vse_port);
	}

	if (sockfd == -1) {
		syslog(LOG_NOTICE, "Scan Engine - connection error (%s:%d) %s",
		    eng_ctx->vse_host, eng_ctx->vse_port,
		    errno ? strerror(errno) : "");
		vs_eng_set_error(eng_ctx, 1);
		vs_eng_release(eng_ctx);
		return (-1);
	}

	(void) pthread_mutex_lock(&vs_eng_mutex);
	switch (cxn->vsc_state) {
	case VS_ENG_DISCONNECTED:
		/* SHUTDOWN occured */
		(void) pthread_mutex_unlock(&vs_eng_mutex);
		vs_eng_release(eng_ctx);
		return (-1);
	case VS_ENG_RESERVED:
		cxn->vsc_state = VS_ENG_INUSE;
		break;
	case VS_ENG_CLOSE_PENDING:
		/* reconfigure occured. Connection will be closed after use */
		break;
	case VS_ENG_INUSE:
	case VS_ENG_AVAILABLE:
	default:
		ASSERT(0);
		break;
	}

	cxn->vsc_sockfd = sockfd;
	eng_ctx->vse_sockfd = sockfd;

	(void) pthread_mutex_unlock(&vs_eng_mutex);
	return (0);
}


/*
 * vs_eng_check_errors
 *
 * Check if all engines with maxconn > 0 are in error state
 *
 * Returns: B_TRUE  - all (valid) engines are in error state
 *          B_FALSE - otherwise
 */
static boolean_t
vs_eng_check_errors()
{
	int i;

	for (i = 0; i < VS_SE_MAX; i++) {
		if (vs_engines[i].vse_cfg.vep_maxconn > 0 &&
		    (vs_engines[i].vse_error == B_FALSE))
			return (B_FALSE);
	}

	return (B_TRUE);
}


/*
 * vs_eng_find_connection
 *
 * Identify the next engine to be used (vs_eng_find_next()).
 * Select the engine's first connection in AVAILABLE state.
 * If no connection is in AVAILABLE state, select the first
 * that is in DISCONNECTED state.
 *
 * Returns: 0 success
 *         -1 no engine connections available (eng_idx & cxn_idx undefined)
 */
static int
vs_eng_find_connection(int *eng_idx, int *cxn_idx, boolean_t retry)
{
	int i, idx;
	vs_engine_t *eng;
	vs_connection_t *cxn;

	/* identify engine */
	if ((idx = vs_eng_find_next(retry)) == -1)
		return (-1);

	eng = &(vs_engines[idx]);
	*eng_idx = idx;

	/* identify connection */
	idx = -1;
	for (i = 0; i < eng->vse_cfg.vep_maxconn; i++) {
		cxn = &(eng->vse_cxns[i]);
		if (cxn->vsc_state == VS_ENG_AVAILABLE) {
			*cxn_idx = i;
			cxn->vsc_state = VS_ENG_INUSE;
			return (0);
		}

		if ((idx == -1) &&
		    (cxn->vsc_state == VS_ENG_DISCONNECTED)) {
			idx = i;
		}
	}

	if (idx == -1)
		return (-1);

	eng->vse_cxns[idx].vsc_state = VS_ENG_RESERVED;
	*cxn_idx = idx;
	return (0);
}


/*
 * vs_eng_find_next
 *
 * Returns: -1 no engine connections available
 *          idx of engine to use
 */
static int
vs_eng_find_next(boolean_t retry)
{
	int i;

	for (i = vs_eng_next; i < VS_SE_MAX; i++) {
		if (vs_engines[i].vse_inuse <
		    vs_engines[i].vse_cfg.vep_maxconn) {
			if (!retry || (vs_engines[i].vse_error == B_FALSE))
				return (i);
		}
	}

	for (i = 0; i < vs_eng_next; i++) {
		if (vs_engines[i].vse_inuse <
		    vs_engines[i].vse_cfg.vep_maxconn) {
			if (!retry || (vs_engines[i].vse_error == B_FALSE))
				return (i);
		}
	}

	return (-1);
}


/*
 * vs_eng_release
 */
void
vs_eng_release(const vs_eng_ctx_t *eng_ctx)
{
	int eidx = eng_ctx->vse_eidx;
	int cidx = eng_ctx->vse_cidx;
	vs_connection_t *cxn;

	(void) pthread_mutex_lock(&vs_eng_mutex);
	cxn = &(vs_engines[eidx].vse_cxns[cidx]);

	switch (cxn->vsc_state) {
	case VS_ENG_DISCONNECTED:
		break;
	case VS_ENG_RESERVED:
		cxn->vsc_state = VS_ENG_DISCONNECTED;
		break;
	case VS_ENG_INUSE:
		if (vs_reuse_connection) {
			cxn->vsc_state = VS_ENG_AVAILABLE;
			(void) gettimeofday(&cxn->vsc_avail_time, NULL);
			break;
		}
		/* FALLTHROUGH */
	case VS_ENG_CLOSE_PENDING:
		(void) close(cxn->vsc_sockfd);
		cxn->vsc_sockfd = -1;
		cxn->vsc_state = VS_ENG_DISCONNECTED;
		break;
	case VS_ENG_AVAILABLE:
	default:
		ASSERT(0);
		break;
	}

	/* decrement in use counts */
	vs_engines[eidx].vse_inuse--;
	vs_eng_total_inuse--;

	/* wake up next thread waiting for a connection */
	(void) pthread_cond_signal(&vs_eng_cv);

	(void) pthread_mutex_unlock(&vs_eng_mutex);
}


/*
 * vs_eng_close_connections
 *
 * Set vs_eng_total_maxcon to 0 to ensure no new engine sessions
 * can be initiated.
 * Close all open connections to abort in-progress scans.
 * Set connection state to DISCONNECTED.
 */
void
vs_eng_close_connections(void)
{
	int i, j;
	vs_connection_t *cxn;

	(void) pthread_mutex_lock(&vs_eng_mutex);
	vs_eng_total_maxcon = 0;

	for (i = 0; i < VS_SE_MAX; i++) {
		for (j = 0; j < VS_CXN_MAX; j++) {
			cxn = &(vs_engines[i].vse_cxns[j]);

			switch (cxn->vsc_state) {
			case VS_ENG_INUSE:
			case VS_ENG_AVAILABLE:
			case VS_ENG_CLOSE_PENDING:
				(void) close(cxn->vsc_sockfd);
				cxn->vsc_sockfd = -1;
				break;
			case VS_ENG_DISCONNECTED:
			case VS_ENG_RESERVED:
			default:
				break;

			}

			cxn->vsc_state = VS_ENG_DISCONNECTED;
		}
	}
	(void) pthread_mutex_unlock(&vs_eng_mutex);
}


/*
 * vs_eng_connect
 * open socket connection to remote scan engine
 *
 * Returns: sockfd or -1 (error)
 */
static int
vs_eng_connect(char *host, int port)
{
	int rc, sockfd, opt_nodelay, opt_keepalive, opt_reuseaddr, err_num;
	struct sockaddr_in addr;
	struct hostent *hp;

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
		return (-1);

	hp = getipnodebyname(host, AF_INET, 0, &err_num);
	if (hp == NULL) {
		(void) close(sockfd);
		return (-1);
	}

	(void) memset(&addr, 0, sizeof (addr));
	(void) memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
	addr.sin_port = htons(port);
	addr.sin_family = hp->h_addrtype;
	freehostent(hp);

#ifdef FIONBIO /* Use non-blocking mode for connect. */
	rc = nbio_connect(sockfd, (struct sockaddr *)&addr,
	    sizeof (struct sockaddr));
#else
	rc = connect(sockfd, (struct sockaddr *)&addr,
	    sizeof (struct sockaddr));
#endif

	opt_nodelay = 1;
	opt_keepalive = 1;
	opt_reuseaddr = 1;

	if ((rc < 0) ||
	    (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
	    &opt_nodelay, sizeof (opt_nodelay)) < 0) ||
	    (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
	    &opt_keepalive, sizeof (opt_keepalive)) < 0) ||
	    (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
	    &opt_reuseaddr, sizeof (opt_reuseaddr)) < 0)) {
		(void) close(sockfd);
		return (-1);
	}

	return (sockfd);
}


/*
 * nbio_connect
 *
 * Attempt to do a non-blocking connect call.
 * Wait for a maximum of "vs_connect_timeout" millisec, then check for
 * socket error to determine if connect successful or not.
 */
#ifdef FIONBIO
static int
nbio_connect(int sockfd, const struct sockaddr *sa, int sa_len)
{
	struct pollfd pfd;
	int nbio, rc;
	int error, len = sizeof (error);

	nbio = 1;
	if ((ioctl(sockfd, FIONBIO, &nbio)) < 0)
		return (connect(sockfd, sa, sa_len));

	if ((rc = connect(sockfd, sa, sa_len)) != 0) {
		if (errno == EINPROGRESS || errno == EINTR) {
			errno = 0;
			pfd.fd = sockfd;
			pfd.events = POLLOUT;
			pfd.revents = 0;

			if ((rc = poll(&pfd, 1, vs_connect_timeout)) <= 0) {
				if (rc == 0)
					errno = ETIMEDOUT;
				rc = -1;
			} else {
				if ((pfd.revents &
				    (POLLHUP | POLLERR | POLLNVAL)) ||
				    (!(pfd.revents & POLLOUT))) {
					rc = -1;
				} else {
					rc = getsockopt(sockfd, SOL_SOCKET,
					    SO_ERROR, &error, &len);
					if (rc != 0 || error != 0)
						rc = -1;
					if (error != 0)
						errno = error;
				}
			}
		}
	}

	nbio = 0;
	(void) ioctl(sockfd, FIONBIO, &nbio);

	return (rc);
}
#endif


/*
 * vs_eng_scanstamp_current
 *
 * Check if scanstamp matches that of ANY engine with no errors.
 * We cannot include engines with errors as they may have been
 * inaccessible for a long time and thus we may have an old
 * scanstamp value for them.
 * If a match is found the scanstamp is considered to be current
 *
 * returns: 1 if current, 0 otherwise
 */
int
vs_eng_scanstamp_current(vs_scanstamp_t scanstamp)
{
	int i;

	/* if scan stamp is null, not current */
	if (scanstamp[0] == '\0')
		return (0);

	/* if scanstamp matches that of any enabled engine with no errors */
	(void) pthread_mutex_lock(&vs_eng_mutex);
	for (i = 0; i < VS_SE_MAX; i++) {
		if ((vs_engines[i].vse_cfg.vep_enable) &&
		    (vs_engines[i].vse_error == B_FALSE) &&
		    (vs_icap_compare_scanstamp(i, scanstamp) == 0))
			break;
	}
	(void) pthread_mutex_unlock(&vs_eng_mutex);

	return ((i < VS_SE_MAX) ? 1 : 0);
}


/*
 * vs_eng_compare
 * compare host and port with that stored for engine idx
 *
 * Returns: 0 - if equal
 */
static int
vs_eng_compare(int idx, char *host, int port)
{
	if (vs_engines[idx].vse_cfg.vep_port != port)
		return (-1);

	if (strcmp(vs_engines[idx].vse_cfg.vep_host, host) != 0)
		return (-1);

	return (0);
}