rpcrt4: Add a STUBLESS_FREE phase for freeing the allocated memory in the server...
[wine] / dlls / rpcrt4 / rpcss_np_client.c
1 /*
2  * RPCSS named pipe client implementation
3  *
4  * Copyright (C) 2002 Greg Turner
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/rpcss_shared.h"
27 #include "wine/debug.h"
28
29 #include "rpc_binding.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(ole);
32
33 HANDLE RPCRT4_RpcssNPConnect(void)
34 {
35   HANDLE the_pipe;
36   DWORD dwmode, wait_result;
37   HANDLE master_mutex = RPCRT4_GetMasterMutex();
38   
39   TRACE("\n");
40
41   while (TRUE) {
42
43     wait_result = WaitForSingleObject(master_mutex, MASTER_MUTEX_TIMEOUT);
44     switch (wait_result) {
45       case WAIT_ABANDONED: 
46       case WAIT_OBJECT_0:
47         break;
48       case WAIT_FAILED:
49       case WAIT_TIMEOUT:
50       default: 
51         ERR("This should never happen: couldn't enter mutex.\n");
52         return NULL;
53     }
54
55     /* try to open the client side of the named pipe. */
56     the_pipe = CreateFileA(
57       NAME_RPCSS_NAMED_PIPE,           /* pipe name */
58       GENERIC_READ | GENERIC_WRITE,    /* r/w access */
59       0,                               /* no sharing */
60       NULL,                            /* no security attributes */
61       OPEN_EXISTING,                   /* open an existing pipe */
62       0,                               /* default attributes */
63       NULL                             /* no template file */
64     );
65
66     if (the_pipe != INVALID_HANDLE_VALUE)
67       break;
68
69     if (GetLastError() != ERROR_PIPE_BUSY) {
70       WARN("Unable to open named pipe %s (assuming unavailable).\n", 
71         debugstr_a(NAME_RPCSS_NAMED_PIPE));
72       break;
73     }
74
75     WARN("Named pipe busy (will wait)\n");
76     
77     if (!ReleaseMutex(master_mutex))
78       ERR("Failed to release master mutex.  Expect deadlock.\n");
79
80     /* wait for the named pipe.  We are only willing to wait for 5 seconds.
81        It should be available /very/ soon. */
82     if (! WaitNamedPipeA(NAME_RPCSS_NAMED_PIPE, MASTER_MUTEX_WAITNAMEDPIPE_TIMEOUT))
83     {
84       ERR("Named pipe unavailable after waiting.  Something is probably wrong.\n");
85       break;
86     }
87
88   }
89
90   if (the_pipe != INVALID_HANDLE_VALUE) {
91     dwmode = PIPE_READMODE_MESSAGE;
92     /* SetNamedPipeHandleState not implemented ATM, but still seems to work somehow. */
93     if (! SetNamedPipeHandleState(the_pipe, &dwmode, NULL, NULL))
94       WARN("Failed to set pipe handle state\n");
95   }
96
97   if (!ReleaseMutex(master_mutex))
98     ERR("Uh oh, failed to leave the RPC Master Mutex!\n");
99
100   return the_pipe;
101 }
102
103 BOOL RPCRT4_SendReceiveNPMsg(HANDLE np, PRPCSS_NP_MESSAGE msg, char *vardata, PRPCSS_NP_REPLY reply)
104 {
105   DWORD count;
106   UINT32 payload_offset;
107   RPCSS_NP_MESSAGE vardata_payload_msg;
108
109   TRACE("(np == %p, msg == %p, vardata == %p, reply == %p)\n",
110     np, msg, vardata, reply);
111
112   if (! WriteFile(np, msg, sizeof(RPCSS_NP_MESSAGE), &count, NULL)) {
113     ERR("write failed.\n");
114     return FALSE;
115   }
116
117   if (count != sizeof(RPCSS_NP_MESSAGE)) {
118     ERR("write count mismatch.\n");
119     return FALSE;
120   }
121
122   /* process the vardata payload if necessary */
123   vardata_payload_msg.message_type = RPCSS_NP_MESSAGE_TYPEID_VARDATAPAYLOADMSG;
124   vardata_payload_msg.vardata_payload_size = 0; /* meaningless */
125   for ( payload_offset = 0; payload_offset < msg->vardata_payload_size; 
126         payload_offset += VARDATA_PAYLOAD_BYTES ) {
127     TRACE("sending vardata payload.  vd=%p, po=%d, ps=%d\n", vardata,
128       payload_offset, msg->vardata_payload_size);
129     ZeroMemory(vardata_payload_msg.message.vardatapayloadmsg.payload, VARDATA_PAYLOAD_BYTES);
130     CopyMemory(vardata_payload_msg.message.vardatapayloadmsg.payload,
131                vardata,
132                min( VARDATA_PAYLOAD_BYTES, msg->vardata_payload_size - payload_offset ));
133     vardata += VARDATA_PAYLOAD_BYTES;
134     if (! WriteFile(np, &vardata_payload_msg, sizeof(RPCSS_NP_MESSAGE), &count, NULL)) {
135       ERR("vardata write failed at %u bytes.\n", payload_offset);
136       return FALSE;
137     }
138   }
139   
140   if (! ReadFile(np, reply, sizeof(RPCSS_NP_REPLY), &count, NULL)) {
141     ERR("read failed.\n");
142     return FALSE;
143   }
144
145   if (count != sizeof(RPCSS_NP_REPLY)) {
146     ERR("read count mismatch. got %d.\n", count);
147     return FALSE;
148   }
149
150   /* message execution was successful */
151   return TRUE;
152 }