libgenl: introduce genl_init_handle

All users of genl have the same code to open a genl socket and resolve
the family for their specific protocol.  Introduce a helper to initialize
the handle, and use it in all the genl code.

Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
This commit is contained in:
Sabrina Dubroca 2016-08-16 16:26:55 +02:00 committed by Stephen Hemminger
parent 08c0466b11
commit 2b68cb77cd
7 changed files with 31 additions and 57 deletions

View File

@ -21,5 +21,7 @@ struct { \
}
extern int genl_resolve_family(struct rtnl_handle *grth, const char *family);
extern int genl_init_handle(struct rtnl_handle *grth, const char *family,
int *genl_family);
#endif /* __LIBGENL_H__ */

View File

@ -136,16 +136,8 @@ static int do_del(int argc, char **argv)
int do_ipfou(int argc, char **argv)
{
if (genl_family < 0) {
if (rtnl_open_byproto(&genl_rth, 0, NETLINK_GENERIC) < 0) {
fprintf(stderr, "Cannot open generic netlink socket\n");
exit(1);
}
genl_family = genl_resolve_family(&genl_rth, FOU_GENL_NAME);
if (genl_family < 0)
exit(1);
}
if (genl_init_handle(&genl_rth, FOU_GENL_NAME, &genl_family))
exit(1);
if (argc < 1)
usage();

View File

@ -240,16 +240,8 @@ static int do_del(int argc, char **argv)
int do_ipila(int argc, char **argv)
{
if (genl_family < 0) {
if (rtnl_open_byproto(&genl_rth, 0, NETLINK_GENERIC) < 0) {
fprintf(stderr, "Cannot open generic netlink socket\n");
exit(1);
}
genl_family = genl_resolve_family(&genl_rth, ILA_GENL_NAME);
if (genl_family < 0)
exit(1);
}
if (genl_init_handle(&genl_rth, ILA_GENL_NAME, &genl_family))
exit(1);
if (argc < 1)
usage();

View File

@ -767,16 +767,8 @@ int do_ipl2tp(int argc, char **argv)
if (argc < 1 || !matches(*argv, "help"))
usage();
if (genl_family < 0) {
if (rtnl_open_byproto(&genl_rth, 0, NETLINK_GENERIC) < 0) {
fprintf(stderr, "Cannot open generic netlink socket\n");
exit(1);
}
genl_family = genl_resolve_family(&genl_rth, L2TP_GENL_NAME);
if (genl_family < 0)
exit(1);
}
if (genl_init_handle(&genl_rth, L2TP_GENL_NAME, &genl_family))
exit(1);
if (matches(*argv, "add") == 0)
return do_add(argc-1, argv+1);

View File

@ -79,21 +79,6 @@ static int genl_family = -1;
_cmd, _flags)
static void init_genl(void)
{
if (genl_family >= 0)
return;
if (rtnl_open_byproto(&genl_rth, 0, NETLINK_GENERIC) < 0) {
fprintf(stderr, "Cannot open generic netlink socket\n");
exit(1);
}
genl_family = genl_resolve_family(&genl_rth, MACSEC_GENL_NAME);
if (genl_family < 0)
exit(1);
}
static void ipmacsec_usage(void)
{
fprintf(stderr, "Usage: ip macsec add DEV tx sa { 0..3 } [ OPTS ] key ID KEY\n");
@ -1001,7 +986,8 @@ static int do_show(int argc, char **argv)
int do_ipmacsec(int argc, char **argv)
{
init_genl();
if (genl_init_handle(&genl_rth, MACSEC_GENL_NAME, &genl_family))
exit(1);
if (argc < 1)
ipmacsec_usage();

View File

@ -398,17 +398,9 @@ static int tcpm_do_cmd(int cmd, int argc, char **argv)
ack = 0;
}
if (genl_family < 0) {
if (rtnl_open_byproto(&grth, 0, NETLINK_GENERIC) < 0) {
fprintf(stderr, "Cannot open generic netlink socket\n");
exit(1);
}
genl_family = genl_resolve_family(&grth,
TCP_METRICS_GENL_NAME);
if (genl_family < 0)
exit(1);
req.n.nlmsg_type = genl_family;
}
if (genl_init_handle(&grth, TCP_METRICS_GENL_NAME, &genl_family))
exit(1);
req.n.nlmsg_type = genl_family;
if (!(cmd & CMD_FLUSH) && (atype >= 0 || (cmd & CMD_DEL))) {
if (ack)

View File

@ -60,3 +60,21 @@ int genl_resolve_family(struct rtnl_handle *grth, const char *family)
return genl_parse_getfamily(&req.n);
}
int genl_init_handle(struct rtnl_handle *grth, const char *family,
int *genl_family)
{
if (*genl_family >= 0)
return 0;
if (rtnl_open_byproto(grth, 0, NETLINK_GENERIC) < 0) {
fprintf(stderr, "Cannot open generic netlink socket\n");
return -1;
}
*genl_family = genl_resolve_family(grth, family);
if (*genl_family < 0)
return -1;
return 0;
}