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 /* IPv6 addresses with ports require square brackets [fe80::1%lo0]:53 */
136 int ares_set_servers_csv(ares_channel channel,
144 int rv = ARES_SUCCESS;
145 struct ares_addr_node *servers = NULL;
146 struct ares_addr_node *last = NULL;
148 if (ares_library_initialized() != ARES_SUCCESS)
149 return ARES_ENOTINITIALIZED;
154 ares__destroy_servers_state(channel);
158 return ARES_SUCCESS; /* blank all servers */
162 if (csv[i-1] != ',') { /* make parsing easier by ensuring ending ',' */
168 for (ptr = csv; *ptr; ptr++) {
170 /* count colons to determine if we have an IPv6 number or IPv4 with
174 else if (*ptr == '[') {
175 /* move start_host if an open square bracket is found wrapping an IPv6
177 start_host = ptr + 1;
179 else if (*ptr == ',') {
183 struct ares_in6_addr in6;
184 struct ares_addr_node *s = NULL;
186 *ptr = 0; /* null terminate host:port string */
187 /* Got an entry..see if the port was specified. */
189 while (pp > start_host) {
190 /* a single close square bracket followed by a colon, ']:' indicates
191 an IPv6 address with port */
192 if ((*pp == ']') && (*p == ':'))
193 break; /* found port */
194 /* a single colon, ':' indicates an IPv4 address with port */
195 if ((*pp == ':') && (cc == 1))
196 break; /* found port */
197 if (!(ISDIGIT(*pp) || (*pp == ':'))) {
198 /* Found end of digits before we found :, so wasn't a port */
199 /* must allow ':' for IPv6 case of ']:' indicates we found a port */
206 if ((pp != start_host) && ((pp + 1) < ptr)) {
207 /* Found it. Parse over the port number */
208 /* when an IPv6 address is wrapped with square brackets the port
211 p++; /* move p before ':' */
212 /* p will point to the start of the port */
213 (void)strtol(p, NULL, 10);
214 *pp = 0; /* null terminate host */
217 /* resolve host, try ipv4 first, rslt is in network byte order */
218 rv = ares_inet_pton(AF_INET, start_host, &in4);
220 /* Ok, try IPv6 then */
221 rv = ares_inet_pton(AF_INET6, start_host, &in6);
226 /* was ipv6, add new server */
227 s = malloc(sizeof(*s));
232 s->family = AF_INET6;
233 memcpy(&s->addr, &in6, sizeof(struct ares_in6_addr));
236 /* was ipv4, add new server */
237 s = malloc(sizeof(*s));
243 memcpy(&s->addr, &in4, sizeof(struct in_addr));
246 /* TODO: Add port to ares_addr_node and assign it here. */
251 /* need to move last to maintain the linked list */
260 /* Set up for next one */
261 start_host = ptr + 1;
266 rv = ares_set_servers(channel, servers);
272 struct ares_addr_node *s = servers;
273 servers = servers->next;