r2249: got rid of some more mem_ctx elements in structures
[samba.git] / source / smb_server / conn.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Manage smbsrv_tcon structures
4    Copyright (C) Andrew Tridgell 1998
5    Copyright (C) Alexander Bokovoy 2002
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
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
27    one time. */
28 #define MAX_CONNECTIONS 128
29
30 /****************************************************************************
31 init the tcon structures
32 ****************************************************************************/
33 void conn_init(struct smbsrv_connection *smb_conn)
34 {
35         smb_conn->tree.bmap = bitmap_allocate(MAX_CONNECTIONS);
36 }
37
38 /****************************************************************************
39 check if a snum is in use
40 ****************************************************************************/
41 BOOL conn_snum_used(struct smbsrv_connection *smb_conn, int snum)
42 {
43         struct smbsrv_tcon *tcon;
44         for (tcon=smb_conn->tree.tcons;tcon;tcon=tcon->next) {
45                 if (tcon->service == snum) {
46                         return(True);
47                 }
48         }
49         return(False);
50 }
51
52
53 /****************************************************************************
54 find a tcon given a cnum
55 ****************************************************************************/
56 struct smbsrv_tcon *conn_find(struct smbsrv_connection *smb_conn, uint_t cnum)
57 {
58         int count=0;
59         struct smbsrv_tcon *tcon;
60
61         for (tcon=smb_conn->tree.tcons;tcon;tcon=tcon->next,count++) {
62                 if (tcon->cnum == cnum) {
63                         if (count > 10) {
64                                 DLIST_PROMOTE(smb_conn->tree.tcons, tcon);
65                         }
66                         return tcon;
67                 }
68         }
69
70         return NULL;
71 }
72
73
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 smbsrv_tcon *conn_new(struct smbsrv_connection *smb_conn)
80 {
81         struct smbsrv_tcon *tcon;
82         int i;
83
84         i = bitmap_find(smb_conn->tree.bmap, 1);
85         
86         if (i == -1) {
87                 DEBUG(1,("ERROR! Out of connection structures\n"));            
88                 return NULL;
89         }
90
91         tcon = talloc_p(smb_conn, struct smbsrv_tcon);
92         if (!tcon) return NULL;
93
94         ZERO_STRUCTP(tcon);
95
96         tcon->cnum = i;
97         tcon->smb_conn = smb_conn;
98
99         bitmap_set(smb_conn->tree.bmap, i);
100
101         smb_conn->tree.num_open++;
102
103         DLIST_ADD(smb_conn->tree.tcons, tcon);
104
105         return tcon;
106 }
107
108 /****************************************************************************
109 close all tcon structures
110 ****************************************************************************/
111 void conn_close_all(struct smbsrv_connection *smb_conn)
112 {
113         struct smbsrv_tcon *tcon, *next;
114         for (tcon=smb_conn->tree.tcons;tcon;tcon=next) {
115                 next=tcon->next;
116                 close_cnum(tcon);
117         }
118 }
119
120
121 #if REWRITE_REMOVED
122 /****************************************************************************
123 clear a vuid out of the validity cache, and as the 'owner' of a connection.
124 ****************************************************************************/
125 void conn_clear_vuid_cache(struct smbsrv_connection *smb_conn, uint16_t vuid)
126 {
127         struct smbsrv_tcon *tcon;
128         uint_t i;
129
130         for (tcon=smb_conn->tree.tcons;tcon;tcon=tcon->next) {
131                 for (i=0;i<tcon->vuid_cache.entries && i< VUID_CACHE_SIZE;i++) {
132                         if (tcon->vuid_cache.list[i] == vuid) {
133                                 tcon->vuid_cache.list[i] = UID_FIELD_INVALID;
134                         }
135                 }
136         }
137 }
138 #endif
139
140 /****************************************************************************
141  Free a tcon structure.
142 ****************************************************************************/
143
144 void conn_free(struct smbsrv_connection *smb_conn, struct smbsrv_tcon *tcon)
145 {
146         DLIST_REMOVE(smb_conn->tree.tcons, tcon);
147
148         bitmap_clear(smb_conn->tree.bmap, tcon->cnum);
149         smb_conn->tree.num_open--;
150
151         talloc_destroy(tcon);
152 }
153