ctdb-tests: Populate per-node known IPs inline
[metze/samba/wip.git] / ctdb / tests / src / ipalloc_read_known_ips.c
1 /*
2    Tests support for CTDB IP allocation
3
4    Copyright (C) Martin Schwenke 2011
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <talloc.h>
21
22 #include "replace.h"
23 #include "system/network.h"
24
25 #include "lib/util/debug.h"
26
27 #include "protocol/protocol.h"
28 #include "common/logging.h"
29 #include "common/system.h"
30
31 #include "ipalloc_read_known_ips.h"
32
33 static bool add_ip(TALLOC_CTX *mem_ctx,
34                    struct ctdb_public_ip_list *l,
35                    ctdb_sock_addr *addr,
36                    uint32_t pnn)
37 {
38
39         l->ip = talloc_realloc(mem_ctx, l->ip,
40                                struct ctdb_public_ip, l->num + 1);
41         if (l->ip == NULL) {
42                 D_ERR(__location__ " out of memory\n");
43                 return false;
44         }
45
46         l->ip[l->num].addr = *addr;
47         l->ip[l->num].pnn  = pnn;
48         l->num++;
49
50         return true;
51 }
52
53 /* Format of each line is "IP CURRENT_PNN [ALLOWED_PNN,...]".
54  * If multi is true then ALLOWED_PNNs are not allowed.  */
55 static bool read_ctdb_public_ip_info_node(bool multi,
56                                           int numnodes,
57                                           struct ctdb_public_ip_list **k,
58                                           struct ctdb_public_ip_list *known)
59 {
60         char line[1024];
61         ctdb_sock_addr addr;
62         char *t, *tok;
63         int pnn, n;
64
65         /* Known public IPs */
66         *k = talloc_zero(known, struct ctdb_public_ip_list);
67         if (*k == NULL) {
68                 goto fail;
69         }
70
71         while (fgets(line, sizeof(line), stdin) != NULL) {
72
73                 /* Get rid of pesky newline */
74                 if ((t = strchr(line, '\n')) != NULL) {
75                         *t = '\0';
76                 }
77
78                 /* Exit on an empty line */
79                 if (line[0] == '\0') {
80                         break;
81                 }
82
83                 /* Get the IP address */
84                 tok = strtok(line, " \t");
85                 if (tok == NULL) {
86                         D_WARNING("WARNING, bad line ignored :%s\n", line);
87                         continue;
88                 }
89
90                 if (!parse_ip(tok, NULL, 0, &addr)) {
91                         D_ERR("ERROR, bad address :%s\n", tok);
92                         continue;
93                 }
94
95                 /* Get the PNN */
96                 pnn = -1;
97                 tok = strtok(NULL, " \t");
98                 if (tok != NULL) {
99                         pnn = (int) strtol(tok, (char **) NULL, 10);
100                 }
101
102                 if (! add_ip(*k, *k, &addr, pnn)) {
103                         goto fail;
104                 }
105
106                 tok = strtok(NULL, " \t#");
107                 if (tok == NULL) {
108                         if (! multi) {
109                                 for (n = 0; n < numnodes; n++) {
110                                         if (! add_ip(known, &known[n],
111                                                      &addr, pnn)) {
112                                                 goto fail;
113                                         }
114                                 }
115                         }
116                         continue;
117                 }
118
119                 /* Handle allowed nodes for addr */
120                 if (multi) {
121                         D_ERR("ERROR, bad token\n");
122                         goto fail;
123                 }
124                 t = strtok(tok, ",");
125                 while (t != NULL) {
126                         n = (int) strtol(t, (char **) NULL, 10);
127                         if (! add_ip(known, &known[n], &addr, pnn)) {
128                                 goto fail;
129                         }
130                         t = strtok(NULL, ",");
131                 }
132         }
133
134         return true;
135
136 fail:
137         TALLOC_FREE(*k);
138         return false;
139 }
140
141 struct ctdb_public_ip_list * ipalloc_read_known_ips(TALLOC_CTX *ctx,
142                                                     int numnodes,
143                                                     bool multi)
144 {
145         int n;
146         struct ctdb_public_ip_list *k;
147         struct ctdb_public_ip_list *known;
148
149         known = talloc_zero_array(ctx, struct ctdb_public_ip_list,
150                                   numnodes);
151         if (known == NULL) {
152                 D_ERR(__location__ " out of memory\n");
153                 goto fail;
154         }
155
156         if (multi) {
157                 for (n = 0; n < numnodes; n++) {
158                         if (! read_ctdb_public_ip_info_node(multi, numnodes,
159                                                             &k, known)) {
160                                 goto fail;
161                         }
162
163                         known[n] = *k;
164                 }
165         } else {
166                 if (! read_ctdb_public_ip_info_node(multi, numnodes,
167                                                     &k, known)) {
168                         goto fail;
169                 }
170         }
171
172         return known;
173
174 fail:
175         talloc_free(known);
176         return NULL;
177 }