xref: /titanic_44/usr/src/cmd/krb5/slave/utils.c (revision d2ec54f7875f7e05edd56195adbeb593c947763f)
1 /*
2  * Copyright 2004 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 #include <k5-int.h>
9 #include <socket-utils.h>
10 #include <inet/ip.h>
11 
12 /*
13  * convert a sockaddr_storage address to a krb5_address
14  * Returns address of krbap if success, NULL if error.
15  */
16 
17 krb5_address *
18 cvtkaddr(struct sockaddr_storage *ss, krb5_address *krbap)
19 {
20 	switch (ss->ss_family) {
21 		case AF_INET:
22 			krbap->contents = (krb5_octet *)
23 				    malloc(sizeof (ss2sin(ss)->sin_addr));
24 			if (krbap->contents == NULL)
25 				return (NULL);
26 			memcpy((char *)krbap->contents,
27 					(char *)&(ss2sin(ss)->sin_addr),
28 					sizeof (ss2sin(ss)->sin_addr));
29 			krbap->addrtype = ADDRTYPE_INET;
30 			krbap->length = sizeof (ss2sin(ss)->sin_addr);
31 			return (krbap);
32 		case AF_INET6:
33 			if (IN6_IS_ADDR_V4MAPPED(&ss2sin6(ss)->sin6_addr)) {
34 				/* coerce to IPv4 address */
35 				krbap->contents = (krb5_octet *)
36 					malloc(IPV4_ADDR_LEN);
37 				if (krbap->contents == NULL)
38 					return (NULL);
39 				IN6_V4MAPPED_TO_IPADDR(
40 					&(ss2sin6(ss)->sin6_addr),
41 					*(ipaddr_t *)(krbap->contents));
42 				krbap->addrtype = ADDRTYPE_INET;
43 				krbap->length = IPV4_ADDR_LEN;
44 			} else {
45 				krbap->contents = (krb5_octet *)
46 					malloc(sizeof (ss2sin6(ss)->sin6_addr));
47 				if (krbap->contents == NULL)
48 					return (NULL);
49 				memcpy((char *)krbap->contents,
50 					(char *)&ss2sin6(ss)->sin6_addr,
51 					sizeof (ss2sin6(ss)->sin6_addr));
52 				krbap->addrtype = ADDRTYPE_INET6;
53 				krbap->length = sizeof (ss2sin6(ss)->sin6_addr);
54 			}
55 			return (krbap);
56 		default:
57 			return (NULL);
58 	}
59 }
60