VERSION: Bump version up to 4.10.12.
[Samba.git] / ctdb / protocol / protocol_util.c
bloba09292c72a9f4de9b3235a81209de0b0bac27117
1 /*
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/>.
20 #include "replace.h"
21 #include "system/network.h"
23 #include <talloc.h>
25 #include "common/line.h"
27 #include "protocol.h"
28 #include "protocol_util.h"
30 static struct {
31 enum ctdb_runstate runstate;
32 const char * label;
33 } runstate_map[] = {
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" },
41 { -1, NULL },
44 const char *ctdb_runstate_to_string(enum ctdb_runstate runstate)
46 int i;
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)
59 int i;
61 for (i=0; runstate_map[i].label != NULL; i++) {
62 if (strcasecmp(runstate_map[i].label,
63 runstate_str) == 0) {
64 return runstate_map[i].runstate;
68 return CTDB_RUNSTATE_UNKNOWN;
71 static struct {
72 enum ctdb_event event;
73 const char *label;
74 } event_map[] = {
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" },
87 { -1, NULL },
90 const char *ctdb_event_to_string(enum ctdb_event event)
92 int i;
94 for (i=0; event_map[i].label != NULL; i++) {
95 if (event_map[i].event == event) {
96 return event_map[i].label;
100 return "unknown";
103 enum ctdb_event ctdb_event_from_string(const char *event_str)
105 int i;
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)
119 const char *t;
121 switch (addr->sa.sa_family) {
122 case AF_INET:
123 t = inet_ntop(addr->ip.sin_family, &addr->ip.sin_addr,
124 buf, buflen);
125 if (t == NULL) {
126 return errno;
128 break;
130 case AF_INET6:
131 t = inet_ntop(addr->ip6.sin6_family, &addr->ip6.sin6_addr,
132 buf, buflen);
133 if (t == NULL) {
134 return errno;
136 break;
138 default:
139 return EAFNOSUPPORT;
140 break;
143 if (with_port) {
144 size_t len = strlen(buf);
145 int ret;
147 ret = snprintf(buf+len, buflen-len,
148 ":%u", ctdb_sock_addr_port(addr));
149 if (ret >= buflen-len) {
150 return ENOSPC;
154 return 0;
157 const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx,
158 ctdb_sock_addr *addr, bool with_port)
160 size_t len = 64;
161 char *cip;
162 int ret;
164 cip = talloc_size(mem_ctx, len);
166 if (cip == NULL) {
167 return NULL;
170 ret = ctdb_sock_addr_to_buf(cip, len, addr, with_port);
171 if (ret != 0) {
172 talloc_free(cip);
173 return NULL;
176 return cip;
179 static int ipv4_from_string(const char *str, struct sockaddr_in *ip)
181 int ret;
183 *ip = (struct sockaddr_in) {
184 .sin_family = AF_INET,
187 ret = inet_pton(AF_INET, str, &ip->sin_addr);
188 if (ret != 1) {
189 return EINVAL;
192 #ifdef HAVE_SOCK_SIN_LEN
193 ip->sin_len = sizeof(*ip);
194 #endif
195 return 0;
198 static int ipv6_from_string(const char *str, struct sockaddr_in6 *ip6)
200 int ret;
202 *ip6 = (struct sockaddr_in6) {
203 .sin6_family = AF_INET6,
206 ret = inet_pton(AF_INET6, str, &ip6->sin6_addr);
207 if (ret != 1) {
208 return EINVAL;
211 #ifdef HAVE_SOCK_SIN6_LEN
212 ip6->sin6_len = sizeof(*ip6);
213 #endif
214 return 0;
217 static int ip_from_string(const char *str, ctdb_sock_addr *addr)
219 char *p;
220 int ret;
222 if (addr == NULL) {
223 return EINVAL;
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, ':');
234 if (p == NULL) {
235 ret = ipv4_from_string(str, &addr->ip);
236 } else {
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);
242 if (ret != 0) {
243 return ret;
247 * Check for IPv4-mapped IPv6 address
248 * (e.g. ::ffff:192.0.2.128) - reparse as IPv4 if
249 * necessary
251 if (memcmp(&addr->ip6.sin6_addr.s6_addr[0],
252 ipv4_mapped_prefix,
253 sizeof(ipv4_mapped_prefix)) == 0) {
254 /* Initialize addr struct to zero before reparsing as IPV4 */
255 ZERO_STRUCTP(addr);
257 /* Reparse as IPv4 */
258 ret = ipv4_from_string(p+1, &addr->ip);
262 return ret;
265 int ctdb_sock_addr_from_string(const char *str,
266 ctdb_sock_addr *addr, bool with_port)
268 char *p;
269 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
270 unsigned port;
271 char *endp = NULL;
272 size_t len;
273 int ret;
275 if (! with_port) {
276 ret = ip_from_string(str, addr);
277 return ret;
280 /* Parse out port number and then IP address */
282 len = strlcpy(s, str, sizeof(s));
283 if (len >= sizeof(s)) {
284 return EINVAL;
287 p = rindex(s, ':');
288 if (p == NULL) {
289 return EINVAL;
292 port = strtoul(p+1, &endp, 10);
293 if (endp == p+1 || *endp != '\0') {
294 /* Empty string or trailing garbage */
295 return EINVAL;
298 *p = '\0';
299 ret = ip_from_string(s, addr);
301 ctdb_sock_addr_set_port(addr, port);
303 return ret;
306 int ctdb_sock_addr_mask_from_string(const char *str,
307 ctdb_sock_addr *addr,
308 unsigned int *mask)
310 char *p;
311 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
312 unsigned int m;
313 char *endp = NULL;
314 ssize_t len;
315 bool ret;
317 if (addr == NULL || mask == NULL) {
318 return EINVAL;
321 len = strlcpy(s, str, sizeof(s));
322 if (len >= sizeof(s)) {
323 return EINVAL;
326 p = rindex(s, '/');
327 if (p == NULL) {
328 return EINVAL;
331 m = strtoul(p+1, &endp, 10);
332 if (endp == p+1 || *endp != '\0') {
333 /* Empty string or trailing garbage */
334 return EINVAL;
337 *p = '\0';
338 ret = ip_from_string(s, addr);
340 if (ret == 0) {
341 *mask = m;
344 return ret;
347 unsigned int ctdb_sock_addr_port(ctdb_sock_addr *addr)
349 switch (addr->sa.sa_family) {
350 case AF_INET:
351 return ntohs(addr->ip.sin_port);
352 break;
353 case AF_INET6:
354 return ntohs(addr->ip6.sin6_port);
355 break;
356 default:
357 return 0;
361 void ctdb_sock_addr_set_port(ctdb_sock_addr *addr, unsigned int port)
363 switch (addr->sa.sa_family) {
364 case AF_INET:
365 addr->ip.sin_port = htons(port);
366 break;
367 case AF_INET6:
368 addr->ip6.sin6_port = htons(port);
369 break;
370 default:
371 break;
375 static int ctdb_sock_addr_cmp_family(const ctdb_sock_addr *addr1,
376 const ctdb_sock_addr *addr2)
378 /* This is somewhat arbitrary. However, when used for sorting
379 * it just needs to be consistent.
381 if (addr1->sa.sa_family < addr2->sa.sa_family) {
382 return -1;
384 if (addr1->sa.sa_family > addr2->sa.sa_family) {
385 return 1;
388 return 0;
391 int ctdb_sock_addr_cmp_ip(const ctdb_sock_addr *addr1,
392 const ctdb_sock_addr *addr2)
394 int ret;
396 ret = ctdb_sock_addr_cmp_family(addr1, addr2);
397 if (ret != 0) {
398 return ret;
401 switch (addr1->sa.sa_family) {
402 case AF_INET:
403 ret = memcmp(&addr1->ip.sin_addr.s_addr,
404 &addr2->ip.sin_addr.s_addr, 4);
405 break;
407 case AF_INET6:
408 ret = memcmp(addr1->ip6.sin6_addr.s6_addr,
409 addr2->ip6.sin6_addr.s6_addr, 16);
410 break;
412 default:
413 ret = -1;
416 return ret;
419 int ctdb_sock_addr_cmp(const ctdb_sock_addr *addr1,
420 const ctdb_sock_addr *addr2)
422 int ret = 0;
424 ret = ctdb_sock_addr_cmp_ip(addr1, addr2);
425 if (ret != 0) {
426 return ret;
429 switch (addr1->sa.sa_family) {
430 case AF_INET:
431 if (addr1->ip.sin_port < addr2->ip.sin_port) {
432 ret = -1;
433 } else if (addr1->ip.sin_port > addr2->ip.sin_port) {
434 ret = 1;
436 break;
438 case AF_INET6:
439 if (addr1->ip6.sin6_port < addr2->ip6.sin6_port) {
440 ret = -1;
441 } else if (addr1->ip6.sin6_port > addr2->ip6.sin6_port) {
442 ret = 1;
444 break;
446 default:
447 ret = -1;
450 return ret;
453 bool ctdb_sock_addr_same_ip(const ctdb_sock_addr *addr1,
454 const ctdb_sock_addr *addr2)
456 return (ctdb_sock_addr_cmp_ip(addr1, addr2) == 0);
459 bool ctdb_sock_addr_same(const ctdb_sock_addr *addr1,
460 const ctdb_sock_addr *addr2)
462 return (ctdb_sock_addr_cmp(addr1, addr2) == 0);
465 int ctdb_connection_to_buf(char *buf, size_t buflen,
466 struct ctdb_connection *conn, bool client_first)
468 char server[64], client[64];
469 int ret;
471 ret = ctdb_sock_addr_to_buf(server, sizeof(server),
472 &conn->server, true);
473 if (ret != 0) {
474 return ret;
477 ret = ctdb_sock_addr_to_buf(client, sizeof(client),
478 &conn->client, true);
479 if (ret != 0) {
480 return ret;
483 if (! client_first) {
484 ret = snprintf(buf, buflen, "%s %s", server, client);
485 } else {
486 ret = snprintf(buf, buflen, "%s %s", client, server);
488 if (ret >= buflen) {
489 return ENOSPC;
492 return 0;
495 const char *ctdb_connection_to_string(TALLOC_CTX *mem_ctx,
496 struct ctdb_connection *conn,
497 bool client_first)
499 const size_t len = 128;
500 char *out;
501 int ret;
503 out = talloc_size(mem_ctx, len);
504 if (out == NULL) {
505 return NULL;
508 ret = ctdb_connection_to_buf(out, len, conn, client_first);
509 if (ret != 0) {
510 talloc_free(out);
511 return NULL;
514 return out;
517 int ctdb_connection_from_string(const char *str, bool client_first,
518 struct ctdb_connection *conn)
520 char s[128];
521 char *t1 = NULL, *t2 = NULL;
522 size_t len;
523 ctdb_sock_addr *first = (client_first ? &conn->client : &conn->server);
524 ctdb_sock_addr *second = (client_first ? &conn->server : &conn->client);
525 int ret;
527 len = strlcpy(s, str, sizeof(s));
528 if (len >= sizeof(s)) {
529 return EINVAL;
532 t1 = strtok(s, " \t\n");
533 if (t1 == NULL) {
534 return EINVAL;
537 t2 = strtok(NULL, " \t\n\0");
538 if (t2 == NULL) {
539 return EINVAL;
542 ret = ctdb_sock_addr_from_string(t1, first, true);
543 if (ret != 0) {
544 return ret;
547 ret = ctdb_sock_addr_from_string(t2, second, true);
548 if (ret != 0) {
549 return ret;
552 ret = ctdb_sock_addr_cmp_family(first, second);
553 if (ret != 0) {
554 return EINVAL;
557 return 0;
560 int ctdb_connection_list_add(struct ctdb_connection_list *conn_list,
561 struct ctdb_connection *conn)
563 uint32_t len;
565 if (conn_list == NULL) {
566 return EINVAL;
569 /* Ensure array is big enough */
570 len = talloc_array_length(conn_list->conn);
571 if (conn_list->num == len) {
572 conn_list->conn = talloc_realloc(conn_list, conn_list->conn,
573 struct ctdb_connection,
574 len+128);
575 if (conn_list->conn == NULL) {
576 return ENOMEM;
580 conn_list->conn[conn_list->num] = *conn;
581 conn_list->num++;
583 return 0;
586 static int connection_cmp(const void *a, const void *b)
588 const struct ctdb_connection *conn_a = a;
589 const struct ctdb_connection *conn_b = b;
590 int ret;
592 ret = ctdb_sock_addr_cmp(&conn_a->server, &conn_b->server);
593 if (ret == 0) {
594 ret = ctdb_sock_addr_cmp(&conn_a->client, &conn_b->client);
597 return ret;
600 int ctdb_connection_list_sort(struct ctdb_connection_list *conn_list)
602 if (conn_list == NULL) {
603 return EINVAL;
606 if (conn_list->num > 0) {
607 qsort(conn_list->conn, conn_list->num,
608 sizeof(struct ctdb_connection), connection_cmp);
611 return 0;
614 const char *ctdb_connection_list_to_string(
615 TALLOC_CTX *mem_ctx,
616 struct ctdb_connection_list *conn_list, bool client_first)
618 uint32_t i;
619 char *out;
621 out = talloc_strdup(mem_ctx, "");
622 if (out == NULL) {
623 return NULL;
626 if (conn_list == NULL || conn_list->num == 0) {
627 return out;
630 for (i = 0; i < conn_list->num; i++) {
631 char buf[128];
632 int ret;
634 ret = ctdb_connection_to_buf(buf, sizeof(buf),
635 &conn_list->conn[i], client_first);
636 if (ret != 0) {
637 talloc_free(out);
638 return NULL;
641 out = talloc_asprintf_append(out, "%s\n", buf);
642 if (out == NULL) {
643 return NULL;
647 return out;
650 struct ctdb_connection_list_read_state {
651 struct ctdb_connection_list *list;
652 bool client_first;
655 static int ctdb_connection_list_read_line(char *line, void *private_data)
657 struct ctdb_connection_list_read_state *state =
658 (struct ctdb_connection_list_read_state *)private_data;
659 struct ctdb_connection conn;
660 int ret;
662 /* Skip empty lines */
663 if (line[0] == '\0') {
664 return 0;
667 /* Comment */
668 if (line[0] == '#') {
669 return 0;
672 ret = ctdb_connection_from_string(line, state->client_first, &conn);
673 if (ret != 0) {
674 return ret;
677 ret = ctdb_connection_list_add(state->list, &conn);
678 if (ret != 0) {
679 return ret;
682 return 0;
685 int ctdb_connection_list_read(TALLOC_CTX *mem_ctx,
686 int fd,
687 bool client_first,
688 struct ctdb_connection_list **conn_list)
690 struct ctdb_connection_list_read_state state;
691 int ret;
693 if (conn_list == NULL) {
694 return EINVAL;
697 state.list = talloc_zero(mem_ctx, struct ctdb_connection_list);
698 if (state.list == NULL) {
699 return ENOMEM;
702 state.client_first = client_first;
704 ret = line_read(fd,
705 128,
706 mem_ctx,
707 ctdb_connection_list_read_line,
708 &state,
709 NULL);
711 *conn_list = state.list;
713 return ret;