handlers.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * netlink/handlers.c default netlink message handlers
  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-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #ifndef NETLINK_HANDLERS_H_
  12. #define NETLINK_HANDLERS_H_
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <sys/socket.h>
  16. #include <sys/types.h>
  17. #include <netlink/netlink-compat.h>
  18. #include <netlink/netlink-kernel.h>
  19. #include <netlink/types.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. struct nl_sock;
  24. struct nl_msg;
  25. struct nl_cb;
  26. /**
  27. * @name Callback Typedefs
  28. * @{
  29. */
  30. /**
  31. * nl_recvmsgs() callback for message processing customization
  32. * @ingroup cb
  33. * @arg msg netlink message being processed
  34. * @arg arg argument passwd on through caller
  35. */
  36. typedef int (*nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg);
  37. /**
  38. * nl_recvmsgs() callback for error message processing customization
  39. * @ingroup cb
  40. * @arg nla netlink address of the peer
  41. * @arg nlerr netlink error message being processed
  42. * @arg arg argument passed on through caller
  43. */
  44. typedef int (*nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla,
  45. struct nlmsgerr *nlerr, void *arg);
  46. /** @} */
  47. /**
  48. * Callback actions
  49. * @ingroup cb
  50. */
  51. enum nl_cb_action {
  52. /** Proceed with wathever would come next */
  53. NL_OK,
  54. /** Skip this message */
  55. NL_SKIP,
  56. /** Stop parsing altogether and discard remaining messages */
  57. NL_STOP,
  58. };
  59. /**
  60. * Callback kinds
  61. * @ingroup cb
  62. */
  63. enum nl_cb_kind {
  64. /** Default handlers (quiet) */
  65. NL_CB_DEFAULT,
  66. /** Verbose default handlers (error messages printed) */
  67. NL_CB_VERBOSE,
  68. /** Debug handlers for debugging */
  69. NL_CB_DEBUG,
  70. /** Customized handler specified by the user */
  71. NL_CB_CUSTOM,
  72. __NL_CB_KIND_MAX,
  73. };
  74. #define NL_CB_KIND_MAX (__NL_CB_KIND_MAX - 1)
  75. /**
  76. * Callback types
  77. * @ingroup cb
  78. */
  79. enum nl_cb_type {
  80. /** Message is valid */
  81. NL_CB_VALID,
  82. /** Last message in a series of multi part messages received */
  83. NL_CB_FINISH,
  84. /** Report received that data was lost */
  85. NL_CB_OVERRUN,
  86. /** Message wants to be skipped */
  87. NL_CB_SKIPPED,
  88. /** Message is an acknowledge */
  89. NL_CB_ACK,
  90. /** Called for every message received */
  91. NL_CB_MSG_IN,
  92. /** Called for every message sent out except for nl_sendto() */
  93. NL_CB_MSG_OUT,
  94. /** Message is malformed and invalid */
  95. NL_CB_INVALID,
  96. /** Called instead of internal sequence number checking */
  97. NL_CB_SEQ_CHECK,
  98. /** Sending of an acknowledge message has been requested */
  99. NL_CB_SEND_ACK,
  100. __NL_CB_TYPE_MAX,
  101. };
  102. #define NL_CB_TYPE_MAX (__NL_CB_TYPE_MAX - 1)
  103. struct nl_cb
  104. {
  105. nl_recvmsg_msg_cb_t cb_set[NL_CB_TYPE_MAX+1];
  106. void * cb_args[NL_CB_TYPE_MAX+1];
  107. nl_recvmsg_err_cb_t cb_err;
  108. void * cb_err_arg;
  109. /** May be used to replace nl_recvmsgs with your own implementation
  110. * in all internal calls to nl_recvmsgs. */
  111. int (*cb_recvmsgs_ow)(struct nl_sock *,
  112. struct nl_cb *);
  113. /** Overwrite internal calls to nl_recv, must return the number of
  114. * octets read and allocate a buffer for the received data. */
  115. int (*cb_recv_ow)(struct nl_sock *,
  116. struct sockaddr_nl *,
  117. unsigned char **,
  118. struct ucred **);
  119. /** Overwrites internal calls to nl_send, must send the netlink
  120. * message. */
  121. int (*cb_send_ow)(struct nl_sock *,
  122. struct nl_msg *);
  123. int cb_refcnt;
  124. };
  125. extern struct nl_cb * nl_cb_alloc(enum nl_cb_kind);
  126. extern struct nl_cb * nl_cb_clone(struct nl_cb *);
  127. extern void nl_cb_put(struct nl_cb *);
  128. extern int nl_cb_set(struct nl_cb *, enum nl_cb_type, enum nl_cb_kind,
  129. nl_recvmsg_msg_cb_t, void *);
  130. extern int nl_cb_err(struct nl_cb *, enum nl_cb_kind, nl_recvmsg_err_cb_t,
  131. void *);
  132. static inline struct nl_cb *nl_cb_get(struct nl_cb *cb)
  133. {
  134. cb->cb_refcnt++;
  135. return cb;
  136. }
  137. /**
  138. * Set up a all callbacks
  139. * @arg cb callback set
  140. * @arg kind kind of callback
  141. * @arg func callback function
  142. * @arg arg argument to be passwd to callback function
  143. *
  144. * @return 0 on success or a negative error code
  145. */
  146. static inline int nl_cb_set_all(struct nl_cb *cb, enum nl_cb_kind kind,
  147. nl_recvmsg_msg_cb_t func, void *arg)
  148. {
  149. int i, err;
  150. for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
  151. err = nl_cb_set(cb,(enum nl_cb_type)i, kind, func, arg);
  152. if (err < 0)
  153. return err;
  154. }
  155. return 0;
  156. }
  157. /**
  158. * @name Overwriting
  159. * @{
  160. */
  161. /**
  162. * Overwrite internal calls to nl_recvmsgs()
  163. * @arg cb callback set
  164. * @arg func replacement callback for nl_recvmsgs()
  165. */
  166. static inline void nl_cb_overwrite_recvmsgs(struct nl_cb *cb,
  167. int (*func)(struct nl_sock *, struct nl_cb *))
  168. {
  169. cb->cb_recvmsgs_ow = func;
  170. }
  171. /**
  172. * Overwrite internal calls to nl_recv()
  173. * @arg cb callback set
  174. * @arg func replacement callback for nl_recv()
  175. */
  176. static inline void nl_cb_overwrite_recv(struct nl_cb *cb,
  177. int (*func)(struct nl_sock *, struct sockaddr_nl *,
  178. unsigned char **, struct ucred **))
  179. {
  180. cb->cb_recv_ow = func;
  181. }
  182. /**
  183. * Overwrite internal calls to nl_send()
  184. * @arg cb callback set
  185. * @arg func replacement callback for nl_send()
  186. */
  187. static inline void nl_cb_overwrite_send(struct nl_cb *cb,
  188. int (*func)(struct nl_sock *, struct nl_msg *))
  189. {
  190. cb->cb_send_ow = func;
  191. }
  192. /** @} */
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif