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