Fix more entries with back slashes that need to use literal formatting
[Samba/bjacke.git] / source3 / libsmb / conncache.c
blobc4a87623d7e66845ac944ed68ea0f00bf07e3b13
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 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/>.
25 #include "includes.h"
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 {
34 fstring domain_name;
35 fstring controller;
36 time_t lookup_time;
37 NTSTATUS nt_status;
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 )
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 */
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",
73 domain, server ));
75 DLIST_REMOVE(failed_connection_cache, fcc);
76 SAFE_FREE(fcc);
78 return NT_STATUS_OK;
82 /* The timeout hasn't expired yet so return false */
84 DEBUG(10, ("check_negative_conn_cache: returning negative entry for %s, %s\n",
85 domain, server ));
87 result = fcc->nt_status;
88 return result;
91 /* end of function means no cache entry */
92 return NT_STATUS_OK;
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
102 decimal IP
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",
117 domain, server ));
118 /* Update the failed time. */
119 fcc->lookup_time = time(NULL);
120 return;
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"));
128 return;
131 ZERO_STRUCTP(fcc);
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",
139 domain, server ));
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;
153 while (fcc) {
154 struct failed_connection_cache *fcc_next;
156 fcc_next = fcc->next;
157 DLIST_REMOVE(failed_connection_cache, fcc);
158 free(fcc);
160 fcc = fcc_next;
165 /****************************************************************************
166 Remove all negative entries for a domain. Used when going to online state in
167 winbindd.
168 ****************************************************************************/
170 void flush_negative_conn_cache_for_domain(const char *domain)
172 struct failed_connection_cache *fcc;
174 fcc = failed_connection_cache;
176 while (fcc) {
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);
186 free(fcc);
189 fcc = fcc_next;