ctdb-killtcp: New helper ctdb_killtcp
[vlendec/samba-autobuild/.git] / ctdb / tools / ctdb_killtcp.c
1 /*
2    CTDB TCP connection killing utility
3
4    Copyright (C) Martin Schwenke <martin@meltin.net> 2016
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/network.h"
22
23 #include "lib/util/debug.h"
24
25 #include "protocol/protocol.h"
26
27 #include "common/system.h"
28 #include "common/logging.h"
29
30 #include "server/killtcp.h"
31
32 static const char *prog;
33
34 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
35 {
36         bool *done = killtcp->destructor_data;
37         *done = true;
38
39         return 0;
40 }
41
42 static void usage(void)
43 {
44         printf("usage: %s <interface> [ <srcip:port> <dstip:port> ]\n", prog);
45         exit(1);
46 }
47
48 int main(int argc, char **argv)
49 {
50         struct ctdb_connection conn;
51         struct ctdb_kill_tcp *killtcp = NULL;
52         struct tevent_context *ev = NULL;
53         struct TALLOC_CONTEXT *mem_ctx = NULL;
54         struct ctdb_connection *conns = NULL;
55         bool done;
56         int num = 0;
57         int i, ret;
58
59         prog = argv[0];
60
61         if (argc != 2 && argc != 4) {
62                 usage();
63         }
64
65         if (argc == 4) {
66                 if (!parse_ip_port(argv[2], &conn.src)) {
67                         DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[2]));
68                         goto fail;
69                 }
70
71                 if (!parse_ip_port(argv[3], &conn.dst)) {
72                         DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[3]));
73                         goto fail;
74                 }
75
76                 conns = &conn;
77                 num = 1;
78         } else {
79                 ret = ctdb_parse_connections(stdin, mem_ctx, &num, &conns);
80                 if (ret != 0) {
81                         DEBUG(DEBUG_ERR,
82                               ("Unable to parse connections [%s]\n",
83                                strerror(ret)));
84                         goto fail;
85                 }
86         }
87
88         mem_ctx = talloc_new(NULL);
89         if (mem_ctx == NULL) {
90                 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
91                 goto fail;
92         }
93
94         ev = tevent_context_init(mem_ctx);
95         if (ev == NULL) {
96                 DEBUG(DEBUG_ERR, ("Failed to initialise tevent\n"));
97                 goto fail;
98         }
99
100         if (num == 0) {
101                 /* No connections, done! */
102                 talloc_free(mem_ctx);
103                 return 0;
104         }
105
106         for (i = 0; i < num; i++) {
107                 ret = ctdb_killtcp(ev, mem_ctx, argv[1],
108                                    &conns[i].src, &conns[i].dst,
109                                    &killtcp);
110                 if (ret != 0) {
111                         DEBUG(DEBUG_ERR, ("Unable to killtcp\n"));
112                         goto fail;
113                 }
114         }
115
116         done = false;
117         killtcp->destructor_data = &done;
118         talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
119
120         while (!done) {
121                 tevent_loop_once(ev);
122         }
123
124         talloc_free(mem_ctx);
125
126         return 0;
127
128 fail:
129         TALLOC_FREE(mem_ctx);
130         return -1;
131 }