handlers.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * lib/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-2008 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup core
  13. * @defgroup cb Callbacks/Customization
  14. *
  15. * @details
  16. * @par 1) Setting up a callback set
  17. * @code
  18. * // Allocate a callback set and initialize it to the verbose default set
  19. * struct nl_cb *cb = nl_cb_alloc(NL_CB_VERBOSE);
  20. *
  21. * // Modify the set to call my_func() for all valid messages
  22. * nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, my_func, NULL);
  23. *
  24. * // Set the error message handler to the verbose default implementation
  25. * // and direct it to print all errors to the given file descriptor.
  26. * FILE *file = fopen(...);
  27. * nl_cb_err(cb, NL_CB_VERBOSE, NULL, file);
  28. * @endcode
  29. * @{
  30. */
  31. #include <netlink-local.h>
  32. #include <netlink/netlink.h>
  33. #include <netlink/utils.h>
  34. #include <netlink/msg.h>
  35. #include <netlink/handlers.h>
  36. /**
  37. * @name Callback Handle Management
  38. * @{
  39. */
  40. /**
  41. * Allocate a new callback handle
  42. * @arg kind callback kind to be used for initialization
  43. * @return Newly allocated callback handle or NULL
  44. */
  45. struct nl_cb *nl_cb_alloc(enum nl_cb_kind kind)
  46. {
  47. int i;
  48. struct nl_cb *cb;
  49. if (kind < 0 || kind > NL_CB_KIND_MAX)
  50. return NULL;
  51. cb = calloc(1, sizeof(*cb));
  52. if (!cb)
  53. return NULL;
  54. cb->cb_refcnt = 1;
  55. for (i = 0; i <= NL_CB_TYPE_MAX; i++)
  56. nl_cb_set(cb, i, kind, NULL, NULL);
  57. nl_cb_err(cb, kind, NULL, NULL);
  58. return cb;
  59. }
  60. /**
  61. * Clone an existing callback handle
  62. * @arg orig original callback handle
  63. * @return Newly allocated callback handle being a duplicate of
  64. * orig or NULL
  65. */
  66. struct nl_cb *nl_cb_clone(struct nl_cb *orig)
  67. {
  68. struct nl_cb *cb;
  69. cb = nl_cb_alloc(NL_CB_DEFAULT);
  70. if (!cb)
  71. return NULL;
  72. memcpy(cb, orig, sizeof(*orig));
  73. cb->cb_refcnt = 1;
  74. return cb;
  75. }
  76. void nl_cb_put(struct nl_cb *cb)
  77. {
  78. if (!cb)
  79. return;
  80. cb->cb_refcnt--;
  81. if (cb->cb_refcnt < 0)
  82. BUG();
  83. if (cb->cb_refcnt <= 0)
  84. free(cb);
  85. }
  86. /** @} */
  87. /**
  88. * @name Callback Setup
  89. * @{
  90. */
  91. /**
  92. * Set up a callback
  93. * @arg cb callback set
  94. * @arg type callback to modify
  95. * @arg kind kind of implementation
  96. * @arg func callback function (NL_CB_CUSTOM)
  97. * @arg arg argument passed to callback
  98. *
  99. * @return 0 on success or a negative error code
  100. */
  101. int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind,
  102. nl_recvmsg_msg_cb_t func, void *arg)
  103. {
  104. if (type < 0 || type > NL_CB_TYPE_MAX)
  105. return -NLE_RANGE;
  106. if (kind < 0 || kind > NL_CB_KIND_MAX)
  107. return -NLE_RANGE;
  108. if (kind == NL_CB_CUSTOM) {
  109. cb->cb_set[type] = func;
  110. cb->cb_args[type] = arg;
  111. }
  112. return 0;
  113. }
  114. /**
  115. * Set up an error callback
  116. * @arg cb callback set
  117. * @arg kind kind of callback
  118. * @arg func callback function
  119. * @arg arg argument to be passed to callback function
  120. */
  121. int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind,
  122. nl_recvmsg_err_cb_t func, void *arg)
  123. {
  124. if (kind < 0 || kind > NL_CB_KIND_MAX)
  125. return -NLE_RANGE;
  126. if (kind == NL_CB_CUSTOM) {
  127. cb->cb_err = func;
  128. cb->cb_err_arg = arg;
  129. }
  130. return 0;
  131. }
  132. /** @} */
  133. /** @} */