-
Notifications
You must be signed in to change notification settings - Fork 0
/
peermap.cpp
244 lines (211 loc) · 5.37 KB
/
peermap.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// ccallsignlist.cpp
// m17ref
//
// Created by Jean-Luc Deltombe (LX3JL) on 30/12/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
// Copyright © 2020 Thomas A. Early, N7TAE
//
// ----------------------------------------------------------------------------
// This file is part of m17ref.
//
// m17ref 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.
//
// m17ref 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
// with this software. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "main.h"
#include "peermap.h"
////////////////////////////////////////////////////////////////////////////////////////
// constructors
CPeerMap::CPeerMap()
{
m_Filename = nullptr;
::memset(&m_LastModTime, 0, sizeof(time_t));
}
////////////////////////////////////////////////////////////////////////////////////////
// file io
bool CPeerMap::LoadFromFile(const char *filename)
{
bool ok = false;
char line[256];
// and load
std::ifstream file (filename);
if ( file.is_open() )
{
Lock();
// empty list
m_Peers.clear();
// fill with file content
while ( file.getline(line, sizeof(line)).good() )
{
// remove leading & trailing spaces
auto name = TrimWhiteSpaces(line);
// crack it
if ( (strlen(name) > 0) && (name[0] != '#') )
{
// 1st token is callsign
if ( (name = strtok(name, " ,\t")) != nullptr )
{
if (strcmp(name, CALLSIGN))
{
if (m_Peers.end() == m_Peers.find(name))
{
CCallsign callsign(ToUpper(name));
// 2nd token is ip
char *szip, *szmods;
if ( (szip = strtok(nullptr, " ,\t")) != nullptr )
{
// 3rd token is modules list
if ( (szmods = strtok(nullptr, " ,\t")) != nullptr )
{
// create and and store
m_Peers[name] = CPeerMapItem(callsign, szip, ToUpper(szmods));
}
else
{
std::cerr << "No modules defined for peeer " << name << std::endl;
}
}
else
{
std::cerr << "No IP address defined for peer " << name << std::endl;
}
}
else
{
std::cerr << "Duplicate found: " << name << " in " << filename << std::endl;
}
}
else
{
std::cerr << "Self linking is not allowed! You cannot use " << name << " in " << filename << std::endl;
}
}
}
}
// close file
file.close();
// keep file path
m_Filename = filename;
// update time
GetLastModTime(&m_LastModTime);
// and done
Unlock();
ok = true;
std::cout << "Gatekeeper loaded " << m_Peers.size() << " lines from " << filename << std::endl;
}
else
{
std::cout << "Gatekeeper cannot find " << filename << std::endl;
}
return ok;
}
bool CPeerMap::ReloadFromFile(void)
{
bool ok = false;
if ( m_Filename != nullptr )
{
ok = LoadFromFile(m_Filename);
}
return ok;
}
bool CPeerMap::NeedReload(void)
{
bool needReload = false;
time_t time;
if ( GetLastModTime(&time) )
{
needReload = time != m_LastModTime;
}
return needReload;
}
////////////////////////////////////////////////////////////////////////////////////////
// compare
bool CPeerMap::IsCallsignListed(const CCallsign &callsign, char module) const
{
for ( const auto &item : m_Peers )
{
if (item.second.HasSameCallsign(callsign) && item.second.HasModuleListed(module))
return true;
}
return false;
}
bool CPeerMap::IsCallsignListed(const CCallsign &callsign, const CIp &ip, const char *modules) const
{
for ( const auto &item : m_Peers )
{
if ( item.second.HasSameCallsign(callsign) )
{
if ( item.second.CheckListedModules(modules) )
{
if ( ip == item.second.GetIp() )
{
return true;
}
}
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////////
// find
CPeerMapItem *CPeerMap::FindMapItem(const std::string &cs)
{
auto it = m_Peers.find(cs);
if (m_Peers.end() == it)
return nullptr;
return &it->second;
}
////////////////////////////////////////////////////////////////////////////////////////
// helpers
char *CPeerMap::TrimWhiteSpaces(char *str)
{
char *end;
// Trim leading space & tabs
while((*str == ' ') || (*str == '\t')) str++;
// All spaces?
if(*str == 0)
return str;
// Trim trailing space, tab or lf
end = str + ::strlen(str) - 1;
while((end > str) && ((*end == ' ') || (*end == '\t') || (*end == '\r'))) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
bool CPeerMap::GetLastModTime(time_t *time)
{
bool ok = false;
if ( m_Filename != nullptr )
{
struct stat fileStat;
if( ::stat(m_Filename, &fileStat) != -1 )
{
*time = fileStat.st_mtime;
ok = true;
}
}
return ok;
}
char *CPeerMap::ToUpper(char *str)
{
constexpr auto diff = 'a' - 'A';
for (char *p=str; *p; p++)
{
if (*p >= 'a' && *p <= 'z')
*p -= diff;
}
return str;
}