| 1 |
#ifndef LIBNFTNL_UTILS_H |
| 2 |
#define LIBNFTNL_UTILS_H 1 |
| 3 |
|
| 4 |
#include <errno.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <string.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <libnftnl/common.h> |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
#ifdef HAVE_VISIBILITY_HIDDEN |
| 13 |
# define __visible __attribute__((visibility("default"))) |
| 14 |
# define EXPORT_SYMBOL(x) typeof(x) (x) __visible; |
| 15 |
#else |
| 16 |
# define __visible |
| 17 |
# define EXPORT_SYMBOL |
| 18 |
#endif |
| 19 |
|
| 20 |
#define __noreturn __attribute__((__noreturn__)) |
| 21 |
|
| 22 |
#define xfree(ptr) free((void *)ptr); |
| 23 |
|
| 24 |
#define div_round_up(n, d) (((n) + (d) - 1) / (d)) |
| 25 |
|
| 26 |
void __noreturn __abi_breakage(const char *file, int line, const char *reason); |
| 27 |
|
| 28 |
#define abi_breakage() \ |
| 29 |
__abi_breakage(__FILE__, __LINE__, strerror(errno)); |
| 30 |
|
| 31 |
void __nftnl_assert_fail(uint16_t attr, const char *filename, int line); |
| 32 |
|
| 33 |
#define nftnl_assert(val, attr, expr) \ |
| 34 |
((!val || expr) \ |
| 35 |
? (void)0 \ |
| 36 |
: __nftnl_assert_fail(attr, __FILE__, __LINE__)) |
| 37 |
|
| 38 |
#define nftnl_assert_validate(data, _validate_array, _attr, _data_len) \ |
| 39 |
({ \ |
| 40 |
if (!data) \ |
| 41 |
__nftnl_assert_fail(_attr, __FILE__, __LINE__); \ |
| 42 |
if (_validate_array[_attr]) \ |
| 43 |
nftnl_assert(data, _attr, _validate_array[_attr] == _data_len); \ |
| 44 |
}) |
| 45 |
|
| 46 |
void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max, |
| 47 |
const char *filename, int line); |
| 48 |
|
| 49 |
#define nftnl_assert_attr_exists(_attr, _attr_max) \ |
| 50 |
({ \ |
| 51 |
if (_attr > _attr_max) \ |
| 52 |
__nftnl_assert_attr_exists(_attr, _attr_max, __FILE__, __LINE__); \ |
| 53 |
}) |
| 54 |
|
| 55 |
#define SNPRINTF_BUFFER_SIZE(ret, remain, offset) \ |
| 56 |
if (ret < 0) \ |
| 57 |
ret = 0; \ |
| 58 |
offset += ret; \ |
| 59 |
if (ret > remain) \ |
| 60 |
ret = remain; \ |
| 61 |
remain -= ret; \ |
| 62 |
|
| 63 |
|
| 64 |
#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) |
| 65 |
|
| 66 |
#define __must_be_array(a) \ |
| 67 |
BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0]))) |
| 68 |
|
| 69 |
#define array_size(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) |
| 70 |
|
| 71 |
const char *nftnl_family2str(uint32_t family); |
| 72 |
|
| 73 |
const char *nftnl_verdict2str(uint32_t verdict); |
| 74 |
|
| 75 |
enum nftnl_cmd_type nftnl_flag2cmd(uint32_t flags); |
| 76 |
|
| 77 |
int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type, |
| 78 |
uint32_t flags, |
| 79 |
int (*snprintf_cb)(char *buf, size_t bufsiz, const void *obj, |
| 80 |
uint32_t cmd, uint32_t type, |
| 81 |
uint32_t flags)); |
| 82 |
|
| 83 |
int nftnl_set_str_attr(const char **dptr, uint32_t *flags, |
| 84 |
uint16_t attr, const void *data, uint32_t data_len); |
| 85 |
|
| 86 |
struct nlattr; |
| 87 |
|
| 88 |
void nftnl_attr_put_ifname(struct nlmsghdr *nlh, const char *ifname); |
| 89 |
char *nftnl_attr_get_ifname(const struct nlattr *attr); |
| 90 |
|
| 91 |
int nftnl_parse_str_attr(const struct nlattr *tb, int attr, |
| 92 |
const char **field, uint32_t *flags); |
| 93 |
|
| 94 |
#endif |