/*
* Copyright (c) 2003 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
* Contributed by Jose Marcio Martins da Cruz - Ecole des Mines de Paris
* Jose-Marcio.Martins@ensmp.fr
*/
/* a part of this code is based on inetd.c for which this copyright applies: */
/*
* Copyright (c) 1983, 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sendmail.h>
SM_RCSID("@(#)$Id: ratectrl.c,v 8.13 2009/05/05 23:19:34 ca Exp $")
/*
** stuff included - given some warnings (inet_ntoa)
** - surely not everything is needed
*/
#endif /* NETINET || NETINET6 */
#ifndef HASH_ALG
#endif /* HASH_ALG */
#ifndef RATECTL_DEBUG
# define RATECTL_DEBUG 0
#endif /* RATECTL_DEBUG */
/* forward declarations */
/*
** CONNECTION_RATE_CHECK - updates connection history data
** and computes connection rate for the given host
**
** Parameters:
** hostaddr -- ip address of smtp client
** e -- envelope
**
** Returns:
** true (always)
**
** Side Effects:
** updates connection history
**
** Warnings:
** For each connection, this call shall be
** done only once with the value true for the
** update parameter.
** Typically, this call is done with the value
** true by the father, and once again with
** the value false by the children.
**
*/
bool
ENVELOPE *e;
{
static int clientconn = 0;
#if RATECTL_DEBUG
#endif /* RATECTL_DEBUG */
/* update server connection rate */
#if RATECTL_DEBUG
#endif /* RATECTL_DEBUG */
/* update client connection rate */
if (e == NULL)
if (e != NULL)
{
char s[16];
s);
}
return true;
}
/*
** Data declarations needed to evaluate connection rate
*/
/* this should be a power of 2, otherwise CPMHMASK doesn't work well */
#ifndef CPMHSIZE
#endif /* CPMHSIZE */
#ifndef MAX_CT_STEPS
#endif /* MAX_CT_STEPS */
/*
** time granularity: 10s (that's one "tick")
** will be initialised to ConnectionRateWindowSize/CHTSIZE
** before being used the first time
*/
/* Number of connections for a certain "tick" */
typedef struct CTime
{
unsigned long ct_Ticks;
int ct_Count;
}
typedef struct CHash
{
union
{
} cu_Addr;
#else /* NETINET6 && NETINET */
# if NETINET6
# else /* NETINET6 */
# endif /* NETINET6 */
#endif /* NETINET6 && NETINET */
int ch_Family;
unsigned long ch_colls;
/* 6 buckets for ticks: 60s */
}
static bool CHashAryOK = false;
/*
** CLIENT_RATE - Evaluate connection rate per smtp client
**
** Parameters:
** now - current time in secs
** saddr - client address
** update - update data / check only
**
** Returns:
** connection rate (connections / ConnectionRateWindowSize)
**
** Side effects:
** update static global data
**
*/
static int
bool update;
{
unsigned int hv;
int i;
int cnt;
bool coll;
unsigned int ticks;
cnt = 0;
hv = 0xABC3D20F;
if (ChtGran < 0)
if (ChtGran <= 0)
ChtGran = 10;
if (!CHashAryOK)
{
CHashAryOK = true;
}
{
char *p;
int addrlen;
#if HASH_ALG != 1
int c, d;
#endif /* HASH_ALG != 1 */
{
#if NETINET
case AF_INET:
break;
#endif /* NETINET */
#if NETINET6
case AF_INET6:
break;
#endif /* NETINET6 */
default:
/* should not happen */
return -1;
}
/* compute hash value */
for (i = 0; i < addrlen; ++i, ++p)
#if HASH_ALG == 1
{
d = *p;
c = d;
c ^= c<<6;
}
{
d = hv & 0xf0000000;
if (d != 0)
{
hv ^= (d >> 24);
hv ^= d;
}
}
#else /* HASH_ALG == 1 */
#endif /* HASH_ALG == 1 */
}
coll = true;
for (i = 0; i < MAX_CT_STEPS; ++i)
{
#if NETINET
{
coll = false;
break;
}
#endif /* NETINET */
#if NETINET6
{
coll = false;
break;
}
#endif /* NETINET6 */
}
/* Let's update data... */
if (update)
{
{
/*
** increment the number of collisions last
** CollTime for this client
*/
/*
** Maybe shall log if collision rate is too high...
** and take measures to resize tables
** if this is the case
*/
}
/*
** If it's not a match, then replace the data.
** Note: this purges the history of a colliding entry,
** which may cause "overruns", i.e., if two entries are
** "cancelling" each other out, then they may exceed
** the limits that are set. This might be mitigated a bit
** by the above "best of 5" function however.
**
** Alternative approach: just use the old data, which may
** cause false positives however.
** To activate this, change deactivate following memset call.
*/
if (coll)
{
#if NETINET
{
}
#endif /* NETINET */
#if NETINET6
{
}
#endif /* NETINET6 */
#if 1
#endif /* 1 */
}
{
{
}
}
}
/* Now let's count connections on the window */
for (i = 0; i < CHTSIZE; ++i)
{
}
#if RATECTL_DEBUG
"cln: cnt=(%d), CHTSIZE=(%d), ChtGran=(%d)",
#endif /* RATECTL_DEBUG */
return cnt;
}
/*
** TOTAL_RATE - Evaluate global connection rate
**
** Parameters:
** now - current time in secs
** update - update data / check only
**
** Returns:
** connection rate (connections / ConnectionRateWindowSize)
*/
static bool srv_Times_OK = false;
static int
bool update;
{
int i;
int cnt = 0;
unsigned int ticks;
if (ChtGran < 0)
if (ChtGran == 0)
ChtGran = 10;
if (!srv_Times_OK)
{
srv_Times_OK = true;
}
/* Let's update data */
if (update)
{
{
}
}
/* Let's count connections on the window */
for (i = 0; i < CHTSIZE; ++i)
{
}
#if RATECTL_DEBUG
"srv: cnt=(%d), CHTSIZE=(%d), ChtGran=(%d)",
#endif /* RATECTL_DEBUG */
return cnt;
}