Thomas Graf | f443be6 | 2011-05-10 12:17:33 +0200 | [diff] [blame] | 1 | %module capi |
| 2 | %{ |
| 3 | #include <netlink/netlink.h> |
| 4 | #include <netlink/types.h> |
| 5 | #include <netlink/socket.h> |
| 6 | #include <netlink/msg.h> |
| 7 | #include <netlink/object.h> |
| 8 | #include <netlink/cache.h> |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 9 | #include <netlink/attr.h> |
| 10 | #include <net/if.h> |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 11 | |
Arend van Spriel | a4a7522 | 2015-05-06 14:26:01 +0200 | [diff] [blame] | 12 | /* enable define below to get swig api debug messages */ |
| 13 | /*#define DEBUG*/ |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 14 | #include "utils.h" |
Thomas Graf | f443be6 | 2011-05-10 12:17:33 +0200 | [diff] [blame] | 15 | %} |
| 16 | |
| 17 | %include <stdint.i> |
| 18 | %include <cstring.i> |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 19 | %include <cpointer.i> |
Arend van Spriel | 36f4adf | 2015-05-10 12:22:17 +0200 | [diff] [blame] | 20 | %include <exception.i> |
Thomas Graf | f443be6 | 2011-05-10 12:17:33 +0200 | [diff] [blame] | 21 | |
| 22 | %inline %{ |
| 23 | struct nl_dump_params *alloc_dump_params(void) |
| 24 | { |
| 25 | struct nl_dump_params *dp; |
| 26 | if (!(dp = calloc(1, sizeof(*dp)))) |
| 27 | return NULL; |
| 28 | dp->dp_fd = stdout; |
| 29 | return dp; |
| 30 | } |
| 31 | |
| 32 | void free_dump_params(struct nl_dump_params *dp) |
| 33 | { |
| 34 | free(dp); |
| 35 | } |
| 36 | %}; |
| 37 | |
| 38 | /* <netlink/types.h> */ |
| 39 | |
| 40 | enum nl_dump_type { |
| 41 | NL_DUMP_LINE, /**< Dump object briefly on one line */ |
| 42 | NL_DUMP_DETAILS, /**< Dump all attributes but no statistics */ |
| 43 | NL_DUMP_STATS, /**< Dump all attributes including statistics */ |
| 44 | __NL_DUMP_MAX, |
| 45 | }; |
| 46 | |
| 47 | struct nl_dump_params |
| 48 | { |
| 49 | /** |
| 50 | * Specifies the type of dump that is requested. |
| 51 | */ |
| 52 | enum nl_dump_type dp_type; |
| 53 | |
| 54 | /** |
| 55 | * Specifies the number of whitespaces to be put in front |
| 56 | * of every new line (indentation). |
| 57 | */ |
| 58 | int dp_prefix; |
| 59 | |
| 60 | /** |
| 61 | * Causes the cache index to be printed for each element. |
| 62 | */ |
| 63 | int dp_print_index; |
| 64 | |
| 65 | /** |
| 66 | * Causes each element to be prefixed with the message type. |
| 67 | */ |
| 68 | int dp_dump_msgtype; |
| 69 | |
| 70 | /** |
| 71 | * A callback invoked for output |
| 72 | * |
| 73 | * Passed arguments are: |
| 74 | * - dumping parameters |
| 75 | * - string to append to the output |
| 76 | */ |
| 77 | void (*dp_cb)(struct nl_dump_params *, char *); |
| 78 | |
| 79 | /** |
| 80 | * A callback invoked for every new line, can be used to |
| 81 | * customize the indentation. |
| 82 | * |
| 83 | * Passed arguments are: |
| 84 | * - dumping parameters |
| 85 | * - line number starting from 0 |
| 86 | */ |
| 87 | void (*dp_nl_cb)(struct nl_dump_params *, int); |
| 88 | |
| 89 | /** |
| 90 | * User data pointer, can be used to pass data to callbacks. |
| 91 | */ |
| 92 | void *dp_data; |
| 93 | |
| 94 | /** |
| 95 | * File descriptor the dumping output should go to |
| 96 | */ |
| 97 | FILE * dp_fd; |
| 98 | |
| 99 | /** |
| 100 | * Alternatively the output may be redirected into a buffer |
| 101 | */ |
| 102 | char * dp_buf; |
| 103 | |
| 104 | /** |
| 105 | * Length of the buffer dp_buf |
| 106 | */ |
| 107 | size_t dp_buflen; |
| 108 | |
| 109 | /** |
| 110 | * PRIVATE |
| 111 | * Set if a dump was performed prior to the actual dump handler. |
| 112 | */ |
| 113 | int dp_pre_dump; |
| 114 | |
| 115 | /** |
| 116 | * PRIVATE |
| 117 | * Owned by the current caller |
| 118 | */ |
| 119 | int dp_ivar; |
| 120 | |
| 121 | unsigned int dp_line; |
| 122 | }; |
| 123 | |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 124 | /* <net/if.h> */ |
| 125 | extern unsigned int if_nametoindex(const char *ifname); |
| 126 | |
Thomas Graf | f443be6 | 2011-05-10 12:17:33 +0200 | [diff] [blame] | 127 | /* <netlink/errno.h> */ |
| 128 | extern const char *nl_geterror(int); |
| 129 | |
| 130 | /* <netlink/utils.h> */ |
| 131 | |
| 132 | extern double nl_cancel_down_bytes(unsigned long long, char **); |
| 133 | extern double nl_cancel_down_bits(unsigned long long, char **); |
Thomas Graf | e56eb06 | 2011-06-10 13:55:29 +0200 | [diff] [blame] | 134 | %cstring_output_maxsize(char *buf, size_t len) |
| 135 | extern int nl_rate2str(unsigned long long rate, int type, char *buf, size_t len); |
Thomas Graf | f443be6 | 2011-05-10 12:17:33 +0200 | [diff] [blame] | 136 | extern double nl_cancel_down_us(uint32_t, char **); |
| 137 | |
| 138 | extern long nl_size2int(const char *); |
| 139 | %cstring_output_maxsize(char *buf, const size_t len) |
| 140 | extern char *nl_size2str(const size_t, char *buf, const size_t len); |
| 141 | extern long nl_prob2int(const char *); |
| 142 | |
| 143 | extern int nl_get_user_hz(void); |
| 144 | extern uint32_t nl_us2ticks(uint32_t); |
| 145 | extern uint32_t nl_ticks2us(uint32_t); |
| 146 | extern int nl_str2msec(const char *, uint64_t *); |
| 147 | |
| 148 | %cstring_output_maxsize(char *buf, size_t len) |
| 149 | extern char *nl_msec2str(uint64_t, char *buf, size_t len); |
| 150 | |
| 151 | %cstring_output_maxsize(char *buf, size_t len) |
| 152 | extern char *nl_llproto2str(int, char *buf, size_t len); |
| 153 | extern int nl_str2llproto(const char *); |
| 154 | |
| 155 | %cstring_output_maxsize(char *buf, size_t len) |
| 156 | extern char *nl_ether_proto2str(int, char *buf, size_t len); |
| 157 | extern int nl_str2ether_proto(const char *); |
| 158 | |
| 159 | %cstring_output_maxsize(char *buf, size_t len) |
| 160 | extern char *nl_ip_proto2str(int, char *buf, size_t len); |
| 161 | extern int nl_str2ip_proto(const char *); |
| 162 | |
| 163 | extern void nl_new_line(struct nl_dump_params *); |
| 164 | extern void nl_dump(struct nl_dump_params *, const char *, ...); |
| 165 | extern void nl_dump_line(struct nl_dump_params *, const char *, ...); |
| 166 | |
| 167 | /* <netlink/netlink.h> */ |
| 168 | extern struct nl_dump_params *alloc_dump_params(void); |
| 169 | extern void free_dump_params(struct nl_dump_params *); |
| 170 | |
| 171 | extern int nl_connect(struct nl_sock *, int); |
| 172 | extern void nl_close(struct nl_sock *); |
Thomas Graf | f443be6 | 2011-05-10 12:17:33 +0200 | [diff] [blame] | 173 | |
| 174 | /* <netlink/socket.h> */ |
| 175 | extern struct nl_sock *nl_socket_alloc(void); |
| 176 | extern struct nl_sock *nl_socket_alloc_cb(struct nl_cb *); |
| 177 | extern void nl_socket_free(struct nl_sock *); |
| 178 | |
| 179 | extern uint32_t nl_socket_get_local_port(const struct nl_sock *); |
| 180 | extern void nl_socket_set_local_port(struct nl_sock *, uint32_t); |
| 181 | |
| 182 | extern uint32_t nl_socket_get_peer_port(const struct nl_sock *); |
| 183 | extern void nl_socket_set_peer_port(struct nl_sock *, uint32_t); |
| 184 | |
| 185 | extern uint32_t nl_socket_get_peer_groups(const struct nl_sock *sk); |
| 186 | extern void nl_socket_set_peer_groups(struct nl_sock *sk, uint32_t groups); |
| 187 | |
| 188 | extern int nl_socket_set_buffer_size(struct nl_sock *, int, int); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 189 | extern void nl_socket_set_cb(struct nl_sock *, struct nl_cb *); |
| 190 | |
Arend van Spriel | ff64671 | 2015-05-10 12:20:06 +0200 | [diff] [blame] | 191 | extern int nl_socket_add_membership(struct nl_sock *, int); |
| 192 | extern int nl_socket_drop_membership(struct nl_sock *, int); |
| 193 | |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 194 | extern int nl_send_auto_complete(struct nl_sock *, struct nl_msg *); |
| 195 | extern int nl_recvmsgs(struct nl_sock *, struct nl_cb *); |
Thomas Graf | f443be6 | 2011-05-10 12:17:33 +0200 | [diff] [blame] | 196 | |
| 197 | /* <netlink/msg.h> */ |
| 198 | extern int nlmsg_size(int); |
| 199 | extern int nlmsg_total_size(int); |
| 200 | extern int nlmsg_padlen(int); |
| 201 | |
| 202 | extern void * nlmsg_data(const struct nlmsghdr *); |
| 203 | extern int nlmsg_datalen(const struct nlmsghdr *); |
| 204 | extern void * nlmsg_tail(const struct nlmsghdr *); |
| 205 | |
| 206 | /* attribute access */ |
| 207 | extern struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int); |
| 208 | extern int nlmsg_attrlen(const struct nlmsghdr *, int); |
| 209 | |
| 210 | /* message parsing */ |
| 211 | extern int nlmsg_valid_hdr(const struct nlmsghdr *, int); |
| 212 | extern int nlmsg_ok(const struct nlmsghdr *, int); |
| 213 | extern struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *); |
| 214 | extern int nlmsg_parse(struct nlmsghdr *, int, struct nlattr **, |
| 215 | int, struct nla_policy *); |
| 216 | extern struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int); |
| 217 | extern int nlmsg_validate(struct nlmsghdr *, int, int, |
| 218 | struct nla_policy *); |
| 219 | |
| 220 | extern struct nl_msg * nlmsg_alloc(void); |
| 221 | extern struct nl_msg * nlmsg_alloc_size(size_t); |
| 222 | extern struct nl_msg * nlmsg_alloc_simple(int, int); |
| 223 | extern void nlmsg_set_default_size(size_t); |
| 224 | extern struct nl_msg * nlmsg_inherit(struct nlmsghdr *); |
| 225 | extern struct nl_msg * nlmsg_convert(struct nlmsghdr *); |
| 226 | extern void * nlmsg_reserve(struct nl_msg *, size_t, int); |
| 227 | extern int nlmsg_append(struct nl_msg *, void *, size_t, int); |
| 228 | extern int nlmsg_expand(struct nl_msg *, size_t); |
| 229 | |
| 230 | extern struct nlmsghdr * nlmsg_put(struct nl_msg *, uint32_t, uint32_t, |
| 231 | int, int, int); |
| 232 | extern struct nlmsghdr * nlmsg_hdr(struct nl_msg *); |
| 233 | extern void nlmsg_get(struct nl_msg *); |
| 234 | extern void nlmsg_free(struct nl_msg *); |
| 235 | |
| 236 | /* attribute modification */ |
| 237 | extern void nlmsg_set_proto(struct nl_msg *, int); |
| 238 | extern int nlmsg_get_proto(struct nl_msg *); |
| 239 | extern size_t nlmsg_get_max_size(struct nl_msg *); |
| 240 | extern void nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *); |
| 241 | extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *); |
| 242 | extern void nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *); |
| 243 | extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *); |
| 244 | extern void nlmsg_set_creds(struct nl_msg *, struct ucred *); |
| 245 | extern struct ucred * nlmsg_get_creds(struct nl_msg *); |
| 246 | |
| 247 | extern char * nl_nlmsgtype2str(int, char *, size_t); |
| 248 | extern int nl_str2nlmsgtype(const char *); |
| 249 | |
| 250 | extern char * nl_nlmsg_flags2str(int, char *, size_t); |
| 251 | |
| 252 | extern int nl_msg_parse(struct nl_msg *, |
| 253 | void (*cb)(struct nl_object *, void *), |
| 254 | void *); |
| 255 | |
| 256 | extern void nl_msg_dump(struct nl_msg *, FILE *); |
| 257 | |
| 258 | %inline %{ |
| 259 | struct nl_object *cast_obj(void *obj) |
| 260 | { |
| 261 | return (struct nl_object *) obj; |
| 262 | } |
| 263 | |
| 264 | struct nl_object *object_alloc_name(const char *name) |
| 265 | { |
| 266 | struct nl_object *obj; |
| 267 | |
| 268 | if (nl_object_alloc_name(name, &obj) < 0) |
| 269 | return NULL; |
| 270 | |
| 271 | return obj; |
| 272 | } |
| 273 | %}; |
| 274 | |
| 275 | extern struct nl_object *nl_object_alloc(struct nl_object_ops *); |
| 276 | extern void nl_object_free(struct nl_object *); |
| 277 | extern struct nl_object *nl_object_clone(struct nl_object *); |
| 278 | extern void nl_object_get(struct nl_object *); |
| 279 | extern void nl_object_put(struct nl_object *); |
| 280 | extern int nl_object_shared(struct nl_object *); |
| 281 | |
| 282 | %cstring_output_maxsize(char *buf, size_t len) |
| 283 | extern void nl_object_dump_buf(struct nl_object *, char *buf, size_t len); |
| 284 | |
| 285 | extern void nl_object_dump(struct nl_object *, struct nl_dump_params *); |
| 286 | |
| 287 | extern int nl_object_identical(struct nl_object *, struct nl_object *); |
| 288 | extern uint32_t nl_object_diff(struct nl_object *, struct nl_object *); |
| 289 | extern int nl_object_match_filter(struct nl_object *, struct nl_object *); |
| 290 | |
| 291 | %cstring_output_maxsize(char *buf, size_t len) |
| 292 | extern char *nl_object_attrs2str(struct nl_object *, uint32_t, char *buf, size_t len); |
| 293 | |
| 294 | %cstring_output_maxsize(char *buf, size_t len) |
| 295 | extern char *nl_object_attr_list(struct nl_object *, char *buf, size_t len); |
| 296 | |
| 297 | extern void nl_object_mark(struct nl_object *); |
| 298 | extern void nl_object_unmark(struct nl_object *); |
| 299 | extern int nl_object_is_marked(struct nl_object *); |
| 300 | |
| 301 | extern int nl_object_get_refcnt(struct nl_object *); |
| 302 | |
| 303 | /* <netlink/cache.h> */ |
| 304 | |
| 305 | typedef void (*change_func_t)(struct nl_cache *, struct nl_object *, int, void *); |
| 306 | |
| 307 | %inline %{ |
| 308 | struct nl_cache *alloc_cache_name(const char *name) |
| 309 | { |
| 310 | struct nl_cache *c; |
| 311 | if (nl_cache_alloc_name(name, &c) < 0) |
| 312 | return NULL; |
| 313 | return c; |
| 314 | } |
| 315 | |
| 316 | struct nl_cache_mngr *alloc_cache_mngr(struct nl_sock *sock, |
| 317 | int protocol, int flags) |
| 318 | { |
| 319 | struct nl_cache_mngr *mngr; |
| 320 | |
| 321 | if (nl_cache_mngr_alloc(sock, protocol, flags, &mngr) < 0) |
| 322 | return NULL; |
| 323 | |
| 324 | return mngr; |
| 325 | } |
| 326 | |
| 327 | struct nl_cache *cache_mngr_add(struct nl_cache_mngr *mngr, |
| 328 | const char *name, change_func_t func, |
| 329 | void *arg) |
| 330 | { |
| 331 | struct nl_cache *cache; |
| 332 | |
| 333 | if (nl_cache_mngr_add(mngr, name, func, arg, &cache) < 0) |
| 334 | return NULL; |
| 335 | |
| 336 | return cache; |
| 337 | } |
| 338 | %} |
| 339 | |
| 340 | /* Access Functions */ |
| 341 | extern int nl_cache_nitems(struct nl_cache *); |
| 342 | extern int nl_cache_nitems_filter(struct nl_cache *, |
| 343 | struct nl_object *); |
| 344 | extern struct nl_cache_ops * nl_cache_get_ops(struct nl_cache *); |
| 345 | extern struct nl_object * nl_cache_get_first(struct nl_cache *); |
| 346 | extern struct nl_object * nl_cache_get_last(struct nl_cache *); |
| 347 | extern struct nl_object * nl_cache_get_next(struct nl_object *); |
| 348 | extern struct nl_object * nl_cache_get_prev(struct nl_object *); |
| 349 | |
| 350 | extern struct nl_cache * nl_cache_alloc(struct nl_cache_ops *); |
| 351 | extern struct nl_cache * nl_cache_subset(struct nl_cache *, |
| 352 | struct nl_object *); |
| 353 | extern void nl_cache_clear(struct nl_cache *); |
| 354 | extern void nl_cache_free(struct nl_cache *); |
| 355 | |
| 356 | /* Cache modification */ |
| 357 | extern int nl_cache_add(struct nl_cache *, |
| 358 | struct nl_object *); |
| 359 | extern int nl_cache_parse_and_add(struct nl_cache *, |
| 360 | struct nl_msg *); |
| 361 | extern void nl_cache_remove(struct nl_object *); |
| 362 | extern int nl_cache_refill(struct nl_sock *, |
| 363 | struct nl_cache *); |
| 364 | extern int nl_cache_pickup(struct nl_sock *, |
| 365 | struct nl_cache *); |
| 366 | extern int nl_cache_resync(struct nl_sock *, |
| 367 | struct nl_cache *, |
| 368 | change_func_t, |
| 369 | void *); |
| 370 | extern int nl_cache_include(struct nl_cache *, |
| 371 | struct nl_object *, |
| 372 | change_func_t, |
| 373 | void *); |
| 374 | extern void nl_cache_set_arg1(struct nl_cache *, int); |
| 375 | extern void nl_cache_set_arg2(struct nl_cache *, int); |
| 376 | |
| 377 | /* General */ |
| 378 | extern int nl_cache_is_empty(struct nl_cache *); |
| 379 | extern struct nl_object * nl_cache_search(struct nl_cache *, |
| 380 | struct nl_object *); |
| 381 | extern void nl_cache_mark_all(struct nl_cache *); |
| 382 | |
| 383 | /* Dumping */ |
| 384 | extern void nl_cache_dump(struct nl_cache *, |
| 385 | struct nl_dump_params *); |
| 386 | extern void nl_cache_dump_filter(struct nl_cache *, |
| 387 | struct nl_dump_params *, |
| 388 | struct nl_object *); |
| 389 | |
| 390 | /* Iterators */ |
| 391 | extern void nl_cache_foreach(struct nl_cache *, |
| 392 | void (*cb)(struct nl_object *, |
| 393 | void *), |
| 394 | void *arg); |
| 395 | extern void nl_cache_foreach_filter(struct nl_cache *, |
| 396 | struct nl_object *, |
| 397 | void (*cb)(struct |
| 398 | nl_object *, |
| 399 | void *), |
| 400 | void *arg); |
| 401 | |
| 402 | /* --- cache management --- */ |
| 403 | |
| 404 | /* Cache type management */ |
| 405 | extern struct nl_cache_ops * nl_cache_ops_lookup(const char *); |
| 406 | extern struct nl_cache_ops * nl_cache_ops_associate(int, int); |
| 407 | extern struct nl_msgtype * nl_msgtype_lookup(struct nl_cache_ops *, int); |
| 408 | extern void nl_cache_ops_foreach(void (*cb)(struct nl_cache_ops *, void *), void *); |
| 409 | extern int nl_cache_mngt_register(struct nl_cache_ops *); |
| 410 | extern int nl_cache_mngt_unregister(struct nl_cache_ops *); |
| 411 | |
| 412 | /* Global cache provisioning/requiring */ |
| 413 | extern void nl_cache_mngt_provide(struct nl_cache *); |
| 414 | extern void nl_cache_mngt_unprovide(struct nl_cache *); |
| 415 | extern struct nl_cache * nl_cache_mngt_require(const char *); |
| 416 | |
| 417 | struct nl_cache_mngr; |
| 418 | |
| 419 | #define NL_AUTO_PROVIDE 1 |
| 420 | |
| 421 | extern int nl_cache_mngr_get_fd(struct nl_cache_mngr *); |
| 422 | extern int nl_cache_mngr_poll(struct nl_cache_mngr *, |
| 423 | int); |
| 424 | extern int nl_cache_mngr_data_ready(struct nl_cache_mngr *); |
| 425 | extern void nl_cache_mngr_free(struct nl_cache_mngr *); |
| 426 | |
| 427 | /* <netlink/addr.h> */ |
| 428 | %inline %{ |
| 429 | struct nl_addr *addr_parse(const char *addr, int guess) |
| 430 | { |
| 431 | struct nl_addr *result; |
| 432 | |
| 433 | if (nl_addr_parse(addr, guess, &result) < 0) |
| 434 | return NULL; |
| 435 | |
| 436 | return result; |
| 437 | } |
| 438 | %}; |
| 439 | |
| 440 | extern struct nl_addr *nl_addr_alloc(size_t); |
| 441 | extern struct nl_addr *nl_addr_alloc_attr(struct nlattr *, int); |
| 442 | extern struct nl_addr *nl_addr_build(int, void *, size_t); |
| 443 | extern struct nl_addr *nl_addr_clone(struct nl_addr *); |
| 444 | |
| 445 | extern struct nl_addr *nl_addr_get(struct nl_addr *); |
| 446 | extern void nl_addr_put(struct nl_addr *); |
| 447 | extern int nl_addr_shared(struct nl_addr *); |
| 448 | |
| 449 | extern int nl_addr_cmp(struct nl_addr *, struct nl_addr *); |
| 450 | extern int nl_addr_cmp_prefix(struct nl_addr *, struct nl_addr *); |
| 451 | extern int nl_addr_iszero(struct nl_addr *); |
| 452 | extern int nl_addr_valid(char *, int); |
| 453 | extern int nl_addr_guess_family(struct nl_addr *); |
| 454 | extern int nl_addr_fill_sockaddr(struct nl_addr *, struct sockaddr *, socklen_t *); |
| 455 | extern int nl_addr_info(struct nl_addr *, struct addrinfo **); |
| 456 | extern int nl_addr_resolve(struct nl_addr *addr, char *host, size_t hostlen); |
| 457 | |
| 458 | extern void nl_addr_set_family(struct nl_addr *, int); |
| 459 | extern int nl_addr_get_family(struct nl_addr *); |
| 460 | extern int nl_addr_set_binary_addr(struct nl_addr *, void *, size_t); |
| 461 | |
| 462 | extern void *nl_addr_get_binary_addr(struct nl_addr *); |
| 463 | extern unsigned int nl_addr_get_len(struct nl_addr *); |
| 464 | extern void nl_addr_set_prefixlen(struct nl_addr *, int); |
| 465 | extern unsigned int nl_addr_get_prefixlen(struct nl_addr *); |
| 466 | |
| 467 | %cstring_output_maxsize(char *buf, size_t len) |
| 468 | extern char *nl_af2str(int, char *buf, size_t len); |
| 469 | extern int nl_str2af(const char *); |
| 470 | |
| 471 | %cstring_output_maxsize(char *buf, size_t len) |
| 472 | extern char *nl_addr2str(struct nl_addr *, char *buf, size_t len); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 473 | |
| 474 | /* Message Handlers <netlink/handlers.h> */ |
| 475 | /** |
| 476 | * Callback actions |
| 477 | * @ingroup cb |
| 478 | */ |
| 479 | enum nl_cb_action { |
| 480 | /** Proceed with wathever would come next */ |
| 481 | NL_OK, |
| 482 | /** Skip this message */ |
| 483 | NL_SKIP, |
| 484 | /** Stop parsing altogether and discard remaining messages */ |
| 485 | NL_STOP, |
| 486 | }; |
| 487 | |
| 488 | /** |
| 489 | * Callback kinds |
| 490 | * @ingroup cb |
| 491 | */ |
| 492 | enum nl_cb_kind { |
| 493 | /** Default handlers (quiet) */ |
| 494 | NL_CB_DEFAULT, |
| 495 | /** Verbose default handlers (error messages printed) */ |
| 496 | NL_CB_VERBOSE, |
| 497 | /** Debug handlers for debugging */ |
| 498 | NL_CB_DEBUG, |
| 499 | /** Customized handler specified by the user */ |
| 500 | NL_CB_CUSTOM, |
| 501 | __NL_CB_KIND_MAX, |
| 502 | }; |
| 503 | |
| 504 | #define NL_CB_KIND_MAX (__NL_CB_KIND_MAX - 1) |
| 505 | |
| 506 | /** |
| 507 | * Callback types |
| 508 | * @ingroup cb |
| 509 | */ |
| 510 | enum nl_cb_type { |
| 511 | /** Message is valid */ |
| 512 | NL_CB_VALID, |
| 513 | /** Last message in a series of multi part messages received */ |
| 514 | NL_CB_FINISH, |
| 515 | /** Report received that data was lost */ |
| 516 | NL_CB_OVERRUN, |
| 517 | /** Message wants to be skipped */ |
| 518 | NL_CB_SKIPPED, |
| 519 | /** Message is an acknowledge */ |
| 520 | NL_CB_ACK, |
| 521 | /** Called for every message received */ |
| 522 | NL_CB_MSG_IN, |
| 523 | /** Called for every message sent out except for nl_sendto() */ |
| 524 | NL_CB_MSG_OUT, |
| 525 | /** Message is malformed and invalid */ |
| 526 | NL_CB_INVALID, |
| 527 | /** Called instead of internal sequence number checking */ |
| 528 | NL_CB_SEQ_CHECK, |
| 529 | /** Sending of an acknowledge message has been requested */ |
| 530 | NL_CB_SEND_ACK, |
| 531 | /** Flag NLM_F_DUMP_INTR is set in message */ |
| 532 | NL_CB_DUMP_INTR, |
| 533 | __NL_CB_TYPE_MAX, |
| 534 | }; |
| 535 | |
| 536 | #define NL_CB_TYPE_MAX (__NL_CB_TYPE_MAX - 1) |
| 537 | |
| 538 | extern struct nl_cb *nl_cb_alloc(enum nl_cb_kind); |
| 539 | extern struct nl_cb *nl_cb_clone(struct nl_cb *); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 540 | |
| 541 | struct nlmsgerr { |
| 542 | int error; |
| 543 | }; |
| 544 | |
| 545 | %{ |
| 546 | |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 547 | struct pynl_callback { |
| 548 | PyObject *cbf; |
| 549 | PyObject *cba; |
| 550 | }; |
| 551 | |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 552 | struct pynl_cbinfo { |
| 553 | struct nl_cb *cb; |
| 554 | struct pynl_callback cbtype[NL_CB_TYPE_MAX+1]; |
| 555 | struct pynl_callback cberr; |
| 556 | struct list_head list; |
| 557 | }; |
| 558 | |
| 559 | LIST_HEAD(callback_list); |
| 560 | |
| 561 | static struct pynl_cbinfo *pynl_find_cbinfo(struct nl_cb *cb, int unlink) |
| 562 | { |
| 563 | struct list_head *pos, *prev; |
| 564 | struct pynl_cbinfo *info; |
| 565 | |
| 566 | list_for_each_safe(pos, prev, &callback_list) { |
| 567 | info = container_of(pos, struct pynl_cbinfo, list); |
| 568 | if (info->cb == cb) { |
| 569 | if (unlink) |
| 570 | list_del(pos, prev); |
| 571 | pynl_dbg("cb=%p: found=%p\n", cb, info); |
| 572 | return info; |
| 573 | } |
| 574 | } |
| 575 | pynl_dbg("cb=%p: not found\n", cb); |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | static struct pynl_cbinfo *pynl_get_cbinfo(struct nl_cb *cb, int unlink) |
| 580 | { |
| 581 | struct pynl_cbinfo *info; |
| 582 | |
| 583 | info = pynl_find_cbinfo(cb, unlink); |
| 584 | |
| 585 | if (info || unlink) { |
| 586 | /* found or no need to allocate a new one */ |
| 587 | pynl_dbg("cb=%p: done\n", cb); |
| 588 | return info; |
| 589 | } |
| 590 | |
| 591 | info = calloc(1, sizeof(*info)); |
| 592 | info->cb = cb; |
| 593 | list_add(&info->list, &callback_list); |
| 594 | pynl_dbg("cb=%p: added %p\n", cb, info); |
| 595 | return info; |
| 596 | } |
| 597 | |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 598 | static int nl_recv_msg_handler(struct nl_msg *msg, void *arg) |
| 599 | { |
| 600 | struct pynl_callback *cbd = arg; |
| 601 | PyObject *msgobj; |
| 602 | PyObject *cbparobj; |
| 603 | PyObject *resobj; |
Arend van Spriel | 522cf98 | 2013-09-05 14:11:27 +0200 | [diff] [blame] | 604 | PyObject *funcobj; |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 605 | int result; |
| 606 | |
Arend van Spriel | ae7f8cc | 2013-09-30 10:21:46 +0200 | [diff] [blame] | 607 | if (!cbd) { |
| 608 | result = NL_STOP; |
| 609 | goto done; |
| 610 | } |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 611 | msgobj = SWIG_NewPointerObj(SWIG_as_voidptr(msg), |
| 612 | SWIGTYPE_p_nl_msg, 0 | 0 ); |
Arend van Spriel | 522cf98 | 2013-09-05 14:11:27 +0200 | [diff] [blame] | 613 | /* add selfobj if callback is a method */ |
| 614 | if (cbd->cbf && PyMethod_Check(cbd->cbf)) { |
| 615 | PyObject *selfobj = PyMethod_Self(cbd->cbf); |
| 616 | cbparobj = Py_BuildValue("(OOO)", selfobj ? selfobj : cbd->cba, |
| 617 | msgobj, cbd->cba); |
| 618 | funcobj = PyMethod_Function(cbd->cbf); |
| 619 | pynl_dbg("callback %sbounded instance method %p\n", |
| 620 | selfobj ? "" : "un", funcobj); |
| 621 | } else { |
| 622 | cbparobj = Py_BuildValue("(OO)", msgobj, cbd->cba); |
| 623 | funcobj = cbd->cbf; |
| 624 | pynl_dbg("callback function %p\n", funcobj); |
| 625 | } |
| 626 | resobj = PyObject_CallObject(funcobj, cbparobj); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 627 | Py_DECREF(cbparobj); |
Arend van Spriel | ae7f8cc | 2013-09-30 10:21:46 +0200 | [diff] [blame] | 628 | if (resobj && PyInt_Check(resobj)) |
| 629 | result = (int)PyInt_AsLong(resobj); |
| 630 | else |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 631 | result = NL_STOP; |
Arend van Spriel | ae7f8cc | 2013-09-30 10:21:46 +0200 | [diff] [blame] | 632 | Py_XDECREF(resobj); |
| 633 | done: |
| 634 | pynl_dbg("result=%d\n", result); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 635 | return result; |
| 636 | } |
| 637 | |
| 638 | static int nl_recv_err_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, |
| 639 | void *arg) |
| 640 | { |
| 641 | struct pynl_callback *cbd = arg; |
| 642 | PyObject *errobj; |
| 643 | PyObject *cbparobj; |
| 644 | PyObject *resobj; |
Arend van Spriel | 522cf98 | 2013-09-05 14:11:27 +0200 | [diff] [blame] | 645 | PyObject *funcobj; |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 646 | int result; |
| 647 | |
| 648 | if (!cbd) |
| 649 | return NL_STOP; |
| 650 | errobj = SWIG_NewPointerObj(SWIG_as_voidptr(err), |
| 651 | SWIGTYPE_p_nlmsgerr, 0 | 0 ); |
Arend van Spriel | 522cf98 | 2013-09-05 14:11:27 +0200 | [diff] [blame] | 652 | /* add selfobj if callback is a method */ |
| 653 | if (cbd->cbf && PyMethod_Check(cbd->cbf)) { |
| 654 | PyObject *selfobj = PyMethod_Self(cbd->cbf); |
| 655 | cbparobj = Py_BuildValue("(OOO)", selfobj ? selfobj : cbd->cba, |
| 656 | errobj, cbd->cba); |
| 657 | funcobj = PyMethod_Function(cbd->cbf); |
| 658 | } else { |
| 659 | cbparobj = Py_BuildValue("(OO)", errobj, cbd->cba); |
| 660 | funcobj = cbd->cbf; |
| 661 | } |
| 662 | resobj = PyObject_CallObject(funcobj, cbparobj); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 663 | Py_DECREF(cbparobj); |
Arend van Spriel | ae7f8cc | 2013-09-30 10:21:46 +0200 | [diff] [blame] | 664 | if (resobj && PyInt_Check(resobj)) |
| 665 | result = (int)PyInt_AsLong(resobj); |
| 666 | else |
| 667 | result = NL_STOP; |
| 668 | Py_XDECREF(resobj); |
| 669 | pynl_dbg("error: err=%d ret=%d\n", err->error, result); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 670 | return result; |
| 671 | } |
| 672 | |
| 673 | %} |
| 674 | %inline %{ |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 675 | struct nl_cb *py_nl_cb_clone(struct nl_cb *cb) |
| 676 | { |
| 677 | struct pynl_cbinfo *info, *clone_info; |
| 678 | struct nl_cb *clone; |
| 679 | int i; |
| 680 | |
| 681 | clone = nl_cb_clone(cb); |
| 682 | info = pynl_find_cbinfo(cb, 0); |
| 683 | if (info) { |
| 684 | clone_info = pynl_get_cbinfo(clone, 0); |
| 685 | /* increase refcnt to callback parameters and copy them */ |
| 686 | for (i = 0; info && i <= NL_CB_TYPE_MAX; i++) { |
| 687 | Py_XINCREF(info->cbtype[i].cbf); |
| 688 | Py_XINCREF(info->cbtype[i].cba); |
| 689 | clone_info->cbtype[i].cbf = info->cbtype[i].cbf; |
| 690 | clone_info->cbtype[i].cba = info->cbtype[i].cba; |
| 691 | } |
| 692 | Py_XINCREF(info->cberr.cbf); |
| 693 | Py_XINCREF(info->cberr.cba); |
| 694 | clone_info->cberr.cbf = info->cberr.cbf; |
| 695 | clone_info->cberr.cba = info->cberr.cba; |
| 696 | } |
| 697 | return clone; |
| 698 | } |
| 699 | |
| 700 | void py_nl_cb_put(struct nl_cb *cb) |
| 701 | { |
| 702 | struct pynl_cbinfo *info; |
| 703 | int i; |
| 704 | |
| 705 | /* obtain callback info (and unlink) */ |
| 706 | info = pynl_get_cbinfo(cb, 1); |
| 707 | pynl_dbg("cb=%p, info=%p\n", cb, info); |
| 708 | /* decrease refcnt for callback type handlers */ |
| 709 | for (i = 0; info && i <= NL_CB_TYPE_MAX; i++) { |
| 710 | Py_XDECREF(info->cbtype[i].cbf); |
| 711 | Py_XDECREF(info->cbtype[i].cba); |
| 712 | } |
| 713 | /* decrease refcnt for error handler and free callback info */ |
| 714 | if (info) { |
| 715 | Py_XDECREF(info->cberr.cbf); |
| 716 | Py_XDECREF(info->cberr.cba); |
| 717 | free(info); |
| 718 | } |
| 719 | nl_cb_put(cb); |
| 720 | } |
| 721 | |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 722 | int py_nl_cb_set(struct nl_cb *cb, enum nl_cb_type t, enum nl_cb_kind k, |
| 723 | PyObject *func, PyObject *a) |
| 724 | { |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 725 | struct pynl_cbinfo *info; |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 726 | |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 727 | /* obtain callback info */ |
| 728 | info = pynl_get_cbinfo(cb, 0); |
| 729 | |
| 730 | /* clear existing handlers (if any) */ |
| 731 | Py_XDECREF(info->cbtype[t].cbf); |
| 732 | Py_XDECREF(info->cbtype[t].cba); |
| 733 | info->cbtype[t].cbf = NULL; |
| 734 | info->cbtype[t].cba = NULL; |
| 735 | pynl_dbg("cb=%p, info=%p, type=%d, kind=%d\n", cb, info, t, k); |
| 736 | /* handle custom callback */ |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 737 | if (k == NL_CB_CUSTOM) { |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 738 | Py_XINCREF(func); |
| 739 | Py_XINCREF(a); |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 740 | info->cbtype[t].cbf = func; |
| 741 | info->cbtype[t].cba = a; |
| 742 | return nl_cb_set(cb, t, k, |
| 743 | nl_recv_msg_handler, &info->cbtype[t]); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 744 | } |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 745 | return nl_cb_set(cb, t, k, NULL, NULL); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | int py_nl_cb_set_all(struct nl_cb *cb, enum nl_cb_kind k, |
| 749 | PyObject *func , PyObject *a) |
| 750 | { |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 751 | struct pynl_cbinfo *info; |
| 752 | int t; |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 753 | |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 754 | info = pynl_get_cbinfo(cb, 0); |
| 755 | pynl_dbg("cb=%p, info=%p, kind=%d\n", cb, info, k); |
| 756 | for (t = 0; t <= NL_CB_TYPE_MAX; t++) { |
| 757 | /* (possibly) free existing handler */ |
| 758 | Py_XDECREF(info->cbtype[t].cbf); |
| 759 | Py_XDECREF(info->cbtype[t].cba); |
| 760 | info->cbtype[t].cbf = NULL; |
| 761 | info->cbtype[t].cba = NULL; |
| 762 | if (k == NL_CB_CUSTOM) { |
| 763 | Py_XINCREF(func); |
| 764 | Py_XINCREF(a); |
| 765 | info->cbtype[t].cbf = func; |
| 766 | info->cbtype[t].cba = a; |
| 767 | } |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 768 | } |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 769 | if (k == NL_CB_CUSTOM) |
| 770 | /* callback argument is same for all so using idx 0 here */ |
| 771 | return nl_cb_set_all(cb, k, nl_recv_msg_handler, |
| 772 | &info->cbtype[0]); |
| 773 | else |
| 774 | return nl_cb_set_all(cb, k, NULL, NULL); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | int py_nl_cb_err(struct nl_cb *cb, enum nl_cb_kind k, |
| 778 | PyObject *func, PyObject *a) |
| 779 | { |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 780 | struct pynl_cbinfo *info; |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 781 | |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 782 | /* obtain callback info */ |
| 783 | info = pynl_get_cbinfo(cb, 0); |
| 784 | pynl_dbg("cb=%p, info=%p, kind=%d\n", cb, info, k); |
| 785 | /* clear existing handlers (if any) */ |
| 786 | Py_XDECREF(info->cberr.cbf); |
| 787 | Py_XDECREF(info->cberr.cba); |
| 788 | info->cberr.cbf = NULL; |
| 789 | info->cberr.cba = NULL; |
| 790 | |
| 791 | /* handle custom callback */ |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 792 | if (k == NL_CB_CUSTOM) { |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 793 | Py_XINCREF(func); |
| 794 | Py_XINCREF(a); |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 795 | info->cberr.cbf = func; |
| 796 | info->cberr.cba = a; |
| 797 | return nl_cb_err(cb, k, |
| 798 | nl_recv_err_handler, &info->cberr); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 799 | } |
Arend van Spriel | 86207d4 | 2013-09-05 14:11:26 +0200 | [diff] [blame] | 800 | return nl_cb_err(cb, k, NULL, NULL); |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 801 | } |
| 802 | %} |
| 803 | |
| 804 | /* Attributes <netlink/attr.h> */ |
| 805 | /* |
| 806 | * This typemap is a bit tricky as it uses arg1, which is knowledge about |
| 807 | * the SWIGged wrapper output. |
| 808 | */ |
| 809 | %typemap(out) void * { |
| 810 | $result = PyByteArray_FromStringAndSize($1, nla_len(arg1)); |
| 811 | } |
| 812 | extern void *nla_data(struct nlattr *); |
| 813 | %typemap(out) void *; |
| 814 | extern int nla_type(const struct nlattr *); |
| 815 | |
Arend van Spriel | 36f4adf | 2015-05-10 12:22:17 +0200 | [diff] [blame] | 816 | %typemap(in) (int, const void *) { |
| 817 | $1 = Py_SIZE($input); |
| 818 | if (PyByteArray_Check($input)) { |
| 819 | $2 = ($2_ltype)PyByteArray_AsString($input); |
| 820 | } else if (PyString_Check($input)) { |
| 821 | $2 = ($2_ltype)PyString_AsString($input); |
| 822 | } else |
| 823 | SWIG_exception(SWIG_TypeError, |
| 824 | "pointer must be bytearray or string."); |
| 825 | } |
| 826 | extern int nla_put(struct nl_msg *, int, int, const void *); |
| 827 | %typemap(in) const void *; |
| 828 | |
Arend van Spriel | e77ea93 | 2013-07-15 12:09:11 +0200 | [diff] [blame] | 829 | /* Integer attribute */ |
| 830 | extern uint8_t nla_get_u8(struct nlattr *); |
| 831 | extern int nla_put_u8(struct nl_msg *, int, uint8_t); |
| 832 | extern uint16_t nla_get_u16(struct nlattr *); |
| 833 | extern int nla_put_u16(struct nl_msg *, int, uint16_t); |
| 834 | extern uint32_t nla_get_u32(struct nlattr *); |
| 835 | extern int nla_put_u32(struct nl_msg *, int, uint32_t); |
| 836 | extern uint64_t nla_get_u64(struct nlattr *); |
| 837 | extern int nla_put_u64(struct nl_msg *, int, uint64_t); |
| 838 | |
| 839 | /* String attribute */ |
| 840 | extern char * nla_get_string(struct nlattr *); |
| 841 | extern char * nla_strdup(struct nlattr *); |
| 842 | extern int nla_put_string(struct nl_msg *, int, const char *); |
| 843 | |
| 844 | /* Flag attribute */ |
| 845 | extern int nla_get_flag(struct nlattr *); |
| 846 | extern int nla_put_flag(struct nl_msg *, int); |
| 847 | |
| 848 | /* Msec attribute */ |
| 849 | extern unsigned long nla_get_msecs(struct nlattr *); |
| 850 | extern int nla_put_msecs(struct nl_msg *, int, unsigned long); |
| 851 | |
| 852 | /* Attribute nesting */ |
| 853 | extern int nla_put_nested(struct nl_msg *, int, struct nl_msg *); |
| 854 | extern struct nlattr * nla_nest_start(struct nl_msg *, int); |
| 855 | extern int nla_nest_end(struct nl_msg *, struct nlattr *); |
| 856 | %inline %{ |
| 857 | PyObject *py_nla_parse_nested(int max, struct nlattr *nest_attr, PyObject *p) |
| 858 | { |
| 859 | struct nlattr *tb_msg[max + 1]; |
| 860 | struct nla_policy *policy = NULL; |
| 861 | void *pol; |
| 862 | PyObject *attrs = Py_None; |
| 863 | PyObject *k; |
| 864 | PyObject *v; |
| 865 | PyObject *resobj; |
| 866 | int err; |
| 867 | int i; |
| 868 | |
| 869 | if (p != Py_None) { |
| 870 | PyObject *pobj; |
| 871 | |
| 872 | if (!PyList_Check(p)) { |
| 873 | fprintf(stderr, "expected list object\n"); |
| 874 | err = -1; |
| 875 | goto fail; |
| 876 | } |
| 877 | pobj = PyList_GetItem(p, 0); |
| 878 | err = SWIG_ConvertPtr(pobj, &pol, SWIGTYPE_p_nla_policy, 0 | 0 ); |
| 879 | if (!SWIG_IsOK(err)) |
| 880 | goto fail; |
| 881 | policy = pol; |
| 882 | } |
| 883 | err = nla_parse_nested(tb_msg, max, nest_attr, policy); |
| 884 | if (err < 0) { |
| 885 | fprintf(stderr, "Failed to parse response message\n"); |
| 886 | } else { |
| 887 | attrs = PyDict_New(); |
| 888 | for (i = 0; i <= max; i++) |
| 889 | if (tb_msg[i]) { |
| 890 | k = PyInt_FromLong((long)i); |
| 891 | v = SWIG_NewPointerObj(SWIG_as_voidptr(tb_msg[i]), SWIGTYPE_p_nlattr, 0 | 0 ); |
| 892 | PyDict_SetItem(attrs, k, v); |
| 893 | } |
| 894 | } |
| 895 | fail: |
| 896 | if (attrs == Py_None) |
| 897 | Py_INCREF(attrs); |
| 898 | resobj = Py_BuildValue("(iO)", err, attrs); |
| 899 | return resobj; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * nla_get_nested() - get list of nested attributes. |
| 904 | * |
| 905 | * nla_for_each_<nested|attr>() is a macro construct that needs another approach |
| 906 | * for Python. Create and return list of nested attributes. |
| 907 | */ |
| 908 | PyObject *nla_get_nested(struct nlattr *nest_attr) |
| 909 | { |
| 910 | PyObject *listobj; |
| 911 | PyObject *nestattrobj; |
| 912 | struct nlattr *pos; |
| 913 | int rem; |
| 914 | |
| 915 | listobj = PyList_New(0); |
| 916 | nla_for_each_nested(pos, nest_attr, rem) { |
| 917 | nestattrobj = SWIG_NewPointerObj(SWIG_as_voidptr(pos), |
| 918 | SWIGTYPE_p_nlattr, 0 | 0 ); |
| 919 | PyList_Append(listobj, nestattrobj); |
| 920 | } |
| 921 | return listobj; |
| 922 | } |
| 923 | %} |
| 924 | |
| 925 | /** |
| 926 | * @ingroup attr |
| 927 | * Basic attribute data types |
| 928 | * |
| 929 | * See \ref attr_datatypes for more details. |
| 930 | */ |
| 931 | enum { |
| 932 | NLA_UNSPEC, /**< Unspecified type, binary data chunk */ |
| 933 | NLA_U8, /**< 8 bit integer */ |
| 934 | NLA_U16, /**< 16 bit integer */ |
| 935 | NLA_U32, /**< 32 bit integer */ |
| 936 | NLA_U64, /**< 64 bit integer */ |
| 937 | NLA_STRING, /**< NUL terminated character string */ |
| 938 | NLA_FLAG, /**< Flag */ |
| 939 | NLA_MSECS, /**< Micro seconds (64bit) */ |
| 940 | NLA_NESTED, /**< Nested attributes */ |
| 941 | __NLA_TYPE_MAX, |
| 942 | }; |
| 943 | |
| 944 | #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) |
| 945 | |
| 946 | /** @} */ |
| 947 | |
| 948 | /** |
| 949 | * @ingroup attr |
| 950 | * Attribute validation policy. |
| 951 | * |
| 952 | * See \ref attr_datatypes for more details. |
| 953 | */ |
| 954 | struct nla_policy { |
| 955 | /** Type of attribute or NLA_UNSPEC */ |
| 956 | uint16_t type; |
| 957 | |
| 958 | /** Minimal length of payload required */ |
| 959 | uint16_t minlen; |
| 960 | |
| 961 | /** Maximal length of payload allowed */ |
| 962 | uint16_t maxlen; |
| 963 | }; |
| 964 | |
| 965 | %inline %{ |
| 966 | PyObject *nla_policy_array(int n_items) |
| 967 | { |
| 968 | struct nla_policy *policies; |
| 969 | PyObject *listobj; |
| 970 | PyObject *polobj; |
| 971 | int i; |
| 972 | |
| 973 | policies = calloc(n_items, sizeof(*policies)); |
| 974 | listobj = PyList_New(n_items); |
| 975 | for (i = 0; i < n_items; i++) { |
| 976 | polobj = SWIG_NewPointerObj(SWIG_as_voidptr(&policies[i]), |
| 977 | SWIGTYPE_p_nla_policy, 0 | 0 ); |
| 978 | PyList_SetItem(listobj, i, polobj); |
| 979 | } |
| 980 | return listobj; |
| 981 | } |
| 982 | %} |