ctdb-server: Use find_public_ip_vnn() in a couple of extra places
[samba4-gss.git] / ctdb / conf / node.c
bloba242c52dfd6f16262d4efdeed83d2ee70b1fd8da
1 /*
2 Node file loading
4 Copyright (C) Martin Andrew Tridgell 2007
5 Copyright (C) Martin Ronnie Sahlberg 2008, 2009
6 Copyright (C) Martin Schwenke 2015
7 Copyright (C) Amitay Isaacs 2015
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #ifndef __CTDB_NODE_H__
24 #define __CTDB_NODE_H__
26 #include "replace.h"
27 #include "system/network.h"
29 #include <talloc.h>
31 #include "lib/util/util_file.h"
32 #include "lib/util/util_strlist.h"
34 #include "protocol/protocol.h"
35 #include "protocol/protocol_util.h"
37 #include "conf/node.h"
40 /* If unset, set port in address to the CTDB port */
41 static void node_set_port(ctdb_sock_addr *address)
43 struct servent *se = NULL;
44 unsigned int port;
46 port = ctdb_sock_addr_port(address);
47 if (port != 0) {
48 return;
51 setservent(0);
52 se = getservbyname("ctdb", "tcp");
53 endservent();
55 if (se == NULL) {
56 port = CTDB_PORT;
57 } else {
58 port = ntohs(se->s_port);
61 ctdb_sock_addr_set_port(address, port);
64 /* Append a node to a node map with given address and flags */
65 static bool node_map_add(struct ctdb_node_map *nodemap,
66 const char *nstr,
67 uint32_t flags)
69 ctdb_sock_addr addr = {};
70 uint32_t num;
71 struct ctdb_node_and_flags *n = NULL;
72 bool ok;
74 ok = ctdb_parse_node_address(nstr, &addr);
75 if (!ok) {
76 fprintf(stderr, "Invalid node address %s\n", nstr);
77 return false;
80 num = nodemap->num;
81 n = talloc_realloc(nodemap,
82 nodemap->node,
83 struct ctdb_node_and_flags,
84 num + 1);
85 if (n == NULL) {
86 return false;
88 nodemap->node = n;
90 n = &nodemap->node[num];
91 n->addr = addr;
92 n->pnn = num;
93 n->flags = flags;
95 nodemap->num = num + 1;
96 return true;
99 static struct ctdb_node_map *ctdb_parse_nodes_lines(TALLOC_CTX *mem_ctx,
100 char **lines,
101 int nlines)
103 int i;
104 struct ctdb_node_map *nodemap = NULL;
106 nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
107 if (nodemap == NULL) {
108 return NULL;
111 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
112 nlines--;
115 for (i = 0; i < nlines; i++) {
116 char *line;
117 const char *node = NULL;
118 uint32_t flags;
119 size_t len;
121 line = lines[i];
122 /* strip leading spaces */
123 while((*line == ' ') || (*line == '\t')) {
124 line++;
127 len = strlen(line);
129 /* strip trailing spaces */
130 while (len > 1 &&
131 (line[len - 1] == ' ' || line[len - 1] == '\t')) {
133 line[len - 1] = '\0';
134 len--;
137 if (len == 0) {
138 continue;
140 if (*line == '#') {
142 * A "deleted" node is a node that is
143 * commented out in the nodes file. This is
144 * used instead of removing a line, which
145 * would cause subsequent nodes to change
146 * their PNN.
148 flags = NODE_FLAGS_DELETED;
149 node = "0.0.0.0";
150 } else {
151 flags = 0;
152 node = line;
154 if (!node_map_add(nodemap, node, flags)) {
155 TALLOC_FREE(nodemap);
156 return NULL;
160 return nodemap;
163 /* Convert a string containing a command line to an array of strings. Does not
164 * handle shell style quoting! A space will always create a new argument.
166 static char **command_str_to_args(TALLOC_CTX *mem_ctx,
167 const char *argstring)
169 return str_list_make(mem_ctx, argstring, " \t");
172 /* Read a nodes file into a node map */
173 static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
174 const char *nlist)
176 char **lines = NULL;
177 int nlines;
178 struct ctdb_node_map *nodemap = NULL;
180 lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
181 if (lines == NULL) {
182 return NULL;
185 nodemap = ctdb_parse_nodes_lines(mem_ctx, lines, nlines);
186 talloc_free(lines);
187 return nodemap;
190 /* Read a nodes file from an external process into a node map */
191 static struct ctdb_node_map *ctdb_read_nodes_cmd(TALLOC_CTX *mem_ctx,
192 const char *nodes_cmd)
194 char **lines = NULL;
195 int nlines;
196 char *p;
197 size_t size;
198 struct ctdb_node_map *nodemap = NULL;
199 char **argl = command_str_to_args(mem_ctx, nodes_cmd);
201 if (argl == NULL) {
202 return NULL;
204 p = file_ploadv(argl, &size);
205 if (!p) {
206 return NULL;
209 lines = file_lines_parse(p, size, &nlines, mem_ctx);
210 talloc_free(p);
211 if (lines == NULL) {
212 return NULL;
215 nodemap = ctdb_parse_nodes_lines(mem_ctx, lines, nlines);
216 talloc_free(lines);
217 return nodemap;
220 bool ctdb_parse_node_address(const char *str, ctdb_sock_addr *address)
222 int ret;
224 ret = ctdb_sock_addr_from_string(str, address, false);
225 if (ret != 0) {
226 return false;
228 node_set_port(address);
230 return true;
233 struct ctdb_node_map *ctdb_read_nodes(TALLOC_CTX *mem_ctx,
234 const char *location)
236 struct ctdb_node_map* nodemap = NULL;
238 if (location != NULL && location[0] == '!') {
239 nodemap = ctdb_read_nodes_cmd(mem_ctx, &location[1]);
240 } else {
241 nodemap = ctdb_read_nodes_file(mem_ctx, location);
244 return nodemap;
247 #endif /* __CTDB_NODE_H__ */