r616: Bug #1333.
[Samba/gebeck_regimport.git] / source3 / libsmb / conncache.c
blobe6604617d6846595cc93db91b0eb85fd11630f66
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon connection manager
6 Copyright (C) Tim Potter 2001
7 Copyright (C) Andrew Bartlett 2002
8 Copyright (C) Gerald (Jerry) Carter 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
28 #define FAILED_CONNECTION_CACHE_TIMEOUT 30 /* Seconds between attempts */
30 #define CONNCACHE_ADDR 1
31 #define CONNCACHE_NAME 2
33 /* cache entry contains either a server name **or** and IP address as
34 the key. This means that a server could have two entries (one for each key) */
36 struct failed_connection_cache {
37 fstring domain_name;
38 fstring controller;
39 time_t lookup_time;
40 NTSTATUS nt_status;
41 struct failed_connection_cache *prev, *next;
44 static struct failed_connection_cache *failed_connection_cache;
46 /**********************************************************************
47 Check for a previously failed connection
48 **********************************************************************/
50 NTSTATUS check_negative_conn_cache( const char *domain, const char *server )
52 struct failed_connection_cache *fcc;
53 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
55 /* can't check if we don't have strings */
57 if ( !domain || !server )
58 return NT_STATUS_OK;
60 for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
62 if ( !(strequal(domain, fcc->domain_name) && strequal(server, fcc->controller)) )
63 continue; /* no match; check the next entry */
65 /* we have a match so see if it is still current */
67 if ((time(NULL) - fcc->lookup_time) > FAILED_CONNECTION_CACHE_TIMEOUT)
69 /* Cache entry has expired, delete it */
71 DEBUG(10, ("check_negative_conn_cache: cache entry expired for %s, %s\n",
72 domain, server ));
74 DLIST_REMOVE(failed_connection_cache, fcc);
75 SAFE_FREE(fcc);
77 return NT_STATUS_OK;
80 /* The timeout hasn't expired yet so return false */
82 DEBUG(10, ("check_negative_conn_cache: returning negative entry for %s, %s\n",
83 domain, server ));
85 result = fcc->nt_status;
86 return result;
89 /* end of function means no cache entry */
90 return NT_STATUS_OK;
93 /**********************************************************************
94 Add an entry to the failed conneciton cache (aither a name of dotted
95 decimal IP
96 **********************************************************************/
98 void add_failed_connection_entry(const char *domain, const char *server, NTSTATUS result)
100 struct failed_connection_cache *fcc;
102 SMB_ASSERT(!NT_STATUS_IS_OK(result));
104 /* Check we already aren't in the cache. We always have to have
105 a domain, but maybe not a specific DC name. */
107 for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
108 if ( strequal(fcc->domain_name, domain) && strequal(fcc->controller, server) )
110 DEBUG(10, ("add_failed_connection_entry: domain %s (%s) already tried and failed\n",
111 domain, server ));
112 return;
116 /* Create negative lookup cache entry for this domain and controller */
118 if ( !(fcc = (struct failed_connection_cache *)malloc(sizeof(struct failed_connection_cache))) )
120 DEBUG(0, ("malloc failed in add_failed_connection_entry!\n"));
121 return;
124 ZERO_STRUCTP(fcc);
126 fstrcpy( fcc->domain_name, domain );
127 fstrcpy( fcc->controller, server );
128 fcc->lookup_time = time(NULL);
129 fcc->nt_status = result;
131 DEBUG(10,("add_failed_connection_entry: added domain %s (%s) to failed conn cache\n",
132 domain, server ));
134 DLIST_ADD(failed_connection_cache, fcc);
137 /****************************************************************************
138 ****************************************************************************/
140 void flush_negative_conn_cache( void )
142 struct failed_connection_cache *fcc;
144 fcc = failed_connection_cache;
146 while (fcc) {
147 struct failed_connection_cache *fcc_next;
149 fcc_next = fcc->next;
150 DLIST_REMOVE(failed_connection_cache, fcc);
151 free(fcc);
153 fcc = fcc_next;