r3463: separated out some more headers (asn_1.h, messages.h, dlinklist.h and ioctl.h)
[jelmer/samba4-debian.git] / source / rpc_server / handles.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    server side dcerpc handle code
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "dlinklist.h"
25
26 /*
27   allocate a new rpc handle
28 */
29 struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_connection *dce_conn, 
30                                         uint8_t handle_type)
31 {
32         struct dcesrv_handle *h;
33
34         h = talloc_p(dce_conn, struct dcesrv_handle);
35         if (!h) {
36                 return NULL;
37         }
38         h->data = NULL;
39         h->destroy = NULL;
40
41         h->wire_handle.handle_type = handle_type;
42         uuid_generate_random(&h->wire_handle.uuid);
43         
44         DLIST_ADD(dce_conn->handles, h);
45
46         return h;
47 }
48
49 /*
50   destroy a rpc handle
51 */
52 void dcesrv_handle_destroy(struct dcesrv_connection *dce_conn, 
53                            struct dcesrv_handle *h)
54 {
55         if (h->destroy) {
56                 h->destroy(dce_conn, h);
57         }
58         DLIST_REMOVE(dce_conn->handles, h);
59         talloc_free(h);
60 }
61
62
63 /*
64   find an internal handle given a wire handle. If the wire handle is NULL then
65   allocate a new handle
66 */
67 struct dcesrv_handle *dcesrv_handle_fetch(struct dcesrv_connection *dce_conn, 
68                                           struct policy_handle *p,
69                                           uint8_t handle_type)
70 {
71         struct dcesrv_handle *h;
72
73         if (policy_handle_empty(p)) {
74                 return dcesrv_handle_new(dce_conn, handle_type);
75         }
76
77         for (h=dce_conn->handles; h; h=h->next) {
78                 if (h->wire_handle.handle_type == p->handle_type &&
79                     uuid_equal(&p->uuid, &h->wire_handle.uuid)) {
80                         if (handle_type != DCESRV_HANDLE_ANY &&
81                             p->handle_type != handle_type) {
82                                 DEBUG(0,("client gave us the wrong handle type (%d should be %d)\n",
83                                          p->handle_type, handle_type));
84                                 return NULL;
85                         }
86                         return h;
87                 }
88         }
89
90         return NULL;
91 }