libmilter.h revision 157001
164562Sgshapiro/*
2157001Sgshapiro * Copyright (c) 1999-2003, 2006 Sendmail, Inc. and its suppliers.
364562Sgshapiro *	All rights reserved.
464562Sgshapiro *
564562Sgshapiro * By using this file, you agree to the terms and conditions set
664562Sgshapiro * forth in the LICENSE file which can be found at the top level of
764562Sgshapiro * the sendmail distribution.
864562Sgshapiro */
964562Sgshapiro
1064562Sgshapiro/*
1164562Sgshapiro**  LIBMILTER.H -- include file for mail filter library functions
1264562Sgshapiro*/
1364562Sgshapiro
1464562Sgshapiro#ifndef _LIBMILTER_H
1564562Sgshapiro# define _LIBMILTER_H	1
1690792Sgshapiro
1790792Sgshapiro#include <sm/gen.h>
1890792Sgshapiro
1964562Sgshapiro#ifdef _DEFINE
2064562Sgshapiro# define EXTERN
2164562Sgshapiro# define INIT(x)	= x
22157001SgshapiroSM_IDSTR(MilterlId, "@(#)$Id: libmilter.h,v 8.51 2006/01/04 02:24:37 ca Exp $")
2364562Sgshapiro#else /* _DEFINE */
2464562Sgshapiro# define EXTERN extern
2564562Sgshapiro# define INIT(x)
2664562Sgshapiro#endif /* _DEFINE */
2764562Sgshapiro
2864562Sgshapiro
2964562Sgshapiro#define NOT_SENDMAIL	1
3064562Sgshapiro#define _SOCK_ADDR	union bigsockaddr
3164562Sgshapiro#include "sendmail.h"
3264562Sgshapiro
3364562Sgshapiro#include "libmilter/milter.h"
3464562Sgshapiro
3564562Sgshapiro# define ValidSocket(sd)	((sd) >= 0)
3690792Sgshapiro# define INVALID_SOCKET		(-1)
3790792Sgshapiro# define closesocket		close
3890792Sgshapiro# define MI_SOCK_READ(s, b, l)	read(s, b, l)
3990792Sgshapiro# define MI_SOCK_READ_FAIL(x)	((x) < 0)
4090792Sgshapiro# define MI_SOCK_WRITE(s, b, l)	write(s, b, l)
4164562Sgshapiro
4264562Sgshapiro# define thread_create(ptid,wr,arg) pthread_create(ptid, NULL, wr, arg)
4364562Sgshapiro# define sthread_get_id()	pthread_self()
4464562Sgshapiro
4571345Sgshapirotypedef pthread_mutex_t smutex_t;
4671345Sgshapiro# define smutex_init(mp)	(pthread_mutex_init(mp, NULL) == 0)
4771345Sgshapiro# define smutex_destroy(mp)	(pthread_mutex_destroy(mp) == 0)
4871345Sgshapiro# define smutex_lock(mp)	(pthread_mutex_lock(mp) == 0)
4971345Sgshapiro# define smutex_unlock(mp)	(pthread_mutex_unlock(mp) == 0)
5071345Sgshapiro# define smutex_trylock(mp)	(pthread_mutex_trylock(mp) == 0)
5171345Sgshapiro
52132943Sgshapiro#if SM_CONF_POLL
53111823Sgshapiro
54111823Sgshapiro# include <poll.h>
55111823Sgshapiro# define MI_POLLSELECT  "poll"
56111823Sgshapiro
57111823Sgshapiro# define MI_POLL_RD_FLAGS (POLLIN | POLLPRI)
58111823Sgshapiro# define MI_POLL_WR_FLAGS (POLLOUT)
59111823Sgshapiro# define MI_MS(timeout)	(((timeout)->tv_sec * 1000) + (timeout)->tv_usec)
60111823Sgshapiro
61111823Sgshapiro# define FD_RD_VAR(rds, excs) struct pollfd rds
62111823Sgshapiro# define FD_WR_VAR(wrs) struct pollfd wrs
63111823Sgshapiro
64111823Sgshapiro# define FD_RD_INIT(sd, rds, excs)			\
65111823Sgshapiro		(rds).fd = (sd);			\
66111823Sgshapiro		(rds).events = MI_POLL_RD_FLAGS;	\
67111823Sgshapiro		(rds).revents = 0
68111823Sgshapiro
69111823Sgshapiro# define FD_WR_INIT(sd, wrs)				\
70111823Sgshapiro		(wrs).fd = (sd);			\
71111823Sgshapiro		(wrs).events = MI_POLL_WR_FLAGS;	\
72111823Sgshapiro		(wrs).revents = 0
73111823Sgshapiro
74111823Sgshapiro# define FD_IS_RD_EXC(sd, rds, excs)	\
75111823Sgshapiro		(((rds).revents & (POLLERR | POLLHUP | POLLNVAL)) != 0)
76111823Sgshapiro
77111823Sgshapiro# define FD_IS_WR_RDY(sd, wrs)		\
78111823Sgshapiro		(((wrs).revents & MI_POLL_WR_FLAGS) != 0)
79111823Sgshapiro
80111823Sgshapiro# define FD_IS_RD_RDY(sd, rds, excs)			\
81111823Sgshapiro		(((rds).revents & MI_POLL_RD_FLAGS) != 0)
82111823Sgshapiro
83111823Sgshapiro# define FD_WR_READY(sd, excs, timeout)	\
84111823Sgshapiro		poll(&(wrs), 1, MI_MS(timeout))
85111823Sgshapiro
86111823Sgshapiro# define FD_RD_READY(sd, rds, excs, timeout)	\
87111823Sgshapiro		poll(&(rds), 1, MI_MS(timeout))
88111823Sgshapiro
89132943Sgshapiro#else /* SM_CONF_POLL */
90111823Sgshapiro
91111823Sgshapiro# include <sm/fdset.h>
92111823Sgshapiro# define MI_POLLSELECT  "select"
93111823Sgshapiro
94111823Sgshapiro# define FD_RD_VAR(rds, excs) fd_set rds, excs
95111823Sgshapiro# define FD_WR_VAR(wrs) fd_set wrs
96111823Sgshapiro
97111823Sgshapiro# define FD_RD_INIT(sd, rds, excs)			\
98111823Sgshapiro		FD_ZERO(&(rds));			\
99111823Sgshapiro		FD_SET((unsigned int) (sd), &(rds));	\
100111823Sgshapiro		FD_ZERO(&(excs));			\
101111823Sgshapiro		FD_SET((unsigned int) (sd), &(excs))
102111823Sgshapiro
103111823Sgshapiro# define FD_WR_INIT(sd, wrs)			\
104111823Sgshapiro		FD_ZERO(&(wrs));			\
105157001Sgshapiro		FD_SET((unsigned int) (sd), &(wrs))
106111823Sgshapiro
107111823Sgshapiro# define FD_IS_RD_EXC(sd, rds, excs) FD_ISSET(sd, &(excs))
108111823Sgshapiro# define FD_IS_WR_RDY(sd, wrs) FD_ISSET((sd), &(wrs))
109111823Sgshapiro# define FD_IS_RD_RDY(sd, rds, excs) FD_ISSET((sd), &(rds))
110111823Sgshapiro
111111823Sgshapiro# define FD_WR_READY(sd, wrs, timeout)	\
112111823Sgshapiro		select((sd) + 1, NULL, &(wrs), NULL, (timeout))
113111823Sgshapiro# define FD_RD_READY(sd, rds, excs, timeout)	\
114111823Sgshapiro		select((sd) + 1, &(rds), NULL, &(excs), (timeout))
115111823Sgshapiro
116132943Sgshapiro#endif /* SM_CONF_POLL */
117111823Sgshapiro
11864562Sgshapiro#include <sys/time.h>
11964562Sgshapiro
12064562Sgshapiro/* version info */
12164562Sgshapiro#define MILTER_PRODUCT_NAME	"libmilter"
12264562Sgshapiro#define MILTER_VERSION		100
12364562Sgshapiro
12464562Sgshapiro/* some defaults */
12577349Sgshapiro#define MI_TIMEOUT	7210		/* default timeout for read/write */
12664562Sgshapiro#define MI_CHK_TIME	5		/* checking whether to terminate */
12764562Sgshapiro
12890792Sgshapiro#ifndef MI_SOMAXCONN
12990792Sgshapiro# if SOMAXCONN > 20
13090792Sgshapiro#  define MI_SOMAXCONN	SOMAXCONN
13190792Sgshapiro# else /* SOMAXCONN */
13290792Sgshapiro#  define MI_SOMAXCONN	20
13390792Sgshapiro# endif /* SOMAXCONN */
13490792Sgshapiro#endif /* ! MI_SOMAXCONN */
13566494Sgshapiro
13664562Sgshapiro/* maximum number of repeated failures in mi_listener() */
13764562Sgshapiro#define MAX_FAILS_M	16	/* malloc() */
13864562Sgshapiro#define MAX_FAILS_T	16	/* thread creation */
13977349Sgshapiro#define MAX_FAILS_A	16	/* accept() */
14090792Sgshapiro#define MAX_FAILS_S	16	/* select() */
14164562Sgshapiro
14264562Sgshapiro/* internal "commands", i.e., error codes */
14364562Sgshapiro#define SMFIC_TIMEOUT	((char) 1)	/* timeout */
14464562Sgshapiro#define SMFIC_SELECT	((char) 2)	/* select error */
14564562Sgshapiro#define SMFIC_MALLOC	((char) 3)	/* malloc error */
14664562Sgshapiro#define SMFIC_RECVERR	((char) 4)	/* recv() error */
14764562Sgshapiro#define SMFIC_EOF	((char) 5)	/* eof */
14864562Sgshapiro#define SMFIC_UNKNERR	((char) 6)	/* unknown error */
14964562Sgshapiro#define SMFIC_TOOBIG	((char) 7)	/* body chunk too big */
15064562Sgshapiro#define SMFIC_VALIDCMD	' '		/* first valid command */
15164562Sgshapiro
15264562Sgshapiro/* hack */
15364562Sgshapiro#define smi_log		syslog
154120256Sgshapiro#define sm_dprintf	(void) printf
15564562Sgshapiro#define milter_ret	int
15664562Sgshapiro#define SMI_LOG_ERR	LOG_ERR
15764562Sgshapiro#define SMI_LOG_FATAL	LOG_ERR
15864562Sgshapiro#define SMI_LOG_WARN	LOG_WARNING
15964562Sgshapiro#define SMI_LOG_INFO	LOG_INFO
16064562Sgshapiro#define SMI_LOG_DEBUG	LOG_DEBUG
16164562Sgshapiro
16264562Sgshapiro/* stop? */
16364562Sgshapiro#define MILTER_CONT	0
16464562Sgshapiro#define MILTER_STOP	1
16564562Sgshapiro#define MILTER_ABRT	2
16664562Sgshapiro
16764562Sgshapiro/* functions */
16864562Sgshapiroextern int	mi_handle_session __P((SMFICTX_PTR));
16964562Sgshapiroextern int	mi_engine __P((SMFICTX_PTR));
17066494Sgshapiroextern int	mi_listener __P((char *, int, smfiDesc_ptr, time_t, int));
17164562Sgshapiroextern void	mi_clr_macros __P((SMFICTX_PTR, int));
17264562Sgshapiroextern int	mi_stop __P((void));
17364562Sgshapiroextern int	mi_control_startup __P((char *));
17464562Sgshapiroextern void	mi_stop_milters __P((int));
17564562Sgshapiroextern void	mi_clean_signals __P((void));
17664562Sgshapiroextern struct hostent *mi_gethostbyname __P((char *, int));
17790792Sgshapiroextern int	mi_inet_pton __P((int, const char *, void *));
17866494Sgshapiroextern void	mi_closener __P((void));
179125820Sgshapiroextern int	mi_opensocket __P((char *, int, int, bool, smfiDesc_ptr));
18064562Sgshapiro
18164562Sgshapiro/* communication functions */
18264562Sgshapiroextern char	*mi_rd_cmd __P((socket_t, struct timeval *, char *, size_t *, char *));
18364562Sgshapiroextern int	mi_wr_cmd __P((socket_t, struct timeval *, int, char *, size_t));
18464562Sgshapiroextern bool	mi_sendok __P((SMFICTX_PTR, int));
18564562Sgshapiro
18690792Sgshapiro
187110560Sgshapiro#endif /* ! _LIBMILTER_H */
188