added a basic dcerpc endpoint mapper to Samba4. Currently only
[nivanova/samba-autobuild/.git] / source4 / 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
25 /*
26   allocate a new rpc handle
27 */
28 struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_state *dce, 
29                                         uint8 handle_type)
30 {
31         TALLOC_CTX *mem_ctx;
32         struct dcesrv_handle *h;
33
34         mem_ctx = talloc_init("rpc handle type %d\n", handle_type);
35         if (!mem_ctx) {
36                 return NULL;
37         }
38         h = talloc(mem_ctx, sizeof(*h));
39         if (!h) {
40                 talloc_destroy(mem_ctx);
41                 return NULL;
42         }
43         h->mem_ctx = mem_ctx;
44         h->data = NULL;
45
46         memset(h->wire_handle.data, 'H', sizeof(h->wire_handle.data));
47         strncpy(h->wire_handle.data, dce->ndr->name, 11);
48         h->wire_handle.data[11] = handle_type;
49         
50         /* TODO: check for wraparound here */
51         SIVAL(&h->wire_handle.data, 12, random());
52         SIVAL(&h->wire_handle.data, 16, dce->next_handle);
53         dce->next_handle++;     
54
55         DLIST_ADD(dce->handles, h);
56
57         return h;
58 }
59
60 /*
61   destroy a rpc handle
62 */
63 void dcesrv_handle_destroy(struct dcesrv_state *dce, 
64                            struct dcesrv_handle *h)
65 {
66         DLIST_REMOVE(dce->handles, h);
67         talloc_destroy(h->mem_ctx);
68 }
69
70
71 /*
72   find an internal handle given a wire handle. If the wire handle is NULL then
73   allocate a new handle
74 */
75 struct dcesrv_handle *dcesrv_handle_fetch(struct dcesrv_state *dce, 
76                                           struct policy_handle *p,
77                                           uint8 handle_type)
78 {
79         struct dcesrv_handle *h;
80
81         if (all_zero(p->data, sizeof(p->data))) {
82                 return dcesrv_handle_new(dce, handle_type);
83         }
84
85         for (h=dce->handles; h; h=h->next) {
86                 if (memcmp(h->wire_handle.data, p->data, sizeof(p->data)) == 0) {
87                         return h;
88                 }
89         }
90
91         return NULL;
92 }