forked from haproxy/haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.c
5642 lines (4931 loc) · 147 KB
/
tools.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
/*
* General purpose functions.
*
* Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*/
#if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL)
#define _GNU_SOURCE
#include <dlfcn.h>
#include <link.h>
#endif
#if defined(__FreeBSD__)
#include <elf.h>
#include <dlfcn.h>
extern void *__elf_aux_vector;
#endif
#if defined(__NetBSD__)
#include <sys/exec_elf.h>
#include <dlfcn.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
#include <sys/auxv.h>
#endif
#include <import/eb32sctree.h>
#include <import/eb32tree.h>
#include <haproxy/api.h>
#include <haproxy/chunk.h>
#include <haproxy/dgram.h>
#include <haproxy/global.h>
#include <haproxy/hlua.h>
#include <haproxy/listener.h>
#include <haproxy/namespace.h>
#include <haproxy/net_helper.h>
#include <haproxy/protocol.h>
#include <haproxy/resolvers.h>
#include <haproxy/sock.h>
#include <haproxy/ssl_sock.h>
#include <haproxy/ssl_utils.h>
#include <haproxy/stream_interface.h>
#include <haproxy/task.h>
#include <haproxy/tools.h>
/* This macro returns false if the test __x is false. Many
* of the following parsing function must be abort the processing
* if it returns 0, so this macro is useful for writing light code.
*/
#define RET0_UNLESS(__x) do { if (!(__x)) return 0; } while (0)
/* enough to store NB_ITOA_STR integers of :
* 2^64-1 = 18446744073709551615 or
* -2^63 = -9223372036854775808
*
* The HTML version needs room for adding the 25 characters
* '<span class="rls"></span>' around digits at positions 3N+1 in order
* to add spacing at up to 6 positions : 18 446 744 073 709 551 615
*/
THREAD_LOCAL char itoa_str[NB_ITOA_STR][171];
THREAD_LOCAL int itoa_idx = 0; /* index of next itoa_str to use */
/* sometimes we'll need to quote strings (eg: in stats), and we don't expect
* to quote strings larger than a max configuration line.
*/
THREAD_LOCAL char quoted_str[NB_QSTR][QSTR_SIZE + 1];
THREAD_LOCAL int quoted_idx = 0;
/* thread-local PRNG state. It's modified to start from a different sequence
* on all threads upon startup. It must not be used or anything beyond getting
* statistical values as it's 100% predictable.
*/
THREAD_LOCAL unsigned int statistical_prng_state = 2463534242U;
/*
* unsigned long long ASCII representation
*
* return the last char '\0' or NULL if no enough
* space in dst
*/
char *ulltoa(unsigned long long n, char *dst, size_t size)
{
int i = 0;
char *res;
switch(n) {
case 1ULL ... 9ULL:
i = 0;
break;
case 10ULL ... 99ULL:
i = 1;
break;
case 100ULL ... 999ULL:
i = 2;
break;
case 1000ULL ... 9999ULL:
i = 3;
break;
case 10000ULL ... 99999ULL:
i = 4;
break;
case 100000ULL ... 999999ULL:
i = 5;
break;
case 1000000ULL ... 9999999ULL:
i = 6;
break;
case 10000000ULL ... 99999999ULL:
i = 7;
break;
case 100000000ULL ... 999999999ULL:
i = 8;
break;
case 1000000000ULL ... 9999999999ULL:
i = 9;
break;
case 10000000000ULL ... 99999999999ULL:
i = 10;
break;
case 100000000000ULL ... 999999999999ULL:
i = 11;
break;
case 1000000000000ULL ... 9999999999999ULL:
i = 12;
break;
case 10000000000000ULL ... 99999999999999ULL:
i = 13;
break;
case 100000000000000ULL ... 999999999999999ULL:
i = 14;
break;
case 1000000000000000ULL ... 9999999999999999ULL:
i = 15;
break;
case 10000000000000000ULL ... 99999999999999999ULL:
i = 16;
break;
case 100000000000000000ULL ... 999999999999999999ULL:
i = 17;
break;
case 1000000000000000000ULL ... 9999999999999999999ULL:
i = 18;
break;
case 10000000000000000000ULL ... ULLONG_MAX:
i = 19;
break;
}
if (i + 2 > size) // (i + 1) + '\0'
return NULL; // too long
res = dst + i + 1;
*res = '\0';
for (; i >= 0; i--) {
dst[i] = n % 10ULL + '0';
n /= 10ULL;
}
return res;
}
/*
* unsigned long ASCII representation
*
* return the last char '\0' or NULL if no enough
* space in dst
*/
char *ultoa_o(unsigned long n, char *dst, size_t size)
{
int i = 0;
char *res;
switch (n) {
case 0U ... 9UL:
i = 0;
break;
case 10U ... 99UL:
i = 1;
break;
case 100U ... 999UL:
i = 2;
break;
case 1000U ... 9999UL:
i = 3;
break;
case 10000U ... 99999UL:
i = 4;
break;
case 100000U ... 999999UL:
i = 5;
break;
case 1000000U ... 9999999UL:
i = 6;
break;
case 10000000U ... 99999999UL:
i = 7;
break;
case 100000000U ... 999999999UL:
i = 8;
break;
#if __WORDSIZE == 32
case 1000000000ULL ... ULONG_MAX:
i = 9;
break;
#elif __WORDSIZE == 64
case 1000000000ULL ... 9999999999UL:
i = 9;
break;
case 10000000000ULL ... 99999999999UL:
i = 10;
break;
case 100000000000ULL ... 999999999999UL:
i = 11;
break;
case 1000000000000ULL ... 9999999999999UL:
i = 12;
break;
case 10000000000000ULL ... 99999999999999UL:
i = 13;
break;
case 100000000000000ULL ... 999999999999999UL:
i = 14;
break;
case 1000000000000000ULL ... 9999999999999999UL:
i = 15;
break;
case 10000000000000000ULL ... 99999999999999999UL:
i = 16;
break;
case 100000000000000000ULL ... 999999999999999999UL:
i = 17;
break;
case 1000000000000000000ULL ... 9999999999999999999UL:
i = 18;
break;
case 10000000000000000000ULL ... ULONG_MAX:
i = 19;
break;
#endif
}
if (i + 2 > size) // (i + 1) + '\0'
return NULL; // too long
res = dst + i + 1;
*res = '\0';
for (; i >= 0; i--) {
dst[i] = n % 10U + '0';
n /= 10U;
}
return res;
}
/*
* signed long ASCII representation
*
* return the last char '\0' or NULL if no enough
* space in dst
*/
char *ltoa_o(long int n, char *dst, size_t size)
{
char *pos = dst;
if (n < 0) {
if (size < 3)
return NULL; // min size is '-' + digit + '\0' but another test in ultoa
*pos = '-';
pos++;
dst = ultoa_o(-n, pos, size - 1);
} else {
dst = ultoa_o(n, dst, size);
}
return dst;
}
/*
* signed long long ASCII representation
*
* return the last char '\0' or NULL if no enough
* space in dst
*/
char *lltoa(long long n, char *dst, size_t size)
{
char *pos = dst;
if (n < 0) {
if (size < 3)
return NULL; // min size is '-' + digit + '\0' but another test in ulltoa
*pos = '-';
pos++;
dst = ulltoa(-n, pos, size - 1);
} else {
dst = ulltoa(n, dst, size);
}
return dst;
}
/*
* write a ascii representation of a unsigned into dst,
* return a pointer to the last character
* Pad the ascii representation with '0', using size.
*/
char *utoa_pad(unsigned int n, char *dst, size_t size)
{
int i = 0;
char *ret;
switch(n) {
case 0U ... 9U:
i = 0;
break;
case 10U ... 99U:
i = 1;
break;
case 100U ... 999U:
i = 2;
break;
case 1000U ... 9999U:
i = 3;
break;
case 10000U ... 99999U:
i = 4;
break;
case 100000U ... 999999U:
i = 5;
break;
case 1000000U ... 9999999U:
i = 6;
break;
case 10000000U ... 99999999U:
i = 7;
break;
case 100000000U ... 999999999U:
i = 8;
break;
case 1000000000U ... 4294967295U:
i = 9;
break;
}
if (i + 2 > size) // (i + 1) + '\0'
return NULL; // too long
if (i < size)
i = size - 2; // padding - '\0'
ret = dst + i + 1;
*ret = '\0';
for (; i >= 0; i--) {
dst[i] = n % 10U + '0';
n /= 10U;
}
return ret;
}
/*
* copies at most <size-1> chars from <src> to <dst>. Last char is always
* set to 0, unless <size> is 0. The number of chars copied is returned
* (excluding the terminating zero).
* This code has been optimized for size and speed : on x86, it's 45 bytes
* long, uses only registers, and consumes only 4 cycles per char.
*/
int strlcpy2(char *dst, const char *src, int size)
{
char *orig = dst;
if (size) {
while (--size && (*dst = *src)) {
src++; dst++;
}
*dst = 0;
}
return dst - orig;
}
/*
* This function simply returns a locally allocated string containing
* the ascii representation for number 'n' in decimal.
*/
char *ultoa_r(unsigned long n, char *buffer, int size)
{
char *pos;
pos = buffer + size - 1;
*pos-- = '\0';
do {
*pos-- = '0' + n % 10;
n /= 10;
} while (n && pos >= buffer);
return pos + 1;
}
/*
* This function simply returns a locally allocated string containing
* the ascii representation for number 'n' in decimal.
*/
char *lltoa_r(long long int in, char *buffer, int size)
{
char *pos;
int neg = 0;
unsigned long long int n;
pos = buffer + size - 1;
*pos-- = '\0';
if (in < 0) {
neg = 1;
n = -in;
}
else
n = in;
do {
*pos-- = '0' + n % 10;
n /= 10;
} while (n && pos >= buffer);
if (neg && pos > buffer)
*pos-- = '-';
return pos + 1;
}
/*
* This function simply returns a locally allocated string containing
* the ascii representation for signed number 'n' in decimal.
*/
char *sltoa_r(long n, char *buffer, int size)
{
char *pos;
if (n >= 0)
return ultoa_r(n, buffer, size);
pos = ultoa_r(-n, buffer + 1, size - 1) - 1;
*pos = '-';
return pos;
}
/*
* This function simply returns a locally allocated string containing
* the ascii representation for number 'n' in decimal, formatted for
* HTML output with tags to create visual grouping by 3 digits. The
* output needs to support at least 171 characters.
*/
const char *ulltoh_r(unsigned long long n, char *buffer, int size)
{
char *start;
int digit = 0;
start = buffer + size;
*--start = '\0';
do {
if (digit == 3 && start >= buffer + 7)
memcpy(start -= 7, "</span>", 7);
if (start >= buffer + 1) {
*--start = '0' + n % 10;
n /= 10;
}
if (digit == 3 && start >= buffer + 18)
memcpy(start -= 18, "<span class=\"rls\">", 18);
if (digit++ == 3)
digit = 1;
} while (n && start > buffer);
return start;
}
/*
* This function simply returns a locally allocated string containing the ascii
* representation for number 'n' in decimal, unless n is 0 in which case it
* returns the alternate string (or an empty string if the alternate string is
* NULL). It use is intended for limits reported in reports, where it's
* desirable not to display anything if there is no limit. Warning! it shares
* the same vector as ultoa_r().
*/
const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
{
return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
}
/* Trims the first "%f" float in a string to its minimum number of digits after
* the decimal point by trimming trailing zeroes, even dropping the decimal
* point if not needed. The string is in <buffer> of length <len>, and the
* number is expected to start at or after position <num_start> (the first
* point appearing there is considered). A NUL character is always placed at
* the end if some trimming occurs. The new buffer length is returned.
*/
size_t flt_trim(char *buffer, size_t num_start, size_t len)
{
char *end = buffer + len;
char *p = buffer + num_start;
char *trim;
do {
if (p >= end)
return len;
trim = p++;
} while (*trim != '.');
/* For now <trim> is on the decimal point. Let's look for any other
* meaningful digit after it.
*/
while (p < end) {
if (*p++ != '0')
trim = p;
}
if (trim < end)
*trim = 0;
return trim - buffer;
}
/*
* This function simply returns a locally allocated string containing
* the ascii representation for number 'n' in decimal with useless trailing
* zeroes trimmed.
*/
char *ftoa_r(double n, char *buffer, int size)
{
flt_trim(buffer, 0, snprintf(buffer, size, "%f", n));
return buffer;
}
/* returns a locally allocated string containing the quoted encoding of the
* input string. The output may be truncated to QSTR_SIZE chars, but it is
* guaranteed that the string will always be properly terminated. Quotes are
* encoded by doubling them as is commonly done in CSV files. QSTR_SIZE must
* always be at least 4 chars.
*/
const char *qstr(const char *str)
{
char *ret = quoted_str[quoted_idx];
char *p, *end;
if (++quoted_idx >= NB_QSTR)
quoted_idx = 0;
p = ret;
end = ret + QSTR_SIZE;
*p++ = '"';
/* always keep 3 chars to support passing "" and the ending " */
while (*str && p < end - 3) {
if (*str == '"') {
*p++ = '"';
*p++ = '"';
}
else
*p++ = *str;
str++;
}
*p++ = '"';
return ret;
}
/*
* Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
*
* It looks like this one would be a good candidate for inlining, but this is
* not interesting because it around 35 bytes long and often called multiple
* times within the same function.
*/
int ishex(char s)
{
s -= '0';
if ((unsigned char)s <= 9)
return 1;
s -= 'A' - '0';
if ((unsigned char)s <= 5)
return 1;
s -= 'a' - 'A';
if ((unsigned char)s <= 5)
return 1;
return 0;
}
/* rounds <i> down to the closest value having max 2 digits */
unsigned int round_2dig(unsigned int i)
{
unsigned int mul = 1;
while (i >= 100) {
i /= 10;
mul *= 10;
}
return i * mul;
}
/*
* Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
* invalid character is found, a pointer to it is returned. If everything is
* fine, NULL is returned.
*/
const char *invalid_char(const char *name)
{
if (!*name)
return name;
while (*name) {
if (!isalnum((unsigned char)*name) && *name != '.' && *name != ':' &&
*name != '_' && *name != '-')
return name;
name++;
}
return NULL;
}
/*
* Checks <name> for invalid characters. Valid chars are [_.-] and those
* accepted by <f> function.
* If an invalid character is found, a pointer to it is returned.
* If everything is fine, NULL is returned.
*/
static inline const char *__invalid_char(const char *name, int (*f)(int)) {
if (!*name)
return name;
while (*name) {
if (!f((unsigned char)*name) && *name != '.' &&
*name != '_' && *name != '-')
return name;
name++;
}
return NULL;
}
/*
* Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_.-].
* If an invalid character is found, a pointer to it is returned.
* If everything is fine, NULL is returned.
*/
const char *invalid_domainchar(const char *name) {
return __invalid_char(name, isalnum);
}
/*
* Checks <name> for invalid characters. Valid chars are [A-Za-z_.-].
* If an invalid character is found, a pointer to it is returned.
* If everything is fine, NULL is returned.
*/
const char *invalid_prefix_char(const char *name) {
return __invalid_char(name, isalnum);
}
/*
* converts <str> to a struct sockaddr_storage* provided by the caller. The
* caller must have zeroed <sa> first, and may have set sa->ss_family to force
* parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then
* the function tries to guess the address family from the syntax. If the
* family is forced and the format doesn't match, an error is returned. The
* string is assumed to contain only an address, no port. The address can be a
* dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to
* indicate INADDR_ANY. NULL is returned if the host part cannot be resolved.
* The return address will only have the address family and the address set,
* all other fields remain zero. The string is not supposed to be modified.
* The IPv6 '::' address is IN6ADDR_ANY. If <resolve> is non-zero, the hostname
* is resolved, otherwise only IP addresses are resolved, and anything else
* returns NULL. If the address contains a port, this one is preserved.
*/
struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, int resolve)
{
struct hostent *he;
/* max IPv6 length, including brackets and terminating NULL */
char tmpip[48];
int port = get_host_port(sa);
/* check IPv6 with square brackets */
if (str[0] == '[') {
size_t iplength = strlen(str);
if (iplength < 4) {
/* minimal size is 4 when using brackets "[::]" */
goto fail;
}
else if (iplength >= sizeof(tmpip)) {
/* IPv6 literal can not be larger than tmpip */
goto fail;
}
else {
if (str[iplength - 1] != ']') {
/* if address started with bracket, it should end with bracket */
goto fail;
}
else {
memcpy(tmpip, str + 1, iplength - 2);
tmpip[iplength - 2] = '\0';
str = tmpip;
}
}
}
/* Any IPv6 address */
if (str[0] == ':' && str[1] == ':' && !str[2]) {
if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
sa->ss_family = AF_INET6;
else if (sa->ss_family != AF_INET6)
goto fail;
set_host_port(sa, port);
return sa;
}
/* Any address for the family, defaults to IPv4 */
if (!str[0] || (str[0] == '*' && !str[1])) {
if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
sa->ss_family = AF_INET;
set_host_port(sa, port);
return sa;
}
/* check for IPv6 first */
if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) &&
inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) {
sa->ss_family = AF_INET6;
set_host_port(sa, port);
return sa;
}
/* then check for IPv4 */
if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) &&
inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) {
sa->ss_family = AF_INET;
set_host_port(sa, port);
return sa;
}
if (!resolve)
return NULL;
if (!resolv_hostname_validation(str, NULL))
return NULL;
#ifdef USE_GETADDRINFO
if (global.tune.options & GTUNE_USE_GAI) {
struct addrinfo hints, *result;
int success = 0;
memset(&result, 0, sizeof(result));
memset(&hints, 0, sizeof(hints));
hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
if (getaddrinfo(str, NULL, &hints, &result) == 0) {
if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
sa->ss_family = result->ai_family;
else if (sa->ss_family != result->ai_family) {
freeaddrinfo(result);
goto fail;
}
switch (result->ai_family) {
case AF_INET:
memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen);
set_host_port(sa, port);
success = 1;
break;
case AF_INET6:
memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen);
set_host_port(sa, port);
success = 1;
break;
}
}
if (result)
freeaddrinfo(result);
if (success)
return sa;
}
#endif
/* try to resolve an IPv4/IPv6 hostname */
he = gethostbyname(str);
if (he) {
if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
sa->ss_family = he->h_addrtype;
else if (sa->ss_family != he->h_addrtype)
goto fail;
switch (sa->ss_family) {
case AF_INET:
((struct sockaddr_in *)sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
set_host_port(sa, port);
return sa;
case AF_INET6:
((struct sockaddr_in6 *)sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
set_host_port(sa, port);
return sa;
}
}
/* unsupported address family */
fail:
return NULL;
}
/*
* Converts <str> to a locally allocated struct sockaddr_storage *, and a port
* range or offset consisting in two integers that the caller will have to
* check to find the relevant input format. The following format are supported :
*
* String format | address | port | low | high
* addr | <addr> | 0 | 0 | 0
* addr: | <addr> | 0 | 0 | 0
* addr:port | <addr> | <port> | <port> | <port>
* addr:pl-ph | <addr> | <pl> | <pl> | <ph>
* addr:+port | <addr> | <port> | 0 | <port>
* addr:-port | <addr> |-<port> | <port> | 0
*
* The detection of a port range or increment by the caller is made by
* comparing <low> and <high>. If both are equal, then port 0 means no port
* was specified. The caller may pass NULL for <low> and <high> if it is not
* interested in retrieving port ranges.
*
* Note that <addr> above may also be :
* - empty ("") => family will be AF_INET and address will be INADDR_ANY
* - "*" => family will be AF_INET and address will be INADDR_ANY
* - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY
* - a host name => family and address will depend on host name resolving.
*
* A prefix may be passed in before the address above to force the family :
* - "ipv4@" => force address to resolve as IPv4 and fail if not possible.
* - "ipv6@" => force address to resolve as IPv6 and fail if not possible.
* - "unix@" => force address to be a path to a UNIX socket even if the
* path does not start with a '/'
* - 'abns@' -> force address to belong to the abstract namespace (Linux
* only). These sockets are just like Unix sockets but without
* the need for an underlying file system. The address is a
* string. Technically it's like a Unix socket with a zero in
* the first byte of the address.
* - "fd@" => an integer must follow, and is a file descriptor number.
*
* IPv6 addresses can be declared with or without square brackets. When using
* square brackets for IPv6 addresses, the port separator (colon) is optional.
* If not using square brackets, and in order to avoid any ambiguity with
* IPv6 addresses, the last colon ':' is mandatory even when no port is specified.
* NULL is returned if the address cannot be parsed. The <low> and <high> ports
* are always initialized if non-null, even for non-IP families.
*
* If <pfx> is non-null, it is used as a string prefix before any path-based
* address (typically the path to a unix socket).
*
* if <fqdn> is non-null, it will be filled with :
* - a pointer to the FQDN of the server name to resolve if there's one, and
* that the caller will have to free(),
* - NULL if there was an explicit address that doesn't require resolution.
*
* Hostnames are only resolved if <opts> has PA_O_RESOLVE. Otherwise <fqdn> is
* still honored so it is possible for the caller to know whether a resolution
* failed by clearing this flag and checking if <fqdn> was filled, indicating
* the need for a resolution.
*
* When a file descriptor is passed, its value is put into the s_addr part of
* the address when cast to sockaddr_in and the address family is
* AF_CUST_EXISTING_FD.
*
* The matching protocol will be set into <proto> if non-null.
*
* Any known file descriptor is also assigned to <fd> if non-null, otherwise it
* is forced to -1.
*/
struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int *high, int *fd,
struct protocol **proto, char **err,
const char *pfx, char **fqdn, unsigned int opts)
{
static THREAD_LOCAL struct sockaddr_storage ss;
struct sockaddr_storage *ret = NULL;
struct protocol *new_proto = NULL;
char *back, *str2;
char *port1, *port2;
int portl, porth, porta;
int abstract = 0;
int new_fd = -1;
int sock_type, ctrl_type;
portl = porth = porta = 0;
if (fqdn)
*fqdn = NULL;
str2 = back = env_expand(strdup(str));
if (str2 == NULL) {
memprintf(err, "out of memory in '%s'\n", __FUNCTION__);
goto out;
}
if (!*str2) {
memprintf(err, "'%s' resolves to an empty address (environment variable missing?)\n", str);
goto out;
}
memset(&ss, 0, sizeof(ss));
/* prepare the default socket types */
if ((opts & (PA_O_STREAM|PA_O_DGRAM)) == PA_O_DGRAM ||
((opts & (PA_O_STREAM|PA_O_DGRAM)) == (PA_O_DGRAM|PA_O_STREAM) && (opts & PA_O_DEFAULT_DGRAM)))
sock_type = ctrl_type = SOCK_DGRAM;
else
sock_type = ctrl_type = SOCK_STREAM;
if (strncmp(str2, "stream+", 7) == 0) {
str2 += 7;
sock_type = ctrl_type = SOCK_STREAM;
}
else if (strncmp(str2, "dgram+", 6) == 0) {
str2 += 6;
sock_type = ctrl_type = SOCK_DGRAM;
}
if (strncmp(str2, "unix@", 5) == 0) {
str2 += 5;
abstract = 0;
ss.ss_family = AF_UNIX;
}
else if (strncmp(str2, "uxdg@", 5) == 0) {
str2 += 5;
abstract = 0;
ss.ss_family = AF_UNIX;
sock_type = ctrl_type = SOCK_DGRAM;
}
else if (strncmp(str2, "uxst@", 5) == 0) {
str2 += 5;
abstract = 0;
ss.ss_family = AF_UNIX;
sock_type = ctrl_type = SOCK_STREAM;
}