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