net-internal.h 5.8 KB

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