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 | |
| 22 | #include <time.h> |
| 23 | |
| 24 | #include <libzrtpcpp/ZIDRecordDb.h> |
| 25 | |
| 26 | void ZIDRecordDb::setNewRs1(const unsigned char* data, int32_t expire) { |
| 27 | |
| 28 | // shift RS1 data into RS2 position |
| 29 | memcpy(record.rs2, record.rs1, RS_LENGTH); |
| 30 | record.rs2Ttl = record.rs1Ttl; |
| 31 | |
| 32 | // set new RS1 data |
| 33 | memcpy(record.rs1, data, RS_LENGTH); |
| 34 | |
| 35 | time_t validThru; |
| 36 | if (expire == -1) { |
| 37 | validThru = -1; |
| 38 | } |
| 39 | else if (expire <= 0) { |
| 40 | validThru = 0; |
| 41 | } |
| 42 | else { |
| 43 | validThru = time(NULL) + expire; |
| 44 | } |
| 45 | record.rs1Ttl = validThru; |
| 46 | resetRs2Valid(); |
| 47 | setRs1Valid(); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | bool ZIDRecordDb::isRs1NotExpired() { |
| 52 | time_t current = time(NULL); |
| 53 | time_t validThru; |
| 54 | |
| 55 | validThru = record.rs1Ttl; |
| 56 | |
| 57 | if (validThru == -1) |
| 58 | return true; |
| 59 | if (validThru == 0) |
| 60 | return false; |
| 61 | return (current <= validThru) ? true : false; |
| 62 | } |
| 63 | |
| 64 | bool ZIDRecordDb::isRs2NotExpired() { |
| 65 | time_t current = time(NULL); |
| 66 | time_t validThru; |
| 67 | |
| 68 | validThru = record.rs2Ttl; |
| 69 | |
| 70 | if (validThru == -1) |
| 71 | return true; |
| 72 | if (validThru == 0) |
| 73 | return false; |
| 74 | return (current <= validThru) ? true : false; |
| 75 | } |
| 76 | |
| 77 | void ZIDRecordDb::setMiTMData(const unsigned char* data) { |
| 78 | memcpy(record.mitmKey, data, RS_LENGTH); |
| 79 | setMITMKeyAvailable(); |
| 80 | } |