Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
l0-server.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "ev_mgr.h"
#include "l0-server.h"
#include "utils/rlutil.h"
#include "jsmn.h"
#include "teonet_l0_client_crypt.h"
#include <openssl/md5.h>

Macros

#define MODULE   _ANSI_LIGHTCYAN "l0_server" _ANSI_NONE
 File: l0-server.c Author: Kirill Scherba kiril.nosp@m.l@sc.nosp@m.herba.nosp@m..ru More...
 
#define TEO_AUTH   "teo-auth"
 
#define WG001   "wg001-"
 
#define WG001_NEW   "wg001-new-"
 
#define kev   ((ksnetEvMgrClass*)kl->ke)
 Pointer to ksnetEvMgrClass. More...
 
#define L0_VERSION   0
 L0 Server version. More...
 
#define CHECK_TIMEOUT   10.00
 
#define SEND_PING_TIMEOUT   30.00
 
#define DISCONNECT_TIMEOUT   60.00
 
#define find_tag(VAR, KEY, TAG)
 
#define BUFFER_SIZE_CLIENT   4096
 

Typedefs

typedef struct string_view string_view
 
typedef struct json_param json_param
 JSON request parameters structure. More...
 

Functions

void * ksnCoreCreatePacket (ksnCoreClass *kc, uint8_t cmd, const void *data, size_t data_len, size_t *packet_len)
 Create ksnet packet. More...
 
void teoLNullPacketCheckMiscrypted (ksnLNullClass *kl, ksnLNullData *kld, teoLNullCPacket *packet)
 
ksnLNullClassksnLNullInit (void *ke)
 Initialize ksnLNull module class. More...
 
void ksnLNullDestroy (ksnLNullClass *kl)
 Destroy ksnLNull module class. More...
 
ksnLNullSStat * ksnLNullStat (ksnLNullClass *kl)
 Get L0 server statistic. More...
 
int ksnLNullSendToL0 (void *ke, char *addr, int port, char *cname, size_t cname_length, uint8_t cmd, void *data, size_t data_len)
 Send data to L0 client. More...
 
int ksnLNullSendEchoToL0 (void *ke, char *addr, int port, char *cname, size_t cname_length, void *data, size_t data_len)
 Send echo to L0 client. More...
 
int ksnLNullSendEchoToL0A (void *ke, char *addr, int port, char *cname, size_t cname_length, void *data, size_t data_len)
 
void _send_subscribe_event_disconnected (ksnetEvMgrClass *ke, char *payload, size_t payload_length)
 
void _send_subscribe_event_connected (ksnetEvMgrClass *ke, char *payload, size_t payload_length)
 Send Connected event to all subscribers. More...
 
void _send_subscribe_event_newvisit (ksnetEvMgrClass *ke, const char *payload, size_t payload_length)
 Send "new visit" event to all subscribers. More...
 
ssize_t ksnLNullPacketSend (ksnLNullClass *kl, int fd, void *pkg, size_t pkg_length)
 Send packet to L0 client. More...
 
void ksnLNullClientDisconnect (ksnLNullClass *kl, int fd, int remove_f)
 Disconnect l0 Server client. More...
 
void _check_connected (uint32_t id, int type, void *data)
 
int cmd_l0_broadcast_cb (ksnetEvMgrClass *ke, ksnCorePacketData *rd)
 Process CMD_L0_CLIENT_BROADCAST teonet command (from peer to l0) More...
 
int cmd_l0_cb (ksnetEvMgrClass *ke, ksnCorePacketData *rd)
 Process CMD_L0 teonet command. More...
 
int ksnLNullClientIsConnected (ksnLNullClass *kl, char *client_name)
 Check if L0 client is connected and return it FD. More...
 
int cmd_l0_to_cb (ksnetEvMgrClass *ke, ksnCorePacketData *rd)
 Process CMD_L0_TO teonet command. More...
 
int cmd_l0_check_cb (ksnCommandClass *kco, ksnCorePacketData *rd)
 Check l0 client answer from authentication application. More...
 
int cmd_l0_kick_client (ksnCommandClass *kco, ksnCorePacketData *rd)
 Kick l0 client. More...
 
teonet_client_data_ar * ksnLNullClientsList (ksnLNullClass *kl)
 Create list of clients. More...
 
size_t ksnLNullClientsListLength (teonet_client_data_ar *clients_data)
 Return size of teonet_client_data_ar data. More...
 
ssize_t recvCheck (trudpChannelData *tcd, char *data, ssize_t data_length)
 
int ksnLNulltrudpCheckPaket (ksnLNullClass *kl, ksnCorePacketData *rd)
 Check and process L0 packet received through TR-UDP. More...
 
teoLNullEncryptionContext * ksnLNullClientGetCrypto (ksnLNullClass *kl, int fd)
 Returns L0 client encryption context and return it if available. More...
 

Variables

const char * localhost
 KSNet mesh core module. More...
 

Macro Definition Documentation

#define BUFFER_SIZE_CLIENT   4096

Referenced by recvCheck().

#define CHECK_TIMEOUT   10.00

Referenced by _check_connected().

#define DISCONNECT_TIMEOUT   60.00

Referenced by _check_connected().

#define find_tag (   VAR,
  KEY,
  TAG 
)
Value:
if(!(keys & KEY) && jsoneq(data, &t[i], TAG) == 0) { \
\
VAR = strndup((char*)data + t[i+1].start, \
t[i+1].end-t[i+1].start); \
keys |= KEY; \
i++; \
}
const char data[]
Make it with: gcc -o post-callback post-callback.c -lcurl.
Definition: post-callback.c:23
#define KEY
#define kev   ((ksnetEvMgrClass*)kl->ke)

Pointer to ksnetEvMgrClass.

#define L0_VERSION   0

L0 Server version.

#define SEND_PING_TIMEOUT   30.00

Referenced by _check_connected().

#define TEO_AUTH   "teo-auth"
#define WG001   "wg001-"
#define WG001_NEW   "wg001-new-"

Typedef Documentation

typedef struct json_param json_param

JSON request parameters structure.

userId clientId username accessToken

typedef struct string_view string_view

Function Documentation

void _send_subscribe_event_connected ( ksnetEvMgrClass ke,
char *  payload,
size_t  payload_length 
)
void _send_subscribe_event_disconnected ( ksnetEvMgrClass ke,
char *  payload,
size_t  payload_length 
)
void _send_subscribe_event_newvisit ( ksnetEvMgrClass ke,
const char *  payload,
size_t  payload_length 
)
int cmd_l0_broadcast_cb ( ksnetEvMgrClass ke,
ksnCorePacketData rd 
)

Process CMD_L0_CLIENT_BROADCAST teonet command (from peer to l0)

Parameters
kePointer to ksnetEvMgrClass
rdPointer to ksnCorePacketData data
Returns
If true - than command was processed by this function

References ksnCorePacketData::cmd, ksnCorePacketData::data, ksnCorePacketData::data_len, ksnetEvMgrClass::kl, teonet_cfg::l0_allow_f, and ksnetEvMgrClass::teo_cfg.

Referenced by ksnCommandCheck().

int cmd_l0_check_cb ( ksnCommandClass kco,
ksnCorePacketData rd 
)

Check l0 client answer from authentication application.

Parameters
kcoPointer to ksnCommandClass
rdPointer to ksnCorePacketData
Returns

References DEBUG, EVENT_MANAGER_OBJECT, free(), ksnetEvMgrClass::kl, ksn_printf, ksnLNullClientIsConnected(), ksnLNullClass::map, and MODULE.

Referenced by ksnCommandCheck().

void * ksnCoreCreatePacket ( ksnCoreClass kc,
uint8_t  cmd,
const void *  data,
size_t  data_len,
size_t *  packet_len 
)
inline

Create ksnet packet.

Parameters
kcPointer to ksnCore Class object
cmdCommand ID
dataPointer to data
data_lenData length
[out]packet_lenPointer to packet length
Returns
Pointer to packet. Should be free after use.

References ksnCoreCreatePacketFrom(), ksnCoreClass::name, and ksnCoreClass::name_len.

Referenced by ksnCoreSendto().

void ksnLNullClientDisconnect ( ksnLNullClass kl,
int  fd,
int  remove_f 
)

Disconnect l0 Server client.

Called when client disconnected or when the L0 Server closing. Close TCP connections and remove data from L0 Server clients map.

Parameters
klPointer to ksnLNullClass
fdTCP client connection file descriptor
remove_fIf true than remove disconnected record from map

References _send_subscribe_event_disconnected(), CONNECT, ksnetEvMgrClass::ev_loop, EVENT_MANAGER_OBJECT, free(), ksn_printf, malloc(), ksnLNullClass::map, ksnLNullClass::map_n, MODULE, ksnLNullData::name, ksnLNullData::name_length, pblMapRemoveFree(), ksnLNullData::read_buffer, ksnLNullData::read_buffer_ptr, ksnLNullData::read_buffer_size, ksnLNullData::server_crypt, ksnLNullClass::stat, ksnLNullData::t_addr, and ksnLNullData::w.

Referenced by _check_connected(), cmd_l0_kick_client(), ksnLNulltrudpCheckPaket(), and trudp_event_cb().

teoLNullEncryptionContext* ksnLNullClientGetCrypto ( ksnLNullClass kl,
int  fd 
)

Returns L0 client encryption context and return it if available.

Parameters
klPointer to ksnLNullClass
fdClient name connection fd
Returns
teoLNullEncryptionContext pointer or null if not available

References ksnLNullData::server_crypt.

int ksnLNullClientIsConnected ( ksnLNullClass kl,
char *  client_name 
)

Check if L0 client is connected and return it FD.

Parameters
klPointer to ksnLNullClass
client_nameClient name
Returns
Connection fd or NULL ifnot connected

References ksnLNullClass::map_n.

Referenced by cmd_l0_check_cb(), cmd_l0_kick_client(), cmd_l0_to_cb(), and ksnCoreSendCmdto().

teonet_client_data_ar* ksnLNullClientsList ( ksnLNullClass kl)
size_t ksnLNullClientsListLength ( teonet_client_data_ar *  clients_data)
inline

Return size of teonet_client_data_ar data.

Parameters
clients_data
Returns
Size of teonet_client_data_ar data
void ksnLNullDestroy ( ksnLNullClass kl)

Destroy ksnLNull module class.

Parameters
klPointer to ksnLNullClass

References free(), ksnLNullClass::map, and ksnLNullClass::map_n.

Referenced by modules_destroy().

ksnLNullClass* ksnLNullInit ( void *  ke)

Initialize ksnLNull module class.

Parameters
kePointer to ksnetEvMgrClass
Returns
Pointer to created ksnLNullClass or NULL if error occurred

References ksnLNullClass::fd_trudp, ksnLNullClass::ke, ke, malloc(), ksnLNullClass::map, ksnLNullClass::map_n, and ksnLNullClass::stat.

Referenced by modules_init().

ssize_t ksnLNullPacketSend ( ksnLNullClass kl,
int  fd,
void *  pkg,
size_t  pkg_length 
)

Send packet to L0 client.

Parameters
fdL0 client socket
pkgPackage to send
pkg_lengthPackage length
Returns
Length of send data or -1 at error

< Remote address

< Remote address length

References CMD_TRUDP_CHECK, dump_bytes(), EVENT_MANAGER_OBJECT, ksnLNullClass::ke, ksn_printf, ksnTRUDPsendto(), MODULE, ksnLNullData::server_crypt, ksnLNullClass::stat, ksnLNullData::t_addr, and ksnLNullData::t_port.

Referenced by _check_connected(), cmd_l0_to_cb(), and ksnCoreSendCmdto().

int ksnLNullSendEchoToL0 ( void *  ke,
char *  addr,
int  port,
char *  cname,
size_t  cname_length,
void *  data,
size_t  data_len 
)

Send echo to L0 client.

Parameters
kePointer to ksnetEvMgrClass
addrIP address of remote peer
portPort of remote peer
cnameL0 client name (include trailing zero)
cname_lengthLength of the L0 client name
dataData
data_lenData length
Returns
Return 0 if success; -1 if data length is too lage (more than 32319)

References CMD_ECHO, free(), kc, ksnCommandEchoBuffer(), and ksnLNullSendToL0().

Referenced by teo::Teonet::sendEchoToL0().

int ksnLNullSendEchoToL0A ( void *  ke,
char *  addr,
int  port,
char *  cname,
size_t  cname_length,
void *  data,
size_t  data_len 
)
int ksnLNullSendToL0 ( void *  ke,
char *  addr,
int  port,
char *  cname,
size_t  cname_length,
uint8_t  cmd,
void *  data,
size_t  data_len 
)

Send data to L0 client.

Usually it is an answer to request from L0 client

Parameters
kePointer to ksnetEvMgrClass
addrIP address of remote peer
portPort of remote peer
cnameL0 client name (include trailing zero)
cname_lengthLength of the L0 client name
cmdCommand
dataData
data_lenData length
Returns
Return 0 if success; -1 if data length is too lage (more than 32319)

References ksnLNullSPacket::client_name_length, ksnLNullSPacket::cmd, CMD_L0_TO, ksnLNullSPacket::data_length, DEBUG_VV, free(), kc, ksn_printf, ksnCoreSendto(), malloc(), MODULE, and ksnLNullSPacket::payload.

Referenced by ksnLNullSendEchoToL0(), sendCmdAnswerToBinaryA(), teo::Teonet::sendToL0(), and teoSScrSend().

ksnLNullSStat* ksnLNullStat ( ksnLNullClass kl)

Get L0 server statistic.

Parameters
klPointer to ksnLNullClass
Returns
Pointer to ksnLNullSStat or NULL if L0 server has not started

References ksnLNullClass::stat.

Referenced by metric_teonet_count().

ssize_t recvCheck ( trudpChannelData *  tcd,
char *  data,
ssize_t  data_length 
)

References BUFFER_SIZE_CLIENT.

Referenced by ksnLNulltrudpCheckPaket().

void teoLNullPacketCheckMiscrypted ( ksnLNullClass kl,
ksnLNullData kld,
teoLNullCPacket *  packet 
)

Variable Documentation

const char* localhost

KSNet mesh core module.