socket.h 5.6 KB

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