net-internal.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Network-related functions for internal library use.
  2. Copyright (C) 2016-2019 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. <http://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. int __inet6_scopeid_pton (const struct in6_addr *address,
  22. const char *scope, uint32_t *result);
  23. libc_hidden_proto (__inet6_scopeid_pton)
  24. /* IDNA conversion. These functions convert domain names between the
  25. current multi-byte character set and the IDNA encoding. On
  26. success, the result string is written to *RESULT (which the caller
  27. has to free), and zero is returned. On error, an EAI_* error code
  28. is returned (see <netdb.h>), and *RESULT is not changed. */
  29. int __idna_to_dns_encoding (const char *name, char **result);
  30. libc_hidden_proto (__idna_to_dns_encoding)
  31. int __idna_from_dns_encoding (const char *name, char **result);
  32. libc_hidden_proto (__idna_from_dns_encoding)
  33. /* Return value of __idna_name_classify below. */
  34. enum idna_name_classification
  35. {
  36. idna_name_ascii, /* No non-ASCII characters. */
  37. idna_name_nonascii, /* Non-ASCII characters, no backslash. */
  38. idna_name_nonascii_backslash, /* Non-ASCII characters with backslash. */
  39. idna_name_encoding_error, /* Decoding error. */
  40. idna_name_memory_error, /* Memory allocation failure. */
  41. idna_name_error, /* Other error during decoding. Check errno. */
  42. };
  43. /* Check the specified name for non-ASCII characters and backslashes
  44. or encoding errors. */
  45. enum idna_name_classification __idna_name_classify (const char *name)
  46. attribute_hidden;
  47. /* Deadline handling for enforcing timeouts.
  48. Code should call __deadline_current_time to obtain the current time
  49. and cache it locally. The cache needs updating after every
  50. long-running or potentially blocking operation. Deadlines relative
  51. to the current time can be computed using __deadline_from_timeval.
  52. The deadlines may have to be recomputed in response to certain
  53. events (such as an incoming packet), but they are absolute (not
  54. relative to the current time). A timeout suitable for use with the
  55. poll function can be computed from such a deadline using
  56. __deadline_to_ms.
  57. The fields in the structs defined belowed should only be used
  58. within the implementation. */
  59. /* Cache of the current time. Used to compute deadlines from relative
  60. timeouts and vice versa. */
  61. struct deadline_current_time
  62. {
  63. struct timespec current;
  64. };
  65. /* Return the current time. Terminates the process if the current
  66. time is not available. */
  67. struct deadline_current_time __deadline_current_time (void) attribute_hidden;
  68. /* Computed absolute deadline. */
  69. struct deadline
  70. {
  71. struct timespec absolute;
  72. };
  73. /* For internal use only. */
  74. static inline bool
  75. __deadline_is_infinite (struct deadline deadline)
  76. {
  77. return deadline.absolute.tv_nsec < 0;
  78. }
  79. /* Return true if the current time is at the deadline or past it. */
  80. static inline bool
  81. __deadline_elapsed (struct deadline_current_time current,
  82. struct deadline deadline)
  83. {
  84. return !__deadline_is_infinite (deadline)
  85. && (current.current.tv_sec > deadline.absolute.tv_sec
  86. || (current.current.tv_sec == deadline.absolute.tv_sec
  87. && current.current.tv_nsec >= deadline.absolute.tv_nsec));
  88. }
  89. /* Return the deadline which occurs first. */
  90. static inline struct deadline
  91. __deadline_first (struct deadline left, struct deadline right)
  92. {
  93. if (__deadline_is_infinite (right)
  94. || left.absolute.tv_sec < right.absolute.tv_sec
  95. || (left.absolute.tv_sec == right.absolute.tv_sec
  96. && left.absolute.tv_nsec < right.absolute.tv_nsec))
  97. return left;
  98. else
  99. return right;
  100. }
  101. /* Add TV to the current time and return it. Returns a special
  102. infinite absolute deadline on overflow. */
  103. struct deadline __deadline_from_timeval (struct deadline_current_time,
  104. struct timeval tv) attribute_hidden;
  105. /* Compute the number of milliseconds until the specified deadline,
  106. from the current time in the argument. The result is mainly for
  107. use with poll. If the deadline has already passed, return 0. If
  108. the result would overflow an int, return INT_MAX. */
  109. int __deadline_to_ms (struct deadline_current_time, struct deadline)
  110. attribute_hidden;
  111. /* Return true if TV.tv_sec is non-negative and TV.tv_usec is in the
  112. interval [0, 999999]. */
  113. static inline bool
  114. __is_timeval_valid_timeout (struct timeval tv)
  115. {
  116. return tv.tv_sec >= 0 && tv.tv_usec >= 0 && tv.tv_usec < 1000 * 1000;
  117. }
  118. #endif /* _NET_INTERNAL_H */