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 cacheduser_t *namecache[256]; /* indexed by hash of name */
41 cacheduser_t *uuidcache[256]; /* indexed by hash of uuid */
43 /********************************************************
45 ********************************************************/
47 static int dumpcache() {
52 struct tm *tmp = NULL;
54 for ( i=0 ; i<256; i++) {
55 if ((entry = namecache[i]) != NULL) {
57 tmp = localtime(&entry->creationtime);
60 if (strftime(timestr, 200, "%c", tmp) == 0)
62 LOG(log_debug9, logtype_default, "namecache{%d}: name:%s, uuid:%s, type: %s, cached: %s",
63 i, entry->name, uuid_bin2string(entry->uuid), uuidtype[entry->type], timestr);
64 } while ((entry = entry->next) != NULL);
68 for ( i=0; i<256; i++) {
69 if ((entry = uuidcache[i]) != NULL) {
72 tmp = localtime(&entry->creationtime);
75 if (strftime(timestr, 200, "%c", tmp) == 0)
77 LOG(log_debug9, logtype_default, "uuidcache{%d}: uuid:%s, name:%s, type: %s, cached: %s",
78 i, uuid_bin2string(entry->uuid), entry->name, uuidtype[entry->type], timestr);
79 } while ((entry = entry->next) != NULL);
86 /* hash string it into unsigned char */
87 static unsigned char hashstring(unsigned char *str) {
88 unsigned long hash = 5381;
91 while ((c = *str++) != 0)
92 hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */
94 index = 85 ^ (hash & 0xff);
95 while ((hash = hash >> 8) != 0)
96 index ^= (hash & 0xff);
101 /* hash atalk_uuid_t into unsigned char */
102 static unsigned char hashuuid(uuidp_t uuid) {
103 unsigned char index = 83;
106 for (i=0; i<16; i++) {
113 /********************************************************
115 ********************************************************/
117 int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t type, const unsigned long uid _U_) {
121 cacheduser_t *cacheduser = NULL;
129 /* allocate mem and copy values */
130 name = malloc(strlen(inname)+1);
132 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
137 uuid = malloc(UUID_BINSIZE);
139 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
144 cacheduser = malloc(sizeof(cacheduser_t));
146 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
151 strcpy(name, inname);
152 memcpy(uuid, inuuid, UUID_BINSIZE);
154 /* fill in the cacheduser */
155 cacheduser->name = name;
156 cacheduser->uuid = uuid;
157 cacheduser->type = type;
158 cacheduser->creationtime = time(NULL);
159 cacheduser->prev = NULL;
160 cacheduser->next = NULL;
163 hash = hashstring((unsigned char *)name);
165 /* insert cache entry into cache array */
166 if (namecache[hash] == NULL) { /* this queue is empty */
167 namecache[hash] = cacheduser;
168 } else { /* queue is not empty, search end of queue*/
169 entry = namecache[hash];
170 while( entry->next != NULL)
172 cacheduser->prev = entry;
173 entry->next = cacheduser;
194 * Caller provides buffer uuid for result
196 int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
206 hash = hashstring((unsigned char *)name);
208 if (! namecache[hash])
211 entry = namecache[hash];
213 ret = strcmp(entry->name, name);
214 if (ret == 0 && type == entry->type) {
215 /* found, now check if expired */
217 if ((tim - entry->creationtime) > CACHESECONDS) {
218 LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\'%s\' in queue {%d}", entry->name, hash);
220 if (entry->prev) /* 2nd to last in queue */
221 entry->prev->next = entry->next;
222 else { /* queue head */
223 if ((namecache[hash] = entry->next) != NULL)
224 namecache[hash]->prev = NULL;
234 memcpy(uuid, entry->uuid, UUID_BINSIZE);
250 * Caller must free allocated name
252 int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
262 hash = hashuuid(uuidp);
264 if (! uuidcache[hash])
267 entry = uuidcache[hash];
269 ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
272 if ((tim - entry->creationtime) > CACHESECONDS) {
273 LOG(log_debug, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
274 if (entry->prev) /* 2nd to last in queue */
275 entry->prev->next = entry->next;
276 else { /* queue head */
277 if ((uuidcache[hash] = entry->next) != NULL)
278 uuidcache[hash]->prev = NULL;
288 *name = malloc(strlen(entry->name)+1);
289 strcpy(*name, entry->name);
307 int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
311 cacheduser_t *cacheduser = NULL;
319 /* allocate mem and copy values */
320 name = malloc(strlen(inname)+1);
322 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
327 uuid = malloc(UUID_BINSIZE);
329 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
334 cacheduser = malloc(sizeof(cacheduser_t));
336 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
341 strcpy(name, inname);
342 memcpy(uuid, inuuid, UUID_BINSIZE);
344 /* fill in the cacheduser */
345 cacheduser->name = name;
346 cacheduser->type = type;
347 cacheduser->uuid = uuid;
348 cacheduser->creationtime = time(NULL);
349 cacheduser->prev = NULL;
350 cacheduser->next = NULL;
353 hash = hashuuid(uuid);
355 /* insert cache entry into cache array */
356 if (uuidcache[hash] == NULL) { /* this queue is empty */
357 uuidcache[hash] = cacheduser;
358 } else { /* queue is not empty, search end of queue*/
359 entry = uuidcache[hash];
360 while( entry->next != NULL)
362 cacheduser->prev = entry;
363 entry->next = cacheduser;