VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/clipboard-transfers-http.cpp@ 87064

Last change on this file since 87064 was 87064, checked in by vboxsync, 4 years ago

Shared Clipboard/Transfers: Assertions. bugref:9874

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 KB
Line 
1/* $Id: clipboard-transfers-http.cpp 87064 2020-12-09 13:55:42Z vboxsync $ */
2/** @file
3 * Shared Clipboard: HTTP server implementation for Shared Clipboard transfers on UNIX-y hosts.
4 */
5
6/*
7 * Copyright (C) 2020 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#include <signal.h>
23
24#include <iprt/http.h>
25#include <iprt/http-server.h>
26
27#include <iprt/net.h> /* To make use of IPv4Addr in RTGETOPTUNION. */
28
29#include <iprt/asm.h>
30#include <iprt/assert.h>
31#include <iprt/ctype.h>
32#include <iprt/err.h>
33#include <iprt/file.h>
34#include <iprt/getopt.h>
35#include <iprt/initterm.h>
36#include <iprt/list.h>
37#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
38#include <iprt/log.h>
39#include <iprt/mem.h>
40#include <iprt/message.h>
41#include <iprt/path.h>
42#include <iprt/rand.h>
43#include <iprt/stream.h>
44#include <iprt/string.h>
45#include <iprt/thread.h>
46#include <iprt/uuid.h>
47#include <iprt/vfs.h>
48
49#include <VBox/HostServices/VBoxClipboardSvc.h>
50#include <VBox/GuestHost/SharedClipboard-transfers.h>
51
52
53/*********************************************************************************************************************************
54* Definitations *
55*********************************************************************************************************************************/
56
57typedef struct _SHCLHTTPSERVERTRANSFER
58{
59 /** The node list. */
60 RTLISTNODE Node;
61 /** Pointer to associated transfer. */
62 PSHCLTRANSFER pTransfer;
63 /** The virtual path of the HTTP server's root directory for this transfer. */
64 char szPathVirtual[RTPATH_MAX];
65} SHCLHTTPSERVERTRANSFER;
66typedef SHCLHTTPSERVERTRANSFER *PSHCLHTTPSERVERTRANSFER;
67
68
69/*********************************************************************************************************************************
70* Prototypes *
71*********************************************************************************************************************************/
72int ShClTransferHttpServerDestroyInternal(PSHCLHTTPSERVER pThis);
73
74
75/*********************************************************************************************************************************
76* Internal functions *
77*********************************************************************************************************************************/
78
79/**
80 * Return the HTTP server transfer for a specific transfer ID.
81 *
82 * @returns Pointer to HTTP server transfer if found, NULL if not found.
83 * @param pSrv HTTP server instance.
84 * @param idTransfer Transfer ID to return HTTP server transfer for.
85 */
86static PSHCLHTTPSERVERTRANSFER shClTransferHttpServerGetTransferById(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer)
87{
88 PSHCLHTTPSERVERTRANSFER pSrvTx;
89 RTListForEach(&pSrv->lstTransfers, pSrvTx, SHCLHTTPSERVERTRANSFER, Node) /** @todo Slow O(n) lookup, but does it for now. */
90 {
91 if (pSrvTx->pTransfer->State.uID == idTransfer)
92 return pSrvTx;
93 }
94
95 return NULL;
96}
97
98/**
99 * Validates a given URL whether it matches a registered HTTP transfer.
100 *
101 * @returns VBox status code.
102 * @param pThis HTTP server instance data.
103 * @param pszUrl URL to validate.
104 */
105static int shClTransferHttpPathValidate(PSHCLHTTPSERVER pThis, const char *pszUrl)
106{
107 PSHCLHTTPSERVERTRANSFER pSrvTx;
108 RTListForEach(&pThis->lstTransfers, pSrvTx, SHCLHTTPSERVERTRANSFER, Node)
109 {
110 AssertPtr(pSrvTx->pTransfer);
111 /* Be picky here, do a case sensitive comparison. */
112 if (RTStrStartsWith(pszUrl, pSrvTx->szPathVirtual))
113 return VINF_SUCCESS;
114 }
115
116 return VERR_PATH_NOT_FOUND;
117}
118
119static DECLCALLBACK(int) shClTransferHttpOpen(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void **ppvHandle)
120{
121 PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser;
122 Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
123
124 int rc = shClTransferHttpPathValidate(pThis, pReq->pszUrl);
125 if (RT_FAILURE(rc))
126 return rc;
127
128 RT_NOREF(ppvHandle);
129
130 LogFlowFuncLeaveRC(rc);
131 return rc;
132}
133
134static DECLCALLBACK(int) shClTransferHttpRead(PRTHTTPCALLBACKDATA pData, void *pvHandle, void *pvBuf, size_t cbBuf, size_t *pcbRead)
135{
136 PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser;
137 Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
138
139 RT_NOREF(pThis, pvHandle, pvBuf, cbBuf, pcbRead);
140
141 int rc = 0;
142
143 LogFlowFuncLeaveRC(rc);
144 return rc;
145}
146
147static DECLCALLBACK(int) shClTransferHttpClose(PRTHTTPCALLBACKDATA pData, void *pvHandle)
148{
149 PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser;
150 Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
151
152 RT_NOREF(pThis, pvHandle);
153
154 int rc = 0;
155
156 LogFlowFuncLeaveRC(rc);
157 return rc;
158}
159
160static DECLCALLBACK(int) shClTransferHttpQueryInfo(PRTHTTPCALLBACKDATA pData,
161 PRTHTTPSERVERREQ pReq, PRTFSOBJINFO pObjInfo, char **ppszMIMEHint)
162{
163 PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser;
164 Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
165
166 int rc = shClTransferHttpPathValidate(pThis, pReq->pszUrl);
167 if (RT_FAILURE(rc))
168 return rc;
169
170 RT_NOREF(pObjInfo, ppszMIMEHint);
171
172 LogFlowFuncLeaveRC(rc);
173 return rc;
174}
175
176static DECLCALLBACK(int) shClTransferHttpDestroy(PRTHTTPCALLBACKDATA pData)
177{
178 PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser;
179 Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
180
181 return ShClTransferHttpServerDestroyInternal(pThis);
182}
183
184int shClTransferHttpServerDestroyInternal(PSHCLHTTPSERVER pSrv)
185{
186 PSHCLHTTPSERVERTRANSFER pSrvTx, pSrvTxNext;
187 RTListForEachSafe(&pSrv->lstTransfers, pSrvTx, pSrvTxNext, SHCLHTTPSERVERTRANSFER, Node)
188 {
189 RTListNodeRemove(&pSrvTx->Node);
190
191 RTMemFree(pSrvTx);
192 pSrvTx = NULL;
193 }
194
195 RTHttpServerResponseDestroy(&pSrv->Resp);
196
197 return RTCritSectDelete(&pSrv->CritSect);
198}
199
200DECLINLINE(void) shClTransferHttpServerLock(PSHCLHTTPSERVER pSrv)
201{
202 int rc2 = RTCritSectEnter(&pSrv->CritSect);
203 AssertRC(rc2);
204}
205
206DECLINLINE(void) shClTransferHttpServerUnlock(PSHCLHTTPSERVER pSrv)
207{
208 int rc2 = RTCritSectLeave(&pSrv->CritSect);
209 AssertRC(rc2);
210}
211
212
213/*********************************************************************************************************************************
214* Public functions *
215*********************************************************************************************************************************/
216
217/**
218 * Creates a new Shared Clipboard HTTP server instance, extended version.
219 *
220 * @returns VBox status code.
221 * @param pSrv HTTP server instance to create.
222 * @param uPort TCP port number to use.
223 */
224int ShClTransferHttpServerCreateEx(PSHCLHTTPSERVER pSrv, uint16_t uPort)
225{
226 AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
227
228 RTHTTPSERVERCALLBACKS Callbacks;
229 RT_ZERO(Callbacks);
230
231 Callbacks.pfnOpen = shClTransferHttpOpen;
232 Callbacks.pfnRead = shClTransferHttpRead;
233 Callbacks.pfnClose = shClTransferHttpClose;
234 Callbacks.pfnQueryInfo = shClTransferHttpQueryInfo;
235 Callbacks.pfnDestroy = shClTransferHttpDestroy;
236
237 /* Note: The server always and *only* runs against the localhost interface. */
238 int rc = RTHttpServerCreate(&pSrv->hHTTPServer, "localhost", uPort, &Callbacks,
239 pSrv, sizeof(SHCLHTTPSERVER));
240 if (RT_SUCCESS(rc))
241 {
242 rc = RTHttpServerResponseInit(&pSrv->Resp);
243 AssertRCReturn(rc, rc);
244
245 rc = RTCritSectInit(&pSrv->CritSect);
246 AssertRCReturn(rc, rc);
247
248 RTListInit(&pSrv->lstTransfers);
249 pSrv->cTransfers = 0;
250
251 pSrv->uPort = uPort;
252
253 LogRel2(("Shared Clipboard: HTTP server running at port %RU16\n", pSrv->uPort));
254 }
255 else
256 {
257 int rc2 = shClTransferHttpServerDestroyInternal(pSrv);
258 AssertRC(rc2);
259 }
260
261 if (RT_FAILURE(rc))
262 LogRel(("Shared Clipboard: HTTP server failed to run, rc=%Rrc\n", rc));
263
264 return rc;
265}
266
267/**
268 * Creates a new Shared Clipboard HTTP server instance.
269 *
270 * This does automatic probing of TCP ports if one already is being used.
271 *
272 * @returns VBox status code.
273 * @param pSrv HTTP server instance to create.
274 * @param puPort Where to return the TCP port number being used on success.
275 */
276int ShClTransferHttpServerCreate(PSHCLHTTPSERVER pSrv, uint16_t *puPort)
277{
278 AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
279 AssertPtrReturn(puPort, VERR_INVALID_POINTER);
280
281 /** @todo Try favorite ports first (e.g. 8080, 8000, ...)? */
282
283 RTRAND hRand;
284 int rc = RTRandAdvCreateSystemFaster(&hRand); /* Should be good enough for this task. */
285 if (RT_SUCCESS(rc))
286 {
287 uint16_t uPort;
288 for (int i = 0; i < 32; i++)
289 {
290 uPort = RTRandAdvU32Ex(hRand, 1024, UINT16_MAX);
291#ifdef DEBUG_andy
292 uPort = 8000; /** @todo BUGBUG Remove this! */
293#endif
294 rc = ShClTransferHttpServerCreateEx(pSrv, (uint32_t)uPort);
295 if (RT_SUCCESS(rc))
296 {
297 *puPort = uPort;
298 break;
299 }
300 }
301
302 RTRandAdvDestroy(hRand);
303 }
304
305 return rc;
306}
307
308/**
309 * Destroys a Shared Clipboard HTTP server instance.
310 *
311 * @returns VBox status code.
312 * @param pSrv HTTP server instance to destroy.
313 */
314int ShClTransferHttpServerDestroy(PSHCLHTTPSERVER pSrv)
315{
316 AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
317
318 if (pSrv->hHTTPServer == NIL_RTHTTPSERVER)
319 return VINF_SUCCESS;
320
321 Assert(pSrv->cTransfers == 0); /* Sanity. */
322
323 int rc = RTHttpServerDestroy(pSrv->hHTTPServer);
324 if (RT_SUCCESS(rc))
325 rc = shClTransferHttpServerDestroyInternal(pSrv);
326
327 if (RT_SUCCESS(rc))
328 LogRel2(("Shared Clipboard: HTTP server stopped\n"));
329 else
330 LogRel(("Shared Clipboard: HTTP server failed to stop, rc=%Rrc\n", rc));
331
332 return rc;
333}
334
335/**
336 * Registers a Shared Clipboard transfer to a HTTP server instance.
337 *
338 * @returns VBox status code.
339 * @param pSrv HTTP server instance to register transfer for.
340 * @param pTransfer Transfer to register.
341 */
342int ShClTransferHttpServerRegisterTransfer(PSHCLHTTPSERVER pSrv, PSHCLTRANSFER pTransfer)
343{
344 AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
345 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
346
347 shClTransferHttpServerLock(pSrv);
348
349 PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)RTMemAllocZ(sizeof(SHCLHTTPSERVERTRANSFER));
350 AssertPtrReturn(pSrvTx, VERR_NO_MEMORY);
351
352 RTUUID Uuid;
353 int rc = RTUuidCreate(&Uuid);
354 if (RT_SUCCESS(rc))
355 {
356 char szUuid[64];
357 rc = RTUuidToStr(&Uuid, szUuid, sizeof(szUuid));
358 if (RT_SUCCESS(rc))
359 {
360 AssertReturn(pTransfer->State.uID, VERR_INVALID_PARAMETER); /* Paranoia. */
361
362 /* Create the virtual HTTP path for the transfer.
363 * Every transfer has a dedicated HTTP path. */
364 ssize_t cch = RTStrPrintf2(pSrvTx->szPathVirtual, sizeof(pSrvTx->szPathVirtual), "%s/%RU16/", szUuid, pTransfer->State.uID);
365 AssertReturn(cch, VERR_BUFFER_OVERFLOW);
366
367 RTListAppend(&pSrv->lstTransfers, &pSrvTx->Node);
368
369 pSrv->cTransfers++;
370 }
371 }
372
373 if (RT_FAILURE(rc))
374 RTMemFree(pSrvTx);
375
376 shClTransferHttpServerUnlock(pSrv);
377
378 return rc;
379}
380
381/**
382 * Unregisters a formerly registered Shared Clipboard transfer.
383 *
384 * @returns VBox status code.
385 * @param pSrv HTTP server instance to unregister transfer from.
386 * @param pTransfer Transfer to unregister.
387 */
388int ShClTransferHttpServerUnregisterTransfer(PSHCLHTTPSERVER pSrv, PSHCLTRANSFER pTransfer)
389{
390 AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
391 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
392
393 shClTransferHttpServerLock(pSrv);
394
395 AssertReturn(pSrv->cTransfers, VERR_WRONG_ORDER);
396
397 PSHCLHTTPSERVERTRANSFER pSrvTx;
398 RTListForEach(&pSrv->lstTransfers, pSrvTx, SHCLHTTPSERVERTRANSFER, Node)
399 {
400 AssertPtr(pSrvTx->pTransfer);
401 if (pSrvTx->pTransfer->State.uID == pTransfer->State.uID)
402 {
403 RTListNodeRemove(&pSrvTx->Node);
404
405 RTMemFree(pSrvTx);
406 pSrvTx = NULL;
407
408 Assert(pSrv->cTransfers);
409 pSrv->cTransfers--;
410
411 shClTransferHttpServerUnlock(pSrv);
412 return VINF_SUCCESS;
413 }
414 }
415
416 shClTransferHttpServerUnlock(pSrv);
417 return VERR_NOT_FOUND;
418}
419
420/**
421 * Returns whether a specific transfer ID is registered with a HTTP server instance or not.
422 *
423 * @returns \c true if the transfer ID is registered, \c false if not.
424 * @param pSrv HTTP server instance.
425 * @param idTransfer Transfer ID to check for.
426 */
427bool ShClTransferHttpServerHasTransfer(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer)
428{
429 AssertPtrReturn(pSrv, false);
430
431 shClTransferHttpServerLock(pSrv);
432
433 const bool fRc = shClTransferHttpServerGetTransferById(pSrv, idTransfer) != NULL;
434
435 shClTransferHttpServerUnlock(pSrv);
436
437 return fRc;
438}
439
440/**
441 * Returns the used TCP port number of a HTTP server instance.
442 *
443 * @returns TCP port number. 0 if not specified yet.
444 * @param pSrv HTTP server instance to return port for.
445 */
446uint16_t ShClTransferHttpServerGetPort(PSHCLHTTPSERVER pSrv)
447{
448 AssertPtrReturn(pSrv, 0);
449
450 shClTransferHttpServerLock(pSrv);
451
452 const uint16_t uPort = pSrv->uPort;
453
454 shClTransferHttpServerUnlock(pSrv);
455
456 return uPort;
457}
458
459/**
460 * Returns the number of registered HTTP server transfers of a HTTP server instance.
461 *
462 * @returns Number of registered transfers.
463 * @param pSrv HTTP server instance to return registered transfers for.
464 */
465uint32_t ShClTransferHttpServerGetTransferCount(PSHCLHTTPSERVER pSrv)
466{
467 AssertPtrReturn(pSrv, 0);
468
469 shClTransferHttpServerLock(pSrv);
470
471 const uint32_t cTransfers = pSrv->cTransfers;
472
473 shClTransferHttpServerUnlock(pSrv);
474
475 return cTransfers;
476}
477
478/**
479 * Returns the host name (scheme) of a HTTP server instance.
480 *
481 * @param pSrv HTTP server instance to return host name (scheme) for.
482 *
483 * @returns Host name (scheme).
484 */
485static const char *shClTransferHttpServerGetHost(PSHCLHTTPSERVER pSrv)
486{
487 RT_NOREF(pSrv);
488 return "http://localhost"; /* Hardcoded for now. */
489}
490
491/**
492 * Returns an allocated string with a HTTP server instance's address.
493 *
494 * @returns Allocated string with a HTTP server instance's address, or NULL on OOM.
495 * Needs to be free'd by the caller using RTStrFree().
496 * @param pSrv HTTP server instance to return address for.
497 */
498char *ShClTransferHttpServerGetAddressA(PSHCLHTTPSERVER pSrv)
499{
500 AssertPtrReturn(pSrv, NULL);
501
502 shClTransferHttpServerLock(pSrv);
503
504 char *pszAddress = RTStrAPrintf2("%s:%RU16", shClTransferHttpServerGetHost(pSrv), pSrv->uPort);
505 AssertPtr(pszAddress);
506
507 shClTransferHttpServerUnlock(pSrv);
508
509 return pszAddress;
510}
511
512/**
513 * Returns an allocated string with the URL of a given Shared Clipboard transfer ID.
514 *
515 * @returns Allocated string with the URL of a given Shared Clipboard transfer ID, or NULL if not found.
516 * Needs to be free'd by the caller using RTStrFree().
517 * @param pSrv HTTP server instance to return URL for.
518 */
519char *ShClTransferHttpServerGetUrlA(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer)
520{
521 AssertPtrReturn(pSrv, NULL);
522 AssertReturn(idTransfer != NIL_SHCLTRANSFERID, NULL);
523
524 shClTransferHttpServerLock(pSrv);
525
526 PSHCLHTTPSERVERTRANSFER pSrvTx = shClTransferHttpServerGetTransferById(pSrv, idTransfer);
527 if (!pSrvTx)
528 {
529 AssertFailed();
530 shClTransferHttpServerUnlock(pSrv);
531 return NULL;
532 }
533
534 AssertReturn(RTStrNLen(pSrvTx->szPathVirtual, RTPATH_MAX), NULL);
535 char *pszUrl = RTStrAPrintf2("%s:%RU16/%s", shClTransferHttpServerGetHost(pSrv), pSrv->uPort, pSrvTx->szPathVirtual);
536 AssertPtr(pszUrl);
537
538 shClTransferHttpServerUnlock(pSrv);
539
540 return pszUrl;
541}
542
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