1 | /* $Id: VBoxGuestR3LibHostChannel.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Host Channel.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | #include <iprt/mem.h>
|
---|
29 |
|
---|
30 | #include <VBox/HostServices/VBoxHostChannel.h>
|
---|
31 |
|
---|
32 | #include "VBoxGuestR3LibInternal.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | VBGLR3DECL(int) VbglR3HostChannelInit(uint32_t *pidClient)
|
---|
36 | {
|
---|
37 | return VbglR3HGCMConnect("VBoxHostChannel", pidClient);
|
---|
38 | }
|
---|
39 |
|
---|
40 | VBGLR3DECL(void) VbglR3HostChannelTerm(uint32_t idClient)
|
---|
41 | {
|
---|
42 | VbglR3HGCMDisconnect(idClient);
|
---|
43 | }
|
---|
44 |
|
---|
45 | VBGLR3DECL(int) VbglR3HostChannelAttach(uint32_t *pu32ChannelHandle,
|
---|
46 | uint32_t u32HGCMClientId,
|
---|
47 | const char *pszName,
|
---|
48 | uint32_t u32Flags)
|
---|
49 | {
|
---|
50 | /* Make a heap copy of the name, because HGCM can not use some of other memory types. */
|
---|
51 | size_t cbName = strlen(pszName) + 1;
|
---|
52 | char *pszCopy = (char *)RTMemAlloc(cbName);
|
---|
53 | if (pszCopy == NULL)
|
---|
54 | {
|
---|
55 | return VERR_NO_MEMORY;
|
---|
56 | }
|
---|
57 |
|
---|
58 | memcpy(pszCopy, pszName, cbName);
|
---|
59 |
|
---|
60 | VBoxHostChannelAttach parms;
|
---|
61 | VBGL_HGCM_HDR_INIT(&parms.hdr, u32HGCMClientId, VBOX_HOST_CHANNEL_FN_ATTACH, 3);
|
---|
62 | VbglHGCMParmPtrSet(&parms.name, pszCopy, (uint32_t)cbName);
|
---|
63 | VbglHGCMParmUInt32Set(&parms.flags, u32Flags);
|
---|
64 | VbglHGCMParmUInt32Set(&parms.handle, 0);
|
---|
65 |
|
---|
66 | int rc = VbglR3HGCMCall(&parms.hdr, sizeof(parms));
|
---|
67 |
|
---|
68 | if (RT_SUCCESS(rc))
|
---|
69 | *pu32ChannelHandle = parms.handle.u.value32;
|
---|
70 |
|
---|
71 | RTMemFree(pszCopy);
|
---|
72 |
|
---|
73 | return rc;
|
---|
74 | }
|
---|
75 |
|
---|
76 | VBGLR3DECL(void) VbglR3HostChannelDetach(uint32_t u32ChannelHandle,
|
---|
77 | uint32_t u32HGCMClientId)
|
---|
78 | {
|
---|
79 | VBoxHostChannelDetach parms;
|
---|
80 | VBGL_HGCM_HDR_INIT(&parms.hdr, u32HGCMClientId, VBOX_HOST_CHANNEL_FN_DETACH, 1);
|
---|
81 | VbglHGCMParmUInt32Set(&parms.handle, u32ChannelHandle);
|
---|
82 |
|
---|
83 | VbglR3HGCMCall(&parms.hdr, sizeof(parms));
|
---|
84 | }
|
---|
85 |
|
---|
86 | VBGLR3DECL(int) VbglR3HostChannelSend(uint32_t u32ChannelHandle,
|
---|
87 | uint32_t u32HGCMClientId,
|
---|
88 | void *pvData,
|
---|
89 | uint32_t cbData)
|
---|
90 | {
|
---|
91 | VBoxHostChannelSend parms;
|
---|
92 | VBGL_HGCM_HDR_INIT(&parms.hdr, u32HGCMClientId, VBOX_HOST_CHANNEL_FN_SEND, 2);
|
---|
93 | VbglHGCMParmUInt32Set(&parms.handle, u32ChannelHandle);
|
---|
94 | VbglHGCMParmPtrSet(&parms.data, pvData, cbData);
|
---|
95 |
|
---|
96 | return VbglR3HGCMCall(&parms.hdr, sizeof(parms));
|
---|
97 | }
|
---|
98 |
|
---|
99 | VBGLR3DECL(int) VbglR3HostChannelRecv(uint32_t u32ChannelHandle,
|
---|
100 | uint32_t u32HGCMClientId,
|
---|
101 | void *pvData,
|
---|
102 | uint32_t cbData,
|
---|
103 | uint32_t *pu32SizeReceived,
|
---|
104 | uint32_t *pu32SizeRemaining)
|
---|
105 | {
|
---|
106 | VBoxHostChannelRecv parms;
|
---|
107 | VBGL_HGCM_HDR_INIT(&parms.hdr, u32HGCMClientId, VBOX_HOST_CHANNEL_FN_RECV, 4);
|
---|
108 | VbglHGCMParmUInt32Set(&parms.handle, u32ChannelHandle);
|
---|
109 | VbglHGCMParmPtrSet(&parms.data, pvData, cbData);
|
---|
110 | VbglHGCMParmUInt32Set(&parms.sizeReceived, 0);
|
---|
111 | VbglHGCMParmUInt32Set(&parms.sizeRemaining, 0);
|
---|
112 |
|
---|
113 | int rc = VbglR3HGCMCall(&parms.hdr, sizeof(parms));
|
---|
114 |
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | {
|
---|
117 | *pu32SizeReceived = parms.sizeReceived.u.value32;
|
---|
118 | *pu32SizeRemaining = parms.sizeRemaining.u.value32;
|
---|
119 | }
|
---|
120 |
|
---|
121 | return rc;
|
---|
122 | }
|
---|
123 |
|
---|
124 | VBGLR3DECL(int) VbglR3HostChannelControl(uint32_t u32ChannelHandle,
|
---|
125 | uint32_t u32HGCMClientId,
|
---|
126 | uint32_t u32Code,
|
---|
127 | void *pvParm,
|
---|
128 | uint32_t cbParm,
|
---|
129 | void *pvData,
|
---|
130 | uint32_t cbData,
|
---|
131 | uint32_t *pu32SizeDataReturned)
|
---|
132 | {
|
---|
133 | VBoxHostChannelControl parms;
|
---|
134 | VBGL_HGCM_HDR_INIT(&parms.hdr, u32HGCMClientId, VBOX_HOST_CHANNEL_FN_CONTROL, 5);
|
---|
135 | VbglHGCMParmUInt32Set(&parms.handle, u32ChannelHandle);
|
---|
136 | VbglHGCMParmUInt32Set(&parms.code, u32Code);
|
---|
137 | VbglHGCMParmPtrSet(&parms.parm, pvParm, cbParm);
|
---|
138 | VbglHGCMParmPtrSet(&parms.data, pvData, cbData);
|
---|
139 | VbglHGCMParmUInt32Set(&parms.sizeDataReturned, 0);
|
---|
140 |
|
---|
141 | int rc = VbglR3HGCMCall(&parms.hdr, sizeof(parms));
|
---|
142 |
|
---|
143 | if (RT_SUCCESS(rc))
|
---|
144 | {
|
---|
145 | *pu32SizeDataReturned = parms.sizeDataReturned.u.value32;
|
---|
146 | }
|
---|
147 |
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | VBGLR3DECL(int) VbglR3HostChannelEventWait(uint32_t *pu32ChannelHandle,
|
---|
152 | uint32_t u32HGCMClientId,
|
---|
153 | uint32_t *pu32EventId,
|
---|
154 | void *pvParm,
|
---|
155 | uint32_t cbParm,
|
---|
156 | uint32_t *pu32SizeReturned)
|
---|
157 | {
|
---|
158 | VBoxHostChannelEventWait parms;
|
---|
159 | VBGL_HGCM_HDR_INIT(&parms.hdr, u32HGCMClientId, VBOX_HOST_CHANNEL_FN_EVENT_WAIT, 4);
|
---|
160 | VbglHGCMParmUInt32Set(&parms.handle, 0);
|
---|
161 | VbglHGCMParmUInt32Set(&parms.id, 0);
|
---|
162 | VbglHGCMParmPtrSet(&parms.parm, pvParm, cbParm);
|
---|
163 | VbglHGCMParmUInt32Set(&parms.sizeReturned, 0);
|
---|
164 |
|
---|
165 | int rc = VbglR3HGCMCall(&parms.hdr, sizeof(parms));
|
---|
166 |
|
---|
167 | if (RT_SUCCESS(rc))
|
---|
168 | {
|
---|
169 | *pu32ChannelHandle = parms.handle.u.value32;
|
---|
170 | *pu32EventId = parms.id.u.value32;
|
---|
171 | *pu32SizeReturned = parms.sizeReturned.u.value32;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return rc;
|
---|
175 | }
|
---|
176 |
|
---|
177 | VBGLR3DECL(int) VbglR3HostChannelEventCancel(uint32_t u32ChannelHandle,
|
---|
178 | uint32_t u32HGCMClientId)
|
---|
179 | {
|
---|
180 | RT_NOREF1(u32ChannelHandle);
|
---|
181 |
|
---|
182 | VBoxHostChannelEventCancel parms;
|
---|
183 | VBGL_HGCM_HDR_INIT(&parms.hdr, u32HGCMClientId, VBOX_HOST_CHANNEL_FN_EVENT_CANCEL, 0);
|
---|
184 |
|
---|
185 | return VbglR3HGCMCall(&parms.hdr, sizeof(parms));
|
---|
186 | }
|
---|
187 |
|
---|
188 | VBGLR3DECL(int) VbglR3HostChannelQuery(const char *pszName,
|
---|
189 | uint32_t u32HGCMClientId,
|
---|
190 | uint32_t u32Code,
|
---|
191 | void *pvParm,
|
---|
192 | uint32_t cbParm,
|
---|
193 | void *pvData,
|
---|
194 | uint32_t cbData,
|
---|
195 | uint32_t *pu32SizeDataReturned)
|
---|
196 | {
|
---|
197 | /* Make a heap copy of the name, because HGCM can not use some of other memory types. */
|
---|
198 | size_t cbName = strlen(pszName) + 1;
|
---|
199 | char *pszCopy = (char *)RTMemAlloc(cbName);
|
---|
200 | if (pszCopy == NULL)
|
---|
201 | {
|
---|
202 | return VERR_NO_MEMORY;
|
---|
203 | }
|
---|
204 |
|
---|
205 | memcpy(pszCopy, pszName, cbName);
|
---|
206 |
|
---|
207 | VBoxHostChannelQuery parms;
|
---|
208 | VBGL_HGCM_HDR_INIT(&parms.hdr, u32HGCMClientId, VBOX_HOST_CHANNEL_FN_QUERY, 5);
|
---|
209 | VbglHGCMParmPtrSet(&parms.name, pszCopy, (uint32_t)cbName);
|
---|
210 | VbglHGCMParmUInt32Set(&parms.code, u32Code);
|
---|
211 | VbglHGCMParmPtrSet(&parms.parm, pvParm, cbParm);
|
---|
212 | VbglHGCMParmPtrSet(&parms.data, pvData, cbData);
|
---|
213 | VbglHGCMParmUInt32Set(&parms.sizeDataReturned, 0);
|
---|
214 |
|
---|
215 | int rc = VbglR3HGCMCall(&parms.hdr, sizeof(parms));
|
---|
216 |
|
---|
217 | if (RT_SUCCESS(rc))
|
---|
218 | {
|
---|
219 | *pu32SizeDataReturned = parms.sizeDataReturned.u.value32;
|
---|
220 | }
|
---|
221 |
|
---|
222 | RTMemFree(pszCopy);
|
---|
223 |
|
---|
224 | return rc;
|
---|
225 | }
|
---|