VirtualBox

Changeset 108818 in vbox


Ignore:
Timestamp:
Apr 1, 2025 9:09:35 PM (2 weeks ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
168264
Message:

libslirp: Handle table cleanups & docs.

Location:
trunk/src/libs/libslirp-4.8.0/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/libslirp-4.8.0/src/nathandletable.c

    r108816 r108818  
     1/* $Id$ */
    12/** @file
    2  * libslirp: NAT Hanlde Table Wrapper
     3 * libslirp: NAT Handle Table Wrapper
    34 */
    45
     
    3839#include "nathandletable.h"
    3940
    40 PRTHANDLETABLE pNATHandleTable = NULL;
     41RTHANDLETABLE g_hNATHandleTable = NIL_RTHANDLETABLE;
    4142
     43/**
     44 * Returns the windows SOCKET for file descriptor @a fd (aka handle).
     45 *
     46 * @returns Windows socket handle. NULL if @a fd is invalid.
     47 */
    4248SOCKET libslirp_wrap_RTHandleTableLookup(int fd)
    4349{
    44     if (pNATHandleTable == NULL)
     50    RTHANDLETABLE const hHandleTable = g_hNATHandleTable;
     51    SOCKET hSock = (SOCKET)RTHandleTableLookup(hHandleTable, fd);
     52    Log6Func(("Looked up %d in %p and returned %d\n", fd, hHandleTable, hSock));
     53    return hSock;
     54}
     55
     56/**
     57 * Allocates a file descriptor (handle) for windows SOCKET @a hSock.
     58 * @returns IPRT status code.
     59 */
     60int libslirp_wrap_RTHandleTableAlloc(SOCKET hSock, uint32_t *pHandle)
     61{
     62    /* Lazy create the handle table (leaked): */
     63    RTHANDLETABLE hHandleTable = g_hNATHandleTable;
     64    if (RT_LIKELY(hHandleTable != NIL_RTHANDLETABLE))
     65    { /* likely*/ }
     66    else
    4567    {
    46         pNATHandleTable = RTMemAlloc(sizeof(RTHANDLETABLE));
    47         int rc = RTHandleTableCreate(pNATHandleTable);
    48         AssertRC(rc);
     68        int rc = RTHandleTableCreate(&hHandleTable);
     69        AssertLogRelRCReturn(rc, -1);
     70        /** @todo potential race here? iff so, use cmpxchg from asm.h   */
     71        g_hNATHandleTable = hHandleTable;
    4972    }
    5073
    51     SOCKET s = (SOCKET)RTHandleTableLookup(*pNATHandleTable, fd);
    52     Log6Func(("Looked up %d on %p and returned %d\n", fd, (void *)pNATHandleTable, s));
    53     return s;
     74    int rc = RTHandleTableAlloc(hHandleTable, (void *)hSock, pHandle);
     75    Log6Func(("Creating sock %p in %p with handle %d\n", hSock, hHandleTable, *pHandle));
     76    return rc;
    5477}
    5578
    56 int libslirp_wrap_RTHandleTableAlloc(SOCKET uSocket, uint32_t *pHandle)
    57 {
    58     if (pNATHandleTable == NULL)
    59     {
    60         pNATHandleTable = RTMemAlloc(sizeof(RTHANDLETABLE));
    61         int rc = RTHandleTableCreate(pNATHandleTable);
    62         AssertRC(rc);
    63     }
    64 
    65     int ret = RTHandleTableAlloc(*pNATHandleTable, (void *)uSocket, pHandle);
    66     Log6Func(("Creating sock %llu on %p with handle %d\n", uSocket, (void *)pNATHandleTable, *pHandle));
    67     return ret;
    68 }
    69 
     79/**
     80 * Frees file descriptor (handle) @a fd after the associated socket has been
     81 * closed.
     82 *
     83 * @returns IPRT status code. (Shouldn't fail unless there are multiple
     84 *          concurrent closesocket calls.)
     85 */
    7086int libslirp_wrap_RTHandleTableFree(int fd)
    7187{
    72     if (pNATHandleTable == NULL)
    73         return VERR_INVALID_PARAMETER;
     88    RTHANDLETABLE const hHandleTable = g_hNATHandleTable;
     89    AssertReturn(hHandleTable != NIL_RTHANDLETABLE, VERR_INVALID_PARAMETER);
    7490
    75     void *ret = RTHandleTableFree(*pNATHandleTable, fd);
    76     Log6Func(("Freeing sock %d on %p\n", fd, (void *)pNATHandleTable));
    77     if (ret)
     91    void * const pvObsoleteSocket = RTHandleTableFree(hHandleTable, fd);
     92    Log6Func(("Freed handle %d (sock %p) from %p\n", fd, pvObsoleteSocket, hHandleTable));
     93    if (pvObsoleteSocket)
    7894        return VINF_SUCCESS;
    7995
  • trunk/src/libs/libslirp-4.8.0/src/nathandletable.h

    r108816 r108818  
     1/* $Id$ */
    12/** @file
    2  * libslirp: NAT Hanlde Table Singleton Wrapper
     3 * libslirp: NAT Handle Table Singleton Wrapper
    34 */
    45
     
    3435 */
    3536
    36 #ifdef _WIN32
    3737#ifndef INCLUDED_nathandletable_h
     38#define INCLUDED_nathandletable_h
    3839#ifndef RT_WITHOUT_PRAGMA_ONCE
    3940# pragma once
    4041#endif
    4142
    42 #include <iprt/assert.h>
    43 #include <iprt/err.h>
    44 #include <iprt/handletable.h>
    45 #include <iprt/mem.h>
     43#ifdef _WIN32
     44# include <iprt/assert.h>
     45# include <iprt/err.h>
     46# include <iprt/handletable.h>
     47# include <iprt/mem.h>
    4648
    4749# ifndef LOG_GROUP
     
    5052# endif
    5153
    52 #include <winsock2.h>
     54# include <winsock2.h>
    5355
    54 #ifdef __cplusplus
    55 extern "C"
    56 {
    57 #endif
     56RT_C_DECLS_BEGIN
    5857
    59 extern PRTHANDLETABLE pNATHandleTable;
     58extern RTHANDLETABLE g_hNATHandleTable;
    6059
    6160/**
     
    8584int libslirp_wrap_RTHandleTableFree(int fd);
    8685
    87 #ifdef __cplusplus
    88 }
    89 #endif
     86RT_C_DECLS_END
     87
     88# endif /* _WIN32*/
    9089
    9190#endif
    92 #endif
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