| 1 | /* |
| 2 | * netlink-local.h Local Netlink 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-2008 Thomas Graf <tgraf@suug.ch> |
| 10 | */ |
| 11 | |
| 12 | #ifndef NETLINK_LOCAL_H_ |
| 13 | #define NETLINK_LOCAL_H_ |
| 14 | #define _GNU_SOURCE |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <errno.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <math.h> |
| 23 | #include <time.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <ctype.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <inttypes.h> |
| 29 | #include <assert.h> |
| 30 | #include <limits.h> |
| 31 | |
| 32 | #include <arpa/inet.h> |
| 33 | #include <netdb.h> |
| 34 | |
| 35 | #ifndef SOL_NETLINK |
| 36 | #define SOL_NETLINK 270 |
| 37 | #endif |
| 38 | |
| 39 | #include <linux/types.h> |
| 40 | |
| 41 | /* local header copies */ |
| 42 | #include <linux/if.h> |
| 43 | #include <linux/if_arp.h> |
| 44 | #include <linux/if_ether.h> |
| 45 | #include <linux/pkt_sched.h> |
| 46 | #include <linux/pkt_cls.h> |
| 47 | #include <linux/gen_stats.h> |
| 48 | |
| 49 | #include <netlink/netlink.h> |
| 50 | #include <netlink/handlers.h> |
| 51 | #include <netlink/cache.h> |
| 52 | #include <netlink/object-api.h> |
| 53 | #include <netlink/cache-api.h> |
| 54 | #include <netlink-types.h> |
| 55 | |
| 56 | struct trans_tbl { |
| 57 | int i; |
| 58 | const char *a; |
| 59 | }; |
| 60 | |
| 61 | #define __ADD(id, name) { .i = id, .a = #name }, |
| 62 | |
| 63 | struct trans_list { |
| 64 | int i; |
| 65 | char *a; |
| 66 | struct nl_list_head list; |
| 67 | }; |
| 68 | |
| 69 | #define NL_DEBUG 1 |
| 70 | |
| 71 | #define NL_DBG(LVL,FMT,ARG...) \ |
| 72 | do {} while (0) |
| 73 | |
| 74 | #define BUG() \ |
| 75 | do { \ |
| 76 | fprintf(stderr, "BUG: %s:%d\n", \ |
| 77 | __FILE__, __LINE__); \ |
| 78 | assert(0); \ |
| 79 | } while (0) |
| 80 | |
| 81 | extern int __nl_read_num_str_file(const char *path, |
| 82 | int (*cb)(long, const char *)); |
| 83 | |
| 84 | extern int __trans_list_add(int, const char *, struct nl_list_head *); |
| 85 | extern void __trans_list_clear(struct nl_list_head *); |
| 86 | |
| 87 | extern char *__type2str(int, char *, size_t, struct trans_tbl *, size_t); |
| 88 | extern int __str2type(const char *, struct trans_tbl *, size_t); |
| 89 | |
| 90 | extern char *__list_type2str(int, char *, size_t, struct nl_list_head *); |
| 91 | extern int __list_str2type(const char *, struct nl_list_head *); |
| 92 | |
| 93 | extern char *__flags2str(int, char *, size_t, struct trans_tbl *, size_t); |
| 94 | extern int __str2flags(const char *, struct trans_tbl *, size_t); |
| 95 | |
| 96 | extern void dump_from_ops(struct nl_object *, struct nl_dump_params *); |
| 97 | |
| 98 | #ifdef disabled |
| 99 | static inline struct nl_cache *dp_cache(struct nl_object *obj) |
| 100 | { |
| 101 | if (obj->ce_cache == NULL) |
| 102 | return nl_cache_mngt_require(obj->ce_ops->oo_name); |
| 103 | |
| 104 | return obj->ce_cache; |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg) |
| 109 | { |
| 110 | return cb->cb_set[type](msg, cb->cb_args[type]); |
| 111 | } |
| 112 | |
| 113 | #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0])) |
| 114 | #ifndef offsetof |
| 115 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 116 | #endif |
| 117 | |
| 118 | #define __init __attribute__ ((constructor)) |
| 119 | #define __exit __attribute__ ((destructor)) |
| 120 | #undef __deprecated |
| 121 | #define __deprecated __attribute__ ((deprecated)) |
| 122 | |
| 123 | #define min(x,y) ({ \ |
| 124 | typeof(x) _x = (x); \ |
| 125 | typeof(y) _y = (y); \ |
| 126 | (void) (&_x == &_y); \ |
| 127 | _x < _y ? _x : _y; }) |
| 128 | |
| 129 | #define max(x,y) ({ \ |
| 130 | typeof(x) _x = (x); \ |
| 131 | typeof(y) _y = (y); \ |
| 132 | (void) (&_x == &_y); \ |
| 133 | _x > _y ? _x : _y; }) |
| 134 | |
| 135 | extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *, |
| 136 | struct nlmsghdr *, struct nl_parser_param *); |
| 137 | |
| 138 | |
| 139 | static inline char *nl_cache_name(struct nl_cache *cache) |
| 140 | { |
| 141 | return cache->c_ops ? cache->c_ops->co_name : "unknown"; |
| 142 | } |
| 143 | |
| 144 | #define GENL_FAMILY(id, name) \ |
| 145 | { \ |
| 146 | { id, NL_ACT_UNSPEC, name }, \ |
| 147 | END_OF_MSGTYPES_LIST, \ |
| 148 | } |
| 149 | |
| 150 | static inline int wait_for_ack(struct nl_sock *sk) |
| 151 | { |
| 152 | if (sk->s_flags & NL_NO_AUTO_ACK) |
| 153 | return 0; |
| 154 | else |
| 155 | return nl_wait_for_ack(sk); |
| 156 | } |
| 157 | |
| 158 | #endif |
| 159 | |