⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dns_client_lib6.c

📁 DNS 的实现代码
💻 C
📖 第 1 页 / 共 2 页
字号:
        } /* switch (family) */    } /* non-blocking call */    /* invalid family supplied */    *p_err = EINVAL;    DNSC_DEBUG("exit - EINVAL\n");    return NULL;} /* dns_getipnodebyaddr *//* dns_client_get_nameserver6 -- *     This routine retrieves an IP nameserver from the existing list. *     It is an API analog to the 'show' console command. * *     Note, this routine will only return an IPv6 nameserver. * * PARAMETERS *     index     - index of nameserver to find *     nsAddrStr - storage for results * * RETURNS: *     ESUCCESS - (nameserver in string buffer), or *     EINVAL   - (nameserver not found) */intdns_client_get_nameserver6(IN int index, OUT char *nsAddrStr){    ATMOS_MQID     qid;    ATMOS_MESSAGE  local_msg;    int            rc;    MSG_D_DNS_GET_NAMESERVER6(p_dns_msg, &local_msg);    if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0)    {        DNSC_TRACE("could not acquire the %s queue handle\n",                   DNSCLIENT_QUEUE_NAME);        return ESRCH;    }    p_dns_msg->error = ESUCCESS;    p_dns_msg->index = index;    p_dns_msg->nsaddr_str = (char *)nsAddrStr;    rc = sendmessage(&local_msg, MSG_N_DNS_GET_NAMESERVER6, qid);    if (rc != ESUCCESS)    {        DNSC_TRACE("error attempting to send get nameserver message\n");        return rc;    }    awaitspecificmessage(&local_msg);    return p_dns_msg->error;} /* dns_client_get_nameserver6 *//* dns_client_flush_cachebyname6 -- *     This routine flushes dns cache IPv6 entry given host name. * * PARAMETERS *     hostName - hostname of entry to be flushed * * RETURNS *     ESUCCESS - on success *     EFAULT   - 'addr' points outside the user's accessible space *     ESRCH    - couldn't obtain dns client queue handle */intdns_client_flush_cachebyname6(IN const char *hostName){    ATMOS_MQID     qid;    ATMOS_MESSAGE  local_msg;    int            rc;    MSG_D_DNS_FLUSH_CACHE_NAME6(p_dns_msg, &local_msg);    if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0)    {        DNSC_TRACE("could not acquire the %s queue handle\n",                   DNSCLIENT_QUEUE_NAME);        return ESRCH;    }    p_dns_msg->error = ESUCCESS;    p_dns_msg->hostname = (char *)hostName;    rc = sendmessage(&local_msg, MSG_N_DNS_FLUSH_CACHE_NAME6, qid);    if (rc != ESUCCESS)    {        DNSC_TRACE("error attempting to send flush cache message\n");        return rc;    }    awaitspecificmessage(&local_msg);    return p_dns_msg->error;} /* dns_client_flush_cachebyname6 *//* dns_client_flush_cachebyaddr -- *     This routine also works as the flush cache routine but allows *     a user to specify a cache entry by one of its IPv4 or IPv6 *     addresses. * * PARAMETERS *     addr  -  IPv4 or IPv6 address of entry to flush * * RETURNS *     ESUCCESS - on success *     EINVAL   - unknown address family *     EFAULT   - 'addr' points outside the user's accessible space *     ESRCH    - couldn't obtain dns client queue handle */intdns_client_flush_cachebyaddr(IN struct sockaddr *addr){    switch (addr->sa_family)    {        case AF_INET:            return dns_client_flush_cachebyip(                ((struct sockaddr_in *)addr)->sin_addr.s_addr);        case AF_INET6:        {            ATMOS_MQID     qid;            ATMOS_MESSAGE  local_msg;            int            rc;            MSG_D_DNS_FLUSH_CACHE_IP6(p_dns_msg, &local_msg);            if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) {                DNSC_TRACE("could not acquire the %s queue handle\n",                           DNSCLIENT_QUEUE_NAME);                return ESRCH;            }            p_dns_msg->error = ESUCCESS;            p_dns_msg->ip6addr = &((struct sockaddr_in6 *)addr)->sin6_addr;            rc = sendmessage(&local_msg, MSG_N_DNS_FLUSH_CACHE_IP6, qid);            if (rc != ESUCCESS)            {                DNSC_TRACE("error attempting to send flush cache message\n");                return rc;            }            awaitspecificmessage(&local_msg);            return p_dns_msg->error;        }        default:            return EINVAL;    } /* switch (addr->sa_family) */} /* dns_client_flush_cachebyaddr *//* dns_alloc_struct_hostent -- *     This routine dynamically allocates memory for struct hostent. * * PARAMETERS *     N/A * * RETURNS *     pointer to allocated area or NULL on failure */struct hostent *dns_alloc_struct_hostent(void){    struct hostent *ptr = malloc(sizeof(struct hostent));    if (ptr != NULL)    {        memset(ptr, 0, sizeof(struct hostent));    }    return ptr;} /* dns_alloc_struct_hostent *//* dns_alloc_hostname -- *     This routine dynamically allocates memory for 'h_name' field *     of hostent structure. * * PARAMETERS: *     p_hent   - pointer to hostent structure *     hostName - hostname to install * * RETURNS: *     pointer to allocated area or NULL on failure * * NOTE: *     In case of failure, this function RELEASES hostent structure. *     Caller then has to set pointer to this structure to NULL. */const char *dns_alloc_hostname(struct hostent *p_hent, const char *hostName){    char  *tmp;    if ((p_hent->h_name = malloc(strlen(hostName) + 1)) == NULL)    {        dns_freehostent(p_hent);        return NULL;    }    tmp = (char *)(p_hent->h_name);    strcpy(tmp, hostName);    return p_hent->h_name;} /* dns_alloc_hostname *//* dns_alloc_aliases -- *     This routine dynamically allocates memory for 'h_aliases' field *     of hostent structure and fills the array with NULLs. * *     Note, however, that memory for aliases themselves is not allocated *     (since we can not know in advance lengthes of all aliases). * * PARAMETERS: *     p_hent      - hostent structure where to allocate aliases *     num_aliases - number of aliases to allocate * * RETURNS: *     pointer to the allocated area or NULL on failure * * NOTE: *     In case of failure, this function RELEASES hostent structure. *     Caller then has to set pointer to this structure to NULL. */char **dns_alloc_aliases(IN OUT struct hostent *p_hent, IN int num_aliases){    p_hent->h_aliases = malloc((sizeof(char *) * (num_aliases + 1)));    if (p_hent->h_aliases == NULL)    {        dns_freehostent(p_hent);        return NULL;    }    memset(p_hent->h_aliases, 0, sizeof(char *) * (num_aliases + 1));    return p_hent->h_aliases;} /* dns_alloc_aliases *//* dns_alloc_addrlist -- *     This routine dynamically allocates memory for 'h_addr_list' *     field of hostent structure and for 'num_addr' addresses of *     type 'type' in it. * * PARAMETERS: *     p_hent   - hostent structure in which to allocate address list *     num_addr - number of addresses to allocate *     family   - family of addresses * * RETURNS: *     pointer to the allocated area or NULL on failure * * NOTE: *     In case of failure, this function RELEASES hostent structure. *     Caller then has to set pointer to this structure to NULL. */char **dns_alloc_addrlist(IN OUT struct hostent *p_hent, IN int num_addr,                   IN int family){    int    size, i;    switch (family)    {        case AF_INET:            size = sizeof(struct in_addr);            break;        case AF_INET6:            size = sizeof(struct in6_addr);            break;        default:            dns_freehostent(p_hent);            return NULL;    }    /* Allocate array for pointers to addresses */    /*     * It is necessary to allocate additional element which must be     * equal to 0 in order to mark end of the list     */    p_hent->h_addr_list = malloc((num_addr + 1) * sizeof(char *));    if (p_hent->h_addr_list == NULL)    {        dns_freehostent(p_hent);        return NULL;    }    /*     * Preset allocated array by zeros in order to dns_freehostent     * works correctly in a case of failure     */    memset(p_hent->h_addr_list, 0, (num_addr + 1) * sizeof(char *));    /*     * Now allocate memory for each addresses separately, since it     * may be freed one by one     */    for (i = 0; i < num_addr; i++)    {        if ((p_hent->h_addr_list[i] = malloc(size)) == NULL)        {            dns_freehostent(p_hent);            return NULL;        }        memset(p_hent->h_addr_list[i], 0, size);    }    return p_hent->h_addr_list;} /* dns_alloc_addrlist *//* dns_dns_hostent_to_hostent -- *     Converts 'dns_hostent' structure to 'hostent' structure. *     A memory for the last is allocated. * * PARAMETERS: *     p_dns_hent - pointer to 'dns_hostent' structure * * RETURNS: *     hostent structure - result of conversion or NULL if some *     failure occured */struct hostent *dns_dns_hostent_to_hostent(IN struct dns_hostent *p_dns_hent){    struct hostent *p_hent;    int             i;    DNSC_DEBUG("entry\n");    if ((p_hent = dns_alloc_struct_hostent()) == NULL)    {        DNSC_DEBUG("exit - hostent allocation failure\n");        return NULL;    }    DNSC_DEBUG("p_hent = 0x%08x\n", (u_long)p_hent);    /* Copy h_name */    if (dns_alloc_hostname(p_hent, p_dns_hent->h_name) == NULL)    {        DNSC_DEBUG("exit - hostname allocation failure\n");        return NULL;    }    DNSC_DEBUG("h_name = %s\n", p_hent->h_name);    DNSC_DEBUG("h_alias length = %d\n", strlen(p_dns_hent->h_alias));    if (strlen(p_dns_hent->h_alias) != 0)    {        /* Copy aliases; actually only one alias */        if (dns_alloc_aliases(p_hent, 1) == NULL)        {            DNSC_DEBUG("exit - aliases allocation failure\n");            return NULL;        }        /* We have only one alias */        ASSERT(strlen(p_dns_hent->h_alias) <= NAMEMAX);        DNSC_DEBUG("h_alias = %s\n", p_dns_hent->h_alias);        p_hent->h_aliases[0] = calloc(strlen(p_dns_hent->h_alias) + 1, 1);        if (p_hent->h_aliases[0] == NULL)        {            /*             * Here we must free 'hent' because here we used no routine             * like 'dns_alloc_hostname' which would destroy it on failure             */            dns_freehostent(p_hent);            DNSC_DEBUG("exit - alias[0] allocation failure\n");            return NULL;        }        strcpy(p_hent->h_aliases[0], p_dns_hent->h_alias);        /*p_hent->h_aliases[0][NAMEMAX + 1] = '\0';*/        /* the NULL trailer is set by memset */        DNSC_DEBUG("h_aliases[0] = %s\n", p_hent->h_aliases[0]);    }    /* Address type */    p_hent->h_addrtype = AF_INET;    DNSC_DEBUG("h_addrtype = %d\n", p_hent->h_addrtype);    p_hent->h_length = sizeof(struct in_addr);    DNSC_DEBUG("h_length = %d\n", p_hent->h_length);    /* Allocate place for addresses */    DNSC_DEBUG("number of addresses = %d\n", p_dns_hent->h_numaddrs);    if (dns_alloc_addrlist(p_hent, p_dns_hent->h_numaddrs, AF_INET)            == NULL)    {        DNSC_DEBUG("exit - addrlist allocation failure\n");        return NULL;    }    for (i = 0; i < p_dns_hent->h_numaddrs; i++)    {        DNSC_DEBUG("copy address #%d\n", i);        memcpy(p_hent->h_addr_list[i], &p_dns_hent->h_addr_list[i],               sizeof(struct in_addr));    }    free(p_dns_hent->h_addr_list);    DNSC_DEBUG("exit - success\n");    return p_hent;} /* dns_dns_hostent_to_hostent */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -