xref: /titanic_50/usr/src/common/smbsrv/smb_inet.c (revision 49b5df1eac768235abeb210f61310e88b7d172b6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
26  */
27 
28 /*
29  * This file was originally generated using rpcgen.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 
36 #if !defined(_KERNEL)
37 #include <errno.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <arpa/inet.h>
41 #else	/* !_KERNEL */
42 #include <sys/errno.h>
43 #include <sys/sunddi.h>
44 /* Don't want the rest of what's in inet/ip.h */
45 extern char	*inet_ntop(int, const void *, char *, int);
46 extern int	inet_pton(int, char *, void *);
47 #endif	/* !_KERNEL */
48 
49 #include <smbsrv/smb_inet.h>
50 
51 const struct in6_addr ipv6addr_any = IN6ADDR_ANY_INIT;
52 
53 boolean_t
smb_inet_equal(smb_inaddr_t * ip1,smb_inaddr_t * ip2)54 smb_inet_equal(smb_inaddr_t *ip1, smb_inaddr_t *ip2)
55 {
56 	if ((ip1->a_family == AF_INET) &&
57 	    (ip2->a_family == AF_INET) &&
58 	    (ip1->a_ipv4 == ip2->a_ipv4))
59 		return (B_TRUE);
60 
61 	if ((ip1->a_family == AF_INET6) &&
62 	    (ip2->a_family == AF_INET6) &&
63 	    (!memcmp(&ip1->a_ipv6, &ip2->a_ipv6, sizeof (in6_addr_t))))
64 		return (B_TRUE);
65 	else
66 		return (B_FALSE);
67 }
68 
69 boolean_t
smb_inet_same_subnet(smb_inaddr_t * ip1,smb_inaddr_t * ip2,uint32_t v4mask)70 smb_inet_same_subnet(smb_inaddr_t *ip1, smb_inaddr_t *ip2, uint32_t v4mask)
71 {
72 	if ((ip1->a_family == AF_INET) &&
73 	    (ip2->a_family == AF_INET) &&
74 	    ((ip1->a_ipv4 & v4mask) == (ip2->a_ipv4 & v4mask)))
75 		return (B_TRUE);
76 
77 	if ((ip1->a_family == AF_INET6) &&
78 	    (ip2->a_family == AF_INET6) &&
79 	    (!memcmp(&ip1->a_ipv6, &ip2->a_ipv6, sizeof (in6_addr_t))))
80 		return (B_TRUE);
81 	else
82 		return (B_FALSE);
83 }
84 
85 boolean_t
smb_inet_iszero(smb_inaddr_t * ipaddr)86 smb_inet_iszero(smb_inaddr_t *ipaddr)
87 {
88 	const void *ipsz = (const void *)&ipv6addr_any;
89 
90 	if ((ipaddr->a_family == AF_INET) &&
91 	    (ipaddr->a_ipv4 == 0))
92 		return (B_TRUE);
93 
94 	if ((ipaddr->a_family == AF_INET6) &&
95 	    !memcmp(&ipaddr->a_ipv6, ipsz, sizeof (in6_addr_t)))
96 		return (B_TRUE);
97 	else
98 		return (B_FALSE);
99 }
100 
101 const char *
smb_inet_ntop(smb_inaddr_t * addr,char * buf,int size)102 smb_inet_ntop(smb_inaddr_t *addr, char *buf, int size)
103 {
104 	/* Lint avoidance. */
105 #if !defined(_KERNEL)
106 	size_t sz = (size_t)size;
107 #else	/* _KERNEL */
108 	int sz = size;
109 
110 	/*
111 	 * Until uts/common/inet/ip/inet_ntop.c is fixed so it
112 	 * no longer uses leading zeros printing IPv4 addresses,
113 	 * we need to handle IPv4 ourselves.  If we leave the
114 	 * leading zeros, Windows clients get errors trying to
115 	 * resolve those address strings to names.  After:
116 	 * https://www.illumos.org/issues/5980 is fixed,
117 	 * this work-around can be removed.
118 	 */
119 	if (addr->a_family == AF_INET) {
120 		uint8_t *p = (void *) &addr->a_ipv4;
121 		(void) snprintf(buf, size, "%d.%d.%d.%d",
122 		    p[0], p[1], p[2], p[3]);
123 		return (buf);
124 	}
125 #endif	/* _KERNEL */
126 
127 	return ((char *)inet_ntop(addr->a_family, addr, buf, sz));
128 }
129