|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "cpr_ipc.h" |
|
6 #include "cpr_types.h" |
|
7 |
|
8 #ifndef SIP_OS_WINDOWS |
|
9 #include "netdb.h" |
|
10 #include "arpa/inet.h" |
|
11 #endif |
|
12 |
|
13 #include "dns_utils.h" |
|
14 #include "phone_debug.h" |
|
15 #include "util_string.h" |
|
16 |
|
17 #ifdef SIP_OS_WINDOWS |
|
18 cc_int32_t |
|
19 dnsGetHostByName (const char *hname, |
|
20 cpr_ip_addr_t *ipaddr_ptr, |
|
21 cc_int32_t timeout, |
|
22 cc_int32_t retries) |
|
23 { |
|
24 return DNS_ERR_NOHOST; |
|
25 } |
|
26 #else |
|
27 /* |
|
28 * dnsGetHostByName |
|
29 * |
|
30 * DESCRIPTION: Perform an DNS lookup of the name specified |
|
31 * |
|
32 * RETURNS: returns 0 for success or DNS_ERR_NOHOST |
|
33 * |
|
34 */ |
|
35 cc_int32_t |
|
36 dnsGetHostByName (const char *hname, |
|
37 cpr_ip_addr_t *ipaddr_ptr, |
|
38 cc_int32_t timeout, |
|
39 cc_int32_t retries) |
|
40 { |
|
41 uint32_t ip_address; |
|
42 struct hostent *dns_response; |
|
43 #ifdef IPV6_STACK_ENABLED |
|
44 uint32_t err; |
|
45 uint16_t ip_valid; |
|
46 char ip_addr[MAX_IPADDR_STR_LEN]; |
|
47 struct addrinfo hints, *result; |
|
48 struct sockaddr sa; |
|
49 #endif |
|
50 |
|
51 #ifdef IPV6_STACK_ENABLED |
|
52 /* Check if the IPv6 address */ |
|
53 ip_valid = cpr_inet_pton(AF_INET6, hname, ip_addr); |
|
54 |
|
55 if (ip_valid) { |
|
56 ipaddr_ptr->type = CPR_IP_ADDR_IPV6; |
|
57 cpr_memcopy(ipaddr_ptr->u.ip6.addr.base8, ip_addr, 16); |
|
58 return(0); |
|
59 } |
|
60 #endif |
|
61 |
|
62 ip_address = inet_addr(hname); |
|
63 if (ip_address != INADDR_NONE) { |
|
64 ipaddr_ptr->u.ip4 = ip_address; |
|
65 ipaddr_ptr->type = CPR_IP_ADDR_IPV4; |
|
66 return 0; |
|
67 } else { |
|
68 |
|
69 #ifdef IPV6_STACK_ENABLED |
|
70 memset(&hints, 0, sizeof(hints)); |
|
71 hints.ai_family = PF_INET6; |
|
72 err = getaddrinfo(hname, NULL, &hints, &result); |
|
73 |
|
74 if (err) { |
|
75 return DNS_ERR_NOHOST; |
|
76 |
|
77 } |
|
78 |
|
79 while (result) { |
|
80 sa = result->ai_addr; |
|
81 if (sa->family == AF_INET) { |
|
82 ipaddr_ptr->type = CPR_IP_ADDR_IPV4; |
|
83 ip_addr->u.ip4 = ((cpr_sockaddr_in_t *)from)->sin_addr.s_addr; |
|
84 |
|
85 } else if (sa->family == AF_INET6) { |
|
86 |
|
87 //Todo IPv6: Add getaddrinfo() functioncall to get the IPv6 address. |
|
88 ipaddr_ptr->type = CPR_IP_ADDR_IPV6; |
|
89 ip_addr->u.ip6 = ((cpr_sockaddr_in6_t *)sa)->sin6_addr; |
|
90 } |
|
91 } |
|
92 |
|
93 freeaddrinfo(result); |
|
94 #endif |
|
95 |
|
96 dns_response = gethostbyname(hname); |
|
97 if (dns_response) { |
|
98 ipaddr_ptr->u.ip4 = *(uint32_t *) dns_response->h_addr_list[0]; |
|
99 ipaddr_ptr->type = CPR_IP_ADDR_IPV4; |
|
100 return 0; |
|
101 } |
|
102 |
|
103 |
|
104 } |
|
105 return DNS_ERR_NOHOST; |
|
106 } |
|
107 |
|
108 #endif |
|
109 |
|
110 #ifdef SIP_OS_WINDOWS |
|
111 |
|
112 cc_int32_t |
|
113 dnsGetHostBySRV (cc_int8_t *service, |
|
114 cc_int8_t *protocol, |
|
115 cc_int8_t *domain, |
|
116 cpr_ip_addr_t *ipaddr_ptr, |
|
117 cc_uint16_t *port, |
|
118 cc_int32_t timeout, |
|
119 cc_int32_t retries, |
|
120 srv_handle_t *psrv_handle) |
|
121 { |
|
122 return DNS_ERR_NOHOST; |
|
123 } |
|
124 |
|
125 #else |
|
126 /* |
|
127 * dnsGetHostBySRV |
|
128 * |
|
129 * DESCRIPTION: This function calls the dns api to |
|
130 * perform a dns srv query |
|
131 * |
|
132 * RETURNS: returns 0 for success or |
|
133 * DNS_ERR_NOHOST, DNS_ERR_HOST_UNAVAIL |
|
134 * |
|
135 */ |
|
136 cc_int32_t |
|
137 dnsGetHostBySRV (cc_int8_t *service, |
|
138 cc_int8_t *protocol, |
|
139 cc_int8_t *domain, |
|
140 cpr_ip_addr_t *ipaddr_ptr, |
|
141 cc_uint16_t *port, |
|
142 cc_int32_t timeout, |
|
143 cc_int32_t retries, |
|
144 srv_handle_t *psrv_handle) |
|
145 { |
|
146 uint32_t ip_address; |
|
147 |
|
148 |
|
149 ip_address = inet_addr(domain); |
|
150 if (ip_address != INADDR_NONE) { |
|
151 ipaddr_ptr->u.ip4 = ip_address; |
|
152 ipaddr_ptr->type = CPR_IP_ADDR_IPV4; |
|
153 return 0; |
|
154 } |
|
155 |
|
156 return DNS_ERR_NOBUF; /* not yet implemented */ |
|
157 } |
|
158 #endif |
|
159 |
|
160 void |
|
161 dnsFreeSrvHandle (srv_handle_t srv_handle) |
|
162 { |
|
163 // this function does nothing |
|
164 // srv_handle = NULL; |
|
165 } |