]>
git.proxmox.com Git - mirror_iproute2.git/blob - tipc/socket.c
2 * socket.c TIPC socket functionality.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Richard Alpe <richard.alpe@ericsson.com>
15 #include <linux/tipc.h>
16 #include <linux/tipc_netlink.h>
17 #include <linux/genetlink.h>
18 #include <libmnl/libmnl.h>
24 #define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */
26 static int publ_list_cb(const struct nlmsghdr
*nlh
, void *data
)
28 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
29 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
30 struct nlattr
*attrs
[TIPC_NLA_SOCK_MAX
+ 1] = {};
32 mnl_attr_parse(nlh
, sizeof(*genl
), parse_attrs
, info
);
33 if (!info
[TIPC_NLA_PUBL
])
36 mnl_attr_parse_nested(info
[TIPC_NLA_PUBL
], parse_attrs
, attrs
);
38 printf(" bound to {%u,%u,%u}\n",
39 mnl_attr_get_u32(attrs
[TIPC_NLA_PUBL_TYPE
]),
40 mnl_attr_get_u32(attrs
[TIPC_NLA_PUBL_LOWER
]),
41 mnl_attr_get_u32(attrs
[TIPC_NLA_PUBL_UPPER
]));
46 static int publ_list(uint32_t sock
)
49 char buf
[MNL_SOCKET_BUFFER_SIZE
];
52 if (!(nlh
= msg_init(buf
, TIPC_NL_PUBL_GET
))) {
53 fprintf(stderr
, "error, message initialisation failed\n");
57 nest
= mnl_attr_nest_start(nlh
, TIPC_NLA_SOCK
);
58 mnl_attr_put_u32(nlh
, TIPC_NLA_SOCK_REF
, sock
);
59 mnl_attr_nest_end(nlh
, nest
);
61 return msg_dumpit(nlh
, publ_list_cb
, NULL
);
64 static int sock_list_cb(const struct nlmsghdr
*nlh
, void *data
)
66 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
67 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
68 struct nlattr
*attrs
[TIPC_NLA_SOCK_MAX
+ 1] = {};
70 mnl_attr_parse(nlh
, sizeof(*genl
), parse_attrs
, info
);
71 if (!info
[TIPC_NLA_SOCK
])
74 mnl_attr_parse_nested(info
[TIPC_NLA_SOCK
], parse_attrs
, attrs
);
75 if (!attrs
[TIPC_NLA_SOCK_REF
])
78 printf("socket %u\n", mnl_attr_get_u32(attrs
[TIPC_NLA_SOCK_REF
]));
80 if (attrs
[TIPC_NLA_SOCK_CON
]) {
82 struct nlattr
*con
[TIPC_NLA_CON_MAX
+ 1] = {};
84 mnl_attr_parse_nested(attrs
[TIPC_NLA_SOCK_CON
], parse_attrs
, con
);
85 node
= mnl_attr_get_u32(con
[TIPC_NLA_CON_NODE
]);
87 printf(" connected to %x:%u", node
,
88 mnl_attr_get_u32(con
[TIPC_NLA_CON_SOCK
]));
90 if (con
[TIPC_NLA_CON_FLAG
])
91 printf(" via {%u,%u}\n",
92 mnl_attr_get_u32(con
[TIPC_NLA_CON_TYPE
]),
93 mnl_attr_get_u32(con
[TIPC_NLA_CON_INST
]));
96 } else if (attrs
[TIPC_NLA_SOCK_HAS_PUBL
]) {
97 publ_list(mnl_attr_get_u32(attrs
[TIPC_NLA_SOCK_REF
]));
103 static int cmd_socket_list(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
104 struct cmdl
*cmdl
, void *data
)
106 char buf
[MNL_SOCKET_BUFFER_SIZE
];
109 fprintf(stderr
, "Usage: %s socket list\n", cmdl
->argv
[0]);
113 if (!(nlh
= msg_init(buf
, TIPC_NL_SOCK_GET
))) {
114 fprintf(stderr
, "error, message initialisation failed\n");
118 return msg_dumpit(nlh
, sock_list_cb
, NULL
);
121 void cmd_socket_help(struct cmdl
*cmdl
)
124 "Usage: %s socket COMMAND\n\n"
126 " list - List sockets (ports)\n",
130 int cmd_socket(struct nlmsghdr
*nlh
, const struct cmd
*cmd
, struct cmdl
*cmdl
,
133 const struct cmd cmds
[] = {
134 { "list", cmd_socket_list
, NULL
},
138 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);