Blame inet/net-internal.h

Packit 6c4009
/* Network-related functions for internal library use.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _NET_INTERNAL_H
Packit 6c4009
#define _NET_INTERNAL_H 1
Packit 6c4009
Packit 6c4009
#include <arpa/inet.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
Packit 6c4009
int __inet6_scopeid_pton (const struct in6_addr *address,
Packit 6c4009
                          const char *scope, uint32_t *result);
Packit 6c4009
libc_hidden_proto (__inet6_scopeid_pton)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* IDNA conversion.  These functions convert domain names between the
Packit 6c4009
   current multi-byte character set and the IDNA encoding.  On
Packit 6c4009
   success, the result string is written to *RESULT (which the caller
Packit 6c4009
   has to free), and zero is returned.  On error, an EAI_* error code
Packit 6c4009
   is returned (see <netdb.h>), and *RESULT is not changed.  */
Packit 6c4009
int __idna_to_dns_encoding (const char *name, char **result);
Packit 6c4009
libc_hidden_proto (__idna_to_dns_encoding)
Packit 6c4009
int __idna_from_dns_encoding (const char *name, char **result);
Packit 6c4009
libc_hidden_proto (__idna_from_dns_encoding)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Return value of __idna_name_classify below.  */
Packit 6c4009
enum idna_name_classification
Packit 6c4009
{
Packit 6c4009
  idna_name_ascii,          /* No non-ASCII characters.  */
Packit 6c4009
  idna_name_nonascii,       /* Non-ASCII characters, no backslash.  */
Packit 6c4009
  idna_name_nonascii_backslash, /* Non-ASCII characters with backslash.  */
Packit 6c4009
  idna_name_encoding_error, /* Decoding error.  */
Packit 6c4009
  idna_name_memory_error,   /* Memory allocation failure.  */
Packit 6c4009
  idna_name_error,          /* Other error during decoding.  Check errno.  */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Check the specified name for non-ASCII characters and backslashes
Packit 6c4009
   or encoding errors.  */
Packit 6c4009
enum idna_name_classification __idna_name_classify (const char *name)
Packit 6c4009
  attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Deadline handling for enforcing timeouts.
Packit 6c4009
Packit 6c4009
   Code should call __deadline_current_time to obtain the current time
Packit 6c4009
   and cache it locally.  The cache needs updating after every
Packit 6c4009
   long-running or potentially blocking operation.  Deadlines relative
Packit 6c4009
   to the current time can be computed using __deadline_from_timeval.
Packit 6c4009
   The deadlines may have to be recomputed in response to certain
Packit 6c4009
   events (such as an incoming packet), but they are absolute (not
Packit 6c4009
   relative to the current time).  A timeout suitable for use with the
Packit 6c4009
   poll function can be computed from such a deadline using
Packit 6c4009
   __deadline_to_ms.
Packit 6c4009
Packit 6c4009
   The fields in the structs defined belowed should only be used
Packit 6c4009
   within the implementation.  */
Packit 6c4009
Packit 6c4009
/* Cache of the current time.  Used to compute deadlines from relative
Packit 6c4009
   timeouts and vice versa.  */
Packit 6c4009
struct deadline_current_time
Packit 6c4009
{
Packit 6c4009
  struct timespec current;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Return the current time.  Terminates the process if the current
Packit 6c4009
   time is not available.  */
Packit 6c4009
struct deadline_current_time __deadline_current_time (void) attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Computed absolute deadline.  */
Packit 6c4009
struct deadline
Packit 6c4009
{
Packit 6c4009
  struct timespec absolute;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* For internal use only.  */
Packit 6c4009
static inline bool
Packit 6c4009
__deadline_is_infinite (struct deadline deadline)
Packit 6c4009
{
Packit 6c4009
  return deadline.absolute.tv_nsec < 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return true if the current time is at the deadline or past it.  */
Packit 6c4009
static inline bool
Packit 6c4009
__deadline_elapsed (struct deadline_current_time current,
Packit 6c4009
                    struct deadline deadline)
Packit 6c4009
{
Packit 6c4009
  return !__deadline_is_infinite (deadline)
Packit 6c4009
    && (current.current.tv_sec > deadline.absolute.tv_sec
Packit 6c4009
        || (current.current.tv_sec == deadline.absolute.tv_sec
Packit 6c4009
            && current.current.tv_nsec >= deadline.absolute.tv_nsec));
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return the deadline which occurs first.  */
Packit 6c4009
static inline struct deadline
Packit 6c4009
__deadline_first (struct deadline left, struct deadline right)
Packit 6c4009
{
Packit 6c4009
  if (__deadline_is_infinite (right)
Packit 6c4009
      || left.absolute.tv_sec < right.absolute.tv_sec
Packit 6c4009
      || (left.absolute.tv_sec == right.absolute.tv_sec
Packit 6c4009
          && left.absolute.tv_nsec < right.absolute.tv_nsec))
Packit 6c4009
    return left;
Packit 6c4009
  else
Packit 6c4009
    return right;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Add TV to the current time and return it.  Returns a special
Packit 6c4009
   infinite absolute deadline on overflow.  */
Packit 6c4009
struct deadline __deadline_from_timeval (struct deadline_current_time,
Packit 6c4009
                                         struct timeval tv) attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Compute the number of milliseconds until the specified deadline,
Packit 6c4009
   from the current time in the argument.  The result is mainly for
Packit 6c4009
   use with poll.  If the deadline has already passed, return 0.  If
Packit 6c4009
   the result would overflow an int, return INT_MAX.  */
Packit 6c4009
int __deadline_to_ms (struct deadline_current_time, struct deadline)
Packit 6c4009
  attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Return true if TV.tv_sec is non-negative and TV.tv_usec is in the
Packit 6c4009
   interval [0, 999999].  */
Packit 6c4009
static inline bool
Packit 6c4009
__is_timeval_valid_timeout (struct timeval tv)
Packit 6c4009
{
Packit 6c4009
  return tv.tv_sec >= 0 && tv.tv_usec >= 0 && tv.tv_usec < 1000 * 1000;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* _NET_INTERNAL_H */