Path: blob/main/crypto/krb5/src/include/socket-utils.h
34878 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright (C) 2001,2005 by the Massachusetts Institute of Technology,3* Cambridge, MA, USA. All Rights Reserved.4*5* This software is being provided to you, the LICENSEE, by the6* Massachusetts Institute of Technology (M.I.T.) under the following7* license. By obtaining, using and/or copying this software, you agree8* that you have read, understood, and will comply with these terms and9* conditions:10*11* Export of this software from the United States of America may12* require a specific license from the United States Government.13* It is the responsibility of any person or organization contemplating14* export to obtain such a license before exporting.15*16* WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute17* this software and its documentation for any purpose and without fee or18* royalty is hereby granted, provided that you agree to comply with the19* following copyright notice and statements, including the disclaimer, and20* that the same appear on ALL copies of the software and documentation,21* including modifications that you make for internal use or for22* distribution:23*24* THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS25* OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not26* limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF27* MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF28* THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY29* PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.30*31* The name of the Massachusetts Institute of Technology or M.I.T. may NOT32* be used in advertising or publicity pertaining to distribution of the33* software. Title to copyright in this software and any associated34* documentation shall at all times remain with M.I.T., and USER agrees to35* preserve same.36*37* Furthermore if you modify this software you must label38* your software as modified software and not distribute it in such a39* fashion that it might be confused with the original M.I.T. software.40*/4142#ifndef SOCKET_UTILS_H43#define SOCKET_UTILS_H4445#include <stdbool.h>4647/* Some useful stuff cross-platform for manipulating socket addresses.48We assume at least ipv4 sockaddr_in support. The sockaddr_storage49stuff comes from the ipv6 socket api enhancements; socklen_t is50provided on some systems; the rest is just convenience for internal51use in the krb5 tree.5253Do NOT install this file. */5455/* for HAVE_SOCKLEN_T etc */56#include "autoconf.h"57/* for sockaddr_storage */58#include "port-sockets.h"59/* for "inline" if needed */60#include "k5-platform.h"6162/*63* There's a lot of confusion between pointers to different sockaddr64* types, and pointers with different degrees of indirection, as in65* the locate_kdc type functions. Use these function to ensure we66* don't do something silly like cast a "sockaddr **" to a67* "sockaddr_in *".68*69* The casts to (void *) are to get GCC to shut up about alignment70* increasing. We assume that struct sockaddr pointers are generally71* read-only; there are a few exceptions, but they all go through72* sa_setport().73*/74static inline const struct sockaddr_in *sa2sin(const struct sockaddr *sa)75{76return (const struct sockaddr_in *)(void *)sa;77}78static inline const struct sockaddr_in6 *sa2sin6(const struct sockaddr *sa)79{80return (const struct sockaddr_in6 *)(void *)sa;81}82static inline struct sockaddr *ss2sa (struct sockaddr_storage *ss)83{84return (struct sockaddr *) ss;85}86static inline struct sockaddr_in *ss2sin (struct sockaddr_storage *ss)87{88return (struct sockaddr_in *) ss;89}90static inline struct sockaddr_in6 *ss2sin6 (struct sockaddr_storage *ss)91{92return (struct sockaddr_in6 *) ss;93}94#ifndef _WIN3295static inline const struct sockaddr_un *sa2sun(const struct sockaddr *sa)96{97return (const struct sockaddr_un *)(void *)sa;98}99static inline struct sockaddr_un *ss2sun(struct sockaddr_storage *ss)100{101return (struct sockaddr_un *)ss;102}103#endif104105/* Set the IPv4 or IPv6 port on sa to port. Do nothing if sa is not an106* Internet socket. */107static inline void108sa_setport(struct sockaddr *sa, uint16_t port)109{110if (sa->sa_family == AF_INET)111((struct sockaddr_in *)sa2sin(sa))->sin_port = htons(port);112else if (sa->sa_family == AF_INET6)113((struct sockaddr_in6 *)sa2sin6(sa))->sin6_port = htons(port);114}115116/* Get the Internet port number of sa, or 0 if it is not an Internet socket. */117static inline uint16_t118sa_getport(const struct sockaddr *sa)119{120if (sa->sa_family == AF_INET)121return ntohs(sa2sin(sa)->sin_port);122else if (sa->sa_family == AF_INET6)123return ntohs(sa2sin6(sa)->sin6_port);124else125return 0;126}127128/* Return true if sa is an IPv4 or IPv6 socket address. */129static inline int130sa_is_inet(const struct sockaddr *sa)131{132return sa->sa_family == AF_INET || sa->sa_family == AF_INET6;133}134135/* Return true if sa is an IPv4 or IPv6 wildcard address. */136static inline int137sa_is_wildcard(const struct sockaddr *sa)138{139if (sa->sa_family == AF_INET6)140return IN6_IS_ADDR_UNSPECIFIED(&sa2sin6(sa)->sin6_addr);141else if (sa->sa_family == AF_INET)142return sa2sin(sa)->sin_addr.s_addr == INADDR_ANY;143return 0;144}145146/* Return the length of an IPv4 or IPv6 socket structure; abort if it is147* neither. */148static inline socklen_t149sa_socklen(const struct sockaddr *sa)150{151if (sa->sa_family == AF_INET6)152return sizeof(struct sockaddr_in6);153else if (sa->sa_family == AF_INET)154return sizeof(struct sockaddr_in);155#ifndef _WIN32156else if (sa->sa_family == AF_UNIX)157return sizeof(struct sockaddr_un);158#endif159else160abort();161}162163/* Return true if a and b are the same address (and port if applicable). */164static inline bool165sa_equal(const struct sockaddr *a, const struct sockaddr *b)166{167if (a == NULL || b == NULL || a->sa_family != b->sa_family)168return false;169170if (a->sa_family == AF_INET) {171const struct sockaddr_in *x = sa2sin(a);172const struct sockaddr_in *y = sa2sin(b);173174if (x->sin_port != y->sin_port)175return false;176return memcmp(&x->sin_addr, &y->sin_addr, sizeof(x->sin_addr)) == 0;177} else if (a->sa_family == AF_INET6) {178const struct sockaddr_in6 *x = sa2sin6(a);179const struct sockaddr_in6 *y = sa2sin6(b);180181if (x->sin6_port != y->sin6_port)182return false;183return memcmp(&x->sin6_addr, &y->sin6_addr, sizeof(x->sin6_addr)) == 0;184#ifndef _WIN32185} else if (a->sa_family == AF_UNIX) {186const struct sockaddr_un *x = sa2sun(a);187const struct sockaddr_un *y = sa2sun(b);188189return strcmp(x->sun_path, y->sun_path) == 0;190#endif191}192193return false;194}195196#endif /* SOCKET_UTILS_H */197198199