| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* socket.c TIPC socket functionality. |
| 4 |
* |
| 5 |
* Authors: Richard Alpe <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <errno.h> |
| 10 |
|
| 11 |
#include <linux/tipc.h> |
| 12 |
#include <linux/tipc_netlink.h> |
| 13 |
#include <linux/genetlink.h> |
| 14 |
#include <libmnl/libmnl.h> |
| 15 |
|
| 16 |
#include "mnl_utils.h" |
| 17 |
#include "cmdl.h" |
| 18 |
#include "msg.h" |
| 19 |
#include "socket.h" |
| 20 |
|
| 21 |
#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */ |
| 22 |
|
| 23 |
static int publ_list_cb(const struct nlmsghdr *nlh, void *data) |
| 24 |
{ |
| 25 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 26 |
struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
| 27 |
struct nlattr *attrs[TIPC_NLA_SOCK_MAX + 1] = {}; |
| 28 |
|
| 29 |
mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); |
| 30 |
if (!info[TIPC_NLA_PUBL]) |
| 31 |
return MNL_CB_ERROR; |
| 32 |
|
| 33 |
mnl_attr_parse_nested(info[TIPC_NLA_PUBL], parse_attrs, attrs); |
| 34 |
|
| 35 |
printf(" bound to {%u,%u,%u}\n", |
| 36 |
mnl_attr_get_u32(attrs[TIPC_NLA_PUBL_TYPE]), |
| 37 |
mnl_attr_get_u32(attrs[TIPC_NLA_PUBL_LOWER]), |
| 38 |
mnl_attr_get_u32(attrs[TIPC_NLA_PUBL_UPPER])); |
| 39 |
|
| 40 |
return MNL_CB_OK; |
| 41 |
} |
| 42 |
|
| 43 |
static int publ_list(uint32_t sock) |
| 44 |
{ |
| 45 |
struct mnlu_gen_socket sock_nlg; |
| 46 |
struct nlmsghdr *nlh; |
| 47 |
struct nlattr *nest; |
| 48 |
int err; |
| 49 |
|
| 50 |
err = mnlu_gen_socket_open(&sock_nlg, TIPC_GENL_V2_NAME, |
| 51 |
TIPC_GENL_V2_VERSION); |
| 52 |
if (err) |
| 53 |
return -1; |
| 54 |
|
| 55 |
nlh = mnlu_gen_socket_cmd_prepare(&sock_nlg, TIPC_NL_PUBL_GET, |
| 56 |
NLM_F_REQUEST | NLM_F_DUMP); |
| 57 |
if (!nlh) { |
| 58 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 59 |
mnlu_gen_socket_close(&sock_nlg); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
|
| 63 |
nest = mnl_attr_nest_start(nlh, TIPC_NLA_SOCK); |
| 64 |
mnl_attr_put_u32(nlh, TIPC_NLA_SOCK_REF, sock); |
| 65 |
mnl_attr_nest_end(nlh, nest); |
| 66 |
|
| 67 |
err = mnlu_gen_socket_sndrcv(&sock_nlg, nlh, publ_list_cb, NULL); |
| 68 |
mnlu_gen_socket_close(&sock_nlg); |
| 69 |
return err; |
| 70 |
} |
| 71 |
|
| 72 |
static int sock_list_cb(const struct nlmsghdr *nlh, void *data) |
| 73 |
{ |
| 74 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 75 |
struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
| 76 |
struct nlattr *attrs[TIPC_NLA_SOCK_MAX + 1] = {}; |
| 77 |
|
| 78 |
mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); |
| 79 |
if (!info[TIPC_NLA_SOCK]) |
| 80 |
return MNL_CB_ERROR; |
| 81 |
|
| 82 |
mnl_attr_parse_nested(info[TIPC_NLA_SOCK], parse_attrs, attrs); |
| 83 |
if (!attrs[TIPC_NLA_SOCK_REF]) |
| 84 |
return MNL_CB_ERROR; |
| 85 |
|
| 86 |
printf("socket %u\n", mnl_attr_get_u32(attrs[TIPC_NLA_SOCK_REF])); |
| 87 |
|
| 88 |
if (attrs[TIPC_NLA_SOCK_CON]) { |
| 89 |
uint32_t node; |
| 90 |
struct nlattr *con[TIPC_NLA_CON_MAX + 1] = {}; |
| 91 |
|
| 92 |
mnl_attr_parse_nested(attrs[TIPC_NLA_SOCK_CON], parse_attrs, con); |
| 93 |
node = mnl_attr_get_u32(con[TIPC_NLA_CON_NODE]); |
| 94 |
|
| 95 |
printf(" connected to %x:%u", node, |
| 96 |
mnl_attr_get_u32(con[TIPC_NLA_CON_SOCK])); |
| 97 |
|
| 98 |
if (con[TIPC_NLA_CON_FLAG]) |
| 99 |
printf(" via {%u,%u}\n", |
| 100 |
mnl_attr_get_u32(con[TIPC_NLA_CON_TYPE]), |
| 101 |
mnl_attr_get_u32(con[TIPC_NLA_CON_INST])); |
| 102 |
else |
| 103 |
printf("\n"); |
| 104 |
} else if (attrs[TIPC_NLA_SOCK_HAS_PUBL]) { |
| 105 |
publ_list(mnl_attr_get_u32(attrs[TIPC_NLA_SOCK_REF])); |
| 106 |
} |
| 107 |
|
| 108 |
return MNL_CB_OK; |
| 109 |
} |
| 110 |
|
| 111 |
static int cmd_socket_list(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 112 |
struct cmdl *cmdl, void *data) |
| 113 |
{ |
| 114 |
if (help_flag) { |
| 115 |
fprintf(stderr, "Usage: %s socket list\n", cmdl->argv[0]); |
| 116 |
return -EINVAL; |
| 117 |
} |
| 118 |
|
| 119 |
nlh = msg_init(TIPC_NL_SOCK_GET); |
| 120 |
if (!nlh) { |
| 121 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
return msg_dumpit(nlh, sock_list_cb, NULL); |
| 126 |
} |
| 127 |
|
| 128 |
void cmd_socket_help(struct cmdl *cmdl) |
| 129 |
{ |
| 130 |
fprintf(stderr, |
| 131 |
"Usage: %s socket COMMAND\n\n" |
| 132 |
"Commands:\n" |
| 133 |
" list - List sockets (ports)\n", |
| 134 |
cmdl->argv[0]); |
| 135 |
} |
| 136 |
|
| 137 |
int cmd_socket(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl, |
| 138 |
void *data) |
| 139 |
{ |
| 140 |
const struct cmd cmds[] = { |
| 141 |
{ "list", cmd_socket_list, NULL }, |
| 142 |
{ NULL } |
| 143 |
}; |
| 144 |
|
| 145 |
return run_cmd(nlh, cmd, cmds, cmdl, NULL); |
| 146 |
} |