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