白天是个程序员,晚上就是个有抱负的演员,这是就是我,我住在天朝的陪都。

wifidog 优化dns解析 gethostbyname
struct in_addr * wd_gethostbyname(const char *name)
    {
    struct hostent *he = NULL;
        struct in_addr *addr = NULL;
        struct in_addr *in_addr_temp = NULL;
    
        /* XXX Calling function is reponsible for free() */
    
        addr = safe_malloc(sizeof(*addr));
    
        LOCK_GHBN();
    
        he = gethostbyname(name);
    
        if (he == NULL) {
            free(addr);
            UNLOCK_GHBN();
            return NULL;
        }
    
        in_addr_temp = (struct in_addr *)he->h_addr_list[0];
        addr->s_addr = in_addr_temp->s_addr;
    
        UNLOCK_GHBN();
    
        return addr;
    }

在多线程下面,gethostbyname会一个更严重的问题,就是如果有一个线程的gethostbyname发生阻塞,其它线程都会在gethostbyname处发生阻塞。

   struct in_addr * wd_gethostbyname(const char *name)
    {
    int rc;
    char                        dns_buff[8192];
    struct hostent                hostinfo,  *phost;
    struct in_addr *addr = NULL;
    struct in_addr *in_addr_temp = NULL;
    addr = safe_malloc(sizeof(*addr));

    if(0 == gethostbyname_r(name, &hostinfo, dns_buff, sizeof(dns_buff), &phost, &rc)) {
        if(phost) {
            in_addr_temp =  (struct in_addr *)phost->h_addr_list[0];
            addr->s_addr = in_addr_temp->s_addr;
            return addr;
        }
    }
    free(addr); 
    return NULL;
}

添加新评论

咻~