You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #ifndef RSPAMD_UTIL_H
  2. #define RSPAMD_UTIL_H
  3. #include "config.h"
  4. #include "mem_pool.h"
  5. #include "radix.h"
  6. #include "statfile.h"
  7. #include "printf.h"
  8. #include "fstring.h"
  9. struct config_file;
  10. struct rspamd_main;
  11. struct workq;
  12. struct statfile;
  13. struct classifier_config;
  14. /*
  15. * Create socket and bind or connect it to specified address and port
  16. */
  17. gint make_tcp_socket (struct in_addr *, u_short, gboolean is_server, gboolean async);
  18. /*
  19. * Create socket and bind or connect it to specified address and port
  20. */
  21. gint make_udp_socket (struct in_addr *, u_short, gboolean is_server, gboolean async);
  22. /*
  23. * Accept from socket
  24. */
  25. gint accept_from_socket (gint listen_sock, struct sockaddr *addr, socklen_t *len);
  26. /*
  27. * Create and bind or connect unix socket
  28. */
  29. gint make_unix_socket (const gchar *, struct sockaddr_un *, gboolean is_server, gboolean async);
  30. /**
  31. * Make universal stream socket
  32. * @param credits host, ip or path to unix socket
  33. * @param port port (used for network sockets)
  34. * @param async make this socket asynced
  35. * @param is_server make this socket as server socket
  36. * @param try_resolve try name resolution for a socket (BLOCKING)
  37. */
  38. gint make_universal_stream_socket (const gchar *credits, guint16 port,
  39. gboolean async, gboolean is_server, gboolean try_resolve);
  40. /*
  41. * Create socketpair
  42. */
  43. gint make_socketpair (gint pair[2]);
  44. /*
  45. * Write pid to file
  46. */
  47. gint write_pid (struct rspamd_main *);
  48. /*
  49. * Make specified socket non-blocking
  50. */
  51. gint make_socket_nonblocking (gint);
  52. /*
  53. * Make specified socket blocking
  54. */
  55. gint make_socket_blocking (gint);
  56. /*
  57. * Poll a sync socket for specified events
  58. */
  59. gint poll_sync_socket (gint fd, gint timeout, short events);
  60. /*
  61. * Init signals
  62. */
  63. #ifdef HAVE_SA_SIGINFO
  64. void init_signals (struct sigaction *sa, void (*sig_handler)(gint, siginfo_t *, void *));
  65. #else
  66. void init_signals (struct sigaction *sa, void (*sig_handler)(gint));
  67. #endif
  68. /*
  69. * Send specified signal to each worker
  70. */
  71. void pass_signal_worker (GHashTable *, gint );
  72. /*
  73. * Convert string to lowercase
  74. */
  75. void convert_to_lowercase (gchar *str, guint size);
  76. #ifndef HAVE_SETPROCTITLE
  77. /*
  78. * Process title utility functions
  79. */
  80. gint init_title(gint argc, gchar *argv[], gchar *envp[]);
  81. gint setproctitle(const gchar *fmt, ...);
  82. #endif
  83. #ifndef HAVE_PIDFILE
  84. /*
  85. * Pidfile functions from FreeBSD libutil code
  86. */
  87. typedef struct rspamd_pidfh_s {
  88. gint pf_fd;
  89. #ifdef HAVE_PATH_MAX
  90. gchar pf_path[PATH_MAX + 1];
  91. #elif defined(HAVE_MAXPATHLEN)
  92. gchar pf_path[MAXPATHLEN + 1];
  93. #else
  94. gchar pf_path[1024 + 1];
  95. #endif
  96. dev_t pf_dev;
  97. ino_t pf_ino;
  98. } rspamd_pidfh_t;
  99. rspamd_pidfh_t *rspamd_pidfile_open(const gchar *path, mode_t mode, pid_t *pidptr);
  100. gint rspamd_pidfile_write(rspamd_pidfh_t *pfh);
  101. gint rspamd_pidfile_close(rspamd_pidfh_t *pfh);
  102. gint rspamd_pidfile_remove(rspamd_pidfh_t *pfh);
  103. #else
  104. typedef struct pidfh rspamd_pidfh_t;
  105. #define rspamd_pidfile_open pidfile_open
  106. #define rspamd_pidfile_write pidfile_write
  107. #define rspamd_pidfile_close pidfile_close
  108. #define rspamd_pidfile_remove pidfile_remove
  109. #endif
  110. /*
  111. * Replace %r with rcpt value and %f with from value, new string is allocated in pool
  112. */
  113. gchar* resolve_stat_filename (memory_pool_t *pool, gchar *pattern, gchar *rcpt, gchar *from);
  114. #ifdef HAVE_CLOCK_GETTIME
  115. /*
  116. * Calculate check time with specified resolution of timer
  117. */
  118. const gchar* calculate_check_time (struct timeval *tv, struct timespec *begin, gint resolution);
  119. #else
  120. const gchar* calculate_check_time (struct timeval *begin, gint resolution);
  121. #endif
  122. /*
  123. * File locking functions
  124. */
  125. gboolean lock_file (gint fd, gboolean async);
  126. gboolean unlock_file (gint fd, gboolean async);
  127. /*
  128. * Hash table utility functions for case insensitive hashing
  129. */
  130. guint rspamd_strcase_hash (gconstpointer key);
  131. gboolean rspamd_strcase_equal (gconstpointer v, gconstpointer v2);
  132. /*
  133. * Hash table utility functions for hashing fixed strings
  134. */
  135. guint fstr_strcase_hash (gconstpointer key);
  136. gboolean fstr_strcase_equal (gconstpointer v, gconstpointer v2);
  137. /*
  138. * Google perf-tools initialization function
  139. */
  140. void gperf_profiler_init (struct config_file *cfg, const gchar *descr);
  141. /*
  142. * Workarounds for older versions of glib
  143. */
  144. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 22))
  145. void g_ptr_array_unref (GPtrArray *array);
  146. #endif
  147. #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 14))
  148. void g_queue_clear (GQueue *queue);
  149. #endif
  150. /*
  151. * Copy src to dest limited to len, in compare with standart strlcpy(3) rspamd strlcpy does not
  152. * traverse the whole string and it is possible to use it for non NULL terminated strings. This is
  153. * more like memccpy(dst, src, size, '\0')
  154. *
  155. * @param dst destination string
  156. * @param src source string
  157. * @param siz length of destination buffer
  158. * @return bytes copied
  159. */
  160. gsize rspamd_strlcpy (gchar *dst, const gchar *src, gsize siz);
  161. /*
  162. * Strip <> from email address
  163. */
  164. gchar * escape_braces_addr_fstr (memory_pool_t *pool, f_str_t *in);
  165. /*
  166. * Convert milliseconds to timeval fields
  167. */
  168. #define msec_to_tv(msec, tv) do { (tv)->tv_sec = (msec) / 1000; (tv)->tv_usec = ((msec) - (tv)->tv_sec * 1000) * 1000; } while(0)
  169. #define tv_to_msec(tv) (tv)->tv_sec * 1000 + (tv)->tv_usec / 1000
  170. /* Compare two emails for building emails tree */
  171. gint compare_email_func (gconstpointer a, gconstpointer b);
  172. /* Compare two urls for building emails tree */
  173. gint compare_url_func (gconstpointer a, gconstpointer b);
  174. /*
  175. * Find string find in string s ignoring case
  176. */
  177. gchar* rspamd_strncasestr (const gchar *s, const gchar *find, gint len);
  178. /*
  179. * Try to convert string of length to long
  180. */
  181. gboolean rspamd_strtol (const gchar *s, gsize len, glong *value);
  182. /*
  183. * Try to convert string of length to unsigned long
  184. */
  185. gboolean rspamd_strtoul (const gchar *s, gsize len, gulong *value);
  186. /**
  187. * Try to allocate a file on filesystem (using fallocate or posix_fallocate)
  188. * @param fd descriptor
  189. * @param offset offset of file
  190. * @param len length to allocate
  191. * @return -1 in case of failure
  192. */
  193. gint rspamd_fallocate (gint fd, off_t offset, off_t len);
  194. /**
  195. * Return worker's control structure by its type
  196. * @param type
  197. * @return worker's control structure or NULL
  198. */
  199. worker_t* get_worker_by_type (GQuark type);
  200. #endif