Blame src/include/socket-utils.h

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2001,2005 by the Massachusetts Institute of Technology,
Packit fd8b60
 * Cambridge, MA, USA.  All Rights Reserved.
Packit fd8b60
 *
Packit fd8b60
 * This software is being provided to you, the LICENSEE, by the
Packit fd8b60
 * Massachusetts Institute of Technology (M.I.T.) under the following
Packit fd8b60
 * license.  By obtaining, using and/or copying this software, you agree
Packit fd8b60
 * that you have read, understood, and will comply with these terms and
Packit fd8b60
 * conditions:
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 * require a specific license from the United States Government.
Packit fd8b60
 * It is the responsibility of any person or organization contemplating
Packit fd8b60
 * export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
Packit fd8b60
 * this software and its documentation for any purpose and without fee or
Packit fd8b60
 * royalty is hereby granted, provided that you agree to comply with the
Packit fd8b60
 * following copyright notice and statements, including the disclaimer, and
Packit fd8b60
 * that the same appear on ALL copies of the software and documentation,
Packit fd8b60
 * including modifications that you make for internal use or for
Packit fd8b60
 * distribution:
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
Packit fd8b60
 * OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
Packit fd8b60
 * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
Packit fd8b60
 * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
Packit fd8b60
 * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
Packit fd8b60
 * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
Packit fd8b60
 *
Packit fd8b60
 * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
Packit fd8b60
 * be used in advertising or publicity pertaining to distribution of the
Packit fd8b60
 * software.  Title to copyright in this software and any associated
Packit fd8b60
 * documentation shall at all times remain with M.I.T., and USER agrees to
Packit fd8b60
 * preserve same.
Packit fd8b60
 *
Packit fd8b60
 * Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#ifndef SOCKET_UTILS_H
Packit fd8b60
#define SOCKET_UTILS_H
Packit fd8b60
Packit fd8b60
/* Some useful stuff cross-platform for manipulating socket addresses.
Packit fd8b60
   We assume at least ipv4 sockaddr_in support.  The sockaddr_storage
Packit fd8b60
   stuff comes from the ipv6 socket api enhancements; socklen_t is
Packit fd8b60
   provided on some systems; the rest is just convenience for internal
Packit fd8b60
   use in the krb5 tree.
Packit fd8b60
Packit fd8b60
   Do NOT install this file.  */
Packit fd8b60
Packit fd8b60
/* for HAVE_SOCKLEN_T etc */
Packit fd8b60
#include "autoconf.h"
Packit fd8b60
/* for sockaddr_storage */
Packit fd8b60
#include "port-sockets.h"
Packit fd8b60
/* for "inline" if needed */
Packit fd8b60
#include "k5-platform.h"
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * There's a lot of confusion between pointers to different sockaddr
Packit fd8b60
 * types, and pointers with different degrees of indirection, as in
Packit fd8b60
 * the locate_kdc type functions.  Use these function to ensure we
Packit fd8b60
 * don't do something silly like cast a "sockaddr **" to a
Packit fd8b60
 * "sockaddr_in *".
Packit fd8b60
 *
Packit fd8b60
 * The casts to (void *) are to get GCC to shut up about alignment
Packit fd8b60
 * increasing.
Packit fd8b60
 */
Packit fd8b60
static inline struct sockaddr_in *sa2sin (struct sockaddr *sa)
Packit fd8b60
{
Packit fd8b60
    return (struct sockaddr_in *) (void *) sa;
Packit fd8b60
}
Packit fd8b60
static inline struct sockaddr_in6 *sa2sin6 (struct sockaddr *sa)
Packit fd8b60
{
Packit fd8b60
    return (struct sockaddr_in6 *) (void *) sa;
Packit fd8b60
}
Packit fd8b60
static inline struct sockaddr *ss2sa (struct sockaddr_storage *ss)
Packit fd8b60
{
Packit fd8b60
    return (struct sockaddr *) ss;
Packit fd8b60
}
Packit fd8b60
static inline struct sockaddr_in *ss2sin (struct sockaddr_storage *ss)
Packit fd8b60
{
Packit fd8b60
    return (struct sockaddr_in *) ss;
Packit fd8b60
}
Packit fd8b60
static inline struct sockaddr_in6 *ss2sin6 (struct sockaddr_storage *ss)
Packit fd8b60
{
Packit fd8b60
    return (struct sockaddr_in6 *) ss;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Set the IPv4 or IPv6 port on sa to port.  Do nothing if sa is not an
Packit fd8b60
 * Internet socket. */
Packit fd8b60
static inline void
Packit fd8b60
sa_setport(struct sockaddr *sa, uint16_t port)
Packit fd8b60
{
Packit fd8b60
    if (sa->sa_family == AF_INET)
Packit fd8b60
        sa2sin(sa)->sin_port = htons(port);
Packit fd8b60
    else if (sa->sa_family == AF_INET6)
Packit fd8b60
        sa2sin6(sa)->sin6_port = htons(port);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Get the Internet port number of sa, or 0 if it is not an Internet socket. */
Packit fd8b60
static inline uint16_t
Packit fd8b60
sa_getport(struct sockaddr *sa)
Packit fd8b60
{
Packit fd8b60
    if (sa->sa_family == AF_INET)
Packit fd8b60
        return ntohs(sa2sin(sa)->sin_port);
Packit fd8b60
    else if (sa->sa_family == AF_INET6)
Packit fd8b60
        return ntohs(sa2sin6(sa)->sin6_port);
Packit fd8b60
    else
Packit fd8b60
        return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Return true if sa is an IPv4 or IPv6 socket address. */
Packit fd8b60
static inline int
Packit fd8b60
sa_is_inet(struct sockaddr *sa)
Packit fd8b60
{
Packit fd8b60
    return sa->sa_family == AF_INET || sa->sa_family == AF_INET6;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Return true if sa is an IPv4 or IPv6 wildcard address. */
Packit fd8b60
static inline int
Packit fd8b60
sa_is_wildcard(struct sockaddr *sa)
Packit fd8b60
{
Packit fd8b60
    if (sa->sa_family == AF_INET6)
Packit fd8b60
        return IN6_IS_ADDR_UNSPECIFIED(&sa2sin6(sa)->sin6_addr);
Packit fd8b60
    else if (sa->sa_family == AF_INET)
Packit fd8b60
        return sa2sin(sa)->sin_addr.s_addr == INADDR_ANY;
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Return the length of an IPv4 or IPv6 socket structure; abort if it is
Packit fd8b60
 * neither. */
Packit fd8b60
static inline socklen_t
Packit fd8b60
sa_socklen(struct sockaddr *sa)
Packit fd8b60
{
Packit fd8b60
    if (sa->sa_family == AF_INET6)
Packit fd8b60
        return sizeof(struct sockaddr_in6);
Packit fd8b60
    else if (sa->sa_family == AF_INET)
Packit fd8b60
        return sizeof(struct sockaddr_in);
Packit fd8b60
    else
Packit fd8b60
        abort();
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
#endif /* SOCKET_UTILS_H */