michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "cpr_ipc.h" michael@0: #include "cpr_types.h" michael@0: michael@0: #ifndef SIP_OS_WINDOWS michael@0: #include "netdb.h" michael@0: #include "arpa/inet.h" michael@0: #endif michael@0: michael@0: #include "dns_utils.h" michael@0: #include "phone_debug.h" michael@0: #include "util_string.h" michael@0: michael@0: #ifdef SIP_OS_WINDOWS michael@0: cc_int32_t michael@0: dnsGetHostByName (const char *hname, michael@0: cpr_ip_addr_t *ipaddr_ptr, michael@0: cc_int32_t timeout, michael@0: cc_int32_t retries) michael@0: { michael@0: return DNS_ERR_NOHOST; michael@0: } michael@0: #else michael@0: /* michael@0: * dnsGetHostByName michael@0: * michael@0: * DESCRIPTION: Perform an DNS lookup of the name specified michael@0: * michael@0: * RETURNS: returns 0 for success or DNS_ERR_NOHOST michael@0: * michael@0: */ michael@0: cc_int32_t michael@0: dnsGetHostByName (const char *hname, michael@0: cpr_ip_addr_t *ipaddr_ptr, michael@0: cc_int32_t timeout, michael@0: cc_int32_t retries) michael@0: { michael@0: uint32_t ip_address; michael@0: struct hostent *dns_response; michael@0: #ifdef IPV6_STACK_ENABLED michael@0: uint32_t err; michael@0: uint16_t ip_valid; michael@0: char ip_addr[MAX_IPADDR_STR_LEN]; michael@0: struct addrinfo hints, *result; michael@0: struct sockaddr sa; michael@0: #endif michael@0: michael@0: #ifdef IPV6_STACK_ENABLED michael@0: /* Check if the IPv6 address */ michael@0: ip_valid = cpr_inet_pton(AF_INET6, hname, ip_addr); michael@0: michael@0: if (ip_valid) { michael@0: ipaddr_ptr->type = CPR_IP_ADDR_IPV6; michael@0: cpr_memcopy(ipaddr_ptr->u.ip6.addr.base8, ip_addr, 16); michael@0: return(0); michael@0: } michael@0: #endif michael@0: michael@0: ip_address = inet_addr(hname); michael@0: if (ip_address != INADDR_NONE) { michael@0: ipaddr_ptr->u.ip4 = ip_address; michael@0: ipaddr_ptr->type = CPR_IP_ADDR_IPV4; michael@0: return 0; michael@0: } else { michael@0: michael@0: #ifdef IPV6_STACK_ENABLED michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_INET6; michael@0: err = getaddrinfo(hname, NULL, &hints, &result); michael@0: michael@0: if (err) { michael@0: return DNS_ERR_NOHOST; michael@0: michael@0: } michael@0: michael@0: while (result) { michael@0: sa = result->ai_addr; michael@0: if (sa->family == AF_INET) { michael@0: ipaddr_ptr->type = CPR_IP_ADDR_IPV4; michael@0: ip_addr->u.ip4 = ((cpr_sockaddr_in_t *)from)->sin_addr.s_addr; michael@0: michael@0: } else if (sa->family == AF_INET6) { michael@0: michael@0: //Todo IPv6: Add getaddrinfo() functioncall to get the IPv6 address. michael@0: ipaddr_ptr->type = CPR_IP_ADDR_IPV6; michael@0: ip_addr->u.ip6 = ((cpr_sockaddr_in6_t *)sa)->sin6_addr; michael@0: } michael@0: } michael@0: michael@0: freeaddrinfo(result); michael@0: #endif michael@0: michael@0: dns_response = gethostbyname(hname); michael@0: if (dns_response) { michael@0: ipaddr_ptr->u.ip4 = *(uint32_t *) dns_response->h_addr_list[0]; michael@0: ipaddr_ptr->type = CPR_IP_ADDR_IPV4; michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: } michael@0: return DNS_ERR_NOHOST; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #ifdef SIP_OS_WINDOWS michael@0: michael@0: cc_int32_t michael@0: dnsGetHostBySRV (cc_int8_t *service, michael@0: cc_int8_t *protocol, michael@0: cc_int8_t *domain, michael@0: cpr_ip_addr_t *ipaddr_ptr, michael@0: cc_uint16_t *port, michael@0: cc_int32_t timeout, michael@0: cc_int32_t retries, michael@0: srv_handle_t *psrv_handle) michael@0: { michael@0: return DNS_ERR_NOHOST; michael@0: } michael@0: michael@0: #else michael@0: /* michael@0: * dnsGetHostBySRV michael@0: * michael@0: * DESCRIPTION: This function calls the dns api to michael@0: * perform a dns srv query michael@0: * michael@0: * RETURNS: returns 0 for success or michael@0: * DNS_ERR_NOHOST, DNS_ERR_HOST_UNAVAIL michael@0: * michael@0: */ michael@0: cc_int32_t michael@0: dnsGetHostBySRV (cc_int8_t *service, michael@0: cc_int8_t *protocol, michael@0: cc_int8_t *domain, michael@0: cpr_ip_addr_t *ipaddr_ptr, michael@0: cc_uint16_t *port, michael@0: cc_int32_t timeout, michael@0: cc_int32_t retries, michael@0: srv_handle_t *psrv_handle) michael@0: { michael@0: uint32_t ip_address; michael@0: michael@0: michael@0: ip_address = inet_addr(domain); michael@0: if (ip_address != INADDR_NONE) { michael@0: ipaddr_ptr->u.ip4 = ip_address; michael@0: ipaddr_ptr->type = CPR_IP_ADDR_IPV4; michael@0: return 0; michael@0: } michael@0: michael@0: return DNS_ERR_NOBUF; /* not yet implemented */ michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: dnsFreeSrvHandle (srv_handle_t srv_handle) michael@0: { michael@0: // this function does nothing michael@0: // srv_handle = NULL; michael@0: }