]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - lib/libgenl.c
vdpa: add .gitignore
[mirror_iproute2.git] / lib / libgenl.c
index ef3e5db60c8cbcec82b808f6e91b384ee66771ed..f2ce698fc71169a808a40e5d3651b06a2ae7b978 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0 */
 /*
  * libgenl.c   GENL library
  */
@@ -49,15 +50,37 @@ int genl_resolve_family(struct rtnl_handle *grth, const char *family)
 {
        GENL_REQUEST(req, 1024, GENL_ID_CTRL, 0, 0, CTRL_CMD_GETFAMILY,
                     NLM_F_REQUEST);
+       struct nlmsghdr *answer;
+       int fnum;
 
        addattr_l(&req.n, sizeof(req), CTRL_ATTR_FAMILY_NAME,
                  family, strlen(family) + 1);
 
-       if (rtnl_talk(grth, &req.n, 0, 0, &req.n) < 0) {
+       if (rtnl_talk(grth, &req.n, &answer) < 0) {
                fprintf(stderr, "Error talking to the kernel\n");
                return -2;
        }
 
-       return genl_parse_getfamily(&req.n);
+       fnum = genl_parse_getfamily(answer);
+       free(answer);
+
+       return fnum;
 }
 
+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;
+}