4 Unix SMB/Netbios implementation.
6 Manage connections_struct structures
7 Copyright (C) Andrew Tridgell 1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern int DEBUGLEVEL;
28 /* set these to define the limits of the server. NOTE These are on a
29 per-client basis. Thus any one machine can't connect to more than
30 MAX_CONNECTIONS services, but any number of machines may connect at
32 #define MAX_CONNECTIONS 128
34 static connection_struct *Connections;
36 /* number of open connections */
37 static struct bitmap *bmap;
40 /****************************************************************************
41 init the conn structures
42 ****************************************************************************/
45 bmap = bitmap_allocate(MAX_CONNECTIONS);
48 /****************************************************************************
49 return the number of open connections
50 ****************************************************************************/
51 int conn_num_open(void)
57 /****************************************************************************
58 check if a snum is in use
59 ****************************************************************************/
60 BOOL conn_snum_used(int snum)
62 connection_struct *conn;
63 for (conn=Connections;conn;conn=conn->next) {
64 if (conn->service == snum) {
72 /****************************************************************************
73 find a conn given a cnum
74 ****************************************************************************/
75 connection_struct *conn_find(int cnum)
78 connection_struct *conn;
80 for (conn=Connections;conn;conn=conn->next,count++) {
81 if (conn->cnum == cnum) {
83 DLIST_PROMOTE(Connections, conn);
93 /****************************************************************************
94 find first available connection slot, starting from a random position.
95 The randomisation stops problems with the server dieing and clients
96 thinking the server is still available.
97 ****************************************************************************/
98 connection_struct *conn_new(void)
100 connection_struct *conn;
103 i = bitmap_find(bmap, 1);
106 DEBUG(1,("ERROR! Out of connection structures\n"));
110 conn = (connection_struct *)malloc(sizeof(*conn));
111 if (!conn) return NULL;
120 string_set(&conn->user,"");
121 string_set(&conn->dirpath,"");
122 string_set(&conn->connectpath,"");
123 string_set(&conn->origpath,"");
125 DLIST_ADD(Connections, conn);
130 /****************************************************************************
131 close all conn structures
132 ****************************************************************************/
133 void conn_close_all(void)
135 connection_struct *conn, *next;
136 for (conn=Connections;conn;conn=next) {
138 close_cnum(conn, (uint16)-1);
142 /****************************************************************************
143 idle inactive connections
144 ****************************************************************************/
145 BOOL conn_idle_all(time_t t, int deadtime)
148 connection_struct *conn, *next;
150 for (conn=Connections;conn;conn=next) {
152 /* close dirptrs on connections that are idle */
153 if ((t-conn->lastused) > DPTR_IDLE_TIMEOUT)
156 if (conn->num_files_open > 0 ||
157 (t-conn->lastused)<deadtime)
164 /****************************************************************************
165 Free a conn structure.
166 ****************************************************************************/
168 void conn_free(connection_struct *conn)
170 /* Free vfs_connection_struct */
173 if (conn->vfs_conn != NULL) {
174 /* Close dlopen() handle */
175 if (conn->vfs_conn->dl_handle) {
176 dlclose(conn->vfs_conn->dl_handle);
178 #endif /* HAVE_LIBDL */
180 if (conn->vfs_conn->groups != NULL) {
181 free(conn->vfs_conn->groups);
183 delete_nt_token(&conn->vfs_conn->nt_user_token);
184 free(conn->vfs_conn);
187 DLIST_REMOVE(Connections, conn);
189 if (conn->ngroups && conn->groups) {
195 delete_nt_token(&conn->nt_user_token);
196 free_namearray(conn->veto_list);
197 free_namearray(conn->hide_list);
198 free_namearray(conn->veto_oplock_list);
200 string_free(&conn->user);
201 string_free(&conn->dirpath);
202 string_free(&conn->connectpath);
203 string_free(&conn->origpath);
205 bitmap_clear(bmap, conn->cnum);