Skip to content

Commit

Permalink
Merge pull request #186 from teonet-co/ref/l0server
Browse files Browse the repository at this point in the history
Ref/l0server
  • Loading branch information
angelskieglazki committed Mar 25, 2021
2 parents 928bfd7 + 001d7d8 commit ef164f6
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 226 deletions.
1 change: 1 addition & 0 deletions embedded/libpbl/src/pbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
*/

#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/ev_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const char *null_str = "";
char run_file[KSN_BUFFER_SIZE];

ksnetEvMgrClass* __ke_from_command_class(ksnCommandClass *X){return ((ksnetEvMgrClass*)((ksnCoreClass*)X->kc)->ke); }
ksnetEvMgrClass* __ke_from_L0_class(ksnLNullClass *X){return ((ksnetEvMgrClass*)X->ke); };

ksnetArpClass* __arp_from_command_class(ksnCommandClass *X){return ((ksnetArpClass*)((ksnCoreClass*)X->kc)->ka); }

// Local functions
Expand Down
6 changes: 4 additions & 2 deletions src/ev_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,12 @@ typedef struct ksnetEvMgrClass {
} ksnetEvMgrClass;

ksnetEvMgrClass* __ke_from_command_class(ksnCommandClass *X);
ksnetEvMgrClass* __ke_from_L0_class(ksnLNullClass *X);
ksnetArpClass* __arp_from_command_class(ksnCommandClass *X);

#define EVENT_MANAGER_CLASS(X) _Generic((X), \
ksnCommandClass* : __ke_from_command_class) (X)
#define EVENT_MANAGER_OBJECT(X) _Generic((X), \
ksnCommandClass* : __ke_from_command_class, \
ksnLNullClass * : __ke_from_L0_class) (X)

#define ARP_TABLE_CLASS(X) _Generic((X), \
ksnCommandClass* : __arp_from_command_class) (X)
Expand Down
283 changes: 141 additions & 142 deletions src/modules/l0-server.c

Large diffs are not rendered by default.

58 changes: 28 additions & 30 deletions src/modules/l0-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,44 @@
#ifndef L0_SERVER_H
#define L0_SERVER_H

#include <ev.h>
#include <pbl.h>
#include "modules/cque.h"
#include "net_com.h"
#include "subscribe.h"
#include "teonet_l0_client.h"

/**
* L0 Server map data structure
*
*/
typedef struct ksnLNullData {
typedef struct ksnLNullData { // \TODO: will be renamed to teoL0Data
ev_io w; ///< TCP Client watcher
char *name; ///< Clients name
size_t name_length; ///< Clients name length
void *read_buffer; ///< Pointer to saved buffer
size_t read_buffer_ptr; ///< Pointer in read buffer
size_t read_buffer_size; ///< Read buffer size

ev_io w; ///< TCP Client watcher
char *name; ///< Clients name
size_t name_length; ///< Clients name length
void *read_buffer; ///< Pointer to saved buffer
size_t read_buffer_ptr; ///< Pointer in read buffer
size_t read_buffer_size; ///< Read buffer size

char *t_addr; ///< TR-UDP IP address
int t_port; ///< TR-UDP port
int t_channel; ///< TR-UDP channel
double last_time;
char *t_addr; ///< TR-UDP IP address
int t_port; ///< TR-UDP port
int t_channel; ///< TR-UDP channel
double last_time;

teoLNullEncryptionContext *server_crypt;
teoLNullEncryptionContext *server_crypt; // \TODO: will be renamed to teoL0EncryptionContext
} ksnLNullData;

/**
* ksnLNull Class structure definition
*/
typedef struct ksnLNullClass {

void *ke; ///< Pointer to ksnEvMgrClass
PblMap *map; ///< Pointer to the L0 clients map (by fd)
PblMap *map_n; ///< Pointer to the L0 FDs map (by name)
int fd; ///< L0 TCP Server FD
ksnLNullSStat stat; ///< L0 server statistic
int fd_trudp; ///< Last free TR-UDP L0 FD
ksnCQueClass *cque; ///< CQUe to check dead clients

typedef struct ksnLNullClass { // \TODO: will be renamed to teoL0Class
void *ke; ///< Pointer to ksnEvMgrClass
PblMap *map; ///< Pointer to the L0 clients map (by fd)
PblMap *map_n; ///< Pointer to the L0 FDs map (by name)
int fd; ///< L0 TCP Server FD
ksnLNullSStat stat; ///< L0 server statistic
int fd_trudp; ///< Last free TR-UDP L0 FD
ksnCQueClass *cque; ///< CQUe to check dead clients
} ksnLNullClass;

#pragma pack(push)
Expand All @@ -55,13 +55,11 @@ typedef struct ksnLNullClass {
* L0 Server resend to peer packet data structure
*
*/
typedef struct ksnLNullSPacket {

uint8_t cmd; ///< Command
uint8_t from_length; ///< From client name length (include leading zero)
uint16_t data_length; ///< Packet data length
char from[]; ///< From client name (include leading zero) + packet data

typedef struct ksnLNullSPacket { // \TODO: will be renamed to teoL0SPacket
uint8_t cmd; ///< Command number
uint8_t client_name_length; ///< Client name length (includes null terminated)
uint16_t data_length; ///< Packet data length
char payload[]; ///< Сlient name (includes null terminated) + packet data
} ksnLNullSPacket;

#pragma pack(pop)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/subscribe.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ typedef struct teoSScrListData {
uint16_t ev; ///< Event (used when send data to subscriber)
uint8_t cmd; ///< Command ID (used when send data to subscriber)
uint8_t l0_f; ///< This is L0 client. The L0 server name added to the beginning of data
char addr[ARP_TABLE_IP_SIZE]; ///< L0 peer IP address
int16_t port; ///< L0 peer port
char addr[ARP_TABLE_IP_SIZE]; ///< L0 peer IP address
char data[]; ///< Remote peer name in list or data in CMD_SUBSCRIBE_ANSWER

} teoSScrListData;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/vpn.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#ifndef VPN_H
#define VPN_H

#include <ev.h>
#include <stdio.h>
#include <pbl.h>

#include "ev_mgr.h"

/**
* KSNet VPN Class data
Expand Down
1 change: 0 additions & 1 deletion src/net_arp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <pbl.h>

#include "net_core.h"
#include "teonet_l0_client.h"

/**
Expand Down
50 changes: 25 additions & 25 deletions src/net_com.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void ksnCommandDestroy(ksnCommandClass *kco) {
*/
int ksnCommandCheck(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

int processed = 0;

Expand Down Expand Up @@ -254,7 +254,7 @@ int ksnCommandCheck(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
void *ksnCommandEchoBuffer(ksnCommandClass *kco, void *data, size_t data_len, size_t *data_t_len) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);
double ct = ksnetEvMgrGetTime(ke);
*data_t_len = data_len + sizeof(double);
void *data_t = malloc(*data_t_len);
Expand Down Expand Up @@ -282,7 +282,7 @@ int ksnCommandSendCmdEcho(ksnCommandClass *kco, char *to, void *data, size_t dat

ksnet_arp_data * arp = ksnCoreSendCmdto(kco->kc, to, CMD_ECHO, data_t, data_t_len);

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);
if(arp != NULL) {
arp->last_triptime_send = ksnetEvMgrGetTime(ke);
}
Expand Down Expand Up @@ -316,7 +316,7 @@ int ksnCommandSendCmdConnect(ksnCommandClass *kco, char *to, char *name,
fillConnectData(data, &ptr, name, addr, port);

// TODO: duplicate code
ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "send CMD_CONNECT = %u to peer by name %s. (Connect to peer: %s, addr: %s:%d)\n",
Expand Down Expand Up @@ -345,7 +345,7 @@ int ksnCommandSendCmdConnectA(ksnCommandClass *kco, char *to_addr, uint32_t to_p
char data[KSN_BUFFER_DB_SIZE];
fillConnectData(data, &ptr, name, addr, port);
// TODO: duplicate code
ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "send CMD_CONNECT = %u to peer by address %s:%d. (Connect to peer: %s, addr: %s:%d)\n",
Expand Down Expand Up @@ -390,7 +390,7 @@ static int cmd_reset_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_echo_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_ECHO (cmd = %u) command, from %s (%s:%d)\n",
Expand All @@ -410,7 +410,7 @@ static int cmd_echo_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

static int cmd_echo_unr_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_ECHO_UNR (cmd = %u) command, from %s (%s:%d)\n",
Expand All @@ -432,7 +432,7 @@ static int cmd_echo_unr_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_peers_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);
ksnetArpClass *arp_class = ARP_TABLE_CLASS(kco);

#ifdef DEBUG_KSNET
Expand Down Expand Up @@ -478,7 +478,7 @@ static int cmd_peers_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_peers_num_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);
ksnetArpClass *arp_class = ARP_TABLE_CLASS(kco);

#ifdef DEBUG_KSNET
Expand Down Expand Up @@ -528,7 +528,7 @@ static int cmd_peers_num_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_l0_clients_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_L0_CLIENTS (cmd = %u) command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -565,7 +565,7 @@ static int cmd_l0_clients_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_l0_clients_n_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_L0_CLIENTS_N (cmd = %u) command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -610,7 +610,7 @@ static int cmd_l0_clients_n_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_l0_info_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_L0_INFO (cmd = %u) command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -657,7 +657,7 @@ static int cmd_l0_info_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_l0_stat_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_L0_STAT (cmd = %u) command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -708,7 +708,7 @@ static int cmd_l0_stat_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_host_info_answer_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);
ksnetArpClass *arp_class = ARP_TABLE_CLASS(kco);

const int not_json = rd->data_len && ((char*)rd->data)[0] != '{' && ((char*)rd->data)[rd->data_len-1] != '}';
Expand Down Expand Up @@ -760,7 +760,7 @@ static int cmd_host_info_answer_cb(ksnCommandClass *kco, ksnCorePacketData *rd)
*/
static int cmd_host_info_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_HOST_INFO = %u command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -846,7 +846,7 @@ static int cmd_host_info_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
* @return
*/
static int cmd_get_public_ip_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_GET_PUBLIC_IP (cmd = %u) command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -905,7 +905,7 @@ static int cmd_get_public_ip_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
* @return
*/
static int cmd_trudp_info_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_TRUDP_INFO (cmd = %u) command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -944,7 +944,7 @@ static int cmd_trudp_info_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_resend_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_RESEND (cmd = %u) command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -987,7 +987,7 @@ static int cmd_resend_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
inline int cmd_reconnect_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_RECONNECT (cmd = %u) command, from %s (%s:%d)\n",
Expand All @@ -1006,7 +1006,7 @@ inline int cmd_reconnect_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
inline static int cmd_reconnect_answer_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_RECONNECT_ANSWER (cmd = %u) command, from %s (%s:%d)\n",
Expand All @@ -1025,7 +1025,7 @@ inline static int cmd_reconnect_answer_cb(ksnCommandClass *kco, ksnCorePacketDat
*/
static int cmd_echo_answer_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_ECHO_ANSWER (cmd = %u) command, from %s (%s:%d)\n",
Expand Down Expand Up @@ -1129,7 +1129,7 @@ int send_cmd_connect_cb_b(ksnetArpClass *ka, char *peer_name,
*/
static int cmd_connect_r_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);
ksnetArpClass *arp_class = ARP_TABLE_CLASS(kco);

#ifdef DEBUG_KSNET
Expand Down Expand Up @@ -1226,7 +1226,7 @@ static void cmd_connect_cque_cb(uint32_t id, int type, void *data) {
*/
static int cmd_connect_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);
ksnetArpClass *arp_class = ARP_TABLE_CLASS(kco);

/**
Expand Down Expand Up @@ -1286,7 +1286,7 @@ static int cmd_connect_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
int cmd_disconnected_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);
ksnetArpClass *arp_class = ARP_TABLE_CLASS(kco);

#ifdef DEBUG_KSNET
Expand Down Expand Up @@ -1338,7 +1338,7 @@ int cmd_disconnected_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
*/
static int cmd_split_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {

ksnetEvMgrClass *ke = EVENT_MANAGER_CLASS(kco);
ksnetEvMgrClass *ke = EVENT_MANAGER_OBJECT(kco);

#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV, "process CMD_SPLIT (cmd = %u) command, from %s (%s:%d)\n",
Expand Down
4 changes: 4 additions & 0 deletions src/net_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

//#include "net_recon.h"


#include "net_arp.h"
#include "teonet_l0_client.h"
#include <stdint.h>
enum ksnCMD {

// Core level not TR-UDP mode: 0...63
Expand Down
Loading

0 comments on commit ef164f6

Please sign in to comment.