2 Unix SMB/CIFS implementation.
3 SMB client library implementation (server cache)
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Define this to get the real SMBCFILE and SMBCSRV structures
29 #define _SMBC_INTERNAL
30 #include "include/libsmbclient.h"
33 * Structure we use if internal caching mechanism is used
36 struct smbc_server_cache
{
43 struct smbc_server_cache
*next
, *prev
;
49 * Add a new connection to the server cache.
50 * This function is only used if the external cache is not enabled
52 static int smbc_add_cached_server(SMBCCTX
* context
, SMBCSRV
* new,
53 const char * server
, const char * share
,
54 const char * workgroup
, const char * username
)
56 struct smbc_server_cache
* srvcache
= NULL
;
58 if (!(srvcache
= SMB_MALLOC_P(struct smbc_server_cache
))) {
60 DEBUG(3, ("Not enough space for server cache allocation\n"));
64 ZERO_STRUCTP(srvcache
);
66 srvcache
->server
= new;
68 srvcache
->server_name
= SMB_STRDUP(server
);
69 if (!srvcache
->server_name
) {
74 srvcache
->share_name
= SMB_STRDUP(share
);
75 if (!srvcache
->share_name
) {
80 srvcache
->workgroup
= SMB_STRDUP(workgroup
);
81 if (!srvcache
->workgroup
) {
86 srvcache
->username
= SMB_STRDUP(username
);
87 if (!srvcache
->username
) {
92 DLIST_ADD((context
->server_cache
), srvcache
);
96 SAFE_FREE(srvcache
->server_name
);
97 SAFE_FREE(srvcache
->share_name
);
98 SAFE_FREE(srvcache
->workgroup
);
99 SAFE_FREE(srvcache
->username
);
107 * Search the server cache for a server
108 * returns server_fd on success, -1 on error (not found)
109 * This function is only used if the external cache is not enabled
111 static SMBCSRV
* smbc_get_cached_server(SMBCCTX
* context
, const char * server
,
112 const char * share
, const char * workgroup
, const char * user
)
114 struct smbc_server_cache
* srv
= NULL
;
116 /* Search the cache lines */
117 for (srv
=((struct smbc_server_cache
*)context
->server_cache
);srv
;srv
=srv
->next
) {
118 if (strcmp(server
,srv
->server_name
) == 0 &&
119 strcmp(share
,srv
->share_name
) == 0 &&
120 strcmp(workgroup
,srv
->workgroup
) == 0 &&
121 strcmp(user
, srv
->username
) == 0)
130 * Search the server cache for a server and remove it
131 * returns 0 on success
132 * This function is only used if the external cache is not enabled
134 static int smbc_remove_cached_server(SMBCCTX
* context
, SMBCSRV
* server
)
136 struct smbc_server_cache
* srv
= NULL
;
138 for (srv
=((struct smbc_server_cache
*)context
->server_cache
);srv
;srv
=srv
->next
) {
139 if (server
== srv
->server
) {
141 /* remove this sucker */
142 DLIST_REMOVE(context
->server_cache
, srv
);
143 SAFE_FREE(srv
->server_name
);
144 SAFE_FREE(srv
->share_name
);
145 SAFE_FREE(srv
->workgroup
);
146 SAFE_FREE(srv
->username
);
151 /* server not found */
157 * Try to remove all the servers in cache
158 * returns 1 on failure and 0 if all servers could be removed.
160 static int smbc_purge_cached(SMBCCTX
* context
)
162 struct smbc_server_cache
* srv
;
163 struct smbc_server_cache
* next
;
164 int could_not_purge_all
= 0;
166 for (srv
= ((struct smbc_server_cache
*) context
->server_cache
),
167 next
= (srv
? srv
->next
:NULL
);
169 srv
= next
, next
= (srv
? srv
->next
: NULL
)) {
171 if (smbc_remove_unused_server(context
, srv
->server
)) {
172 /* could not be removed */
173 could_not_purge_all
= 1;
176 return could_not_purge_all
;
182 * This functions initializes all server-cache related functions
183 * to the default (internal) system.
185 * We use this to make the rest of the cache system static.
188 int smbc_default_cache_functions(SMBCCTX
* context
)
190 context
->callbacks
.add_cached_srv_fn
= smbc_add_cached_server
;
191 context
->callbacks
.get_cached_srv_fn
= smbc_get_cached_server
;
192 context
->callbacks
.remove_cached_srv_fn
= smbc_remove_cached_server
;
193 context
->callbacks
.purge_cached_fn
= smbc_purge_cached
;