1 | /* $Id: DrvUDP.cpp 62515 2016-07-22 19:13:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * UDP socket stream driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015-2016 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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DRV_UDP
|
---|
23 | #include <VBox/log.h>
|
---|
24 | #include <VBox/vmm/pdmdrv.h>
|
---|
25 |
|
---|
26 | #include "VBoxDD.h"
|
---|
27 |
|
---|
28 | #include <iprt/socket.h>
|
---|
29 | #include <iprt/udp.h>
|
---|
30 | #include <iprt/uuid.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /*********************************************************************************************************************************
|
---|
34 | * Defined Constants And Macros *
|
---|
35 | *********************************************************************************************************************************/
|
---|
36 | /** Converts a pointer to DRVUDP::IStream to a PDRVUDP. */
|
---|
37 | #define PDMISTREAM_2_DRVUDP(pInterface) ( (PDRVUDP)((uintptr_t)pInterface - RT_OFFSETOF(DRVUDP, IStream)) )
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Structures and Typedefs *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | /**
|
---|
44 | * UDP driver instance data.
|
---|
45 | *
|
---|
46 | * @implements PDMISTREAM
|
---|
47 | */
|
---|
48 | typedef struct DRVUDP
|
---|
49 | {
|
---|
50 | /** The stream interface. */
|
---|
51 | PDMISTREAM IStream;
|
---|
52 | /** Pointer to the driver instance. */
|
---|
53 | PPDMDRVINS pDrvIns;
|
---|
54 | /** The server port. */
|
---|
55 | uint16_t uServerPort;
|
---|
56 | /** The server address. */
|
---|
57 | char *pszServerAddress;
|
---|
58 | /** The resolved server address struct. */
|
---|
59 | RTNETADDR ServerAddr;
|
---|
60 | /** The UDP socket. */
|
---|
61 | RTSOCKET hSocket;
|
---|
62 | } DRVUDP, *PDRVUDP;
|
---|
63 |
|
---|
64 |
|
---|
65 | /*********************************************************************************************************************************
|
---|
66 | * Internal Functions *
|
---|
67 | *********************************************************************************************************************************/
|
---|
68 |
|
---|
69 |
|
---|
70 | /** @copydoc PDMISTREAM::pfnRead */
|
---|
71 | static DECLCALLBACK(int) drvUDPRead(PPDMISTREAM pInterface, void *pvBuf, size_t *pcbRead)
|
---|
72 | {
|
---|
73 | int rc = VINF_SUCCESS;
|
---|
74 | PDRVUDP pThis = PDMISTREAM_2_DRVUDP(pInterface);
|
---|
75 | LogFlowFunc(("pvBuf=%p *pcbRead=%#x (%s:%u)\n", pvBuf, *pcbRead, pThis->pszServerAddress, pThis->uServerPort));
|
---|
76 |
|
---|
77 | Assert(pvBuf);
|
---|
78 | Assert(pcbRead);
|
---|
79 | if (pThis->hSocket != NIL_RTSOCKET)
|
---|
80 | {
|
---|
81 | size_t cbReallyRead = 0;
|
---|
82 | rc = RTSocketRead(pThis->hSocket, pvBuf, *pcbRead, &cbReallyRead);
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | *pcbRead = cbReallyRead;
|
---|
85 | }
|
---|
86 | else
|
---|
87 | rc = VERR_NET_NOT_SOCKET;
|
---|
88 |
|
---|
89 | LogFlowFunc(("*pcbRead=%zu returns %Rrc\n", *pcbRead, rc));
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /** @copydoc PDMISTREAM::pfnWrite */
|
---|
95 | static DECLCALLBACK(int) drvUDPWrite(PPDMISTREAM pInterface, const void *pvBuf, size_t *pcbWrite)
|
---|
96 | {
|
---|
97 | int rc = VINF_SUCCESS;
|
---|
98 | PDRVUDP pThis = PDMISTREAM_2_DRVUDP(pInterface);
|
---|
99 | LogFlowFunc(("pvBuf=%p *pcbWrite=%#x (%s:%u)\n", pvBuf, *pcbWrite, pThis->pszServerAddress, pThis->uServerPort));
|
---|
100 |
|
---|
101 | Assert(pvBuf);
|
---|
102 | Assert(pcbWrite);
|
---|
103 | if (pThis->hSocket != NIL_RTSOCKET)
|
---|
104 | {
|
---|
105 | size_t cbBuf = *pcbWrite;
|
---|
106 | rc = RTSocketWriteTo(pThis->hSocket, pvBuf, cbBuf, NULL /*pDstAddr*/);
|
---|
107 | if (RT_SUCCESS(rc))
|
---|
108 | *pcbWrite = cbBuf;
|
---|
109 | }
|
---|
110 | else
|
---|
111 | rc = VERR_NET_NOT_SOCKET;
|
---|
112 |
|
---|
113 | LogFlowFunc(("*pcbWrite=%zu returns %Rrc\n", *pcbWrite, rc));
|
---|
114 | return rc;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
120 | */
|
---|
121 | static DECLCALLBACK(void *) drvUDPQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
122 | {
|
---|
123 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
124 | PDRVUDP pThis = PDMINS_2_DATA(pDrvIns, PDRVUDP);
|
---|
125 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
126 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMISTREAM, &pThis->IStream);
|
---|
127 | return NULL;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /* -=-=-=-=- PDMDRVREG -=-=-=-=- */
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Destruct a UDP socket stream driver instance.
|
---|
135 | *
|
---|
136 | * Most VM resources are freed by the VM. This callback is provided so that
|
---|
137 | * any non-VM resources can be freed correctly.
|
---|
138 | *
|
---|
139 | * @param pDrvIns The driver instance data.
|
---|
140 | */
|
---|
141 | static DECLCALLBACK(void) drvUDPDestruct(PPDMDRVINS pDrvIns)
|
---|
142 | {
|
---|
143 | PDRVUDP pThis = PDMINS_2_DATA(pDrvIns, PDRVUDP);
|
---|
144 | LogFlowFunc(("\n"));
|
---|
145 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
146 |
|
---|
147 | if (pThis->hSocket != NIL_RTSOCKET)
|
---|
148 | {
|
---|
149 | /*
|
---|
150 | * We shutdown the socket here to poke out any blocking socket reads. The caller
|
---|
151 | * on the other thread/s need to ensure that they do -not- invoke drvUDPRead()
|
---|
152 | * or drvUDPWrite() after this.
|
---|
153 | */
|
---|
154 | RTSocketRetain(pThis->hSocket);
|
---|
155 | RTSocketShutdown(pThis->hSocket, true, true);
|
---|
156 | RTSocketClose(pThis->hSocket);
|
---|
157 | pThis->hSocket = NIL_RTSOCKET;
|
---|
158 | LogRel(("DrvUDP#%u: Closed socket to %s:%u\n", pThis->pDrvIns->iInstance, pThis->pszServerAddress, pThis->uServerPort));
|
---|
159 | }
|
---|
160 |
|
---|
161 | MMR3HeapFree(pThis->pszServerAddress);
|
---|
162 | pThis->pszServerAddress = NULL;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Construct a UDP socket stream driver instance.
|
---|
168 | *
|
---|
169 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
170 | */
|
---|
171 | static DECLCALLBACK(int) drvUDPConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
172 | {
|
---|
173 | PDRVUDP pThis = PDMINS_2_DATA(pDrvIns, PDRVUDP);
|
---|
174 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * Init the static parts.
|
---|
178 | */
|
---|
179 | pThis->pDrvIns = pDrvIns;
|
---|
180 | /* IBase */
|
---|
181 | pDrvIns->IBase.pfnQueryInterface = drvUDPQueryInterface;
|
---|
182 | /* IStream */
|
---|
183 | pThis->IStream.pfnRead = drvUDPRead;
|
---|
184 | pThis->IStream.pfnWrite = drvUDPWrite;
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Validate and read the configuration.
|
---|
188 | */
|
---|
189 | PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "ServerAddress|ServerPort", "");
|
---|
190 |
|
---|
191 | int rc = CFGMR3QueryStringAlloc(pCfg, "ServerAddress", &pThis->pszServerAddress);
|
---|
192 | if (RT_FAILURE(rc))
|
---|
193 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
194 | N_("Configuration error: querying \"ServerAddress\" resulted in %Rrc"), rc);
|
---|
195 | rc = CFGMR3QueryU16(pCfg, "ServerPort", &pThis->uServerPort);
|
---|
196 | if (RT_FAILURE(rc))
|
---|
197 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
198 | N_("Configuration error: querying \"ServerPort\" resulted in %Rrc"), rc);
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Create the socket and connect.
|
---|
202 | */
|
---|
203 | rc = RTUdpCreateClientSocket(pThis->pszServerAddress, pThis->uServerPort, NULL, &pThis->hSocket);
|
---|
204 | if (RT_SUCCESS(rc))
|
---|
205 | {
|
---|
206 | LogRel(("DrvUDP#%u: Connected socket to %s:%u\n", pThis->pDrvIns->iInstance, pThis->pszServerAddress,
|
---|
207 | pThis->uServerPort));
|
---|
208 | }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | LogRel(("DrvUDP#%u: Failed to create/connect socket to %s:%u rc=%Rrc\n", pThis->pDrvIns->iInstance,
|
---|
212 | pThis->pszServerAddress, pThis->uServerPort, rc));
|
---|
213 | }
|
---|
214 | return VINF_SUCCESS;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * UDP socket driver registration record.
|
---|
220 | */
|
---|
221 | const PDMDRVREG g_DrvUDP =
|
---|
222 | {
|
---|
223 | /* u32Version */
|
---|
224 | PDM_DRVREG_VERSION,
|
---|
225 | /* szName */
|
---|
226 | "UDP",
|
---|
227 | /* szRCMod */
|
---|
228 | "",
|
---|
229 | /* szR0Mod */
|
---|
230 | "",
|
---|
231 | /* pszDescription */
|
---|
232 | "UDP socket stream driver.",
|
---|
233 | /* fFlags */
|
---|
234 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
235 | /* fClass. */
|
---|
236 | PDM_DRVREG_CLASS_STREAM,
|
---|
237 | /* cMaxInstances */
|
---|
238 | ~0U,
|
---|
239 | /* cbInstance */
|
---|
240 | sizeof(DRVUDP),
|
---|
241 | /* pfnConstruct */
|
---|
242 | drvUDPConstruct,
|
---|
243 | /* pfnDestruct */
|
---|
244 | drvUDPDestruct,
|
---|
245 | /* pfnRelocate */
|
---|
246 | NULL,
|
---|
247 | /* pfnIOCtl */
|
---|
248 | NULL,
|
---|
249 | /* pfnPowerOn */
|
---|
250 | NULL,
|
---|
251 | /* pfnReset */
|
---|
252 | NULL,
|
---|
253 | /* pfnSuspend */
|
---|
254 | NULL,
|
---|
255 | /* pfnResume */
|
---|
256 | NULL,
|
---|
257 | /* pfnAttach */
|
---|
258 | NULL,
|
---|
259 | /* pfnDetach */
|
---|
260 | NULL,
|
---|
261 | /* pfnPowerOff */
|
---|
262 | NULL,
|
---|
263 | /* pfnSoftReset */
|
---|
264 | NULL,
|
---|
265 | /* u32EndVersion */
|
---|
266 | PDM_DRVREG_VERSION
|
---|
267 | };
|
---|
268 |
|
---|