// // cxlxprotocol.cpp // xlxd // // Created by Jean-Luc Deltombe (LX3JL) on 28/01/2016. // Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. // // ---------------------------------------------------------------------------- // This file is part of xlxd. // // xlxd 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 3 of the License, or // (at your option) any later version. // // xlxd 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 Foobar. If not, see . // ---------------------------------------------------------------------------- #include "main.h" #include #include "cxlxclient.h" #include "cxlxprotocol.h" #include "creflector.h" #include "cgatekeeper.h" //////////////////////////////////////////////////////////////////////////////////////// // operation bool CXlxProtocol::Init(void) { bool ok; // base class ok = CProtocol::Init(); // update the reflector callsign m_ReflectorCallsign.PatchCallsign(0, (const uint8 *)"XLX", 3); // create our socket ok &= m_Socket.Open(XLX_PORT); if ( !ok ) { std::cout << "Error opening socket on port UDP" << XLX_PORT << " on ip " << g_Reflector.GetListenIp() << std::endl; } // update time m_LastKeepaliveTime.Now(); // done return ok; } //////////////////////////////////////////////////////////////////////////////////////// // task void CXlxProtocol::Task(void) { CBuffer Buffer; CIp Ip; CCallsign Callsign; char Modules[NB_MODULES_MAX+1]; CDvHeaderPacket *Header; CDvFramePacket *Frame; CDvLastFramePacket *LastFrame; // any incoming packet ? if ( m_Socket.Receive(&Buffer, &Ip, 20) != -1 ) { // crack the packet if ( (Frame = IsValidDvFramePacket(Buffer)) != NULL ) { //std::cout << "XLX (DExtra) DV frame" << std::endl; // handle it OnDvFramePacketIn(Frame); } else if ( (Header = IsValidDvHeaderPacket(Buffer)) != NULL ) { //std::cout << "XLX (DExtra) DV header:" << std::endl << *Header << std::endl; // callsign muted? if ( g_GateKeeper.MayTransmit(Header->GetMyCallsign(), Ip) ) { // handle it OnDvHeaderPacketIn(Header, Ip); } else { delete Header; } } else if ( (LastFrame = IsValidDvLastFramePacket(Buffer)) != NULL ) { //std::cout << "XLX (DExtra) DV last frame" << std::endl; // handle it OnDvLastFramePacketIn(LastFrame); } else if ( IsValidConnectPacket(Buffer, &Callsign, Modules) || IsValidAckPacket(Buffer, &Callsign, Modules) ) { std::cout << "XLX connect/ack packet for modules " << Modules << " from " << Callsign << " at " << Ip << std::endl; // already connected ? CClients *clients = g_Reflector.GetClients(); if ( clients->FindClient(Callsign, Ip, PROTOCOL_XLX) == NULL ) { // callsign authorized? if ( g_GateKeeper.MayLink(Callsign, Ip, PROTOCOL_XLX, Modules) ) { // acknowledge the request EncodeConnectAckPacket(&Buffer, Modules); m_Socket.Send(Buffer, Ip); // create the client CXlxClient *client = new CXlxClient(Callsign, Ip, Modules); // and append clients->AddClient(client); } else { // deny the request EncodeConnectNackPacket(&Buffer); m_Socket.Send(Buffer, Ip); } } // done g_Reflector.ReleaseClients(); } else if ( IsValidDisconnectPacket(Buffer, &Callsign) ) { std::cout << "XLX disconnect packet from " << Callsign << " at " << Ip << std::endl; // find client & remove it CClients *clients = g_Reflector.GetClients(); CClient *client = clients->FindClient(Callsign, Ip, PROTOCOL_XLX); if ( client != NULL ) { clients->RemoveClient(client); } g_Reflector.ReleaseClients(); } else if ( IsValidNackPacket(Buffer, &Callsign) ) { std::cout << "XLX nack packet from " << Callsign << " at " << Ip << std::endl; } else if ( IsValidKeepAlivePacket(Buffer, &Callsign) ) { //std::cout << "XLX keepalive packet from " << Callsign << " at " << Ip << std::endl; // find client & keep it alive CClient *GetClient(const CCallsign &, const CIp &, char, int); CClient *client = g_Reflector.GetClients()->FindClient(Callsign, Ip, PROTOCOL_XLX); if ( client != NULL ) { client->Alive(); } g_Reflector.ReleaseClients(); } else { std::cout << "XLX packet (" << Buffer.size() << ")" << std::endl; } } // handle end of streaming timeout CheckStreamsTimeout(); // handle queue from reflector HandleQueue(); // keep alive if ( m_LastKeepaliveTime.DurationSinceNow() > XLX_KEEPALIVE_PERIOD ) { // handle keep alives HandleKeepalives(); // handle remote peers connections HandlePeerLinks(); // update time m_LastKeepaliveTime.Now(); } } //////////////////////////////////////////////////////////////////////////////////////// // queue helper void CXlxProtocol::HandleQueue(void) { m_Queue.Lock(); while ( !m_Queue.empty() ) { // get the packet CPacket *packet = m_Queue.front(); m_Queue.pop(); // check if origin of packet is local // if not, do not stream it out as it will cause // network loop between linked XLX peers if ( packet->IsLocalOrigin() ) { // encode it CBuffer buffer; if ( EncodeDvPacket(*packet, &buffer) ) { // and push it to all our clients linked to the module and who are not streaming in CClients *clients = g_Reflector.GetClients(); int index = -1; CClient *client = NULL; while ( (client = clients->FindNextClient(PROTOCOL_XLX, &index)) != NULL ) { // is this client busy ? // here check that origin module of the stream is listed in client xlx // TODO: and check that client of origin is not another XLX to avoid loops if ( !client->IsAMaster() && client->HasThisReflectorModule(packet->GetModuleId()) ) { // no, send the packet m_Socket.Send(buffer, client->GetIp()); } } g_Reflector.ReleaseClients(); } } // done delete packet; } m_Queue.Unlock(); } //////////////////////////////////////////////////////////////////////////////////////// // keepalive helpers void CXlxProtocol::HandleKeepalives(void) { // DExtra protocol sends and monitors keepalives packets // event if the client is currently streaming // so, send keepalives to all CBuffer keepalive; EncodeKeepAlivePacket(&keepalive); // iterate on clients CClients *clients = g_Reflector.GetClients(); int index = -1; CClient *client = NULL; while ( (client = clients->FindNextClient(PROTOCOL_XLX, &index)) != NULL ) { // send keepalive m_Socket.Send(keepalive, client->GetIp()); // client busy ? if ( client->IsAMaster() ) { // yes, just tickle it client->Alive(); } // otherwise check if still with us else if ( !client->IsAlive() ) { // no, disconnect CBuffer disconnect; EncodeDisconnectPacket(&disconnect); m_Socket.Send(disconnect, client->GetIp()); // remove it std::cout << "XLX peer " << client->GetCallsign() << " keepalive timeout" << std::endl; clients->RemoveClient(client); } } g_Reflector.ReleaseClients(); } //////////////////////////////////////////////////////////////////////////////////////// // Peers helpers void CXlxProtocol::HandlePeerLinks(void) { CBuffer buffer; // get the list of peers CPeerCallsignList *list = g_GateKeeper.GetPeerList(); // todo: analyse possibility of double-lock hang-up CClients *clients = g_Reflector.GetClients(); // check if all our connected client are still listed by gatekeeper // if not, disconnect int index = -1; CClient *client = NULL; while ( (client = clients->FindNextClient(PROTOCOL_XLX, &index)) != NULL ) { if ( list->FindListItem(client->GetCallsign()) == NULL ) { // send disconnect packet EncodeDisconnectPacket(&buffer); m_Socket.Send(buffer, client->GetIp()); std::cout << "Sending disconnect packet to XLX peer " << client->GetCallsign() << std::endl; // remove client clients->RemoveClient(client); } } // check if all ours peers listed by gatekeeper are connected // if not, connect or reconnect for ( int i = 0; i < list->size(); i++ ) { CCallsignListItem *item = &((list->data())[i]); if ( clients->FindClient(item->GetCallsign(), PROTOCOL_XLX) == NULL ) { // send connect packet to re-initiate peer link EncodeConnectPacket(&buffer, item->GetModules()); m_Socket.Send(buffer, item->GetIp(), XLX_PORT); std::cout << "Sending connect packet to XLX peer " << item->GetCallsign() << " @ " << item->GetIp() << " for modules " << item->GetModules() << std::endl; } } // done g_Reflector.ReleaseClients(); g_GateKeeper.ReleasePeerList(); } //////////////////////////////////////////////////////////////////////////////////////// // streams helpers bool CXlxProtocol::OnDvHeaderPacketIn(CDvHeaderPacket *Header, const CIp &Ip) { bool newstream = false; CCallsign peer; // todo: verify Packet.GetModuleId() is in authorized list of XLX of origin // todo: do the same for DVFrame and DVLAstFrame packets // tag packet as remote peer origin Header->SetRemotePeerOrigin(); // find the stream CPacketStream *stream = GetStream(Header->GetStreamId()); if ( stream == NULL ) { // no stream open yet, open a new one // find this client CClient *client = g_Reflector.GetClients()->FindClient(Ip, PROTOCOL_XLX); if ( client != NULL ) { // and try to open the stream if ( (stream = g_Reflector.OpenStream(Header, client)) != NULL ) { // keep the handle m_Streams.push_back(stream); newstream = true; } // get origin peer = client->GetCallsign(); } // release g_Reflector.ReleaseClients(); } else { // stream already open // skip packet, but tickle the stream stream->Tickle(); } // update last heard g_Reflector.GetUsers()->Hearing(Header->GetMyCallsign(), Header->GetRpt1Callsign(), peer); g_Reflector.ReleaseUsers(); // done return newstream; } void CXlxProtocol::OnDvFramePacketIn(CDvFramePacket *DvFrame) { // tag packet as remote peer origin DvFrame->SetRemotePeerOrigin(); // anc call base class CDextraProtocol::OnDvFramePacketIn(DvFrame); } void CXlxProtocol::OnDvLastFramePacketIn(CDvLastFramePacket *DvFrame) { // tag packet as remote peer origin DvFrame->SetRemotePeerOrigin(); // anc call base class CDextraProtocol::OnDvLastFramePacketIn(DvFrame); } //////////////////////////////////////////////////////////////////////////////////////// // packet decoding helpers bool CXlxProtocol::IsValidKeepAlivePacket(const CBuffer &Buffer, CCallsign *callsign) { bool valid = false; if (Buffer.size() == 9) { callsign->SetCallsign(Buffer.data(), 8); valid = callsign->IsValid(); } return valid; } bool CXlxProtocol::IsValidConnectPacket(const CBuffer &Buffer, CCallsign *callsign, char *modules) { bool valid = false; if ((Buffer.size() == 39) && (Buffer.data()[0] == 'L') && (Buffer.data()[38] == 0)) { callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8); ::strcpy(modules, (const char *)&(Buffer.data()[12])); valid = callsign->IsValid(); for ( int i = 0; i < ::strlen(modules); i++ ) { valid &= IsLetter(modules[i]); } } return valid; } bool CXlxProtocol::IsValidDisconnectPacket(const CBuffer &Buffer, CCallsign *callsign) { bool valid = false; if ((Buffer.size() == 10) && (Buffer.data()[0] == 'U') && (Buffer.data()[9] == 0)) { callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8); valid = callsign->IsValid(); } return valid; } bool CXlxProtocol::IsValidAckPacket(const CBuffer &Buffer, CCallsign *callsign, char *modules) { bool valid = false; if ((Buffer.size() == 39) && (Buffer.data()[0] == 'A') && (Buffer.data()[38] == 0)) { callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8); ::strcpy(modules, (const char *)&(Buffer.data()[12])); valid = callsign->IsValid(); for ( int i = 0; i < ::strlen(modules); i++ ) { valid &= IsLetter(modules[i]); } } return valid; } bool CXlxProtocol::IsValidNackPacket(const CBuffer &Buffer, CCallsign *callsign) { bool valid = false; if ((Buffer.size() == 10) && (Buffer.data()[0] == 'N') && (Buffer.data()[9] == 0)) { callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8); valid = callsign->IsValid(); } return valid; } //////////////////////////////////////////////////////////////////////////////////////// // packet encoding helpers void CXlxProtocol::EncodeKeepAlivePacket(CBuffer *Buffer) { Buffer->Set(GetReflectorCallsign()); } void CXlxProtocol::EncodeConnectPacket(CBuffer *Buffer, const char *Modules) { uint8 tag[] = { 'L' }; // tag Buffer->Set(tag, sizeof(tag)); // our callsign Buffer->resize(Buffer->size()+8); g_Reflector.GetCallsign().GetCallsign(Buffer->data()+1); // our version Buffer->Append((uint8)VERSION_MAJOR); Buffer->Append((uint8)VERSION_MINOR); Buffer->Append((uint8)VERSION_REVISION); // the modules we share Buffer->Append(Modules); Buffer->resize(39); } void CXlxProtocol::EncodeDisconnectPacket(CBuffer *Buffer) { uint8 tag[] = { 'U' }; // tag Buffer->Set(tag, sizeof(tag)); // our callsign Buffer->resize(Buffer->size()+8); g_Reflector.GetCallsign().GetCallsign(Buffer->data()+1); Buffer->Append((uint8)0); } void CXlxProtocol::EncodeConnectAckPacket(CBuffer *Buffer, const char *Modules) { uint8 tag[] = { 'A' }; // tag Buffer->Set(tag, sizeof(tag)); // our callsign Buffer->resize(Buffer->size()+8); g_Reflector.GetCallsign().GetCallsign(Buffer->data()+1); // our version Buffer->Append((uint8)VERSION_MAJOR); Buffer->Append((uint8)VERSION_MINOR); Buffer->Append((uint8)VERSION_REVISION); // the modules we share Buffer->Append(Modules); Buffer->resize(39); } void CXlxProtocol::EncodeConnectNackPacket(CBuffer *Buffer) { uint8 tag[] = { 'N' }; // tag Buffer->Set(tag, sizeof(tag)); // our callsign Buffer->resize(Buffer->size()+8); g_Reflector.GetCallsign().GetCallsign(Buffer->data()+1); Buffer->Append((uint8)0); }