1 | /* $Id: DBGCIoProvTcp.cpp 96865 2022-09-26 14:45:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGC - Debugger Console, TCP I/O provider.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <VBox/dbg.h>
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 |
|
---|
36 | #include <iprt/mem.h>
|
---|
37 | #include <iprt/tcp.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 |
|
---|
40 | #include "DBGCIoProvInternal.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Structures and Typedefs *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | /**
|
---|
47 | * Debug console TCP connection data.
|
---|
48 | */
|
---|
49 | typedef struct DBGCTCPCON
|
---|
50 | {
|
---|
51 | /** The I/O callback table for the console. */
|
---|
52 | DBGCIO Io;
|
---|
53 | /** The socket of the connection. */
|
---|
54 | RTSOCKET hSock;
|
---|
55 | /** Connection status. */
|
---|
56 | bool fAlive;
|
---|
57 | } DBGCTCPCON;
|
---|
58 | /** Pointer to the instance data of the console TCP backend. */
|
---|
59 | typedef DBGCTCPCON *PDBGCTCPCON;
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Internal Functions *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @interface_method_impl{DBGCIO,pfnDestroy}
|
---|
68 | */
|
---|
69 | static DECLCALLBACK(void) dbgcIoProvTcpIoDestroy(PCDBGCIO pIo)
|
---|
70 | {
|
---|
71 | PDBGCTCPCON pTcpCon = RT_FROM_MEMBER(pIo, DBGCTCPCON, Io);
|
---|
72 | RTSocketRelease(pTcpCon->hSock);
|
---|
73 | pTcpCon->fAlive =false;
|
---|
74 | RTMemFree(pTcpCon);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @interface_method_impl{DBGCIO,pfnInput}
|
---|
80 | */
|
---|
81 | static DECLCALLBACK(bool) dbgcIoProvTcpIoInput(PCDBGCIO pIo, uint32_t cMillies)
|
---|
82 | {
|
---|
83 | PDBGCTCPCON pTcpCon = RT_FROM_MEMBER(pIo, DBGCTCPCON, Io);
|
---|
84 | if (!pTcpCon->fAlive)
|
---|
85 | return false;
|
---|
86 | int rc = RTTcpSelectOne(pTcpCon->hSock, cMillies);
|
---|
87 | if (RT_FAILURE(rc) && rc != VERR_TIMEOUT)
|
---|
88 | pTcpCon->fAlive = false;
|
---|
89 | return rc != VERR_TIMEOUT;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * @interface_method_impl{DBGCIO,pfnRead}
|
---|
95 | */
|
---|
96 | static DECLCALLBACK(int) dbgcIoProvTcpIoRead(PCDBGCIO pIo, void *pvBuf, size_t cbBuf, size_t *pcbRead)
|
---|
97 | {
|
---|
98 | PDBGCTCPCON pTcpCon = RT_FROM_MEMBER(pIo, DBGCTCPCON, Io);
|
---|
99 | if (!pTcpCon->fAlive)
|
---|
100 | return VERR_INVALID_HANDLE;
|
---|
101 | int rc = RTTcpRead(pTcpCon->hSock, pvBuf, cbBuf, pcbRead);
|
---|
102 | if (RT_SUCCESS(rc) && pcbRead != NULL && *pcbRead == 0)
|
---|
103 | rc = VERR_NET_SHUTDOWN;
|
---|
104 | if (RT_FAILURE(rc))
|
---|
105 | pTcpCon->fAlive = false;
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * @interface_method_impl{DBGCIO,pfnWrite}
|
---|
112 | */
|
---|
113 | static DECLCALLBACK(int) dbgcIoProvTcpIoWrite(PCDBGCIO pIo, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
|
---|
114 | {
|
---|
115 | PDBGCTCPCON pTcpCon = RT_FROM_MEMBER(pIo, DBGCTCPCON, Io);
|
---|
116 | if (!pTcpCon->fAlive)
|
---|
117 | return VERR_INVALID_HANDLE;
|
---|
118 |
|
---|
119 | int rc = RTTcpWrite(pTcpCon->hSock, pvBuf, cbBuf);
|
---|
120 | if (RT_FAILURE(rc))
|
---|
121 | pTcpCon->fAlive = false;
|
---|
122 |
|
---|
123 | if (pcbWritten)
|
---|
124 | *pcbWritten = cbBuf;
|
---|
125 |
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * @interface_method_impl{DBGCIO,pfnSetReady}
|
---|
132 | */
|
---|
133 | static DECLCALLBACK(void) dbgcIoProvTcpIoSetReady(PCDBGCIO pIo, bool fReady)
|
---|
134 | {
|
---|
135 | /* stub */
|
---|
136 | NOREF(pIo);
|
---|
137 | NOREF(fReady);
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * @interface_method_impl{DBGCIOPROVREG,pfnCreate}
|
---|
143 | */
|
---|
144 | static DECLCALLBACK(int) dbgcIoProvTcpCreate(PDBGCIOPROV phDbgcIoProv, PCFGMNODE pCfg)
|
---|
145 | {
|
---|
146 | /*
|
---|
147 | * Get the port configuration.
|
---|
148 | */
|
---|
149 | uint32_t u32Port;
|
---|
150 | int rc = CFGMR3QueryU32Def(pCfg, "Port", &u32Port, 5000);
|
---|
151 | if (RT_FAILURE(rc))
|
---|
152 | {
|
---|
153 | LogRel(("Configuration error: Failed querying \"Port\" -> rc=%Rc\n", rc));
|
---|
154 | return rc;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Get the address configuration.
|
---|
159 | */
|
---|
160 | char szAddress[512];
|
---|
161 | rc = CFGMR3QueryStringDef(pCfg, "Address", szAddress, sizeof(szAddress), "");
|
---|
162 | if (RT_FAILURE(rc))
|
---|
163 | {
|
---|
164 | LogRel(("Configuration error: Failed querying \"Address\" -> rc=%Rc\n", rc));
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Create the server.
|
---|
170 | */
|
---|
171 | PRTTCPSERVER pServer;
|
---|
172 | rc = RTTcpServerCreateEx(szAddress, u32Port, &pServer);
|
---|
173 | if (RT_SUCCESS(rc))
|
---|
174 | {
|
---|
175 | LogFlow(("dbgcIoProvTcpCreate: Created server on port %d %s\n", u32Port, szAddress));
|
---|
176 | *phDbgcIoProv = (DBGCIOPROV)pServer;
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | return rc;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @interface_method_impl{DBGCIOPROVREG,pfnDestroy}
|
---|
186 | */
|
---|
187 | static DECLCALLBACK(void) dbgcIoProvTcpDestroy(DBGCIOPROV hDbgcIoProv)
|
---|
188 | {
|
---|
189 | int rc = RTTcpServerDestroy((PRTTCPSERVER)hDbgcIoProv);
|
---|
190 | AssertRC(rc);
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * @interface_method_impl{DBGCIOPROVREG,pfnWaitForConnect}
|
---|
196 | */
|
---|
197 | static DECLCALLBACK(int) dbgcIoProvTcpWaitForConnect(DBGCIOPROV hDbgcIoProv, RTMSINTERVAL cMsTimeout, PCDBGCIO *ppDbgcIo)
|
---|
198 | {
|
---|
199 | PRTTCPSERVER pTcpSrv = (PRTTCPSERVER)hDbgcIoProv;
|
---|
200 | RT_NOREF(cMsTimeout);
|
---|
201 |
|
---|
202 | RTSOCKET hSockCon = NIL_RTSOCKET;
|
---|
203 | int rc = RTTcpServerListen2(pTcpSrv, &hSockCon);
|
---|
204 | if (RT_SUCCESS(rc))
|
---|
205 | {
|
---|
206 | PDBGCTCPCON pTcpCon = (PDBGCTCPCON)RTMemAllocZ(sizeof(*pTcpCon));
|
---|
207 | if (RT_LIKELY(pTcpCon))
|
---|
208 | {
|
---|
209 | pTcpCon->Io.pfnDestroy = dbgcIoProvTcpIoDestroy;
|
---|
210 | pTcpCon->Io.pfnInput = dbgcIoProvTcpIoInput;
|
---|
211 | pTcpCon->Io.pfnRead = dbgcIoProvTcpIoRead;
|
---|
212 | pTcpCon->Io.pfnWrite = dbgcIoProvTcpIoWrite;
|
---|
213 | pTcpCon->Io.pfnPktBegin = NULL;
|
---|
214 | pTcpCon->Io.pfnPktEnd = NULL;
|
---|
215 | pTcpCon->Io.pfnSetReady = dbgcIoProvTcpIoSetReady;
|
---|
216 | pTcpCon->hSock = hSockCon;
|
---|
217 | pTcpCon->fAlive = true;
|
---|
218 | *ppDbgcIo = &pTcpCon->Io;
|
---|
219 | }
|
---|
220 | else
|
---|
221 | rc = VERR_NO_MEMORY;
|
---|
222 | }
|
---|
223 |
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * @interface_method_impl{DBGCIOPROVREG,pfnWaitInterrupt}
|
---|
230 | */
|
---|
231 | static DECLCALLBACK(int) dbgcIoProvTcpWaitInterrupt(DBGCIOPROV hDbgcIoProv)
|
---|
232 | {
|
---|
233 | PRTTCPSERVER pTcpSrv = (PRTTCPSERVER)hDbgcIoProv;
|
---|
234 |
|
---|
235 | RT_NOREF(pTcpSrv);
|
---|
236 | /** @todo */
|
---|
237 | return VINF_SUCCESS;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * TCP I/O provider registration record.
|
---|
243 | */
|
---|
244 | const DBGCIOPROVREG g_DbgcIoProvTcp =
|
---|
245 | {
|
---|
246 | /** pszName */
|
---|
247 | "tcp",
|
---|
248 | /** pszDesc */
|
---|
249 | "TCP I/O provider.",
|
---|
250 | /** pfnCreate */
|
---|
251 | dbgcIoProvTcpCreate,
|
---|
252 | /** pfnDestroy */
|
---|
253 | dbgcIoProvTcpDestroy,
|
---|
254 | /** pfnWaitForConnect */
|
---|
255 | dbgcIoProvTcpWaitForConnect,
|
---|
256 | /** pfnWaitInterrupt */
|
---|
257 | dbgcIoProvTcpWaitInterrupt
|
---|
258 | };
|
---|
259 |
|
---|