68 |
68 |
69 // get cached user from map |
69 // get cached user from map |
70 time_t now = time(NULL); |
70 time_t now = time(NULL); |
71 size_t slot = mapkey.hash%cache.size; |
71 size_t slot = mapkey.hash%cache.size; |
72 |
72 |
|
73 User *u = NULL; |
73 pthread_mutex_lock(&auth_cache_mutex); |
74 pthread_mutex_lock(&auth_cache_mutex); |
74 |
75 |
75 UserCacheElm *elm = cache.map[slot]; |
76 UserCacheElm *elm = cache.map[slot]; |
76 while(elm && elm->key.hash != mapkey.hash) { |
77 while(elm && elm->key.hash != mapkey.hash) { |
77 elm = elm->next_elm; |
78 elm = elm->next_elm; |
78 } |
79 } |
79 // if we have an elm, the hash is correct |
80 // if we have an elm, the hash is correct |
80 if(elm) { |
81 if(elm) { |
81 // compare the key data to be sure it is the correct user |
82 // compare the key data to be sure it is the correct user |
82 int n = (mapkey.len > elm->key.len) ? elm->key.len : mapkey.len; |
83 int n = (mapkey.len > elm->key.len) ? elm->key.len : mapkey.len; |
83 if (memcmp(elm->key.data, mapkey.data, n)) { |
84 if (!memcmp(elm->key.data, mapkey.data, n)) { |
84 free(key); |
85 // elm is now the correct UserCacheElm |
85 pthread_mutex_unlock(&auth_cache_mutex); |
86 // TODO: use configuration for expire time |
86 return NULL; |
87 if(now - elm->created > 120) { |
87 } |
88 // cached user expired |
88 } else { |
89 // remove all users from the list from the first to this one |
89 free(key); |
90 UserCacheElm *e = cache.head; |
90 pthread_mutex_unlock(&auth_cache_mutex); |
91 while(e) { |
91 return NULL; |
92 if(e == elm) { |
92 } |
93 break; |
93 |
94 } |
94 // elm is now the correct UserCacheElm |
95 UserCacheElm *n = e->next_user; |
95 // TODO: use configuration for expire time |
96 auth_cache_remove_from_map(e); |
96 if(now - elm->created > 120) { |
97 e = n; |
97 // cached user expired |
98 } |
98 // remove all users from the list from the first to this one |
99 cache.head = elm->next_user; |
99 UserCacheElm *e = cache.head; |
100 if(cache.trail == elm) { |
100 while(e) { |
101 cache.trail = NULL; |
101 if(e == elm) { |
102 } |
102 break; |
103 auth_cache_remove_from_map(elm); |
|
104 u = NULL; |
|
105 } else { |
|
106 u = (User*)elm->user; |
103 } |
107 } |
104 UserCacheElm *n = e->next_user; |
108 } |
105 auth_cache_remove_from_map(e); |
|
106 e = n; |
|
107 } |
|
108 cache.head = elm->next_user; |
|
109 if(cache.trail == elm) { |
|
110 cache.trail = NULL; |
|
111 } |
|
112 auth_cache_remove_from_map(elm); |
|
113 free(key); |
|
114 pthread_mutex_unlock(&auth_cache_mutex); |
|
115 return NULL; |
|
116 } |
109 } |
117 |
110 |
118 pthread_mutex_unlock(&auth_cache_mutex); |
111 pthread_mutex_unlock(&auth_cache_mutex); |
119 free(key); |
112 free(key); |
120 return (User*)elm->user; |
113 return u; |
121 } |
114 } |
122 |
115 |
123 void auth_cache_add( |
116 void auth_cache_add( |
124 char *authdb, |
117 char *authdb, |
125 User *user, |
118 User *user, |