Commit 0628cddd authored by Nicolas Dichtel's avatar Nicolas Dichtel Committed by Stephen Hemminger

libnetlink: introduce rtnl_listen_filter_t

There is no functional change with this commit. It only prepares the next one.
Signed-off-by: default avatarNicolas Dichtel <nicolas.dichtel@6wind.com>
parent 2503247d
...@@ -36,6 +36,7 @@ static void usage(void) ...@@ -36,6 +36,7 @@ static void usage(void)
} }
static int accept_msg(const struct sockaddr_nl *who, static int accept_msg(const struct sockaddr_nl *who,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg) struct nlmsghdr *n, void *arg)
{ {
FILE *fp = arg; FILE *fp = arg;
......
...@@ -177,8 +177,9 @@ static int print_ctrl_grp(FILE *fp, struct rtattr *arg, __u32 ctrl_ver) ...@@ -177,8 +177,9 @@ static int print_ctrl_grp(FILE *fp, struct rtattr *arg, __u32 ctrl_ver)
/* /*
* The controller sends one nlmsg per family * The controller sends one nlmsg per family
*/ */
static int print_ctrl(const struct sockaddr_nl *who, struct nlmsghdr *n, static int print_ctrl(const struct sockaddr_nl *who,
void *arg) struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg)
{ {
struct rtattr *tb[CTRL_ATTR_MAX + 1]; struct rtattr *tb[CTRL_ATTR_MAX + 1];
struct genlmsghdr *ghdr = NLMSG_DATA(n); struct genlmsghdr *ghdr = NLMSG_DATA(n);
...@@ -281,6 +282,12 @@ static int print_ctrl(const struct sockaddr_nl *who, struct nlmsghdr *n, ...@@ -281,6 +282,12 @@ static int print_ctrl(const struct sockaddr_nl *who, struct nlmsghdr *n,
return 0; return 0;
} }
static int print_ctrl2(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg)
{
return print_ctrl(who, NULL, n, arg);
}
static int ctrl_list(int cmd, int argc, char **argv) static int ctrl_list(int cmd, int argc, char **argv)
{ {
struct rtnl_handle rth; struct rtnl_handle rth;
...@@ -339,7 +346,7 @@ static int ctrl_list(int cmd, int argc, char **argv) ...@@ -339,7 +346,7 @@ static int ctrl_list(int cmd, int argc, char **argv)
goto ctrl_done; goto ctrl_done;
} }
if (print_ctrl(NULL, nlh, (void *) stdout) < 0) { if (print_ctrl2(NULL, nlh, (void *) stdout) < 0) {
fprintf(stderr, "Dump terminated\n"); fprintf(stderr, "Dump terminated\n");
goto ctrl_done; goto ctrl_done;
} }
...@@ -355,7 +362,7 @@ static int ctrl_list(int cmd, int argc, char **argv) ...@@ -355,7 +362,7 @@ static int ctrl_list(int cmd, int argc, char **argv)
goto ctrl_done; goto ctrl_done;
} }
rtnl_dump_filter(&rth, print_ctrl, stdout); rtnl_dump_filter(&rth, print_ctrl2, stdout);
} }
...@@ -408,5 +415,5 @@ static int parse_ctrl(struct genl_util *a, int argc, char **argv) ...@@ -408,5 +415,5 @@ static int parse_ctrl(struct genl_util *a, int argc, char **argv)
struct genl_util ctrl_genl_util = { struct genl_util ctrl_genl_util = {
.name = "ctrl", .name = "ctrl",
.parse_genlopt = parse_ctrl, .parse_genlopt = parse_ctrl,
.print_genlopt = print_ctrl, .print_genlopt = print_ctrl2,
}; };
...@@ -41,9 +41,16 @@ extern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, ...@@ -41,9 +41,16 @@ extern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
int len) int len)
__attribute__((warn_unused_result)); __attribute__((warn_unused_result));
struct rtnl_ctrl_data {
};
typedef int (*rtnl_filter_t)(const struct sockaddr_nl *, typedef int (*rtnl_filter_t)(const struct sockaddr_nl *,
struct nlmsghdr *n, void *); struct nlmsghdr *n, void *);
typedef int (*rtnl_listen_filter_t)(const struct sockaddr_nl *,
struct rtnl_ctrl_data *,
struct nlmsghdr *n, void *);
struct rtnl_dump_filter_arg struct rtnl_dump_filter_arg
{ {
rtnl_filter_t filter; rtnl_filter_t filter;
...@@ -118,9 +125,9 @@ static inline const char *rta_getattr_str(const struct rtattr *rta) ...@@ -118,9 +125,9 @@ static inline const char *rta_getattr_str(const struct rtattr *rta)
return (const char *)RTA_DATA(rta); return (const char *)RTA_DATA(rta);
} }
extern int rtnl_listen(struct rtnl_handle *, rtnl_filter_t handler, extern int rtnl_listen(struct rtnl_handle *, rtnl_listen_filter_t handler,
void *jarg); void *jarg);
extern int rtnl_from_file(FILE *, rtnl_filter_t handler, extern int rtnl_from_file(FILE *, rtnl_listen_filter_t handler,
void *jarg); void *jarg);
#define NLMSG_TAIL(nmsg) \ #define NLMSG_TAIL(nmsg) \
......
...@@ -33,6 +33,7 @@ extern int print_prefix(const struct sockaddr_nl *who, ...@@ -33,6 +33,7 @@ extern int print_prefix(const struct sockaddr_nl *who,
extern int print_rule(const struct sockaddr_nl *who, extern int print_rule(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg); struct nlmsghdr *n, void *arg);
extern int print_netconf(const struct sockaddr_nl *who, extern int print_netconf(const struct sockaddr_nl *who,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg); struct nlmsghdr *n, void *arg);
extern void netns_map_init(void); extern void netns_map_init(void);
extern int print_nsid(const struct sockaddr_nl *who, extern int print_nsid(const struct sockaddr_nl *who,
......
...@@ -1111,7 +1111,9 @@ static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, ...@@ -1111,7 +1111,9 @@ static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
return ret == n->nlmsg_len ? 0 : ret; return ret == n->nlmsg_len ? 0 : ret;
} }
static int show_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) static int show_handler(const struct sockaddr_nl *nl,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg)
{ {
struct ifaddrmsg *ifa = NLMSG_DATA(n); struct ifaddrmsg *ifa = NLMSG_DATA(n);
...@@ -1128,7 +1130,9 @@ static int ipaddr_showdump(void) ...@@ -1128,7 +1130,9 @@ static int ipaddr_showdump(void)
exit(rtnl_from_file(stdin, &show_handler, NULL)); exit(rtnl_from_file(stdin, &show_handler, NULL));
} }
static int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) static int restore_handler(const struct sockaddr_nl *nl,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg)
{ {
int ret; int ret;
......
...@@ -180,6 +180,7 @@ static int get_addr_gen_mode(const char *mode) ...@@ -180,6 +180,7 @@ static int get_addr_gen_mode(const char *mode)
static int have_rtnl_newlink = -1; static int have_rtnl_newlink = -1;
static int accept_msg(const struct sockaddr_nl *who, static int accept_msg(const struct sockaddr_nl *who,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg) struct nlmsghdr *n, void *arg)
{ {
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n); struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n);
......
...@@ -38,6 +38,7 @@ static void usage(void) ...@@ -38,6 +38,7 @@ static void usage(void)
} }
static int accept_msg(const struct sockaddr_nl *who, static int accept_msg(const struct sockaddr_nl *who,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg) struct nlmsghdr *n, void *arg)
{ {
FILE *fp = (FILE*)arg; FILE *fp = (FILE*)arg;
...@@ -122,7 +123,7 @@ static int accept_msg(const struct sockaddr_nl *who, ...@@ -122,7 +123,7 @@ static int accept_msg(const struct sockaddr_nl *who,
if (n->nlmsg_type == RTM_NEWNETCONF) { if (n->nlmsg_type == RTM_NEWNETCONF) {
if (prefix_banner) if (prefix_banner)
fprintf(fp, "[NETCONF]"); fprintf(fp, "[NETCONF]");
print_netconf(who, n, arg); print_netconf(who, ctrl, n, arg);
return 0; return 0;
} }
if (n->nlmsg_type == NLMSG_TSTAMP) { if (n->nlmsg_type == NLMSG_TSTAMP) {
......
...@@ -40,7 +40,8 @@ static void usage(void) ...@@ -40,7 +40,8 @@ static void usage(void)
#define NETCONF_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct netconfmsg)))) #define NETCONF_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct netconfmsg))))
int print_netconf(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg)
{ {
FILE *fp = (FILE*)arg; FILE *fp = (FILE*)arg;
struct netconfmsg *ncm = NLMSG_DATA(n); struct netconfmsg *ncm = NLMSG_DATA(n);
...@@ -123,6 +124,12 @@ int print_netconf(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -123,6 +124,12 @@ int print_netconf(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
return 0; return 0;
} }
static int print_netconf2(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg)
{
return print_netconf(who, NULL, n, arg);
}
void ipnetconf_reset_filter(int ifindex) void ipnetconf_reset_filter(int ifindex)
{ {
memset(&filter, 0, sizeof(filter)); memset(&filter, 0, sizeof(filter));
...@@ -177,7 +184,7 @@ dump: ...@@ -177,7 +184,7 @@ dump:
perror("Cannot send dump request"); perror("Cannot send dump request");
exit(1); exit(1);
} }
if (rtnl_dump_filter(&rth, print_netconf, stdout) < 0) { if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) {
fprintf(stderr, "Dump terminated\n"); fprintf(stderr, "Dump terminated\n");
exit(1); exit(1);
} }
......
...@@ -43,6 +43,7 @@ static struct rtnl_handle rtnsh = { .fd = -1 }; ...@@ -43,6 +43,7 @@ static struct rtnl_handle rtnsh = { .fd = -1 };
static int have_rtnl_getnsid = -1; static int have_rtnl_getnsid = -1;
static int ipnetns_accept_msg(const struct sockaddr_nl *who, static int ipnetns_accept_msg(const struct sockaddr_nl *who,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg) struct nlmsghdr *n, void *arg)
{ {
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n); struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n);
......
...@@ -1681,8 +1681,9 @@ static int iproute_get(int argc, char **argv) ...@@ -1681,8 +1681,9 @@ static int iproute_get(int argc, char **argv)
exit(0); exit(0);
} }
static int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, static int restore_handler(const struct sockaddr_nl *nl,
void *arg) struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg)
{ {
int ret; int ret;
...@@ -1724,7 +1725,9 @@ static int iproute_restore(void) ...@@ -1724,7 +1725,9 @@ static int iproute_restore(void)
exit(rtnl_from_file(stdin, &restore_handler, NULL)); exit(rtnl_from_file(stdin, &restore_handler, NULL));
} }
static int show_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) static int show_handler(const struct sockaddr_nl *nl,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg)
{ {
print_route(nl, n, stdout); print_route(nl, n, stdout);
return 0; return 0;
......
...@@ -45,8 +45,8 @@ static void write_stamp(FILE *fp) ...@@ -45,8 +45,8 @@ static void write_stamp(FILE *fp)
fwrite((void*)n1, 1, NLMSG_ALIGN(n1->nlmsg_len), fp); fwrite((void*)n1, 1, NLMSG_ALIGN(n1->nlmsg_len), fp);
} }
static int dump_msg(const struct sockaddr_nl *who, struct nlmsghdr *n, static int dump_msg(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
void *arg) struct nlmsghdr *n, void *arg)
{ {
FILE *fp = (FILE*)arg; FILE *fp = (FILE*)arg;
if (!init_phase) if (!init_phase)
...@@ -56,6 +56,12 @@ static int dump_msg(const struct sockaddr_nl *who, struct nlmsghdr *n, ...@@ -56,6 +56,12 @@ static int dump_msg(const struct sockaddr_nl *who, struct nlmsghdr *n,
return 0; return 0;
} }
static int dump_msg2(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg)
{
return dump_msg(who, NULL, n, arg);
}
static void usage(void) static void usage(void)
{ {
fprintf(stderr, "Usage: rtmon file FILE [ all | LISTofOBJECTS]\n"); fprintf(stderr, "Usage: rtmon file FILE [ all | LISTofOBJECTS]\n");
...@@ -163,7 +169,7 @@ main(int argc, char **argv) ...@@ -163,7 +169,7 @@ main(int argc, char **argv)
write_stamp(fp); write_stamp(fp);
if (rtnl_dump_filter(&rth, dump_msg, fp) < 0) { if (rtnl_dump_filter(&rth, dump_msg2, fp) < 0) {
fprintf(stderr, "Dump terminated\n"); fprintf(stderr, "Dump terminated\n");
return 1; return 1;
} }
......
...@@ -290,6 +290,7 @@ static int xfrm_mapping_print(const struct sockaddr_nl *who, ...@@ -290,6 +290,7 @@ static int xfrm_mapping_print(const struct sockaddr_nl *who,
} }
static int xfrm_accept_msg(const struct sockaddr_nl *who, static int xfrm_accept_msg(const struct sockaddr_nl *who,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg) struct nlmsghdr *n, void *arg)
{ {
FILE *fp = (FILE*)arg; FILE *fp = (FILE*)arg;
......
...@@ -419,7 +419,7 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, ...@@ -419,7 +419,7 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
} }
int rtnl_listen(struct rtnl_handle *rtnl, int rtnl_listen(struct rtnl_handle *rtnl,
rtnl_filter_t handler, rtnl_listen_filter_t handler,
void *jarg) void *jarg)
{ {
int status; int status;
...@@ -475,7 +475,7 @@ int rtnl_listen(struct rtnl_handle *rtnl, ...@@ -475,7 +475,7 @@ int rtnl_listen(struct rtnl_handle *rtnl,
exit(1); exit(1);
} }
err = handler(&nladdr, h, jarg); err = handler(&nladdr, NULL, h, jarg);
if (err < 0) if (err < 0)
return err; return err;
...@@ -493,7 +493,7 @@ int rtnl_listen(struct rtnl_handle *rtnl, ...@@ -493,7 +493,7 @@ int rtnl_listen(struct rtnl_handle *rtnl,
} }
} }
int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler, int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
void *jarg) void *jarg)
{ {
int status; int status;
...@@ -541,7 +541,7 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler, ...@@ -541,7 +541,7 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
return -1; return -1;
} }
err = handler(&nladdr, h, jarg); err = handler(&nladdr, NULL, h, jarg);
if (err < 0) if (err < 0)
return err; return err;
} }
......
...@@ -33,7 +33,8 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, ...@@ -33,7 +33,8 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
void *jarg) void *jarg)
.sp .sp
int rtnl_listen(struct rtnl_handle *rtnl, int rtnl_listen(struct rtnl_handle *rtnl,
int (*handler)(struct sockaddr_nl *,struct nlmsghdr *n, void *), int (*handler)(struct sockaddr_nl *, struct rtnl_ctrl_data *,
struct nlmsghdr *n, void *),
void *jarg) void *jarg)
.sp .sp
int rtnl_from_file(FILE *rtnl, int rtnl_from_file(FILE *rtnl,
...@@ -108,8 +109,8 @@ rtnl_listen ...@@ -108,8 +109,8 @@ rtnl_listen
Receive netlink data after a request and pass it to Receive netlink data after a request and pass it to
.I handler. .I handler.
.B handler .B handler
is a callback that gets the message source address, the message itself, is a callback that gets the message source address, anscillary data, the message
and the itself, and the
.B jarg .B jarg
cookie as arguments. It will get called for all received messages. cookie as arguments. It will get called for all received messages.
Only one message bundle is received. If there is a message Only one message bundle is received. If there is a message
......
...@@ -36,6 +36,7 @@ static void usage(void) ...@@ -36,6 +36,7 @@ static void usage(void)
static int accept_tcmsg(const struct sockaddr_nl *who, static int accept_tcmsg(const struct sockaddr_nl *who,
struct rtnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg) struct nlmsghdr *n, void *arg)
{ {
FILE *fp = (FILE*)arg; FILE *fp = (FILE*)arg;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment