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/>.
21 #include "smb_server/smb_server.h"
24 /****************************************************************************
25 init the handle structures
26 ****************************************************************************/
27 NTSTATUS
smbsrv_init_handles(struct smbsrv_tcon
*tcon
, uint32_t limit
)
30 * the idr_* functions take 'int' as limit,
31 * and only work with a max limit 0x00FFFFFF
35 tcon
->handles
.idtree_hid
= idr_init(tcon
);
36 NT_STATUS_HAVE_NO_MEMORY(tcon
->handles
.idtree_hid
);
37 tcon
->handles
.idtree_limit
= limit
;
38 tcon
->handles
.list
= NULL
;
43 /****************************************************************************
44 find a handle given a handle id
45 ****************************************************************************/
46 static struct smbsrv_handle
*smbsrv_handle_find(struct smbsrv_handles_context
*handles_ctx
,
47 uint32_t hid
, struct timeval request_time
)
50 struct smbsrv_handle
*handle
;
52 if (hid
== 0) return NULL
;
54 if (hid
> handles_ctx
->idtree_limit
) return NULL
;
56 p
= idr_find(handles_ctx
->idtree_hid
, hid
);
59 handle
= talloc_get_type(p
, struct smbsrv_handle
);
60 if (!handle
) return NULL
;
62 /* only give it away when the ntvfs subsystem has made the handle valid */
63 if (!handle
->ntvfs
) return NULL
;
65 handle
->statistics
.last_use_time
= request_time
;
70 struct smbsrv_handle
*smbsrv_smb_handle_find(struct smbsrv_tcon
*smb_tcon
,
71 uint16_t fnum
, struct timeval request_time
)
73 return smbsrv_handle_find(&smb_tcon
->handles
, fnum
, request_time
);
76 struct smbsrv_handle
*smbsrv_smb2_handle_find(struct smbsrv_tcon
*smb_tcon
,
77 uint32_t hid
, struct timeval request_time
)
79 return smbsrv_handle_find(&smb_tcon
->handles
, hid
, request_time
);
83 destroy a connection structure
85 static int smbsrv_handle_destructor(struct smbsrv_handle
*handle
)
87 struct smbsrv_handles_context
*handles_ctx
;
89 handles_ctx
= &handle
->tcon
->handles
;
91 idr_remove(handles_ctx
->idtree_hid
, handle
->hid
);
92 DLIST_REMOVE(handles_ctx
->list
, handle
);
93 DLIST_REMOVE(handle
->session
->handles
, &handle
->session_item
);
95 /* tell the ntvfs backend that we are disconnecting */
97 talloc_free(handle
->ntvfs
);
105 find first available handle slot
107 struct smbsrv_handle
*smbsrv_handle_new(struct smbsrv_session
*session
,
108 struct smbsrv_tcon
*tcon
,
110 struct timeval request_time
)
112 struct smbsrv_handles_context
*handles_ctx
= &tcon
->handles
;
113 struct smbsrv_handle
*handle
;
116 handle
= talloc_zero(mem_ctx
, struct smbsrv_handle
);
117 if (!handle
) return NULL
;
119 handle
->session
= session
;
121 i
= idr_get_new_above(handles_ctx
->idtree_hid
, handle
, 1, handles_ctx
->idtree_limit
);
123 DEBUG(1,("ERROR! Out of handle structures\n"));
127 handle
->session_item
.handle
= handle
;
129 DLIST_ADD(handles_ctx
->list
, handle
);
130 DLIST_ADD(session
->handles
, &handle
->session_item
);
131 talloc_set_destructor(handle
, smbsrv_handle_destructor
);
133 /* now fill in some statistics */
134 handle
->statistics
.open_time
= request_time
;
135 handle
->statistics
.last_use_time
= request_time
;