#include <sendmail.h>
SM_RCSID("@(#)$Id: ratectrl.c,v 8.13 2009/05/05 23:19:34 ca Exp $")
#if NETINET || NETINET6
# include <arpa/inet.h>
#endif
#include <sm/time.h>
#ifndef HASH_ALG
# define HASH_ALG 2
#endif
#ifndef RATECTL_DEBUG
# define RATECTL_DEBUG 0
#endif
static int client_rate __P((time_t, SOCKADDR *, bool));
static int total_rate __P((time_t, bool));
bool
connection_rate_check(hostaddr, e)
SOCKADDR *hostaddr;
ENVELOPE *e;
{
time_t now;
int totalrate, clientrate;
static int clientconn = 0;
now = time(NULL);
#if RATECTL_DEBUG
sm_syslog(LOG_INFO, NOQID, "connection_rate_check entering...");
#endif
totalrate = total_rate(now, e == NULL);
#if RATECTL_DEBUG
sm_syslog(LOG_INFO, NOQID, "global connection rate: %d", totalrate);
#endif
clientrate = client_rate(now, hostaddr, e == NULL);
if (e == NULL)
clientconn = count_open_connections(hostaddr);
if (e != NULL)
{
char s[16];
sm_snprintf(s, sizeof(s), "%d", clientrate);
macdefine(&e->e_macro, A_TEMP, macid("{client_rate}"), s);
sm_snprintf(s, sizeof(s), "%d", totalrate);
macdefine(&e->e_macro, A_TEMP, macid("{total_rate}"), s);
sm_snprintf(s, sizeof(s), "%d", clientconn);
macdefine(&e->e_macro, A_TEMP, macid("{client_connections}"),
s);
}
return true;
}
static int CollTime = 60;
#ifndef CPMHSIZE
# define CPMHSIZE 1024
#endif
#define CPMHMASK (CPMHSIZE-1)
#ifndef MAX_CT_STEPS
# define MAX_CT_STEPS 10
#endif
static int ChtGran = -1;
#define CHTSIZE 6
typedef struct CTime
{
unsigned long ct_Ticks;
int ct_Count;
}
CTime_T;
typedef struct CHash
{
#if NETINET6 && NETINET
union
{
struct in_addr c4_Addr;
struct in6_addr c6_Addr;
} cu_Addr;
# define ch_Addr4 cu_Addr.c4_Addr
# define ch_Addr6 cu_Addr.c6_Addr
#else
# if NETINET6
struct in6_addr ch_Addr;
# define ch_Addr6 ch_Addr
# else
struct in_addr ch_Addr;
# define ch_Addr4 ch_Addr
# endif
#endif
int ch_Family;
time_t ch_LTime;
unsigned long ch_colls;
CTime_T ch_Times[CHTSIZE];
}
CHash_T;
static CHash_T CHashAry[CPMHSIZE];
static bool CHashAryOK = false;
static int
client_rate(now, saddr, update)
time_t now;
SOCKADDR *saddr;
bool update;
{
unsigned int hv;
int i;
int cnt;
bool coll;
CHash_T *chBest = NULL;
unsigned int ticks;
cnt = 0;
hv = 0xABC3D20F;
if (ChtGran < 0)
ChtGran = ConnectionRateWindowSize / CHTSIZE;
if (ChtGran <= 0)
ChtGran = 10;
ticks = now / ChtGran;
if (!CHashAryOK)
{
memset(CHashAry, 0, sizeof(CHashAry));
CHashAryOK = true;
}
{
char *p;
int addrlen;
#if HASH_ALG != 1
int c, d;
#endif
switch (saddr->sa.sa_family)
{
#if NETINET
case AF_INET:
p = (char *)&saddr->sin.sin_addr;
addrlen = sizeof(struct in_addr);
break;
#endif
#if NETINET6
case AF_INET6:
p = (char *)&saddr->sin6.sin6_addr;
addrlen = sizeof(struct in6_addr);
break;
#endif
default:
return -1;
}
for (i = 0; i < addrlen; ++i, ++p)
#if HASH_ALG == 1
hv = (hv << 5) ^ (hv >> 23) ^ *p;
hv = (hv ^ (hv >> 16));
#elif HASH_ALG == 2
{
d = *p;
c = d;
c ^= c<<6;
hv += (c<<11) ^ (c>>1);
hv ^= (d<<14) + (d<<7) + (d<<4) + d;
}
#elif HASH_ALG == 3
{
hv = (hv << 4) + *p;
d = hv & 0xf0000000;
if (d != 0)
{
hv ^= (d >> 24);
hv ^= d;
}
}
#else
hv = ((hv << 1) ^ (*p & 0377)) % cctx->cc_size;
#endif
}
coll = true;
for (i = 0; i < MAX_CT_STEPS; ++i)
{
CHash_T *ch = &CHashAry[(hv + i) & CPMHMASK];
#if NETINET
if (saddr->sa.sa_family == AF_INET &&
ch->ch_Family == AF_INET &&
(saddr->sin.sin_addr.s_addr == ch->ch_Addr4.s_addr ||
ch->ch_Addr4.s_addr == 0))
{
chBest = ch;
coll = false;
break;
}
#endif
#if NETINET6
if (saddr->sa.sa_family == AF_INET6 &&
ch->ch_Family == AF_INET6 &&
(IN6_ARE_ADDR_EQUAL(&saddr->sin6.sin6_addr,
&ch->ch_Addr6) != 0 ||
IN6_IS_ADDR_UNSPECIFIED(&ch->ch_Addr6)))
{
chBest = ch;
coll = false;
break;
}
#endif
if (chBest == NULL || ch->ch_LTime == 0 ||
ch->ch_LTime < chBest->ch_LTime)
chBest = ch;
}
if (update)
{
if (coll && (now - chBest->ch_LTime < CollTime))
{
chBest->ch_colls++;
}
if (coll)
{
#if NETINET
if (saddr->sa.sa_family == AF_INET)
{
chBest->ch_Family = AF_INET;
chBest->ch_Addr4 = saddr->sin.sin_addr;
}
#endif
#if NETINET6
if (saddr->sa.sa_family == AF_INET6)
{
chBest->ch_Family = AF_INET6;
chBest->ch_Addr6 = saddr->sin6.sin6_addr;
}
#endif
#if 1
memset(chBest->ch_Times, '\0',
sizeof(chBest->ch_Times));
#endif
}
chBest->ch_LTime = now;
{
CTime_T *ct = &chBest->ch_Times[ticks % CHTSIZE];
if (ct->ct_Ticks != ticks)
{
ct->ct_Ticks = ticks;
ct->ct_Count = 0;
}
++ct->ct_Count;
}
}
for (i = 0; i < CHTSIZE; ++i)
{
CTime_T *ct = &chBest->ch_Times[i];
if (ct->ct_Ticks <= ticks && ct->ct_Ticks >= ticks - CHTSIZE)
cnt += ct->ct_Count;
}
#if RATECTL_DEBUG
sm_syslog(LOG_WARNING, NOQID,
"cln: cnt=(%d), CHTSIZE=(%d), ChtGran=(%d)",
cnt, CHTSIZE, ChtGran);
#endif
return cnt;
}
static CTime_T srv_Times[CHTSIZE];
static bool srv_Times_OK = false;
static int
total_rate(now, update)
time_t now;
bool update;
{
int i;
int cnt = 0;
CTime_T *ct;
unsigned int ticks;
if (ChtGran < 0)
ChtGran = ConnectionRateWindowSize / CHTSIZE;
if (ChtGran == 0)
ChtGran = 10;
ticks = now / ChtGran;
if (!srv_Times_OK)
{
memset(srv_Times, 0, sizeof(srv_Times));
srv_Times_OK = true;
}
if (update)
{
ct = &srv_Times[ticks % CHTSIZE];
if (ct->ct_Ticks != ticks)
{
ct->ct_Ticks = ticks;
ct->ct_Count = 0;
}
++ct->ct_Count;
}
for (i = 0; i < CHTSIZE; ++i)
{
ct = &srv_Times[i];
if (ct->ct_Ticks <= ticks && ct->ct_Ticks >= ticks - CHTSIZE)
cnt += ct->ct_Count;
}
#if RATECTL_DEBUG
sm_syslog(LOG_WARNING, NOQID,
"srv: cnt=(%d), CHTSIZE=(%d), ChtGran=(%d)",
cnt, CHTSIZE, ChtGran);
#endif
return cnt;
}