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*55467Seric static char sccsid[] = "@(#)mci.c 5.4 (Berkeley) 07/20/92"; 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); 10754963Seric return (&MciCache[0]); 10854963Seric } 10954963Seric 11054963Seric now = curtime(); 11154963Seric bestmci = &MciCache[0]; 11254963Seric for (i = 0; i < MaxMciCache; i++) 11354963Seric { 11454963Seric mci = MciCache[i]; 11554963Seric if (mci == NULL || mci->mci_state == MCIS_CLOSED) 11654963Seric { 11754963Seric bestmci = &MciCache[i]; 11854963Seric continue; 11954963Seric } 12054963Seric if (mci->mci_lastuse + MciCacheTimeout < now && mci != savemci) 12154963Seric { 12254963Seric /* connection idle too long -- close it */ 12354963Seric bestmci = &MciCache[i]; 12454963Seric mci_uncache(bestmci); 12554963Seric continue; 12654963Seric } 12754963Seric if (*bestmci == NULL) 12854963Seric continue; 12954963Seric if (mci->mci_lastuse < (*bestmci)->mci_lastuse) 13054963Seric bestmci = &MciCache[i]; 13154963Seric } 13254963Seric return bestmci; 13354963Seric } 13454963Seric /* 13554963Seric ** MCI_UNCACHE -- remove a connection from a slot. 13654963Seric ** 13754963Seric ** May close a connection. 13854963Seric ** 13954963Seric ** Parameters: 14054963Seric ** mcislot -- the slot to empty. 14154963Seric ** 14254963Seric ** Returns: 14354963Seric ** none. 14454963Seric */ 14554963Seric 14654963Seric mci_uncache(mcislot) 14754967Seric register MCI **mcislot; 14854963Seric { 14954967Seric register MCI *mci; 15055020Seric extern ENVELOPE BlankEnvelope; 15154963Seric 15254963Seric mci = *mcislot; 15354963Seric if (mci == NULL) 15454963Seric return; 15554963Seric *mcislot = NULL; 15654963Seric mci->mci_flags &= ~MCIF_CACHED; 15754963Seric 158*55467Seric message(Arpa_Info, "Closing connection to %s", mci->mci_host); 159*55467Seric 16054963Seric /* only uses the envelope to flush the transcript file */ 16154963Seric if (mci->mci_state != MCIS_CLOSED) 16254963Seric smtpquit(mci->mci_mailer, mci, &BlankEnvelope); 16354963Seric } 16454963Seric /* 16554963Seric ** MCI_FLUSH -- flush the entire cache 16654963Seric */ 16754963Seric 16854963Seric mci_flush() 16954963Seric { 17054963Seric register int i; 17154963Seric 17254963Seric if (MciCache == NULL) 17354963Seric return; 17454963Seric 17554963Seric for (i = 0; i < MaxMciCache; i++) 17654963Seric mci_uncache(&MciCache[i]); 17754963Seric } 17854963Seric /* 17954963Seric ** MCI_GET -- get information about a particular host 18054963Seric */ 18154963Seric 18254967Seric MCI * 18354963Seric mci_get(host, m) 18454963Seric char *host; 18554963Seric MAILER *m; 18654963Seric { 18754967Seric register MCI *mci; 188*55467Seric register STAB *s; 18954967Seric 190*55467Seric s = stab(host, ST_MCI + m->m_mno, ST_ENTER); 191*55467Seric mci = &s->s_mci; 192*55467Seric mci->mci_host = s->s_name; 19354967Seric 19454967Seric if (tTd(42, 2)) 19554967Seric { 19654967Seric printf("mci_get(%s %s): mci_state=%d, _flags=%x, _exitstat=%d, _errno=%d\n", 19754967Seric host, m->m_name, mci->mci_state, mci->mci_flags, 19854967Seric mci->mci_exitstat, mci->mci_errno); 19954967Seric } 20054967Seric 20154967Seric /* try poking this to see if it is still usable */ 20254967Seric switch (mci->mci_state) 20354967Seric { 20454967Seric case MCIS_OPEN: 20554967Seric smtpnoop(mci); 20654967Seric break; 20754967Seric } 20854967Seric 20954967Seric return mci; 21054963Seric } 211