2 Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
25 #include <atalk/logger.h>
26 #include <atalk/afp.h>
27 #include <atalk/uuid.h>
30 typedef struct cacheduser {
31 unsigned long uid; /* for future use */
36 struct cacheduser *prev;
37 struct cacheduser *next;
40 static cacheduser_t *namecache[256]; /* indexed by hash of name */
41 static cacheduser_t *uuidcache[256]; /* indexed by hash of uuid */
43 /********************************************************
45 ********************************************************/
47 void uuidcache_dump(void) {
51 struct tm *tmp = NULL;
53 for ( i=0 ; i<256; i++) {
54 if ((entry = namecache[i]) != NULL) {
56 tmp = localtime(&entry->creationtime);
59 if (strftime(timestr, 200, "%c", tmp) == 0)
61 LOG(log_debug, logtype_default,
62 "namecache{%d}: name:%s, uuid:%s, type%s: %s, cached: %s",
65 uuid_bin2string(entry->uuid),
66 (entry->type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
67 uuidtype[entry->type & UUIDTYPESTR_MASK],
69 } while ((entry = entry->next) != NULL);
73 for ( i=0; i<256; i++) {
74 if ((entry = uuidcache[i]) != NULL) {
77 tmp = localtime(&entry->creationtime);
80 if (strftime(timestr, 200, "%c", tmp) == 0)
82 LOG(log_debug, logtype_default,
83 "uuidcache{%d}: uuid:%s, name:%s, type%s: %s, cached: %s",
85 uuid_bin2string(entry->uuid),
87 (entry->type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
88 uuidtype[entry->type & UUIDTYPESTR_MASK],
90 } while ((entry = entry->next) != NULL);
95 /* hash string it into unsigned char */
96 static unsigned char hashstring(unsigned char *str) {
97 unsigned long hash = 5381;
100 while ((c = *str++) != 0)
101 hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */
103 index = 85 ^ (hash & 0xff);
104 while ((hash = hash >> 8) != 0)
105 index ^= (hash & 0xff);
110 /* hash atalk_uuid_t into unsigned char */
111 static unsigned char hashuuid(uuidp_t uuid) {
112 unsigned char index = 83;
115 for (i=0; i<16; i++) {
122 /********************************************************
124 ********************************************************/
126 int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t type, const unsigned long uid _U_) {
129 unsigned char *uuid = NULL;
130 cacheduser_t *cacheduser = NULL;
133 /* allocate mem and copy values */
134 name = malloc(strlen(inname)+1);
136 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
141 uuid = malloc(UUID_BINSIZE);
143 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
148 cacheduser = malloc(sizeof(cacheduser_t));
150 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
155 strcpy(name, inname);
156 memcpy(uuid, inuuid, UUID_BINSIZE);
158 /* fill in the cacheduser */
159 cacheduser->name = name;
160 cacheduser->uuid = uuid;
161 cacheduser->type = type;
162 cacheduser->creationtime = time(NULL);
163 cacheduser->prev = NULL;
164 cacheduser->next = NULL;
167 hash = hashstring((unsigned char *)name);
169 /* insert cache entry into cache array at head of queue */
170 if (namecache[hash] == NULL) {
171 /* this queue is empty */
172 namecache[hash] = cacheduser;
174 cacheduser->next = namecache[hash];
175 namecache[hash]->prev = cacheduser;
176 namecache[hash] = cacheduser;
193 * Search cache by name and uuid type
195 * @args name (r) name to search
196 * @args type (rw) type (user or group) of name, returns found type here which might
197 * mark it as a negative entry
198 * @args uuid (w) found uuid is returned here
199 * @returns 0 on sucess, entry found
202 int search_cachebyname( const char *name, uuidtype_t *type, unsigned char *uuid) {
208 hash = hashstring((unsigned char *)name);
210 if (namecache[hash] == NULL)
213 entry = namecache[hash];
215 ret = strcmp(entry->name, name);
216 if (ret == 0 && *type == (entry->type & UUIDTYPESTR_MASK)) {
217 /* found, now check if expired */
219 if ((tim - entry->creationtime) > CACHESECONDS) {
220 LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\"%s\"", entry->name);
223 /* 2nd to last in queue */
224 entry->prev->next = entry->next;
226 /* not the last element */
227 entry->next->prev = entry->prev;
230 if ((namecache[hash] = entry->next) != NULL)
231 namecache[hash]->prev = NULL;
238 memcpy(uuid, entry->uuid, UUID_BINSIZE);
250 * Caller must free allocated name
252 int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
258 hash = hashuuid(uuidp);
260 if (! uuidcache[hash])
263 entry = uuidcache[hash];
265 ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
268 if ((tim - entry->creationtime) > CACHESECONDS) {
269 LOG(log_debug, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
271 /* 2nd to last in queue */
272 entry->prev->next = entry->next;
274 /* not the last element */
275 entry->next->prev = entry->prev;
278 if ((uuidcache[hash] = entry->next) != NULL)
279 uuidcache[hash]->prev = NULL;
286 *name = malloc(strlen(entry->name)+1);
287 strcpy(*name, entry->name);
298 int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
301 unsigned char *uuid = NULL;
302 cacheduser_t *cacheduser = NULL;
305 /* allocate mem and copy values */
306 name = malloc(strlen(inname)+1);
308 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
313 uuid = malloc(UUID_BINSIZE);
315 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
320 cacheduser = malloc(sizeof(cacheduser_t));
322 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
327 strcpy(name, inname);
328 memcpy(uuid, inuuid, UUID_BINSIZE);
330 /* fill in the cacheduser */
331 cacheduser->name = name;
332 cacheduser->type = type;
333 cacheduser->uuid = uuid;
334 cacheduser->creationtime = time(NULL);
335 cacheduser->prev = NULL;
336 cacheduser->next = NULL;
339 hash = hashuuid(uuid);
341 /* insert cache entry into cache array at head of queue */
342 if (uuidcache[hash] == NULL) {
343 /* this queue is empty */
344 uuidcache[hash] = cacheduser;
346 cacheduser->next = uuidcache[hash];
347 uuidcache[hash]->prev = cacheduser;
348 uuidcache[hash] = cacheduser;