VirtualBox

source: vbox/trunk/src/VBox/Devices/GIMDev/DrvUDP.cpp@ 58143

Last change on this file since 58143 was 57989, checked in by vboxsync, 9 years ago

Added support for GIM Hyper-V hypercalls and guest debugging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: DrvUDP.cpp 57989 2015-10-01 16:44:12Z vboxsync $ */
2/** @file
3 * UDP socket stream driver.
4 */
5
6/*
7 * Copyright (C) 2015 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 */
48typedef 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 */
71static 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 = RTSocketReadNB(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 */
95static 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 cbReallyWritten = 0;
106 rc = RTSocketWriteNB(pThis->hSocket, pvBuf, *pcbWrite, &cbReallyWritten);
107 if (RT_SUCCESS(rc))
108 *pcbWrite = cbReallyWritten;
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 */
121static 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 */
141static 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 RTSocketClose(pThis->hSocket);
150 pThis->hSocket = NIL_RTSOCKET;
151 LogRel(("DrvUDP#%u: Closed socket to %s:%u\n", pThis->pDrvIns->iInstance, pThis->pszServerAddress, pThis->uServerPort));
152 }
153
154 MMR3HeapFree(pThis->pszServerAddress);
155 pThis->pszServerAddress = NULL;
156}
157
158
159/**
160 * Construct a UDP socket stream driver instance.
161 *
162 * @copydoc FNPDMDRVCONSTRUCT
163 */
164static DECLCALLBACK(int) drvUDPConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
165{
166 PDRVUDP pThis = PDMINS_2_DATA(pDrvIns, PDRVUDP);
167 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
168
169 /*
170 * Init the static parts.
171 */
172 pThis->pDrvIns = pDrvIns;
173 /* IBase */
174 pDrvIns->IBase.pfnQueryInterface = drvUDPQueryInterface;
175 /* IStream */
176 pThis->IStream.pfnRead = drvUDPRead;
177 pThis->IStream.pfnWrite = drvUDPWrite;
178
179 /*
180 * Validate and read the configuration.
181 */
182 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "ServerAddress|ServerPort", "");
183
184 int rc = CFGMR3QueryStringAlloc(pCfg, "ServerAddress", &pThis->pszServerAddress);
185 if (RT_FAILURE(rc))
186 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
187 N_("Configuration error: querying \"ServerAddress\" resulted in %Rrc"), rc);
188 rc = CFGMR3QueryU16(pCfg, "ServerPort", &pThis->uServerPort);
189 if (RT_FAILURE(rc))
190 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
191 N_("Configuration error: querying \"ServerPort\" resulted in %Rrc"), rc);
192
193 /*
194 * Create the socket and connect.
195 */
196 rc = RTUdpCreateClientSocket(pThis->pszServerAddress, pThis->uServerPort, NULL, &pThis->hSocket);
197 if (RT_SUCCESS(rc))
198 {
199 LogRel(("DrvUDP#%u: Connected socket to %s:%u\n", pThis->pDrvIns->iInstance, pThis->pszServerAddress,
200 pThis->uServerPort));
201 }
202 else
203 {
204 LogRel(("DrvUDP#%u: Failed to create/connect socket to %s:%u rc=%Rrc\n", pThis->pDrvIns->iInstance,
205 pThis->pszServerAddress, pThis->uServerPort, rc));
206 }
207 return VINF_SUCCESS;
208}
209
210
211/**
212 * UDP socket driver registration record.
213 */
214const PDMDRVREG g_DrvUDP =
215{
216 /* u32Version */
217 PDM_DRVREG_VERSION,
218 /* szName */
219 "UDP",
220 /* szRCMod */
221 "",
222 /* szR0Mod */
223 "",
224 /* pszDescription */
225 "UDP socket stream driver.",
226 /* fFlags */
227 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
228 /* fClass. */
229 PDM_DRVREG_CLASS_STREAM,
230 /* cMaxInstances */
231 ~0U,
232 /* cbInstance */
233 sizeof(DRVUDP),
234 /* pfnConstruct */
235 drvUDPConstruct,
236 /* pfnDestruct */
237 drvUDPDestruct,
238 /* pfnRelocate */
239 NULL,
240 /* pfnIOCtl */
241 NULL,
242 /* pfnPowerOn */
243 NULL,
244 /* pfnReset */
245 NULL,
246 /* pfnSuspend */
247 NULL,
248 /* pfnResume */
249 NULL,
250 /* pfnAttach */
251 NULL,
252 /* pfnDetach */
253 NULL,
254 /* pfnPowerOff */
255 NULL,
256 /* pfnSoftReset */
257 NULL,
258 /* u32EndVersion */
259 PDM_DRVREG_VERSION
260};
261
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