SIP Witch 1.9.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
digests.cpp
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
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 3 of the License, or
7 // (at your option) any later version.
8 //
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.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #include "server.h"
18 
19 #define INDEX_KEYSIZE 177
20 
21 namespace sipwitch {
22 
23 class __LOCAL key : public LinkedObject
24 {
25 public:
26  key(const char *keyid, const char *keyhash);
27 
28  const char *id;
29  char *hash;
30 };
31 
32 static memalloc private_cache;
33 static LinkedObject *private_paths[INDEX_KEYSIZE];
34 static condlock_t private_lock;
35 
36 key::key(const char *keyid, const char *keyhash) :
37 LinkedObject(&private_paths[NamedObject::keyindex(keyid, INDEX_KEYSIZE)])
38 {
39  id = private_cache.dup(keyid);
40  hash = private_cache.dup(keyhash);
41 }
42 
43 void digests::reload(void)
44 {
45  private_lock.modify();
46  memset(private_paths, 0, sizeof(private_paths));
47  private_cache.purge();
48  private_lock.commit();
49  load();
50 }
51 
52 const char *digests::get(const char *id)
53 {
54  assert(id != NULL);
55 
56  private_lock.access();
57  unsigned path = NamedObject::keyindex(id, INDEX_KEYSIZE);
58  linked_pointer<key> keys = private_paths[path];
59  while(is(keys)) {
60  if(String::equal(id, keys->id))
61  return keys->hash;
62  keys.next();
63  }
64  private_lock.release();
65  return NULL;
66 }
67 
68 void digests::release(const char *id)
69 {
70  if(id)
71  private_lock.release();
72 }
73 
74 bool digests::set(const char *id, const char *hash)
75 {
76  assert(id != NULL && hash != NULL);
77 
78  void *mp;
79  size_t len = strlen(hash);
80 
81  private_lock.access();
82  unsigned path = NamedObject::keyindex(id, INDEX_KEYSIZE);
83  linked_pointer<key> keys = private_paths[path];
84  while(is(keys)) {
85  if(String::equal(id, keys->id)) {
86  if(len == strlen(keys->hash)) {
87  String::set(keys->hash, ++len, hash);
88  private_lock.commit();
89  return true;
90  }
91  return false;
92  }
93  keys.next();
94  }
95  mp = private_cache.alloc(sizeof(key));
96  new(mp) key(id, hash);
97  private_lock.commit();
98  return true;
99 }
100 
101 void digests::load(void)
102 {
103  FILE *fp;
104  char buffer[256];
105  char *cp, *ep;
106 
107  dir::create(DEFAULT_VARPATH "/lib/sipwitch/digests", fsys::GROUP_PRIVATE);
108  string_t path = str(DEFAULT_VARPATH "/lib/sipwitch/digests/") + registry::getRealm();
109  fp = fopen(*path, "r");
110 
111  if(!fp)
112  return;
113 
114  while(NULL != fgets(buffer, sizeof(buffer), fp)) {
115  if(feof(fp))
116  break;
117 
118  cp = strchr(buffer, ':');
119  if(!cp)
120  continue;
121 
122  *(cp++) = 0;
123 
124  ep = strchr(cp, '\r');
125  if(!ep)
126  ep = strchr(cp, '\n');
127 
128  if(ep)
129  *ep = 0;
130 
131  set(buffer, cp);
132  }
133  fclose(fp);
134 }
135 
136 } // end namespace
const char * id
Definition: digests.cpp:28
static void reload(void)
Definition: digests.cpp:43
key(const char *keyid, const char *keyhash)
Definition: digests.cpp:36
static bool set(const char *id, const char *hash)
Definition: digests.cpp:74
static const char * getRealm(void)
Definition: server.h:178
static void load(void)
Definition: digests.cpp:101
static const char * get(const char *id)
Definition: digests.cpp:52
#define INDEX_KEYSIZE
Definition: digests.cpp:19
static void release(const char *hash)
Definition: digests.cpp:68
char * hash
Definition: digests.cpp:29