1 /*
2   Copyright (C) 2006-2013 Werner Dittmann
3 
4   This program is free software: you can redistribute it and/or modify
5   it under the terms of the GNU Lesser 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 
18 #include <stdio.h>
19 
20 #include <libzrtpcpp/ZIDCache.h>
21 #include <libzrtpcpp/ZIDRecordDb.h>
22 #include <libzrtpcpp/zrtpCacheDbBackend.h>
23 
24 #ifndef _ZIDCACHEDB_H_
25 #define _ZIDCACHEDB_H_
26 
27 
28 /**
29  * @file ZIDCacheDb.h
30  * @brief ZID cache management
31  *
32  * A ZID file stores (caches) some data that helps ZRTP to achives its
33  * key continuity feature. See @c ZIDRecordDb for further info which data
34  * the ZID file contains.
35  *
36  * @ingroup GNU_ZRTP
37  * @{
38  */
39 
40 /**
41  * This class implements a ZID (ZRTP Identifiers) file.
42  *
43  * The interface defintion @c ZIDCache.h contains the method documentation.
44  * The ZID cache file holds information about peers.
45  *
46  * @author: Werner Dittmann <Werner.Dittmann@t-online.de>
47  */
48 
49 class __EXPORT ZIDCacheDb: public ZIDCache {
50 
51 private:
52 
53     void *zidFile;
54     unsigned char associatedZid[IDENTIFIER_LEN];
55 
56     dbCacheOps_t cacheOps;
57 
58     char errorBuffer[DB_CACHE_ERR_BUFF_SIZE];
59 
60     void createZIDFile(char* name);
61     void formatOutput(remoteZidRecord_t *remZid, const char *nameBuffer, std::string *output);
62 
63 public:
64 
ZIDCacheDb()65     ZIDCacheDb(): zidFile(NULL) {
66         getDbCacheOps(&cacheOps);
67     };
68 
69     ~ZIDCacheDb();
70 
71     int open(char *name);
72 
isOpen()73     bool isOpen() { return (zidFile != NULL); };
74 
75     void close();
76 
77     ZIDRecord *getRecord(unsigned char *zid);
78 
79     unsigned int saveRecord(ZIDRecord *zidRecord);
80 
getZid()81     const unsigned char* getZid() { return associatedZid; };
82 
83     int32_t getPeerName(const uint8_t *peerZid, std::string *name);
84 
85     void putPeerName(const uint8_t *peerZid, const std::string name);
86 
87     void cleanup();
88 
89     void *prepareReadAll();
90 
91     void *readNextRecord(void *stmt, std::string *name);
92 
93     void closeOpenStatment(void *stmt);
94 };
95 
96 /**
97  * @}
98  */
99 #endif
100