VirtualBox

Changeset 39287 in vbox


Ignore:
Timestamp:
Nov 14, 2011 9:41:52 AM (13 years ago)
Author:
vboxsync
Message:

NAT: "sending to home" on wide cast and cloning udp sockets. Both disabled.

Location:
trunk/src/VBox/Devices/Network/slirp
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Network/slirp/slirp.c

    r39248 r39287  
    5454# include <sys/ioctl.h>
    5555# include <poll.h>
     56# include <netinet/in.h>
    5657#else
    5758# include <Winnls.h>
     
    681682            dns_alias_load(pData);
    682683    }
     684#ifdef VBOX_WITH_NAT_SEND2HOME
     685    /* @todo: we should know all interfaces available on host. */
     686    pData->pInSockAddrHomeAddress = RTMemAllocZ(sizeof(struct sockaddr));
     687    pData->cInHomeAddressSize = 1;
     688    inet_aton("192.168.1.25", &pData->pInSockAddrHomeAddress[0].sin_addr);
     689    pData->pInSockAddrHomeAddress[0].sin_family = AF_INET;
     690#ifdef RT_OS_DARWIN
     691    pData->pInSockAddrHomeAddress[0].sin_len = sizeof(struct sockaddr_in);
     692#endif
     693#endif
    683694    return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
    684695}
  • trunk/src/VBox/Devices/Network/slirp/slirp.h

    r39101 r39287  
    5555#include <iprt/dir.h>
    5656#include <iprt/rand.h>
     57#include <iprt/net.h>
    5758#include <VBox/types.h>
    5859
     
    515516    return slirpServiceMbufAlloc(pData, CTL_DNS);
    516517}
    517 #endif
    518 
     518
     519DECLINLINE(bool) slirpIsWideCasting(PNATState pData, uint32_t u32Addr)
     520{
     521    bool fWideCasting = false;
     522    LogFlowFunc(("Enter: u32Addr:%RTnaipv4\n", u32Addr));
     523    fWideCasting =  (   u32Addr == INADDR_BROADCAST
     524                    || (u32Addr & RT_H2N_U32_C(~pData->netmask)) == RT_H2N_U32_C(~pData->netmask));
     525    LogFlowFunc(("Leave: %RTbool\n", fWideCasting));
     526    return fWideCasting;
     527}
     528#endif
     529
  • trunk/src/VBox/Devices/Network/slirp/slirp_state.h

    r38968 r39287  
    288288    int cRedirectionsStored;
    289289    struct arp_cache_head arp_cache;
    290     /*libalis modules' handlers*/
     290    /* libalis modules' handlers*/
    291291    struct proto_handler *ftp_module;
    292292    struct proto_handler *nbt_module;
    293293    struct proto_handler *dns_module;
    294 
     294#ifdef VBOX_WITH_NAT_SEND2HOME
     295    /* array of home addresses */
     296    struct sockaddr_in *pInSockAddrHomeAddress;
     297    /* size of pInSockAddrHomeAddress in elements */
     298    int cInHomeAddressSize;
     299#endif
    295300} NATState;
    296301
  • trunk/src/VBox/Devices/Network/slirp/socket.c

    r39269 r39287  
    3838#endif
    3939
    40 
     40#ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
     41/**
     42 *
     43 */
     44struct socket * soCloneUDPSocketWithForegnAddr(PNATState pData, const struct socket *pSo, uint32_t u32ForeignAddr)
     45{
     46    struct socket *pNewSocket = NULL;
     47    LogFlowFunc(("Enter: so:%R[natsock], u32ForeignAddr:%RTnaipv4\n", pSo, u32ForeignAddr));
     48    pNewSocket = socreate();
     49    if (!pNewSocket)
     50    {
     51        LogFunc(("Can't create socket\n"));
     52        LogFlowFunc(("Leave: NULL\n"));
     53        return NULL;
     54    }
     55    if (udp_attach(pData, pNewSocket, 0) <= 0)
     56    {
     57        sofree(pData, pNewSocket);
     58        LogFunc(("Can't attach fresh created socket\n"));
     59        return NULL;
     60    }
     61    pNewSocket->so_laddr = pSo->so_laddr;
     62    pNewSocket->so_lport = pSo->so_lport;
     63    pNewSocket->so_faddr.s_addr = u32ForeignAddr;
     64    pNewSocket->so_fport = pSo->so_fport;
     65    return pNewSocket;
     66}
     67#endif
     68
     69#ifdef VBOX_WITH_NAT_SEND2HOME
     70DECLINLINE(bool) slirpSend2Home(PNATState pData, struct socket *pSo, const void *pvBuf, uint32_t cbBuf, int iFlags)
     71{
     72    int idxAddr;
     73    int ret = 0;
     74    bool fSendDone = false;
     75    LogFlowFunc(("Enter pSo:%R[natsock] pvBuf: %p, cbBuf: %d, iFlags: %d\n", pSo, pvBuf, cbBuf, iFlags));
     76    for (idxAddr = 0; idxAddr < pData->cInHomeAddressSize; ++idxAddr)
     77    {
     78
     79        struct socket *pNewSocket = soCloneUDPSocketWithForegnAddr(pData, pSo, pData->pInSockAddrHomeAddress[idxAddr].sin_addr);
     80        AssertReturn((pNewSocket, false));
     81        pData->pInSockAddrHomeAddress[idxAddr].sin_port = pSo->so_fport;
     82        /* @todo: more verbose on errors,
     83         * @note: we shouldn't care if this send fail or not (we're in broadcast).
     84         */
     85        LogFunc(("send %d bytes to %RTnaipv4 from %R[natsock]\n", cbBuf, pData->pInSockAddrHomeAddress[idxAddr].sin_addr.s_addr, pNewSocket));
     86        ret = sendto(pNewSocket->s, pvBuf, cbBuf, iFlags, (struct sockaddr *)&pData->pInSockAddrHomeAddress[idxAddr], sizeof(struct sockaddr_in));
     87        if (ret < 0)
     88            LogFunc(("Failed to send %d bytes to %RTnaipv4\n", cbBuf, pData->pInSockAddrHomeAddress[idxAddr].sin_addr.s_addr));
     89        fSendDone |= ret > 0;
     90    }
     91    LogFlowFunc(("Leave %RTbool\n", fSendDone));
     92    return fSendDone;
     93}
     94#endif /* !VBOX_WITH_NAT_SEND2HOME */
    4195static void send_icmp_to_guest(PNATState, char *, size_t, const struct sockaddr_in *);
    4296#ifdef RT_OS_WINDOWS
     
    9481002    ret = sendto(so->s, buf, mlen, 0,
    9491003                 (struct sockaddr *)&addr, sizeof (struct sockaddr));
     1004#ifdef VBOX_WITH_NAT_SEND2HOME
     1005    if (slirpIsWideCasting(pData, so->so_faddr.s_addr))
     1006    {
     1007        slirpSend2Home(pData, so, buf, mlen, 0);
     1008    }
     1009#endif
    9501010    if (buf)
    9511011        RTMemFree(buf);
  • trunk/src/VBox/Devices/Network/slirp/socket.h

    r28800 r39287  
    192192void sofwdrain (struct socket *);
    193193
     194/**
     195 * Creates copy of UDP socket with specified addr
     196 * @return copy of the socket with f_addr equal to u32ForeignAddr
     197 */
     198#ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
     199struct socket * soCloneUDPSocketWithForegnAddr(PNATState pData, const struct socket *so, uint32_t u32ForeignAddr);
     200#endif
     201
    194202#endif /* _SOCKET_H_ */
  • trunk/src/VBox/Devices/Network/slirp/udp.c

    r39259 r39287  
    245245            goto bad_free_mbuf;
    246246        }
    247         if (udp_attach(pData, so, 0) == -1)
     247        if (udp_attach(pData, so, 0) <= 0)
    248248        {
    249249            Log2(("NAT: IP(id: %hd) udp_attach errno = %d (%s)\n",
     
    402402    {
    403403        saddr.sin_addr.s_addr = so->so_faddr.s_addr;
    404         if ((so->so_faddr.s_addr & RT_H2N_U32(~pData->netmask)) == RT_H2N_U32(~pData->netmask))
     404        if (slirpIsWideCasting(pData, so->so_faddr.s_addr))
    405405        {
    406406            /**
     
    417417            else
    418418                saddr.sin_addr.s_addr = addr->sin_addr.s_addr;
     419            /* we shouldn't override initial socket */
     420#ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
     421            so = soCloneUDPSocketWithForegnAddr(pData, so, addr->sin_addr.s_addr);
     422            Assert((so));
     423#else
    419424            so->so_faddr.s_addr = addr->sin_addr.s_addr;
     425#endif
    420426        }
    421427    }
     
    478484    so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
    479485    so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
     486#if 0
     487    so->so_state = SS_ISFCONNECTED; /* validly opened UDP socked always connected */
     488#endif
    480489    SOCKET_LOCK_CREATE(so);
    481490    QSOCKET_LOCK(udb);
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