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