msg.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * netlink/msg.c Netlink Messages Interface
  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_MSG_H_
  12. #define NETLINK_MSG_H_
  13. #include <netlink/netlink.h>
  14. #include <netlink/object.h>
  15. #include <netlink/attr.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define NL_DONTPAD 0
  20. /**
  21. * @ingroup msg
  22. * @brief
  23. * Will cause the netlink port to be set to the port assigned to
  24. * the netlink icoket ust before sending the message off.
  25. *
  26. * @note Requires the use of nl_send_auto()!
  27. */
  28. #define NL_AUTO_PORT 0
  29. #define NL_AUTO_PID NL_AUTO_PORT
  30. /**
  31. * @ingroup msg
  32. * @brief
  33. * May be used to refer to a sequence number which should be
  34. * automatically set just before sending the message off.
  35. *
  36. * @note Requires the use of nl_send_auto()!
  37. */
  38. #define NL_AUTO_SEQ 0
  39. struct nl_msg;
  40. struct nl_tree;
  41. struct ucred;
  42. extern int nlmsg_size(int);
  43. extern int nlmsg_total_size(int);
  44. extern int nlmsg_padlen(int);
  45. extern void * nlmsg_data(const struct nlmsghdr *);
  46. extern int nlmsg_datalen(const struct nlmsghdr *);
  47. extern void * nlmsg_tail(const struct nlmsghdr *);
  48. /* attribute access */
  49. extern struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int);
  50. extern int nlmsg_attrlen(const struct nlmsghdr *, int);
  51. /* message parsing */
  52. extern int nlmsg_valid_hdr(const struct nlmsghdr *, int);
  53. extern int nlmsg_ok(const struct nlmsghdr *, int);
  54. extern struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *);
  55. extern int nlmsg_parse(struct nlmsghdr *, int, struct nlattr **,
  56. int, struct nla_policy *);
  57. extern struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int);
  58. extern int nlmsg_validate(struct nlmsghdr *, int, int,
  59. struct nla_policy *);
  60. extern struct nl_msg * nlmsg_alloc(void);
  61. extern struct nl_msg * nlmsg_alloc_size(size_t);
  62. extern struct nl_msg * nlmsg_alloc_simple(int, int);
  63. extern void nlmsg_set_default_size(size_t);
  64. extern struct nl_msg * nlmsg_inherit(struct nlmsghdr *);
  65. extern struct nl_msg * nlmsg_convert(struct nlmsghdr *);
  66. extern void * nlmsg_reserve(struct nl_msg *, size_t, int);
  67. extern int nlmsg_append(struct nl_msg *, void *, size_t, int);
  68. extern int nlmsg_expand(struct nl_msg *, size_t);
  69. extern struct nlmsghdr * nlmsg_put(struct nl_msg *, uint32_t, uint32_t,
  70. int, int, int);
  71. extern struct nlmsghdr * nlmsg_hdr(struct nl_msg *);
  72. extern void nlmsg_get(struct nl_msg *);
  73. extern void nlmsg_free(struct nl_msg *);
  74. /* attribute modification */
  75. extern void nlmsg_set_proto(struct nl_msg *, int);
  76. extern int nlmsg_get_proto(struct nl_msg *);
  77. extern size_t nlmsg_get_max_size(struct nl_msg *);
  78. extern void nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *);
  79. extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *);
  80. extern void nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *);
  81. extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *);
  82. extern void nlmsg_set_creds(struct nl_msg *, struct ucred *);
  83. extern struct ucred * nlmsg_get_creds(struct nl_msg *);
  84. extern char * nl_nlmsgtype2str(int, char *, size_t);
  85. extern int nl_str2nlmsgtype(const char *);
  86. extern char * nl_nlmsg_flags2str(int, char *, size_t);
  87. extern int nl_msg_parse(struct nl_msg *,
  88. void (*cb)(struct nl_object *, void *),
  89. void *);
  90. extern void nl_msg_dump(struct nl_msg *, FILE *);
  91. /**
  92. * @name Iterators
  93. * @{
  94. */
  95. /**
  96. * @ingroup msg
  97. * Iterate over a stream of attributes in a message
  98. * @arg pos loop counter, set to current attribute
  99. * @arg nlh netlink message header
  100. * @arg hdrlen length of family header
  101. * @arg rem initialized to len, holds bytes currently remaining in stream
  102. */
  103. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  104. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  105. nlmsg_attrlen(nlh, hdrlen), rem)
  106. /**
  107. * Iterate over a stream of messages
  108. * @arg pos loop counter, set to current message
  109. * @arg head head of message stream
  110. * @arg len length of message stream
  111. */
  112. #define nlmsg_for_each(pos, head, len) \
  113. for (int rem = len, pos = head; \
  114. nlmsg_ok(pos, rem); \
  115. pos = nlmsg_next(pos, &rem))
  116. #define nlmsg_for_each_msg(pos, head, len, rem) \
  117. nlmsg_for_each(pos, head, len)
  118. /** @} */
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif