using read_with_timeout(), min data size 16 bytes (DCE/RPC header), max
[amitay/samba.git] / source3 / rpc_server / srv_pipe.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1998
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
8  *  Copyright (C) Paul Ashton                  1997-1998.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 /*  this module apparently provides an implementation of DCE/RPC over a
26  *  named pipe (IPC$ connection using SMBtrans).  details of DCE/RPC
27  *  documentation are available (in on-line form) from the X-Open group.
28  *
29  *  this module should provide a level of abstraction between SMB
30  *  and DCE/RPC, while minimising the amount of mallocs, unnecessary
31  *  data copies, and network traffic.
32  *
33  *  in this version, which takes a "let's learn what's going on and
34  *  get something running" approach, there is additional network
35  *  traffic generated, but the code should be easier to understand...
36  *
37  *  ... if you read the docs.  or stare at packets for weeks on end.
38  *
39  */
40
41 #include "includes.h"
42 #include "nterr.h"
43
44 extern int DEBUGLEVEL;
45
46 /*******************************************************************
47  entry point from msrpc to smb.  adds data received to pdu; checks
48  pdu; hands pdu off to msrpc, which gets a pdu back (except in the
49  case of the RPC_BINDCONT pdu).
50  ********************************************************************/
51 BOOL readwrite_pipe(pipes_struct *p, char *data, int len,
52                 char **rdata, int *rlen)
53 {
54         DEBUG(10,("rpc_to_smb_readwrite: len %d\n", len));
55
56         if (write(p->m->fd, data, len) != len)
57         {
58                 return False;
59         }
60
61         if ((*rlen) == 0)
62         {
63                 return False;
64         }
65
66         (*rdata) = (char*)Realloc((*rdata), (*rlen));
67         if ((*rdata) == NULL)
68         {
69                 return False;
70         }
71
72         /* read a minimum of an rpc header, then wait for up to 10 seconds
73          * to read up to a maximum of the SMBtrans max data size
74          */
75         (*rlen) = read_with_timeout(p->m->fd, (*rdata), 16, (*rlen), 10000);
76         if ((*rlen) < 0)
77         {
78                 return False;
79         }
80         (*rdata) = (char*)Realloc((*rdata), (*rlen));
81         if ((*rdata) == NULL)
82         {
83                 return False;
84         }
85         return True;
86 }
87
88 /****************************************************************************
89  writes data to a pipe.
90  ****************************************************************************/
91 ssize_t write_pipe(pipes_struct *p, char *data, size_t n)
92 {
93         DEBUG(6,("write_pipe: %x", p->pnum));
94         DEBUG(6,("name: %s open: %s len: %d",
95                  p->name, BOOLSTR(p->open), n));
96
97         dump_data(50, data, n);
98
99         return write(p->m->fd, data, n);
100 }
101
102
103 /****************************************************************************
104  reads data from a pipe.
105
106  headers are interspersed with the data at regular intervals.  by the time
107  this function is called, the start of the data could possibly have been
108  read by an SMBtrans (file_offset != 0).
109
110  ****************************************************************************/
111 int read_pipe(pipes_struct *p, char *data, int n)
112 {
113         DEBUG(6,("read_pipe: %x name: %s open: %s len: %d",
114                  p->pnum, p->name, BOOLSTR(p->open), n));
115
116         if (!p || !p->open)
117         {
118                 DEBUG(6,("pipe not open\n"));
119                 return -1;              
120         }
121
122         return read_data(p->m->fd, data, n);
123 }
124