r12528: Add seperate proto headers for ntvfs, tdr, smb_server and nbt_server.
[samba.git] / source4 / smb_server / tcon.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 #include "smbd/service_stream.h"
27 #include "ntvfs/ntvfs.h"
28
29
30 /****************************************************************************
31 init the tcon structures
32 ****************************************************************************/
33 static NTSTATUS smbsrv_init_tcons(struct smbsrv_tcons_context *tcons_ctx, TALLOC_CTX *mem_ctx, uint32_t limit)
34 {
35         /* 
36          * the idr_* functions take 'int' as limit,
37          * and only work with a max limit 0x00FFFFFF
38          */
39         limit &= 0x00FFFFFF;
40
41         tcons_ctx->idtree_tid   = idr_init(mem_ctx);
42         NT_STATUS_HAVE_NO_MEMORY(tcons_ctx->idtree_tid);
43         tcons_ctx->idtree_limit = limit;
44         tcons_ctx->list         = NULL;
45
46         return NT_STATUS_OK;
47 }
48
49 NTSTATUS smbsrv_smb_init_tcons(struct smbsrv_connection *smb_conn)
50 {
51         return smbsrv_init_tcons(&smb_conn->smb_tcons, smb_conn, UINT16_MAX);
52 }
53
54 NTSTATUS smbsrv_smb2_init_tcons(struct smbsrv_session *smb_sess)
55 {
56         return smbsrv_init_tcons(&smb_sess->smb2_tcons, smb_sess, UINT32_MAX);
57 }
58
59 /****************************************************************************
60 find a tcon given a tid for SMB
61 ****************************************************************************/
62 static struct smbsrv_tcon *smbsrv_tcon_find(struct smbsrv_tcons_context *tcons_ctx, uint32_t tid)
63 {
64         void *p;
65         struct smbsrv_tcon *tcon;
66
67         if (tid == 0) return NULL;
68
69         if (tid > tcons_ctx->idtree_limit) return NULL;
70
71         p = idr_find(tcons_ctx->idtree_tid, tid);
72         if (!p) return NULL;
73
74         tcon = talloc_get_type(p, struct smbsrv_tcon);
75
76         return tcon;
77 }
78
79 struct smbsrv_tcon *smbsrv_smb_tcon_find(struct smbsrv_connection *smb_conn, uint32_t tid)
80 {
81         return smbsrv_tcon_find(&smb_conn->smb_tcons, tid);
82 }
83
84 struct smbsrv_tcon *smbsrv_smb2_tcon_find(struct smbsrv_session *smb_sess, uint32_t tid)
85 {
86         if (!smb_sess) return NULL;
87         return smbsrv_tcon_find(&smb_sess->smb2_tcons, tid);
88 }
89
90 /*
91   destroy a connection structure
92 */
93 static int smbsrv_tcon_destructor(void *ptr)
94 {
95         struct smbsrv_tcon *tcon = talloc_get_type(ptr, struct smbsrv_tcon);
96         struct smbsrv_tcons_context *tcons_ctx;
97
98         DEBUG(3,("%s closed connection to service %s\n",
99                  socket_get_peer_addr(tcon->smb_conn->connection->socket, tcon),
100                  lp_servicename(tcon->service)));
101
102         /* tell the ntvfs backend that we are disconnecting */
103         if (tcon->ntvfs_ctx) {
104                 ntvfs_disconnect(tcon);
105         }
106
107         if (tcon->smb2.session) {
108                 tcons_ctx = &tcon->smb2.session->smb2_tcons;
109         } else {
110                 tcons_ctx = &tcon->smb_conn->smb_tcons;
111         }
112
113         idr_remove(tcons_ctx->idtree_tid, tcon->tid);
114         DLIST_REMOVE(tcons_ctx->list, tcon);
115         return 0;
116 }
117
118 /*
119   find first available connection slot
120 */
121 static struct smbsrv_tcon *smbsrv_tcon_new(struct smbsrv_connection *smb_conn, struct smbsrv_session *smb_sess)
122 {
123         TALLOC_CTX *mem_ctx;
124         struct smbsrv_tcons_context *tcons_ctx;
125         struct smbsrv_tcon *tcon;
126         int i;
127
128         if (smb_sess) {
129                 mem_ctx = smb_sess;
130                 tcons_ctx = &smb_sess->smb2_tcons;
131         } else {
132                 mem_ctx = smb_conn;
133                 tcons_ctx = &smb_conn->smb_tcons;
134         }
135
136         tcon = talloc_zero(mem_ctx, struct smbsrv_tcon);
137         if (!tcon) return NULL;
138         tcon->smb_conn          = smb_conn;
139         tcon->smb2.session      = smb_sess;
140
141         i = idr_get_new_random(tcons_ctx->idtree_tid, tcon, tcons_ctx->idtree_limit);
142         if (i == -1) {
143                 DEBUG(1,("ERROR! Out of connection structures\n"));
144                 return NULL;
145         }
146         tcon->tid = i;
147
148         DLIST_ADD(tcons_ctx->list, tcon);
149         talloc_set_destructor(tcon, smbsrv_tcon_destructor);
150
151         /* now fill in some statistics */
152         tcon->statistics.connect_time = timeval_current();
153
154         return tcon;
155 }
156
157 struct smbsrv_tcon *smbsrv_smb_tcon_new(struct smbsrv_connection *smb_conn)
158 {
159         return smbsrv_tcon_new(smb_conn, NULL);
160 }
161
162 struct smbsrv_tcon *smbsrv_smb2_tcon_new(struct smbsrv_session *smb_sess)
163 {
164         return smbsrv_tcon_new(smb_sess->smb_conn, smb_sess);
165 }