2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2008-2010 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"
22 #include "ares_data.h"
23 #include "ares_private.h"
26 int ares_get_servers(ares_channel channel,
27 struct ares_addr_node **servers)
29 struct ares_addr_node *srvr_head = NULL;
30 struct ares_addr_node *srvr_last = NULL;
31 struct ares_addr_node *srvr_curr;
32 int status = ARES_SUCCESS;
38 for (i = 0; i < channel->nservers; i++)
40 /* Allocate storage for this server node appending it to the list */
41 srvr_curr = ares_malloc_data(ARES_DATATYPE_ADDR_NODE);
49 srvr_last->next = srvr_curr;
53 srvr_head = srvr_curr;
55 srvr_last = srvr_curr;
57 /* Fill this server node data */
58 srvr_curr->family = channel->servers[i].addr.family;
59 if (srvr_curr->family == AF_INET)
60 memcpy(&srvr_curr->addrV4, &channel->servers[i].addr.addrV4,
61 sizeof(srvr_curr->addrV4));
63 memcpy(&srvr_curr->addrV6, &channel->servers[i].addr.addrV6,
64 sizeof(srvr_curr->addrV6));
67 if (status != ARES_SUCCESS)
71 ares_free_data(srvr_head);
82 int ares_set_servers(ares_channel channel,
83 struct ares_addr_node *servers)
85 struct ares_addr_node *srvr;
89 if (ares_library_initialized() != ARES_SUCCESS)
90 return ARES_ENOTINITIALIZED;
95 ares__destroy_servers_state(channel);
97 for (srvr = servers; srvr; srvr = srvr->next)
104 /* Allocate storage for servers state */
105 channel->servers = malloc(num_srvrs * sizeof(struct server_state));
106 if (!channel->servers)
110 channel->nservers = num_srvrs;
111 /* Fill servers state address data */
112 for (i = 0, srvr = servers; srvr; i++, srvr = srvr->next)
114 channel->servers[i].addr.family = srvr->family;
115 if (srvr->family == AF_INET)
116 memcpy(&channel->servers[i].addr.addrV4, &srvr->addrV4,
117 sizeof(srvr->addrV4));
119 memcpy(&channel->servers[i].addr.addrV6, &srvr->addrV6,
120 sizeof(srvr->addrV6));
122 /* Initialize servers state remaining data */
123 ares__init_servers_state(channel);