1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "errno-util.h"
9 #include "hash-funcs.h"
10 #include "in-addr-util.h"
11 #include "logarithm.h"
12 #include "memory-util.h"
13 #include "parse-util.h"
14 #include "random-util.h"
15 #include "siphash24.h"
16 #include "stdio-util.h"
17 #include "string-util.h"
19 bool in4_addr_is_null(const struct in_addr
*a
) {
22 return a
->s_addr
== 0;
25 bool in6_addr_is_null(const struct in6_addr
*a
) {
28 return eqzero(a
->s6_addr32
);
31 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
34 if (family
== AF_INET
)
35 return in4_addr_is_null(&u
->in
);
37 if (family
== AF_INET6
)
38 return in6_addr_is_null(&u
->in6
);
43 bool in4_addr_is_link_local(const struct in_addr
*a
) {
46 return (be32toh(a
->s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
49 bool in4_addr_is_link_local_dynamic(const struct in_addr
*a
) {
52 if (!in4_addr_is_link_local(a
))
55 /* 169.254.0.0/24 and 169.254.255.0/24 must not be used for the dynamic IPv4LL assignment.
56 * See RFC 3927 Section 2.1:
57 * The IPv4 prefix 169.254/16 is registered with the IANA for this purpose. The first 256 and last
58 * 256 addresses in the 169.254/16 prefix are reserved for future use and MUST NOT be selected by a
59 * host using this dynamic configuration mechanism. */
60 return !IN_SET(be32toh(a
->s_addr
) & 0x0000FF00U
, 0x0000U
, 0xFF00U
);
63 bool in6_addr_is_link_local(const struct in6_addr
*a
) {
66 return (a
->s6_addr32
[0] & htobe32(0xffc00000)) == htobe32(0xfe800000);
69 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
72 if (family
== AF_INET
)
73 return in4_addr_is_link_local(&u
->in
);
75 if (family
== AF_INET6
)
76 return in6_addr_is_link_local(&u
->in6
);
81 bool in6_addr_is_link_local_all_nodes(const struct in6_addr
*a
) {
85 return be32toh(a
->s6_addr32
[0]) == UINT32_C(0xff020000) &&
86 a
->s6_addr32
[1] == 0 &&
87 a
->s6_addr32
[2] == 0 &&
88 be32toh(a
->s6_addr32
[3]) == UINT32_C(0x00000001);
91 bool in4_addr_is_multicast(const struct in_addr
*a
) {
94 return IN_MULTICAST(be32toh(a
->s_addr
));
97 bool in6_addr_is_multicast(const struct in6_addr
*a
) {
100 return a
->s6_addr
[0] == 0xff;
103 int in_addr_is_multicast(int family
, const union in_addr_union
*u
) {
106 if (family
== AF_INET
)
107 return in4_addr_is_multicast(&u
->in
);
109 if (family
== AF_INET6
)
110 return in6_addr_is_multicast(&u
->in6
);
112 return -EAFNOSUPPORT
;
115 bool in4_addr_is_local_multicast(const struct in_addr
*a
) {
118 return (be32toh(a
->s_addr
) & UINT32_C(0xffffff00)) == UINT32_C(0xe0000000);
121 bool in4_addr_is_localhost(const struct in_addr
*a
) {
124 /* All of 127.x.x.x is localhost. */
125 return (be32toh(a
->s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
128 bool in4_addr_is_non_local(const struct in_addr
*a
) {
129 /* Whether the address is not null and not localhost.
131 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
132 return !in4_addr_is_null(a
) &&
133 !in4_addr_is_localhost(a
);
136 static bool in6_addr_is_loopback(const struct in6_addr
*a
) {
137 return memcmp(a
, &(struct in6_addr
) IN6ADDR_LOOPBACK_INIT
, sizeof(struct in6_addr
)) == 0;
140 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
143 if (family
== AF_INET
)
144 return in4_addr_is_localhost(&u
->in
);
146 if (family
== AF_INET6
)
147 return in6_addr_is_loopback(&u
->in6
);
149 return -EAFNOSUPPORT
;
152 int in_addr_is_localhost_one(int family
, const union in_addr_union
*u
) {
155 if (family
== AF_INET
)
157 return be32toh(u
->in
.s_addr
) == UINT32_C(0x7F000001);
159 if (family
== AF_INET6
)
160 return in6_addr_is_loopback(&u
->in6
);
162 return -EAFNOSUPPORT
;
165 bool in6_addr_is_ipv4_mapped_address(const struct in6_addr
*a
) {
166 return a
->s6_addr32
[0] == 0 &&
167 a
->s6_addr32
[1] == 0 &&
168 a
->s6_addr32
[2] == htobe32(UINT32_C(0x0000ffff));
171 bool in4_addr_equal(const struct in_addr
*a
, const struct in_addr
*b
) {
175 return a
->s_addr
== b
->s_addr
;
178 bool in6_addr_equal(const struct in6_addr
*a
, const struct in6_addr
*b
) {
182 return memcmp(a
, b
, sizeof(struct in6_addr
)) == 0;
185 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
189 if (family
== AF_INET
)
190 return in4_addr_equal(&a
->in
, &b
->in
);
192 if (family
== AF_INET6
)
193 return in6_addr_equal(&a
->in6
, &b
->in6
);
195 return -EAFNOSUPPORT
;
198 bool in4_addr_prefix_intersect(
199 const struct in_addr
*a
,
201 const struct in_addr
*b
,
202 unsigned bprefixlen
) {
207 unsigned m
= MIN3(aprefixlen
, bprefixlen
, (unsigned) (sizeof(struct in_addr
) * 8));
209 return true; /* Let's return earlier, to avoid shift by 32. */
211 uint32_t x
= be32toh(a
->s_addr
^ b
->s_addr
);
212 uint32_t n
= 0xFFFFFFFFUL
<< (32 - m
);
216 bool in6_addr_prefix_intersect(
217 const struct in6_addr
*a
,
219 const struct in6_addr
*b
,
220 unsigned bprefixlen
) {
225 unsigned m
= MIN3(aprefixlen
, bprefixlen
, (unsigned) (sizeof(struct in6_addr
) * 8));
229 for (size_t i
= 0; i
< sizeof(struct in6_addr
); i
++) {
230 uint8_t x
= a
->s6_addr
[i
] ^ b
->s6_addr
[i
];
231 uint8_t n
= m
< 8 ? (0xFF << (8 - m
)) : 0xFF;
244 int in_addr_prefix_intersect(
246 const union in_addr_union
*a
,
248 const union in_addr_union
*b
,
249 unsigned bprefixlen
) {
254 /* Checks whether there are any addresses that are in both networks. */
256 if (family
== AF_INET
)
257 return in4_addr_prefix_intersect(&a
->in
, aprefixlen
, &b
->in
, bprefixlen
);
259 if (family
== AF_INET6
)
260 return in6_addr_prefix_intersect(&a
->in6
, aprefixlen
, &b
->in6
, bprefixlen
);
262 return -EAFNOSUPPORT
;
265 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
268 /* Increases the network part of an address by one. Returns 0 if that succeeds, or -ERANGE if
271 return in_addr_prefix_nth(family
, u
, prefixlen
, 1);
275 * Calculates the nth prefix of size prefixlen starting from the address denoted by u.
277 * On success 0 will be returned and the calculated prefix will be available in
278 * u. In case the calculation cannot be performed (invalid prefix length,
279 * overflows would occur) -ERANGE is returned. If the address family given isn't
280 * supported -EAFNOSUPPORT will be returned.
283 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 2), returns 0, writes 192.168.2.0 to u
284 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 0), returns 0, no data written
285 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 24, 1), returns -ERANGE, no data written
286 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 0, 1), returns -ERANGE, no data written
287 * - in_addr_prefix_nth(AF_INET6, 2001:db8, 64, 0xff00) returns 0, writes 2001:0db8:0000:ff00:: to u
289 int in_addr_prefix_nth(int family
, union in_addr_union
*u
, unsigned prefixlen
, uint64_t nth
) {
295 if (family
== AF_INET
) {
301 c
= be32toh(u
->in
.s_addr
);
303 t
= nth
<< (32 - prefixlen
);
306 if (c
> UINT32_MAX
- t
)
311 n
&= UINT32_C(0xFFFFFFFF) << (32 - prefixlen
);
312 u
->in
.s_addr
= htobe32(n
);
316 if (family
== AF_INET6
) {
317 bool overflow
= false;
322 for (unsigned i
= 16; i
> 0; i
--) {
323 unsigned t
, j
= i
- 1, p
= j
* 8;
325 if (p
>= prefixlen
) {
326 u
->in6
.s6_addr
[j
] = 0;
330 if (prefixlen
- p
< 8) {
331 u
->in6
.s6_addr
[j
] &= 0xff << (8 - (prefixlen
- p
));
332 t
= u
->in6
.s6_addr
[j
] + ((nth
& 0xff) << (8 - (prefixlen
- p
)));
333 nth
>>= prefixlen
- p
;
335 t
= u
->in6
.s6_addr
[j
] + (nth
& 0xff) + overflow
;
339 overflow
= t
> UINT8_MAX
;
340 u
->in6
.s6_addr
[j
] = (uint8_t) (t
& 0xff);
343 if (overflow
|| nth
!= 0)
349 return -EAFNOSUPPORT
;
352 int in_addr_random_prefix(
354 union in_addr_union
*u
,
355 unsigned prefixlen_fixed_part
,
356 unsigned prefixlen
) {
360 /* Random network part of an address by one. */
365 if (family
== AF_INET
) {
368 if (prefixlen_fixed_part
> 32)
369 prefixlen_fixed_part
= 32;
372 if (prefixlen_fixed_part
>= prefixlen
)
375 c
= be32toh(u
->in
.s_addr
);
376 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
378 random_bytes(&n
, sizeof(n
));
379 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
381 u
->in
.s_addr
= htobe32(n
| c
);
385 if (family
== AF_INET6
) {
389 if (prefixlen_fixed_part
> 128)
390 prefixlen_fixed_part
= 128;
393 if (prefixlen_fixed_part
>= prefixlen
)
396 random_bytes(&n
, sizeof(n
));
398 for (i
= 0; i
< 16; i
++) {
399 uint8_t mask_fixed_part
= 0, mask
= 0;
401 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
402 if (i
< prefixlen_fixed_part
/ 8)
403 mask_fixed_part
= 0xffu
;
405 j
= prefixlen_fixed_part
% 8;
406 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
410 if (i
< (prefixlen
+ 7) / 8) {
411 if (i
< prefixlen
/ 8)
412 mask
= 0xffu
^ mask_fixed_part
;
415 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
419 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
420 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
426 return -EAFNOSUPPORT
;
429 int in_addr_prefix_range(
431 const union in_addr_union
*in
,
433 union in_addr_union
*ret_start
,
434 union in_addr_union
*ret_end
) {
436 union in_addr_union start
, end
;
441 if (!IN_SET(family
, AF_INET
, AF_INET6
))
442 return -EAFNOSUPPORT
;
446 r
= in_addr_prefix_nth(family
, &start
, prefixlen
, 0);
453 r
= in_addr_prefix_nth(family
, &end
, prefixlen
, 1);
466 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
467 _cleanup_free_
char *x
= NULL
;
473 if (family
== AF_INET
)
475 else if (family
== AF_INET6
)
476 l
= INET6_ADDRSTRLEN
;
478 return -EAFNOSUPPORT
;
485 if (!typesafe_inet_ntop(family
, u
, x
, l
))
486 return errno_or_else(EINVAL
);
492 const char* typesafe_inet_ntop(int family
, const union in_addr_union
*a
, char *buf
, size_t len
) {
493 return inet_ntop(family
, a
, buf
, len
);
496 const char* typesafe_inet_ntop4(const struct in_addr
*a
, char *buf
, size_t len
) {
497 return inet_ntop(AF_INET
, a
, buf
, len
);
500 const char* typesafe_inet_ntop6(const struct in6_addr
*a
, char *buf
, size_t len
) {
501 return inet_ntop(AF_INET6
, a
, buf
, len
);
504 int in_addr_prefix_to_string(
506 const union in_addr_union
*u
,
514 if (!IN_SET(family
, AF_INET
, AF_INET6
))
515 return -EAFNOSUPPORT
;
518 if (!typesafe_inet_ntop(family
, u
, buf
, buf_len
))
519 return errno_or_else(ENOSPC
);
521 size_t l
= strlen(buf
);
522 if (!snprintf_ok(buf
+ l
, buf_len
- l
, "/%u", prefixlen
))
527 int in_addr_port_ifindex_name_to_string(int family
, const union in_addr_union
*u
, uint16_t port
, int ifindex
, const char *server_name
, char **ret
) {
528 _cleanup_free_
char *ip_str
= NULL
, *x
= NULL
;
531 assert(IN_SET(family
, AF_INET
, AF_INET6
));
535 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
536 * handle IPv6 link-local addresses. */
538 r
= in_addr_to_string(family
, u
, &ip_str
);
542 if (family
== AF_INET6
) {
543 r
= in_addr_is_link_local(family
, u
);
549 ifindex
= 0; /* For IPv4 address, ifindex is always ignored. */
551 if (port
== 0 && ifindex
== 0 && isempty(server_name
)) {
552 *ret
= TAKE_PTR(ip_str
);
556 const char *separator
= isempty(server_name
) ? "" : "#";
557 server_name
= strempty(server_name
);
560 if (family
== AF_INET6
) {
562 r
= asprintf(&x
, "[%s]:%"PRIu16
"%%%i%s%s", ip_str
, port
, ifindex
, separator
, server_name
);
564 r
= asprintf(&x
, "[%s]:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
566 r
= asprintf(&x
, "%s:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
569 r
= asprintf(&x
, "%s%%%i%s%s", ip_str
, ifindex
, separator
, server_name
);
571 x
= strjoin(ip_str
, separator
, server_name
);
582 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
583 union in_addr_union buffer
;
586 if (!IN_SET(family
, AF_INET
, AF_INET6
))
587 return -EAFNOSUPPORT
;
590 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
591 return errno_or_else(EINVAL
);
596 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
601 r
= in_addr_from_string(AF_INET
, s
, ret
);
604 *ret_family
= AF_INET
;
608 r
= in_addr_from_string(AF_INET6
, s
, ret
);
611 *ret_family
= AF_INET6
;
618 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
621 return 32U - u32ctz(be32toh(addr
->s_addr
));
624 /* Calculate an IPv4 netmask from prefix length, for example /8 -> 255.0.0.0. */
625 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
627 assert(prefixlen
<= 32);
629 /* Shifting beyond 32 is not defined, handle this specially. */
633 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
638 /* Calculate an IPv6 netmask from prefix length, for example /16 -> ffff::. */
639 struct in6_addr
* in6_addr_prefixlen_to_netmask(struct in6_addr
*addr
, unsigned char prefixlen
) {
641 assert(prefixlen
<= 128);
643 for (unsigned i
= 0; i
< 16; i
++) {
646 if (prefixlen
>= 8) {
649 } else if (prefixlen
> 0) {
650 mask
= 0xFF << (8 - prefixlen
);
653 assert(prefixlen
== 0);
657 addr
->s6_addr
[i
] = mask
;
663 /* Calculate an IPv4 or IPv6 netmask from prefix length, for example /8 -> 255.0.0.0 or /16 -> ffff::. */
664 int in_addr_prefixlen_to_netmask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
669 in4_addr_prefixlen_to_netmask(&addr
->in
, prefixlen
);
672 in6_addr_prefixlen_to_netmask(&addr
->in6
, prefixlen
);
675 return -EAFNOSUPPORT
;
679 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
680 uint8_t msb_octet
= *(uint8_t*) addr
;
682 /* addr may not be aligned, so make sure we only access it byte-wise */
688 /* class A, leading bits: 0 */
690 else if (msb_octet
< 192)
691 /* class B, leading bits 10 */
693 else if (msb_octet
< 224)
694 /* class C, leading bits 110 */
697 /* class D or E, no default prefixlen */
703 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
704 unsigned char prefixlen
;
710 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
714 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
718 int in4_addr_mask(struct in_addr
*addr
, unsigned char prefixlen
) {
723 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
726 addr
->s_addr
&= mask
.s_addr
;
730 int in6_addr_mask(struct in6_addr
*addr
, unsigned char prefixlen
) {
733 for (i
= 0; i
< 16; i
++) {
736 if (prefixlen
>= 8) {
739 } else if (prefixlen
> 0) {
740 mask
= 0xFF << (8 - prefixlen
);
743 assert(prefixlen
== 0);
747 addr
->s6_addr
[i
] &= mask
;
753 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
758 return in4_addr_mask(&addr
->in
, prefixlen
);
760 return in6_addr_mask(&addr
->in6
, prefixlen
);
762 return -EAFNOSUPPORT
;
766 int in4_addr_prefix_covers_full(
767 const struct in_addr
*prefix
,
768 unsigned char prefixlen
,
769 const struct in_addr
*address
,
770 unsigned char address_prefixlen
) {
772 struct in_addr masked_prefix
, masked_address
;
778 if (prefixlen
> address_prefixlen
)
781 masked_prefix
= *prefix
;
782 r
= in4_addr_mask(&masked_prefix
, prefixlen
);
786 masked_address
= *address
;
787 r
= in4_addr_mask(&masked_address
, prefixlen
);
791 return in4_addr_equal(&masked_prefix
, &masked_address
);
794 int in6_addr_prefix_covers_full(
795 const struct in6_addr
*prefix
,
796 unsigned char prefixlen
,
797 const struct in6_addr
*address
,
798 unsigned char address_prefixlen
) {
800 struct in6_addr masked_prefix
, masked_address
;
806 if (prefixlen
> address_prefixlen
)
809 masked_prefix
= *prefix
;
810 r
= in6_addr_mask(&masked_prefix
, prefixlen
);
814 masked_address
= *address
;
815 r
= in6_addr_mask(&masked_address
, prefixlen
);
819 return in6_addr_equal(&masked_prefix
, &masked_address
);
822 int in_addr_prefix_covers_full(
824 const union in_addr_union
*prefix
,
825 unsigned char prefixlen
,
826 const union in_addr_union
*address
,
827 unsigned char address_prefixlen
) {
834 return in4_addr_prefix_covers_full(&prefix
->in
, prefixlen
, &address
->in
, address_prefixlen
);
836 return in6_addr_prefix_covers_full(&prefix
->in6
, prefixlen
, &address
->in6
, address_prefixlen
);
838 return -EAFNOSUPPORT
;
842 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
846 if (!IN_SET(family
, AF_INET
, AF_INET6
))
847 return -EAFNOSUPPORT
;
849 r
= safe_atou8(p
, &u
);
853 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
860 int in_addr_prefix_from_string(
863 union in_addr_union
*ret_prefix
,
864 unsigned char *ret_prefixlen
) {
866 _cleanup_free_
char *str
= NULL
;
867 union in_addr_union buffer
;
874 if (!IN_SET(family
, AF_INET
, AF_INET6
))
875 return -EAFNOSUPPORT
;
879 str
= strndup(p
, e
- p
);
887 r
= in_addr_from_string(family
, l
, &buffer
);
892 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
896 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
899 *ret_prefix
= buffer
;
906 int in_addr_prefix_from_string_auto_full(
908 InAddrPrefixLenMode mode
,
910 union in_addr_union
*ret_prefix
,
911 unsigned char *ret_prefixlen
) {
913 _cleanup_free_
char *str
= NULL
;
914 union in_addr_union buffer
;
923 str
= strndup(p
, e
- p
);
931 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
936 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
942 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
944 case PREFIXLEN_REFUSE
:
945 return -ENOANO
; /* To distinguish this error from others. */
947 assert_not_reached();
951 *ret_family
= family
;
953 *ret_prefix
= buffer
;
960 void in_addr_hash_func(const union in_addr_union
*u
, int family
, struct siphash
*state
) {
964 siphash24_compress(u
->bytes
, FAMILY_ADDRESS_SIZE(family
), state
);
967 void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
971 siphash24_compress_typesafe(a
->family
, state
);
972 in_addr_hash_func(&a
->address
, a
->family
, state
);
975 int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
981 r
= CMP(x
->family
, y
->family
);
985 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
989 in_addr_data_hash_ops
,
991 in_addr_data_hash_func
,
992 in_addr_data_compare_func
);
994 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
995 in_addr_data_hash_ops_free
,
997 in_addr_data_hash_func
,
998 in_addr_data_compare_func
,
1001 void in6_addr_hash_func(const struct in6_addr
*addr
, struct siphash
*state
) {
1005 siphash24_compress_typesafe(*addr
, state
);
1008 int in6_addr_compare_func(const struct in6_addr
*a
, const struct in6_addr
*b
) {
1012 return memcmp(a
, b
, sizeof(*a
));
1019 in6_addr_compare_func
);
1021 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
1022 in6_addr_hash_ops_free
,
1025 in6_addr_compare_func
,