summaryrefslogtreecommitdiff
path: root/src/ZIDRecord.cpp
blob: d62c2e3787cb061c92e99df64f3fc9f323b37ec8 (plain)
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
/*
  Copyright (C) 2006-2007 Werner Dittmann

  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/>.
*/

/*
 * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
 */

#include <time.h>

#include <libzrtpcpp/ZIDRecord.h>

void ZIDRecord::setNewRs1(const unsigned char* data, int32_t expire) {

    // shift RS1 data into RS2 position
    memcpy(record.rs2Data, record.rs1Data, RS_LENGTH);
    memcpy(record.rs2Interval, record.rs1Interval, TIME_LENGTH);

    // now propagate flags as well
    if (isRs1Valid()) {
	setRs2Valid();
    }

    // set new RS1 data
    memcpy(record.rs1Data, data, RS_LENGTH);

    time_t validThru;
    if (expire == -1) {
	validThru = -1;
    }
    else if (expire <= 0) {
	validThru = 0;
    }
    else {
	validThru = time(NULL) + expire;
    }

    if (sizeof(time_t) == 4) {
	long long temp = validThru;
	memcpy(record.rs1Interval, (unsigned char*)&temp, TIME_LENGTH);
    }
    else {
	memcpy(record.rs1Interval, (unsigned char*)&validThru, TIME_LENGTH);
    }
    setRs1Valid();
}


const bool ZIDRecord::isRs1NotExpired() {
    time_t current = time(NULL);
    time_t validThru;

    if (sizeof(time_t) == 4) {
	long long temp;
	memcpy((unsigned char*)&temp, record.rs1Interval, TIME_LENGTH);
	validThru = temp;
    }
    else {
	memcpy((unsigned char*)&validThru, record.rs1Interval, TIME_LENGTH);
    }

    if (validThru == -1)
	return true;
    if (validThru == 0)
	return false;
    return (current <= validThru) ? true : false;
}

const bool ZIDRecord::isRs2NotExpired() {
    time_t current = time(NULL);
    time_t validThru;

    if (sizeof(time_t) == 4) {
	long long temp;
	memcpy((unsigned char*)&temp, record.rs2Interval, TIME_LENGTH);
	validThru = temp;
    }
    else {
	memcpy((unsigned char*)&validThru, record.rs2Interval, TIME_LENGTH);
    }

    if (validThru == -1)
	return true;
    if (validThru == 0)
	return false;
    return (current <= validThru) ? true : false;
}

void ZIDRecord::setMiTMData(const unsigned char* data) {
    memcpy(record.mitmKey, data, RS_LENGTH);
    setMITMKeyAvailable();
}

/** EMACS **
 * Local variables:
 * mode: c++
 * c-default-style: ellemtel
 * c-basic-offset: 4
 * End:
 */