xref: /titanic_52/usr/src/cmd/krb5/slave/utils.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate #include <k5-int.h>
9*7c478bd9Sstevel@tonic-gate #include <socket-utils.h>
10*7c478bd9Sstevel@tonic-gate #include <inet/ip.h>
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate /*
13*7c478bd9Sstevel@tonic-gate  * convert a sockaddr_storage address to a krb5_address
14*7c478bd9Sstevel@tonic-gate  * Returns address of krbap if success, NULL if error.
15*7c478bd9Sstevel@tonic-gate  */
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate krb5_address *
18*7c478bd9Sstevel@tonic-gate cvtkaddr(struct sockaddr_storage *ss, krb5_address *krbap)
19*7c478bd9Sstevel@tonic-gate {
20*7c478bd9Sstevel@tonic-gate 	switch (ss->ss_family) {
21*7c478bd9Sstevel@tonic-gate 		case AF_INET:
22*7c478bd9Sstevel@tonic-gate 			krbap->contents = (krb5_octet *)
23*7c478bd9Sstevel@tonic-gate 				    malloc(sizeof (ss2sin(ss)->sin_addr));
24*7c478bd9Sstevel@tonic-gate 			if (krbap->contents == NULL)
25*7c478bd9Sstevel@tonic-gate 				return (NULL);
26*7c478bd9Sstevel@tonic-gate 			memcpy((char *)krbap->contents,
27*7c478bd9Sstevel@tonic-gate 					(char *)&(ss2sin(ss)->sin_addr),
28*7c478bd9Sstevel@tonic-gate 					sizeof (ss2sin(ss)->sin_addr));
29*7c478bd9Sstevel@tonic-gate 			krbap->addrtype = ADDRTYPE_INET;
30*7c478bd9Sstevel@tonic-gate 			krbap->length = sizeof (ss2sin(ss)->sin_addr);
31*7c478bd9Sstevel@tonic-gate 			return (krbap);
32*7c478bd9Sstevel@tonic-gate 		case AF_INET6:
33*7c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&ss2sin6(ss)->sin6_addr)) {
34*7c478bd9Sstevel@tonic-gate 				/* coerce to IPv4 address */
35*7c478bd9Sstevel@tonic-gate 				krbap->contents = (krb5_octet *)
36*7c478bd9Sstevel@tonic-gate 					malloc(IPV4_ADDR_LEN);
37*7c478bd9Sstevel@tonic-gate 				if (krbap->contents == NULL)
38*7c478bd9Sstevel@tonic-gate 					return (NULL);
39*7c478bd9Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(
40*7c478bd9Sstevel@tonic-gate 					&(ss2sin6(ss)->sin6_addr),
41*7c478bd9Sstevel@tonic-gate 					*(ipaddr_t *)(krbap->contents));
42*7c478bd9Sstevel@tonic-gate 				krbap->addrtype = ADDRTYPE_INET;
43*7c478bd9Sstevel@tonic-gate 				krbap->length = IPV4_ADDR_LEN;
44*7c478bd9Sstevel@tonic-gate 			} else {
45*7c478bd9Sstevel@tonic-gate 				krbap->contents = (krb5_octet *)
46*7c478bd9Sstevel@tonic-gate 					malloc(sizeof (ss2sin6(ss)->sin6_addr));
47*7c478bd9Sstevel@tonic-gate 				if (krbap->contents == NULL)
48*7c478bd9Sstevel@tonic-gate 					return (NULL);
49*7c478bd9Sstevel@tonic-gate 				memcpy((char *)krbap->contents,
50*7c478bd9Sstevel@tonic-gate 					(char *)&ss2sin6(ss)->sin6_addr,
51*7c478bd9Sstevel@tonic-gate 					sizeof (ss2sin6(ss)->sin6_addr));
52*7c478bd9Sstevel@tonic-gate 				krbap->addrtype = ADDRTYPE_INET6;
53*7c478bd9Sstevel@tonic-gate 				krbap->length = sizeof (ss2sin6(ss)->sin6_addr);
54*7c478bd9Sstevel@tonic-gate 			}
55*7c478bd9Sstevel@tonic-gate 			return (krbap);
56*7c478bd9Sstevel@tonic-gate 		default:
57*7c478bd9Sstevel@tonic-gate 			return (NULL);
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate }
60