1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

ctdb-protocol: Optionally print port for address printing functions

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
This commit is contained in:
Martin Schwenke 2017-09-11 15:00:10 +10:00 committed by Martin Schwenke
parent de9f05e8fe
commit 93668f5026
11 changed files with 107 additions and 70 deletions

View File

@ -111,7 +111,8 @@ enum ctdb_event ctdb_event_from_string(const char *event_str)
return CTDB_EVENT_MAX;
}
int ctdb_sock_addr_to_buf(char *buf, socklen_t buflen, ctdb_sock_addr *addr)
int ctdb_sock_addr_to_buf(char *buf, socklen_t buflen,
ctdb_sock_addr *addr, bool with_port)
{
const char *t;
@ -137,10 +138,22 @@ int ctdb_sock_addr_to_buf(char *buf, socklen_t buflen, ctdb_sock_addr *addr)
break;
}
if (with_port) {
size_t len = strlen(buf);
int ret;
ret = snprintf(buf+len, buflen-len,
":%u", ctdb_sock_addr_port(addr));
if (ret >= buflen-len) {
return ENOSPC;
}
}
return 0;
}
const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx, ctdb_sock_addr *addr)
const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx,
ctdb_sock_addr *addr, bool with_port)
{
size_t len = 64;
char *cip;
@ -152,7 +165,7 @@ const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx, ctdb_sock_addr *addr)
return NULL;
}
ret = ctdb_sock_addr_to_buf(cip, len, addr);
ret = ctdb_sock_addr_to_buf(cip, len, addr, with_port);
if (ret != 0) {
talloc_free(cip);
return NULL;

View File

@ -31,12 +31,14 @@ const char *ctdb_event_to_string(enum ctdb_event event);
enum ctdb_event ctdb_event_from_string(const char *event_str);
/*
* buflen must be long enough to hold the longest possible address.
* For example, 1122:3344:5566:7788:99aa:bbcc:ddee:ff00.
* buflen must be long enough to hold the longest possible "address:port".
* For example, 1122:3344:5566:7788:99aa:bbcc:ddee:ff00:12345.
* 64 is sane value for buflen.
*/
int ctdb_sock_addr_to_buf(char *buf, socklen_t buflen, ctdb_sock_addr *addr);
const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx, ctdb_sock_addr *addr);
int ctdb_sock_addr_to_buf(char *buf, socklen_t buflen,
ctdb_sock_addr *addr, bool with_port);
const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx,
ctdb_sock_addr *addr, bool with_port);
unsigned int ctdb_sock_addr_port(ctdb_sock_addr *addr);
void ctdb_sock_addr_set_port(ctdb_sock_addr *addr, unsigned int port);
int ctdb_sock_addr_cmp_ip(const ctdb_sock_addr *addr1,

View File

@ -295,7 +295,8 @@ static struct tevent_req *release_ip_send(TALLOC_CTX *mem_ctx,
substate->req = req;
substate->ip_str = ctdb_sock_addr_to_string(substate,
&tmp_ip->addr);
&tmp_ip->addr,
false);
if (tevent_req_nomem(substate->ip_str, req)) {
return tevent_req_post(req, ev);
}
@ -479,7 +480,8 @@ static struct tevent_req *take_ip_send(TALLOC_CTX *mem_ctx,
substate->pnn = tmp_ip->pnn;
substate->ip_str = ctdb_sock_addr_to_string(substate,
&tmp_ip->addr);
&tmp_ip->addr,
false);
if (tevent_req_nomem(substate->ip_str, req)) {
return tevent_req_post(req, ev);
}

View File

@ -113,7 +113,8 @@ int find_takeover_node(struct ipalloc_state *ipalloc_state,
if (pnn == -1) {
DEBUG(DEBUG_WARNING,(__location__ " Could not find node to take over public address '%s'\n",
ctdb_sock_addr_to_string(ipalloc_state,
&ip->addr)));
&ip->addr,
false)));
return -1;
}
@ -164,7 +165,8 @@ void basic_allocate_unassigned(struct ipalloc_state *ipalloc_state)
DEBUG(DEBUG_WARNING,
("Failed to find node to cover ip %s\n",
ctdb_sock_addr_to_string(ipalloc_state,
&t->addr)));
&t->addr,
false)));
}
}
}
@ -184,8 +186,9 @@ void unassign_unsuitable_ips(struct ipalloc_state *ipalloc_state)
if (!can_node_host_ip(ipalloc_state, t->pnn, t) != 0) {
/* this node can not serve this ip. */
DEBUG(DEBUG_DEBUG,("Unassign IP: %s from %d\n",
ctdb_sock_addr_to_string(ipalloc_state,
&t->addr),
ctdb_sock_addr_to_string(
ipalloc_state,
&t->addr, false),
t->pnn));
t->pnn = -1;
}

View File

@ -242,7 +242,8 @@ static void lcp2_allocate_unassigned(struct ipalloc_state *ipalloc_state,
DEBUG(DEBUG_DEBUG,
(" %s -> %d [+%d]\n",
ctdb_sock_addr_to_string(ipalloc_state,
&(t->addr)),
&(t->addr),
false),
dstnode,
dstimbl - lcp2_imbalances[dstnode]));
@ -266,7 +267,7 @@ static void lcp2_allocate_unassigned(struct ipalloc_state *ipalloc_state,
DEBUG(DEBUG_INFO,(" %s -> %d [+%d]\n",
ctdb_sock_addr_to_string(
ipalloc_state,
&(minip->addr)),
&(minip->addr), false),
minnode,
mindsum));
}
@ -289,7 +290,8 @@ static void lcp2_allocate_unassigned(struct ipalloc_state *ipalloc_state,
DEBUG(DEBUG_WARNING,
("Failed to find node to cover ip %s\n",
ctdb_sock_addr_to_string(ipalloc_state,
&t->addr)));
&t->addr,
false)));
}
}
}
@ -360,7 +362,8 @@ static bool lcp2_failback_candidate(struct ipalloc_state *ipalloc_state,
DEBUG(DEBUG_DEBUG,(" %d [%d] -> %s -> %d [+%d]\n",
srcnode, -srcdsum,
ctdb_sock_addr_to_string(
ipalloc_state, &(t->addr)),
ipalloc_state,
&(t->addr), false),
dstnode, dstdsum));
if ((dstimbl < lcp2_imbalances[srcnode]) &&
@ -382,7 +385,8 @@ static bool lcp2_failback_candidate(struct ipalloc_state *ipalloc_state,
DEBUG(DEBUG_INFO,
("%d [%d] -> %s -> %d [+%d]\n",
srcnode, minsrcimbl - lcp2_imbalances[srcnode],
ctdb_sock_addr_to_string(ipalloc_state, &(minip->addr)),
ctdb_sock_addr_to_string(ipalloc_state,
&(minip->addr), false),
mindstnode, mindstimbl - lcp2_imbalances[mindstnode]));

View File

@ -95,7 +95,7 @@ try_again:
DEBUG(DEBUG_WARNING,
(__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
ctdb_sock_addr_to_string(ipalloc_state,
&t->addr)));
&t->addr, false)));
continue;
}

View File

@ -39,7 +39,7 @@ static void print_ctdb_public_ip_list(TALLOC_CTX *mem_ctx,
{
while (ips) {
printf("%s %d\n",
ctdb_sock_addr_to_string(mem_ctx, &(ips->addr)),
ctdb_sock_addr_to_string(mem_ctx, &(ips->addr), false),
ips->pnn);
ips = ips->next;
}

View File

@ -2067,7 +2067,7 @@ static void control_release_ip(TALLOC_CTX *mem_ctx,
if (ctdb->known_ips == NULL) {
D_INFO("RELEASE_IP %s - not a public IP\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr));
ctdb_sock_addr_to_string(mem_ctx, &ip->addr, false));
goto done;
}
@ -2082,14 +2082,15 @@ static void control_release_ip(TALLOC_CTX *mem_ctx,
}
if (t == NULL) {
D_INFO("RELEASE_IP %s - not a public IP\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr));
ctdb_sock_addr_to_string(mem_ctx, &ip->addr, false));
goto done;
}
if (t->pnn != header->destnode) {
if (header->destnode == ip->pnn) {
D_ERR("error: RELEASE_IP %s - to TAKE_IP node %d\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr),
ctdb_sock_addr_to_string(mem_ctx,
&ip->addr, false),
ip->pnn);
reply.status = -1;
reply.errmsg = "RELEASE_IP to TAKE_IP node";
@ -2098,12 +2099,12 @@ static void control_release_ip(TALLOC_CTX *mem_ctx,
}
D_INFO("RELEASE_IP %s - to node %d - redundant\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr),
ctdb_sock_addr_to_string(mem_ctx, &ip->addr, false),
ip->pnn);
t->pnn = ip->pnn;
} else {
D_NOTICE("RELEASE_IP %s - to node %d\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr),
ctdb_sock_addr_to_string(mem_ctx, &ip->addr, false),
ip->pnn);
t->pnn = ip->pnn;
}
@ -2132,7 +2133,7 @@ static void control_takeover_ip(TALLOC_CTX *mem_ctx,
if (ctdb->known_ips == NULL) {
D_INFO("TAKEOVER_IP %s - not a public IP\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr));
ctdb_sock_addr_to_string(mem_ctx, &ip->addr, false));
goto done;
}
@ -2147,16 +2148,16 @@ static void control_takeover_ip(TALLOC_CTX *mem_ctx,
}
if (t == NULL) {
D_INFO("TAKEOVER_IP %s - not a public IP\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr));
ctdb_sock_addr_to_string(mem_ctx, &ip->addr, false));
goto done;
}
if (t->pnn == header->destnode) {
D_INFO("TAKEOVER_IP %s - redundant\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr));
ctdb_sock_addr_to_string(mem_ctx, &ip->addr, false));
} else {
D_NOTICE("TAKEOVER_IP %s\n",
ctdb_sock_addr_to_string(mem_ctx, &ip->addr));
ctdb_sock_addr_to_string(mem_ctx, &ip->addr, false));
t->pnn = ip->pnn;
}
@ -2545,7 +2546,7 @@ static void control_get_public_ip_info(TALLOC_CTX *mem_ctx,
if (i == known->num) {
D_ERR("GET_PUBLIC_IP_INFO: not known public IP %s\n",
ctdb_sock_addr_to_string(mem_ctx, addr));
ctdb_sock_addr_to_string(mem_ctx, addr, false));
reply.status = -1;
reply.errmsg = "Unknown address";
goto done;

View File

@ -29,13 +29,19 @@
#include "common/system_util.c"
/* Test parsing of IPs, conversion to string */
static void test_sock_addr_to_string(const char *ip)
static void test_sock_addr_to_string(const char *ip, bool with_port)
{
ctdb_sock_addr sa;
const char *s;
bool status;
assert(parse_ip(ip, NULL, 0, &sa));
s = ctdb_sock_addr_to_string(NULL, &sa);
if (with_port) {
status = parse_ip_port(ip, &sa);
} else {
status = parse_ip(ip, NULL, 0, &sa);
}
assert(status);
s = ctdb_sock_addr_to_string(NULL, &sa, with_port);
assert(strcmp(ip, s) == 0);
talloc_free(discard_const(s));
}
@ -59,11 +65,18 @@ static void test_sock_addr_cmp(const char *ip1, const char *ip2, int res)
int main(int argc, char *argv[])
{
test_sock_addr_to_string("0.0.0.0");
test_sock_addr_to_string("127.0.0.1");
test_sock_addr_to_string("::1");
test_sock_addr_to_string("192.168.2.1");
test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136");
test_sock_addr_to_string("0.0.0.0", false);
test_sock_addr_to_string("127.0.0.1", false);
test_sock_addr_to_string("::1", false);
test_sock_addr_to_string("192.168.2.1", false);
test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136", false);
test_sock_addr_to_string("0.0.0.0:0", true);
test_sock_addr_to_string("127.0.0.1:123", true);
test_sock_addr_to_string("::1:234", true);
test_sock_addr_to_string("192.168.2.1:123", true);
test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136:234", true);
test_sock_addr_cmp("127.0.0.1", "127.0.0.1" , 0);
test_sock_addr_cmp("127.0.0.1", "127.0.0.2" , -1);

View File

@ -788,7 +788,7 @@ static void print_nodemap_machine(TALLOC_CTX *mem_ctx,
printf("%s%u%s%s%s%d%s%d%s%d%s%d%s%d%s%d%s%d%s%c%s\n",
options.sep,
node->pnn, options.sep,
ctdb_sock_addr_to_string(mem_ctx, &node->addr),
ctdb_sock_addr_to_string(mem_ctx, &node->addr, false),
options.sep,
!! (node->flags & NODE_FLAGS_DISCONNECTED), options.sep,
!! (node->flags & NODE_FLAGS_BANNED), options.sep,
@ -835,7 +835,7 @@ static void print_nodemap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
printf("pnn:%u %-16s %s%s\n",
node->pnn,
ctdb_sock_addr_to_string(mem_ctx, &node->addr),
ctdb_sock_addr_to_string(mem_ctx, &node->addr, false),
partially_online(mem_ctx, ctdb, node) ?
"PARTIALLYONLINE" :
pretty_print_flags(mem_ctx, node->flags),
@ -1449,12 +1449,12 @@ static void print_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
if (options.machinereadable == 1) {
printf("%s%s%s%d%s", options.sep,
ctdb_sock_addr_to_string(
mem_ctx, &ips->ip[i].addr),
mem_ctx, &ips->ip[i].addr, false),
options.sep,
(int)ips->ip[i].pnn, options.sep);
} else {
printf("%s", ctdb_sock_addr_to_string(
mem_ctx, &ips->ip[i].addr));
mem_ctx, &ips->ip[i].addr, false));
}
if (options.verbose == 0) {
@ -1731,11 +1731,11 @@ static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
}
printf("Public IP[%s] info on node %u\n",
ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr, false),
ctdb->cmd_pnn);
printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr, false),
ipinfo->ip.pnn, ipinfo->ifaces->num);
for (i=0; i<ipinfo->ifaces->num; i++) {
@ -3082,28 +3082,27 @@ static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
for (i=0; i<tickles->num; i++) {
printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
ctdb_sock_addr_to_string(
mem_ctx, &tickles->conn[i].src),
mem_ctx, &tickles->conn[i].src, false),
options.sep,
ntohs(tickles->conn[i].src.ip.sin_port),
options.sep,
ctdb_sock_addr_to_string(
mem_ctx, &tickles->conn[i].dst),
mem_ctx, &tickles->conn[i].dst, false),
options.sep,
ntohs(tickles->conn[i].dst.ip.sin_port),
options.sep);
}
} else {
printf("Connections for IP: %s\n",
ctdb_sock_addr_to_string(mem_ctx, &tickles->addr));
ctdb_sock_addr_to_string(mem_ctx,
&tickles->addr, false));
printf("Num connections: %u\n", tickles->num);
for (i=0; i<tickles->num; i++) {
printf("SRC: %s:%u DST: %s:%u\n",
printf("SRC: %s DST: %s\n",
ctdb_sock_addr_to_string(
mem_ctx, &tickles->conn[i].src),
ntohs(tickles->conn[i].src.ip.sin_port),
mem_ctx, &tickles->conn[i].src, true),
ctdb_sock_addr_to_string(
mem_ctx, &tickles->conn[i].dst),
ntohs(tickles->conn[i].dst.ip.sin_port));
mem_ctx, &tickles->conn[i].dst, true));
}
}
@ -3348,12 +3347,12 @@ static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
printf("%s%u%s%s%s\n", options.sep,
nodemap->node[i].pnn, options.sep,
ctdb_sock_addr_to_string(
mem_ctx, &nodemap->node[i].addr),
mem_ctx, &nodemap->node[i].addr, false),
options.sep);
} else {
printf("%s\n",
ctdb_sock_addr_to_string(
mem_ctx, &nodemap->node[i].addr));
mem_ctx, &nodemap->node[i].addr, false));
}
}
@ -3401,7 +3400,7 @@ static int check_node_file_changes(TALLOC_CTX *mem_ctx,
"Node %u (%s) missing from nodes file\n",
nm->node[i].pnn,
ctdb_sock_addr_to_string(
mem_ctx, &nm->node[i].addr));
mem_ctx, &nm->node[i].addr, false));
check_failed = true;
continue;
}
@ -3421,9 +3420,11 @@ static int check_node_file_changes(TALLOC_CTX *mem_ctx,
" (was %s, now %s)\n",
nm->node[i].pnn,
ctdb_sock_addr_to_string(
mem_ctx, &nm->node[i].addr),
mem_ctx,
&nm->node[i].addr, false),
ctdb_sock_addr_to_string(
mem_ctx, &fnm->node[i].addr));
mem_ctx,
&fnm->node[i].addr, false));
check_failed = true;
} else {
if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
@ -3702,7 +3703,7 @@ static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
if (i == pubip_list->num) {
fprintf(stderr, "Node %u CANNOT host IP address %s\n",
pnn, ctdb_sock_addr_to_string(mem_ctx, addr));
pnn, ctdb_sock_addr_to_string(mem_ctx, addr, false));
return 1;
}
@ -3826,7 +3827,8 @@ static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
for (i=0; i<pubip_list->num; i++) {
if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
fprintf(stderr, "Node already knows about IP %s\n",
ctdb_sock_addr_to_string(mem_ctx, &addr));
ctdb_sock_addr_to_string(mem_ctx,
&addr, false));
return 0;
}
}
@ -3895,7 +3897,7 @@ static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
if (i == pubip_list->num) {
fprintf(stderr, "Node does not know about IP address %s\n",
ctdb_sock_addr_to_string(mem_ctx, &addr));
ctdb_sock_addr_to_string(mem_ctx, &addr, false));
return 0;
}

View File

@ -131,11 +131,10 @@ static void capture_tcp_handler(struct tevent_context *ev,
if (window == htons(1234) && (rst || seq == 0)) {
/* Ignore packets that we sent! */
DEBUG(DEBUG_DEBUG,
("Ignoring packet with dst=%s:%d, src=%s:%d, seq=%"PRIu32", ack_seq=%"PRIu32", rst=%d, window=%"PRIu16"\n",
ctdb_sock_addr_to_string(killtcp, &dst),
ntohs(dst.ip.sin_port),
ctdb_sock_addr_to_string(killtcp, &src),
ntohs(src.ip.sin_port),
("Ignoring packet with dst=%s, src=%s, seq=%"PRIu32", "
"ack_seq=%"PRIu32", rst=%d, window=%"PRIu16"\n",
ctdb_sock_addr_to_string(killtcp, &dst, true),
ctdb_sock_addr_to_string(killtcp, &src, true),
seq, ack_seq, rst, ntohs(window)));
return;
}
@ -153,11 +152,9 @@ static void capture_tcp_handler(struct tevent_context *ev,
/* This connection has been tickled! RST it and remove it
* from the list. */
DEBUG(DEBUG_INFO,
("Sending a TCP RST to kill connection (%s:%d) -> %s:%d\n",
ctdb_sock_addr_to_string(con, &con->src_addr),
ntohs(con->src_addr.ip.sin_port),
ctdb_sock_addr_to_string(con, &con->dst_addr),
ntohs(con->dst_addr.ip.sin_port)));
("Sending a TCP RST to kill connection (%s) -> %s\n",
ctdb_sock_addr_to_string(con, &con->src_addr, true),
ctdb_sock_addr_to_string(con, &con->dst_addr, true)));
ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
talloc_free(con);