-
Notifications
You must be signed in to change notification settings - Fork 17
/
protocol.h
147 lines (118 loc) · 4.19 KB
/
protocol.h
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
140
141
142
143
144
145
146
147
//
// cprotocol.h
// mrefd
//
// Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
// Copyright © 2022 Thomas A. Early, N7TAE
//
// ----------------------------------------------------------------------------
// This file is part of mrefd.
//
// mrefd 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.
//
// mrefd 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 <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
#pragma once
#include <atomic>
#include <future>
#include <list>
#include <regex>
#include "udpsocket.h"
#include "packetstream.h"
#include "packet.h"
#include "base.h"
#include "crc.h"
////////////////////////////////////////////////////////////////////////////////////////
// class
class CProtocol : public CBase
{
public:
// constructor
CProtocol();
// destructor
virtual ~CProtocol();
// initialization
bool Initialize(const uint16_t port, const std::string &ipv4bind, const std::string &ipv6bind);
void Close(void);
// queue
CPacketQueue *GetQueue(void) { m_Queue.Lock(); return &m_Queue; }
void ReleaseQueue(void) { m_Queue.Unlock(); }
// get
const CCallsign &GetReflectorCallsign(void)const { return m_ReflectorCallsign; }
// task
void Thread(void);
void Task(void);
protected:
// queue helper
void HandleQueue(void);
// keepalive helpers
void HandlePeerLinks(void);
void HandleKeepalives(void);
// stream helpers
void OnPacketIn(std::unique_ptr<CPacket> &, const CIp &);
void OnFirstPacketIn(std::unique_ptr<CPacket> &, const CIp &);
// packet decoding helpers
bool IsValidConnect(const uint8_t *, CCallsign &, char *);
bool IsValidDisconnect(const uint8_t *, CCallsign &);
bool IsValidKeepAlive(const uint8_t *, CCallsign &);
bool IsValidPacket(const uint8_t *, bool is_internal, std::unique_ptr<CPacket> &);
bool IsValidNAcknowledge(const uint8_t *, CCallsign &);
bool IsValidInterlinkConnect(const uint8_t *, CCallsign &, char *);
bool IsValidInterlinkAcknowledge(const uint8_t *, CCallsign &, char *);
// packet encoding helpers
void EncodeKeepAlivePacket(uint8_t *);
void EncodeConnectAckPacket(uint8_t *);
void EncodeConnectNackPacket(uint8_t *);
void EncodeDisconnectPacket(uint8_t *, char);
void EncodeDisconnectedPacket(uint8_t *);
void EncodeInterlinkConnectPacket(SInterConnect &, const std::string &);
void EncodeInterlinkAckPacket(SInterConnect &, const char *);
void EncodeInterlinkNackPacket(uint8_t *);
// stream handle helpers
std::shared_ptr<CPacketStream> GetStream(uint16_t, const CIp &);
void CheckStreamsTimeout(void);
// syntax helper
bool IsNumber(char) const;
bool IsLetter(char) const;
bool IsSpace(char) const;
ssize_t Receive6(uint8_t *buf, CIp &Ip, int time_ms);
ssize_t Receive4(uint8_t *buf, CIp &Ip, int time_ms);
ssize_t ReceiveDS(uint8_t *buf, CIp &Ip, int time_ms);
ssize_t (CProtocol::*Receive)(uint8_t *buf, CIp &Ip, int time_ms);
void Send(const char *buf, const CIp &Ip) const;
void Send(const uint8_t *buf, size_t size, const CIp &Ip) const;
void Send(const char *buf, const CIp &Ip, uint16_t port) const;
void Send(const uint8_t *buf, size_t size, const CIp &Ip, uint16_t port) const;
// socket
CUdpSocket m_Socket4;
CUdpSocket m_Socket6;
// streams
std::list<std::shared_ptr<CPacketStream>> m_Streams;
// queue
CPacketQueue m_Queue;
// thread
std::atomic<bool> keep_running;
std::future<void> m_Future;
// identity
CCallsign m_ReflectorCallsign;
// debug
CTimer m_DebugTimer;
// time
CTimer m_LastKeepaliveTime;
CTimer m_LastPeersLinkTime;
// for PutDHTInfo
bool publish;
private:
std::regex clientRegEx, peerRegEx, lstnRegEx;
CCRC crc;
};