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() {
51 char *uuidstring = NULL;
53 struct tm *tmp = NULL;
55 for ( i=0 ; i<256; i++) {
56 if ((entry = namecache[i]) != NULL) {
58 uuid_bin2string(entry->uuid, &uuidstring);
59 tmp = localtime(&entry->creationtime);
62 if (strftime(timestr, 200, "%c", tmp) == 0)
64 LOG(log_debug9, logtype_default, "namecache{%d}: name:%s, uuid:%s, type: %s, cached: %s",
65 i, entry->name, uuidstring, uuidtype[entry->type], timestr);
67 } while ((entry = entry->next) != NULL);
71 for ( i=0; i<256; i++) {
72 if ((entry = uuidcache[i]) != NULL) {
74 uuid_bin2string(entry->uuid, &uuidstring);
75 tmp = localtime(&entry->creationtime);
78 if (strftime(timestr, 200, "%c", tmp) == 0)
80 LOG(log_debug9, logtype_default, "uuidcache{%d}: uuid:%s, name:%s, type: %s, cached: %s",
81 i, uuidstring, entry->name, uuidtype[entry->type], timestr);
83 } while ((entry = entry->next) != NULL);
90 /* hash string it into unsigned char */
91 static unsigned char hashstring(unsigned char *str) {
92 unsigned long hash = 5381;
95 while ((c = *str++) != 0)
96 hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */
98 index = 85 ^ (hash & 0xff);
99 while ((hash = hash >> 8) != 0)
100 index ^= (hash & 0xff);
105 /* hash uuid_t into unsigned char */
106 static unsigned char hashuuid(uuidp_t uuid) {
107 unsigned char index = 83;
110 for (i=0; i<16; i++) {
117 /********************************************************
119 ********************************************************/
121 int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t type, const unsigned long uid _U_) {
125 cacheduser_t *cacheduser = NULL;
132 /* allocate mem and copy values */
133 name = malloc(strlen(inname)+1);
135 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
140 uuid = malloc(UUID_BINSIZE);
142 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
147 cacheduser = malloc(sizeof(cacheduser_t));
149 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
154 strcpy(name, inname);
155 memcpy(uuid, inuuid, UUID_BINSIZE);
157 /* fill in the cacheduser */
158 cacheduser->name = name;
159 cacheduser->uuid = uuid;
160 cacheduser->type = type;
161 cacheduser->creationtime = time(NULL);
162 cacheduser->prev = NULL;
163 cacheduser->next = NULL;
166 hash = hashstring((unsigned char *)name);
168 /* insert cache entry into cache array at head of queue */
169 if (namecache[hash] == NULL) {
170 /* this queue is empty */
171 namecache[hash] = cacheduser;
173 cacheduser->next = namecache[hash];
174 namecache[hash]->prev = cacheduser;
175 namecache[hash] = cacheduser;
196 * Caller provides buffer uuid for result
198 int search_cachebyname( const char *name, uuidtype_t type, uuidp_t 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) {
217 /* found, now check if expired */
219 if ((tim - entry->creationtime) > CACHESECONDS) {
220 LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\'%s\' in queue {%d}", entry->name, hash);
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;
241 memcpy(uuid, entry->uuid, UUID_BINSIZE);
257 * Caller must free allocated name
259 int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
269 hash = hashuuid(uuidp);
271 if (! uuidcache[hash])
274 entry = uuidcache[hash];
276 ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
279 if ((tim - entry->creationtime) > CACHESECONDS) {
280 LOG(log_debug, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
282 /* 2nd to last in queue */
283 entry->prev->next = entry->next;
285 /* not the last element */
286 entry->next->prev = entry->prev;
289 if ((uuidcache[hash] = entry->next) != NULL)
290 uuidcache[hash]->prev = NULL;
300 *name = malloc(strlen(entry->name)+1);
301 strcpy(*name, entry->name);
319 int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
323 cacheduser_t *cacheduser = NULL;
331 /* allocate mem and copy values */
332 name = malloc(strlen(inname)+1);
334 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
339 uuid = malloc(UUID_BINSIZE);
341 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
346 cacheduser = malloc(sizeof(cacheduser_t));
348 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
353 strcpy(name, inname);
354 memcpy(uuid, inuuid, UUID_BINSIZE);
356 /* fill in the cacheduser */
357 cacheduser->name = name;
358 cacheduser->type = type;
359 cacheduser->uuid = uuid;
360 cacheduser->creationtime = time(NULL);
361 cacheduser->prev = NULL;
362 cacheduser->next = NULL;
365 hash = hashuuid(uuid);
367 /* insert cache entry into cache array at head of queue */
368 if (uuidcache[hash] == NULL) {
369 /* this queue is empty */
370 uuidcache[hash] = cacheduser;
372 cacheduser->next = uuidcache[hash];
373 uuidcache[hash]->prev = cacheduser;
374 uuidcache[hash] = cacheduser;