2 CTDB protocol marshalling
4 Copyright (C) Amitay Isaacs 2015
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/>.
21 #include "system/network.h"
25 #include "common/line.h"
28 #include "protocol_util.h"
31 enum ctdb_runstate runstate;
34 { CTDB_RUNSTATE_UNKNOWN, "UNKNOWN" },
35 { CTDB_RUNSTATE_INIT, "INIT" },
36 { CTDB_RUNSTATE_SETUP, "SETUP" },
37 { CTDB_RUNSTATE_FIRST_RECOVERY, "FIRST_RECOVERY" },
38 { CTDB_RUNSTATE_STARTUP, "STARTUP" },
39 { CTDB_RUNSTATE_RUNNING, "RUNNING" },
40 { CTDB_RUNSTATE_SHUTDOWN, "SHUTDOWN" },
44 const char *ctdb_runstate_to_string(enum ctdb_runstate runstate)
48 for (i=0; runstate_map[i].label != NULL; i++) {
49 if (runstate_map[i].runstate == runstate) {
50 return runstate_map[i].label;
54 return runstate_map[0].label;
57 enum ctdb_runstate ctdb_runstate_from_string(const char *runstate_str)
61 for (i=0; runstate_map[i].label != NULL; i++) {
62 if (strcasecmp(runstate_map[i].label,
64 return runstate_map[i].runstate;
68 return CTDB_RUNSTATE_UNKNOWN;
72 enum ctdb_event event;
75 { CTDB_EVENT_INIT, "init" },
76 { CTDB_EVENT_SETUP, "setup" },
77 { CTDB_EVENT_STARTUP, "startup" },
78 { CTDB_EVENT_START_RECOVERY, "startrecovery" },
79 { CTDB_EVENT_RECOVERED, "recovered" },
80 { CTDB_EVENT_TAKE_IP, "takeip" },
81 { CTDB_EVENT_RELEASE_IP, "releaseip" },
82 { CTDB_EVENT_MONITOR, "monitor" },
83 { CTDB_EVENT_SHUTDOWN, "shutdown" },
84 { CTDB_EVENT_UPDATE_IP, "updateip" },
85 { CTDB_EVENT_IPREALLOCATED, "ipreallocated" },
86 { CTDB_EVENT_MAX, "all" },
90 const char *ctdb_event_to_string(enum ctdb_event event)
94 for (i=0; event_map[i].label != NULL; i++) {
95 if (event_map[i].event == event) {
96 return event_map[i].label;
103 enum ctdb_event ctdb_event_from_string(const char *event_str)
107 for (i=0; event_map[i].label != NULL; i++) {
108 if (strcmp(event_map[i].label, event_str) == 0) {
109 return event_map[i].event;
113 return CTDB_EVENT_MAX;
116 int ctdb_sock_addr_to_buf(char *buf, socklen_t buflen,
117 ctdb_sock_addr *addr, bool with_port)
121 switch (addr->sa.sa_family) {
123 t = inet_ntop(addr->ip.sin_family, &addr->ip.sin_addr,
131 t = inet_ntop(addr->ip6.sin6_family, &addr->ip6.sin6_addr,
144 size_t len = strlen(buf);
147 ret = snprintf(buf+len, buflen-len,
148 ":%u", ctdb_sock_addr_port(addr));
149 if (ret >= buflen-len) {
157 const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx,
158 ctdb_sock_addr *addr, bool with_port)
164 cip = talloc_size(mem_ctx, len);
170 ret = ctdb_sock_addr_to_buf(cip, len, addr, with_port);
179 static int ipv4_from_string(const char *str, struct sockaddr_in *ip)
183 *ip = (struct sockaddr_in) {
184 .sin_family = AF_INET,
187 ret = inet_pton(AF_INET, str, &ip->sin_addr);
192 #ifdef HAVE_SOCK_SIN_LEN
193 ip->sin_len = sizeof(*ip);
198 static int ipv6_from_string(const char *str, struct sockaddr_in6 *ip6)
202 *ip6 = (struct sockaddr_in6) {
203 .sin6_family = AF_INET6,
206 ret = inet_pton(AF_INET6, str, &ip6->sin6_addr);
211 #ifdef HAVE_SOCK_SIN6_LEN
212 ip6->sin6_len = sizeof(*ip6);
217 static int ip_from_string(const char *str, ctdb_sock_addr *addr)
226 ZERO_STRUCTP(addr); /* valgrind :-) */
228 /* IPv4 or IPv6 address?
230 * Use rindex() because we need the right-most ':' below for
231 * IPv4-mapped IPv6 addresses anyway...
233 p = rindex(str, ':');
235 ret = ipv4_from_string(str, &addr->ip);
237 uint8_t ipv4_mapped_prefix[12] = {
238 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
241 ret = ipv6_from_string(str, &addr->ip6);
247 * Check for IPv4-mapped IPv6 address
248 * (e.g. ::ffff:192.0.2.128) - reparse as IPv4 if
251 if (memcmp(&addr->ip6.sin6_addr.s6_addr[0],
253 sizeof(ipv4_mapped_prefix)) == 0) {
254 /* Reparse as IPv4 */
255 ret = ipv4_from_string(p+1, &addr->ip);
262 int ctdb_sock_addr_from_string(const char *str,
263 ctdb_sock_addr *addr, bool with_port)
266 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
273 ret = ip_from_string(str, addr);
277 /* Parse out port number and then IP address */
279 len = strlcpy(s, str, sizeof(s));
280 if (len >= sizeof(s)) {
289 port = strtoul(p+1, &endp, 10);
290 if (endp == p+1 || *endp != '\0') {
291 /* Empty string or trailing garbage */
296 ret = ip_from_string(s, addr);
298 ctdb_sock_addr_set_port(addr, port);
303 int ctdb_sock_addr_mask_from_string(const char *str,
304 ctdb_sock_addr *addr,
308 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
314 if (addr == NULL || mask == NULL) {
318 len = strlcpy(s, str, sizeof(s));
319 if (len >= sizeof(s)) {
328 m = strtoul(p+1, &endp, 10);
329 if (endp == p+1 || *endp != '\0') {
330 /* Empty string or trailing garbage */
335 ret = ip_from_string(s, addr);
344 unsigned int ctdb_sock_addr_port(ctdb_sock_addr *addr)
346 switch (addr->sa.sa_family) {
348 return ntohs(addr->ip.sin_port);
351 return ntohs(addr->ip6.sin6_port);
358 void ctdb_sock_addr_set_port(ctdb_sock_addr *addr, unsigned int port)
360 switch (addr->sa.sa_family) {
362 addr->ip.sin_port = htons(port);
365 addr->ip6.sin6_port = htons(port);
372 static int ctdb_sock_addr_cmp_family(const ctdb_sock_addr *addr1,
373 const ctdb_sock_addr *addr2)
375 /* This is somewhat arbitrary. However, when used for sorting
376 * it just needs to be consistent.
378 if (addr1->sa.sa_family < addr2->sa.sa_family) {
381 if (addr1->sa.sa_family > addr2->sa.sa_family) {
388 int ctdb_sock_addr_cmp_ip(const ctdb_sock_addr *addr1,
389 const ctdb_sock_addr *addr2)
393 ret = ctdb_sock_addr_cmp_family(addr1, addr2);
398 switch (addr1->sa.sa_family) {
400 ret = memcmp(&addr1->ip.sin_addr.s_addr,
401 &addr2->ip.sin_addr.s_addr, 4);
405 ret = memcmp(addr1->ip6.sin6_addr.s6_addr,
406 addr2->ip6.sin6_addr.s6_addr, 16);
416 int ctdb_sock_addr_cmp(const ctdb_sock_addr *addr1,
417 const ctdb_sock_addr *addr2)
421 ret = ctdb_sock_addr_cmp_ip(addr1, addr2);
426 switch (addr1->sa.sa_family) {
428 if (addr1->ip.sin_port < addr2->ip.sin_port) {
430 } else if (addr1->ip.sin_port > addr2->ip.sin_port) {
436 if (addr1->ip6.sin6_port < addr2->ip6.sin6_port) {
438 } else if (addr1->ip6.sin6_port > addr2->ip6.sin6_port) {
450 bool ctdb_sock_addr_same_ip(const ctdb_sock_addr *addr1,
451 const ctdb_sock_addr *addr2)
453 return (ctdb_sock_addr_cmp_ip(addr1, addr2) == 0);
456 bool ctdb_sock_addr_same(const ctdb_sock_addr *addr1,
457 const ctdb_sock_addr *addr2)
459 return (ctdb_sock_addr_cmp(addr1, addr2) == 0);
462 int ctdb_connection_to_buf(char *buf, size_t buflen,
463 struct ctdb_connection *conn, bool client_first)
465 char server[64], client[64];
468 ret = ctdb_sock_addr_to_buf(server, sizeof(server),
469 &conn->server, true);
474 ret = ctdb_sock_addr_to_buf(client, sizeof(client),
475 &conn->client, true);
480 if (! client_first) {
481 ret = snprintf(buf, buflen, "%s %s", server, client);
483 ret = snprintf(buf, buflen, "%s %s", client, server);
492 const char *ctdb_connection_to_string(TALLOC_CTX *mem_ctx,
493 struct ctdb_connection *conn,
496 const size_t len = 128;
500 out = talloc_size(mem_ctx, len);
505 ret = ctdb_connection_to_buf(out, len, conn, client_first);
514 int ctdb_connection_from_string(const char *str, bool client_first,
515 struct ctdb_connection *conn)
518 char *t1 = NULL, *t2 = NULL;
520 ctdb_sock_addr *first = (client_first ? &conn->client : &conn->server);
521 ctdb_sock_addr *second = (client_first ? &conn->server : &conn->client);
524 len = strlcpy(s, str, sizeof(s));
525 if (len >= sizeof(s)) {
529 t1 = strtok(s, " \t\n");
534 t2 = strtok(NULL, " \t\n\0");
539 ret = ctdb_sock_addr_from_string(t1, first, true);
544 ret = ctdb_sock_addr_from_string(t2, second, true);
549 ret = ctdb_sock_addr_cmp_family(first, second);
557 int ctdb_connection_list_add(struct ctdb_connection_list *conn_list,
558 struct ctdb_connection *conn)
562 if (conn_list == NULL) {
566 /* Ensure array is big enough */
567 len = talloc_array_length(conn_list->conn);
568 if (conn_list->num == len) {
569 conn_list->conn = talloc_realloc(conn_list, conn_list->conn,
570 struct ctdb_connection,
572 if (conn_list->conn == NULL) {
577 conn_list->conn[conn_list->num] = *conn;
583 static int connection_cmp(const void *a, const void *b)
585 const struct ctdb_connection *conn_a = a;
586 const struct ctdb_connection *conn_b = b;
589 ret = ctdb_sock_addr_cmp(&conn_a->server, &conn_b->server);
591 ret = ctdb_sock_addr_cmp(&conn_a->client, &conn_b->client);
597 int ctdb_connection_list_sort(struct ctdb_connection_list *conn_list)
599 if (conn_list == NULL) {
603 if (conn_list->num > 0) {
604 qsort(conn_list->conn, conn_list->num,
605 sizeof(struct ctdb_connection), connection_cmp);
611 const char *ctdb_connection_list_to_string(
613 struct ctdb_connection_list *conn_list, bool client_first)
618 out = talloc_strdup(mem_ctx, "");
623 if (conn_list == NULL || conn_list->num == 0) {
627 for (i = 0; i < conn_list->num; i++) {
631 ret = ctdb_connection_to_buf(buf, sizeof(buf),
632 &conn_list->conn[i], client_first);
638 out = talloc_asprintf_append(out, "%s\n", buf);
647 struct ctdb_connection_list_read_state {
648 struct ctdb_connection_list *list;
652 static int ctdb_connection_list_read_line(char *line, void *private_data)
654 struct ctdb_connection_list_read_state *state =
655 (struct ctdb_connection_list_read_state *)private_data;
656 struct ctdb_connection conn;
659 /* Skip empty lines */
660 if (line[0] == '\0') {
665 if (line[0] == '#') {
669 ret = ctdb_connection_from_string(line, state->client_first, &conn);
674 ret = ctdb_connection_list_add(state->list, &conn);
682 int ctdb_connection_list_read(TALLOC_CTX *mem_ctx,
685 struct ctdb_connection_list **conn_list)
687 struct ctdb_connection_list_read_state state;
690 if (conn_list == NULL) {
694 state.list = talloc_zero(mem_ctx, struct ctdb_connection_list);
695 if (state.list == NULL) {
699 state.client_first = client_first;
704 ctdb_connection_list_read_line,
708 *conn_list = state.list;