xref: /illumos-gate/usr/src/common/smbsrv/smb_inet.c (revision 8c69cc8fbe729fa7b091e901c4b50508ccc6bb33)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2017 Nexenta Systems, Inc.
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
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
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
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 *
102 smb_inet_ntop(smb_inaddr_t *addr, char *buf, int size)
103 {
104 	/* Lint avoidance */
105 #ifdef	_KERNEL
106 	int sz = size;
107 #else
108 	size_t sz = (size_t)size;
109 #endif
110 
111 	return ((char *)inet_ntop(addr->a_family, addr, buf, sz));
112 }
113