r14739: keep the last request time for the smbsrv_connection,
[jelmer/samba4-debian.git] / source / 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 "dlinklist.h"
24 #include "smb_server/smb_server.h"
25 #include "smbd/service_stream.h"
26 #include "ntvfs/ntvfs.h"
27
28
29 /****************************************************************************
30 init the tcon structures
31 ****************************************************************************/
32 static NTSTATUS smbsrv_init_tcons(struct smbsrv_tcons_context *tcons_ctx, TALLOC_CTX *mem_ctx, uint32_t limit)
33 {
34         /* 
35          * the idr_* functions take 'int' as limit,
36          * and only work with a max limit 0x00FFFFFF
37          */
38         limit &= 0x00FFFFFF;
39
40         tcons_ctx->idtree_tid   = idr_init(mem_ctx);
41         NT_STATUS_HAVE_NO_MEMORY(tcons_ctx->idtree_tid);
42         tcons_ctx->idtree_limit = limit;
43         tcons_ctx->list         = NULL;
44
45         return NT_STATUS_OK;
46 }
47
48 NTSTATUS smbsrv_smb_init_tcons(struct smbsrv_connection *smb_conn)
49 {
50         return smbsrv_init_tcons(&smb_conn->smb_tcons, smb_conn, UINT16_MAX);
51 }
52
53 NTSTATUS smbsrv_smb2_init_tcons(struct smbsrv_session *smb_sess)
54 {
55         return smbsrv_init_tcons(&smb_sess->smb2_tcons, smb_sess, UINT32_MAX);
56 }
57
58 /****************************************************************************
59 find a tcon given a tid for SMB
60 ****************************************************************************/
61 static struct smbsrv_tcon *smbsrv_tcon_find(struct smbsrv_tcons_context *tcons_ctx,
62                                             uint32_t tid, struct timeval request_time)
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         if (!tcon) return NULL;
76
77         tcon->statistics.last_request_time = request_time;
78
79         return tcon;
80 }
81
82 struct smbsrv_tcon *smbsrv_smb_tcon_find(struct smbsrv_connection *smb_conn,
83                                          uint32_t tid, struct timeval request_time)
84 {
85         return smbsrv_tcon_find(&smb_conn->smb_tcons, tid, request_time);
86 }
87
88 struct smbsrv_tcon *smbsrv_smb2_tcon_find(struct smbsrv_session *smb_sess,
89                                           uint32_t tid, struct timeval request_time)
90 {
91         if (!smb_sess) return NULL;
92         return smbsrv_tcon_find(&smb_sess->smb2_tcons, tid, request_time);
93 }
94
95 /*
96   destroy a connection structure
97 */
98 static int smbsrv_tcon_destructor(void *ptr)
99 {
100         struct smbsrv_tcon *tcon = talloc_get_type(ptr, struct smbsrv_tcon);
101         struct smbsrv_tcons_context *tcons_ctx;
102         struct socket_address *client_addr;
103
104         client_addr = socket_get_peer_addr(tcon->smb_conn->connection->socket, ptr);
105         DEBUG(3,("%s closed connection to service %s\n",
106                  client_addr ? client_addr->addr : "(unknown)",
107                  tcon->share_name));
108
109         /* tell the ntvfs backend that we are disconnecting */
110         if (tcon->ntvfs) {
111                 ntvfs_disconnect(tcon->ntvfs);
112         }
113
114         if (tcon->smb2.session) {
115                 tcons_ctx = &tcon->smb2.session->smb2_tcons;
116         } else {
117                 tcons_ctx = &tcon->smb_conn->smb_tcons;
118         }
119
120         idr_remove(tcons_ctx->idtree_tid, tcon->tid);
121         DLIST_REMOVE(tcons_ctx->list, tcon);
122         return 0;
123 }
124
125 /*
126   find first available connection slot
127 */
128 static struct smbsrv_tcon *smbsrv_tcon_new(struct smbsrv_connection *smb_conn,
129                                            struct smbsrv_session *smb_sess,
130                                            const char *share_name)
131 {
132         TALLOC_CTX *mem_ctx;
133         struct smbsrv_tcons_context *tcons_ctx;
134         struct smbsrv_tcon *tcon;
135         int i;
136
137         if (smb_sess) {
138                 mem_ctx = smb_sess;
139                 tcons_ctx = &smb_sess->smb2_tcons;
140         } else {
141                 mem_ctx = smb_conn;
142                 tcons_ctx = &smb_conn->smb_tcons;
143         }
144
145         tcon = talloc_zero(mem_ctx, struct smbsrv_tcon);
146         if (!tcon) return NULL;
147         tcon->smb_conn          = smb_conn;
148         tcon->smb2.session      = smb_sess;
149         tcon->share_name        = talloc_strdup(tcon, share_name);
150         if (!tcon->share_name) goto failed;
151
152         i = idr_get_new_random(tcons_ctx->idtree_tid, tcon, tcons_ctx->idtree_limit);
153         if (i == -1) {
154                 DEBUG(1,("ERROR! Out of connection structures\n"));
155                 goto failed;
156         }
157         tcon->tid = i;
158
159         DLIST_ADD(tcons_ctx->list, tcon);
160         talloc_set_destructor(tcon, smbsrv_tcon_destructor);
161
162         /* now fill in some statistics */
163         tcon->statistics.connect_time = timeval_current();
164
165         return tcon;
166
167 failed:
168         talloc_free(tcon);
169         return NULL;
170 }
171
172 struct smbsrv_tcon *smbsrv_smb_tcon_new(struct smbsrv_connection *smb_conn, const char *share_name)
173 {
174         return smbsrv_tcon_new(smb_conn, NULL, share_name);
175 }
176
177 struct smbsrv_tcon *smbsrv_smb2_tcon_new(struct smbsrv_session *smb_sess, const char *share_name)
178 {
179         return smbsrv_tcon_new(smb_sess->smb_conn, smb_sess, share_name);
180 }