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