2 Unix SMB/Netbios implementation.
4 Manage connections_struct structures
5 Copyright (C) Andrew Tridgell 1998
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 extern int DEBUGLEVEL
;
26 /* set these to define the limits of the server. NOTE These are on a
27 per-client basis. Thus any one machine can't connect to more than
28 MAX_CONNECTIONS services, but any number of machines may connect at
30 #define MAX_CONNECTIONS 128
32 static connection_struct
*Connections
;
34 /* number of open connections */
35 static struct bitmap
*bmap
;
38 /****************************************************************************
39 init the conn structures
40 ****************************************************************************/
43 bmap
= bitmap_allocate(MAX_CONNECTIONS
);
46 /****************************************************************************
47 return the number of open connections
48 ****************************************************************************/
49 int conn_num_open(void)
55 /****************************************************************************
56 check if a snum is in use
57 ****************************************************************************/
58 BOOL
conn_snum_used(int snum
)
60 connection_struct
*conn
;
61 for (conn
=Connections
;conn
;conn
=conn
->next
) {
62 if (conn
->service
== snum
) {
70 /****************************************************************************
71 find a conn given a cnum
72 ****************************************************************************/
73 connection_struct
*conn_find(int cnum
)
76 connection_struct
*conn
;
78 for (conn
=Connections
;conn
;conn
=conn
->next
,count
++) {
79 if (conn
->cnum
== cnum
) {
81 DLIST_PROMOTE(Connections
, conn
);
91 /****************************************************************************
92 find first available connection slot, starting from a random position.
93 The randomisation stops problems with the server dieing and clients
94 thinking the server is still available.
95 ****************************************************************************/
96 connection_struct
*conn_new(void)
98 connection_struct
*conn
;
101 i
= bitmap_find(bmap
, 1);
104 DEBUG(1,("ERROR! Out of connection structures\n"));
108 conn
= (connection_struct
*)malloc(sizeof(*conn
));
109 if (!conn
) return NULL
;
113 conn
->smbd_pid
= getpid();
119 string_init(&conn
->user
,"");
120 string_init(&conn
->dirpath
,"");
121 string_init(&conn
->connectpath
,"");
122 string_init(&conn
->origpath
,"");
124 DLIST_ADD(Connections
, conn
);
129 /****************************************************************************
130 close all conn structures
131 ****************************************************************************/
132 void conn_close_all(void)
134 connection_struct
*conn
, *next
;
135 for (conn
=Connections
;conn
;conn
=next
) {
137 close_cnum(conn
, (uint16
)-1);
141 /****************************************************************************
142 idle inactive connections
143 ****************************************************************************/
144 BOOL
conn_idle_all(time_t t
, int deadtime
)
147 connection_struct
*conn
, *next
;
149 for (conn
=Connections
;conn
;conn
=next
) {
151 /* close dirptrs on connections that are idle */
152 if ((t
-conn
->lastused
) > DPTR_IDLE_TIMEOUT
)
155 if (conn
->num_files_open
> 0 ||
156 (t
-conn
->lastused
)<deadtime
)
163 /****************************************************************************
164 free a conn structure
165 ****************************************************************************/
166 void conn_free(connection_struct
*conn
)
168 DLIST_REMOVE(Connections
, conn
);
170 if (conn
->ngroups
&& conn
->groups
) {
176 free_namearray(conn
->veto_list
);
177 free_namearray(conn
->hide_list
);
178 free_namearray(conn
->veto_oplock_list
);
180 string_free(&conn
->user
);
181 string_free(&conn
->dirpath
);
182 string_free(&conn
->connectpath
);
183 string_free(&conn
->origpath
);
185 bitmap_clear(bmap
, conn
->cnum
);