c70ed95915c1f05640eb7f9ca0419e5aced257fb
[abartlet/samba.git/.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 #include "dlinklist.h"
25 #include "smb_server/smb_server.h"
26
27
28 /****************************************************************************
29 init the tcon structures
30 ****************************************************************************/
31 void smbsrv_tcon_init(struct smbsrv_connection *smb_conn)
32 {
33         smb_conn->tree.idtree_tid = idr_init(smb_conn);
34 }
35
36 /****************************************************************************
37 find a tcon given a cnum
38 ****************************************************************************/
39 struct smbsrv_tcon *smbsrv_tcon_find(struct smbsrv_connection *smb_conn, uint_t tid)
40 {
41         return idr_find(smb_conn->tree.idtree_tid, tid);
42 }
43
44 /*
45   destroy a connection structure
46 */
47 static int smbsrv_tcon_destructor(void *ptr)
48 {
49         struct smbsrv_tcon *tcon = ptr;
50
51
52         DEBUG(3,("%s closed connection to service %s\n",
53                  socket_get_peer_addr(tcon->smb_conn->connection->socket, tcon),
54                  lp_servicename(SNUM(tcon))));
55
56         /* tell the ntvfs backend that we are disconnecting */
57         ntvfs_disconnect(tcon);
58
59         idr_remove(tcon->smb_conn->tree.idtree_tid, tcon->tid);
60         DLIST_REMOVE(tcon->smb_conn->tree.tcons, tcon);
61         return 0;
62 }
63
64 /*
65   find first available connection slot
66 */
67 struct smbsrv_tcon *smbsrv_tcon_new(struct smbsrv_connection *smb_conn)
68 {
69         struct smbsrv_tcon *tcon;
70         int i;
71
72         tcon = talloc_zero_p(smb_conn, struct smbsrv_tcon);
73         if (!tcon) return NULL;
74
75         i = idr_get_new(smb_conn->tree.idtree_tid, tcon, UINT16_MAX);
76         if (i == -1) {
77                 DEBUG(1,("ERROR! Out of connection structures\n"));            
78                 return NULL;
79         }
80
81         tcon->tid = i;
82         tcon->smb_conn = smb_conn;
83
84         talloc_set_destructor(tcon, smbsrv_tcon_destructor);
85
86         DLIST_ADD(smb_conn->tree.tcons, tcon);
87
88         return tcon;
89 }