1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * Copyright (C) 2001,2005 by the Massachusetts Institute of Technology,
4 * Cambridge, MA, USA. All Rights Reserved.
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
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.
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
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.
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
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.
43 #ifndef SOCKET_UTILS_H
44 #define SOCKET_UTILS_H
46 /* Some useful stuff cross-platform for manipulating socket addresses.
47 We assume at least ipv4 sockaddr_in support. The sockaddr_storage
48 stuff comes from the ipv6 socket api enhancements; socklen_t is
49 provided on some systems; the rest is just convenience for internal
52 Do NOT install this file. */
54 /* for HAVE_SOCKLEN_T etc */
56 /* for sockaddr_storage */
57 #include "port-sockets.h"
58 /* for "inline" if needed */
59 #include "k5-platform.h"
62 * There's a lot of confusion between pointers to different sockaddr
63 * types, and pointers with different degrees of indirection, as in
64 * the locate_kdc type functions. Use these function to ensure we
65 * don't do something silly like cast a "sockaddr **" to a
68 * The casts to (void *) are to get GCC to shut up about alignment
71 static inline struct sockaddr_in *sa2sin (struct sockaddr *sa)
73 return (struct sockaddr_in *) (void *) sa;
75 static inline struct sockaddr_in6 *sa2sin6 (struct sockaddr *sa)
77 return (struct sockaddr_in6 *) (void *) sa;
79 static inline struct sockaddr *ss2sa (struct sockaddr_storage *ss)
81 return (struct sockaddr *) ss;
83 static inline struct sockaddr_in *ss2sin (struct sockaddr_storage *ss)
85 return (struct sockaddr_in *) ss;
87 static inline struct sockaddr_in6 *ss2sin6 (struct sockaddr_storage *ss)
89 return (struct sockaddr_in6 *) ss;
92 /* Set the IPv4 or IPv6 port on sa to port. Do nothing if sa is not an
95 sa_setport(struct sockaddr *sa, uint16_t port)
97 if (sa->sa_family == AF_INET)
98 sa2sin(sa)->sin_port = htons(port);
99 else if (sa->sa_family == AF_INET6)
100 sa2sin6(sa)->sin6_port = htons(port);
103 /* Get the Internet port number of sa, or 0 if it is not an Internet socket. */
104 static inline uint16_t
105 sa_getport(struct sockaddr *sa)
107 if (sa->sa_family == AF_INET)
108 return ntohs(sa2sin(sa)->sin_port);
109 else if (sa->sa_family == AF_INET6)
110 return ntohs(sa2sin6(sa)->sin6_port);
115 /* Return true if sa is an IPv4 or IPv6 socket address. */
117 sa_is_inet(struct sockaddr *sa)
119 return sa->sa_family == AF_INET || sa->sa_family == AF_INET6;
122 /* Return true if sa is an IPv4 or IPv6 wildcard address. */
124 sa_is_wildcard(struct sockaddr *sa)
126 if (sa->sa_family == AF_INET6)
127 return IN6_IS_ADDR_UNSPECIFIED(&sa2sin6(sa)->sin6_addr);
128 else if (sa->sa_family == AF_INET)
129 return sa2sin(sa)->sin_addr.s_addr == INADDR_ANY;
133 /* Return the length of an IPv4 or IPv6 socket structure; abort if it is
135 static inline socklen_t
136 sa_socklen(struct sockaddr *sa)
138 if (sa->sa_family == AF_INET6)
139 return sizeof(struct sockaddr_in6);
140 else if (sa->sa_family == AF_INET)
141 return sizeof(struct sockaddr_in);
146 #endif /* SOCKET_UTILS_H */