1505d05c7Sgtb /*
2*159d09a2SMark Phalan * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3505d05c7Sgtb * Use is subject to license terms.
4505d05c7Sgtb */
5505d05c7Sgtb
67c478bd9Sstevel@tonic-gate
77c478bd9Sstevel@tonic-gate /*
8*159d09a2SMark Phalan * Copyright (C) 2001,2005 by the Massachusetts Institute of Technology,
97c478bd9Sstevel@tonic-gate * Cambridge, MA, USA. All Rights Reserved.
107c478bd9Sstevel@tonic-gate *
117c478bd9Sstevel@tonic-gate * This software is being provided to you, the LICENSEE, by the
127c478bd9Sstevel@tonic-gate * Massachusetts Institute of Technology (M.I.T.) under the following
137c478bd9Sstevel@tonic-gate * license. By obtaining, using and/or copying this software, you agree
147c478bd9Sstevel@tonic-gate * that you have read, understood, and will comply with these terms and
157c478bd9Sstevel@tonic-gate * conditions:
167c478bd9Sstevel@tonic-gate *
177c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may
187c478bd9Sstevel@tonic-gate * require a specific license from the United States Government.
197c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
207c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting.
217c478bd9Sstevel@tonic-gate *
227c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
237c478bd9Sstevel@tonic-gate * this software and its documentation for any purpose and without fee or
247c478bd9Sstevel@tonic-gate * royalty is hereby granted, provided that you agree to comply with the
257c478bd9Sstevel@tonic-gate * following copyright notice and statements, including the disclaimer, and
267c478bd9Sstevel@tonic-gate * that the same appear on ALL copies of the software and documentation,
277c478bd9Sstevel@tonic-gate * including modifications that you make for internal use or for
287c478bd9Sstevel@tonic-gate * distribution:
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
317c478bd9Sstevel@tonic-gate * OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not
327c478bd9Sstevel@tonic-gate * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
337c478bd9Sstevel@tonic-gate * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
347c478bd9Sstevel@tonic-gate * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
357c478bd9Sstevel@tonic-gate * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
367c478bd9Sstevel@tonic-gate *
377c478bd9Sstevel@tonic-gate * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
387c478bd9Sstevel@tonic-gate * be used in advertising or publicity pertaining to distribution of the
397c478bd9Sstevel@tonic-gate * software. Title to copyright in this software and any associated
407c478bd9Sstevel@tonic-gate * documentation shall at all times remain with M.I.T., and USER agrees to
417c478bd9Sstevel@tonic-gate * preserve same.
427c478bd9Sstevel@tonic-gate *
437c478bd9Sstevel@tonic-gate * Furthermore if you modify this software you must label
447c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a
457c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #ifndef SOCKET_UTILS_H
497c478bd9Sstevel@tonic-gate #define SOCKET_UTILS_H
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /* Some useful stuff cross-platform for manipulating socket addresses.
527c478bd9Sstevel@tonic-gate We assume at least ipv4 sockaddr_in support. The sockaddr_storage
537c478bd9Sstevel@tonic-gate stuff comes from the ipv6 socket api enhancements; socklen_t is
547c478bd9Sstevel@tonic-gate provided on some systems; the rest is just convenience for internal
557c478bd9Sstevel@tonic-gate use in the krb5 tree.
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate Do NOT install this file. */
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /* for HAVE_SOCKLEN_T, KRB5_USE_INET6, etc */
607c478bd9Sstevel@tonic-gate #include "autoconf.h"
61505d05c7Sgtb /* for sockaddr_storage */
62505d05c7Sgtb #include "port-sockets.h"
63*159d09a2SMark Phalan /* for "inline" if needed */
64*159d09a2SMark Phalan #include "k5-platform.h"
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #if defined (__GNUC__)
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * There's a lot of confusion between pointers to different sockaddr
697c478bd9Sstevel@tonic-gate * types, and pointers with different degrees of indirection, as in
707c478bd9Sstevel@tonic-gate * the locate_kdc type functions. Use these function to ensure we
717c478bd9Sstevel@tonic-gate * don't do something silly like cast a "sockaddr **" to a
727c478bd9Sstevel@tonic-gate * "sockaddr_in *".
737c478bd9Sstevel@tonic-gate *
747c478bd9Sstevel@tonic-gate * The casts to (void *) are to get GCC to shut up about alignment
757c478bd9Sstevel@tonic-gate * increasing.
767c478bd9Sstevel@tonic-gate */
sa2sin(struct sockaddr * sa)777c478bd9Sstevel@tonic-gate static __inline__ struct sockaddr_in *sa2sin (struct sockaddr *sa)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate return (struct sockaddr_in *) (void *) sa;
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate #ifdef KRB5_USE_INET6
sa2sin6(struct sockaddr * sa)827c478bd9Sstevel@tonic-gate static __inline__ struct sockaddr_in6 *sa2sin6 (struct sockaddr *sa)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate return (struct sockaddr_in6 *) (void *) sa;
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate #endif
ss2sa(struct sockaddr_storage * ss)877c478bd9Sstevel@tonic-gate static __inline__ struct sockaddr *ss2sa (struct sockaddr_storage *ss)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate return (struct sockaddr *) ss;
907c478bd9Sstevel@tonic-gate }
ss2sin(struct sockaddr_storage * ss)917c478bd9Sstevel@tonic-gate static __inline__ struct sockaddr_in *ss2sin (struct sockaddr_storage *ss)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate return (struct sockaddr_in *) ss;
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate #ifdef KRB5_USE_INET6
ss2sin6(struct sockaddr_storage * ss)967c478bd9Sstevel@tonic-gate static __inline__ struct sockaddr_in6 *ss2sin6 (struct sockaddr_storage *ss)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate return (struct sockaddr_in6 *) ss;
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate #else
1027c478bd9Sstevel@tonic-gate #define sa2sin(S) ((struct sockaddr_in *)(S))
1037c478bd9Sstevel@tonic-gate #define sa2sin6(S) ((struct sockaddr_in6 *)(S))
1047c478bd9Sstevel@tonic-gate #define ss2sa(S) ((struct sockaddr *)(S))
1057c478bd9Sstevel@tonic-gate #define ss2sin(S) ((struct sockaddr_in *)(S))
1067c478bd9Sstevel@tonic-gate #define ss2sin6(S) ((struct sockaddr_in6 *)(S))
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate #if !defined (socklen)
1107c478bd9Sstevel@tonic-gate /* socklen_t socklen (struct sockaddr *) */
1117c478bd9Sstevel@tonic-gate # ifdef HAVE_SA_LEN
1127c478bd9Sstevel@tonic-gate # define socklen(X) ((X)->sa_len)
1137c478bd9Sstevel@tonic-gate # else
1147c478bd9Sstevel@tonic-gate # ifdef KRB5_USE_INET6
1157c478bd9Sstevel@tonic-gate # 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))
1167c478bd9Sstevel@tonic-gate # else
1177c478bd9Sstevel@tonic-gate # define socklen(X) ((X)->sa_family == AF_INET ? (socklen_t) sizeof (struct sockaddr_in) : (socklen_t) sizeof (struct sockaddr))
1187c478bd9Sstevel@tonic-gate # endif
1197c478bd9Sstevel@tonic-gate # endif
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate #endif /* SOCKET_UTILS_H */
123