|
| 1 | +// Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 2 | + |
| 3 | +/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */ |
| 4 | +/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */ |
| 5 | +/* |
| 6 | + * This is an adaptation of Android's implementation of RFC 6724 |
| 7 | + * (in Android's getaddrinfo.c). It has some cosmetic differences |
| 8 | + * from Android's getaddrinfo.c, but Android's getaddrinfo.c was |
| 9 | + * used as a guide or example of a way to implement the RFC 6724 spec when |
| 10 | + * this was written. |
| 11 | + */ |
| 12 | + |
| 13 | +#ifndef ADDRESS_SORTING_H |
| 14 | +#define ADDRESS_SORTING_H |
| 15 | + |
| 16 | +#include <stddef.h> |
| 17 | + |
| 18 | +#ifdef __cplusplus |
| 19 | +extern "C" { |
| 20 | +#endif |
| 21 | + |
| 22 | +typedef struct address_sorting_address { |
| 23 | + char addr[128]; |
| 24 | + size_t len; |
| 25 | +} address_sorting_address; |
| 26 | + |
| 27 | +/* address_sorting_sortable represents one entry in a list of destination |
| 28 | + * IP addresses to sort. It contains the destination IP address |
| 29 | + * "sorting key", along with placeholder and scratch fields. */ |
| 30 | +typedef struct address_sorting_sortable { |
| 31 | + // input data; sorting key |
| 32 | + address_sorting_address dest_addr; |
| 33 | + // input data; optional value to attach to the sorting key |
| 34 | + void* user_data; |
| 35 | + // internal fields, these must be zero'd when passed to sort function |
| 36 | + address_sorting_address source_addr; |
| 37 | + bool source_addr_exists; |
| 38 | + size_t original_index; |
| 39 | +} address_sorting_sortable; |
| 40 | + |
| 41 | +void address_sorting_rfc_6724_sort(address_sorting_sortable* sortables, |
| 42 | + size_t sortables_len); |
| 43 | + |
| 44 | +void address_sorting_init(); |
| 45 | +void address_sorting_shutdown(); |
| 46 | + |
| 47 | +struct address_sorting_source_addr_factory; |
| 48 | + |
| 49 | +/* The interfaces below are exposed only for testing */ |
| 50 | +typedef struct { |
| 51 | + /* Gets the source address that would be used for the passed-in destination |
| 52 | + * address, and fills in *source_addr* with it if one exists. |
| 53 | + * Returns true if a source address exists for the destination address, |
| 54 | + * and false otherwise. */ |
| 55 | + bool (*get_source_addr)(struct address_sorting_source_addr_factory* factory, |
| 56 | + const address_sorting_address* dest_addr, |
| 57 | + address_sorting_address* source_addr); |
| 58 | + void (*destroy)(struct address_sorting_source_addr_factory* factory); |
| 59 | +} address_sorting_source_addr_factory_vtable; |
| 60 | + |
| 61 | +typedef struct address_sorting_source_addr_factory { |
| 62 | + const address_sorting_source_addr_factory_vtable* vtable; |
| 63 | +} address_sorting_source_addr_factory; |
| 64 | + |
| 65 | +/* Platform-compatible address family types */ |
| 66 | +typedef enum { |
| 67 | + ADDRESS_SORTING_AF_INET, |
| 68 | + ADDRESS_SORTING_AF_INET6, |
| 69 | + ADDRESS_SORTING_UNKNOWN_FAMILY, |
| 70 | +} address_sorting_family; |
| 71 | + |
| 72 | +/* Indicates whether the address is AF_INET, AF_INET6, or another address |
| 73 | + * family. */ |
| 74 | +address_sorting_family address_sorting_abstract_get_family( |
| 75 | + const address_sorting_address* address); |
| 76 | + |
| 77 | +void address_sorting_override_source_addr_factory_for_testing( |
| 78 | + address_sorting_source_addr_factory* factory); |
| 79 | + |
| 80 | +bool address_sorting_get_source_addr_for_testing( |
| 81 | + const address_sorting_address* dest, address_sorting_address* source); |
| 82 | + |
| 83 | +#ifdef __cplusplus |
| 84 | +} |
| 85 | +#endif |
| 86 | + |
| 87 | +#endif // ADDRESS_SORTING_H |
0 commit comments