3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Copyright (C) 2008-2010 by Daniel Stenberg
6 * Permission to use, copy, modify, and distribute this
7 * software and its documentation for any purpose and without
8 * fee is hereby granted, provided that the above copyright
9 * notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting
11 * documentation, and that the name of M.I.T. not be used in
12 * advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 * M.I.T. makes no representations about the suitability of
15 * this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
20 #include "ares_setup.h"
23 #include "ares_data.h"
24 #include "ares_private.h"
27 int ares_get_servers(ares_channel channel,
28 struct ares_addr_node **servers)
30 struct ares_addr_node *srvr_head = NULL;
31 struct ares_addr_node *srvr_last = NULL;
32 struct ares_addr_node *srvr_curr;
33 int status = ARES_SUCCESS;
39 for (i = 0; i < channel->nservers; i++)
41 /* Allocate storage for this server node appending it to the list */
42 srvr_curr = ares_malloc_data(ARES_DATATYPE_ADDR_NODE);
50 srvr_last->next = srvr_curr;
54 srvr_head = srvr_curr;
56 srvr_last = srvr_curr;
58 /* Fill this server node data */
59 srvr_curr->family = channel->servers[i].addr.family;
60 if (srvr_curr->family == AF_INET)
61 memcpy(&srvr_curr->addrV4, &channel->servers[i].addr.addrV4,
62 sizeof(srvr_curr->addrV4));
64 memcpy(&srvr_curr->addrV6, &channel->servers[i].addr.addrV6,
65 sizeof(srvr_curr->addrV6));
68 if (status != ARES_SUCCESS)
72 ares_free_data(srvr_head);
83 int ares_set_servers(ares_channel channel,
84 struct ares_addr_node *servers)
86 struct ares_addr_node *srvr;
90 if (ares_library_initialized() != ARES_SUCCESS)
91 return ARES_ENOTINITIALIZED;
96 ares__destroy_servers_state(channel);
98 for (srvr = servers; srvr; srvr = srvr->next)
105 /* Allocate storage for servers state */
106 channel->servers = malloc(num_srvrs * sizeof(struct server_state));
107 if (!channel->servers)
111 channel->nservers = num_srvrs;
112 /* Fill servers state address data */
113 for (i = 0, srvr = servers; srvr; i++, srvr = srvr->next)
115 channel->servers[i].addr.family = srvr->family;
116 if (srvr->family == AF_INET)
117 memcpy(&channel->servers[i].addr.addrV4, &srvr->addrV4,
118 sizeof(srvr->addrV4));
120 memcpy(&channel->servers[i].addr.addrV6, &srvr->addrV6,
121 sizeof(srvr->addrV6));
123 /* Initialize servers state remaining data */
124 ares__init_servers_state(channel);