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);
87 int ares_set_servers(ares_channel channel,
88 struct ares_addr_node *servers)
90 struct ares_addr_node *srvr;
94 if (ares_library_initialized() != ARES_SUCCESS)
95 return ARES_ENOTINITIALIZED;
100 ares__destroy_servers_state(channel);
102 for (srvr = servers; srvr; srvr = srvr->next)
109 /* Allocate storage for servers state */
110 channel->servers = malloc(num_srvrs * sizeof(struct server_state));
111 if (!channel->servers)
115 channel->nservers = num_srvrs;
116 /* Fill servers state address data */
117 for (i = 0, srvr = servers; srvr; i++, srvr = srvr->next)
119 channel->servers[i].addr.family = srvr->family;
120 if (srvr->family == AF_INET)
121 memcpy(&channel->servers[i].addr.addrV4, &srvr->addrV4,
122 sizeof(srvr->addrV4));
124 memcpy(&channel->servers[i].addr.addrV6, &srvr->addrV6,
125 sizeof(srvr->addrV6));
127 /* Initialize servers state remaining data */
128 ares__init_servers_state(channel);
134 /* Incomming string format: host[:port][,host[:port]]... */
135 int ares_set_servers_csv(ares_channel channel,
142 int rv = ARES_SUCCESS;
143 struct ares_addr_node *servers = NULL;
144 struct ares_addr_node *last = NULL;
146 if (ares_library_initialized() != ARES_SUCCESS)
147 return ARES_ENOTINITIALIZED;
152 ares__destroy_servers_state(channel);
156 return ARES_SUCCESS; /* blank all servers */
160 if (csv[i-1] != ',') { /* make parsing easier by ensuring ending ',' */
166 for (ptr = csv; *ptr; ptr++) {
170 struct ares_in6_addr in6;
171 struct ares_addr_node *s = NULL;
173 *ptr = 0; /* null terminate host:port string */
174 /* Got an entry..see if port was specified. */
175 while (pp > start_host) {
179 /* Found end of digits before we found :, so wasn't a port */
185 if ((pp != start_host) && ((pp + 1) < ptr)) {
186 /* Found it. Parse over the port number */
187 (void)strtol(pp + 1, NULL, 10);
188 *pp = 0; /* null terminate host */
190 /* resolve host, try ipv4 first, rslt is in network byte order */
191 rv = ares_inet_pton(AF_INET, start_host, &in4);
193 /* Ok, try IPv6 then */
194 rv = ares_inet_pton(AF_INET6, start_host, &in6);
199 /* was ipv6, add new server */
200 s = malloc(sizeof(*s));
205 s->family = AF_INET6;
206 memcpy(&s->addr, &in6, sizeof(struct ares_in6_addr));
209 /* was ipv4, add new server */
210 s = malloc(sizeof(*s));
216 memcpy(&s->addr, &in4, sizeof(struct in_addr));
219 /* TODO: Add port to ares_addr_node and assign it here. */
231 /* Set up for next one */
232 start_host = ptr + 1;
236 rv = ares_set_servers(channel, servers);
242 struct ares_addr_node *s = servers;
243 servers = servers->next;