r5185: make all the events data structures private to events.c. This will
[idra/samba.git] / source4 / rpc_server / echo / rpc_echo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the echo pipe
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan (metze) Metzmacher 2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "librpc/gen_ndr/ndr_echo.h"
27 #include "events.h"
28
29
30 static NTSTATUS echo_AddOne(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_AddOne *r)
31 {
32         *r->out.v = *r->in.v + 1;
33         return NT_STATUS_OK;
34 }
35
36 static NTSTATUS echo_EchoData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_EchoData *r)
37 {
38         if (!r->in.len) {
39                 return NT_STATUS_OK;
40         }
41
42         r->out.out_data = talloc_memdup(mem_ctx, r->in.in_data, r->in.len);
43         if (!r->out.out_data) {
44                 return NT_STATUS_NO_MEMORY;
45         }
46
47         return NT_STATUS_OK;
48 }
49
50 static NTSTATUS echo_SinkData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_SinkData *r)
51 {
52         return NT_STATUS_OK;
53 }
54
55 static NTSTATUS echo_SourceData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_SourceData *r)
56 {
57         int i;
58         for (i=0;i<r->in.len;i++) {
59                 r->out.data[i] = i;
60         }
61
62         return NT_STATUS_OK;
63 }
64
65 static NTSTATUS echo_TestCall(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestCall *r)
66 {
67         r->out.s2 = "this is a test string";
68         
69         return NT_STATUS_OK;
70 }
71
72 static NTSTATUS echo_TestCall2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestCall2 *r)
73 {
74         r->out.info = talloc(mem_ctx, union echo_Info);
75         if (!r->out.info) {
76                 return NT_STATUS_NO_MEMORY;
77         }
78
79         switch (r->in.level) {
80         case 1:
81                 r->out.info->info1.v = 10;
82                 break;
83         case 2:
84                 r->out.info->info2.v = 20;
85                 break;
86         case 3:
87                 r->out.info->info3.v = 30;
88                 break;
89         case 4:
90                 r->out.info->info4.v = 40;
91                 break;
92         case 5:
93                 r->out.info->info5.v1 = 50;
94                 r->out.info->info5.v2 = 60;
95                 break;
96         case 6:
97                 r->out.info->info6.v1 = 70;
98                 r->out.info->info6.info1.v= 80;
99                 break;
100         case 7:
101                 r->out.info->info7.v1 = 80;
102                 r->out.info->info7.info4.v = 90;
103                 break;
104         default:
105                 return NT_STATUS_INVALID_LEVEL;
106         }
107
108         return NT_STATUS_OK;
109 }
110
111 static NTSTATUS echo_TestEnum(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestEnum *r)
112 {
113         r->out.foo2->e1 = ECHO_ENUM2;
114         return NT_STATUS_OK;
115 }
116
117 struct echo_TestSleep_private {
118         struct dcesrv_call_state *dce_call;
119         struct echo_TestSleep *r;
120 };
121
122 static void echo_TestSleep_handler(struct event_context *ev, struct timed_event *te, 
123                                    struct timeval t, void *private)
124 {
125         struct echo_TestSleep_private *p = talloc_get_type(private, 
126                                                            struct echo_TestSleep_private);
127         struct echo_TestSleep *r = p->r;
128         NTSTATUS status;
129
130         r->out.result = r->in.seconds;
131
132         status = dcesrv_reply(p->dce_call);
133         if (!NT_STATUS_IS_OK(status)) {
134                 DEBUG(0,("echo_TestSleep_handler: dcesrv_reply() failed - %s\n",
135                         nt_errstr(status)));
136         }
137 }
138
139 static long echo_TestSleep(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestSleep *r)
140 {
141         struct echo_TestSleep_private *p;
142
143         if (!(dce_call->state_flags & DCESRV_CALL_STATE_FLAG_MAY_ASYNC)) {
144                 /* we're not allowed to reply async */
145                 sleep(r->in.seconds);
146                 return r->in.seconds;
147         }
148
149         /* we're allowed to reply async */
150         p = talloc(mem_ctx, struct echo_TestSleep_private);
151         if (!p) {
152                 return 0;
153         }
154
155         p->dce_call     = dce_call;
156         p->r            = r;
157
158         event_add_timed(dce_call->event_ctx, p, 
159                         timeval_add(&dce_call->time, r->in.seconds, 0),
160                         echo_TestSleep_handler, p);
161
162         dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
163         return 0;
164 }
165
166 /* include the generated boilerplate */
167 #include "librpc/gen_ndr/ndr_echo_s.c"