xref: /csrg-svn/usr.sbin/sendmail/src/mci.c (revision 57380)
154963Seric /*
254963Seric  * Copyright (c) 1983 Eric P. Allman
354963Seric  * Copyright (c) 1988 Regents of the University of California.
454963Seric  * All rights reserved.
554963Seric  *
654963Seric  * %sccs.include.redist.c%
754963Seric  */
854963Seric 
954963Seric #ifndef lint
10*57380Seric static char sccsid[] = "@(#)mci.c	6.2 (Berkeley) 01/01/93";
1154963Seric #endif /* not lint */
1254963Seric 
1354963Seric #include "sendmail.h"
1454963Seric 
1554963Seric /*
1654967Seric **  Mail Connection Information (MCI) Caching Module.
1754967Seric **
1854967Seric **	There are actually two separate things cached.  The first is
1954967Seric **	the set of all open connections -- these are stored in a
2054967Seric **	(small) list.  The second is stored in the symbol table; it
2154967Seric **	has the overall status for all hosts, whether or not there
2254967Seric **	is a connection open currently.
2354967Seric **
2454967Seric **	There should never be too many connections open (since this
2554967Seric **	could flood the socket table), nor should a connection be
2654967Seric **	allowed to sit idly for too long.
2754967Seric **
2854967Seric **	MaxMciCache is the maximum number of open connections that
2954967Seric **	will be supported.
3054967Seric **
3154967Seric **	MciCacheTimeout is the time (in seconds) that a connection
3254967Seric **	is permitted to survive without activity.
3354967Seric **
3454967Seric **	We actually try any cached connections by sending a NOOP
3554967Seric **	before we use them; if the NOOP fails we close down the
3654967Seric **	connection and reopen it.  Note that this means that a
3754967Seric **	server SMTP that doesn't support NOOP will hose the
3854967Seric **	algorithm -- but that doesn't seem too likely.
3954967Seric */
4054967Seric 
4154967Seric MCI	**MciCache;		/* the open connection cache */
4254967Seric /*
4354963Seric **  MCI_CACHE -- enter a connection structure into the open connection cache
4454963Seric **
4554963Seric **	This may cause something else to be flushed.
4654963Seric **
4754963Seric **	Parameters:
4854963Seric **		mci -- the connection to cache.
4954963Seric **
5054963Seric **	Returns:
5154963Seric **		none.
5254963Seric */
5354963Seric 
5454963Seric mci_cache(mci)
5554967Seric 	register MCI *mci;
5654963Seric {
5754967Seric 	register MCI **mcislot;
5854967Seric 	extern MCI **mci_scan();
5954963Seric 
6054963Seric 	if (MaxMciCache <= 0)
6154963Seric 	{
6254963Seric 		/* we don't support caching */
6354963Seric 		return;
6454963Seric 	}
6554963Seric 
6654963Seric 	/*
6754963Seric 	**  Find the best slot.  This may cause expired connections
6854963Seric 	**  to be closed.
6954963Seric 	*/
7054963Seric 
7154963Seric 	mcislot = mci_scan(mci);
7254963Seric 
7354963Seric 	/* if this is already cached, we are done */
7454963Seric 	if (bitset(MCIF_CACHED, mci->mci_flags))
7554963Seric 		return;
7654963Seric 
7754963Seric 	/* otherwise we may have to clear the slot */
7854963Seric 	if (*mcislot != NULL)
7954963Seric 		mci_uncache(mcislot);
8054963Seric 
8154963Seric 	*mcislot = mci;
8254963Seric 	mci->mci_flags |= MCIF_CACHED;
8354963Seric }
8454963Seric /*
8554963Seric **  MCI_SCAN -- scan the cache, flush junk, and return best slot
8654963Seric **
8754963Seric **	Parameters:
8854963Seric **		savemci -- never flush this one.  Can be null.
8954963Seric **
9054963Seric **	Returns:
9154963Seric **		The LRU (or empty) slot.
9254963Seric */
9354963Seric 
9454967Seric MCI **
9554963Seric mci_scan(savemci)
9654967Seric 	MCI *savemci;
9754963Seric {
9854963Seric 	time_t now;
9954967Seric 	register MCI **bestmci;
10054967Seric 	register MCI *mci;
10154963Seric 	register int i;
10254963Seric 
10354963Seric 	if (MciCache == NULL)
10454963Seric 	{
10554963Seric 		/* first call */
10654967Seric 		MciCache = (MCI **) xalloc(MaxMciCache * sizeof *MciCache);
10756215Seric 		bzero((char *) MciCache, MaxMciCache * sizeof *MciCache);
10854963Seric 		return (&MciCache[0]);
10954963Seric 	}
11054963Seric 
11154963Seric 	now = curtime();
11254963Seric 	bestmci = &MciCache[0];
11354963Seric 	for (i = 0; i < MaxMciCache; i++)
11454963Seric 	{
11554963Seric 		mci = MciCache[i];
11654963Seric 		if (mci == NULL || mci->mci_state == MCIS_CLOSED)
11754963Seric 		{
11854963Seric 			bestmci = &MciCache[i];
11954963Seric 			continue;
12054963Seric 		}
12154963Seric 		if (mci->mci_lastuse + MciCacheTimeout < now && mci != savemci)
12254963Seric 		{
12354963Seric 			/* connection idle too long -- close it */
12454963Seric 			bestmci = &MciCache[i];
12554963Seric 			mci_uncache(bestmci);
12654963Seric 			continue;
12754963Seric 		}
12854963Seric 		if (*bestmci == NULL)
12954963Seric 			continue;
13054963Seric 		if (mci->mci_lastuse < (*bestmci)->mci_lastuse)
13154963Seric 			bestmci = &MciCache[i];
13254963Seric 	}
13354963Seric 	return bestmci;
13454963Seric }
13554963Seric /*
13654963Seric **  MCI_UNCACHE -- remove a connection from a slot.
13754963Seric **
13854963Seric **	May close a connection.
13954963Seric **
14054963Seric **	Parameters:
14154963Seric **		mcislot -- the slot to empty.
14254963Seric **
14354963Seric **	Returns:
14454963Seric **		none.
14554963Seric */
14654963Seric 
14754963Seric mci_uncache(mcislot)
14854967Seric 	register MCI **mcislot;
14954963Seric {
15054967Seric 	register MCI *mci;
15155020Seric 	extern ENVELOPE BlankEnvelope;
15254963Seric 
15354963Seric 	mci = *mcislot;
15454963Seric 	if (mci == NULL)
15554963Seric 		return;
15654963Seric 	*mcislot = NULL;
15754963Seric 	mci->mci_flags &= ~MCIF_CACHED;
15854963Seric 
15955467Seric 	message(Arpa_Info, "Closing connection to %s", mci->mci_host);
16055467Seric 
16154963Seric 	/* only uses the envelope to flush the transcript file */
16254963Seric 	if (mci->mci_state != MCIS_CLOSED)
16354963Seric 		smtpquit(mci->mci_mailer, mci, &BlankEnvelope);
16454963Seric }
16554963Seric /*
16654963Seric **  MCI_FLUSH -- flush the entire cache
16754963Seric */
16854963Seric 
16954963Seric mci_flush()
17054963Seric {
17154963Seric 	register int i;
17254963Seric 
17354963Seric 	if (MciCache == NULL)
17454963Seric 		return;
17554963Seric 
17654963Seric 	for (i = 0; i < MaxMciCache; i++)
17754963Seric 		mci_uncache(&MciCache[i]);
17854963Seric }
17954963Seric /*
18054963Seric **  MCI_GET -- get information about a particular host
18154963Seric */
18254963Seric 
18354967Seric MCI *
18454963Seric mci_get(host, m)
18554963Seric 	char *host;
18654963Seric 	MAILER *m;
18754963Seric {
18854967Seric 	register MCI *mci;
18955467Seric 	register STAB *s;
19054967Seric 
19155467Seric 	s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
19255467Seric 	mci = &s->s_mci;
19355467Seric 	mci->mci_host = s->s_name;
19454967Seric 
19554967Seric 	if (tTd(42, 2))
19654967Seric 	{
19754967Seric 		printf("mci_get(%s %s): mci_state=%d, _flags=%x, _exitstat=%d, _errno=%d\n",
19854967Seric 			host, m->m_name, mci->mci_state, mci->mci_flags,
19954967Seric 			mci->mci_exitstat, mci->mci_errno);
20054967Seric 	}
20154967Seric 
20254967Seric 	/* try poking this to see if it is still usable */
20354967Seric 	switch (mci->mci_state)
20454967Seric 	{
20554967Seric 	  case MCIS_OPEN:
20654967Seric 		smtpnoop(mci);
20754967Seric 		break;
20854967Seric 	}
20954967Seric 
21054967Seric 	return mci;
21154963Seric }
212*57380Seric /*
213*57380Seric **  MCI_DUMP -- dump the contents of an MCI structure.
214*57380Seric **
215*57380Seric **	Parameters:
216*57380Seric **		mci -- the MCI structure to dump.
217*57380Seric **
218*57380Seric **	Returns:
219*57380Seric **		none.
220*57380Seric **
221*57380Seric **	Side Effects:
222*57380Seric **		none.
223*57380Seric */
224*57380Seric 
225*57380Seric mci_dump(mci)
226*57380Seric 	register MCI *mci;
227*57380Seric {
228*57380Seric 	extern char *ctime();
229*57380Seric 
230*57380Seric 	printf("MCI@%x: ", mci);
231*57380Seric 	if (mci == NULL)
232*57380Seric 	{
233*57380Seric 		printf("NULL\n");
234*57380Seric 		return;
235*57380Seric 	}
236*57380Seric 	printf("flags=%o, errno=%d, exitstat=%d, state=%d, pid=%d,\n",
237*57380Seric 		mci->mci_flags, mci->mci_errno, mci->mci_exitstat,
238*57380Seric 		mci->mci_state, mci->mci_pid);
239*57380Seric 	printf("\tphase=%s, mailer=%s,\n",
240*57380Seric 		mci->mci_phase == NULL ? "NULL" : mci->mci_phase,
241*57380Seric 		mci->mci_mailer == NULL ? "NULL" : mci->mci_mailer->m_name);
242*57380Seric 	printf("\thost=%s, lastuse=%s\n",
243*57380Seric 		mci->mci_host == NULL ? "NULL" : mci->mci_host,
244*57380Seric 		ctime(&mci->mci_lastuse));
245*57380Seric }
246