2 Tests support for CTDB IP allocation
4 Copyright (C) Martin Schwenke 2011
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.
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.
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/>.
23 #include "system/network.h"
25 #include "lib/util/debug.h"
27 #include "protocol/protocol.h"
28 #include "common/logging.h"
29 #include "common/system.h"
31 #include "ipalloc_read_known_ips.h"
33 static bool add_ip(TALLOC_CTX *mem_ctx,
34 struct ctdb_public_ip_list *l,
39 l->ip = talloc_realloc(mem_ctx, l->ip,
40 struct ctdb_public_ip, l->num + 1);
42 D_ERR(__location__ " out of memory\n");
46 l->ip[l->num].addr = *addr;
47 l->ip[l->num].pnn = pnn;
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,
57 struct ctdb_public_ip_list **k,
58 struct ctdb_public_ip_list *known)
65 /* Known public IPs */
66 *k = talloc_zero(known, struct ctdb_public_ip_list);
71 while (fgets(line, sizeof(line), stdin) != NULL) {
73 /* Get rid of pesky newline */
74 if ((t = strchr(line, '\n')) != NULL) {
78 /* Exit on an empty line */
79 if (line[0] == '\0') {
83 /* Get the IP address */
84 tok = strtok(line, " \t");
86 D_WARNING("WARNING, bad line ignored :%s\n", line);
90 if (!parse_ip(tok, NULL, 0, &addr)) {
91 D_ERR("ERROR, bad address :%s\n", tok);
97 tok = strtok(NULL, " \t");
99 pnn = (int) strtol(tok, (char **) NULL, 10);
102 if (! add_ip(*k, *k, &addr, pnn)) {
106 tok = strtok(NULL, " \t#");
109 for (n = 0; n < numnodes; n++) {
110 if (! add_ip(known, &known[n],
119 /* Handle allowed nodes for addr */
121 D_ERR("ERROR, bad token\n");
124 t = strtok(tok, ",");
126 n = (int) strtol(t, (char **) NULL, 10);
127 if (! add_ip(known, &known[n], &addr, pnn)) {
130 t = strtok(NULL, ",");
141 struct ctdb_public_ip_list * ipalloc_read_known_ips(TALLOC_CTX *ctx,
146 struct ctdb_public_ip_list *k;
147 struct ctdb_public_ip_list *known;
149 known = talloc_zero_array(ctx, struct ctdb_public_ip_list,
152 D_ERR(__location__ " out of memory\n");
157 for (n = 0; n < numnodes; n++) {
158 if (! read_ctdb_public_ip_info_node(multi, numnodes,
166 if (! read_ctdb_public_ip_info_node(multi, numnodes,