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 3 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, see <http://www.gnu.org/licenses/>.
27 #define CONNCACHE_ADDR 1
28 #define CONNCACHE_NAME 2
30 /* cache entry contains either a server name **or** and IP address as
31 the key. This means that a server could have two entries (one for each key) */
33 struct failed_connection_cache
{
38 struct failed_connection_cache
*prev
, *next
;
41 static struct failed_connection_cache
*failed_connection_cache
;
43 /**********************************************************************
44 Check for a previously failed connection.
45 failed_cache_timeout is an a absolute number of seconds after which
46 we should time this out. If failed_cache_timeout == 0 then time out
47 immediately. If failed_cache_timeout == -1 then never time out.
48 **********************************************************************/
50 NTSTATUS
check_negative_conn_cache_timeout( const char *domain
, const char *server
, unsigned int failed_cache_timeout
)
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
)
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 */
66 /* we have a match so see if it is still current */
67 if (failed_cache_timeout
!= (unsigned int)-1) {
68 if (failed_cache_timeout
== 0 ||
69 (time(NULL
) - fcc
->lookup_time
) > (time_t)failed_cache_timeout
) {
70 /* Cache entry has expired, delete it */
72 DEBUG(10, ("check_negative_conn_cache: cache entry expired for %s, %s\n",
75 DLIST_REMOVE(failed_connection_cache
, fcc
);
82 /* The timeout hasn't expired yet so return false */
84 DEBUG(10, ("check_negative_conn_cache: returning negative entry for %s, %s\n",
87 result
= fcc
->nt_status
;
91 /* end of function means no cache entry */
95 NTSTATUS
check_negative_conn_cache( const char *domain
, const char *server
)
97 return check_negative_conn_cache_timeout(domain
, server
, FAILED_CONNECTION_CACHE_TIMEOUT
);
100 /**********************************************************************
101 Add an entry to the failed conneciton cache (aither a name of dotted
103 **********************************************************************/
105 void add_failed_connection_entry(const char *domain
, const char *server
, NTSTATUS result
)
107 struct failed_connection_cache
*fcc
;
109 SMB_ASSERT(!NT_STATUS_IS_OK(result
));
111 /* Check we already aren't in the cache. We always have to have
112 a domain, but maybe not a specific DC name. */
114 for (fcc
= failed_connection_cache
; fcc
; fcc
= fcc
->next
) {
115 if ( strequal(fcc
->domain_name
, domain
) && strequal(fcc
->controller
, server
) ) {
116 DEBUG(10, ("add_failed_connection_entry: domain %s (%s) already tried and failed\n",
118 /* Update the failed time. */
119 fcc
->lookup_time
= time(NULL
);
124 /* Create negative lookup cache entry for this domain and controller */
126 if ( !(fcc
= SMB_MALLOC_P(struct failed_connection_cache
)) ) {
127 DEBUG(0, ("malloc failed in add_failed_connection_entry!\n"));
133 fstrcpy( fcc
->domain_name
, domain
);
134 fstrcpy( fcc
->controller
, server
);
135 fcc
->lookup_time
= time(NULL
);
136 fcc
->nt_status
= result
;
138 DEBUG(10,("add_failed_connection_entry: added domain %s (%s) to failed conn cache\n",
141 DLIST_ADD(failed_connection_cache
, fcc
);
144 /****************************************************************************
145 ****************************************************************************/
147 void flush_negative_conn_cache( void )
149 struct failed_connection_cache
*fcc
;
151 fcc
= failed_connection_cache
;
154 struct failed_connection_cache
*fcc_next
;
156 fcc_next
= fcc
->next
;
157 DLIST_REMOVE(failed_connection_cache
, fcc
);
165 /****************************************************************************
166 Remove all negative entries for a domain. Used when going to online state in
168 ****************************************************************************/
170 void flush_negative_conn_cache_for_domain(const char *domain
)
172 struct failed_connection_cache
*fcc
;
174 fcc
= failed_connection_cache
;
177 struct failed_connection_cache
*fcc_next
;
179 fcc_next
= fcc
->next
;
181 if (strequal(fcc
->domain_name
, domain
)) {
182 DEBUG(10,("flush_negative_conn_cache_for_domain: removed server %s "
183 " from failed cache for domain %s\n",
184 fcc
->controller
, domain
));
185 DLIST_REMOVE(failed_connection_cache
, fcc
);