mngt.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * netlink/genl/mngt.h Generic Netlink Management
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #ifndef NETLINK_GENL_MNGT_H_
  12. #define NETLINK_GENL_MNGT_H_
  13. #include <netlink/netlink.h>
  14. #include <netlink/attr.h>
  15. #include <netlink/list.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. struct nl_cache_ops;
  20. /**
  21. * @ingroup genl_mngt
  22. * @struct genl_info netlink/genl/mngt.h
  23. *
  24. * Informative structure passed on to message parser callbacks
  25. *
  26. * This structure is passed on to all message parser callbacks and contains
  27. * information about the sender of the message as well as pointers to all
  28. * relevant sections of the parsed message.
  29. *
  30. * @see genl_cmd::c_msg_parser
  31. */
  32. struct genl_info
  33. {
  34. /** Socket address of sender */
  35. struct sockaddr_nl * who;
  36. /** Pointer to Netlink message header */
  37. struct nlmsghdr * nlh;
  38. /** Pointer to Generic Netlink message header */
  39. struct genlmsghdr * genlhdr;
  40. /** Pointer to user header */
  41. void * userhdr;
  42. /** Pointer to array of parsed attributes */
  43. struct nlattr ** attrs;
  44. };
  45. /**
  46. * @ingroup genl_mngt
  47. * @struct genl_cmd netlink/genl/mngt.h
  48. *
  49. * Definition of a Generic Netlink command.
  50. *
  51. * This structure is used to define the list of available commands on the
  52. * receiving side.
  53. *
  54. * @par Example:
  55. * @code
  56. * static struct genl_cmd foo_cmds[] = {
  57. * {
  58. * .c_id = FOO_CMD_NEW,
  59. * .c_name = "NEWFOO" ,
  60. * .c_maxattr = FOO_ATTR_MAX,
  61. * .c_attr_policy = foo_policy,
  62. * .c_msg_parser = foo_msg_parser,
  63. * },
  64. * {
  65. * .c_id = FOO_CMD_DEL,
  66. * .c_name = "DELFOO" ,
  67. * },
  68. * };
  69. *
  70. * static struct genl_ops my_genl_ops = {
  71. * [...]
  72. * .o_cmds = foo_cmds,
  73. * .o_ncmds = ARRAY_SIZE(foo_cmds),
  74. * };
  75. * @endcode
  76. */
  77. struct genl_cmd
  78. {
  79. /** Numeric command identifier (required) */
  80. int c_id;
  81. /** Human readable name (required) */
  82. char * c_name;
  83. /** Maximum attribute identifier that the command is prepared to handle. */
  84. int c_maxattr;
  85. /** Called whenever a message for this command is received */
  86. int (*c_msg_parser)(struct nl_cache_ops *,
  87. struct genl_cmd *,
  88. struct genl_info *, void *);
  89. /** Attribute validation policy, enforced before the callback is called */
  90. struct nla_policy * c_attr_policy;
  91. };
  92. /**
  93. * @ingroup genl_mngt
  94. * @struct genl_ops netlink/genl/mngt.h
  95. *
  96. * Definition of a Generic Netlink family
  97. *
  98. * @par Example:
  99. * @code
  100. * static struct genl_cmd foo_cmds[] = {
  101. * [...]
  102. * };
  103. *
  104. * static struct genl_ops my_genl_ops = {
  105. * .o_name = "foo",
  106. * .o_hdrsize = sizeof(struct my_hdr),
  107. * .o_cmds = foo_cmds,
  108. * .o_ncmds = ARRAY_SIZE(foo_cmds),
  109. * };
  110. *
  111. * if ((err = genl_register_family(&my_genl_ops)) < 0)
  112. * // ERROR
  113. * @endcode
  114. *
  115. * @see genl_cmd
  116. */
  117. struct genl_ops
  118. {
  119. /** Length of user header */
  120. unsigned int o_hdrsize;
  121. /** Numeric identifier, automatically filled in by genl_ops_resolve() */
  122. int o_id;
  123. /** Human readable name, used by genl_ops_resolve() to resolve numeric id */
  124. char * o_name;
  125. /**
  126. * If registered via genl_register(), will point to the related
  127. * cache operations.
  128. */
  129. struct nl_cache_ops * o_cache_ops;
  130. /** Optional array defining the available Generic Netlink commands */
  131. struct genl_cmd * o_cmds;
  132. /** Number of elements in \c o_cmds array */
  133. int o_ncmds;
  134. /**
  135. * @private
  136. * Used internally to link together all registered operations.
  137. */
  138. struct nl_list_head o_list;
  139. };
  140. extern int genl_register_family(struct genl_ops *);
  141. extern int genl_unregister_family(struct genl_ops *);
  142. extern int genl_handle_msg(struct nl_msg *, void *);
  143. extern int genl_register(struct nl_cache_ops *);
  144. extern void genl_unregister(struct nl_cache_ops *);
  145. extern int genl_ops_resolve(struct nl_sock *, struct genl_ops *);
  146. extern int genl_mngt_resolve(struct nl_sock *);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif