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