Browse Source

added base class for Dump

master
Tom Early 12 months ago
parent
commit
975fa71e3a
  1. 2
      Makefile
  2. 66
      base.cpp
  3. 28
      base.h
  4. 12
      callsignlist.cpp
  5. 2
      callsignlistitem.cpp
  6. 4
      gatekeeper.cpp
  7. 9
      m17peer.cpp
  8. 31
      m17protocol.cpp
  9. 6
      packet.h
  10. 3
      protocol.h

2
Makefile

@ -36,7 +36,7 @@ endif
LDFLAGS=-pthread
SRCS = callsign.cpp callsignlist.cpp callsignlistitem.cpp client.cpp clients.cpp crc.cpp gatekeeper.cpp ip.cpp m17client.cpp m17peer.cpp m17protocol.cpp notification.cpp packet.cpp packetstream.cpp peer.cpp peers.cpp peercallsignlist.cpp protocol.cpp reflector.cpp timepoint.cpp udpsocket.cpp user.cpp users.cpp version.cpp main.cpp
SRCS = base.cpp callsign.cpp callsignlist.cpp callsignlistitem.cpp client.cpp clients.cpp crc.cpp gatekeeper.cpp ip.cpp m17client.cpp m17peer.cpp m17protocol.cpp notification.cpp packet.cpp packetstream.cpp peer.cpp peers.cpp peercallsignlist.cpp protocol.cpp reflector.cpp timepoint.cpp udpsocket.cpp user.cpp users.cpp version.cpp main.cpp
OBJS = $(SRCS:.cpp=.o)
DEPS = $(SRCS:.cpp=.d)

66
base.cpp

@ -0,0 +1,66 @@
/*
* Copyright (C) 2020 by Thomas 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 <cstdio>
#include <iostream>
#include <iomanip>
#include "base.h"
void CBase::Dump(const char *title, const void *pointer, int length)
{
const unsigned char *data = (const unsigned char *)pointer;
std::cout << title << std::endl;
unsigned int offset = 0U;
while (length > 0) {
unsigned int bytes = (length > 16) ? 16U : length;
for (unsigned i = 0U; i < bytes; i++) {
if (i)
std::cout << " ";
std::cout << std::hex << std::setw(2) << std::right << std::setfill('0') << int(data[offset + i]);
}
for (unsigned int i = bytes; i < 16U; i++)
std::cout << " ";
std::cout << " *";
for (unsigned i = 0U; i < bytes; i++) {
unsigned char c = data[offset + i];
if (::isprint(c))
std::cout << c;
else
std::cout << '.';
}
std::cout << '*' << std::endl;
offset += 16U;
if (length >= 16)
length -= 16;
else
length = 0;
}
}

28
base.h

@ -0,0 +1,28 @@
#pragma once
/*
* Copyright (C) 2020 by Thomas 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.
*/
class CBase
{
public:
CBase() {}
virtual ~CBase() {}
protected:
void Dump(const char *title, const void *pointer, int length);
};

12
callsignlist.cpp

@ -166,8 +166,16 @@ bool CCallsignList::IsCallsignListed(const CCallsign &callsign, const CIp &ip, c
{
for ( const auto &item : m_Callsigns )
{
if (item.HasSameCallsign(callsign) && item.CheckListedModules(modules))
return true;
if ( item.HasSameCallsign(callsign) )
{
if ( item.CheckListedModules(modules) )
{
if ( ip == item.GetIp() )
{
return true;
}
}
}
}
return false;

2
callsignlistitem.cpp

@ -128,7 +128,7 @@ bool CCallsignListItem::CheckListedModules(const char *mods) const
else
found[pos] = true;
}
for (auto i=0; i<count; i++)
for (unsigned i=0; i<count; i++)
{
if (! found[i])
return false;

4
gatekeeper.cpp

@ -178,15 +178,13 @@ bool CGateKeeper::IsPeerListedOk(const CCallsign &callsign, const CIp &ip, const
{
bool ok = true;
// first check IP
// next, check callsign
if ( ok )
{
// look for an exact match in the list
const_cast<CPeerCallsignList &>(m_PeerList).Lock();
if ( !m_PeerList.empty() )
{
// find an exact match
ok = m_PeerList.IsCallsignListed(callsign, ip, modules);
}
const_cast<CPeerCallsignList &>(m_PeerList).Unlock();

9
m17peer.cpp

@ -34,16 +34,15 @@ CM17Peer::CM17Peer()
{
}
CM17Peer::CM17Peer(const CCallsign &callsign, const CIp &ip, const char *modules)
: CPeer(callsign, ip, modules)
CM17Peer::CM17Peer(const CCallsign &cs, const CIp &ip, const char *modules) : CPeer(cs, ip, modules)
{
std::cout << "Adding M17 peer" << std::endl;
std::cout << "Adding M17 peer " << cs << " module(s) " << modules << std::endl;
// and construct the M17 clients
for ( unsigned i = 0; i < ::strlen(modules); i++ )
for (auto p=modules; *p; p++)
{
// create and append to vector
m_Clients.push_back(std::make_shared<CM17Client>(callsign, ip, modules[i]));
m_Clients.push_back(std::make_shared<CM17Client>(cs, ip, *p));
}
}

31
m17protocol.cpp

@ -112,13 +112,31 @@ void CM17Protocol::Task(void)
}
else if (IsVaildInterlinkAcknowledge(buf, cs, mods))
{
std::cout << "Peer ACQN packet for modules " << mods << " from " << cs << " at " << ip << std::endl;
// callsign authorized?
if ( g_GateKeeper.MayLink(cs, ip, PROTOCOL_M17) )
{
// already connected ?
CPeers *peers = g_Reflector.GetPeers();
if ( nullptr == peers->FindPeer(cs, ip, PROTOCOL_M17) )
{
// create the new peer
// this also create one client per module
auto peer = std::make_shared<CM17Peer>(cs, ip, mods);
// append the peer to reflector peer list
// this also add all new clients to reflector client list
peers->AddPeer(peer);
}
g_Reflector.ReleasePeers();
}
}
else
{
Dump("Unknown packet", buf, len);
}
break;
case 11:
if ( IsValidConnect(buf, cs, &mod) )
{
@ -522,12 +540,12 @@ bool CM17Protocol::IsValidInterlinkConnect(const uint8_t *buf, CCallsign &cs, ch
bool CM17Protocol::IsVaildInterlinkAcknowledge(const uint8_t *buf, CCallsign &cs, char *mods)
{
return true;
}
bool CM17Protocol::IsValidNAcknowledge(const uint8_t *buf, CCallsign &cs)
{
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
@ -556,12 +574,11 @@ void CM17Protocol::EncodeConnectAckPacket(uint8_t *buf)
void CM17Protocol::EncodeInterlinkAckPacket(uint8_t *buf, const char *mods)
{
memset(buf, 0, sizeof(SInterConnect));
memcpy(buf, "ACKN", 4);
CCallsign cs(GetReflectorCallsign());
cs.CodeOut(buf+4);
memset(buf+10, 0, 27);
for (int i=0; i<26 && mods[i]; i++)
buf[i+10] = mods[i];
memcpy(buf + 10, mods, strlen(mods));
}
void CM17Protocol::EncodeInterlinkNackPacket(uint8_t *buf)

6
packet.h

@ -30,7 +30,7 @@
// M17 Packets
//all structures must be big endian on the wire, so you'll want htonl (man byteorder 3) and such.
using SM17Lich = struct __attribute__((__packed__)) _LICH {
using SM17Lich = struct __attribute__((__packed__)) lich_tag {
uint8_t addr_dst[6];
uint8_t addr_src[6];
uint16_t frametype; //frametype flag field per the M17 spec
@ -38,7 +38,7 @@ using SM17Lich = struct __attribute__((__packed__)) _LICH {
}; // 6 + 6 + 2 + 14 = 28 bytes
//without SYNC or other parts
using SM17Frame = struct __attribute__((__packed__)) _ip_frame {
using SM17Frame = struct __attribute__((__packed__)) m17_tag {
uint8_t magic[4];
uint16_t streamid;
SM17Lich lich;
@ -48,7 +48,7 @@ using SM17Frame = struct __attribute__((__packed__)) _ip_frame {
}; // 4 + 2 + 28 + 2 + 16 + 2 = 54 bytes
// includes extra bool (1 byte) for enforcing one-hop policy
using SRefM17Frame = struct __attribute__((__packed__)) _ip_frame {
using SRefM17Frame = struct __attribute__((__packed__)) peer_tag {
SM17Frame frame;
bool relayed;
}; // 4 + 2 + 28 + 2 + 16 + 2 + 1 = 55 bytes

3
protocol.h

@ -28,12 +28,13 @@
#include "udpsocket.h"
#include "packetstream.h"
#include "packet.h"
#include "base.h"
////////////////////////////////////////////////////////////////////////////////////////
// class
class CProtocol
class CProtocol : public CBase
{
public:
// constructor

Loading…
Cancel
Save