VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/win/svchlp.cpp@ 53082

Last change on this file since 53082 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 7.3 KB
Line 
1/** @file
2 * Definition of SVC Helper Process control routines.
3 */
4
5/*
6 * Copyright (C) 2006-2012 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#include "svchlp.h"
18
19//#include "HostImpl.h"
20#include "Logging.h"
21
22#include <VBox/err.h>
23
24int netIfNetworkInterfaceHelperServer (SVCHlpClient *aClient,
25 SVCHlpMsg::Code aMsgCode);
26
27using namespace com;
28
29enum { PipeBufSize = 1024 };
30
31////////////////////////////////////////////////////////////////////////////////
32
33/**
34 * GetLastError() is known to return NO_ERROR even after the Win32 API
35 * function (i.e. Write() to a non-connected server end of a pipe) returns
36 * FALSE... This method ensures that at least VERR_GENERAL_FAILURE is returned
37 * in cases like that. Intended to be called immediately after a failed API
38 * call.
39 */
40static inline int rtErrConvertFromWin32OnFailure()
41{
42 DWORD err = GetLastError();
43 return err == NO_ERROR ? VERR_GENERAL_FAILURE
44 : RTErrConvertFromWin32 (err);
45}
46
47////////////////////////////////////////////////////////////////////////////////
48
49SVCHlpClient::SVCHlpClient()
50 : mIsOpen (false), mIsServer (false)
51 , mReadEnd (NULL), mWriteEnd (NULL)
52{
53}
54
55SVCHlpClient::~SVCHlpClient()
56{
57 close();
58}
59
60int SVCHlpClient::create(const char *aName)
61{
62 AssertReturn(aName, VERR_INVALID_PARAMETER);
63
64 if (mIsOpen)
65 return VERR_WRONG_ORDER;
66
67 Bstr pipeName = Utf8StrFmt("\\\\.\\pipe\\%s", aName);
68
69 HANDLE pipe = CreateNamedPipe(pipeName.raw(),
70 PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
71 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
72 1, // PIPE_UNLIMITED_INSTANCES,
73 PipeBufSize, PipeBufSize,
74 NMPWAIT_USE_DEFAULT_WAIT,
75 NULL);
76
77 if (pipe == INVALID_HANDLE_VALUE)
78 rtErrConvertFromWin32OnFailure();
79
80 mIsOpen = true;
81 mIsServer = true;
82 mReadEnd = pipe;
83 mWriteEnd = pipe;
84 mName = aName;
85
86 return VINF_SUCCESS;
87}
88
89int SVCHlpClient::open(const char *aName)
90{
91 AssertReturn(aName, VERR_INVALID_PARAMETER);
92
93 if (mIsOpen)
94 return VERR_WRONG_ORDER;
95
96 Bstr pipeName = Utf8StrFmt("\\\\.\\pipe\\%s", aName);
97
98 HANDLE pipe = CreateFile(pipeName.raw(),
99 GENERIC_READ | GENERIC_WRITE,
100 0,
101 NULL,
102 OPEN_EXISTING,
103 0,
104 NULL);
105
106 if (pipe == INVALID_HANDLE_VALUE)
107 rtErrConvertFromWin32OnFailure();
108
109 mIsOpen = true;
110 mIsServer = false;
111 mReadEnd = pipe;
112 mWriteEnd = pipe;
113 mName = aName;
114
115 return VINF_SUCCESS;
116}
117
118int SVCHlpClient::connect()
119{
120 if (!mIsOpen || !mIsServer)
121 return VERR_WRONG_ORDER;
122
123 BOOL ok = ConnectNamedPipe (mReadEnd, NULL);
124 if (!ok && GetLastError() != ERROR_PIPE_CONNECTED)
125 rtErrConvertFromWin32OnFailure();
126
127 return VINF_SUCCESS;
128}
129
130int SVCHlpClient::close()
131{
132 if (!mIsOpen)
133 return VERR_WRONG_ORDER;
134
135 if (mWriteEnd != NULL && mWriteEnd != mReadEnd)
136 {
137 if (!CloseHandle (mWriteEnd))
138 rtErrConvertFromWin32OnFailure();
139 mWriteEnd = NULL;
140 }
141
142 if (mReadEnd != NULL)
143 {
144 if (!CloseHandle (mReadEnd))
145 rtErrConvertFromWin32OnFailure();
146 mReadEnd = NULL;
147 }
148
149 mIsOpen = false;
150 mIsServer = false;
151 mName.setNull();
152
153 return VINF_SUCCESS;
154}
155
156int SVCHlpClient::write (const void *aVal, size_t aLen)
157{
158 AssertReturn(aVal != NULL, VERR_INVALID_PARAMETER);
159 AssertReturn(aLen != 0, VERR_INVALID_PARAMETER);
160
161 if (!mIsOpen)
162 return VERR_WRONG_ORDER;
163
164 DWORD written = 0;
165 BOOL ok = WriteFile (mWriteEnd, aVal, (ULONG)aLen, &written, NULL);
166 AssertReturn(!ok || written == aLen, VERR_GENERAL_FAILURE);
167 return ok ? VINF_SUCCESS : rtErrConvertFromWin32OnFailure();
168}
169
170int SVCHlpClient::write (const Utf8Str &aVal)
171{
172 if (!mIsOpen)
173 return VERR_WRONG_ORDER;
174
175 /* write -1 for NULL strings */
176 if (aVal.isEmpty())
177 return write ((size_t) ~0);
178
179 size_t len = aVal.length();
180
181 /* write string length */
182 int vrc = write (len);
183 if (RT_SUCCESS(vrc))
184 {
185 /* write string data */
186 vrc = write (aVal.c_str(), len);
187 }
188
189 return vrc;
190}
191
192int SVCHlpClient::write (const Guid &aGuid)
193{
194 Utf8Str guidStr = aGuid.toString();
195 return write (guidStr);
196}
197
198int SVCHlpClient::read (void *aVal, size_t aLen)
199{
200 AssertReturn(aVal != NULL, VERR_INVALID_PARAMETER);
201 AssertReturn(aLen != 0, VERR_INVALID_PARAMETER);
202
203 if (!mIsOpen)
204 return VERR_WRONG_ORDER;
205
206 DWORD read = 0;
207 BOOL ok = ReadFile (mReadEnd, aVal, (ULONG)aLen, &read, NULL);
208 AssertReturn(!ok || read == aLen, VERR_GENERAL_FAILURE);
209 return ok ? VINF_SUCCESS : rtErrConvertFromWin32OnFailure();
210}
211
212int SVCHlpClient::read (Utf8Str &aVal)
213{
214 if (!mIsOpen)
215 return VERR_WRONG_ORDER;
216
217 size_t len = 0;
218
219 /* read string length */
220 int vrc = read (len);
221 if (RT_FAILURE(vrc))
222 return vrc;
223
224 /* length -1 means a NULL string */
225 if (len == (size_t) ~0)
226 {
227 aVal.setNull();
228 return VINF_SUCCESS;
229 }
230
231 aVal.reserve(len + 1);
232 aVal.mutableRaw()[len] = 0;
233
234 /* read string data */
235 vrc = read (aVal.mutableRaw(), len);
236
237 return vrc;
238}
239
240int SVCHlpClient::read (Guid &aGuid)
241{
242 Utf8Str guidStr;
243 int vrc = read (guidStr);
244 if (RT_SUCCESS(vrc))
245 aGuid = Guid (guidStr.c_str());
246 return vrc;
247}
248
249////////////////////////////////////////////////////////////////////////////////
250
251SVCHlpServer::SVCHlpServer ()
252{
253}
254
255int SVCHlpServer::run()
256{
257 int vrc = VINF_SUCCESS;
258 SVCHlpMsg::Code msgCode = SVCHlpMsg::Null;
259
260 do
261 {
262 vrc = read (msgCode);
263 if (RT_FAILURE(vrc))
264 return vrc;
265
266 /* terminate request received */
267 if (msgCode == SVCHlpMsg::Null)
268 return VINF_SUCCESS;
269
270 switch (msgCode)
271 {
272 case SVCHlpMsg::CreateHostOnlyNetworkInterface:
273 case SVCHlpMsg::RemoveHostOnlyNetworkInterface:
274 case SVCHlpMsg::EnableDynamicIpConfig:
275 case SVCHlpMsg::EnableStaticIpConfig:
276 case SVCHlpMsg::EnableStaticIpConfigV6:
277 case SVCHlpMsg::DhcpRediscover:
278 {
279#ifdef VBOX_WITH_NETFLT
280 vrc = netIfNetworkInterfaceHelperServer(this, msgCode);
281#endif
282 break;
283 }
284 default:
285 AssertMsgFailedReturn(("Invalid message code %d (%08lX)\n", msgCode, msgCode),
286 VERR_GENERAL_FAILURE);
287 }
288
289 if (RT_FAILURE(vrc))
290 return vrc;
291 }
292 while (1);
293
294 /* we never get here */
295 AssertFailed();
296 return VERR_GENERAL_FAILURE;
297}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette