1 /* Copyright (c) 2013, Vsevolod Stakhov
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *       * Redistributions of source code must retain the above copyright
7  *         notice, this list of conditions and the following disclaimer.
8  *       * Redistributions in binary form must reproduce the above copyright
9  *         notice, this list of conditions and the following disclaimer in the
10  *         documentation and/or other materials provided with the distribution.
11  *
12  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23 
24 #ifndef UTIL_H_
25 #define UTIL_H_
26 
27 #include "config.h"
28 
29 struct rmilter_inet_address {
30 	int family;
31 	union {
32 		struct sockaddr_in sa4;
33 		struct sockaddr_in6 sa6;
34 		struct sockaddr sa;
35 	} addr;
36 };
37 
38 /**
39  * Copy src to dest limited to len, in compare with standard strlcpy(3) rmilter_strlcpy does not
40  * traverse the whole string and it is possible to use it for non NULL terminated strings. This is
41  * more like memccpy(dst, src, size, '\0')
42  *
43  * @param dst destination string
44  * @param src source string
45  * @param siz length of destination buffer
46  * @return bytes copied
47  */
48 size_t rmilter_strlcpy (char *dst, const char *src, size_t siz);
49 
50 typedef struct rmilter_pidfh_s {
51 	int pf_fd;
52 #ifdef HAVE_PATH_MAX
53 	char pf_path[PATH_MAX + 1];
54 #elif defined(HAVE_MAXPATHLEN)
55 	char pf_path[MAXPATHLEN + 1];
56 #else
57 	char pf_path[1024 + 1];
58 #endif
59 	dev_t pf_dev;
60 	ino_t pf_ino;
61 } rmilter_pidfh_t;
62 rmilter_pidfh_t * rmilter_pidfile_open (const char *path,
63 	mode_t mode,
64 	pid_t *pidptr);
65 int rmilter_pidfile_write (rmilter_pidfh_t *pfh);
66 int rmilter_pidfile_close (rmilter_pidfh_t *pfh);
67 int rmilter_pidfile_remove (rmilter_pidfh_t *pfh);
68 
69 #define msg_err(args...) syslog(LOG_ERR, ##args)
70 #define msg_warn(args...)	syslog(LOG_WARNING, ##args)
71 #define msg_info(args...)	syslog(LOG_INFO, ##args)
72 #ifdef WITH_DEBUG
73 #define msg_debug(args...) syslog(LOG_DEBUG, ##args)
74 #else
75 #define msg_debug(args...) do {} while(0)
76 #endif
77 
78 struct mlfi_priv;
79 
80 char* rmilter_encode_base64 (const u_char *in, size_t inlen, int str_len,
81 		size_t *outlen);
82 
83 int rmilter_connect_addr (const char *addr, int port, int msec,
84 		const struct mlfi_priv *priv);
85 int rmilter_poll_fd (int fd, int timeout, short events);
86 
87 #define msec_to_tv(msec, tv) do { (tv)->tv_sec = (msec) / 1000; (tv)->tv_usec = \
88 		((msec) - (tv)->tv_sec * 1000) * 1000; \
89 } while (0)
90 
91 void rmilter_str_lc (char *str, unsigned int size);
92 
93 ssize_t rmilter_atomic_write (int fd, const void *buf, size_t len);
94 
95 int rmilter_file_xopen (const char *fname, int oflags, unsigned int mode);
96 void* rmilter_file_xmap (const char *fname, unsigned int mode, size_t *size);
97 
98 /**
99  * Fold header using rfc822 rules, return new GString from the previous one
100  * @param name name of header (used just for folding)
101  * @param value value of header
102  * @return new GString with the folded value
103  */
104 GString *rmilter_header_value_fold (const gchar *name,
105 		const gchar *value,
106 		guint fold_max);
107 
108 #endif /* UTIL_H_ */
109