s4-netlogon: implement dcesrv_netr_DsRAddressToSitenamesExW
[Samba/aatanasov.git] / source4 / smb_server / handle.c
blob6ee4e163ad3d82aa648887005ce43e05e193cf78
1 /*
2 Unix SMB/CIFS implementation.
3 Manage smbsrv_handle structures
4 Copyright (C) Stefan Metzmacher 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "../lib/util/dlinklist.h"
22 #include "smb_server/smb_server.h"
23 #include "ntvfs/ntvfs.h"
26 /****************************************************************************
27 init the handle structures
28 ****************************************************************************/
29 NTSTATUS smbsrv_init_handles(struct smbsrv_tcon *tcon, uint32_t limit)
31 /*
32 * the idr_* functions take 'int' as limit,
33 * and only work with a max limit 0x00FFFFFF
35 limit &= 0x00FFFFFF;
37 tcon->handles.idtree_hid = idr_init(tcon);
38 NT_STATUS_HAVE_NO_MEMORY(tcon->handles.idtree_hid);
39 tcon->handles.idtree_limit = limit;
40 tcon->handles.list = NULL;
42 return NT_STATUS_OK;
45 /****************************************************************************
46 find a handle given a handle id
47 ****************************************************************************/
48 static struct smbsrv_handle *smbsrv_handle_find(struct smbsrv_handles_context *handles_ctx,
49 uint32_t hid, struct timeval request_time)
51 void *p;
52 struct smbsrv_handle *handle;
54 if (hid == 0) return NULL;
56 if (hid > handles_ctx->idtree_limit) return NULL;
58 p = idr_find(handles_ctx->idtree_hid, hid);
59 if (!p) return NULL;
61 handle = talloc_get_type(p, struct smbsrv_handle);
62 if (!handle) return NULL;
64 /* only give it away when the ntvfs subsystem has made the handle valid */
65 if (!handle->ntvfs) return NULL;
67 handle->statistics.last_use_time = request_time;
69 return handle;
72 struct smbsrv_handle *smbsrv_smb_handle_find(struct smbsrv_tcon *smb_tcon,
73 uint16_t fnum, struct timeval request_time)
75 return smbsrv_handle_find(&smb_tcon->handles, fnum, request_time);
78 struct smbsrv_handle *smbsrv_smb2_handle_find(struct smbsrv_tcon *smb_tcon,
79 uint32_t hid, struct timeval request_time)
81 return smbsrv_handle_find(&smb_tcon->handles, hid, request_time);
85 destroy a connection structure
87 static int smbsrv_handle_destructor(struct smbsrv_handle *handle)
89 struct smbsrv_handles_context *handles_ctx;
91 handles_ctx = &handle->tcon->handles;
93 idr_remove(handles_ctx->idtree_hid, handle->hid);
94 DLIST_REMOVE(handles_ctx->list, handle);
95 DLIST_REMOVE(handle->session->handles, &handle->session_item);
97 /* tell the ntvfs backend that we are disconnecting */
98 if (handle->ntvfs) {
99 talloc_free(handle->ntvfs);
100 handle->ntvfs = NULL;
103 return 0;
107 find first available handle slot
109 struct smbsrv_handle *smbsrv_handle_new(struct smbsrv_session *session,
110 struct smbsrv_tcon *tcon,
111 TALLOC_CTX *mem_ctx,
112 struct timeval request_time)
114 struct smbsrv_handles_context *handles_ctx = &tcon->handles;
115 struct smbsrv_handle *handle;
116 int i;
118 handle = talloc_zero(mem_ctx, struct smbsrv_handle);
119 if (!handle) return NULL;
120 handle->tcon = tcon;
121 handle->session = session;
123 i = idr_get_new_above(handles_ctx->idtree_hid, handle, 1, handles_ctx->idtree_limit);
124 if (i == -1) {
125 DEBUG(1,("ERROR! Out of handle structures\n"));
126 goto failed;
128 handle->hid = i;
129 handle->session_item.handle = handle;
131 DLIST_ADD(handles_ctx->list, handle);
132 DLIST_ADD(session->handles, &handle->session_item);
133 talloc_set_destructor(handle, smbsrv_handle_destructor);
135 /* now fill in some statistics */
136 handle->statistics.open_time = request_time;
137 handle->statistics.last_use_time = request_time;
139 return handle;
141 failed:
142 talloc_free(handle);
143 return NULL;