batadv-genl.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* SPDX-License-Identifier: MIT */
  2. /* batman-adv helpers functions library
  3. *
  4. * Copyright (c) 2017, Sven Eckelmann <sven@narfation.org>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #ifndef _BATADV_GENL_H_
  25. #define _BATADV_GENL_H_
  26. #include <netlink/netlink.h>
  27. #include <netlink/genl/genl.h>
  28. #include <netlink/genl/ctrl.h>
  29. #include <stddef.h>
  30. #include <stdbool.h>
  31. #include "batman_adv.h"
  32. /**
  33. * struct batadv_nlquery_opts - internal state for batadv_genl_query()
  34. *
  35. * This structure should be used as member of a struct which tracks the state
  36. * for the callback. The macro batadv_container_of can be used to convert the
  37. * arg pointer from batadv_nlquery_opts to the member which contains this
  38. * struct.
  39. */
  40. struct batadv_nlquery_opts {
  41. /** @err: current error */
  42. int err;
  43. };
  44. /**
  45. * BATADV_ARRAY_SIZE() - Get number of items in static array
  46. * @x: array with known length
  47. *
  48. * Return: number of items in array
  49. */
  50. #ifndef BATADV_ARRAY_SIZE
  51. #define BATADV_ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
  52. #endif
  53. /**
  54. * batadv_container_of() - Calculate address of object that contains address ptr
  55. * @ptr: pointer to member variable
  56. * @type: type of the structure containing ptr
  57. * @member: name of the member variable in struct @type
  58. *
  59. * Return: @type pointer of object containing ptr
  60. */
  61. #ifndef batadv_container_of
  62. #define batadv_container_of(ptr, type, member) __extension__ ({ \
  63. const __typeof__(((type *)0)->member) *__pmember = (ptr); \
  64. (type *)((char *)__pmember - offsetof(type, member)); })
  65. #endif
  66. /**
  67. * batadv_genl_missing_attrs() - Check whether @attrs is missing mandatory
  68. * attribute
  69. * @attrs: attributes which was parsed by nla_parse()
  70. * @mandatory: list of required attributes
  71. * @num: number of required attributes in @mandatory
  72. *
  73. * Return: Return true when a attribute is missing, false otherwise
  74. */
  75. static inline bool
  76. batadv_genl_missing_attrs(struct nlattr *attrs[],
  77. const enum batadv_nl_attrs mandatory[], size_t num)
  78. {
  79. size_t i;
  80. for (i = 0; i < num; i++) {
  81. if (!attrs[mandatory[i]])
  82. return true;
  83. }
  84. return false;
  85. }
  86. extern struct nla_policy batadv_genl_policy[];
  87. int batadv_genl_query(const char *mesh_iface, enum batadv_nl_commands nl_cmd,
  88. nl_recvmsg_msg_cb_t callback, int flags,
  89. struct batadv_nlquery_opts *query_opts);
  90. #endif /* _BATADV_GENL_H_ */