-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestsetup.c
894 lines (700 loc) · 18.6 KB
/
testsetup.c
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2023 Ericsson AB
*/
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <xcm_addr.h>
#include "conn.h"
#include "util.h"
#include "testutil.h"
#include "testsetup.h"
struct server ts_servers[TS_NUM_SERVERS];
char *ts_domains_dir;
char *ts_domain_name;
char *ts_domains_filename;
bool ts_is_proto(const char *addr, const char *proto)
{
char actual_proto[16];
xcm_addr_parse_proto(addr, actual_proto, sizeof(actual_proto));
return strcmp(actual_proto, proto) == 0;
}
bool ts_is_tcp(const char *addr)
{
return ts_is_proto(addr, XCM_TCP_PROTO);
}
bool ts_is_tls(const char *addr)
{
return ts_is_proto(addr, XCM_TLS_PROTO) ||
ts_is_proto(addr, XCM_UTLS_PROTO);
}
bool ts_is_tcp_based(const char *addr)
{
return ts_is_tcp(addr) || ts_is_tls(addr);
}
static pid_t run_server(const char *net_ns, const char *addr)
{
pid_t p = fork();
if (p < 0)
return -1;
else if (p == 0) {
if (setenv("XCM_TLS_CERT", TS_SERVER_CERT_DIR, 1) < 0)
exit(EXIT_FAILURE);
if (net_ns != NULL && ut_net_ns_enter(net_ns) < 0)
exit(EXIT_FAILURE);
const char *pafd = TS_DEFAULT_PAFD_BIN;
const char *pafd_env = getenv(TS_PAFD_ENV);
if (pafd_env != NULL)
pafd = pafd_env;
#ifdef PAFD_DEBUG
execlp(pafd, pafd, "-l", "debug", addr, NULL);
#else
execlp(pafd, pafd, addr, NULL);
#endif
exit(EXIT_FAILURE);
} else
return p;
}
int ts_server_start(struct server *server)
{
if (server->pid > 0)
return 0;
pid_t server_pid = run_server(server->net_ns, server->addr);
if (server_pid < 0)
return -1;
if (ts_assure_server_up(server) < 0)
return -1;
server->pid = server_pid;
return 0;
}
int ts_start_servers(void)
{
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
int rc = ts_server_start(&ts_servers[i]);
if (rc < 0)
return rc;
}
return 0;
}
int ts_server_signal(struct server *server, int signo)
{
return server->pid >= 0 ? kill(server->pid, signo) : -1;
}
int ts_signal_servers(int signo)
{
int rc = 0;
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
struct server *server = &ts_servers[i];
if (server->pid > 0 && ts_server_signal(server, signo) < 0)
rc = -1;
}
return rc;
}
int ts_server_pause(struct server *server)
{
if (ts_server_signal(server, SIGSTOP) < 0)
return -1;
return 0;
}
int ts_pause_servers(void)
{
int rc = 0;
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
struct server *server = &ts_servers[i];
if (server->pid > 0 && ts_server_pause(server) < 0)
rc = -1;
}
return rc;
}
int ts_server_unpause(struct server *server)
{
if (ts_server_signal(server, SIGCONT) < 0)
return -1;
return 0;
}
int ts_unpause_servers(void)
{
int rc = 0;
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
struct server *server = &ts_servers[i];
if (server->pid > 0 && ts_server_unpause(server) < 0)
rc = -1;
}
return rc;
}
int ts_server_stop(struct server *server)
{
if (ts_server_signal(server, SIGTERM) < 0)
return -1;
if (tu_waitstatus(server->pid) < 0)
return -1;
server->pid = -1;
return 0;
}
int ts_stop_servers(void)
{
int rc = 0;
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
struct server *server = &ts_servers[i];
if (server->pid > 0 && ts_server_stop(server) < 0)
rc = -1;
}
return rc;
}
void ts_server_clear(struct server *server)
{
ut_free(server->net_ns);
ut_free(server->addr);
ut_free(server->local_addr);
server->net_ns = NULL;
server->addr = NULL;
}
uint16_t ts_random_tcp_port(void)
{
return tu_randint(15000, 25000);
}
static char *localhost_port_addr(const char *proto, uint16_t port)
{
int b = tu_randint(1, 254);
int c = tu_randint(1, 254);
int d = tu_randint(1, 254);
return ut_asprintf("%s:127.%d.%d.%d:%d", proto, b, c, d, port);
}
static char *gen_localhost_port_addr(const char *proto)
{
return localhost_port_addr(proto, ts_random_tcp_port());
}
char *ts_random_tls_addr(void)
{
return gen_localhost_port_addr("tls");
}
char *ts_random_tcp_addr(void)
{
return gen_localhost_port_addr("tcp");
}
char *ts_random_ux_addr(void)
{
return ut_asprintf("ux:%d-%d", getpid(), tu_randint(0, INT_MAX));
}
char *ts_random_addr(void)
{
int type = tu_randint(0, 2);
switch (type) {
case 0:
return ts_random_tls_addr();
case 1:
return ts_random_tcp_addr();
default:
return ts_random_ux_addr();
}
}
static char *random_local_addr(const char *addr,
bool force_kernel_allocated_port)
{
if (!ts_is_tcp_based(addr))
return NULL;
bool has_local = tu_randbool();
if (!has_local)
return NULL;
uint16_t port;
if (force_kernel_allocated_port || tu_randbool())
port = 0;
else
port = ts_random_tcp_port();
char proto[16];
xcm_addr_parse_proto(addr, proto, sizeof(proto));
return localhost_port_addr(proto, port);
}
static char *random_net_ns(void)
{
return ut_asprintf("ns-%d-%d", getpid(), tu_randint(0, INT_MAX));
}
int ts_write_nl_domains_file(const char *filename, struct server *servers,
size_t num_servers)
{
FILE *domains_file = fopen(filename, "w");
if (domains_file == NULL)
return -1;
int i;
for (i = 0; i < num_servers; i++)
if (fprintf(domains_file, "%s\n", servers[i].addr) < 0)
return -1;
if (fclose(domains_file) < 0)
return -1;
return 0;
}
int ts_write_json_domain_file(const char *filename, const char *cert_file,
const char *key_file, const char *tc_file,
const char *crl_file, int64_t proto_version_min,
int64_t proto_version_max, double idle_min,
double idle_max, struct server *servers,
size_t num_servers)
{
FILE *domains_file = fopen(filename, "w");
if (domains_file == NULL)
return -1;
fprintf(domains_file, "{\n \"servers\": [\n");
if (cert_file != NULL && key_file != NULL && tc_file != NULL &&
crl_file != NULL && unsetenv("XCM_TLS_CERT") < 0)
return -1;
int i;
for (i = 0; i < num_servers; i++) {
struct server *server = &servers[i];
fprintf(domains_file, " {\n"
" \"address\": \"%s\"", server->addr);
if (server->local_addr != NULL)
fprintf(domains_file, ",\n"
" \"localAddress\": \"%s\"", server->local_addr);
if (server->net_ns != NULL)
fprintf(domains_file, ",\n"
" \"networkNamespace\": \"%s\"", server->net_ns);
if (ts_is_tls(server->addr) && cert_file != NULL)
fprintf(domains_file, ",\n"
" \"tlsCertificateFile\": \"%s\"", cert_file);
if (ts_is_tls(server->addr) && key_file != NULL)
fprintf(domains_file, ",\n"
" \"tlsKeyFile\": \"%s\"", key_file);
if (ts_is_tls(server->addr) && tc_file != NULL)
fprintf(domains_file, ",\n"
" \"tlsTrustedCaFile\": \"%s\"", tc_file);
if (ts_is_tls(server->addr) && crl_file != NULL)
fprintf(domains_file, ",\n"
" \"tlsCrlFile\": \"%s\"", crl_file);
if (proto_version_min >= 0)
fprintf(domains_file, ",\n"
" \"minProtocolVersion\": %"PRId64,
proto_version_min);
if (proto_version_max >= 0)
fprintf(domains_file, ",\n"
" \"maxProtocolVersion\": %"PRId64,
proto_version_max);
if (idle_min >= 0)
fprintf(domains_file, ",\n"
" \"minIdleTime\": %f", idle_min);
if (idle_max >= 0)
fprintf(domains_file, ",\n"
" \"maxIdleTime\": %f", idle_max);
fprintf(domains_file, "\n"
" }");
if (i != (num_servers - 1))
fprintf(domains_file, ",");
fprintf(domains_file, "\n");
}
fprintf(domains_file, " ]\n}\n");
if (fclose(domains_file) < 0)
return -1;
return 0;
}
static int cert_setup(const char *ns_name)
{
int rc;
rc = tu_executef_es("cp %s %s/cert_%s.pem && "
"cp %s %s/key_%s.pem && "
"cp %s %s/tc_%s.pem",
TS_CLIENT_CERT, TS_CLIENT_CERT_DIR, ns_name,
TS_CLIENT_KEY, TS_CLIENT_CERT_DIR, ns_name,
TS_CLIENT_TC, TS_CLIENT_CERT_DIR, ns_name);
if (rc != 0)
return -1;
rc = tu_executef_es("cp %s %s/cert_%s.pem && "
"cp %s %s/key_%s.pem && "
"cp %s %s/tc_%s.pem",
TS_SERVER_CERT, TS_SERVER_CERT_DIR, ns_name,
TS_SERVER_KEY, TS_SERVER_CERT_DIR, ns_name,
TS_SERVER_TC, TS_SERVER_CERT_DIR, ns_name);
if (rc != 0)
return -1;
return 0;
}
static int cert_teardown(const char *ns_name)
{
int rc = tu_executef_es("rm %s/cert_%s.pem %s/key_%s.pem %s/tc_%s.pem "
"%s/cert_%s.pem %s/key_%s.pem %s/tc_%s.pem",
TS_CLIENT_CERT_DIR, ns_name, TS_CLIENT_CERT_DIR,
ns_name, TS_CLIENT_CERT_DIR, ns_name,
TS_SERVER_CERT_DIR, ns_name, TS_SERVER_CERT_DIR,
ns_name, TS_SERVER_CERT_DIR, ns_name);
return rc != 0 ? -1 : 0;
}
int ts_domain_setup(unsigned int setup_flags)
{
ts_domains_dir = ut_asprintf("./test/domains/%d", getpid());
if (tu_executef_es("mkdir -p %s", ts_domains_dir) < 0)
return -1;
ts_domain_name = ut_asprintf("testdomain-%d", getpid());
ts_domains_filename = ut_asprintf("%s/%s", ts_domains_dir, ts_domain_name);
bool use_net_ns = tu_has_sys_admin_capability() && tu_randbool();
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
struct server *server = &ts_servers[i];
if (use_net_ns && tu_randbool()) {
server->net_ns = random_net_ns();
cert_setup(server->net_ns);
} else
server->net_ns = NULL;
server->addr = ts_random_addr();
server->local_addr =
random_local_addr(server->addr,
setup_flags & REQUIRE_NO_LOCAL_PORT_BIND);
server->pid = -1;
if (server->net_ns != NULL && tu_add_net_ns(server->net_ns) < 0)
return -1;
}
if (tu_randbool() && !use_net_ns) {
if (ts_write_nl_domains_file(ts_domains_filename, ts_servers,
TS_NUM_SERVERS) < 0)
return -1;
} else {
bool tls_conf = tu_randbool();
if (tls_conf) {
if (ts_write_json_domain_file(ts_domains_filename, TS_CLIENT_CERT,
TS_CLIENT_KEY, TS_CLIENT_TC,
NULL, -1, -1, -1, -1, ts_servers,
TS_NUM_SERVERS) < 0)
return -1;
} else {
if (ts_write_json_domain_file(ts_domains_filename, NULL, NULL,
NULL, NULL, -1, -1, -1, -1, ts_servers,
TS_NUM_SERVERS) < 0)
return -1;
}
}
return 0;
}
void ts_domain_teardown(void)
{
tu_executef("rm -f %s", ts_domains_filename);
tu_executef("rmdir %s", ts_domains_dir);
ut_free(ts_domains_dir);
ut_free(ts_domain_name);
ut_free(ts_domains_filename);
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
struct server *server = &ts_servers[i];
if (server->net_ns != NULL) {
cert_teardown(server->net_ns);
tu_del_net_ns(server->net_ns);
}
ts_server_clear(server);
}
}
#define CONNECT_RETRIES 100
#define CONNECT_RETRY_INTERVAL_MS 10
static struct conn *server_connect(const struct server *server)
{
struct server_conf server_conf = {
.net_ns = server->net_ns,
.addr = server->addr,
.proto_version_min = -1,
.proto_version_max = -1
};
int64_t client_id = ut_rand_id();
char *old_cert_dir = NULL;
char *env = getenv("XCM_TLS_CERT");
if (env != NULL)
old_cert_dir = ut_strdup(env);
if (setenv("XCM_TLS_CERT", TS_CLIENT_CERT_DIR, 1) < 0)
abort();
struct conn *conn;
int i;
for (i = 0; i < CONNECT_RETRIES; i++) {
conn = conn_connect(&server_conf, client_id, NULL);
if (conn != NULL)
break;
tu_msleep(CONNECT_RETRY_INTERVAL_MS);
}
if (conn != NULL && conn_hello(conn, NULL) < 0) {
conn_close(conn);
conn = NULL;
}
if (old_cert_dir != NULL) {
if (setenv("XCM_TLS_CERT", old_cert_dir, 1) < 0)
abort();
ut_free(old_cert_dir);
} else if (unsetenv("XCM_TLS_CERT") < 0)
abort();
return conn;
}
int ts_assure_server_up(struct server *server)
{
int rc = -1;
struct conn *conn = server_connect(server);
if (conn == NULL)
goto out;
if (conn_ping(conn) < 0)
goto out_close;
rc = 0;
out_close:
conn_close(conn);
out:
return rc;
}
struct count_client_state
{
const char *client_addr;
int count;
};
static void count_client_cb(int64_t client_id, const char *client_addr,
int64_t connect_time, const double *idle,
const int64_t *proto_version,
const double *latency, void *cb_data)
{
struct count_client_state *state = cb_data;
if (state->client_addr == NULL ||
strcmp(state->client_addr, client_addr) == 0)
state->count++;
}
static int assure_client_count(struct server *server, const char *client_addr,
int count)
{
int rc = -1;
struct conn *conn = server_connect(server);
if (conn == NULL)
goto out;
struct count_client_state state = {
.client_addr = client_addr
};
if (conn_clients(conn, count_client_cb, &state) < 0)
goto out_close;
/* is this client expected to be included in the count? */
if (client_addr == NULL ||
strcmp(conn_get_local_addr(conn), client_addr) == 0)
state.count--;
if (state.count == count)
rc = 0;
out_close:
conn_close(conn);
out:
return rc;
}
int ts_assure_client_from(struct server *server, const char *client_addr)
{
return assure_client_count(server, client_addr, 1);
}
int ts_assure_client_count(struct server *server, int count)
{
return assure_client_count(server, NULL, count);
}
struct count_service_state
{
int64_t service_id;
const struct paf_props *props;
int count;
};
static void count_service_cb(int64_t service_id, int64_t generation,
const struct paf_props *props,
int64_t ttl, int64_t client_id,
const double *orphan_since, void *cb_data)
{
struct count_service_state *state = cb_data;
if ((state->service_id < 0 || state->service_id == service_id) &&
(state->props == NULL || paf_props_equal(props, state->props)))
state->count++;
}
static int server_assure_service_count(struct server *server,
int64_t service_id,
const struct paf_props *props,
int count)
{
int rc = -1;
struct conn *conn = server_connect(server);
if (conn == NULL)
goto out;
struct count_service_state state = {
.service_id = service_id,
.props = props
};
if (conn_services(conn, NULL, count_service_cb, &state) < 0)
goto out_close;
if (state.count == count)
rc = 0;
out_close:
conn_close(conn);
out:
return rc;
}
int ts_server_assure_service(struct server *server, int64_t service_id,
const struct paf_props *props)
{
return server_assure_service_count(server, service_id, props, 1);
}
int ts_assure_service(int64_t service_id, const struct paf_props *props)
{
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
int rc = ts_server_assure_service(&ts_servers[i], service_id,
props);
if (rc < 0)
return rc;
}
return 0;
}
int ts_server_assure_service_count(struct server *server, int count)
{
return server_assure_service_count(server, -1, NULL, count);
}
int ts_assure_service_count(int count)
{
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
int rc = ts_server_assure_service_count(&ts_servers[i], count);
if (rc < 0)
return rc;
}
return 0;
}
struct count_subscription_state
{
int64_t sub_id;
const char *filter;
int count;
};
static void count_subscription_cb(int64_t sub_id, int64_t client_id,
const char *filter, void *cb_data)
{
struct count_subscription_state *state = cb_data;
if ((state->sub_id < 0 || state->sub_id == sub_id) &&
(state->filter == NULL || strcmp(filter, state->filter) == 0))
state->count++;
}
int ts_server_assure_subscription(struct server *server, int64_t sub_id,
const char *filter)
{
int rc = -1;
struct conn *conn = server_connect(server);
if (conn == NULL)
goto out;
struct count_subscription_state state = {
.sub_id = sub_id,
.filter = filter
};
if (conn_subscriptions(conn, count_subscription_cb, &state) < 0)
goto out_close;
if (state.count == 1)
rc = 0;
out_close:
conn_close(conn);
out:
return rc;
}
int ts_assure_subscription(int64_t sub_id, const char *filter)
{
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
int rc = ts_server_assure_subscription(&ts_servers[i], sub_id, filter);
if (rc < 0)
return rc;
}
return 0;
}
int ts_server_assure_supports_v3(struct server *server)
{
int rc = -1;
struct conn *conn = server_connect(server);
if (conn == NULL)
goto out_close;
int64_t proto_version = conn_get_proto_version(conn);
if (proto_version == 3)
rc = 0;
else
errno = ENOTSUP;
out_close:
conn_close(conn);
return rc;
}
int ts_assure_supports_v3(void)
{
int i;
for (i = 0; i < TS_NUM_SERVERS; i++) {
int rc = ts_server_assure_supports_v3(&ts_servers[i]);
if (rc < 0)
return rc;
}
return 0;
}
struct list_client_state
{
struct conn *conn;
int64_t *clients;
int capacity;
int count;
};
static void list_client_cb(int64_t client_id, const char *client_addr,
int64_t connect_time, const double *idle,
const int64_t *proto_version,
const double *latency, void *cb_data)
{
struct list_client_state *state = cb_data;
if (conn_get_client_id(state->conn) == client_id)
return;
int idx = state->count;
state->count++;
if (state->count <= state->capacity)
state->clients[idx] = client_id;
}
int ts_server_get_client_ids(struct server *server, int64_t *clients,
size_t capacity)
{
int rc = -1;
struct conn *conn = server_connect(server);
if (conn == NULL)
goto out;
struct list_client_state state = {
.conn = conn,
.clients = clients,
.capacity = capacity
};
if (conn_clients(conn, list_client_cb, &state) < 0)
goto out_close;
return state.count;
out_close:
conn_close(conn);
out:
return rc;
}
static void get_client_cb(int64_t client_id, const char *client_addr,
int64_t connect_time, const double *idle,
const int64_t *proto_version,
const double *latency, void *cb_data)
{
struct ts_client *client = cb_data;
if (client_id == client->client_id) {
strcpy(client->client_addr, client_addr);
client->connect_time = connect_time;
client->idle = idle != NULL ? *idle : -1;
client->proto_version = proto_version != NULL ? *proto_version : -1;
client->latency = latency != NULL ? *latency : -1;
}
}
int ts_server_get_client(struct server *server, int64_t client_id,
struct ts_client *client)
{
int rc = -1;
struct conn *conn = server_connect(server);
if (conn == NULL)
goto out;
client->client_id = client_id;
client->connect_time = -1;
if (conn_clients(conn, get_client_cb, client) < 0)
goto out_close;
if (client->connect_time < 0) {
client->client_id = -1;
errno = ENOENT;
goto out_close;
}
return 0;
out_close:
conn_close(conn);
out:
return rc;
}