Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref/l0server #186

Merged
merged 6 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Some renaming for correct meaning.
  • Loading branch information
angelskieglazki committed Feb 26, 2021
commit d756e089a449fdf797199aa280987e3d8b364446
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
50 changes: 24 additions & 26 deletions src/modules/l0-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#include <stdbool.h>
#include <string.h>

#include "jsmn.h"
#include "ev_mgr.h"
#include "l0-server.h"
#include "utils/rlutil.h"
#include "jsmn.h"

#include "teonet_l0_client_crypt.h"

Expand Down Expand Up @@ -413,18 +413,18 @@ static ksnet_arp_data *ksnLNullSendFromL0(ksnLNullClass *kl, teoLNullCPacket *pa

// Create teonet L0 packet
spacket->cmd = packet->cmd;
spacket->from_length = cname_length;
memcpy(spacket->from, cname, cname_length);
spacket->client_name_length = cname_length;
memcpy(spacket->payload, cname, cname_length);
spacket->data_length = packet->data_length;
memcpy(spacket->from + spacket->from_length,
memcpy(spacket->payload + spacket->client_name_length,
packet->peer_name + packet->peer_name_length, spacket->data_length);

// Send teonet L0 packet
ksnet_arp_data *arp_data = NULL;
#ifdef DEBUG_KSNET
ksn_printf(kev, MODULE, extendedLog(kl),
"send packet to peer \"%s\" from L0 client \"%s\" ...\n",
packet->peer_name, spacket->from);
packet->peer_name, spacket->payload);
#endif

// Send to peer
Expand Down Expand Up @@ -480,16 +480,16 @@ int ksnLNullSendToL0(void *ke, char *addr, int port, char *cname,

// Create teonet L0 packet
spacket->cmd = cmd;
spacket->from_length = cname_length;
memcpy(spacket->from, cname, cname_length);
spacket->client_name_length = cname_length;
memcpy(spacket->payload, cname, cname_length);
spacket->data_length = data_len;
memcpy(spacket->from + spacket->from_length, data, data_len);
memcpy(spacket->payload + spacket->client_name_length, data, data_len);

// Send command to client of L0 server
#ifdef DEBUG_KSNET
ksn_printf(((ksnetEvMgrClass*)ke), MODULE, DEBUG_VV,
"send command to L0 server for client \"%s\" ...\n",
spacket->from);
spacket->payload);
#endif
int rv = ksnCoreSendto(((ksnetEvMgrClass*)ke)->kc, addr, port, CMD_L0_TO,
out_data, out_data_len);
Expand Down Expand Up @@ -1290,34 +1290,32 @@ int cmd_l0_cb(ksnetEvMgrClass *ke, ksnCorePacketData *rd) {
(data->cmd >= CMD_USER && data->cmd < CMD_192_RESERVED) ||
(data->cmd >= CMD_USER_NR && data->cmd < CMD_LAST)) {

// \TODO: char *client_name = data->payload;
#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV,
"got valid command No %d from %s client with %d bytes data ...\n",
data->cmd, data->from, data->data_length);
data->cmd, data->payload, data->data_length);
#endif

ksnCorePacketData *rds = rd;

// Process command
rds->cmd = data->cmd;
rds->from = data->from;
rds->from_len = data->from_length;
rds->data = data->from + data->from_length;
rds->from = data->payload;
rds->from_len = data->client_name_length;
rds->data = data->payload + data->client_name_length;
rds->data_len = data->data_length;
rds->l0_f = 1;

// Execute L0 client command
retval = ksnCommandCheck(ke->kc->kco, rds);
}
// Wrong command
else {

} else {
#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV,
"%s" "got wrong command No %d from %s client with %d bytes data, "
"the command skipped ...%s\n",
ANSI_RED,
data->cmd, data->from, data->data_length,
data->cmd, data->payload, data->data_length,
ANSI_NONE);
#endif
}
Expand Down Expand Up @@ -1358,7 +1356,7 @@ int cmd_l0_to_cb(ksnetEvMgrClass *ke, ksnCorePacketData *rd) {
ksn_printf(ke, MODULE, extendedLog(ke->kl),
"got command No %d for \"%s\" L0 client from peer \"%s\" "
"with %d bytes data\n",
data->cmd, data->from, rd->from, data->data_length);
data->cmd, data->payload, rd->from, data->data_length);
#endif

// Got packet from peer statistic
Expand All @@ -1367,7 +1365,7 @@ int cmd_l0_to_cb(ksnetEvMgrClass *ke, ksnCorePacketData *rd) {
// If l0 module is initialized
if(ke->kl) {

int fd = ksnLNullClientIsConnected(ke->kl, data->from);
int fd = ksnLNullClientIsConnected(ke->kl, data->payload);
if (fd) {

// Create L0 packet
Expand All @@ -1376,7 +1374,7 @@ int cmd_l0_to_cb(ksnetEvMgrClass *ke, ksnCorePacketData *rd) {
char *out_data = malloc(out_data_len);
memset(out_data, 0, out_data_len);
size_t packet_length = teoLNullPacketCreate(out_data, out_data_len,
data->cmd, rd->from, (const uint8_t*)data->from + data->from_length,
data->cmd, rd->from, (const uint8_t*)data->payload + data->client_name_length,
data->data_length);

// Send command to L0 client
Expand All @@ -1391,7 +1389,7 @@ int cmd_l0_to_cb(ksnetEvMgrClass *ke, ksnCorePacketData *rd) {
ksn_printf(ke, MODULE, DEBUG_VV,
"send %d bytes to \"%s\" L0 client: %d bytes data, "
"from peer \"%s\"\n",
(int)snd, data->from,
(int)snd, data->payload,
packet->data_length, packet->peer_name);
#endif
free(out_data);
Expand All @@ -1403,7 +1401,7 @@ int cmd_l0_to_cb(ksnetEvMgrClass *ke, ksnCorePacketData *rd) {
#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, DEBUG_VV,
"%s" "the \"%s\" L0 client has not connected to the server%s\n",
ANSI_RED, data->from, ANSI_NONE);
ANSI_RED, data->payload, ANSI_NONE);
#endif
}
}
Expand All @@ -1413,7 +1411,7 @@ int cmd_l0_to_cb(ksnetEvMgrClass *ke, ksnCorePacketData *rd) {
#ifdef DEBUG_KSNET
ksn_printf(ke, MODULE, ERROR_M,
"can't resend command %d to L0 client \"%s\" from peer \"%s\": " "%s" "the L0 module has not been initialized at this host%s\n",
data->cmd, data->from, rd->from, ANSI_RED, ANSI_NONE);
data->cmd, data->payload, rd->from, ANSI_RED, ANSI_NONE);
#endif
}

Expand Down Expand Up @@ -1644,7 +1642,6 @@ int cmd_l0_check_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
}

// Create & Send websocket allow answer message
size_t snd;
char *ALLOW = ksnet_formatMessage("{ \"name\": \"%s\", \"networks\": %s }",
kld->name ? kld->name : "", jp.networks ? jp.networks : "null");
size_t ALLOW_len = strlen(ALLOW) + 1;
Expand All @@ -1658,7 +1655,8 @@ int cmd_l0_check_cb(ksnCommandClass *kco, ksnCorePacketData *rd) {
teoLNullPacketCreate(out_data, out_data_len,
CMD_L0_AUTH, rd->from, (uint8_t *)ALLOW, ALLOW_len);
// Send websocket allow message
if((snd = ksnLNullPacketSend(ke->kl, fd, out_data, packet_length)) >= 0);
ssize_t snd = ksnLNullPacketSend(ke->kl, fd, out_data, packet_length);
(void)snd;
free(out_data);
free(ALLOW);
}
Expand Down
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/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
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
18 changes: 9 additions & 9 deletions src/net_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,22 +430,22 @@ ksnet_arp_data *ksnCoreSendCmdto(ksnCoreClass *kc, char *to, uint8_t cmd,
"send command to L0 client \"%s\" to r-host\n", to);
#endif

ssize_t snd;
ksnLNullSPacket *cmd_l0_data = data;
ksnLNullSPacket *spacket = data;

const size_t buf_length = teoLNullBufferSize(
cmd_l0_data->from_length,
cmd_l0_data->data_length
spacket->client_name_length,
spacket->data_length
);

char *buf = malloc(buf_length);
teoLNullPacketCreate(buf, buf_length,
cmd_l0_data->cmd,
cmd_l0_data->from,
(uint8_t *)cmd_l0_data->from + cmd_l0_data->from_length,
(size_t)cmd_l0_data->data_length);
spacket->cmd,
spacket->payload,
(uint8_t *)spacket->payload + spacket->client_name_length,
(size_t)spacket->data_length);

if((snd = ksnLNullPacketSend(ke->kl, fd, buf, buf_length)) >= 0);
ssize_t snd = ksnLNullPacketSend(ke->kl, fd, buf, buf_length);
(void)snd;
free(buf);
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "utils.h"
#include "ev_mgr.h"


#define ADDRSTRLEN 128

//double ksnetEvMgrGetTime(void *ke);
Expand Down