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