b3970b252c221f65761dd19b25d1fc1528042660
[bbaumbach/samba-autobuild/.git] / source4 / 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 #include "system/filesys.h"
24
25 /****************************************************************************
26 init the tcon structures
27 ****************************************************************************/
28 void conn_init(struct smbsrv_connection *smb_conn)
29 {
30         smb_conn->tree.idtree_tid = idr_init(smb_conn);
31 }
32
33 /****************************************************************************
34 find a tcon given a cnum
35 ****************************************************************************/
36 struct smbsrv_tcon *conn_find(struct smbsrv_connection *smb_conn, uint_t cnum)
37 {
38         return idr_find(smb_conn->tree.idtree_tid, cnum);
39 }
40
41 /*
42   destroy a connection structure
43 */
44 static int conn_destructor(void *ptr)
45 {
46         struct smbsrv_tcon *tcon = ptr;
47         idr_remove(tcon->smb_conn->tree.idtree_tid, tcon->cnum);
48         DLIST_REMOVE(tcon->smb_conn->tree.tcons, tcon);
49         return 0;
50 }
51
52 /*
53   find first available connection slot
54 */
55 struct smbsrv_tcon *conn_new(struct smbsrv_connection *smb_conn)
56 {
57         struct smbsrv_tcon *tcon;
58         int i;
59
60         tcon = talloc_zero_p(smb_conn, struct smbsrv_tcon);
61         if (!tcon) return NULL;
62
63         i = idr_get_new(smb_conn->tree.idtree_tid, tcon, UINT16_MAX);
64         if (i == -1) {
65                 DEBUG(1,("ERROR! Out of connection structures\n"));            
66                 return NULL;
67         }
68
69         tcon->cnum = i;
70         tcon->smb_conn = smb_conn;
71
72         talloc_set_destructor(tcon, conn_destructor);
73
74         DLIST_ADD(smb_conn->tree.tcons, tcon);
75
76         return tcon;
77 }
78
79 /****************************************************************************
80 close all tcon structures
81 ****************************************************************************/
82 void conn_close_all(struct smbsrv_connection *smb_conn)
83 {
84         struct smbsrv_tcon *tcon, *next;
85         for (tcon=smb_conn->tree.tcons;tcon;tcon=next) {
86                 next=tcon->next;
87                 close_cnum(tcon);
88         }
89 }
90
91
92 /****************************************************************************
93  Free a tcon structure.
94 ****************************************************************************/
95 void conn_free(struct smbsrv_connection *smb_conn, struct smbsrv_tcon *tcon)
96 {
97         talloc_free(tcon);
98 }
99