VirtualBox

Changeset 43219 in vbox for trunk/src/VBox/ExtPacks


Ignore:
Timestamp:
Sep 6, 2012 10:06:51 AM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
80609
Message:

VBoxVNC: Made it less ugly, hope it works (I don't care).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ExtPacks/VNC/VBoxVNC.cpp

    r43214 r43219  
    1919 */
    2020
     21/*******************************************************************************
     22*   Header Files                                                               *
     23*******************************************************************************/
    2124#define LOG_GROUP LOG_GROUP_VRDE
    2225#include <VBox/log.h>
     
    4043#include <rfb/rfb.h>
    4144
    42 #define VNC_SIZEOFRGBA 4
    43 #define VNC_PASSWORDSIZE 20
    44 #define VNC_ADDRESSSIZE 60
    45 #define VNC_PORTSSIZE 20
    46 #define VNC_ADDRESS_OPTION_MAX 500
    47 
     45
     46/*******************************************************************************
     47*   Defined Constants And Macros                                               *
     48*******************************************************************************/
     49#define VNC_SIZEOFRGBA          4
     50#define VNC_PASSWORDSIZE        20
     51#define VNC_ADDRESSSIZE         60
     52#define VNC_PORTSSIZE           20
     53#define VNC_ADDRESS_OPTION_MAX  500
     54
     55
     56/*******************************************************************************
     57*   Structures and Typedefs                                                    *
     58*******************************************************************************/
    4859class VNCServerImpl
    4960{
     
    98109    }
    99110
     111    int     queryVrdeFeature(const char *pszName, char *pszValue, size_t cbValue);
     112
    100113    static VRDEENTRYPOINTS_4 Entries;
    101114    VRDECALLBACKS_4 *mCallbacks;
    102115
    103 
    104116    static DECLCALLBACK(void) VRDEDestroy(HVRDESERVER hServer);
    105     static DECLCALLBACK(int) VRDEEnableConnections(HVRDESERVER hServer, bool fEnable);
     117    static DECLCALLBACK(int)  VRDEEnableConnections(HVRDESERVER hServer, bool fEnable);
    106118    static DECLCALLBACK(void) VRDEDisconnect(HVRDESERVER hServer, uint32_t u32ClientId, bool fReconnect);
    107119    static DECLCALLBACK(void) VRDEResize(HVRDESERVER hServer);
     
    182194
    183195
     196/**
     197 * Query a feature and store it's value in a user supplied buffer.
     198 *
     199 * @returns VBox status code.
     200 * @param   pszName             The feature name.
     201 * @param   pszValue            The value buffer.  The buffer is not touched at
     202 *                              all on failure.
     203 * @param   cbValue             The size of the output buffer.
     204 */
     205int VNCServerImpl::queryVrdeFeature(const char *pszName, char *pszValue, size_t cbValue)
     206{
     207    union
     208    {
     209        VRDEFEATURE Feat;
     210        uint8_t     abBuf[VNC_ADDRESS_OPTION_MAX + sizeof(VRDEFEATURE)];
     211    } u;
     212
     213    u.Feat.u32ClientId = 0;
     214    int rc = RTStrCopy(u.Feat.achInfo, VNC_ADDRESS_OPTION_MAX, pszName); AssertRC(rc);
     215    if (RT_SUCCESS(rc))
     216    {
     217        uint32_t cbOut = 0;
     218        rc = mCallbacks->VRDECallbackProperty(mCallback,
     219                                              VRDE_QP_FEATURE,
     220                                              &u.Feat,
     221                                              VNC_ADDRESS_OPTION_MAX,
     222                                              &cbOut);
     223        if (RT_SUCCESS(rc))
     224        {
     225            size_t cbRet = strlen(u.Feat.achInfo) + 1;
     226            if (cbRet <= cbValue)
     227                memcpy(pszValue, u.Feat.achInfo, cbRet);
     228            else
     229                rc = VERR_BUFFER_OVERFLOW;
     230        }
     231    }
     232
     233    return rc;
     234}
     235
     236
    184237/** The server should start to accept clients connections.
    185238 *
     
    333386    char szIPv4ListenAll[] = "0.0.0.0";
    334387
    335     uint32_t ulServerPort4 = 0;
    336     uint32_t ulServerPort6 = 0;
     388    uint32_t uServerPort4 = 0;
     389    uint32_t uServerPort6 = 0;
    337390    uint32_t cbOut = 0;
    338391    size_t resSize = 0;
    339392    RTNETADDRTYPE enmAddrType;
    340     char *pszTCPAddress = NULL;
    341     char *pszTCPPort = NULL;
    342     char *pszVNCAddress4 = NULL;
    343     char *pszVNCPort4 = NULL;
    344393    char *pszVNCAddress6 = NULL;
    345394    char *pszVNCPort6 = NULL;
     
    350399
    351400    // get address
    352     rc = -1;
    353     pszTCPAddress = (char *) RTMemTmpAlloc(VNC_ADDRESS_OPTION_MAX);
    354     memset(pszTCPAddress, '\0', VNC_ADDRESS_OPTION_MAX);
    355 
     401    char *pszTCPAddress = (char *)RTMemTmpAllocZ(VNC_ADDRESS_OPTION_MAX);
    356402    rc = instance->mCallbacks->VRDECallbackProperty(instance->mCallback,
    357403                                                    VRDE_QP_NETWORK_ADDRESS,
     
    361407
    362408    // get port (range)
    363     rc = -1;
    364     pszTCPPort = (char *) RTMemTmpAlloc(VNC_ADDRESS_OPTION_MAX);
    365     memset(pszTCPPort,'\0',VNC_ADDRESS_OPTION_MAX);
    366 
     409    char *pszTCPPort = (char *)RTMemTmpAllocZ(VNC_ADDRESS_OPTION_MAX);
    367410    rc = instance->mCallbacks->VRDECallbackProperty(instance->mCallback,
    368411                                                    VRDE_QP_NETWORK_PORT_RANGE,
     
    370413                                                    VNC_ADDRESS_OPTION_MAX,
    371414                                                    &cbOut);
    372 
    373415    Assert(cbOut < VNC_ADDRESS_OPTION_MAX);
    374416
    375     // get tcp ports option from vrde
    376 
    377     rc = -1;
    378 
    379     pszTCPPort = (char *) RTMemTmpAlloc(6);
    380     memset(pszTCPPort,'\0',6);
    381 
    382     VRDEFEATURE *pVRDEFeature = (VRDEFEATURE *) RTMemTmpAlloc(VNC_ADDRESS_OPTION_MAX);
    383     pVRDEFeature->u32ClientId = 0;
    384 
    385     /** @todo r=bird: I believe this code is more than a little of confused
    386      *        about how to use RTStrCopy...  The 2nd parameter is the size of the
    387      *        destination buffer, not the size of the source string!!   */
    388     RTStrCopy(pVRDEFeature->achInfo, 19, "Property/TCP/Ports");
    389 
    390     cbOut = 1;
    391 
    392     rc = instance->mCallbacks->VRDECallbackProperty(instance->mCallback,
    393                                                     VRDE_QP_FEATURE,
    394                                                     pVRDEFeature,
    395                                                     VNC_ADDRESS_OPTION_MAX,
    396                                                     &cbOut);
    397 
    398     Assert(cbOut < VNC_ADDRESS_OPTION_MAX);
    399 
    400     if (RT_SUCCESS(rc))
    401     {
    402         RTStrCopy(pszTCPPort, cbOut, pVRDEFeature->achInfo);
    403     }
     417    // get tcp ports option from vrde.
     418    /** @todo r=bird: Is this intentionally overriding VRDE_QP_NETWORK_PORT_RANGE? */
     419    instance->queryVrdeFeature("Property/TCP/Ports", pszTCPPort, VNC_ADDRESS_OPTION_MAX);
    404420
    405421    // get VNCAddress4
    406     rc = -1;
    407     const uint32_t lVNCAddress4FeatureSize = sizeof(VRDEFEATURE) + 24;
    408 
    409     pszVNCAddress4 = (char *) RTMemTmpAllocZ(24);
    410 
    411     pVRDEFeature->u32ClientId = 0;
    412 
    413     RTStrCopy(pVRDEFeature->achInfo, 21, "Property/VNCAddress4");
    414 
    415     cbOut = 1;
    416 
    417     rc = instance->mCallbacks->VRDECallbackProperty(instance->mCallback,
    418                                                     VRDE_QP_FEATURE,
    419                                                     pVRDEFeature,
    420                                                     VNC_ADDRESS_OPTION_MAX,
    421                                                     &cbOut);
    422 
    423     Assert(cbOut <= 24);
    424 
    425     if (RT_SUCCESS(rc))
    426     {
    427        RTStrCopy(pszVNCAddress4,cbOut,pVRDEFeature->achInfo);
    428     }
     422    char *pszVNCAddress4 = (char *)RTMemTmpAllocZ(24);
     423    instance->queryVrdeFeature("Property/VNCAddress4", pszVNCAddress4, 24);
    429424
    430425    // VNCPort4
    431     rc = -1;
    432     pszVNCPort4 = (char *) RTMemTmpAlloc(6);
    433 
    434     pVRDEFeature->u32ClientId = 0;
    435     RTStrCopy(pVRDEFeature->achInfo, VNC_ADDRESS_OPTION_MAX, "Property/VNCPort4");
    436 
    437     cbOut = VNC_ADDRESS_OPTION_MAX;
    438     rc = instance->mCallbacks->VRDECallbackProperty(instance->mCallback,
    439                                                     VRDE_QP_FEATURE,
    440                                                     pVRDEFeature,
    441                                                     VNC_ADDRESS_OPTION_MAX,
    442                                                     &cbOut);
    443 
    444 
    445     Assert(cbOut <= VNC_ADDRESS_OPTION_MAX);
    446     if (RT_SUCCESS(rc))
    447     {
    448         if (cbOut < 6)
    449         {
    450             RTStrCopy(pszVNCPort4, cbOut, pVRDEFeature->achInfo);
    451         }
    452 
    453     }
     426    char *pszVNCPort4 = (char *)RTMemTmpAlloc(6);
     427    instance->queryVrdeFeature("Property/VNCPort4", pszVNCPort4, 6);
    454428
    455429    // VNCAddress6
    456     rc = -1;
    457 
    458430    pszVNCAddress6 = (char *) RTMemTmpAllocZ(VNC_ADDRESS_OPTION_MAX);
    459 
    460     pVRDEFeature->u32ClientId = 0;
    461     RTStrCopy(pVRDEFeature->achInfo, 21,"Property/VNCAddress6");
    462 
    463     cbOut = VNC_ADDRESS_OPTION_MAX;
    464 
    465     rc = instance->mCallbacks->VRDECallbackProperty(instance->mCallback,
    466                                                     VRDE_QP_FEATURE,
    467                                                     pVRDEFeature,
    468                                                     VNC_ADDRESS_OPTION_MAX,
    469                                                     &cbOut);
    470 
    471     Assert(cbOut <= VNC_ADDRESS_OPTION_MAX);
    472 
    473     if (RT_SUCCESS(rc))
    474     {
    475         RTStrCopy(pszVNCAddress6, cbOut, pVRDEFeature->achInfo);
    476     }
     431    instance->queryVrdeFeature("Property/VNCAddress6", pszVNCAddress6, VNC_ADDRESS_OPTION_MAX);
    477432
    478433    // VNCPort6
    479     pszVNCPort6 = (char *) RTMemTmpAllocZ(6);
    480 
    481     pVRDEFeature->u32ClientId = 0;
    482     RTStrCopy(pVRDEFeature->achInfo, 18 , "Property/VNCPort6");
    483 
    484     cbOut = 5;
    485 
    486     rc = instance->mCallbacks->VRDECallbackProperty(instance->mCallback,
    487                                                     VRDE_QP_FEATURE,
    488                                                     pVRDEFeature,
    489                                                     VNC_ADDRESS_OPTION_MAX,
    490                                                     &cbOut);
    491 
    492     Assert(cbOut <= 6);
    493 
    494     if (RT_SUCCESS(rc))
    495     {
    496         RTStrCopy(pszVNCPort6, cbOut, pVRDEFeature->achInfo);
    497     }
     434    pszVNCPort6 = (char *)RTMemTmpAllocZ(6);
     435    instance->queryVrdeFeature("Property/VNCPort6", pszVNCPort6, 6);
     436
    498437
    499438    if (RTNetIsIPv4AddrStr(pszTCPAddress))
     
    503442        if (strlen(pszTCPPort) > 0)
    504443        {
    505             rc = RTStrToUInt32Ex(pszTCPPort, NULL, 10, &ulServerPort4);
    506 
    507             if (!RT_SUCCESS(rc) || ulServerPort4 > 65535)
    508                 ulServerPort4 = 0;
     444            rc = RTStrToUInt32Ex(pszTCPPort, NULL, 10, &uServerPort4);
     445            if (!RT_SUCCESS(rc) || uServerPort4 > 65535)
     446                uServerPort4 = 0;
    509447        }
    510448
     
    516454        if (strlen(pszVNCPort6) > 0)
    517455        {
    518             rc = RTStrToUInt32Ex(pszVNCPort6, NULL, 10, &ulServerPort6);
    519 
    520             if (!RT_SUCCESS(rc) || ulServerPort6 > 65535)
    521                 ulServerPort6 = 0;
     456            rc = RTStrToUInt32Ex(pszVNCPort6, NULL, 10, &uServerPort6);
     457            if (!RT_SUCCESS(rc) || uServerPort6 > 65535)
     458                uServerPort6 = 0;
    522459
    523460        }
     
    531468        if (strlen(pszTCPPort) > 0)
    532469        {
    533             rc = RTStrToUInt32Ex(pszTCPPort, NULL, 10, &ulServerPort6);
    534 
    535             if (!RT_SUCCESS(rc) || ulServerPort6 > 65535)
    536                 ulServerPort6 = 0;
     470            rc = RTStrToUInt32Ex(pszTCPPort, NULL, 10, &uServerPort6);
     471            if (!RT_SUCCESS(rc) || uServerPort6 > 65535)
     472                uServerPort6 = 0;
    537473        }
    538474
     
    544480        if (strlen(pszVNCPort4) > 0)
    545481        {
    546             rc = RTStrToUInt32Ex(pszVNCPort4, NULL, 10, &ulServerPort4);
    547 
    548             if (!RT_SUCCESS(rc) || ulServerPort4 > 65535)
    549                 ulServerPort4 = 0;
     482            rc = RTStrToUInt32Ex(pszVNCPort4, NULL, 10, &uServerPort4);
     483            if (!RT_SUCCESS(rc) || uServerPort4 > 65535)
     484                uServerPort4 = 0;
    550485
    551486        }
     
    560495
    561496        rc = RTSocketQueryAddressStr(pszTCPAddress, pszGetAddrInfo6, &resSize, &enmAddrType);
    562 
    563497        if (RT_SUCCESS(rc))
    564         {
    565498            pszServerAddress6 = pszGetAddrInfo6;
    566 
    567         }
    568499        else
    569500        {
     
    581512
    582513            if (RT_SUCCESS(rc))
    583             {
    584514                pszServerAddress4 = pszGetAddrInfo4;
    585             }
    586515            else
    587516            {
     
    632561    }
    633562
    634     if (pszVNCPort4 && ulServerPort4 == 0)
    635     {
    636         rc = RTStrToUInt32Ex(pszVNCPort4, NULL, 10, &ulServerPort4);
    637 
    638         if (!RT_SUCCESS(rc) || ulServerPort4 > 65535)
    639             ulServerPort4 = 0;
    640     }
    641     if (pszVNCPort6 && ulServerPort6 == 0)
    642     {
    643         rc = RTStrToUInt32Ex(pszVNCPort6, NULL, 10, &ulServerPort6);
    644 
    645         if (!RT_SUCCESS(rc) || ulServerPort6 > 65535)
    646             ulServerPort6 = 0;
    647     }
    648     if (ulServerPort4 == 0 || ulServerPort6 == 0)
    649     {
     563    if (pszVNCPort4 && uServerPort4 == 0)
     564    {
     565        rc = RTStrToUInt32Ex(pszVNCPort4, NULL, 10, &uServerPort4);
     566        if (!RT_SUCCESS(rc) || uServerPort4 > 65535)
     567            uServerPort4 = 0;
     568    }
     569
     570    if (pszVNCPort6 && uServerPort6 == 0)
     571    {
     572        rc = RTStrToUInt32Ex(pszVNCPort6, NULL, 10, &uServerPort6);
     573        if (!RT_SUCCESS(rc) || uServerPort6 > 65535)
     574            uServerPort6 = 0;
     575    }
     576
     577    if (uServerPort4 == 0 || uServerPort6 == 0)
    650578        vncServer->autoPort = 1;
    651     }
    652579    else
    653580    {
    654         vncServer->port = ulServerPort4;
    655         vncServer->ipv6port = ulServerPort6;
     581        vncServer->port = uServerPort4;
     582        vncServer->ipv6port = uServerPort6;
    656583    }
    657584
     
    683610    LogRel(("VNC: port6 = %u\n", port));
    684611
    685     if (pVRDEFeature)
    686         RTMemTmpFree(pVRDEFeature);
    687612
    688613    if (pszTCPAddress)
     
    697622    }
    698623
    699     if (pszTCPPort)
    700         RTMemTmpFree(pszTCPPort);
    701 
    702     if (pszVNCAddress4)
    703         RTMemTmpFree(pszVNCAddress4);
    704 
    705     if (pszVNCPort4)
    706         RTMemTmpFree(pszVNCPort4);
    707 
    708     if (pszGetAddrInfo4)
    709         RTMemTmpFree(pszGetAddrInfo4);
    710 
    711     if (pszVNCAddress6)
    712         RTMemTmpFree(pszVNCAddress6);
    713 
    714     if (pszGetAddrInfo6)
    715         RTMemTmpFree(pszGetAddrInfo6);
     624    RTMemTmpFree(pszTCPPort);
     625    RTMemTmpFree(pszVNCAddress4);
     626    RTMemTmpFree(pszVNCPort4);
     627    RTMemTmpFree(pszGetAddrInfo4);
     628    RTMemTmpFree(pszVNCAddress6);
     629    RTMemTmpFree(pszGetAddrInfo6);
    716630
    717631    // with ipv6 to here
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette