forked from n7tae/QnetGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPlusAuthenticator.cpp
188 lines (154 loc) · 5.2 KB
/
DPlusAuthenticator.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
/*
* Copyright (C) 2010-2015 by Jonathan Naylor G4KLX
* Copyright (C) 2018 by Thomas A. Early N7TAE
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string>
#include <cassert>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <thread>
#include <chrono>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "DPlusAuthenticator.h"
//#include "DStarDefines.h"
//#include "Utils.h"
//#include "Defs.h"
CDPlusAuthenticator::CDPlusAuthenticator(const std::string &loginCallsign, const std::string &address) :
m_loginCallsign(loginCallsign),
m_address(address)
{
assert(loginCallsign.size());
Trim(m_loginCallsign);
}
CDPlusAuthenticator::~CDPlusAuthenticator()
{
}
bool CDPlusAuthenticator::Process(std::map<std::string, std::string> &gwy_map, const bool reflectors, const bool repeaters)
// return true if everything went okay
{
struct addrinfo hints, *infoptr;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; // AF_INET means IPv4 only addresses
hints.ai_socktype = SOCK_STREAM;
int result = EAI_AGAIN;
int count = 0;
while (EAI_AGAIN==result && 20>count++) { // we'll wait up to 60 seconds for this to work
result = getaddrinfo(m_address.c_str(), NULL, &hints, &infoptr);
if (EAI_AGAIN == result) {
fprintf(stdout, "getaddrinfo not ready: please wait...\n");
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}
if (result) {
fprintf(stderr, "DPlus Authroization failed: %s\n", gai_strerror(result));
return false;
}
struct addrinfo *p;
char host[256];
bool success = false;
for (p = infoptr; p != NULL && !success; p = p->ai_next) {
getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
printf("Trying %s from %s\n", host, m_address.c_str());
success = authenticate(m_loginCallsign, std::string(host), gwy_map, reflectors, repeaters);
}
freeaddrinfo(infoptr);
return success;
}
bool CDPlusAuthenticator::authenticate(const std::string &callsign, const std::string &hostname, std::map<std::string, std::string> &gwy_map, const bool reflectors, const bool repeaters)
{
CTCPReaderWriterClient socket(hostname, 20001U);
bool ret = socket.open();
if (!ret)
return false;
unsigned char* buffer = new unsigned char[4096U];
::memset(buffer, ' ', 56U);
buffer[0U] = 0x38U;
buffer[1U] = 0xC0U;
buffer[2U] = 0x01U;
buffer[3U] = 0x00U;
::memcpy(buffer+4, callsign.c_str(), callsign.size());
::memcpy(buffer+12, "DV019999", 8);
::memcpy(buffer+28, "W7IB2", 5);
::memcpy(buffer+40, "DHS0257", 7);
ret = socket.write(buffer, 56U);
if (!ret) {
socket.close();
delete[] buffer;
return false;
}
ret = read(socket, buffer, 2U);
while (ret) {
unsigned int len = (buffer[1U] & 0x0FU) * 256U + buffer[0U];
// Ensure that we get exactly len - 2U bytes from the TCP stream
ret = read(socket, buffer + 2U, len - 2U);
if (!ret) {
fprintf(stderr, "Short read from %s:20001", hostname.c_str());
return false;
}
if ((buffer[1U] & 0xC0U) != 0xC0U || buffer[2U] != 0x01U) {
fprintf(stderr, "Invalid packet received from %s:20001", hostname.c_str());
return false;
}
for (unsigned int i = 8U; (i + 25U) < len; i += 26U) {
std::string address((char *)(buffer + i));
std::string name((char *)(buffer + i + 16U));
Trim(address);
Trim(name);
name.resize(8, ' ');
// Get the active flag
bool active = (buffer[i + 25U] & 0x80U) == 0x80U;
// An empty name or IP address or an inactive gateway/reflector is not added
if (address.size()>0U && name.size()>0U && active) {
if (reflectors && 0==name.compare(0, 3, "REF"))
gwy_map[name] = address.append(" 20001");
else if (repeaters && name.compare(0, 3, "REF"))
gwy_map[name] = address.append(" 20001");
}
}
ret = read(socket, buffer, 2U);
}
printf("Authorized DPlus with %s using callsign %s\n", hostname.c_str(), callsign.c_str());
printf("Added %d DPlus gateways\n", (int)gwy_map.size());
socket.close();
delete[] buffer;
return true;
}
void CDPlusAuthenticator::Trim(std::string &s)
{
auto it = s.begin();
while (it!=s.end() && isspace(*it))
s.erase(it);
auto rit = s.rbegin();
while (rit!=s.rend() && isspace(*rit)) {
s.resize(s.size() - 1);
rit = s.rbegin();
}
}
bool CDPlusAuthenticator::read(CTCPReaderWriterClient &socket, unsigned char *buffer, unsigned int len) const
{
unsigned int offset = 0U;
do {
int n = socket.read(buffer + offset, len - offset, 10U);
if (n < 0)
return false;
offset += n;
} while ((len - offset) > 0U);
return true;
}