-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDPlusAuthenticator.cpp
139 lines (119 loc) · 3.67 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
/*
* Copyright (C) 2010-2015 by Jonathan Naylor G4KLX
* Copyright (C) 2018-2020 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 "Utilities.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()
{
}
int CDPlusAuthenticator::Process(CQnetDB &db, const bool reflectors, const bool repeaters)
// return true if everything went okay
{
int result = client.Open(m_address, AF_UNSPEC, "20001");
if (result)
{
fprintf(stderr, "DPlus Authorization failed: %s\n", gai_strerror(result));
return 0;
}
return authenticate(db, reflectors, repeaters);
}
int CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const bool repeaters)
{
unsigned char buffer[4096U];
::memset(buffer, ' ', 56U);
buffer[0U] = 0x38U;
buffer[1U] = 0xC0U;
buffer[2U] = 0x01U;
buffer[3U] = 0x00U;
::memcpy(buffer+4, m_loginCallsign.c_str(), m_loginCallsign.size());
::memcpy(buffer+12, "DV019999", 8);
::memcpy(buffer+28, "W7IB2", 5);
::memcpy(buffer+40, "DHS0257", 7);
if (client.Write(buffer, 56U))
{
fprintf(stderr, "ERROR: could not write opening phrase\n");
client.Close();
return 0;
}
int ret = client.ReadExact(buffer, 2U);
unsigned int rval = 0;
CHostQueue hqueue;
while (ret == 2)
{
unsigned int len = (buffer[1U] & 0x0FU) * 256U + buffer[0U];
// Ensure that we get exactly len - 2U bytes from the TCP stream
ret = client.ReadExact(buffer + 2U, len - 2U);
if (0 > ret)
{
fprintf(stderr, "Problem reading line, it returned %d\n", errno);
return rval;
}
if ((buffer[1U] & 0xC0U) != 0xC0U || buffer[2U] != 0x01U)
{
fprintf(stderr, "Invalid packet received from 20001\n");
return rval;
}
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(6, ' ');
// 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"))
{
rval++;
hqueue.Push(CHost(name.c_str(), address.c_str(), 20001));
}
else if (repeaters && name.compare(0, 3, "REF"))
{
rval++;
hqueue.Push(CHost(name.c_str(), address.c_str(), 20001));
}
}
}
if (! hqueue.Empty())
db.UpdateGW(hqueue);
ret = client.ReadExact(buffer, 2U);
}
printf("Probably authorized DPlus on %s using callsign %s\n", m_address.c_str(), m_loginCallsign.c_str());
client.Close();
return rval;
}