ctdb-daemon: Add ctdb_vfork_exec()
[Samba.git] / ctdb / protocol / protocol_util.c
blobb91c652251f9cc0aeea1dc6f10402c436642ff2e
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>
24 #include <tdb.h>
26 #include "protocol.h"
27 #include "protocol_private.h"
28 #include "protocol_api.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 const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx, ctdb_sock_addr *addr)
118 char *cip;
120 cip = talloc_size(mem_ctx, 128);
121 if (cip == NULL) {
122 return "Memory Error";
125 switch (addr->sa.sa_family) {
126 case AF_INET:
127 inet_ntop(addr->ip.sin_family, &addr->ip.sin_addr,
128 cip, 128);
129 break;
131 case AF_INET6:
132 inet_ntop(addr->ip6.sin6_family, &addr->ip6.sin6_addr,
133 cip, 128);
134 break;
136 default:
137 sprintf(cip, "Unknown family %u", addr->sa.sa_family);
138 break;
141 return cip;
144 bool ctdb_sock_addr_same_ip(ctdb_sock_addr *addr1, ctdb_sock_addr *addr2)
146 if (addr1->sa.sa_family != addr2->sa.sa_family) {
147 return false;
150 switch (addr1->sa.sa_family) {
151 case AF_INET:
152 if (addr1->ip.sin_addr.s_addr != addr2->ip.sin_addr.s_addr) {
153 return false;
155 break;
157 case AF_INET6:
158 if (memcmp(addr1->ip6.sin6_addr.s6_addr,
159 addr2->ip6.sin6_addr.s6_addr, 16) != 0) {
160 return false;
162 break;
164 default:
165 return false;
168 return true;
171 bool ctdb_sock_addr_same(ctdb_sock_addr *addr1, ctdb_sock_addr *addr2)
173 if (! ctdb_sock_addr_same_ip(addr1, addr2)) {
174 return false;
177 switch (addr1->sa.sa_family) {
178 case AF_INET:
179 if (addr1->ip.sin_port != addr2->ip.sin_port) {
180 return false;
182 break;
184 case AF_INET6:
185 if (addr1->ip6.sin6_port != addr2->ip6.sin6_port) {
186 return false;
188 break;
190 default:
191 return false;
194 return true;