Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 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 | /* |
| 19 | * Authors: Werner Dittmann <Werner.Dittmann@t-online.de> |
| 20 | */ |
| 21 | // #define UNIT_TEST |
| 22 | |
| 23 | #include <string> |
| 24 | #include <time.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include <libzrtpcpp/ZIDCacheDb.h> |
| 28 | |
| 29 | |
| 30 | static ZIDCacheDb* instance; |
| 31 | |
| 32 | /** |
| 33 | * A poor man's factory. |
| 34 | * |
| 35 | * The build process must not allow to implementation classes linked |
| 36 | * into the same library. |
| 37 | */ |
| 38 | |
| 39 | ZIDCache* getZidCacheInstance() { |
| 40 | |
| 41 | if (instance == NULL) { |
| 42 | instance = new ZIDCacheDb(); |
| 43 | } |
| 44 | return instance; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | ZIDCacheDb::~ZIDCacheDb() { |
| 49 | close(); |
| 50 | } |
| 51 | |
| 52 | int ZIDCacheDb::open(char* name) { |
| 53 | |
| 54 | // check for an already active ZID file |
| 55 | if (zidFile != NULL) { |
| 56 | return 0; |
| 57 | } |
| 58 | if (cacheOps.openCache(name, &zidFile, errorBuffer) == 0) |
| 59 | cacheOps.readLocalZid(zidFile, associatedZid, NULL, errorBuffer); |
| 60 | else { |
| 61 | cacheOps.closeCache(zidFile); |
| 62 | zidFile = NULL; |
| 63 | } |
| 64 | |
| 65 | return ((zidFile == NULL) ? -1 : 1); |
| 66 | } |
| 67 | |
| 68 | void ZIDCacheDb::close() { |
| 69 | |
| 70 | if (zidFile != NULL) { |
| 71 | cacheOps.closeCache(zidFile); |
| 72 | zidFile = NULL; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | ZIDRecord *ZIDCacheDb::getRecord(unsigned char *zid) { |
| 77 | ZIDRecordDb *zidRecord = new ZIDRecordDb(); |
| 78 | |
| 79 | cacheOps.readRemoteZidRecord(zidFile, zid, associatedZid, zidRecord->getRecordData(), errorBuffer); |
| 80 | |
| 81 | zidRecord->setZid(zid); |
| 82 | |
| 83 | // We need to create a new ZID record. |
| 84 | if (!zidRecord->isValid()) { |
| 85 | zidRecord->setValid(); |
| 86 | zidRecord->getRecordData()->secureSince = (int64_t)time(NULL); |
| 87 | cacheOps.insertRemoteZidRecord(zidFile, zid, associatedZid, zidRecord->getRecordData(), errorBuffer); |
| 88 | } |
| 89 | return zidRecord; |
| 90 | } |
| 91 | |
| 92 | unsigned int ZIDCacheDb::saveRecord(ZIDRecord *zidRec) { |
| 93 | ZIDRecordDb *zidRecord = reinterpret_cast<ZIDRecordDb *>(zidRec); |
| 94 | |
| 95 | cacheOps.updateRemoteZidRecord(zidFile, zidRecord->getIdentifier(), associatedZid, zidRecord->getRecordData(), errorBuffer); |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | int32_t ZIDCacheDb::getPeerName(const uint8_t *peerZid, std::string *name) { |
| 100 | zidNameRecord_t nameRec; |
| 101 | char buffer[201] = {'\0'}; |
| 102 | |
| 103 | nameRec.name = buffer; |
| 104 | nameRec.nameLength = 200; |
| 105 | cacheOps.readZidNameRecord(zidFile, peerZid, associatedZid, NULL, &nameRec, errorBuffer); |
| 106 | if ((nameRec.flags & Valid) != Valid) { |
| 107 | return 0; |
| 108 | } |
| 109 | name->assign(buffer); |
| 110 | return name->length(); |
| 111 | } |
| 112 | |
| 113 | void ZIDCacheDb::putPeerName(const uint8_t *peerZid, const std::string name) { |
| 114 | zidNameRecord_t nameRec; |
| 115 | char buffer[201] = {'\0'}; |
| 116 | |
| 117 | nameRec.name = buffer; |
| 118 | nameRec.nameLength = 200; |
| 119 | cacheOps.readZidNameRecord(zidFile, peerZid, associatedZid, NULL, &nameRec, errorBuffer); |
| 120 | |
| 121 | nameRec.name = (char*)name.c_str(); |
| 122 | nameRec.nameLength = name.length(); |
| 123 | nameRec.nameLength = nameRec.nameLength > 200 ? 200 : nameRec.nameLength; |
| 124 | if ((nameRec.flags & Valid) != Valid) { |
| 125 | nameRec.flags = Valid; |
| 126 | cacheOps.insertZidNameRecord(zidFile, peerZid, associatedZid, NULL, &nameRec, errorBuffer); |
| 127 | } |
| 128 | else |
| 129 | cacheOps.updateZidNameRecord(zidFile, peerZid, associatedZid, NULL, &nameRec, errorBuffer); |
| 130 | |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | void ZIDCacheDb::cleanup() { |
| 135 | cacheOps.cleanCache(zidFile, errorBuffer); |
| 136 | cacheOps.readLocalZid(zidFile, associatedZid, NULL, errorBuffer); |
| 137 | } |