VirtualBox

Changeset 58297 in vbox


Ignore:
Timestamp:
Oct 18, 2015 3:12:50 PM (9 years ago)
Author:
vboxsync
Message:

RTLocalIpc: Added flag to the server and connection creation functions to accept native paths (default is a portable name that gets prefixed with platform specific path).

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/localipc.h

    r58295 r58297  
    7777/** The server can handle multiple sessions. */
    7878#define RTLOCALIPC_FLAGS_MULTI_SESSION      RT_BIT_32(0)
     79/** Native name, as apposed to a portable one. */
     80#define RTLOCALIPC_FLAGS_NATIVE_NAME        RT_BIT_32(1)
    7981/** The mask of valid flags. */
    80 #define RTLOCALIPC_FLAGS_VALID_MASK         UINT32_C(0x00000001)
     82#define RTLOCALIPC_FLAGS_VALID_MASK         UINT32_C(0x00000003)
    8183/** @} */
    8284
     
    124126 * @param   phSession           Where to store the sesson handle on success.
    125127 * @param   pszName             The server name (see RTLocalIpcServerCreate for details).
    126  * @param   fFlags              Flags. Current undefined, pass 0.
     128 * @param   fFlags              Flags, RTLOCALIPC_C_FLAGS_XXX.
    127129 */
    128130RTDECL(int) RTLocalIpcSessionConnect(PRTLOCALIPCSESSION phSession, const char *pszName, uint32_t fFlags);
     131
     132/** @name RTLOCALIPC_C_FLAGS_XXX - RTLocalIpcSessionConnect flags
     133 * @{ */
     134/** Native name, as apposed to a portable one. */
     135#define RTLOCALIPC_C_FLAGS_NATIVE_NAME      RT_BIT_32(0)
     136/** The mask of valid flags. */
     137#define RTLOCALIPC_C_FLAGS_VALID_MASK       UINT32_C(0x00000001)
     138/** @} */
    129139
    130140
  • trunk/src/VBox/Runtime/r3/posix/localipc-posix.cpp

    r58295 r58297  
    5555
    5656#include "internal/magics.h"
     57#include "internal/path.h"
    5758#include "internal/socket.h"
    5859
     
    113114
    114115
    115 /** Local IPC name prefix. */
     116/** Local IPC name prefix for portable names. */
    116117#define RTLOCALIPC_POSIX_NAME_PREFIX    "/tmp/.iprt-localipc-"
    117118
     
    122123 * @returns IPRT status code.
    123124 * @param   pszName             The name to validate.
    124  * @param   pcchName            Where to return the length.
    125  */
    126 static int rtLocalIpcPosixValidateName(const char *pszName, size_t *pcchName)
     125 * @param   fNative             Whether it's a native name or a portable name.
     126 */
     127static int rtLocalIpcPosixValidateName(const char *pszName, bool fNative)
    127128{
    128129    AssertPtrReturn(pszName, VERR_INVALID_POINTER);
    129 
    130     uint32_t cchName = 0;
    131     for (;;)
     130    AssertReturn(*pszName, VERR_INVALID_NAME);
     131
     132    if (!fNative)
    132133    {
    133         char ch = pszName[cchName];
    134         if (!ch)
    135             break;
    136         AssertReturn(!RT_C_IS_CNTRL(ch), VERR_INVALID_NAME);
    137         AssertReturn((unsigned)ch < 0x80, VERR_INVALID_NAME);
    138         AssertReturn(ch != '\\', VERR_INVALID_NAME);
    139         AssertReturn(ch != '/', VERR_INVALID_NAME);
    140         cchName++;
     134        for (;;)
     135        {
     136            char ch = *pszName++;
     137            if (!ch)
     138                break;
     139            AssertReturn(!RT_C_IS_CNTRL(ch), VERR_INVALID_NAME);
     140            AssertReturn((unsigned)ch < 0x80, VERR_INVALID_NAME);
     141            AssertReturn(ch != '\\', VERR_INVALID_NAME);
     142            AssertReturn(ch != '/', VERR_INVALID_NAME);
     143        }
    141144    }
    142 
    143     *pcchName = cchName;
    144     AssertReturn(sizeof(RTLOCALIPC_POSIX_NAME_PREFIX) + cchName <= RT_SIZEOFMEMB(struct sockaddr_un, sun_path),
    145                  VERR_FILENAME_TOO_LONG);
    146     AssertReturn(cchName, VERR_INVALID_NAME);
     145    else
     146    {
     147        int rc = RTStrValidateEncoding(pszName);
     148        AssertRCReturn(rc, rc);
     149    }
    147150
    148151    return VINF_SUCCESS;
     
    157160 * @param   pcbAddr             Where to return the address size.
    158161 * @param   pszName             The user specified name (valid).
    159  * @param   cchName             The user specified name length.
    160  */
    161 static int rtLocalIpcPosixConstructName(struct sockaddr_un *pAddr, uint8_t *pcbAddr, const char *pszName, size_t cchName)
    162 {
    163     AssertMsgReturn(cchName + sizeof(RTLOCALIPC_POSIX_NAME_PREFIX) <= sizeof(pAddr->sun_path),
    164                     ("cchName=%zu sizeof(sun_path)=%zu\n", cchName, sizeof(pAddr->sun_path)),
    165                     VERR_FILENAME_TOO_LONG);
    166 
    167 /** @todo Bother converting to local codeset/encoding??  */
    168 
    169     RT_ZERO(*pAddr);
     162 * @param   fNative             Whether it's a native name or a portable name.
     163 */
     164static int rtLocalIpcPosixConstructName(struct sockaddr_un *pAddr, uint8_t *pcbAddr, const char *pszName, bool fNative)
     165{
     166    const char *pszNativeName;
     167    int rc = rtPathToNative(&pszNativeName, pszName, NULL /*pszBasePath not support*/);
     168    if (RT_SUCCESS(rc))
     169    {
     170        size_t cchNativeName = strlen(pszNativeName);
     171        size_t cbFull = !fNative ? cchNativeName + sizeof(RTLOCALIPC_POSIX_NAME_PREFIX) : cchNativeName + 1;
     172        if (cbFull <= sizeof(pAddr->sun_path))
     173        {
     174            RT_ZERO(*pAddr);
    170175#ifdef RT_OS_OS2 /* Size must be exactly right on OS/2. */
    171     *pcbAddr = sizeof(*pAddr);
     176            *pcbAddr = sizeof(*pAddr);
    172177#else
    173     *pcbAddr = RT_OFFSETOF(struct sockaddr_un, sun_path) + (uint8_t)cchName + sizeof(RTLOCALIPC_POSIX_NAME_PREFIX);
     178            *pcbAddr = RT_OFFSETOF(struct sockaddr_un, sun_path) + (uint8_t)cbFull;
    174179#endif
    175180#ifdef HAVE_SUN_LEN_MEMBER
    176     pAddr->sun_len     = *pcbAddr;
     181            pAddr->sun_len     = *pcbAddr;
    177182#endif
    178     pAddr->sun_family  = AF_LOCAL;
    179     memcpy(pAddr->sun_path, RTLOCALIPC_POSIX_NAME_PREFIX, sizeof(RTLOCALIPC_POSIX_NAME_PREFIX) - 1);
    180     memcpy(&pAddr->sun_path[sizeof(RTLOCALIPC_POSIX_NAME_PREFIX) - 1], pszName, cchName + 1);
    181 
    182     return VINF_SUCCESS;
     183            pAddr->sun_family  = AF_LOCAL;
     184
     185            if (!fNative)
     186            {
     187                memcpy(pAddr->sun_path, RTLOCALIPC_POSIX_NAME_PREFIX, sizeof(RTLOCALIPC_POSIX_NAME_PREFIX) - 1);
     188                memcpy(&pAddr->sun_path[sizeof(RTLOCALIPC_POSIX_NAME_PREFIX) - 1], pszNativeName, cchNativeName + 1);
     189            }
     190            else
     191                memcpy(pAddr->sun_path, pszNativeName, cchNativeName + 1);
     192        }
     193        else
     194            rc = VERR_FILENAME_TOO_LONG;
     195        rtPathFreeNative(pszNativeName, pszName);
     196    }
     197    return rc;
    183198}
    184199
     
    195210    AssertReturn(!(fFlags & ~RTLOCALIPC_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
    196211
    197     size_t cchName;
    198     int rc = rtLocalIpcPosixValidateName(pszName, &cchName);
     212    int rc = rtLocalIpcPosixValidateName(pszName, RT_BOOL(fFlags & RTLOCALIPC_FLAGS_NATIVE_NAME));
    199213    if (RT_SUCCESS(rc))
    200214    {
     
    222236
    223237                    uint8_t cbAddr;
    224                     rc = rtLocalIpcPosixConstructName(&pThis->Name, &cbAddr, pszName, cchName);
     238                    rc = rtLocalIpcPosixConstructName(&pThis->Name, &cbAddr, pszName,
     239                                                      RT_BOOL(fFlags & RTLOCALIPC_FLAGS_NATIVE_NAME));
    225240                    if (RT_SUCCESS(rc))
    226241                    {
     
    475490    *phSession = NIL_RTLOCALIPCSESSION;
    476491
    477     AssertReturn(!fFlags, VERR_INVALID_FLAGS);
    478 
    479     size_t cchName;
    480     int rc = rtLocalIpcPosixValidateName(pszName, &cchName);
     492    AssertReturn(!(fFlags & ~RTLOCALIPC_C_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
     493
     494    int rc = rtLocalIpcPosixValidateName(pszName, RT_BOOL(fFlags & RTLOCALIPC_C_FLAGS_NATIVE_NAME));
    481495    if (RT_SUCCESS(rc))
    482496    {
     
    507521                    struct sockaddr_un  Addr;
    508522                    uint8_t             cbAddr;
    509                     rc = rtLocalIpcPosixConstructName(&Addr, &cbAddr, pszName, cchName);
     523                    rc = rtLocalIpcPosixConstructName(&Addr, &cbAddr, pszName, RT_BOOL(fFlags & RTLOCALIPC_C_FLAGS_NATIVE_NAME));
    510524                    if (RT_SUCCESS(rc))
    511525                    {
Note: See TracChangeset for help on using the changeset viewer.

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