Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / libsmbfs / smb / findvc.c
blob63c6cce242c11fb79f2795a755dca88abf053235
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Find existing an VC given a list of addresses.
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <netdb.h>
38 #include <libintl.h>
39 #include <xti.h>
40 #include <assert.h>
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <sys/byteorder.h>
45 #include <sys/socket.h>
46 #include <sys/fcntl.h>
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
50 #include <arpa/inet.h>
52 #include <netsmb/smb.h>
53 #include <netsmb/smb_lib.h>
54 #include <netsmb/netbios.h>
55 #include <netsmb/nb_lib.h>
56 #include <netsmb/smb_dev.h>
58 #include "charsets.h"
59 #include "private.h"
62 * Ask the driver if it has a VC with this IP address.
64 static int
65 findvc(struct smb_ctx *ctx, struct addrinfo *ai)
67 smbioc_ossn_t *ssn = &ctx->ct_ssn;
70 * Copy the passed address into ssn_srvaddr,
71 * but first sanity-check lengths. Also,
72 * zero it first to avoid trailing junk.
74 if (ai->ai_addrlen > sizeof (ssn->ssn_srvaddr))
75 return (EINVAL);
76 bzero(&ssn->ssn_srvaddr, sizeof (ssn->ssn_srvaddr));
77 bcopy(ai->ai_addr, &ssn->ssn_srvaddr, ai->ai_addrlen);
79 if (ioctl(ctx->ct_dev_fd, SMBIOC_SSN_FIND, ssn) == -1)
80 return (errno);
82 return (0);
86 * Find (and reuse) an existing VC.
87 * See also: newvc.c
89 int
90 smb_ctx_findvc(struct smb_ctx *ctx)
92 struct addrinfo *ai;
93 int err;
95 /* Should already have the address list. */
96 if ((ctx->ct_flags & SMBCF_RESOLVED) == 0)
97 return (EINVAL);
99 if (ctx->ct_dev_fd < 0) {
100 if ((err = smb_ctx_gethandle(ctx)))
101 return (err);
104 for (ai = ctx->ct_addrinfo; ai; ai = ai->ai_next) {
106 switch (ai->ai_family) {
108 case AF_INET:
109 case AF_INET6:
110 case AF_NETBIOS:
111 err = findvc(ctx, ai);
112 break;
114 default:
115 DPRINT("skipped family %d", ai->ai_family);
116 err = EPROTONOSUPPORT;
117 break;
120 if (err == 0) {
121 /* re-use an existing VC */
122 ctx->ct_flags |= SMBCF_SSNACTIVE;
123 return (0);
127 return (ENOENT);
131 * Forcibly disconnect the current session, even if
132 * there are others using it! This is used by the
133 * SMB server netlogon when it wants to setup a new
134 * logon session and does not want any re-use.
137 smb_ctx_kill(struct smb_ctx *ctx)
140 if (ioctl(ctx->ct_dev_fd, SMBIOC_SSN_KILL, NULL) == -1)
141 return (errno);
143 return (0);