ratectrl.h revision 363466
1/*
2 * Copyright (c) 2003 Proofpoint, 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 * Contributed by Jose Marcio Martins da Cruz - Ecole des Mines de Paris
10 *   Jose-Marcio.Martins@ensmp.fr
11 */
12
13/* a part of this code is based on inetd.c for which this copyright applies: */
14/*
15 * Copyright (c) 1983, 1991, 1993, 1994
16 *      The Regents of the University of California.  All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 *    must display the following acknowledgement:
28 *      This product includes software developed by the University of
29 *      California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 *    may be used to endorse or promote products derived from this software
32 *    without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 */
46
47#ifndef RATECTRL_H
48#define RATECTRL_H 1
49
50#include <sendmail.h>
51
52/*
53**  stuff included - given some warnings (inet_ntoa)
54**	- surely not everything is needed
55*/
56
57#if NETINET || NETINET6
58# include <arpa/inet.h>
59#endif
60
61#include <sm/time.h>
62
63#ifndef HASH_ALG
64# define HASH_ALG	2
65#endif
66
67#ifndef RATECTL_DEBUG
68# define RATECTL_DEBUG  0
69#endif
70
71/* this should be a power of 2, otherwise CPMHMASK doesn't work well */
72#ifndef CPMHSIZE
73# define CPMHSIZE	1024
74#endif
75
76#define CPMHMASK	(CPMHSIZE-1)
77#define CHTSIZE		6
78
79/* Number of connections for a certain "tick" */
80typedef struct CTime
81{
82	unsigned long	ct_Ticks;
83	int		ct_Count;
84}
85CTime_T;
86
87typedef struct CHash
88{
89#if NETINET6 && NETINET
90	union
91	{
92		struct in_addr	c4_Addr;
93		struct in6_addr	c6_Addr;
94	} cu_Addr;
95# define ch_Addr4	cu_Addr.c4_Addr
96# define ch_Addr6	cu_Addr.c6_Addr
97#else /* NETINET6 && NETINET */
98# if NETINET6
99	struct in6_addr	ch_Addr;
100#  define ch_Addr6	ch_Addr
101# else /* NETINET6 */
102	struct in_addr ch_Addr;
103#  define ch_Addr4	ch_Addr
104# endif /* NETINET6 */
105#endif /* NETINET6 && NETINET */
106
107	int		ch_Family;
108	time_t		ch_LTime;
109	unsigned long	ch_colls;
110
111	/* 6 buckets for ticks: 60s */
112	CTime_T		ch_Times[CHTSIZE];
113#if _FFR_OCC
114	int		ch_oc;	/* open connections */
115#endif
116}
117CHash_T;
118
119#define SM_CLFL_NONE	0x00
120#define SM_CLFL_UPDATE	0x01
121#define SM_CLFL_EXC	0x02	/* check if limit is exceeded */
122
123extern void	connection_rate_check __P((SOCKADDR *, ENVELOPE *));
124extern int	conn_limits __P((ENVELOPE *, time_t, SOCKADDR *, int, CHash_T *, int, int));
125extern bool	occ_exceeded __P((ENVELOPE *, MCI *, const char *, SOCKADDR *));
126extern bool	occ_close __P((ENVELOPE *, MCI *, const char *, SOCKADDR *));
127extern void	dump_ch __P((SM_FILE_T *));
128#endif /* ! RATECTRL_H */
129