diff --git a/tipc/link.c b/tipc/link.c index 4ae1c915..02f14aad 100644 --- a/tipc/link.c +++ b/tipc/link.c @@ -616,8 +616,7 @@ static void link_mon_print_non_applied(uint16_t applied, uint16_t member_cnt, if (i != applied) printf(","); - sprintf(addr_str, "%u.%u.%u:", tipc_zone(members[i]), - tipc_cluster(members[i]), tipc_node(members[i])); + sprintf(addr_str, "%x:", members[i]); state = map_get(up_map, i) ? 'U' : 'D'; printf("%s%c", addr_str, state); } diff --git a/tipc/misc.c b/tipc/misc.c index 80912228..16849f18 100644 --- a/tipc/misc.c +++ b/tipc/misc.c @@ -12,7 +12,7 @@ #include #include #include - +#include #include "misc.h" #define IN_RANGE(val, low, high) ((val) <= (high) && (val) >= (low)) @@ -33,3 +33,79 @@ uint32_t str2addr(char *str) fprintf(stderr, "invalid network address \"%s\"\n", str); return 0; } + +static int is_hex(char *arr, int last) +{ + int i; + + while (!arr[last]) + last--; + + for (i = 0; i <= last; i++) { + if (!IN_RANGE(arr[i], '0', '9') && + !IN_RANGE(arr[i], 'a', 'f') && + !IN_RANGE(arr[i], 'A', 'F')) + return 0; + } + return 1; +} + +static int is_name(char *arr, int last) +{ + int i; + char c; + + while (!arr[last]) + last--; + + if (last > 15) + return 0; + + for (i = 0; i <= last; i++) { + c = arr[i]; + if (!IN_RANGE(c, '0', '9') && !IN_RANGE(c, 'a', 'z') && + !IN_RANGE(c, 'A', 'Z') && c != '-' && c != '_' && + c != '.' && c != ':' && c != '@') + return 0; + } + return 1; +} + +int str2nodeid(char *str, uint8_t *id) +{ + int len = strlen(str); + int i; + + if (len > 32) + return -1; + + if (is_name(str, len - 1)) { + memcpy(id, str, len); + return 0; + } + if (!is_hex(str, len - 1)) + return -1; + + str[len] = '0'; + for (i = 0; i < 16; i++) { + if (sscanf(&str[2 * i], "%2hhx", &id[i]) != 1) + break; + } + return 0; +} + +void nodeid2str(uint8_t *id, char *str) +{ + int i; + + if (is_name((char *)id, 15)) { + memcpy(str, id, 16); + return; + } + + for (i = 0; i < 16; i++) + sprintf(&str[2 * i], "%02x", id[i]); + + for (i = 31; str[i] == '0'; i--) + str[i] = 0; +} diff --git a/tipc/misc.h b/tipc/misc.h index 585df745..6e8afddf 100644 --- a/tipc/misc.h +++ b/tipc/misc.h @@ -15,5 +15,7 @@ #include uint32_t str2addr(char *str); +int str2nodeid(char *str, uint8_t *id); +void nodeid2str(uint8_t *id, char *str); #endif diff --git a/tipc/nametable.c b/tipc/nametable.c index 770a644c..2578940f 100644 --- a/tipc/nametable.c +++ b/tipc/nametable.c @@ -26,7 +26,6 @@ static int nametable_show_cb(const struct nlmsghdr *nlh, void *data) { int *iteration = data; - char port_id[PORTID_STR_LEN]; struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); struct nlattr *info[TIPC_NLA_MAX + 1] = {}; struct nlattr *attrs[TIPC_NLA_NAME_TABLE_MAX + 1] = {}; @@ -46,22 +45,17 @@ static int nametable_show_cb(const struct nlmsghdr *nlh, void *data) return MNL_CB_ERROR; if (!*iteration) - printf("%-10s %-10s %-10s %-26s %-10s\n", - "Type", "Lower", "Upper", "Port Identity", + printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", + "Type", "Lower", "Upper", "Node", "Port", "Publication Scope"); (*iteration)++; - snprintf(port_id, sizeof(port_id), "<%u.%u.%u:%u>", - tipc_zone(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE])), - tipc_cluster(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE])), - tipc_node(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE])), - mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF])); - - printf("%-10u %-10u %-10u %-26s %-12u", + printf("%-10u %-10u %-10u %-10x %-10u %-12u", mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]), mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]), mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]), - port_id, + mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), + mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]), mnl_attr_get_u32(publ[TIPC_NLA_PUBL_KEY])); printf("%s\n", scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]); diff --git a/tipc/node.c b/tipc/node.c index fe085aec..b73b644c 100644 --- a/tipc/node.c +++ b/tipc/node.c @@ -40,10 +40,7 @@ static int node_list_cb(const struct nlmsghdr *nlh, void *data) return MNL_CB_ERROR; addr = mnl_attr_get_u32(attrs[TIPC_NLA_NODE_ADDR]); - printf("<%u.%u.%u>: ", - tipc_zone(addr), - tipc_cluster(addr), - tipc_node(addr)); + printf("%x: ", addr); if (attrs[TIPC_NLA_NODE_UP]) printf("up\n"); @@ -123,14 +120,94 @@ static int cmd_node_get_addr(struct nlmsghdr *nlh, const struct cmd *cmd, } close(sk); - printf("<%u.%u.%u>\n", - tipc_zone(addr.addr.id.node), - tipc_cluster(addr.addr.id.node), - tipc_node(addr.addr.id.node)); - + printf("%x\n", addr.addr.id.node); return 0; } +static int cmd_node_set_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd, + struct cmdl *cmdl, void *data) +{ + char buf[MNL_SOCKET_BUFFER_SIZE]; + uint8_t id[16] = {0,}; + uint64_t *w0 = (uint64_t *) &id[0]; + uint64_t *w1 = (uint64_t *) &id[8]; + struct nlattr *nest; + char *str; + + if (cmdl->argc != cmdl->optind + 1) { + fprintf(stderr, "Usage: %s node set nodeid NODE_ID\n", + cmdl->argv[0]); + return -EINVAL; + } + + str = shift_cmdl(cmdl); + if (str2nodeid(str, id)) { + fprintf(stderr, "Invalid node identity\n"); + return -EINVAL; + } + + nlh = msg_init(buf, TIPC_NL_NET_SET); + if (!nlh) { + fprintf(stderr, "error, message initialisation failed\n"); + return -1; + } + nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET); + mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID, *w0); + mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID_W1, *w1); + mnl_attr_nest_end(nlh, nest); + return msg_doit(nlh, NULL, NULL); +} + +static int nodeid_get_cb(const struct nlmsghdr *nlh, void *data) +{ + struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); + struct nlattr *info[TIPC_NLA_MAX + 1] = {}; + struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {}; + char str[33] = {0,}; + uint8_t id[16] = {0,}; + uint64_t *w0 = (uint64_t *) &id[0]; + uint64_t *w1 = (uint64_t *) &id[8]; + int i; + + mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); + if (!info[TIPC_NLA_NET]) + return MNL_CB_ERROR; + + mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs); + if (!attrs[TIPC_NLA_NET_ID]) + return MNL_CB_ERROR; + + *w0 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID]); + *w1 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]); + nodeid2str(id, str); + printf("Node Identity Hash\n"); + printf("%s", str); + for (i = strlen(str); i <= 33; i++) + printf(" "); + cmd_node_get_addr(NULL, NULL, NULL, NULL); + return MNL_CB_OK; +} + +static int cmd_node_get_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd, + struct cmdl *cmdl, void *data) +{ + char buf[MNL_SOCKET_BUFFER_SIZE]; + + if (help_flag) { + (cmd->help)(cmdl); + return -EINVAL; + } + + nlh = msg_init(buf, TIPC_NL_NET_GET); + if (!nlh) { + fprintf(stderr, "error, message initialisation failed\n"); + return -1; + } + + return msg_dumpit(nlh, nodeid_get_cb, NULL); +} + + static int netid_get_cb(const struct nlmsghdr *nlh, void *data) { struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); @@ -204,8 +281,8 @@ static void cmd_node_set_help(struct cmdl *cmdl) fprintf(stderr, "Usage: %s node set PROPERTY\n\n" "PROPERTIES\n" - " address ADDRESS - Set local address\n" - " netid NETID - Set local netid\n", + " identity NODEID - Set node identity\n" + " clusterid CLUSTERID - Set local cluster id\n", cmdl->argv[0]); } @@ -213,8 +290,10 @@ static int cmd_node_set(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl, void *data) { const struct cmd cmds[] = { - { "address", cmd_node_set_addr, NULL }, + { "address", cmd_node_set_addr, NULL }, + { "identity", cmd_node_set_nodeid, NULL }, { "netid", cmd_node_set_netid, NULL }, + { "clusterid", cmd_node_set_netid, NULL }, { NULL } }; @@ -226,8 +305,8 @@ static void cmd_node_get_help(struct cmdl *cmdl) fprintf(stderr, "Usage: %s node get PROPERTY\n\n" "PROPERTIES\n" - " address - Get local address\n" - " netid - Get local netid\n", + " identity - Get node identity\n" + " clusterid - Get local clusterid\n", cmdl->argv[0]); } @@ -236,7 +315,9 @@ static int cmd_node_get(struct nlmsghdr *nlh, const struct cmd *cmd, { const struct cmd cmds[] = { { "address", cmd_node_get_addr, NULL }, + { "identity", cmd_node_get_nodeid, NULL }, { "netid", cmd_node_get_netid, NULL }, + { "clusterid", cmd_node_get_netid, NULL }, { NULL } }; diff --git a/tipc/socket.c b/tipc/socket.c index 48ba8215..852984ec 100644 --- a/tipc/socket.c +++ b/tipc/socket.c @@ -84,8 +84,7 @@ static int sock_list_cb(const struct nlmsghdr *nlh, void *data) mnl_attr_parse_nested(attrs[TIPC_NLA_SOCK_CON], parse_attrs, con); node = mnl_attr_get_u32(con[TIPC_NLA_CON_NODE]); - printf(" connected to <%u.%u.%u:%u>", tipc_zone(node), - tipc_cluster(node), tipc_node(node), + printf(" connected to %x:%u", node, mnl_attr_get_u32(con[TIPC_NLA_CON_SOCK])); if (con[TIPC_NLA_CON_FLAG])