2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2008-2013 by Daniel Stenberg
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
19 #include "ares_setup.h"
21 #ifdef HAVE_ARPA_INET_H
22 # include <arpa/inet.h>
26 #include "ares_data.h"
27 #include "ares_inet_net_pton.h"
28 #include "ares_private.h"
31 int ares_get_servers(ares_channel channel,
32 struct ares_addr_node **servers)
34 struct ares_addr_node *srvr_head = NULL;
35 struct ares_addr_node *srvr_last = NULL;
36 struct ares_addr_node *srvr_curr;
37 int status = ARES_SUCCESS;
43 for (i = 0; i < channel->nservers; i++)
45 /* Allocate storage for this server node appending it to the list */
46 srvr_curr = ares_malloc_data(ARES_DATATYPE_ADDR_NODE);
54 srvr_last->next = srvr_curr;
58 srvr_head = srvr_curr;
60 srvr_last = srvr_curr;
62 /* Fill this server node data */
63 srvr_curr->family = channel->servers[i].addr.family;
64 if (srvr_curr->family == AF_INET)
65 memcpy(&srvr_curr->addrV4, &channel->servers[i].addr.addrV4,
66 sizeof(srvr_curr->addrV4));
68 memcpy(&srvr_curr->addrV6, &channel->servers[i].addr.addrV6,
69 sizeof(srvr_curr->addrV6));
72 if (status != ARES_SUCCESS)
76 ares_free_data(srvr_head);
86 int ares_get_servers_ports(ares_channel channel,
87 struct ares_addr_port_node **servers)
89 struct ares_addr_port_node *srvr_head = NULL;
90 struct ares_addr_port_node *srvr_last = NULL;
91 struct ares_addr_port_node *srvr_curr;
92 int status = ARES_SUCCESS;
98 for (i = 0; i < channel->nservers; i++)
100 /* Allocate storage for this server node appending it to the list */
101 srvr_curr = ares_malloc_data(ARES_DATATYPE_ADDR_PORT_NODE);
104 status = ARES_ENOMEM;
109 srvr_last->next = srvr_curr;
113 srvr_head = srvr_curr;
115 srvr_last = srvr_curr;
117 /* Fill this server node data */
118 srvr_curr->family = channel->servers[i].addr.family;
119 srvr_curr->udp_port = ntohs((unsigned short)channel->servers[i].addr.udp_port);
120 srvr_curr->tcp_port = ntohs((unsigned short)channel->servers[i].addr.tcp_port);
121 if (srvr_curr->family == AF_INET)
122 memcpy(&srvr_curr->addrV4, &channel->servers[i].addr.addrV4,
123 sizeof(srvr_curr->addrV4));
125 memcpy(&srvr_curr->addrV6, &channel->servers[i].addr.addrV6,
126 sizeof(srvr_curr->addrV6));
129 if (status != ARES_SUCCESS)
133 ares_free_data(srvr_head);
138 *servers = srvr_head;
143 int ares_set_servers(ares_channel channel,
144 struct ares_addr_node *servers)
146 struct ares_addr_node *srvr;
150 if (ares_library_initialized() != ARES_SUCCESS)
151 return ARES_ENOTINITIALIZED; /* LCOV_EXCL_LINE: n/a on non-WinSock */
156 ares__destroy_servers_state(channel);
158 for (srvr = servers; srvr; srvr = srvr->next)
165 /* Allocate storage for servers state */
166 channel->servers = ares_malloc(num_srvrs * sizeof(struct server_state));
167 if (!channel->servers)
171 channel->nservers = num_srvrs;
172 /* Fill servers state address data */
173 for (i = 0, srvr = servers; srvr; i++, srvr = srvr->next)
175 channel->servers[i].addr.family = srvr->family;
176 channel->servers[i].addr.udp_port = 0;
177 channel->servers[i].addr.tcp_port = 0;
178 if (srvr->family == AF_INET)
179 memcpy(&channel->servers[i].addr.addrV4, &srvr->addrV4,
180 sizeof(srvr->addrV4));
182 memcpy(&channel->servers[i].addr.addrV6, &srvr->addrV6,
183 sizeof(srvr->addrV6));
185 /* Initialize servers state remaining data */
186 ares__init_servers_state(channel);
192 int ares_set_servers_ports(ares_channel channel,
193 struct ares_addr_port_node *servers)
195 struct ares_addr_port_node *srvr;
199 if (ares_library_initialized() != ARES_SUCCESS)
200 return ARES_ENOTINITIALIZED; /* LCOV_EXCL_LINE: n/a on non-WinSock */
205 ares__destroy_servers_state(channel);
207 for (srvr = servers; srvr; srvr = srvr->next)
214 /* Allocate storage for servers state */
215 channel->servers = ares_malloc(num_srvrs * sizeof(struct server_state));
216 if (!channel->servers)
220 channel->nservers = num_srvrs;
221 /* Fill servers state address data */
222 for (i = 0, srvr = servers; srvr; i++, srvr = srvr->next)
224 channel->servers[i].addr.family = srvr->family;
225 channel->servers[i].addr.udp_port = htons((unsigned short)srvr->udp_port);
226 channel->servers[i].addr.tcp_port = htons((unsigned short)srvr->tcp_port);
227 if (srvr->family == AF_INET)
228 memcpy(&channel->servers[i].addr.addrV4, &srvr->addrV4,
229 sizeof(srvr->addrV4));
231 memcpy(&channel->servers[i].addr.addrV6, &srvr->addrV6,
232 sizeof(srvr->addrV6));
234 /* Initialize servers state remaining data */
235 ares__init_servers_state(channel);
241 /* Incomming string format: host[:port][,host[:port]]... */
242 /* IPv6 addresses with ports require square brackets [fe80::1%lo0]:53 */
243 static int set_servers_csv(ares_channel channel,
244 const char* _csv, int use_port)
251 int rv = ARES_SUCCESS;
252 struct ares_addr_port_node *servers = NULL;
253 struct ares_addr_port_node *last = NULL;
255 if (ares_library_initialized() != ARES_SUCCESS)
256 return ARES_ENOTINITIALIZED; /* LCOV_EXCL_LINE: n/a on non-WinSock */
261 ares__destroy_servers_state(channel);
265 return ARES_SUCCESS; /* blank all servers */
267 csv = ares_malloc(i + 2);
272 if (csv[i-1] != ',') { /* make parsing easier by ensuring ending ',' */
278 for (ptr = csv; *ptr; ptr++) {
280 /* count colons to determine if we have an IPv6 number or IPv4 with
284 else if (*ptr == '[') {
285 /* move start_host if an open square bracket is found wrapping an IPv6
287 start_host = ptr + 1;
289 else if (*ptr == ',') {
294 struct ares_in6_addr in6;
295 struct ares_addr_port_node *s = NULL;
297 *ptr = 0; /* null terminate host:port string */
298 /* Got an entry..see if the port was specified. */
300 while (pp > start_host) {
301 /* a single close square bracket followed by a colon, ']:' indicates
302 an IPv6 address with port */
303 if ((*pp == ']') && (*p == ':'))
304 break; /* found port */
305 /* a single colon, ':' indicates an IPv4 address with port */
306 if ((*pp == ':') && (cc == 1))
307 break; /* found port */
308 if (!(ISDIGIT(*pp) || (*pp == ':'))) {
309 /* Found end of digits before we found :, so wasn't a port */
310 /* must allow ':' for IPv6 case of ']:' indicates we found a port */
317 if ((pp != start_host) && ((pp + 1) < ptr)) {
318 /* Found it. Parse over the port number */
319 /* when an IPv6 address is wrapped with square brackets the port
322 p++; /* move p before ':' */
323 /* p will point to the start of the port */
324 port = (int)strtol(p, NULL, 10);
325 *pp = 0; /* null terminate host */
328 /* resolve host, try ipv4 first, rslt is in network byte order */
329 rv = ares_inet_pton(AF_INET, start_host, &in4);
331 /* Ok, try IPv6 then */
332 rv = ares_inet_pton(AF_INET6, start_host, &in6);
337 /* was ipv6, add new server */
338 s = ares_malloc(sizeof(*s));
343 s->family = AF_INET6;
344 memcpy(&s->addr, &in6, sizeof(struct ares_in6_addr));
347 /* was ipv4, add new server */
348 s = ares_malloc(sizeof(*s));
354 memcpy(&s->addr, &in4, sizeof(struct in_addr));
357 s->udp_port = use_port ? port: 0;
358 s->tcp_port = s->udp_port;
362 /* need to move last to maintain the linked list */
371 /* Set up for next one */
372 start_host = ptr + 1;
377 rv = ares_set_servers_ports(channel, servers);
383 struct ares_addr_port_node *s = servers;
384 servers = servers->next;
391 int ares_set_servers_csv(ares_channel channel,
394 return set_servers_csv(channel, _csv, FALSE);
397 int ares_set_servers_ports_csv(ares_channel channel,
400 return set_servers_csv(channel, _csv, TRUE);