r13208: Clearly separate named pipes from the IPC$ NTVFS type.
[samba.git] / source4 / ntvfs / ipc / np_echo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    DCE/RPC over named pipes support (glue between dcerpc and smb servers)
4
5    Copyright (C) Jelmer Vernooij 2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "lib/socket/socket.h"
24 #include "lib/events/events.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "ntvfs/ipc/ipc.h"
27
28 static NTSTATUS echo_pipe_open (void *context_data, const char *path, struct auth_session_info *session_info, struct stream_connection *srv_conn, TALLOC_CTX *mem_ctx, void **private_data)
29 {
30         *private_data = talloc_zero(mem_ctx, DATA_BLOB);
31
32         return NT_STATUS_OK;
33 }
34
35 static NTSTATUS echo_pipe_trans(void *private_data, DATA_BLOB *in, DATA_BLOB *out)
36 {
37         memcpy(out->data, in->data, MIN(out->length,in->length));
38
39         return NT_STATUS_OK;
40 }
41
42 static NTSTATUS echo_pipe_write(void *private_data, DATA_BLOB *out)
43 {
44         DATA_BLOB *cache = private_data;
45         return data_blob_append(cache, cache, out->data, out->length);
46 }
47
48 static NTSTATUS echo_pipe_read(void *private_data, DATA_BLOB *in)
49 {
50         uint8_t *newdata;
51         DATA_BLOB *cache = private_data;
52         uint32_t numread = MIN(in->length, cache->length);
53
54         memcpy(in->data, cache->data, numread);
55
56         cache->length -= numread;
57         newdata = talloc_memdup(cache, cache+numread, cache->length);
58         if (newdata == NULL)
59                 return NT_STATUS_NO_MEMORY;
60
61         talloc_free(cache->data);
62         cache->data = newdata;
63
64         return NT_STATUS_OK;
65 }
66
67 const struct named_pipe_ops echo_pipe_ops = {
68         .open = echo_pipe_open,
69         .write = echo_pipe_write,
70         .read = echo_pipe_read,
71         .trans = echo_pipe_trans
72 };
73
74 NTSTATUS np_echo_init(void)
75 {
76         return named_pipe_listen("\\PIPE\\NPECHO", &echo_pipe_ops, NULL);
77 }