parent
0ff3ea12d5
commit
e9645fad82
@ -0,0 +1,177 @@
|
||||
/*
|
||||
* 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 <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 = getaddrinfo(m_address.c_str(), NULL, &hints, &infoptr);
|
||||
if (result) {
|
||||
fprintf(stderr, "getaddrinfo: %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);
|
||||
|
||||
// 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 %ld DPlus gateways\n", 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;
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
/*
|
||||
* Copyright (C) 2010-2013 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 <netinet/in.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "TCPReaderWriterClient.h"
|
||||
|
||||
class CDPlusAuthenticator {
|
||||
public:
|
||||
CDPlusAuthenticator(const std::string &loginCallsign, const std::string &address);
|
||||
~CDPlusAuthenticator();
|
||||
|
||||
bool Process(std::map<std::string, std::string> &gwy_map, const bool reflectors, const bool repeaters);
|
||||
|
||||
private:
|
||||
std::string m_loginCallsign;
|
||||
std::string m_address;
|
||||
|
||||
void Trim(std::string &s);
|
||||
bool authenticate(const std::string &callsign, const std::string &hostname, std::map<std::string, std::string> &gwy_map, const bool reflectors, const bool repeaters);
|
||||
bool read(CTCPReaderWriterClient &socket, unsigned char *buffer, unsigned int len) const;
|
||||
};
|
||||
@ -0,0 +1,243 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2013 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* 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 "TCPReaderWriterClient.h"
|
||||
//#include "UDPReaderWriter.h"
|
||||
#include <cstdio>
|
||||
#include <cerrno>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
CTCPReaderWriterClient::CTCPReaderWriterClient(const std::string &address, unsigned int port, const std::string &localAddress) :
|
||||
m_address(address),
|
||||
m_port(port),
|
||||
m_localAddress(localAddress),
|
||||
m_fd(-1)
|
||||
{
|
||||
assert(address.size());
|
||||
assert(port > 0U);
|
||||
}
|
||||
|
||||
CTCPReaderWriterClient::CTCPReaderWriterClient(int fd) :
|
||||
m_address(),
|
||||
m_port(0U),
|
||||
m_localAddress(),
|
||||
m_fd(fd)
|
||||
{
|
||||
assert(fd >= 0);
|
||||
}
|
||||
|
||||
CTCPReaderWriterClient::CTCPReaderWriterClient() :
|
||||
m_address(),
|
||||
m_port(0U),
|
||||
m_localAddress(),
|
||||
m_fd(-1)
|
||||
{
|
||||
}
|
||||
|
||||
CTCPReaderWriterClient::~CTCPReaderWriterClient()
|
||||
{
|
||||
}
|
||||
|
||||
bool CTCPReaderWriterClient::open(const std::string& address, unsigned int port, const std::string& localAddress)
|
||||
{
|
||||
m_address = address;
|
||||
m_port = port;
|
||||
m_localAddress = localAddress;
|
||||
|
||||
return open();
|
||||
}
|
||||
|
||||
bool CTCPReaderWriterClient::open()
|
||||
{
|
||||
if (m_fd != -1)
|
||||
return true;
|
||||
|
||||
if (0 == m_address.size() || m_port == 0U)
|
||||
return false;
|
||||
|
||||
m_fd = ::socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (m_fd < 0) {
|
||||
fprintf(stderr, "Cannot create the TCP client socket, err=%d\n", errno);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_localAddress.size()) {
|
||||
sockaddr_in addr;
|
||||
::memset(&addr, 0x00, sizeof(struct sockaddr_in));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = 0U;
|
||||
addr.sin_addr.s_addr = ::inet_addr(m_localAddress.c_str());
|
||||
if (addr.sin_addr.s_addr == INADDR_NONE) {
|
||||
fprintf(stderr, "The address is invalid - %s\n", m_localAddress.c_str());
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
|
||||
fprintf(stderr, "Cannot bind the TCP client address, err=%d\n", errno);
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
struct sockaddr_in addr;
|
||||
::memset(&addr, 0x00, sizeof(struct sockaddr_in));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(m_port);
|
||||
addr.sin_addr = lookup(m_address);
|
||||
|
||||
if (addr.sin_addr.s_addr == INADDR_NONE) {
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (::connect(m_fd, (sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1) {
|
||||
fprintf(stderr, "Cannot connect the TCP client socket, err=%d\n", errno);
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
int noDelay = 1;
|
||||
if (::setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&noDelay, sizeof(noDelay)) == -1) {
|
||||
fprintf(stderr, "Cannot set the TCP client socket option, err=%d\n", errno);
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int CTCPReaderWriterClient::read(unsigned char* buffer, unsigned int length, unsigned int secs, unsigned int msecs)
|
||||
{
|
||||
assert(buffer != NULL);
|
||||
assert(length > 0U);
|
||||
assert(m_fd != -1);
|
||||
|
||||
// Check that the recv() won't block
|
||||
fd_set readFds;
|
||||
FD_ZERO(&readFds);
|
||||
FD_SET(m_fd, &readFds);
|
||||
|
||||
// Return after timeout
|
||||
timeval tv;
|
||||
tv.tv_sec = secs;
|
||||
tv.tv_usec = msecs * 1000;
|
||||
|
||||
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error returned from TCP client select, err=%d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!FD_ISSET(m_fd, &readFds))
|
||||
return 0;
|
||||
|
||||
ssize_t len = ::recv(m_fd, (char*)buffer, length, 0);
|
||||
if (len == 0) {
|
||||
return -2;
|
||||
} else if (len < 0) {
|
||||
fprintf(stderr, "Error returned from recv, err=%d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int CTCPReaderWriterClient::readLine(std::string& line, unsigned int secs)
|
||||
{
|
||||
//maybe there is a better way to do this like reading blocks, pushing them for later calls
|
||||
//Nevermind, we'll read one char at a time for the time being.
|
||||
unsigned char c;
|
||||
int resultCode;
|
||||
int len = 0;
|
||||
line = "";
|
||||
|
||||
do
|
||||
{
|
||||
resultCode = read(&c, 1, secs);
|
||||
if(resultCode == 1){
|
||||
line += c;
|
||||
len++;
|
||||
}
|
||||
}while(c != '\n' && resultCode == 1);
|
||||
|
||||
return resultCode <= 0 ? resultCode : len;
|
||||
}
|
||||
|
||||
bool CTCPReaderWriterClient::write(const unsigned char* buffer, unsigned int length)
|
||||
{
|
||||
assert(buffer != NULL);
|
||||
assert(length > 0U);
|
||||
assert(m_fd != -1);
|
||||
|
||||
ssize_t ret = ::send(m_fd, (char *)buffer, length, 0);
|
||||
if (ret != ssize_t(length)) {
|
||||
fprintf(stderr, "Error returned from send, err=%d\n", errno);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CTCPReaderWriterClient::writeLine(const std::string& line)
|
||||
{
|
||||
std::string lineCopy(line);
|
||||
if(lineCopy.size() > 0 && lineCopy.at(lineCopy.size() - 1) != '\n')
|
||||
lineCopy.append("\n");
|
||||
|
||||
//stupidly write one char after the other
|
||||
size_t len = lineCopy.size();
|
||||
bool result = true;
|
||||
for(size_t i = 0; i < len && result; i++){
|
||||
unsigned char c = lineCopy.at(i);
|
||||
result = write(&c , 1);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void CTCPReaderWriterClient::close()
|
||||
{
|
||||
if (m_fd != -1) {
|
||||
::close(m_fd);
|
||||
m_fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
in_addr CTCPReaderWriterClient::lookup(const std::string &hostname)
|
||||
{
|
||||
in_addr addr;
|
||||
in_addr_t address = ::inet_addr(hostname.c_str());
|
||||
if (address != in_addr_t(-1)) {
|
||||
addr.s_addr = address;
|
||||
return addr;
|
||||
}
|
||||
|
||||
struct hostent* hp = ::gethostbyname(hostname.c_str());
|
||||
if (hp != NULL) {
|
||||
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
|
||||
return addr;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Cannot find address for host %s", hostname.c_str());
|
||||
|
||||
addr.s_addr = INADDR_NONE;
|
||||
return addr;
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
/* end of inma once */
|
||||
/*
|
||||
* Copyright (C) 2010,2011,2012,2013 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* 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 <netdb.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <string>
|
||||
|
||||
class CTCPReaderWriterClient {
|
||||
public:
|
||||
CTCPReaderWriterClient(const std::string &address, unsigned int port, const std::string &localAddress = std::string(""));
|
||||
CTCPReaderWriterClient(int fd);
|
||||
CTCPReaderWriterClient();
|
||||
~CTCPReaderWriterClient();
|
||||
|
||||
bool open(const std::string &address, unsigned int port, const std::string &localAddress = std::string(""));
|
||||
bool open();
|
||||
|
||||
int read(unsigned char *buffer, unsigned int length, unsigned int secs, unsigned int msecs = 0U);
|
||||
int readLine(std::string &line, unsigned int secs);
|
||||
bool write(const unsigned char* buffer, unsigned int length);
|
||||
bool writeLine(const std::string &line);
|
||||
in_addr lookup(const std::string &hostname);
|
||||
|
||||
void close();
|
||||
|
||||
private:
|
||||
std::string m_address;
|
||||
unsigned short m_port;
|
||||
std::string m_localAddress;
|
||||
int m_fd;
|
||||
};
|
||||
Loading…
Reference in new issue