SourceForge has been redesigned. Learn more.
Close

[50ef25]: / common / db.cpp  Maximize  Restore  History

Download this file

134 lines (110 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <sipwitch-config.h>
#include <ucommon/ucommon.h>
#include <ucommon/export.h>
#include <sipwitch/db.h>
#define INDEX_KEYSIZE 177
using namespace SIPWITCH_NAMESPACE;
using namespace UCOMMON_NAMESPACE;
class __LOCAL key : public LinkedObject
{
public:
key(const char *keyid, const char *keyhash);
const char *id;
char *hash;
};
static memalloc private_cache;
static LinkedObject *private_paths[INDEX_KEYSIZE];
static condlock_t private_lock;
key::key(const char *keyid, const char *keyhash) :
LinkedObject(&private_paths[NamedObject::keyindex(keyid, INDEX_KEYSIZE)])
{
id = private_cache.dup(keyid);
hash = private_cache.dup(keyhash);
}
void digests::clear(void)
{
private_lock.modify();
memset(private_paths, 0, sizeof(private_paths));
private_cache.purge();
private_lock.commit();
}
const char *digests::get(const char *id)
{
assert(id != NULL);
private_lock.access();
unsigned path = NamedObject::keyindex(id, INDEX_KEYSIZE);
linked_pointer<key> keys = private_paths[path];
while(is(keys)) {
if(String::equal(id, keys->id))
return keys->hash;
keys.next();
}
private_lock.release();
return NULL;
}
void digests::release(const char *id)
{
if(id)
private_lock.release();
}
bool digests::set(const char *id, const char *hash)
{
assert(id != NULL && hash != NULL);
caddr_t mp;
size_t len = strlen(hash);
private_lock.access();
unsigned path = NamedObject::keyindex(id, INDEX_KEYSIZE);
linked_pointer<key> keys = private_paths[path];
while(is(keys)) {
if(String::equal(id, keys->id)) {
if(len == strlen(keys->hash)) {
String::set(keys->hash, ++len, hash);
private_lock.commit();
return true;
}
return false;
}
keys.next();
}
mp = (caddr_t)private_cache.alloc(sizeof(key));
new(mp) key(id, hash);
private_lock.commit();
return true;
}
void digests::load(void)
{
FILE *fp = fopen(DEFAULT_VARPATH "/lib/sipwitch/digests.db", "r");
char buffer[256];
char *cp, *ep;
if(!fp)
return;
while(NULL != fgets(buffer, sizeof(buffer), fp)) {
if(feof(fp))
break;
cp = strchr(buffer, ':');
if(!cp)
continue;
*(cp++) = 0;
ep = strchr(cp, '\r');
if(!ep)
ep = strchr(cp, '\n');
if(ep)
*ep = 0;
set(buffer, cp);
}
fclose(fp);
}