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 the
6 * Massachusetts Institute of Technology (M.I.T.) under the following
7 * license. By obtaining, using and/or copying this software, you agree
8 * that you have read, understood, and will comply with these terms and
9 * conditions:
10 *
11 * Export of this software from the United States of America may
12 * require a specific license from the United States Government.
13 * It is the responsibility of any person or organization contemplating
14 * export to obtain such a license before exporting.
15 *
16 * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
17 * this software and its documentation for any purpose and without fee or
18 * royalty is hereby granted, provided that you agree to comply with the
19 * following copyright notice and statements, including the disclaimer, and
20 * that the same appear on ALL copies of the software and documentation,
21 * including modifications that you make for internal use or for
22 * distribution:
23 *
24 * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
25 * OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not
26 * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
27 * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
28 * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
29 * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
30 *
31 * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
32 * be used in advertising or publicity pertaining to distribution of the
33 * software. Title to copyright in this software and any associated
34 * documentation shall at all times remain with M.I.T., and USER agrees to
35 * preserve same.
36 *
37 * Furthermore if you modify this software you must label
38 * your software as modified software and not distribute it in such a
39 * fashion that it might be confused with the original M.I.T. software.
40 */
41
42 #ifndef SOCKET_UTILS_H
43 #define SOCKET_UTILS_H
44
45 /* Some useful stuff cross-platform for manipulating socket addresses.
46 We assume at least ipv4 sockaddr_in support. The sockaddr_storage
47 stuff comes from the ipv6 socket api enhancements; socklen_t is
48 provided on some systems; the rest is just convenience for internal
49 use in the krb5 tree.
50
51 Do NOT install this file. */
52
53 /* for HAVE_SOCKLEN_T, KRB5_USE_INET6, etc */
54 #include "autoconf.h"
55 /* for sockaddr_storage */
56 #include "port-sockets.h"
57 /* for "inline" if needed */
58 #include "k5-platform.h"
59
60 /*
61 * There's a lot of confusion between pointers to different sockaddr
62 * types, and pointers with different degrees of indirection, as in
63 * the locate_kdc type functions. Use these function to ensure we
64 * don't do something silly like cast a "sockaddr **" to a
65 * "sockaddr_in *".
66 *
67 * The casts to (void *) are to get GCC to shut up about alignment
68 * increasing.
69 */
sa2sin(struct sockaddr * sa)70 static inline struct sockaddr_in *sa2sin (struct sockaddr *sa)
71 {
72 return (struct sockaddr_in *) (void *) sa;
73 }
74 #ifdef KRB5_USE_INET6
sa2sin6(struct sockaddr * sa)75 static inline struct sockaddr_in6 *sa2sin6 (struct sockaddr *sa)
76 {
77 return (struct sockaddr_in6 *) (void *) sa;
78 }
79 #endif
ss2sa(struct sockaddr_storage * ss)80 static inline struct sockaddr *ss2sa (struct sockaddr_storage *ss)
81 {
82 return (struct sockaddr *) ss;
83 }
ss2sin(struct sockaddr_storage * ss)84 static inline struct sockaddr_in *ss2sin (struct sockaddr_storage *ss)
85 {
86 return (struct sockaddr_in *) ss;
87 }
88 #ifdef KRB5_USE_INET6
ss2sin6(struct sockaddr_storage * ss)89 static inline struct sockaddr_in6 *ss2sin6 (struct sockaddr_storage *ss)
90 {
91 return (struct sockaddr_in6 *) ss;
92 }
93 #endif
94
95 #if !defined (socklen)
96 /* socklen_t socklen (struct sockaddr *) */
97 # ifdef HAVE_SA_LEN
98 # define socklen(X) ((X)->sa_len)
99 # else
100 # ifdef KRB5_USE_INET6
101 # define socklen(X) ((X)->sa_family == AF_INET6 ? (socklen_t) sizeof (struct sockaddr_in6) : (X)->sa_family == AF_INET ? (socklen_t) sizeof (struct sockaddr_in) : (socklen_t) sizeof (struct sockaddr))
102 # else
103 # define socklen(X) ((X)->sa_family == AF_INET ? (socklen_t) sizeof (struct sockaddr_in) : (socklen_t) sizeof (struct sockaddr))
104 # endif
105 # endif
106 #endif
107
108 #endif /* SOCKET_UTILS_H */
109