Added IPv6 name servers support
[platform/upstream/c-ares.git] / ares_options.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  * Copyright (C) 2008-2010 by Daniel Stenberg
5  *
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.
17  */
18
19
20 #include "ares_setup.h"
21
22 #include "ares.h"
23 #include "ares_data.h"
24 #include "ares_private.h"
25
26
27 int ares_get_servers(ares_channel channel,
28                      struct ares_addr_node **servers)
29 {
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;
34   int i;
35
36   if (!channel)
37     return ARES_ENODATA;
38
39   for (i = 0; i < channel->nservers; i++)
40     {
41       /* Allocate storage for this server node appending it to the list */
42       srvr_curr = ares_malloc_data(ARES_DATATYPE_ADDR_NODE);
43       if (!srvr_curr)
44         {
45           status = ARES_ENOMEM;
46           break;
47         }
48       if (srvr_last)
49         {
50           srvr_last->next = srvr_curr;
51         }
52       else
53         {
54           srvr_head = srvr_curr;
55         }
56       srvr_last = srvr_curr;
57
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));
63       else
64         memcpy(&srvr_curr->addrV6, &channel->servers[i].addr.addrV6,
65                sizeof(srvr_curr->addrV6));
66     }
67
68   if (status != ARES_SUCCESS)
69     {
70       if (srvr_head)
71         {
72           ares_free_data(srvr_head);
73           srvr_head = NULL;
74         }
75     }
76
77   *servers = srvr_head;
78
79   return status;
80 }
81
82
83 int ares_set_servers(ares_channel channel,
84                      struct ares_addr_node *servers)
85 {
86   struct ares_addr_node *srvr;
87   int num_srvrs = 0;
88   int i;
89
90   if (ares_library_initialized() != ARES_SUCCESS)
91     return ARES_ENOTINITIALIZED;
92
93   if (!channel)
94     return ARES_ENODATA;
95
96   ares__destroy_servers_state(channel);
97
98   for (srvr = servers; srvr; srvr = srvr->next)
99     {
100       num_srvrs++;
101     }
102
103   if (num_srvrs > 0)
104     {
105       /* Allocate storage for servers state */
106       channel->servers = malloc(num_srvrs * sizeof(struct server_state));
107       if (!channel->servers)
108         {
109           return ARES_ENOMEM;
110         }
111       channel->nservers = num_srvrs;
112       /* Fill servers state address data */
113       for (i = 0, srvr = servers; srvr; i++, srvr = srvr->next)
114         {
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));
119           else
120             memcpy(&channel->servers[i].addr.addrV6, &srvr->addrV6,
121                    sizeof(srvr->addrV6));
122         }
123       /* Initialize servers state remaining data */
124       ares__init_servers_state(channel);
125     }
126
127   return ARES_SUCCESS;
128 }