2 Unix SMB/CIFS implementation.
3 Manage connections_struct structures
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Alexander Bokovoy 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* set these to define the limits of the server. NOTE These are on a
25 per-client basis. Thus any one machine can't connect to more than
26 MAX_CONNECTIONS services, but any number of machines may connect at
28 #define MAX_CONNECTIONS 128
30 /****************************************************************************
31 init the conn structures
32 ****************************************************************************/
33 void conn_init(struct smbsrv_context *smb_ctx)
35 smb_ctx->tree.bmap = bitmap_allocate(MAX_CONNECTIONS);
38 /****************************************************************************
39 check if a snum is in use
40 ****************************************************************************/
41 BOOL conn_snum_used(struct smbsrv_context *smb_ctx, int snum)
43 struct tcon_context *conn;
44 for (conn=smb_ctx->tree.connections;conn;conn=conn->next) {
45 if (conn->service == snum) {
53 /****************************************************************************
54 find a conn given a cnum
55 ****************************************************************************/
56 struct tcon_context *conn_find(struct smbsrv_context *smb_ctx, uint_t cnum)
59 struct tcon_context *conn;
61 for (conn=smb_ctx->tree.connections;conn;conn=conn->next,count++) {
62 if (conn->cnum == cnum) {
64 DLIST_PROMOTE(smb_ctx->tree.connections, conn);
74 /****************************************************************************
75 find first available connection slot, starting from a random position.
76 The randomisation stops problems with the server dieing and clients
77 thinking the server is still available.
78 ****************************************************************************/
79 struct tcon_context *conn_new(struct smbsrv_context *smb_ctx)
82 struct tcon_context *conn;
85 i = bitmap_find(smb_ctx->tree.bmap, 1);
88 DEBUG(1,("ERROR! Out of connection structures\n"));
92 mem_ctx = talloc_init("tcon_context[%d]", i);
94 conn = (struct tcon_context *)talloc(mem_ctx, sizeof(*conn));
95 if (!conn) return NULL;
99 conn->mem_ctx = mem_ctx;
101 conn->smb_ctx = smb_ctx;
103 bitmap_set(smb_ctx->tree.bmap, i);
105 smb_ctx->tree.num_open++;
107 DLIST_ADD(smb_ctx->tree.connections, conn);
112 /****************************************************************************
113 close all conn structures
114 ****************************************************************************/
115 void conn_close_all(struct smbsrv_context *smb_ctx)
117 struct tcon_context *conn, *next;
118 for (conn=smb_ctx->tree.connections;conn;conn=next) {
126 /****************************************************************************
127 clear a vuid out of the validity cache, and as the 'owner' of a connection.
128 ****************************************************************************/
129 void conn_clear_vuid_cache(struct smbsrv_context *smb_ctx, uint16_t vuid)
131 struct tcon_context *conn;
134 for (conn=smb_ctx->tree.connections;conn;conn=conn->next) {
135 for (i=0;i<conn->vuid_cache.entries && i< VUID_CACHE_SIZE;i++) {
136 if (conn->vuid_cache.list[i] == vuid) {
137 conn->vuid_cache.list[i] = UID_FIELD_INVALID;
144 /****************************************************************************
145 Free a conn structure.
146 ****************************************************************************/
148 void conn_free(struct smbsrv_context *smb_ctx, struct tcon_context *conn)
150 DLIST_REMOVE(smb_ctx->tree.connections, conn);
152 bitmap_clear(smb_ctx->tree.bmap, conn->cnum);
153 smb_ctx->tree.num_open--;
155 talloc_destroy(conn->mem_ctx);