PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/package/libs/libnl-tiny/src/include/netlink/socket.h

https://gitlab.com/YoungCereal/openwrt-bpi-r1
C Header | 221 lines | 102 code | 38 blank | 81 comment | 0 complexity | f4f3de279f23ec6fbdcf555c20d68379 MD5 | raw file
  1. /*
  2. * netlink/socket.h Netlink Socket
  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. #ifndef NETLINK_SOCKET_H_
  12. #define NETLINK_SOCKET_H_
  13. #include <netlink/types.h>
  14. #include <netlink/handlers.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define NL_SOCK_BUFSIZE_SET (1<<0)
  19. #define NL_SOCK_PASSCRED (1<<1)
  20. #define NL_OWN_PORT (1<<2)
  21. #define NL_MSG_PEEK (1<<3)
  22. #define NL_NO_AUTO_ACK (1<<4)
  23. struct nl_cb;
  24. struct nl_sock
  25. {
  26. struct sockaddr_nl s_local;
  27. struct sockaddr_nl s_peer;
  28. int s_fd;
  29. int s_proto;
  30. unsigned int s_seq_next;
  31. unsigned int s_seq_expect;
  32. int s_flags;
  33. struct nl_cb * s_cb;
  34. };
  35. extern struct nl_sock * nl_socket_alloc(void);
  36. extern struct nl_sock * nl_socket_alloc_cb(struct nl_cb *);
  37. extern void nl_socket_free(struct nl_sock *);
  38. extern void nl_socket_set_local_port(struct nl_sock *, uint32_t);
  39. extern int nl_socket_add_memberships(struct nl_sock *, int, ...);
  40. extern int nl_socket_drop_memberships(struct nl_sock *, int, ...);
  41. extern int nl_socket_set_buffer_size(struct nl_sock *, int, int);
  42. extern int nl_socket_set_passcred(struct nl_sock *, int);
  43. extern int nl_socket_recv_pktinfo(struct nl_sock *, int);
  44. extern void nl_socket_disable_seq_check(struct nl_sock *);
  45. extern int nl_socket_set_nonblocking(struct nl_sock *);
  46. /**
  47. * Use next sequence number
  48. * @arg sk Netlink socket.
  49. *
  50. * Uses the next available sequence number and increases the counter
  51. * by one for subsequent calls.
  52. *
  53. * @return Unique serial sequence number
  54. */
  55. static inline unsigned int nl_socket_use_seq(struct nl_sock *sk)
  56. {
  57. return sk->s_seq_next++;
  58. }
  59. /**
  60. * Disable automatic request for ACK
  61. * @arg sk Netlink socket.
  62. *
  63. * The default behaviour of a socket is to request an ACK for
  64. * each message sent to allow for the caller to synchronize to
  65. * the completion of the netlink operation. This function
  66. * disables this behaviour and will result in requests being
  67. * sent which will not have the NLM_F_ACK flag set automatically.
  68. * However, it is still possible for the caller to set the
  69. * NLM_F_ACK flag explicitely.
  70. */
  71. static inline void nl_socket_disable_auto_ack(struct nl_sock *sk)
  72. {
  73. sk->s_flags |= NL_NO_AUTO_ACK;
  74. }
  75. /**
  76. * Enable automatic request for ACK (default)
  77. * @arg sk Netlink socket.
  78. * @see nl_socket_disable_auto_ack
  79. */
  80. static inline void nl_socket_enable_auto_ack(struct nl_sock *sk)
  81. {
  82. sk->s_flags &= ~NL_NO_AUTO_ACK;
  83. }
  84. /**
  85. * @name Source Idenficiation
  86. * @{
  87. */
  88. static inline uint32_t nl_socket_get_local_port(struct nl_sock *sk)
  89. {
  90. return sk->s_local.nl_pid;
  91. }
  92. /**
  93. * Join multicast groups (deprecated)
  94. * @arg sk Netlink socket.
  95. * @arg groups Bitmask of groups to join.
  96. *
  97. * This function defines the old way of joining multicast group which
  98. * has to be done prior to calling nl_connect(). It works on any kernel
  99. * version but is very limited as only 32 groups can be joined.
  100. */
  101. static inline void nl_join_groups(struct nl_sock *sk, int groups)
  102. {
  103. sk->s_local.nl_groups |= groups;
  104. }
  105. /**
  106. * @name Peer Identfication
  107. * @{
  108. */
  109. static inline uint32_t nl_socket_get_peer_port(struct nl_sock *sk)
  110. {
  111. return sk->s_peer.nl_pid;
  112. }
  113. static inline void nl_socket_set_peer_port(struct nl_sock *sk, uint32_t port)
  114. {
  115. sk->s_peer.nl_pid = port;
  116. }
  117. /** @} */
  118. /**
  119. * @name File Descriptor
  120. * @{
  121. */
  122. static inline int nl_socket_get_fd(struct nl_sock *sk)
  123. {
  124. return sk->s_fd;
  125. }
  126. /**
  127. * Enable use of MSG_PEEK when reading from socket
  128. * @arg sk Netlink socket.
  129. */
  130. static inline void nl_socket_enable_msg_peek(struct nl_sock *sk)
  131. {
  132. sk->s_flags |= NL_MSG_PEEK;
  133. }
  134. /**
  135. * Disable use of MSG_PEEK when reading from socket
  136. * @arg sk Netlink socket.
  137. */
  138. static inline void nl_socket_disable_msg_peek(struct nl_sock *sk)
  139. {
  140. sk->s_flags &= ~NL_MSG_PEEK;
  141. }
  142. /**
  143. * @name Callback Handler
  144. * @{
  145. */
  146. static inline struct nl_cb *nl_socket_get_cb(struct nl_sock *sk)
  147. {
  148. return nl_cb_get(sk->s_cb);
  149. }
  150. static inline void nl_socket_set_cb(struct nl_sock *sk, struct nl_cb *cb)
  151. {
  152. nl_cb_put(sk->s_cb);
  153. sk->s_cb = nl_cb_get(cb);
  154. }
  155. /**
  156. * Modify the callback handler associated to the socket
  157. * @arg sk Netlink socket.
  158. * @arg type which type callback to set
  159. * @arg kind kind of callback
  160. * @arg func callback function
  161. * @arg arg argument to be passwd to callback function
  162. *
  163. * @see nl_cb_set
  164. */
  165. static inline int nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type,
  166. enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func,
  167. void *arg)
  168. {
  169. return nl_cb_set(sk->s_cb, type, kind, func, arg);
  170. }
  171. /** @} */
  172. static inline int nl_socket_add_membership(struct nl_sock *sk, int group)
  173. {
  174. return nl_socket_add_memberships(sk, group, 0);
  175. }
  176. static inline int nl_socket_drop_membership(struct nl_sock *sk, int group)
  177. {
  178. return nl_socket_drop_memberships(sk, group, 0);
  179. }
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif