utils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef __FW3_UTILS_H
  19. #define __FW3_UTILS_H
  20. #include <stdlib.h>
  21. #include <stdbool.h>
  22. #include <unistd.h>
  23. #include <signal.h>
  24. #include <fcntl.h>
  25. #include <limits.h>
  26. #include <sys/stat.h>
  27. #include <sys/wait.h>
  28. #include <sys/file.h>
  29. #include <sys/types.h>
  30. #include <ifaddrs.h>
  31. #include <netdb.h>
  32. #include <libubox/list.h>
  33. #include <libubox/blob.h>
  34. #include <uci.h>
  35. #define FW3_STATEFILE "/var/run/fw3.state"
  36. #define FW3_LOCKFILE "/var/run/fw3.lock"
  37. #define FW3_HELPERCONF "/usr/share/fw3/helpers.conf"
  38. #define FW3_HOTPLUG "/sbin/hotplug-call"
  39. extern bool fw3_pr_debug;
  40. struct fw3_address;
  41. void warn_elem(struct uci_element *e, const char *format, ...)
  42. __attribute__ ((format (printf, 2, 3)));
  43. void warn(const char *format, ...)
  44. __attribute__ ((format (printf, 1, 2)));
  45. void error(const char *format, ...)
  46. __attribute__ ((format (printf, 1, 2)));
  47. void info(const char *format, ...)
  48. __attribute__ ((format (printf, 1, 2)));
  49. #define warn_section(t, r, e, fmt, ...) \
  50. do { \
  51. if (e) \
  52. warn_elem(e, fmt, ##__VA_ARGS__); \
  53. else \
  54. warn("Warning: ubus " t " (%s) " fmt, \
  55. (r && r->name) ? r->name : "?", ##__VA_ARGS__); \
  56. } while(0)
  57. #define fw3_setbit(field, flag) field |= (1 << (flag))
  58. #define fw3_delbit(field, flag) field &= ~(1 << (flag))
  59. #define fw3_hasbit(field, flag) (field & (1 << (flag)))
  60. #define set(field, family, flag) fw3_setbit(field[family == FW3_FAMILY_V6], flag)
  61. #define del(field, family, flag) fw3_delbit(field[family == FW3_FAMILY_V6], flag)
  62. #define has(field, family, flag) fw3_hasbit(field[family == FW3_FAMILY_V6], flag)
  63. #define fw3_foreach(p, h) \
  64. for (p = list_empty(h) ? NULL : list_first_entry(h, typeof(*p), list); \
  65. list_empty(h) ? (p == NULL) : (&p->list != (h)); \
  66. p = list_empty(h) ? list_first_entry(h, typeof(*p), list) \
  67. : list_entry(p->list.next, typeof(*p), list))
  68. #define fw3_is_family(p, f) \
  69. (!p || (p)->family == FW3_FAMILY_ANY || (p)->family == f)
  70. #define fw3_no_family(flags) \
  71. (!(flags & ((1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6))))
  72. #define fw3_no_table(flags) \
  73. (!(flags & ((1<<FW3_TABLE_FILTER)|(1<<FW3_TABLE_NAT)| \
  74. (1<<FW3_TABLE_MANGLE)|(1<<FW3_TABLE_RAW))))
  75. void * fw3_alloc(size_t size);
  76. char * fw3_strdup(const char *s);
  77. const char * fw3_find_command(const char *cmd);
  78. bool fw3_stdout_pipe(void);
  79. bool __fw3_command_pipe(bool silent, const char *command, ...);
  80. #define fw3_command_pipe(...) __fw3_command_pipe(__VA_ARGS__, NULL)
  81. void fw3_command_close(void);
  82. void fw3_pr(const char *fmt, ...)
  83. __attribute__ ((format (printf, 1, 2)));
  84. bool fw3_has_target(const bool ipv6, const char *target);
  85. bool fw3_lock(void);
  86. void fw3_unlock(void);
  87. bool fw3_lock_path(int *fw3_lock_fd, const char *path);
  88. void fw3_unlock_path(int *fw3_lock_fd, const char *path);
  89. void fw3_write_statefile(void *state);
  90. void fw3_free_object(void *obj, const void *opts);
  91. void fw3_free_list(struct list_head *head);
  92. bool fw3_hotplug(bool add, void *zone, void *device);
  93. int fw3_netmask2bitlen(int family, void *mask);
  94. bool fw3_bitlen2netmask(int family, int bits, void *mask);
  95. void fw3_flush_conntrack(void *zone);
  96. bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type);
  97. const char * fw3_protoname(void *proto);
  98. bool fw3_check_loopback_dev(const char *name);
  99. bool fw3_check_loopback_addr(struct fw3_address *addr);
  100. #endif