xref: /illumos-gate/usr/src/common/smbsrv/smb_inet.c (revision fc724630)
17f667e74Sjose borrego /*
27f667e74Sjose borrego  * CDDL HEADER START
37f667e74Sjose borrego  *
47f667e74Sjose borrego  * The contents of this file are subject to the terms of the
57f667e74Sjose borrego  * Common Development and Distribution License (the "License").
67f667e74Sjose borrego  * You may not use this file except in compliance with the License.
77f667e74Sjose borrego  *
87f667e74Sjose borrego  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97f667e74Sjose borrego  * or http://www.opensolaris.org/os/licensing.
107f667e74Sjose borrego  * See the License for the specific language governing permissions
117f667e74Sjose borrego  * and limitations under the License.
127f667e74Sjose borrego  *
137f667e74Sjose borrego  * When distributing Covered Code, include this CDDL HEADER in each
147f667e74Sjose borrego  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157f667e74Sjose borrego  * If applicable, add the following below this CDDL HEADER, with the
167f667e74Sjose borrego  * fields enclosed by brackets "[]" replaced with your own identifying
177f667e74Sjose borrego  * information: Portions Copyright [yyyy] [name of copyright owner]
187f667e74Sjose borrego  *
197f667e74Sjose borrego  * CDDL HEADER END
207f667e74Sjose borrego  */
217f667e74Sjose borrego /*
227f667e74Sjose borrego  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237f667e74Sjose borrego  * Use is subject to license terms.
247f667e74Sjose borrego  */
257f667e74Sjose borrego 
267f667e74Sjose borrego /*
277f667e74Sjose borrego  * This file was originally generated using rpcgen.
287f667e74Sjose borrego  */
297f667e74Sjose borrego 
307f667e74Sjose borrego #ifndef _KERNEL
317f667e74Sjose borrego #include <string.h>
327f667e74Sjose borrego #include <stdlib.h>
337f667e74Sjose borrego #include <arpa/inet.h>
347f667e74Sjose borrego #endif /* !_KERNEL */
357f667e74Sjose borrego #include <sys/types.h>
367f667e74Sjose borrego #include <sys/socket.h>
377f667e74Sjose borrego #include <netinet/in.h>
387f667e74Sjose borrego #include <inet/tcp.h>
397f667e74Sjose borrego #include <smbsrv/smb_inet.h>
407f667e74Sjose borrego 
417f667e74Sjose borrego const struct in6_addr ipv6addr_any = IN6ADDR_ANY_INIT;
427f667e74Sjose borrego 
437f667e74Sjose borrego boolean_t
44*fc724630SAlan Wright smb_inet_equal(smb_inaddr_t *ip1, smb_inaddr_t *ip2)
45*fc724630SAlan Wright {
46*fc724630SAlan Wright 	if ((ip1->a_family == AF_INET) &&
47*fc724630SAlan Wright 	    (ip2->a_family == AF_INET) &&
48*fc724630SAlan Wright 	    (ip1->a_ipv4 == ip2->a_ipv4))
49*fc724630SAlan Wright 		return (B_TRUE);
50*fc724630SAlan Wright 
51*fc724630SAlan Wright 	if ((ip1->a_family == AF_INET6) &&
52*fc724630SAlan Wright 	    (ip2->a_family == AF_INET6) &&
53*fc724630SAlan Wright 	    (!memcmp(&ip1->a_ipv6, &ip2->a_ipv6, IPV6_ADDR_LEN)))
54*fc724630SAlan Wright 		return (B_TRUE);
55*fc724630SAlan Wright 	else
56*fc724630SAlan Wright 		return (B_FALSE);
57*fc724630SAlan Wright }
58*fc724630SAlan Wright 
59*fc724630SAlan Wright boolean_t
60*fc724630SAlan Wright smb_inet_same_subnet(smb_inaddr_t *ip1, smb_inaddr_t *ip2, uint32_t v4mask)
617f667e74Sjose borrego {
627f667e74Sjose borrego 	if ((ip1->a_family == AF_INET) &&
637f667e74Sjose borrego 	    (ip2->a_family == AF_INET) &&
647f667e74Sjose borrego 	    ((ip1->a_ipv4 & v4mask) == (ip2->a_ipv4 & v4mask)))
657f667e74Sjose borrego 		return (B_TRUE);
667f667e74Sjose borrego 
677f667e74Sjose borrego 	if ((ip1->a_family == AF_INET6) &&
687f667e74Sjose borrego 	    (ip2->a_family == AF_INET6) &&
697f667e74Sjose borrego 	    (!memcmp(&ip1->a_ipv6, &ip2->a_ipv6, IPV6_ADDR_LEN)))
707f667e74Sjose borrego 		return (B_TRUE);
717f667e74Sjose borrego 	else
727f667e74Sjose borrego 		return (B_FALSE);
737f667e74Sjose borrego }
747f667e74Sjose borrego 
757f667e74Sjose borrego boolean_t
767f667e74Sjose borrego smb_inet_iszero(smb_inaddr_t *ipaddr)
777f667e74Sjose borrego {
787f667e74Sjose borrego 	const void *ipsz = (const void *)&ipv6addr_any;
797f667e74Sjose borrego 
807f667e74Sjose borrego 	if ((ipaddr->a_family == AF_INET) &&
817f667e74Sjose borrego 	    (ipaddr->a_ipv4 == 0))
827f667e74Sjose borrego 		return (B_TRUE);
837f667e74Sjose borrego 
847f667e74Sjose borrego 	if ((ipaddr->a_family == AF_INET6) &&
857f667e74Sjose borrego 	    !memcmp(&ipaddr->a_ipv6, ipsz, IPV6_ADDR_LEN))
867f667e74Sjose borrego 		return (B_TRUE);
877f667e74Sjose borrego 	else
887f667e74Sjose borrego 		return (B_FALSE);
897f667e74Sjose borrego }
907f667e74Sjose borrego 
917f667e74Sjose borrego const char *
927f667e74Sjose borrego smb_inet_ntop(smb_inaddr_t *addr, char *buf, int size)
937f667e74Sjose borrego {
947f667e74Sjose borrego 	return ((char *)inet_ntop(addr->a_family, (char *)addr, buf, size));
957f667e74Sjose borrego }
96