xref: /titanic_51/usr/src/common/smbsrv/smb_inet.c (revision d218c8f01e2f96c4d73489408eb378adbfa58bc7)
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 
26 /*
27  * This file was originally generated using rpcgen.
28  */
29 
30 #ifndef _KERNEL
31 #include <string.h>
32 #include <stdlib.h>
33 #include <arpa/inet.h>
34 #endif /* !_KERNEL */
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <inet/tcp.h>
39 #include <smbsrv/smb_inet.h>
40 
41 const struct in6_addr ipv6addr_any = IN6ADDR_ANY_INIT;
42 
43 boolean_t
44 smb_inet_equal(smb_inaddr_t *ip1, smb_inaddr_t *ip2)
45 {
46 	if ((ip1->a_family == AF_INET) &&
47 	    (ip2->a_family == AF_INET) &&
48 	    (ip1->a_ipv4 == ip2->a_ipv4))
49 		return (B_TRUE);
50 
51 	if ((ip1->a_family == AF_INET6) &&
52 	    (ip2->a_family == AF_INET6) &&
53 	    (!memcmp(&ip1->a_ipv6, &ip2->a_ipv6, IPV6_ADDR_LEN)))
54 		return (B_TRUE);
55 	else
56 		return (B_FALSE);
57 }
58 
59 boolean_t
60 smb_inet_same_subnet(smb_inaddr_t *ip1, smb_inaddr_t *ip2, uint32_t v4mask)
61 {
62 	if ((ip1->a_family == AF_INET) &&
63 	    (ip2->a_family == AF_INET) &&
64 	    ((ip1->a_ipv4 & v4mask) == (ip2->a_ipv4 & v4mask)))
65 		return (B_TRUE);
66 
67 	if ((ip1->a_family == AF_INET6) &&
68 	    (ip2->a_family == AF_INET6) &&
69 	    (!memcmp(&ip1->a_ipv6, &ip2->a_ipv6, IPV6_ADDR_LEN)))
70 		return (B_TRUE);
71 	else
72 		return (B_FALSE);
73 }
74 
75 boolean_t
76 smb_inet_iszero(smb_inaddr_t *ipaddr)
77 {
78 	const void *ipsz = (const void *)&ipv6addr_any;
79 
80 	if ((ipaddr->a_family == AF_INET) &&
81 	    (ipaddr->a_ipv4 == 0))
82 		return (B_TRUE);
83 
84 	if ((ipaddr->a_family == AF_INET6) &&
85 	    !memcmp(&ipaddr->a_ipv6, ipsz, IPV6_ADDR_LEN))
86 		return (B_TRUE);
87 	else
88 		return (B_FALSE);
89 }
90 
91 const char *
92 smb_inet_ntop(smb_inaddr_t *addr, char *buf, int size)
93 {
94 	return ((char *)inet_ntop(addr->a_family, (char *)addr, buf, size));
95 }
96