VirtualBox

Ignore:
Timestamp:
Dec 8, 2015 9:39:32 PM (9 years ago)
Author:
vboxsync
Message:

NAT: Move host resolver out of libalias. This is a minimal change
that only adapts existing code to its new call site, though I have
g/c'ed some of the bogus bits of doanswer().

Location:
trunk/src/VBox/Devices/Network/slirp
Files:
5 edited
1 moved

Legend:

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

    r59028 r59063  
    11/* $Id$ */
    22/** @file
    3  * libalias helper for using the host resolver instead of dnsproxy.
     3 * Host resolver
    44 */
    55
     
    2222#include <iprt/assert.h>
    2323#include <slirp.h>
    24 #include "alias.h"
    25 #include "alias_local.h"
    26 #include "alias_mod.h"
     24
    2725#define isdigit(ch)    RT_C_IS_DIGIT(ch)
    2826#define isalpha(ch)    RT_C_IS_ALPHA(ch)
     
    6866
    6967/* see RFC 1035(4.1) */
    70 static int  dns_alias_handler(PNATState pData, int type);
    7168static void CStr2QStr(const char *pcszStr, char *pszQStr, size_t cQStr);
    7269static void QStr2CStr(const char *pcszQStr, char *pszStr, size_t cStr);
     
    7572#endif
    7673
    77 static int
    78 fingerprint(struct libalias *la, struct ip *pIp, struct alias_data *ah)
    79 {
    80 
    81     NOREF(la);
    82     NOREF(pIp);
    83     if (!ah->dport || !ah->sport || !ah->lnk)
    84         return -1;
    85 
    86     Log(("NAT:%s: ah(dport: %hd, sport: %hd) oaddr:%RTnaipv4 aaddr:%RTnaipv4\n",
    87         RT_GCC_EXTENSION __FUNCTION__, ntohs(*ah->dport), ntohs(*ah->sport),
    88         ah->oaddr, ah->aaddr));
    89 
    90     if (   (ntohs(*ah->dport) == DNS_CONTROL_PORT_NUMBER
    91         || ntohs(*ah->sport) == DNS_CONTROL_PORT_NUMBER)
    92         && (ah->oaddr->s_addr == htonl(ntohl(la->pData->special_addr.s_addr)|CTL_DNS)))
    93         return 0;
    94 
    95     return -1;
    96 }
    97 
    98 static void doanswer(union dnsmsg_header *pHdr, struct dns_meta_data *pReqMeta, char *pszQname, struct ip *pIp, struct hostent *pHostent)
    99 {
     74
     75static void
     76doanswer(struct mbuf *m, struct hostent *pHostent)
     77{
     78    union dnsmsg_header *pHdr;
    10079    int i;
     80
     81    pHdr = mtod(m, union dnsmsg_header *);
    10182
    10283    if (!pHostent)
     
    10990    else
    11091    {
    111         char *query;
    11292        char *answers;
    11393        uint16_t off;
    11494        char **cstr;
    11595        char *c;
    116         uint16_t packet_len = 0;
     96        size_t anslen = 0;
    11797        uint16_t addr_off = (uint16_t)~0;
    11898        struct dns_meta_data *meta;
    11999
    120 #if 0
    121         /* here is no compressed names+answers + new query */
    122         m_inc(m, pHostent->h_length * sizeof(struct dnsmsg_answer) + strlen(pszQname) + 2 * sizeof(uint16_t));
    123 #endif
    124         packet_len = (pIp->ip_hl << 2)
    125                    + sizeof(struct udphdr)
    126                    + sizeof(union dnsmsg_header)
    127                    + strlen(pszQname)
    128                    + sizeof(struct dns_meta_data); /* ip + udp + header + query */
    129         query = (char *)&pHdr[1];
    130 
    131         strcpy(query, pszQname);
    132         query += strlen(pszQname) + 1;
    133         /* class & type informations lay right after symbolic inforamtion. */
    134         meta = (struct dns_meta_data *)query;
    135         meta->type = pReqMeta->type;
    136         meta->class = pReqMeta->class;
    137 
    138100        /* answers zone lays after query in response packet */
    139         answers = (char *)&meta[1];
     101        answers = (char *)pHdr + m->m_len;
    140102
    141103        off = (char *)&pHdr[1] - (char *)pHdr;
     
    161123                addr_off = off;
    162124            answers = (char *)&ans[1] + len - 2;  /* note: 1 symbol already counted */
    163             packet_len += sizeof(struct dnsmsg_answer) + len - 2;
     125            anslen += sizeof(struct dnsmsg_answer) + len - 2;
    164126            pHdr->X.ancount++;
    165127        }
     128
    166129        /* add addresses */
    167 
    168130        for(i = 0; i < pHostent->h_length && pHostent->h_addr_list[i] != NULL; ++i)
    169131        {
     
    177139            *(uint32_t *)ans->rdata = *(uint32_t *)pHostent->h_addr_list[i];
    178140            answers = (char *)&ans[1] + 2;
    179             packet_len += sizeof(struct dnsmsg_answer) + 3;
     141            anslen += sizeof(struct dnsmsg_answer) + 3;
    180142            pHdr->X.ancount++;
    181143        }
     
    186148        pHdr->X.rcode = 0;
    187149        HTONS(pHdr->X.ancount);
    188         /* don't forget update m_len */
    189         pIp->ip_len = htons(packet_len);
    190     }
    191 }
    192 
    193 static int
    194 protohandler(struct libalias *la, struct ip *pIp, struct alias_data *ah)
     150
     151        m->m_len += anslen;
     152    }
     153}
     154
     155int
     156hostresolver(PNATState pData, struct mbuf *m)
    195157{
    196158    int i;
     
    202164    struct dns_meta_data *meta;
    203165
    204     struct udphdr *udp = NULL;
    205166    union dnsmsg_header *pHdr = NULL;
    206     NOREF(la);
    207     NOREF(ah);
    208     udp = (struct udphdr *)ip_next(pIp);
    209     pHdr = (union dnsmsg_header *)udp_next(udp);
     167
     168    pHdr = mtod(m, union dnsmsg_header *);
    210169
    211170    if (pHdr->X.qr == 1)
    212         return 0; /* this is respose */
     171        return 1; /* this is respose */
    213172
    214173    memset(pszCname, 0, sizeof(pszCname));
    215174    qw_qname = (char *)&pHdr[1];
    216     Assert((ntohs(pHdr->X.qdcount) == 1));
     175
    217176    if ((ntohs(pHdr->X.qdcount) != 1))
    218177    {
     
    226185    }
    227186
    228     for (i = 0; i < ntohs(pHdr->X.qdcount); ++i)
    229187    {
    230188        meta = (struct dns_meta_data *)(qw_qname + strlen(qw_qname) + 1);
     
    248206#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
    249207        if (   pHostent
    250             && !LIST_EMPTY(&la->pData->DNSMapHead))
    251             alterHostentWithDataFromDNSMap(la->pData, pHostent);
     208            && !LIST_EMPTY(&pData->DNSMapHead))
     209            alterHostentWithDataFromDNSMap(pData, pHostent);
    252210#endif
    253         doanswer(pHdr, meta, qw_qname, pIp, pHostent);
    254     }
    255 
    256     /*
    257      * We have changed the size and the content of udp, to avoid double csum calculation
    258      * will assign to zero
    259      */
    260     udp->uh_sum = 0;
    261     udp->uh_ulen = ntohs(htons(pIp->ip_len) - (pIp->ip_hl << 2));
    262     pIp->ip_sum = 0;
    263     pIp->ip_sum = LibAliasInternetChecksum(la, (uint16_t *)pIp, pIp->ip_hl << 2);
     211        doanswer(m, pHostent);
     212    }
     213
    264214    return 0;
    265215}
     
    324274    }
    325275    *q = '\0';
    326 }
    327 
    328 
    329 int
    330 dns_alias_load(PNATState pData)
    331 {
    332     return dns_alias_handler(pData, MOD_LOAD);
    333 }
    334 
    335 int
    336 dns_alias_unload(PNATState pData)
    337 {
    338     return dns_alias_handler(pData, MOD_UNLOAD);
    339 }
    340 
    341 #define handlers pData->dns_module
    342 static int
    343 dns_alias_handler(PNATState pData, int type)
    344 {
    345     int error;
    346 
    347     if (!handlers)
    348         handlers = RTMemAllocZ(2 * sizeof(struct proto_handler));
    349 
    350     handlers[0].pri = 20;
    351     handlers[0].dir = IN;
    352     handlers[0].proto = UDP;
    353     handlers[0].fingerprint = &fingerprint;
    354     handlers[0].protohandler = &protohandler;
    355     handlers[1].pri = EOH;
    356 
    357     switch (type)
    358     {
    359         case MOD_LOAD:
    360             error = 0;
    361             LibAliasAttachHandlers(pData, handlers);
    362             break;
    363 
    364         case MOD_UNLOAD:
    365             error = 0;
    366             LibAliasDetachHandlers(pData, handlers);
    367             RTMemFree(handlers);
    368             handlers = NULL;
    369             break;
    370 
    371         default:
    372             error = EINVAL;
    373     }
    374     return error;
    375276}
    376277
  • trunk/src/VBox/Devices/Network/slirp/ip_input.c

    r56960 r59063  
    114114        else
    115115            m->m_flags &= ~M_SKIP_FIREWALL;
     116
     117        /*
     118         * XXX: TODO: this is most likely a leftover spooky action at
     119         * a distance from alias_dns.c host resolver code and can be
     120         * g/c'ed.
     121         */
    116122        if (m->m_len != RT_N2H_U16(ip->ip_len))
    117123            m->m_len = RT_N2H_U16(ip->ip_len);
  • trunk/src/VBox/Devices/Network/slirp/slirp.c

    r58077 r59063  
    407407        ftp_alias_load(pData);
    408408        nbt_alias_load(pData);
    409         if (pData->fUseHostResolver)
    410             dns_alias_load(pData);
    411409    }
    412410#ifdef VBOX_WITH_NAT_SEND2HOME
     
    523521    if (pData->fUseHostResolver)
    524522    {
    525         dns_alias_unload(pData);
    526523#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
    527524        while (!LIST_EMPTY(&pData->DNSMapHead))
  • trunk/src/VBox/Devices/Network/slirp/slirp.h

    r56292 r59063  
    351351struct tcpcb *tcp_drop(PNATState, struct tcpcb *tp, int err);
    352352
     353/* hostres.c */
     354int hostresolver(PNATState, struct mbuf *);
     355
    353356/*slirp.c*/
    354357void slirp_arp_who_has(PNATState pData, uint32_t dst);
     
    465468int nbt_alias_load(PNATState);
    466469int nbt_alias_unload(PNATState);
    467 int dns_alias_load(PNATState);
    468 int dns_alias_unload(PNATState);
    469470int slirp_arp_lookup_ip_by_ether(PNATState, const uint8_t *, uint32_t *);
    470471int slirp_arp_lookup_ether_by_ip(PNATState, uint32_t, uint8_t *);
  • trunk/src/VBox/Devices/Network/slirp/slirp_dns.c

    r56292 r59063  
    246246        if (get_dns_addr_domain(pData, NULL) < 0)
    247247        {
    248             /* Load the DNS handler if host resolver mode was not used before. */
    249             if (!pData->fUseHostResolver)
    250                 dns_alias_load(pData);
    251248            pData->fUseHostResolver = true;
    252249        }
    253250        else
    254251        {
    255             /* Unload to not intercept in the future. */
    256             if (pData->fUseHostResolver)
    257                 dns_alias_unload(pData);
    258252            pData->fUseHostResolver = false;
    259253            dnsproxy_init(pData);
  • trunk/src/VBox/Devices/Network/slirp/udp.c

    r59034 r59063  
    183183             ip->ip_dst.s_addr, RT_N2H_U16(uh->uh_dport)));
    184184
     185    /*
     186     * handle DNS host resolver without creating a socket
     187     */
    185188    if (   pData->fUseHostResolver
    186189        && uh->uh_dport == RT_H2N_U16_C(53)
     
    188191    {
    189192        struct sockaddr_in dst, src;
     193        int error;
     194
    190195        src.sin_addr.s_addr = ip->ip_dst.s_addr;
    191196        src.sin_port = uh->uh_dport;
     
    193198        dst.sin_port = uh->uh_sport;
    194199
     200        m_adj(m, sizeof(struct udpiphdr));
     201
     202        error = hostresolver(pData, m);
     203        if (error)
     204            goto done_free_mbuf;
     205
    195206        slirpMbufTagService(pData, m, CTL_DNS);
    196         /* udp_output2() expects a pointer to the body of UDP packet. */
    197         m->m_data += sizeof(struct udpiphdr);
    198         m->m_len -= sizeof(struct udpiphdr);
     207
    199208        udp_output2(pData, NULL, m, &src, &dst, IPTOS_LOWDELAY);
    200209        LogFlowFuncLeave();
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