xref: /titanic_50/usr/src/cmd/sendmail/libmilter/libmilter.h (revision c13de8f6a88563211bd4432ca11ca38ed3bf0fc0)
1 /*
2  * Copyright (c) 1999-2003, 2006 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9 
10 /*
11 **  LIBMILTER.H -- include file for mail filter library functions
12 */
13 
14 #ifndef _LIBMILTER_H
15 # define _LIBMILTER_H	1
16 
17 #pragma ident	"%Z%%M%	%I%	%E% SMI"
18 
19 #include <sm/gen.h>
20 
21 #ifdef _DEFINE
22 # define EXTERN
23 # define INIT(x)	= x
24 SM_IDSTR(MilterlId, "@(#)$Id: libmilter.h,v 8.51 2006/01/04 02:24:37 ca Exp $")
25 #else /* _DEFINE */
26 # define EXTERN extern
27 # define INIT(x)
28 #endif /* _DEFINE */
29 
30 
31 #define NOT_SENDMAIL	1
32 #define _SOCK_ADDR	union bigsockaddr
33 #include "sendmail.h"
34 
35 #include "libmilter/milter.h"
36 
37 # define ValidSocket(sd)	((sd) >= 0)
38 # define INVALID_SOCKET		(-1)
39 # define closesocket		close
40 # define MI_SOCK_READ(s, b, l)	read(s, b, l)
41 # define MI_SOCK_READ_FAIL(x)	((x) < 0)
42 # define MI_SOCK_WRITE(s, b, l)	write(s, b, l)
43 
44 # define thread_create(ptid,wr,arg) pthread_create(ptid, NULL, wr, arg)
45 # define sthread_get_id()	pthread_self()
46 
47 typedef pthread_mutex_t smutex_t;
48 # define smutex_init(mp)	(pthread_mutex_init(mp, NULL) == 0)
49 # define smutex_destroy(mp)	(pthread_mutex_destroy(mp) == 0)
50 # define smutex_lock(mp)	(pthread_mutex_lock(mp) == 0)
51 # define smutex_unlock(mp)	(pthread_mutex_unlock(mp) == 0)
52 # define smutex_trylock(mp)	(pthread_mutex_trylock(mp) == 0)
53 
54 #if SM_CONF_POLL
55 
56 # include <poll.h>
57 # define MI_POLLSELECT  "poll"
58 
59 # define MI_POLL_RD_FLAGS (POLLIN | POLLPRI)
60 # define MI_POLL_WR_FLAGS (POLLOUT)
61 # define MI_MS(timeout)	(((timeout)->tv_sec * 1000) + (timeout)->tv_usec)
62 
63 # define FD_RD_VAR(rds, excs) struct pollfd rds
64 # define FD_WR_VAR(wrs) struct pollfd wrs
65 
66 # define FD_RD_INIT(sd, rds, excs)			\
67 		(rds).fd = (sd);			\
68 		(rds).events = MI_POLL_RD_FLAGS;	\
69 		(rds).revents = 0
70 
71 # define FD_WR_INIT(sd, wrs)				\
72 		(wrs).fd = (sd);			\
73 		(wrs).events = MI_POLL_WR_FLAGS;	\
74 		(wrs).revents = 0
75 
76 # define FD_IS_RD_EXC(sd, rds, excs)	\
77 		(((rds).revents & (POLLERR | POLLHUP | POLLNVAL)) != 0)
78 
79 # define FD_IS_WR_RDY(sd, wrs)		\
80 		(((wrs).revents & MI_POLL_WR_FLAGS) != 0)
81 
82 # define FD_IS_RD_RDY(sd, rds, excs)			\
83 		(((rds).revents & MI_POLL_RD_FLAGS) != 0)
84 
85 # define FD_WR_READY(sd, excs, timeout)	\
86 		poll(&(wrs), 1, MI_MS(timeout))
87 
88 # define FD_RD_READY(sd, rds, excs, timeout)	\
89 		poll(&(rds), 1, MI_MS(timeout))
90 
91 #else /* SM_CONF_POLL */
92 
93 # include <sm/fdset.h>
94 # define MI_POLLSELECT  "select"
95 
96 # define FD_RD_VAR(rds, excs) fd_set rds, excs
97 # define FD_WR_VAR(wrs) fd_set wrs
98 
99 # define FD_RD_INIT(sd, rds, excs)			\
100 		FD_ZERO(&(rds));			\
101 		FD_SET((unsigned int) (sd), &(rds));	\
102 		FD_ZERO(&(excs));			\
103 		FD_SET((unsigned int) (sd), &(excs))
104 
105 # define FD_WR_INIT(sd, wrs)			\
106 		FD_ZERO(&(wrs));			\
107 		FD_SET((unsigned int) (sd), &(wrs))
108 
109 # define FD_IS_RD_EXC(sd, rds, excs) FD_ISSET(sd, &(excs))
110 # define FD_IS_WR_RDY(sd, wrs) FD_ISSET((sd), &(wrs))
111 # define FD_IS_RD_RDY(sd, rds, excs) FD_ISSET((sd), &(rds))
112 
113 # define FD_WR_READY(sd, wrs, timeout)	\
114 		select((sd) + 1, NULL, &(wrs), NULL, (timeout))
115 # define FD_RD_READY(sd, rds, excs, timeout)	\
116 		select((sd) + 1, &(rds), NULL, &(excs), (timeout))
117 
118 #endif /* SM_CONF_POLL */
119 
120 #include <sys/time.h>
121 
122 /* version info */
123 #define MILTER_PRODUCT_NAME	"libmilter"
124 #define MILTER_VERSION		100
125 
126 /* some defaults */
127 #define MI_TIMEOUT	7210		/* default timeout for read/write */
128 #define MI_CHK_TIME	5		/* checking whether to terminate */
129 
130 #ifndef MI_SOMAXCONN
131 # if SOMAXCONN > 20
132 #  define MI_SOMAXCONN	SOMAXCONN
133 # else /* SOMAXCONN */
134 #  define MI_SOMAXCONN	20
135 # endif /* SOMAXCONN */
136 #endif /* ! MI_SOMAXCONN */
137 
138 /* maximum number of repeated failures in mi_listener() */
139 #define MAX_FAILS_M	16	/* malloc() */
140 #define MAX_FAILS_T	16	/* thread creation */
141 #define MAX_FAILS_A	16	/* accept() */
142 #define MAX_FAILS_S	16	/* select() */
143 
144 /* internal "commands", i.e., error codes */
145 #define SMFIC_TIMEOUT	((char) 1)	/* timeout */
146 #define SMFIC_SELECT	((char) 2)	/* select error */
147 #define SMFIC_MALLOC	((char) 3)	/* malloc error */
148 #define SMFIC_RECVERR	((char) 4)	/* recv() error */
149 #define SMFIC_EOF	((char) 5)	/* eof */
150 #define SMFIC_UNKNERR	((char) 6)	/* unknown error */
151 #define SMFIC_TOOBIG	((char) 7)	/* body chunk too big */
152 #define SMFIC_VALIDCMD	' '		/* first valid command */
153 
154 /* hack */
155 #define smi_log		syslog
156 #define sm_dprintf	(void) printf
157 #define milter_ret	int
158 #define SMI_LOG_ERR	LOG_ERR
159 #define SMI_LOG_FATAL	LOG_ERR
160 #define SMI_LOG_WARN	LOG_WARNING
161 #define SMI_LOG_INFO	LOG_INFO
162 #define SMI_LOG_DEBUG	LOG_DEBUG
163 
164 /* stop? */
165 #define MILTER_CONT	0
166 #define MILTER_STOP	1
167 #define MILTER_ABRT	2
168 
169 /* functions */
170 extern int	mi_handle_session __P((SMFICTX_PTR));
171 extern int	mi_engine __P((SMFICTX_PTR));
172 extern int	mi_listener __P((char *, int, smfiDesc_ptr, time_t, int));
173 extern void	mi_clr_macros __P((SMFICTX_PTR, int));
174 extern int	mi_stop __P((void));
175 extern int	mi_control_startup __P((char *));
176 extern void	mi_stop_milters __P((int));
177 extern void	mi_clean_signals __P((void));
178 extern struct hostent *mi_gethostbyname __P((char *, int));
179 extern int	mi_inet_pton __P((int, const char *, void *));
180 extern void	mi_closener __P((void));
181 extern int	mi_opensocket __P((char *, int, int, bool, smfiDesc_ptr));
182 
183 /* communication functions */
184 extern char	*mi_rd_cmd __P((socket_t, struct timeval *, char *, size_t *, char *));
185 extern int	mi_wr_cmd __P((socket_t, struct timeval *, int, char *, size_t));
186 extern bool	mi_sendok __P((SMFICTX_PTR, int));
187 
188 
189 #endif /* ! _LIBMILTER_H */
190