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