more merging voodoo
[tprouty/samba.git] / source / smbd / conn.c
1 #define OLD_NTDOMAIN 1
2
3 /* 
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    Manage connections_struct structures
7    Copyright (C) Andrew Tridgell 1998
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25
26 extern int DEBUGLEVEL;
27
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
31    one time. */
32 #define MAX_CONNECTIONS 128
33
34 static connection_struct *Connections;
35
36 /* number of open connections */
37 static struct bitmap *bmap;
38 static int num_open;
39
40 /****************************************************************************
41 init the conn structures
42 ****************************************************************************/
43 void conn_init(void)
44 {
45         bmap = bitmap_allocate(MAX_CONNECTIONS);
46 }
47
48 /****************************************************************************
49 return the number of open connections
50 ****************************************************************************/
51 int conn_num_open(void)
52 {
53         return num_open;
54 }
55
56
57 /****************************************************************************
58 check if a snum is in use
59 ****************************************************************************/
60 BOOL conn_snum_used(int snum)
61 {
62         connection_struct *conn;
63         for (conn=Connections;conn;conn=conn->next) {
64                 if (conn->service == snum) {
65                         return(True);
66                 }
67         }
68         return(False);
69 }
70
71
72 /****************************************************************************
73 find a conn given a cnum
74 ****************************************************************************/
75 connection_struct *conn_find(int cnum)
76 {
77         int count=0;
78         connection_struct *conn;
79
80         for (conn=Connections;conn;conn=conn->next,count++) {
81                 if (conn->cnum == cnum) {
82                         if (count > 10) {
83                                 DLIST_PROMOTE(Connections, conn);
84                         }
85                         return conn;
86                 }
87         }
88
89         return NULL;
90 }
91
92
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)
99 {
100         connection_struct *conn;
101         int i;
102
103         i = bitmap_find(bmap, 1);
104         
105         if (i == -1) {
106                 DEBUG(1,("ERROR! Out of connection structures\n"));            
107                 return NULL;
108         }
109
110         conn = (connection_struct *)malloc(sizeof(*conn));
111         if (!conn) return NULL;
112
113         ZERO_STRUCTP(conn);
114         conn->cnum = i;
115
116         bitmap_set(bmap, i);
117
118         num_open++;
119
120         string_set(&conn->user,"");
121         string_set(&conn->dirpath,"");
122         string_set(&conn->connectpath,"");
123         string_set(&conn->origpath,"");
124         
125         DLIST_ADD(Connections, conn);
126
127         return conn;
128 }
129
130 /****************************************************************************
131 close all conn structures
132 ****************************************************************************/
133 void conn_close_all(void)
134 {
135         connection_struct *conn, *next;
136         for (conn=Connections;conn;conn=next) {
137                 next=conn->next;
138                 close_cnum(conn, (uint16)-1);
139         }
140 }
141
142 /****************************************************************************
143 idle inactive connections
144 ****************************************************************************/
145 BOOL conn_idle_all(time_t t, int deadtime)
146 {
147         BOOL allidle = True;
148         connection_struct *conn, *next;
149
150         for (conn=Connections;conn;conn=next) {
151                 next=conn->next;
152                 /* close dirptrs on connections that are idle */
153                 if ((t-conn->lastused) > DPTR_IDLE_TIMEOUT)
154                         dptr_idlecnum(conn);
155
156                 if (conn->num_files_open > 0 || 
157                     (t-conn->lastused)<deadtime)
158                         allidle = False;
159         }
160
161         return allidle;
162 }
163
164 /****************************************************************************
165 free a conn structure
166 ****************************************************************************/
167 void conn_free(connection_struct *conn)
168 {
169         /* Free vfs_connection_struct */
170             
171         if (conn->vfs_conn != NULL) {
172                 /* Close dlopen() handle */
173                 if (conn->vfs_conn->dl_handle) {
174                         dlclose(conn->vfs_conn->dl_handle);
175                 }
176                 if (conn->vfs_conn->groups != NULL) {
177                         free(conn->vfs_conn->groups);
178                 }
179                 free(conn->vfs_conn);
180         }
181
182         DLIST_REMOVE(Connections, conn);
183
184         if (conn->ngroups && conn->groups) {
185                 free(conn->groups);
186                 conn->groups = NULL;
187                 conn->ngroups = 0;
188         }
189
190         free_namearray(conn->veto_list);
191         free_namearray(conn->hide_list);
192         free_namearray(conn->veto_oplock_list);
193         
194         string_free(&conn->user);
195         string_free(&conn->dirpath);
196         string_free(&conn->connectpath);
197         string_free(&conn->origpath);
198
199         bitmap_clear(bmap, conn->cnum);
200         num_open--;
201
202         ZERO_STRUCTP(conn);
203         free(conn);
204 }
205
206 #undef OLD_NTDOMAIN