r14683: Get rid of hardcoded output file. With no arg, print to stdout,
[Samba.git] / source / libsmb / conncache.c
blob49512d7a2e58ec24d1eae804add4ad3de90f92de
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 CONNCACHE_ADDR 1
29 #define CONNCACHE_NAME 2
31 /* cache entry contains either a server name **or** and IP address as
32 the key. This means that a server could have two entries (one for each key) */
34 struct failed_connection_cache {
35 fstring domain_name;
36 fstring controller;
37 time_t lookup_time;
38 NTSTATUS nt_status;
39 struct failed_connection_cache *prev, *next;
42 static struct failed_connection_cache *failed_connection_cache;
44 /**********************************************************************
45 Check for a previously failed connection.
46 failed_cache_timeout is an a absolute number of seconds after which
47 we should time this out. If failed_cache_timeout == 0 then time out
48 immediately. If failed_cache_timeout == -1 then never time out.
49 **********************************************************************/
51 NTSTATUS check_negative_conn_cache_timeout( const char *domain, const char *server, unsigned int failed_cache_timeout )
53 struct failed_connection_cache *fcc;
54 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
56 /* can't check if we don't have strings */
58 if ( !domain || !server )
59 return NT_STATUS_OK;
61 for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
63 if (!(strequal(domain, fcc->domain_name) && strequal(server, fcc->controller))) {
64 continue; /* no match; check the next entry */
67 /* we have a match so see if it is still current */
68 if (failed_cache_timeout != (unsigned int)-1) {
69 if (failed_cache_timeout == 0 ||
70 (time(NULL) - fcc->lookup_time) > (time_t)failed_cache_timeout) {
71 /* Cache entry has expired, delete it */
73 DEBUG(10, ("check_negative_conn_cache: cache entry expired for %s, %s\n",
74 domain, server ));
76 DLIST_REMOVE(failed_connection_cache, fcc);
77 SAFE_FREE(fcc);
79 return NT_STATUS_OK;
83 /* The timeout hasn't expired yet so return false */
85 DEBUG(10, ("check_negative_conn_cache: returning negative entry for %s, %s\n",
86 domain, server ));
88 result = fcc->nt_status;
89 return result;
92 /* end of function means no cache entry */
93 return NT_STATUS_OK;
96 NTSTATUS check_negative_conn_cache( const char *domain, const char *server)
98 return check_negative_conn_cache_timeout(domain, server, FAILED_CONNECTION_CACHE_TIMEOUT);
101 /**********************************************************************
102 Add an entry to the failed conneciton cache (aither a name of dotted
103 decimal IP
104 **********************************************************************/
106 void add_failed_connection_entry(const char *domain, const char *server, NTSTATUS result)
108 struct failed_connection_cache *fcc;
110 SMB_ASSERT(!NT_STATUS_IS_OK(result));
112 /* Check we already aren't in the cache. We always have to have
113 a domain, but maybe not a specific DC name. */
115 for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
116 if ( strequal(fcc->domain_name, domain) && strequal(fcc->controller, server) ) {
117 DEBUG(10, ("add_failed_connection_entry: domain %s (%s) already tried and failed\n",
118 domain, server ));
119 /* Update the failed time. */
120 fcc->lookup_time = time(NULL);
121 return;
125 /* Create negative lookup cache entry for this domain and controller */
127 if ( !(fcc = SMB_MALLOC_P(struct failed_connection_cache)) ) {
128 DEBUG(0, ("malloc failed in add_failed_connection_entry!\n"));
129 return;
132 ZERO_STRUCTP(fcc);
134 fstrcpy( fcc->domain_name, domain );
135 fstrcpy( fcc->controller, server );
136 fcc->lookup_time = time(NULL);
137 fcc->nt_status = result;
139 DEBUG(10,("add_failed_connection_entry: added domain %s (%s) to failed conn cache\n",
140 domain, server ));
142 DLIST_ADD(failed_connection_cache, fcc);
145 /****************************************************************************
146 ****************************************************************************/
148 void flush_negative_conn_cache( void )
150 struct failed_connection_cache *fcc;
152 fcc = failed_connection_cache;
154 while (fcc) {
155 struct failed_connection_cache *fcc_next;
157 fcc_next = fcc->next;
158 DLIST_REMOVE(failed_connection_cache, fcc);
159 free(fcc);
161 fcc = fcc_next;