1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * Copyright (C) 2001,2005 by the Massachusetts Institute of Technology, 4 * Cambridge, MA, USA. All Rights Reserved. 5 * 6 * This software is being provided to you, the LICENSEE, by the 7 * Massachusetts Institute of Technology (M.I.T.) under the following 8 * license. By obtaining, using and/or copying this software, you agree 9 * that you have read, understood, and will comply with these terms and 10 * conditions: 11 * 12 * Export of this software from the United States of America may 13 * require a specific license from the United States Government. 14 * It is the responsibility of any person or organization contemplating 15 * export to obtain such a license before exporting. 16 * 17 * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute 18 * this software and its documentation for any purpose and without fee or 19 * royalty is hereby granted, provided that you agree to comply with the 20 * following copyright notice and statements, including the disclaimer, and 21 * that the same appear on ALL copies of the software and documentation, 22 * including modifications that you make for internal use or for 23 * distribution: 24 * 25 * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS 26 * OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not 27 * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF 28 * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF 29 * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY 30 * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. 31 * 32 * The name of the Massachusetts Institute of Technology or M.I.T. may NOT 33 * be used in advertising or publicity pertaining to distribution of the 34 * software. Title to copyright in this software and any associated 35 * documentation shall at all times remain with M.I.T., and USER agrees to 36 * preserve same. 37 * 38 * Furthermore if you modify this software you must label 39 * your software as modified software and not distribute it in such a 40 * fashion that it might be confused with the original M.I.T. software. 41 */ 42 43 #ifndef SOCKET_UTILS_H 44 #define SOCKET_UTILS_H 45 46 #include <stdbool.h> 47 48 /* Some useful stuff cross-platform for manipulating socket addresses. 49 We assume at least ipv4 sockaddr_in support. The sockaddr_storage 50 stuff comes from the ipv6 socket api enhancements; socklen_t is 51 provided on some systems; the rest is just convenience for internal 52 use in the krb5 tree. 53 54 Do NOT install this file. */ 55 56 /* for HAVE_SOCKLEN_T etc */ 57 #include "autoconf.h" 58 /* for sockaddr_storage */ 59 #include "port-sockets.h" 60 /* for "inline" if needed */ 61 #include "k5-platform.h" 62 63 /* 64 * There's a lot of confusion between pointers to different sockaddr 65 * types, and pointers with different degrees of indirection, as in 66 * the locate_kdc type functions. Use these function to ensure we 67 * don't do something silly like cast a "sockaddr **" to a 68 * "sockaddr_in *". 69 * 70 * The casts to (void *) are to get GCC to shut up about alignment 71 * increasing. We assume that struct sockaddr pointers are generally 72 * read-only; there are a few exceptions, but they all go through 73 * sa_setport(). 74 */ 75 static inline const struct sockaddr_in *sa2sin(const struct sockaddr *sa) 76 { 77 return (const struct sockaddr_in *)(void *)sa; 78 } 79 static inline const struct sockaddr_in6 *sa2sin6(const struct sockaddr *sa) 80 { 81 return (const struct sockaddr_in6 *)(void *)sa; 82 } 83 static inline struct sockaddr *ss2sa (struct sockaddr_storage *ss) 84 { 85 return (struct sockaddr *) ss; 86 } 87 static inline struct sockaddr_in *ss2sin (struct sockaddr_storage *ss) 88 { 89 return (struct sockaddr_in *) ss; 90 } 91 static inline struct sockaddr_in6 *ss2sin6 (struct sockaddr_storage *ss) 92 { 93 return (struct sockaddr_in6 *) ss; 94 } 95 #ifndef _WIN32 96 static inline const struct sockaddr_un *sa2sun(const struct sockaddr *sa) 97 { 98 return (const struct sockaddr_un *)(void *)sa; 99 } 100 static inline struct sockaddr_un *ss2sun(struct sockaddr_storage *ss) 101 { 102 return (struct sockaddr_un *)ss; 103 } 104 #endif 105 106 /* Set the IPv4 or IPv6 port on sa to port. Do nothing if sa is not an 107 * Internet socket. */ 108 static inline void 109 sa_setport(struct sockaddr *sa, uint16_t port) 110 { 111 if (sa->sa_family == AF_INET) 112 ((struct sockaddr_in *)sa2sin(sa))->sin_port = htons(port); 113 else if (sa->sa_family == AF_INET6) 114 ((struct sockaddr_in6 *)sa2sin6(sa))->sin6_port = htons(port); 115 } 116 117 /* Get the Internet port number of sa, or 0 if it is not an Internet socket. */ 118 static inline uint16_t 119 sa_getport(const struct sockaddr *sa) 120 { 121 if (sa->sa_family == AF_INET) 122 return ntohs(sa2sin(sa)->sin_port); 123 else if (sa->sa_family == AF_INET6) 124 return ntohs(sa2sin6(sa)->sin6_port); 125 else 126 return 0; 127 } 128 129 /* Return true if sa is an IPv4 or IPv6 socket address. */ 130 static inline int 131 sa_is_inet(const struct sockaddr *sa) 132 { 133 return sa->sa_family == AF_INET || sa->sa_family == AF_INET6; 134 } 135 136 /* Return true if sa is an IPv4 or IPv6 wildcard address. */ 137 static inline int 138 sa_is_wildcard(const struct sockaddr *sa) 139 { 140 if (sa->sa_family == AF_INET6) 141 return IN6_IS_ADDR_UNSPECIFIED(&sa2sin6(sa)->sin6_addr); 142 else if (sa->sa_family == AF_INET) 143 return sa2sin(sa)->sin_addr.s_addr == INADDR_ANY; 144 return 0; 145 } 146 147 /* Return the length of an IPv4 or IPv6 socket structure; abort if it is 148 * neither. */ 149 static inline socklen_t 150 sa_socklen(const struct sockaddr *sa) 151 { 152 if (sa->sa_family == AF_INET6) 153 return sizeof(struct sockaddr_in6); 154 else if (sa->sa_family == AF_INET) 155 return sizeof(struct sockaddr_in); 156 #ifndef _WIN32 157 else if (sa->sa_family == AF_UNIX) 158 return sizeof(struct sockaddr_un); 159 #endif 160 else 161 abort(); 162 } 163 164 /* Return true if a and b are the same address (and port if applicable). */ 165 static inline bool 166 sa_equal(const struct sockaddr *a, const struct sockaddr *b) 167 { 168 if (a == NULL || b == NULL || a->sa_family != b->sa_family) 169 return false; 170 171 if (a->sa_family == AF_INET) { 172 const struct sockaddr_in *x = sa2sin(a); 173 const struct sockaddr_in *y = sa2sin(b); 174 175 if (x->sin_port != y->sin_port) 176 return false; 177 return memcmp(&x->sin_addr, &y->sin_addr, sizeof(x->sin_addr)) == 0; 178 } else if (a->sa_family == AF_INET6) { 179 const struct sockaddr_in6 *x = sa2sin6(a); 180 const struct sockaddr_in6 *y = sa2sin6(b); 181 182 if (x->sin6_port != y->sin6_port) 183 return false; 184 return memcmp(&x->sin6_addr, &y->sin6_addr, sizeof(x->sin6_addr)) == 0; 185 #ifndef _WIN32 186 } else if (a->sa_family == AF_UNIX) { 187 const struct sockaddr_un *x = sa2sun(a); 188 const struct sockaddr_un *y = sa2sun(b); 189 190 return strcmp(x->sun_path, y->sun_path) == 0; 191 #endif 192 } 193 194 return false; 195 } 196 197 #endif /* SOCKET_UTILS_H */ 198