14bff34e3Sthurlow /*
24bff34e3Sthurlow * Copyright (c) 2000-2001 Boris Popov
34bff34e3Sthurlow * All rights reserved.
44bff34e3Sthurlow *
54bff34e3Sthurlow * Redistribution and use in source and binary forms, with or without
64bff34e3Sthurlow * modification, are permitted provided that the following conditions
74bff34e3Sthurlow * are met:
84bff34e3Sthurlow * 1. Redistributions of source code must retain the above copyright
94bff34e3Sthurlow * notice, this list of conditions and the following disclaimer.
104bff34e3Sthurlow * 2. Redistributions in binary form must reproduce the above copyright
114bff34e3Sthurlow * notice, this list of conditions and the following disclaimer in the
124bff34e3Sthurlow * documentation and/or other materials provided with the distribution.
134bff34e3Sthurlow * 3. All advertising materials mentioning features or use of this software
144bff34e3Sthurlow * must display the following acknowledgement:
154bff34e3Sthurlow * This product includes software developed by Boris Popov.
164bff34e3Sthurlow * 4. Neither the name of the author nor the names of any co-contributors
174bff34e3Sthurlow * may be used to endorse or promote products derived from this software
184bff34e3Sthurlow * without specific prior written permission.
194bff34e3Sthurlow *
204bff34e3Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34e3Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34e3Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34e3Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34e3Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34e3Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34e3Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34e3Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34e3Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34e3Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34e3Sthurlow * SUCH DAMAGE.
314bff34e3Sthurlow *
324bff34e3Sthurlow * Selected code from smb_conn.c
334bff34e3Sthurlow */
344bff34e3Sthurlow
354bff34e3Sthurlow /*
364bff34e3Sthurlow * Helper functions for smb_trantcp.c
374bff34e3Sthurlow * (and maybe future transports)
384bff34e3Sthurlow */
394bff34e3Sthurlow
404bff34e3Sthurlow #include <sys/param.h>
414bff34e3Sthurlow #include <sys/systm.h>
424bff34e3Sthurlow #include <sys/kmem.h>
434bff34e3Sthurlow #include <sys/cmn_err.h>
444bff34e3Sthurlow
454bff34e3Sthurlow #include <netinet/in.h>
464bff34e3Sthurlow #include <netinet/tcp.h>
474bff34e3Sthurlow
484bff34e3Sthurlow #include <netsmb/smb_osdep.h>
49*02d09e03SGordon Ross #include <netsmb/netbios.h>
504bff34e3Sthurlow
514bff34e3Sthurlow #include <netsmb/smb.h>
524bff34e3Sthurlow #include <netsmb/smb_conn.h>
534bff34e3Sthurlow #include <netsmb/smb_subr.h>
544bff34e3Sthurlow #include <netsmb/smb_tran.h>
554bff34e3Sthurlow
564bff34e3Sthurlow /*
574bff34e3Sthurlow * Return the length of a sockaddr structure.
584bff34e3Sthurlow * Only needs to handle the address formats
594bff34e3Sthurlow * used by smb_dup_sockaddr.
604bff34e3Sthurlow */
614bff34e3Sthurlow static size_t
SA_LEN(struct sockaddr * sa)624bff34e3Sthurlow SA_LEN(struct sockaddr *sa)
634bff34e3Sthurlow {
644bff34e3Sthurlow size_t len;
654bff34e3Sthurlow
664bff34e3Sthurlow switch (sa->sa_family) {
674bff34e3Sthurlow case AF_INET:
684bff34e3Sthurlow len = sizeof (struct sockaddr_in);
694bff34e3Sthurlow break;
704bff34e3Sthurlow case AF_INET6:
714bff34e3Sthurlow len = sizeof (struct sockaddr_in6);
724bff34e3Sthurlow break;
734bff34e3Sthurlow case AF_NETBIOS:
744bff34e3Sthurlow len = sizeof (struct sockaddr_nb);
754bff34e3Sthurlow break;
764bff34e3Sthurlow default:
774bff34e3Sthurlow SMBSDEBUG("invalid address family %d\n", sa->sa_family);
784bff34e3Sthurlow len = sizeof (struct sockaddr);
794bff34e3Sthurlow break;
804bff34e3Sthurlow }
814bff34e3Sthurlow
824bff34e3Sthurlow return (len);
834bff34e3Sthurlow }
844bff34e3Sthurlow
854bff34e3Sthurlow /*
864bff34e3Sthurlow * Compare two sockaddr contents
874bff34e3Sthurlow * Return zero if identical.
884bff34e3Sthurlow */
894bff34e3Sthurlow int
smb_cmp_sockaddr(struct sockaddr * a1,struct sockaddr * a2)904bff34e3Sthurlow smb_cmp_sockaddr(struct sockaddr *a1, struct sockaddr *a2)
914bff34e3Sthurlow {
924bff34e3Sthurlow size_t l1, l2;
934bff34e3Sthurlow
944bff34e3Sthurlow l1 = SA_LEN(a1);
954bff34e3Sthurlow l2 = SA_LEN(a2);
964bff34e3Sthurlow
974bff34e3Sthurlow if (l1 != l2)
984bff34e3Sthurlow return (-1);
994bff34e3Sthurlow
1004bff34e3Sthurlow return (bcmp(a1, a2, l1));
1014bff34e3Sthurlow }
1024bff34e3Sthurlow
1034bff34e3Sthurlow /*
1044bff34e3Sthurlow * Copy a socket address of varying size.
1054bff34e3Sthurlow */
1064bff34e3Sthurlow struct sockaddr *
smb_dup_sockaddr(struct sockaddr * sa)1074bff34e3Sthurlow smb_dup_sockaddr(struct sockaddr *sa)
1084bff34e3Sthurlow {
1094bff34e3Sthurlow struct sockaddr *sa2;
1104bff34e3Sthurlow size_t len;
1114bff34e3Sthurlow
1124bff34e3Sthurlow /* Get the length (varies per family) */
1134bff34e3Sthurlow len = SA_LEN(sa);
1144bff34e3Sthurlow
1154bff34e3Sthurlow sa2 = kmem_alloc(len, KM_SLEEP);
1164bff34e3Sthurlow if (sa2)
1174bff34e3Sthurlow bcopy(sa, sa2, len);
1184bff34e3Sthurlow
1194bff34e3Sthurlow return (sa2);
1204bff34e3Sthurlow }
1214bff34e3Sthurlow
1224bff34e3Sthurlow void
smb_free_sockaddr(struct sockaddr * sa)1234bff34e3Sthurlow smb_free_sockaddr(struct sockaddr *sa)
1244bff34e3Sthurlow {
1254bff34e3Sthurlow size_t len;
1264bff34e3Sthurlow
1274bff34e3Sthurlow /* Get the length (varies per family) */
1284bff34e3Sthurlow len = SA_LEN(sa);
1294bff34e3Sthurlow
1304bff34e3Sthurlow kmem_free(sa, len);
1314bff34e3Sthurlow }
132