Update to support changes since IPv6 was added.

This commit is contained in:
Jonathan Naylor
2020-09-03 10:52:22 +01:00
parent 0e029e30d2
commit 94289e062d
4 changed files with 28 additions and 34 deletions

View File

@@ -2444,10 +2444,9 @@ void CDMRGateway::processHomePosition()
void CDMRGateway::processDynamicTGControl() void CDMRGateway::processDynamicTGControl()
{ {
unsigned char buffer[100U]; unsigned char buffer[100U];
in_addr address; sockaddr_storage address;
unsigned int port; unsigned int addrlen;
int len = m_socket->read(buffer, 100U, address, addrlen);
int len = m_socket->read(buffer, 100U, address, port);
if (len <= 0) if (len <= 0)
return; return;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006-2016 by Jonathan Naylor G4KLX * Copyright (C) 2006-2016,2020 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -62,28 +62,26 @@ CUDPSocket::~CUDPSocket()
#endif #endif
} }
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length) int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length)
{ {
struct addrinfo hints; struct addrinfo hints;
::memset(&hints, 0, sizeof(hints)); ::memset(&hints, 0, sizeof(hints));
return lookup(hostname, port, addr, address_length, hints); return lookup(hostname, port, addr, address_length, hints);
} }
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length, struct addrinfo &hints) int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
{ {
int err;
std::string portstr = std::to_string(port); std::string portstr = std::to_string(port);
struct addrinfo *res; struct addrinfo *res;
/* port is always digits, no needs to lookup service */ /* port is always digits, no needs to lookup service */
hints.ai_flags |= AI_NUMERICSERV; hints.ai_flags |= AI_NUMERICSERV;
err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res); int err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
if (err) { if (err != 0) {
sockaddr_in *paddr = (sockaddr_in *)&addr; sockaddr_in* paddr = (sockaddr_in*)&addr;
::memset(paddr, 0, address_length = sizeof(sockaddr_in)); ::memset(paddr, 0x00U, address_length = sizeof(sockaddr_in));
paddr->sin_family = AF_INET; paddr->sin_family = AF_INET;
paddr->sin_port = htons(port); paddr->sin_port = htons(port);
paddr->sin_addr.s_addr = htonl(INADDR_NONE); paddr->sin_addr.s_addr = htonl(INADDR_NONE);
@@ -94,10 +92,11 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen); ::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
freeaddrinfo(res); freeaddrinfo(res);
return 0; return 0;
} }
bool CUDPSocket::match(const sockaddr_storage &addr1, const sockaddr_storage &addr2) bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
{ {
if (addr1.ss_family != addr2.ss_family) if (addr1.ss_family != addr2.ss_family)
return false; return false;
@@ -105,27 +104,24 @@ bool CUDPSocket::match(const sockaddr_storage &addr1, const sockaddr_storage &ad
switch (addr1.ss_family) { switch (addr1.ss_family) {
case AF_INET: case AF_INET:
struct sockaddr_in *in_1, *in_2; struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in *)&addr1; in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in *)&addr2; in_2 = (struct sockaddr_in*)&addr2;
return ( (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port));
(in_1->sin_port == in_2->sin_port) );
case AF_INET6: case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2; struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6 *)&addr1; in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6 *)&addr2; in6_2 = (struct sockaddr_in6*)&addr2;
return ( IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port));
(in6_1->sin6_port == in6_2->sin6_port) );
default: default:
return false; return false;
} }
} }
bool CUDPSocket::isnone(const sockaddr_storage &addr) bool CUDPSocket::isnone(const sockaddr_storage& addr)
{ {
struct sockaddr_in *in = (struct sockaddr_in *)&addr; struct sockaddr_in *in = (struct sockaddr_in *)&addr;
return ( (addr.ss_family == AF_INET) && return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
(in->sin_addr.s_addr == htonl(INADDR_NONE)) );
} }
bool CUDPSocket::open() bool CUDPSocket::open()
@@ -135,7 +131,6 @@ bool CUDPSocket::open()
bool CUDPSocket::open(const unsigned int af) bool CUDPSocket::open(const unsigned int af)
{ {
int err;
sockaddr_storage addr; sockaddr_storage addr;
unsigned int addrlen; unsigned int addrlen;
struct addrinfo hints; struct addrinfo hints;
@@ -145,8 +140,8 @@ bool CUDPSocket::open(const unsigned int af)
hints.ai_family = af; hints.ai_family = af;
/* to determine protocol family, call lookup() first. */ /* to determine protocol family, call lookup() first. */
err = lookup(m_address, m_port, addr, addrlen, hints); int err = lookup(m_address, m_port, addr, addrlen, hints);
if (err) { if (err != 0) {
LogError("The local address is invalid - %s", m_address.c_str()); LogError("The local address is invalid - %s", m_address.c_str());
return false; return false;
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2011,2013,2015,2016 by Jonathan Naylor G4KLX * Copyright (C) 2009-2011,2013,2015,2016,2020 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -49,10 +49,10 @@ public:
void close(); void close();
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length); static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length, struct addrinfo &hints); static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints);
static bool match(const sockaddr_storage &addr1, const sockaddr_storage &addr2); static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2);
static bool isnone(const sockaddr_storage &addr); static bool isnone(const sockaddr_storage& addr);
private: private:
std::string m_address; std::string m_address;

View File

@@ -19,6 +19,6 @@
#if !defined(VERSION_H) #if !defined(VERSION_H)
#define VERSION_H #define VERSION_H
const char* VERSION = "20200421"; const char* VERSION = "20200903";
#endif #endif