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/>.
21 #include "system/network.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)
38 static void listen_callback(struct ctdb_tunnel_context
*tctx
,
39 uint32_t srcnode
, uint32_t reqid
,
40 uint8_t *buf
, size_t buflen
,
43 struct listen_state
*state
= (struct listen_state
*)private_data
;
48 ret
= ctdb_stringn_pull(buf
, buflen
, state
->mem_ctx
, &msg
, &np
);
50 fprintf(stderr
, "Invalid tunnel message, ret=%d\n", ret
);
54 fprintf(stderr
, "%u: %s\n", srcnode
, msg
);
56 if (strcmp(msg
, "quit") == 0) {
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
;
70 state
.mem_ctx
= mem_ctx
;
73 ret
= ctdb_tunnel_setup(mem_ctx
, ev
, client
, TUNNEL_ID
,
74 listen_callback
, &state
, &tunnel
);
79 ctdb_client_wait(ev
, &state
.done
);
81 ret
= ctdb_tunnel_destroy(ev
, tunnel
);
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
;
105 ret
= ctdb_tunnel_setup(mem_ctx
, ev
, client
, TUNNEL_ID
,
106 send_callback
, NULL
, &tunnel
);
111 buflen
= ctdb_stringn_len(&msg
);
112 buf
= talloc_size(mem_ctx
, buflen
);
116 ctdb_stringn_push(&msg
, buf
, &np
);
118 ret
= ctdb_tunnel_request(mem_ctx
, ev
, tunnel
, destnode
,
119 tevent_timeval_zero(), buf
, buflen
, false);
124 ret
= ctdb_tunnel_destroy(ev
, tunnel
);
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
)
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
;
146 bool do_listen
= false;
147 bool do_send
= false;
149 if (argc
!= 3 && argc
!= 5) {
156 if (strcmp(argv
[2], "listen") == 0) {
158 } else if (strcmp(argv
[2], "send") == 0) {
172 mem_ctx
= talloc_new(NULL
);
173 if (mem_ctx
== NULL
) {
177 ev
= tevent_context_init(mem_ctx
);
179 talloc_free(mem_ctx
);
183 ret
= ctdb_client_init(mem_ctx
, ev
, socket
, &client
);
185 talloc_free(mem_ctx
);
190 ret
= cmd_listen(mem_ctx
, ev
, client
);
193 ret
= cmd_send(mem_ctx
, ev
, client
, pnn
, msg
);
196 talloc_free(mem_ctx
);