S390: Optimize atomic macros.
[glibc.git] / inet / net-internal.h
blob2b2632c7ba845c7b3ea6d735f7e7451728f5c5b2
1 /* Network-related functions for internal library use.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #ifndef _NET_INTERNAL_H
20 #define _NET_INTERNAL_H 1
22 #include <arpa/inet.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <sys/time.h>
27 int __inet6_scopeid_pton (const struct in6_addr *address,
28 const char *scope, uint32_t *result)
29 internal_function attribute_hidden;
30 libc_hidden_proto (__inet6_scopeid_pton)
33 /* Deadline handling for enforcing timeouts.
35 Code should call __deadline_current_time to obtain the current time
36 and cache it locally. The cache needs updating after every
37 long-running or potentially blocking operation. Deadlines relative
38 to the current time can be computed using __deadline_from_timeval.
39 The deadlines may have to be recomputed in response to certain
40 events (such as an incoming packet), but they are absolute (not
41 relative to the current time). A timeout suitable for use with the
42 poll function can be computed from such a deadline using
43 __deadline_to_ms.
45 The fields in the structs defined belowed should only be used
46 within the implementation. */
48 /* Cache of the current time. Used to compute deadlines from relative
49 timeouts and vice versa. */
50 struct deadline_current_time
52 struct timespec current;
55 /* Return the current time. Terminates the process if the current
56 time is not available. */
57 struct deadline_current_time __deadline_current_time (void)
58 internal_function attribute_hidden;
60 /* Computed absolute deadline. */
61 struct deadline
63 struct timespec absolute;
67 /* For internal use only. */
68 static inline bool
69 __deadline_is_infinite (struct deadline deadline)
71 return deadline.absolute.tv_nsec < 0;
74 /* Return true if the current time is at the deadline or past it. */
75 static inline bool
76 __deadline_elapsed (struct deadline_current_time current,
77 struct deadline deadline)
79 return !__deadline_is_infinite (deadline)
80 && (current.current.tv_sec > deadline.absolute.tv_sec
81 || (current.current.tv_sec == deadline.absolute.tv_sec
82 && current.current.tv_nsec >= deadline.absolute.tv_nsec));
85 /* Return the deadline which occurs first. */
86 static inline struct deadline
87 __deadline_first (struct deadline left, struct deadline right)
89 if (__deadline_is_infinite (right)
90 || left.absolute.tv_sec < right.absolute.tv_sec
91 || (left.absolute.tv_sec == right.absolute.tv_sec
92 && left.absolute.tv_nsec < right.absolute.tv_nsec))
93 return left;
94 else
95 return right;
98 /* Add TV to the current time and return it. Returns a special
99 infinite absolute deadline on overflow. */
100 struct deadline __deadline_from_timeval (struct deadline_current_time,
101 struct timeval tv)
102 internal_function attribute_hidden;
104 /* Compute the number of milliseconds until the specified deadline,
105 from the current time in the argument. The result is mainly for
106 use with poll. If the deadline has already passed, return 0. If
107 the result would overflow an int, return INT_MAX. */
108 int __deadline_to_ms (struct deadline_current_time, struct deadline)
109 internal_function 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)
116 return tv.tv_sec >= 0 && tv.tv_usec >= 0 && tv.tv_usec < 1000 * 1000;
119 #endif /* _NET_INTERNAL_H */