WHATSNEW: Add release notes for Samba 4.16.10.
[Samba.git] / ctdb / tests / src / tunnel_cmd.c
blob73a229784256857d883a6bb39a0846eed41d38d7
1 /*
2 CTDB tunnel test
4 Copyright (C) Amitay Isaacs 2017
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 <tevent.h>
26 #include "lib/util/tevent_unix.h"
28 #include "protocol/protocol_private.h"
29 #include "client/client.h"
31 #define TUNNEL_ID (CTDB_TUNNEL_TEST | 0xf0f0f0f0)
33 struct listen_state {
34 TALLOC_CTX *mem_ctx;
35 bool done;
38 static void listen_callback(struct ctdb_tunnel_context *tctx,
39 uint32_t srcnode, uint32_t reqid,
40 uint8_t *buf, size_t buflen,
41 void *private_data)
43 struct listen_state *state = (struct listen_state *)private_data;
44 const char *msg;
45 size_t np;
46 int ret;
48 ret = ctdb_stringn_pull(buf, buflen, state->mem_ctx, &msg, &np);
49 if (ret != 0) {
50 fprintf(stderr, "Invalid tunnel message, ret=%d\n", ret);
51 return;
54 fprintf(stderr, "%u: %s\n", srcnode, msg);
56 if (strcmp(msg, "quit") == 0) {
57 state->done = true;
60 talloc_free(discard_const(msg));
63 static int cmd_listen(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
64 struct ctdb_client_context *client)
66 struct ctdb_tunnel_context *tunnel;
67 struct listen_state state;
68 int ret;
70 state.mem_ctx = mem_ctx;
71 state.done = false;
73 ret = ctdb_tunnel_setup(mem_ctx, ev, client, TUNNEL_ID,
74 listen_callback, &state, &tunnel);
75 if (ret != 0) {
76 return ret;
79 ctdb_client_wait(ev, &state.done);
81 ret = ctdb_tunnel_destroy(ev, tunnel);
82 if (ret != 0) {
83 return ret;
86 return 0;
89 static void send_callback(struct ctdb_tunnel_context *tctx,
90 uint32_t srcnode, uint32_t reqid,
91 uint8_t *buf, size_t buflen, void *private_data)
93 fprintf(stderr, "send received a message - %u: %zu\n", srcnode, buflen);
96 static int cmd_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
97 struct ctdb_client_context *client,
98 uint32_t destnode, const char *msg)
100 struct ctdb_tunnel_context *tunnel;
101 uint8_t *buf;
102 size_t buflen, np;
103 int ret;
105 ret = ctdb_tunnel_setup(mem_ctx, ev, client, TUNNEL_ID,
106 send_callback, NULL, &tunnel);
107 if (ret != 0) {
108 return ret;
111 buflen = ctdb_stringn_len(&msg);
112 buf = talloc_size(mem_ctx, buflen);
113 if (buf == NULL) {
114 return ENOMEM;
116 ctdb_stringn_push(&msg, buf, &np);
118 ret = ctdb_tunnel_request(mem_ctx, ev, tunnel, destnode,
119 tevent_timeval_zero(), buf, buflen, false);
120 if (ret != 0) {
121 return ret;
124 ret = ctdb_tunnel_destroy(ev, tunnel);
125 if (ret != 0) {
126 return ret;
129 return 0;
132 static void usage(const char *cmd)
134 fprintf(stderr, "usage: %s <ctdb-socket> listen\n", cmd);
135 fprintf(stderr, "usage: %s <ctdb-socket> send <pnn> <msg>\n", cmd);
138 int main(int argc, const char **argv)
140 TALLOC_CTX *mem_ctx;
141 struct tevent_context *ev;
142 struct ctdb_client_context *client;
143 const char *socket = NULL, *msg = NULL;
144 uint32_t pnn = CTDB_UNKNOWN_PNN;
145 int ret;
146 bool do_listen = false;
147 bool do_send = false;
149 if (argc != 3 && argc != 5) {
150 usage(argv[0]);
151 exit(1);
154 socket = argv[1];
156 if (strcmp(argv[2], "listen") == 0) {
157 do_listen = true;
158 } else if (strcmp(argv[2], "send") == 0) {
159 if (argc != 5) {
160 usage(argv[0]);
161 exit(1);
164 pnn = atol(argv[3]);
165 msg = argv[4];
166 do_send = true;
167 } else {
168 usage(argv[0]);
169 exit(1);
172 mem_ctx = talloc_new(NULL);
173 if (mem_ctx == NULL) {
174 exit(1);
177 ev = tevent_context_init(mem_ctx);
178 if (ev == NULL) {
179 talloc_free(mem_ctx);
180 exit(1);
183 ret = ctdb_client_init(mem_ctx, ev, socket, &client);
184 if (ret != 0) {
185 talloc_free(mem_ctx);
186 exit(1);
189 if (do_listen) {
190 ret = cmd_listen(mem_ctx, ev, client);
192 if (do_send) {
193 ret = cmd_send(mem_ctx, ev, client, pnn, msg);
196 talloc_free(mem_ctx);
198 return ret;