-
Notifications
You must be signed in to change notification settings - Fork 337
/
ddsi_config.c
2704 lines (2459 loc) · 93.2 KB
/
ddsi_config.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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright(c) 2006 to 2022 ZettaScale Technology and others
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
// v. 1.0 which is available at
// http://www.eclipse.org/org/documents/edl-v10.php.
//
// SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <math.h>
#include "dds/ddsrt/heap.h"
#include "dds/ddsrt/log.h"
#include "dds/ddsrt/md5.h"
#include "dds/ddsrt/string.h"
#include "dds/ddsrt/strtod.h"
#include "dds/ddsrt/misc.h"
#include "dds/ddsrt/environ.h"
#include "dds/ddsrt/avl.h"
#include "dds/ddsrt/xmlparser.h"
#include "dds/ddsrt/io.h"
#include "dds/ddsi/ddsi_log.h"
#include "dds/ddsi/ddsi_unused.h"
#include "ddsi__config_impl.h"
#include "ddsi__misc.h"
#include "ddsi__addrset.h"
#include "dds/config.h"
#define MAX_PATH_DEPTH 10 /* max nesting level of configuration elements */
struct cfgelem;
struct ddsi_cfgst;
enum update_result {
URES_SUCCESS, /* value processed successfully */
URES_ERROR, /* invalid value, reject configuration */
URES_SKIP_ELEMENT /* entire subtree should be ignored */
};
typedef int (*init_fun_t) (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem);
typedef enum update_result (*update_fun_t) (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, int first, const char *value);
typedef void (*free_fun_t) (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem);
typedef void (*print_fun_t) (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, uint32_t sources);
struct unit {
const char *name;
int64_t multiplier;
};
struct cfgelem {
const char *name;
const struct cfgelem *children;
const struct cfgelem *attributes;
int multiplicity;
const char *defvalue; /* NULL -> no default */
int relative_offset;
int elem_offset;
init_fun_t init;
update_fun_t update;
free_fun_t free;
print_fun_t print;
};
struct ddsi_cfgst_nodekey {
const struct cfgelem *e;
void *p;
};
struct ddsi_cfgst_node {
ddsrt_avl_node_t avlnode;
struct ddsi_cfgst_nodekey key;
int count;
uint32_t sources;
int failed;
};
enum implicit_toplevel {
ITL_DISALLOWED = -1,
ITL_ALLOWED = 0,
ITL_INSERTED_1 = 1,
ITL_INSERTED_2 = 2
};
struct ddsi_cfgst {
ddsrt_avl_tree_t found;
struct ddsi_config *cfg;
const struct ddsrt_log_cfg *logcfg; /* for LOG_LC_CONFIG */
/* error flag set so that we can continue parsing for some errors and still fail properly */
int error;
/* Whether this is the first element in this source: used to reject about configurations
where the deprecated Domain/Id element is used but is set after some other things
have been set. The underlying reason being that the configuration handling can't
backtrack and so needs to reject a configuration for an unrelated domain before
anything is set.
The new form, where the id is an attribute of the Domain element, avoids this
by guaranteeing that the id comes first. It seems most likely that the Domain/Id
was either not set or at the start of the file and that this is therefore good
enough. */
bool first_data_in_source;
/* Whether inserting an implicit CycloneDDS is allowed (for making it easier to hack
a configuration through the environment variables, and if allowed, whether it has
been inserted */
enum implicit_toplevel implicit_toplevel;
/* Whether unique prefix matching on a name is allowed (again for environment
variables) */
bool partial_match_allowed;
/* current input, mask with 1 bit set */
uint32_t source;
/* ~ current input and line number */
char *input;
int line;
/* path_depth, isattr and path together control the formatting of
error messages by cfg_error() */
int path_depth;
int isattr[MAX_PATH_DEPTH];
const struct cfgelem *path[MAX_PATH_DEPTH];
void *parent[MAX_PATH_DEPTH];
};
static int cfgst_node_cmp(const void *va, const void *vb);
static const ddsrt_avl_treedef_t cfgst_found_treedef =
DDSRT_AVL_TREEDEF_INITIALIZER(offsetof(struct ddsi_cfgst_node, avlnode), offsetof(struct ddsi_cfgst_node, key), cfgst_node_cmp, 0);
#define DU(fname) static enum update_result uf_##fname (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, int first, const char *value)
#define PF(fname) static void pf_##fname (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, uint32_t sources)
#define DUPF(fname) DU(fname) ; PF(fname)
DUPF(nop);
DUPF(networkAddress);
DUPF(networkAddresses);
DU(ipv4);
DUPF(allow_multicast);
DUPF(boolean);
DU(boolean_default);
PF(boolean_default);
DUPF(string);
DU(tracingOutputFileName);
DU(verbosity);
DUPF(tracemask);
DUPF(xcheck);
DUPF(int);
DUPF(uint);
#if 0
DUPF(int32);
DUPF(uint32);
#endif
DU(natint);
DU(natint_255);
DU(pos_uint);
DUPF(participantIndex);
DU(dyn_port);
DUPF(memsize);
DUPF(memsize16);
DU(duration_inf);
DU(duration_ms_1hr);
DU(duration_ms_1s);
DU(duration_us_1s);
DU(duration_100ms_1hr);
DU(nop_duration_ms_1hr);
DU(maybe_duration_ms_1hr);
PF(duration);
PF(maybe_duration);
DUPF(standards_conformance);
DUPF(besmode);
DUPF(retransmit_merging);
DUPF(sched_class);
DUPF(random_seed);
DUPF(entity_naming_mode);
DUPF(maybe_memsize);
DUPF(maybe_int32);
DUPF(domainId);
DUPF(transport_selector);
DUPF(many_sockets_mode);
DU(deaf_mute);
#ifdef DDS_HAS_TCP_TLS
DUPF(min_tls_version);
#endif
DUPF(shm_loglevel);
#undef DUPF
#undef DU
#undef PF
#define DF(fname) static void fname (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
DF(ff_free);
DF(ff_networkAddresses);
#undef DF
#define DI(fname) static int fname (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
#ifdef DDS_HAS_NETWORK_PARTITIONS
DI(if_network_partition);
DI(if_ignored_partition);
DI(if_partition_mapping);
#endif
DI(if_network_interfaces);
DI(if_psmx);
DI(if_peer);
DI(if_thread_properties);
#ifdef DDS_HAS_SECURITY
DI(if_omg_security);
#endif
#undef DI
/* drop extra information, i.e. DESCRIPTION, RANGE, UNIT and VALUES */
#define ELEMENT( \
name, elems, attrs, multip, dflt, \
relofst, elemofst, \
init, update, free, print, ...) \
{ \
name, elems, attrs, multip, dflt, \
relofst, elemofst, \
init, update, free, print \
}
#define DEPRECATED(name) "|" name
#define MEMBER(name) 0, ((int) offsetof (struct ddsi_config, name))
#define MEMBEROF(parent, name) 1, ((int) offsetof (struct parent, name))
#define FUNCTIONS(init, update, free, print) init, update, free, print
#define DESCRIPTION(...) /* drop */
#define RANGE(...) /* drop */
#define UNIT(...) /* drop */
#define VALUES(...) /* drop */
#define MAXIMUM(...) /* drop */
#define MINIMUM(...) /* drop */
#define NOMEMBER 0, 0
#define NOFUNCTIONS 0, 0, 0, 0
#define NODATA 1, NULL, NOMEMBER, NOFUNCTIONS
#define END_MARKER { NULL, NULL, NULL, NODATA }
/* MOVED: whereto must be a path starting with CycloneDDS, may not be used in/for lists and only for elements, may not be chained */
#define MOVED(name, whereto) \
{ ">" name, NULL, NULL, 0, whereto, NOMEMBER, NOFUNCTIONS }
#define NOP(name) \
{ name, NULL, NULL, 1, NULL, NOMEMBER, 0, uf_nop, 0, pf_nop }
#define WILDCARD { "*", NULL, NULL, NODATA }
/* Visual Studio requires indirect expansion */
#define EXPAND(macro, args) macro args
#define BOOL(name, attrs, multip, dflt, ...) \
EXPAND(ELEMENT, (name, NULL, attrs, multip, dflt, __VA_ARGS__))
#define INT(name, attrs, multip, dflt, ...) \
EXPAND(ELEMENT, (name, NULL, attrs, multip, dflt, __VA_ARGS__))
#define STRING(name, attrs, multip, dflt, ...) \
EXPAND(ELEMENT, (name, NULL, attrs, multip, dflt, __VA_ARGS__))
#define ENUM(name, attrs, multip, dflt, ...) \
EXPAND(ELEMENT, (name, NULL, attrs, multip, dflt, __VA_ARGS__))
#define LIST(name, attrs, multip, dflt, ...) \
EXPAND(ELEMENT, (name, NULL, attrs, multip, dflt, __VA_ARGS__))
#define GROUP(name, elems, attrs, multip, ...) \
EXPAND(ELEMENT, (name, elems, attrs, multip, NULL, __VA_ARGS__))
#include "ddsi__cfgelems.h"
static const struct cfgelem root_cfgelem = {
"/", cyclonedds_root_cfgelems, NULL, NODATA
};
#undef DEPRECATED
#undef MEMBER
#undef MEMBEROF
#undef FUNCTIONS
#undef DESCRIPTION
#undef RANGE
#undef UNIT
#undef VALUES
#undef MAXIMUM
#undef MINIMUM
#undef NOMEMBER
#undef NOFUNCTIONS
#undef NODATA
#undef END_MARKER
#undef MOVED
#undef NOP
#undef WILDCARD
#undef EXPAND
#undef BOOL
#undef INT
#undef STRING
#undef ENUM
#undef LIST
#undef GROUP
#undef ELEMENT
static const struct unit unittab_duration[] = {
{ "ns", 1 },
{ "us", DDS_USECS (1) },
{ "ms", DDS_MSECS (1) },
{ "s", DDS_SECS (1) },
{ "min", DDS_SECS (60) },
{ "hr", DDS_SECS (3600) },
{ "day", DDS_SECS (24 * 3600) },
{ NULL, 0 }
};
/* note: order affects whether printed as KiB or kB, for consistency
with bandwidths and pedanticness, favour KiB. */
static const struct unit unittab_memsize[] = {
{ "B", 1 },
{ "KiB", 1024 },
{ "kB", 1024 },
{ "MiB", 1048576 },
{ "MB", 1048576 },
{ "GiB", 1073741824 },
{ "GB", 1073741824 },
{ NULL, 0 }
};
static void free_configured_elements (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem);
static void free_configured_element (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem);
static const struct cfgelem *lookup_element (const char *target, bool *isattr);
static enum update_result cfg_error (struct ddsi_cfgst *cfgst, const char *fmt, ...) ddsrt_attribute_format_printf(2, 3);
static bool cfgst_push_maybe_reservespace (struct ddsi_cfgst *cfgst, int isattr, const struct cfgelem *elem, void *parent, bool allow_reservespace)
{
assert(isattr == 0 || isattr == 1);
if (cfgst->path_depth >= MAX_PATH_DEPTH - (allow_reservespace ? 0 : 1))
{
cfg_error (cfgst, "XML too deeply nested");
return false;
}
cfgst->isattr[cfgst->path_depth] = isattr;
cfgst->path[cfgst->path_depth] = elem;
cfgst->parent[cfgst->path_depth] = parent;
cfgst->path_depth++;
return true;
}
static bool cfgst_push (struct ddsi_cfgst *cfgst, int isattr, const struct cfgelem *elem, void *parent)
ddsrt_attribute_warn_unused_result;
static bool cfgst_push (struct ddsi_cfgst *cfgst, int isattr, const struct cfgelem *elem, void *parent)
{
return cfgst_push_maybe_reservespace (cfgst, isattr, elem, parent, false);
}
static void cfgst_push_nofail (struct ddsi_cfgst *cfgst, int isattr, const struct cfgelem *elem, void *parent)
{
bool ok = cfgst_push_maybe_reservespace (cfgst, isattr, elem, parent, false);
assert (ok);
(void) ok;
}
static void cfgst_push_errorhandling (struct ddsi_cfgst *cfgst, int isattr, const struct cfgelem *elem, void *parent)
{
bool ok = cfgst_push_maybe_reservespace (cfgst, isattr, elem, parent, true);
assert (ok);
(void) ok;
}
static void cfgst_pop (struct ddsi_cfgst *cfgst)
{
assert(cfgst->path_depth > 0);
cfgst->path_depth--;
}
static const struct cfgelem *cfgst_tos_w_isattr (const struct ddsi_cfgst *cfgst, bool *isattr)
{
assert(cfgst->path_depth > 0);
*isattr = cfgst->isattr[cfgst->path_depth - 1];
return cfgst->path[cfgst->path_depth - 1];
}
static const struct cfgelem *cfgst_tos (const struct ddsi_cfgst *cfgst)
{
bool dummy_isattr;
return cfgst_tos_w_isattr (cfgst, &dummy_isattr);
}
static void *cfgst_parent (const struct ddsi_cfgst *cfgst)
{
assert(cfgst->path_depth > 0);
return cfgst->parent[cfgst->path_depth - 1];
}
struct cfg_note_buf {
size_t bufpos;
size_t bufsize;
char *buf;
};
static size_t cfg_note_vsnprintf (struct cfg_note_buf *bb, const char *fmt, va_list ap)
{
int x;
x = vsnprintf(bb->buf + bb->bufpos, bb->bufsize - bb->bufpos, fmt, ap);
if (x >= 0 && (size_t) x >= bb->bufsize - bb->bufpos)
{
size_t nbufsize = ((bb->bufsize + (size_t) x + 1) + 1023) & (size_t) (-1024);
char *nbuf = ddsrt_realloc (bb->buf, nbufsize);
bb->buf = nbuf;
bb->bufsize = nbufsize;
return nbufsize;
}
if (x < 0)
DDS_FATAL("cfg_note_vsnprintf: vsnprintf failed\n");
else
bb->bufpos += (size_t) x;
return 0;
}
static void cfg_note_snprintf (struct cfg_note_buf *bb, const char *fmt, ...) ddsrt_attribute_format ((printf, 2, 3));
static void cfg_note_snprintf (struct cfg_note_buf *bb, const char *fmt, ...)
{
/* The reason the 2nd call to os_vsnprintf is here and not inside
cfg_note_vsnprintf is because I somehow doubt that all platforms
implement va_copy() */
va_list ap;
size_t r;
va_start (ap, fmt);
r = cfg_note_vsnprintf (bb, fmt, ap);
va_end (ap);
if (r > 0) {
int s;
va_start (ap, fmt);
s = vsnprintf (bb->buf + bb->bufpos, bb->bufsize - bb->bufpos, fmt, ap);
if (s < 0 || (size_t) s >= bb->bufsize - bb->bufpos)
DDS_FATAL ("cfg_note_snprintf: vsnprintf failed\n");
va_end (ap);
bb->bufpos += (size_t) s;
}
}
static size_t cfg_note (struct ddsi_cfgst *cfgst, uint32_t cat, size_t bsz, const char *fmt, const char *suffix, va_list ap)
{
/* Have to snprintf our way to a single string so we can OS_REPORT
as well as ddsi_log. Otherwise configuration errors will be lost
completely on platforms where stderr doesn't actually work for
outputting error messages (this includes Windows because of the
way "ospl start" does its thing). */
struct cfg_note_buf bb;
int i, sidx;
size_t r;
if (cat & DDS_LC_ERROR)
cfgst->error = 1;
bb.bufpos = 0;
bb.bufsize = (bsz == 0) ? 1024 : bsz;
if ((bb.buf = ddsrt_malloc(bb.bufsize)) == NULL)
DDS_FATAL ("cfg_note: out of memory\n");
cfg_note_snprintf (&bb, "config: ");
/* Path to element/attribute causing the error. Have to stop once an
attribute is reached: a NULL marker may have been pushed onto the
stack afterward in the default handling. */
sidx = 0;
while (sidx < cfgst->path_depth && cfgst->path[sidx]->name == NULL)
sidx++;
const struct cfgelem *prev_path = NULL;
for (i = sidx; i < cfgst->path_depth && (i == sidx || !cfgst->isattr[i - 1]); i++)
{
if (cfgst->path[i] == NULL)
{
assert(i > sidx);
cfg_note_snprintf (&bb, "/#text");
}
else if (cfgst->isattr[i])
{
cfg_note_snprintf (&bb, "[@%s]", cfgst->path[i]->name);
}
else if (cfgst->path[i] == prev_path)
{
/* skip printing this level: it means a group contained an element indicating that
it was moved to the first group (i.e., stripping a level) -- this is currently
only used for stripping out the DDSI2E level, and the sole purpose of this
special case is making any warnings from elements contained within it look
reasonable by always printing the new location */
}
else
{
/* first character is '>' means it was moved, so print what follows instead */
const char *name = cfgst->path[i]->name + ((cfgst->path[i]->name[0] == '>') ? 1 : 0);
const char *p = strchr (name, '|');
int n = p ? (int) (p - name) : (int) strlen(name);
cfg_note_snprintf (&bb, "%s%*.*s", (i == sidx) ? "" : "/", n, n, name);
}
prev_path = cfgst->path[i];
}
cfg_note_snprintf (&bb, ": ");
if ((r = cfg_note_vsnprintf (&bb, fmt, ap)) > 0)
{
/* Can't reset ap ... and va_copy isn't widely available - so
instead abort and hope the caller tries again with a larger
initial buffer */
ddsrt_free (bb.buf);
return r;
}
cfg_note_snprintf (&bb, "%s", suffix);
if (cat & (DDS_LC_WARNING | DDS_LC_ERROR))
{
if (cfgst->input == NULL)
cfg_note_snprintf (&bb, " (line %d)", cfgst->line);
else
{
cfg_note_snprintf (&bb, " (%s line %d)", cfgst->input, cfgst->line);
cfgst->input = NULL;
}
}
if (cfgst->logcfg)
DDS_CLOG (cat, cfgst->logcfg, "%s\n", bb.buf);
else
DDS_ILOG (cat, cfgst->cfg->domainId, "%s\n", bb.buf);
ddsrt_free (bb.buf);
return 0;
}
static void cfg_warning (struct ddsi_cfgst *cfgst, const char *fmt, ...) ddsrt_attribute_format_printf(2, 3);
static void cfg_warning (struct ddsi_cfgst *cfgst, const char *fmt, ...)
{
va_list ap;
size_t bsz = 0;
do {
va_start (ap, fmt);
bsz = cfg_note (cfgst, DDS_LC_WARNING, bsz, fmt, "", ap);
va_end (ap);
} while (bsz > 0);
}
static enum update_result cfg_error (struct ddsi_cfgst *cfgst, const char *fmt, ...)
{
va_list ap;
size_t bsz = 0;
do {
va_start (ap, fmt);
bsz = cfg_note (cfgst, DDS_LC_ERROR, bsz, fmt, "", ap);
va_end (ap);
} while (bsz > 0);
return URES_ERROR;
}
static void cfg_logelem (struct ddsi_cfgst *cfgst, uint32_t sources, const char *fmt, ...) ddsrt_attribute_format_printf(3, 4);
static void cfg_logelem (struct ddsi_cfgst *cfgst, uint32_t sources, const char *fmt, ...)
{
/* 89 = 1 + 2 + 31 + 1 + 10 + 2*22: the number of characters in
a string formed by concatenating all numbers from 0 .. 31 in decimal notation,
31 separators, a opening and closing brace pair, a terminating 0, and a leading
space */
char srcinfo[89];
va_list ap;
size_t bsz = 0;
srcinfo[0] = ' ';
srcinfo[1] = '{';
int pos = 2;
for (uint32_t i = 0, m = 1; i < 32; i++, m <<= 1)
if (sources & m)
pos += snprintf (srcinfo + pos, sizeof (srcinfo) - (size_t) pos, "%s%"PRIu32, (pos == 2) ? "" : ",", i);
srcinfo[pos] = '}';
srcinfo[pos + 1] = 0;
assert ((size_t) pos <= sizeof (srcinfo) - 2);
do {
va_start (ap, fmt);
bsz = cfg_note (cfgst, DDS_LC_CONFIG, bsz, fmt, srcinfo, ap);
va_end (ap);
} while (bsz > 0);
}
static int list_index (const char *list[], const char *elem)
{
for (int i = 0; list[i] != NULL; i++)
if (ddsrt_strcasecmp (list[i], elem) == 0)
return i;
return -1;
}
static int64_t lookup_multiplier (struct ddsi_cfgst *cfgst, const struct unit *unittab, const char *value, int unit_pos, int value_is_zero, int64_t def_mult, int err_on_unrecognised)
ddsrt_nonnull((1, 3));
static int64_t lookup_multiplier (struct ddsi_cfgst *cfgst, const struct unit *unittab, const char *value, int unit_pos, int value_is_zero, int64_t def_mult, int err_on_unrecognised)
{
assert (0 <= unit_pos && (size_t) unit_pos <= strlen(value));
while (value[unit_pos] == ' ')
unit_pos++;
if (unittab == NULL)
{
if (value[unit_pos] != 0) {
(void) cfg_error (cfgst, "%s: no unit expected", value);
return 0;
}
return def_mult;
}
else if (value[unit_pos] == 0)
{
if (value_is_zero) {
/* No matter what unit, 0 remains just that. For convenience,
always allow 0 to be specified without a unit */
return 1;
} else if (def_mult == 0 && err_on_unrecognised) {
(void) cfg_error (cfgst, "%s: unit is required", value);
return 0;
} else {
cfg_warning (cfgst, "%s: use of default unit is deprecated", value);
return def_mult;
}
}
else
{
for (int i = 0; unittab[i].name != NULL; i++)
if (strcmp(unittab[i].name, value + unit_pos) == 0)
return unittab[i].multiplier;
if (err_on_unrecognised)
(void) cfg_error(cfgst, "%s: unrecognised unit", value + unit_pos);
return 0;
}
}
static void *cfg_address (UNUSED_ARG (struct ddsi_cfgst *cfgst), void *parent, struct cfgelem const * const cfgelem)
{
assert (cfgelem->multiplicity <= 1);
return (char *) parent + cfgelem->elem_offset;
}
static struct ddsi_config_listelem **cfg_list_address (UNUSED_ARG (struct ddsi_cfgst *cfgst), void *parent, struct cfgelem const * const cfgelem)
{
assert (cfgelem->multiplicity > 1);
return ((struct ddsi_config_listelem **) ((char *) parent + cfgelem->elem_offset));
}
static void *cfg_deref_address (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
assert (cfgelem->multiplicity > 1);
return (void *) *cfg_list_address (cfgst, parent, cfgelem);
}
static void *if_common (UNUSED_ARG (struct ddsi_cfgst *cfgst), void *parent, struct cfgelem const * const cfgelem, unsigned size)
{
struct ddsi_config_listelem **current = (struct ddsi_config_listelem **) ((char *) parent + cfgelem->elem_offset);
struct ddsi_config_listelem *new = ddsrt_malloc (size);
memset (new, 0, size);
new->next = *current;
*current = new;
return new;
}
static int if_thread_properties (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
struct ddsi_config_thread_properties_listelem *new = if_common (cfgst, parent, cfgelem, sizeof(*new));
if (new == NULL)
return -1;
new->name = NULL;
return 0;
}
static int if_network_interfaces(struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
struct ddsi_config_network_interface_listelem *new = if_common (cfgst, parent, cfgelem, sizeof(*new));
if (new == NULL)
return -1;
new->cfg.name = NULL;
new->cfg.address = NULL;
return 0;
}
static int if_psmx(struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
struct ddsi_config_psmx_listelem *new = if_common (cfgst, parent, cfgelem, sizeof(*new));
if (new == NULL)
return -1;
new->cfg.name = NULL;
new->cfg.library = NULL;
new->cfg.config = NULL;
return 0;
}
#ifdef DDS_HAS_NETWORK_PARTITIONS
static int if_network_partition (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
struct ddsi_config_networkpartition_listelem *new = if_common (cfgst, parent, cfgelem, sizeof(*new));
if (new == NULL)
return -1;
new->address_string = NULL;
new->interface_names = NULL;
new->uc_addresses = NULL;
new->asm_addresses = NULL;
#ifdef DDS_HAS_SSM
new->ssm_addresses = NULL;
#endif
new->name = NULL;
return 0;
}
static int if_ignored_partition (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
struct ddsi_config_ignoredpartition_listelem *new = if_common (cfgst, parent, cfgelem, sizeof(*new));
if (new == NULL)
return -1;
new->DCPSPartitionTopic = NULL;
return 0;
}
static int if_partition_mapping (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
struct ddsi_config_partitionmapping_listelem *new = if_common (cfgst, parent, cfgelem, sizeof(*new));
if (new == NULL)
return -1;
new->DCPSPartitionTopic = NULL;
new->networkPartition = NULL;
new->partition = NULL;
return 0;
}
#endif /* DDS_HAS_NETWORK_PARTITIONS */
static int if_peer (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
struct ddsi_config_peer_listelem *new = if_common (cfgst, parent, cfgelem, sizeof (*new));
if (new == NULL)
return -1;
new->peer = NULL;
return 0;
}
#ifdef DDS_HAS_SECURITY
static int if_omg_security (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
struct ddsi_config_omg_security_listelem *new = if_common (cfgst, parent, cfgelem, sizeof (*new));
if (new == NULL)
return -1;
memset(&new->cfg, 0, sizeof(new->cfg));
return 0;
}
#endif
static void ff_free (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem)
{
void ** const elem = cfg_address (cfgst, parent, cfgelem);
ddsrt_free (*elem);
}
static enum update_result uf_nop (UNUSED_ARG (struct ddsi_cfgst *cfgst), UNUSED_ARG (void *parent), UNUSED_ARG (struct cfgelem const * const cfgelem), UNUSED_ARG (int first), UNUSED_ARG (const char *value))
{
return URES_SUCCESS;
}
static void pf_nop (UNUSED_ARG (struct ddsi_cfgst *cfgst), UNUSED_ARG (void *parent), UNUSED_ARG (struct cfgelem const * const cfgelem), UNUSED_ARG (uint32_t sources))
{
}
static enum update_result do_uint32_bitset (struct ddsi_cfgst *cfgst, uint32_t *cats, const char **names, const uint32_t *codes, const char *value)
{
char *copy = ddsrt_strdup (value), *cursor = copy, *tok;
while ((tok = ddsrt_strsep (&cursor, ",")) != NULL)
{
const int idx = list_index (names, tok[0] == '-' ? tok+1 : tok);
if (idx < 0)
{
const enum update_result ret = cfg_error (cfgst, "'%s' in '%s' undefined", tok, value);
ddsrt_free (copy);
return ret;
}
if (tok[0] == '-')
*cats &= ~codes[idx];
else
*cats |= codes[idx];
}
ddsrt_free (copy);
return URES_SUCCESS;
}
static unsigned uint32_popcnt (uint32_t x)
{
unsigned n = 0;
while (x != 0)
{
n += ((x & 1u) != 0);
x >>= 1;
}
return n;
}
static void do_print_uint32_bitset (struct ddsi_cfgst *cfgst, uint32_t mask, size_t ncodes, const char **names, const uint32_t *codes, uint32_t sources, const char *suffix)
{
char res[256] = "";
size_t res_offset = 0;
const char *prefix = "";
#ifndef NDEBUG
{
size_t max = 0;
for (size_t i = 0; i < ncodes; i++)
max += 1 + strlen (names[i]);
max += 11; /* ,0x%x */
max += 1; /* \0 */
assert (max <= sizeof (res));
}
#endif
while (mask)
{
size_t i_best = 0;
unsigned pc_best = 0;
for (size_t i = 0; i < ncodes; i++)
{
uint32_t m = mask & codes[i];
if (m == codes[i])
{
unsigned pc = uint32_popcnt (m);
if (pc > pc_best)
{
i_best = i;
pc_best = pc;
}
}
}
if (pc_best != 0)
{
int characters_written = snprintf (res + res_offset, sizeof(res) - res_offset, "%s%s", prefix, names[i_best]);
assert(characters_written >= 0);
res_offset += (size_t) characters_written;
mask &= ~codes[i_best];
prefix = ",";
}
else
{
int characters_written = snprintf (res + res_offset, sizeof(res) - res_offset, "%s0x%x", prefix, (unsigned) mask);
assert(characters_written >= 0);
res_offset += (size_t) characters_written;
mask = 0;
}
}
assert (res_offset <= sizeof (res));
cfg_logelem (cfgst, sources, "%s%s", res, suffix);
}
static enum update_result uf_int64_unit (struct ddsi_cfgst *cfgst, int64_t *elem, const char *value, const struct unit *unittab, int64_t def_mult, int64_t min, int64_t max)
ddsrt_nonnull ((1, 2, 3));
static enum update_result uf_int64_unit (struct ddsi_cfgst *cfgst, int64_t *elem, const char *value, const struct unit *unittab, int64_t def_mult, int64_t min, int64_t max)
{
DDSRT_WARNING_MSVC_OFF(4996);
int pos;
double v_dbl;
int64_t v_int;
int64_t mult;
// Some static analyzers don't "get it" and will report false positives on an
// uninitialized value for *elem when returning cfg_error(...)
*elem = 0;
/* try convert as integer + optional unit; if that fails, try
floating point + optional unit (round, not truncate, to integer) */
if (*value == 0) {
return cfg_error(cfgst, "%s: empty string is not a valid value", value);
} else if (sscanf (value, "%" SCNd64 "%n", &v_int, &pos) == 1 && (mult = lookup_multiplier (cfgst, unittab, value, pos, v_int == 0, def_mult, 0)) != 0) {
assert(mult > 0);
if (v_int < INT64_MIN / mult || v_int > INT64_MAX / mult)
return cfg_error (cfgst, "%s: value out of range", value);
const int64_t v_int_scaled = v_int * mult;
if (v_int_scaled < min || v_int_scaled > max)
return cfg_error (cfgst, "%s: value out of range", value);
*elem = v_int_scaled;
return URES_SUCCESS;
} else if (sscanf (value, "%lf%n", &v_dbl, &pos) == 1 && (mult = lookup_multiplier (cfgst, unittab, value, pos, v_dbl == 0, def_mult, 1)) != 0) {
double dmult = (double) mult;
assert (dmult > 0);
// avoid needing something llround(), make it positive, scale it and
// add 0.5 to get decent rounding behaviour
const double v_dbl_abs_scaled = ((v_dbl >= 0) ? 1.0 : -1.0) * v_dbl * dmult + 0.5;
// - C99, therefore 2's complement by 7.18.1.1
// - INT64_MIN = 0x8000...0 is exactly representable
// - INT64_MAX = 0x7fff...f is not exactly representable and rounds up
// v_dbl_abs_scaled is >= 0 and with -INT64_MAX > INT64_MIN, checking
// it is strictly less than ((double) INT64_MAX) guarantees we can
// convert to int64_t and negate it if the input is negative without
// risking signed overflow.
//
// Only accepting the value if A < B works to reject out-of-range and
// NaNs. Rewriting as (A >= B) and swapping the two branches is not
// the same thing because NaNs always compare false.
if (v_dbl_abs_scaled < (double) INT64_MAX) {
const int64_t v_int_scaled = ((v_dbl >= 0) ? 1 : -1) * (int64_t) v_dbl_abs_scaled;
if (v_int_scaled < min || v_int_scaled > max)
return cfg_error (cfgst, "%s: value out of range", value);
*elem = v_int_scaled;
return URES_SUCCESS;
} else {
return cfg_error(cfgst, "%s: value out of range", value);
}
} else {
*elem = 0; /* some static analyzers don't "get it" */
return cfg_error (cfgst, "%s: invalid value", value);
}
DDSRT_WARNING_MSVC_ON(4996);
}
static void pf_int64_unit (struct ddsi_cfgst *cfgst, int64_t value, uint32_t sources, const struct unit *unittab, const char *zero_unit)
{
if (value == 0) {
/* 0s is a bit of a special case: we don't want to print 0hr (or
whatever unit will have the greatest multiplier), so hard-code
as 0s */
cfg_logelem (cfgst, sources, "0 %s", zero_unit);
} else {
int64_t m = 0;
const char *unit = NULL;
int i;
for (i = 0; unittab[i].name != NULL; i++)
{
if (unittab[i].multiplier > m && (value % unittab[i].multiplier) == 0)
{
m = unittab[i].multiplier;
unit = unittab[i].name;
}
}
assert (m > 0);
assert (unit != NULL);
cfg_logelem (cfgst, sources, "%"PRId64" %s", value / m, unit);
}
}
#define GENERIC_ENUM_CTYPE_UF(type_, c_type_) \
DDSRT_STATIC_ASSERT (sizeof (en_##type_##_vs) / sizeof (*en_##type_##_vs) == \
sizeof (en_##type_##_ms) / sizeof (*en_##type_##_ms)); \
\
static enum update_result uf_##type_ (struct ddsi_cfgst *cfgst, void *parent, UNUSED_ARG (struct cfgelem const * const cfgelem), UNUSED_ARG (int first), const char *value) \
{ \
const int idx = list_index (en_##type_##_vs, value); \
c_type_ * const elem = cfg_address (cfgst, parent, cfgelem); \
/* idx >= length of ms check is to shut up clang's static analyzer */ \
if (idx < 0 || idx >= (int) (sizeof (en_##type_##_ms) / sizeof (en_##type_##_ms[0]))) \
return cfg_error (cfgst, "'%s': undefined value", value); \
*elem = en_##type_##_ms[idx]; \
return URES_SUCCESS; \
}
#define GENERIC_ENUM_CTYPE_PF(type_, c_type_) \
static void pf_##type_ (struct ddsi_cfgst *cfgst, void *parent, struct cfgelem const * const cfgelem, uint32_t sources) \
{ \
c_type_ const * const p = cfg_address (cfgst, parent, cfgelem); \
const char *str = "INVALID"; \
/* i < length of ms check is to shut up clang's static analyzer */ \
for (int i = 0; en_##type_##_vs[i] != NULL && i < (int) (sizeof (en_##type_##_ms) / sizeof (en_##type_##_ms[0])); i++) { \
if (en_##type_##_ms[i] == *p) \
{ \
str = en_##type_##_vs[i]; \
break; \
} \
} \
cfg_logelem(cfgst, sources, "%s", str); \
}
#define GENERIC_ENUM_CTYPE(type_, c_type_) \
GENERIC_ENUM_CTYPE_UF(type_, c_type_) \
GENERIC_ENUM_CTYPE_PF(type_, c_type_)
#define GENERIC_ENUM_UF(type_) GENERIC_ENUM_CTYPE_UF(type_, enum type_)
#define GENERIC_ENUM(type_) GENERIC_ENUM_CTYPE(type_, enum type_)
static const char *en_boolean_vs[] = { "false", "true", NULL };
static const int en_boolean_ms[] = { 0, 1, 0 };
GENERIC_ENUM_CTYPE (boolean, int)
static const char *en_boolean_default_vs[] = { "default", "false", "true", NULL };
static const enum ddsi_boolean_default en_boolean_default_ms[] = { DDSI_BOOLDEF_DEFAULT, DDSI_BOOLDEF_FALSE, DDSI_BOOLDEF_TRUE, 0 };
GENERIC_ENUM_CTYPE (boolean_default, enum ddsi_boolean_default)
static const char *en_besmode_vs[] = { "full", "writers", "minimal", NULL };
static const enum ddsi_besmode en_besmode_ms[] = { DDSI_BESMODE_FULL, DDSI_BESMODE_WRITERS, DDSI_BESMODE_MINIMAL, 0 };
GENERIC_ENUM_CTYPE (besmode, enum ddsi_besmode)
static const char *en_retransmit_merging_vs[] = { "never", "adaptive", "always", NULL };
static const enum ddsi_retransmit_merging en_retransmit_merging_ms[] = { DDSI_REXMIT_MERGE_NEVER, DDSI_REXMIT_MERGE_ADAPTIVE, DDSI_REXMIT_MERGE_ALWAYS, 0 };
GENERIC_ENUM_CTYPE (retransmit_merging, enum ddsi_retransmit_merging)
static const char *en_sched_class_vs[] = { "realtime", "timeshare", "default", NULL };
static const ddsrt_sched_t en_sched_class_ms[] = { DDSRT_SCHED_REALTIME, DDSRT_SCHED_TIMESHARE, DDSRT_SCHED_DEFAULT, 0 };
GENERIC_ENUM_CTYPE (sched_class, ddsrt_sched_t)
static const char *en_transport_selector_vs[] = { "default", "udp", "udp6", "tcp", "tcp6", "raweth", "none", NULL };
static const enum ddsi_transport_selector en_transport_selector_ms[] = { DDSI_TRANS_DEFAULT, DDSI_TRANS_UDP, DDSI_TRANS_UDP6, DDSI_TRANS_TCP, DDSI_TRANS_TCP6, DDSI_TRANS_RAWETH, DDSI_TRANS_NONE, 0 };
GENERIC_ENUM_CTYPE (transport_selector, enum ddsi_transport_selector)
/* by putting the "true" and "false" aliases at the end, they won't come out of the
generic printing function */
static const char *en_many_sockets_mode_vs[] = { "single", "none", "many", "false", "true", NULL };
static const enum ddsi_many_sockets_mode en_many_sockets_mode_ms[] = {
DDSI_MSM_SINGLE_UNICAST, DDSI_MSM_NO_UNICAST, DDSI_MSM_MANY_UNICAST, DDSI_MSM_SINGLE_UNICAST, DDSI_MSM_MANY_UNICAST, 0 };
GENERIC_ENUM_CTYPE (many_sockets_mode, enum ddsi_many_sockets_mode)
static const char *en_standards_conformance_vs[] = { "pedantic", "strict", "lax", NULL };
static const enum ddsi_standards_conformance en_standards_conformance_ms[] = { DDSI_SC_PEDANTIC, DDSI_SC_STRICT, DDSI_SC_LAX, 0 };
GENERIC_ENUM_CTYPE (standards_conformance, enum ddsi_standards_conformance)
static const char *en_entity_naming_mode_vs[] = { "empty", "fancy", NULL };