| 1 | /* Network-related functions for internal library use. | 
|---|
| 2 | Copyright (C) 2016-2020 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of the GNU C Library. | 
|---|
| 4 |  | 
|---|
| 5 | The GNU C Library is free software; you can redistribute it and/or | 
|---|
| 6 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 7 | License as published by the Free Software Foundation; either | 
|---|
| 8 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 9 |  | 
|---|
| 10 | The GNU C Library is distributed in the hope that it will be useful, | 
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 13 | Lesser General Public License for more details. | 
|---|
| 14 |  | 
|---|
| 15 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 16 | License along with the GNU C Library; if not, see | 
|---|
| 17 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 18 |  | 
|---|
| 19 | #ifndef _NET_INTERNAL_H | 
|---|
| 20 | #define _NET_INTERNAL_H 1 | 
|---|
| 21 |  | 
|---|
| 22 | #include <arpa/inet.h> | 
|---|
| 23 | #include <stdbool.h> | 
|---|
| 24 | #include <stdint.h> | 
|---|
| 25 | #include <sys/time.h> | 
|---|
| 26 | #include <libc-diag.h> | 
|---|
| 27 | #include <struct___timespec64.h> | 
|---|
| 28 |  | 
|---|
| 29 | int __inet6_scopeid_pton (const struct in6_addr *address, | 
|---|
| 30 | const char *scope, uint32_t *result); | 
|---|
| 31 | libc_hidden_proto (__inet6_scopeid_pton) | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | /* IDNA conversion.  These functions convert domain names between the | 
|---|
| 35 | current multi-byte character set and the IDNA encoding.  On | 
|---|
| 36 | success, the result string is written to *RESULT (which the caller | 
|---|
| 37 | has to free), and zero is returned.  On error, an EAI_* error code | 
|---|
| 38 | is returned (see <netdb.h>), and *RESULT is not changed.  */ | 
|---|
| 39 | int __idna_to_dns_encoding (const char *name, char **result); | 
|---|
| 40 | libc_hidden_proto (__idna_to_dns_encoding) | 
|---|
| 41 | int __idna_from_dns_encoding (const char *name, char **result); | 
|---|
| 42 | libc_hidden_proto (__idna_from_dns_encoding) | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | /* Return value of __idna_name_classify below.  */ | 
|---|
| 46 | enum idna_name_classification | 
|---|
| 47 | { | 
|---|
| 48 | idna_name_ascii,          /* No non-ASCII characters.  */ | 
|---|
| 49 | idna_name_nonascii,       /* Non-ASCII characters, no backslash.  */ | 
|---|
| 50 | idna_name_nonascii_backslash, /* Non-ASCII characters with backslash.  */ | 
|---|
| 51 | idna_name_encoding_error, /* Decoding error.  */ | 
|---|
| 52 | idna_name_memory_error,   /* Memory allocation failure.  */ | 
|---|
| 53 | idna_name_error,          /* Other error during decoding.  Check errno.  */ | 
|---|
| 54 | }; | 
|---|
| 55 |  | 
|---|
| 56 | /* Check the specified name for non-ASCII characters and backslashes | 
|---|
| 57 | or encoding errors.  */ | 
|---|
| 58 | enum idna_name_classification __idna_name_classify (const char *name) | 
|---|
| 59 | attribute_hidden; | 
|---|
| 60 |  | 
|---|
| 61 | /* Deadline handling for enforcing timeouts. | 
|---|
| 62 |  | 
|---|
| 63 | Code should call __deadline_current_time to obtain the current time | 
|---|
| 64 | and cache it locally.  The cache needs updating after every | 
|---|
| 65 | long-running or potentially blocking operation.  Deadlines relative | 
|---|
| 66 | to the current time can be computed using __deadline_from_timeval. | 
|---|
| 67 | The deadlines may have to be recomputed in response to certain | 
|---|
| 68 | events (such as an incoming packet), but they are absolute (not | 
|---|
| 69 | relative to the current time).  A timeout suitable for use with the | 
|---|
| 70 | poll function can be computed from such a deadline using | 
|---|
| 71 | __deadline_to_ms. | 
|---|
| 72 |  | 
|---|
| 73 | The fields in the structs defined belowed should only be used | 
|---|
| 74 | within the implementation.  */ | 
|---|
| 75 |  | 
|---|
| 76 | /* Cache of the current time.  Used to compute deadlines from relative | 
|---|
| 77 | timeouts and vice versa.  */ | 
|---|
| 78 | struct deadline_current_time | 
|---|
| 79 | { | 
|---|
| 80 | struct __timespec64 current; | 
|---|
| 81 | }; | 
|---|
| 82 |  | 
|---|
| 83 | /* Return the current time.  Terminates the process if the current | 
|---|
| 84 | time is not available.  */ | 
|---|
| 85 | struct deadline_current_time __deadline_current_time (void) attribute_hidden; | 
|---|
| 86 |  | 
|---|
| 87 | /* Computed absolute deadline.  */ | 
|---|
| 88 | struct deadline | 
|---|
| 89 | { | 
|---|
| 90 | struct __timespec64 absolute; | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | /* For internal use only.  */ | 
|---|
| 95 | static inline bool | 
|---|
| 96 | __deadline_is_infinite (struct deadline deadline) | 
|---|
| 97 | { | 
|---|
| 98 | return deadline.absolute.tv_nsec < 0; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | /* GCC 8.3 and 9.2 both incorrectly report total_deadline | 
|---|
| 102 | * (from sunrpc/clnt_udp.c) as maybe-uninitialized when tv_sec is 8 bytes | 
|---|
| 103 | * (64-bits) wide on 32-bit systems. We have to set -Wmaybe-uninitialized | 
|---|
| 104 | * here as it won't fix the error in sunrpc/clnt_udp.c. | 
|---|
| 105 | * A GCC bug has been filed here: | 
|---|
| 106 | *    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91691 | 
|---|
| 107 | */ | 
|---|
| 108 | DIAG_PUSH_NEEDS_COMMENT; | 
|---|
| 109 | DIAG_IGNORE_NEEDS_COMMENT (9, "-Wmaybe-uninitialized"); | 
|---|
| 110 |  | 
|---|
| 111 | /* Return true if the current time is at the deadline or past it.  */ | 
|---|
| 112 | static inline bool | 
|---|
| 113 | __deadline_elapsed (struct deadline_current_time current, | 
|---|
| 114 | struct deadline deadline) | 
|---|
| 115 | { | 
|---|
| 116 | return !__deadline_is_infinite (deadline) | 
|---|
| 117 | && (current.current.tv_sec > deadline.absolute.tv_sec | 
|---|
| 118 | || (current.current.tv_sec == deadline.absolute.tv_sec | 
|---|
| 119 | && current.current.tv_nsec >= deadline.absolute.tv_nsec)); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | /* Return the deadline which occurs first.  */ | 
|---|
| 123 | static inline struct deadline | 
|---|
| 124 | __deadline_first (struct deadline left, struct deadline right) | 
|---|
| 125 | { | 
|---|
| 126 | if (__deadline_is_infinite (right) | 
|---|
| 127 | || left.absolute.tv_sec < right.absolute.tv_sec | 
|---|
| 128 | || (left.absolute.tv_sec == right.absolute.tv_sec | 
|---|
| 129 | && left.absolute.tv_nsec < right.absolute.tv_nsec)) | 
|---|
| 130 | return left; | 
|---|
| 131 | else | 
|---|
| 132 | return right; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | DIAG_POP_NEEDS_COMMENT; | 
|---|
| 136 |  | 
|---|
| 137 | /* Add TV to the current time and return it.  Returns a special | 
|---|
| 138 | infinite absolute deadline on overflow.  */ | 
|---|
| 139 | struct deadline __deadline_from_timeval (struct deadline_current_time, | 
|---|
| 140 | struct timeval tv) attribute_hidden; | 
|---|
| 141 |  | 
|---|
| 142 | /* Compute the number of milliseconds until the specified deadline, | 
|---|
| 143 | from the current time in the argument.  The result is mainly for | 
|---|
| 144 | use with poll.  If the deadline has already passed, return 0.  If | 
|---|
| 145 | the result would overflow an int, return INT_MAX.  */ | 
|---|
| 146 | int __deadline_to_ms (struct deadline_current_time, struct deadline) | 
|---|
| 147 | attribute_hidden; | 
|---|
| 148 |  | 
|---|
| 149 | /* Return true if TV.tv_sec is non-negative and TV.tv_usec is in the | 
|---|
| 150 | interval [0, 999999].  */ | 
|---|
| 151 | static inline bool | 
|---|
| 152 | __is_timeval_valid_timeout (struct timeval tv) | 
|---|
| 153 | { | 
|---|
| 154 | return tv.tv_sec >= 0 && tv.tv_usec >= 0 && tv.tv_usec < 1000 * 1000; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | #endif /* _NET_INTERNAL_H */ | 
|---|
| 158 |  | 
|---|