Teonet library
0.4.7
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config/config.h"
#include <ev.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "ev_mgr.h"
#include "net_split.h"
#include "net_multi.h"
#include "net_com.h"
#include "utils/utils.h"
#include "utils/rlutil.h"
#include "utils/teo_memory.h"
#include "tr-udp.h"
#include "commands_creator.h"
Macros | |
#define | PACKET_HEADER_ADD_SIZE 2 |
#define | MODULE _ANSI_GREEN "net_core" _ANSI_GREY |
#define | kev ((ksnetEvMgrClass *)teo_cfg->ke) |
#define | ksn_socket(domain, type, protocol) socket(domain, type, protocol) |
#define | ksn_bind(fd, addr, addr_len) bind(fd, addr, addr_len) |
#define | ksn_sendto(tu, cmd, fd, data, data_len, flags, remaddr, addrlen) ksnTRUDPsendto(tu, 0, 0, 0, cmd, fd, data, data_len, flags, remaddr, addrlen) |
#define | ksn_recvfrom(ku, fd, buf, buf_len, flags, remaddr, addrlen) ksnTRUDPrecvfrom(ku, fd, buf, buf_len, flags, remaddr, addrlen) |
#define | sendto_encrypt(kc, cmd, DATA, D_LEN) |
Encrypt packet and sent to. More... | |
#define | rd ((ksnCorePacketData*) data) |
#define | new_peer rd->from |
Typedefs | |
typedef struct send_by_type_check_t | send_by_type_check_t |
typedef struct peer_type_req | peer_type_req_t |
Functions | |
void | host_cb (EV_P_ ev_io *w, int revents) |
Teonet host fd read event callback. More... | |
int | ksnCoreBind (ksnCoreClass *kc) |
Create and bind UDP socket for client/server. More... | |
void * | ksnCoreCreatePacket (ksnCoreClass *kc, uint8_t cmd, const void *data, size_t data_len, size_t *packet_len) |
Create ksnet packet. More... | |
void * | ksnCoreCreatePacketFrom (ksnCoreClass *kc, uint8_t cmd, char *from, size_t from_len, const void *data, size_t data_len, size_t *packet_len) |
Create ksnet packet with from field from another host (Resend) More... | |
int | send_cmd_disconnect_peer_cb (ksnetArpClass *ka, char *name, ksnet_arp_data_ext *arp_data, void *data) |
Send command to peer to disconnect peer. More... | |
int | send_cmd_disconnect_cb (ksnetArpClass *ka, char *name, ksnet_arp_data_ext *arp_data, void *data) |
Send command disconnect to peer. More... | |
int | send_cmd_connected_cb (ksnetArpClass *ka, char *child_peer, ksnet_arp_data *arp_data, void *data) |
Send command connected to peer. More... | |
ksnCoreClass * | ksnCoreInit (void *ke, char *name, int port, char *addr) |
Initialize ksnet core. More... | |
void | ksnCoreDestroy (ksnCoreClass *kc) |
Close socket and free memory. More... | |
int | ksnCoreBindRaw (int *port, int allow_port_increment_f) |
Create and bind UDP socket for client/server. More... | |
int | ksnCoreSendto (ksnCoreClass *kc, char *addr, int port, uint8_t cmd, void *data, size_t data_len) |
Send data to remote peer IP:Port. More... | |
int | send_by_type_check_cb (ksnetArpClass *ka, char *peer_name, ksnet_arp_data_ext *arp, void *pd) |
void | teoBroadcastSend (ksnCoreClass *kc, char *to, uint8_t cmd, void *data, size_t data_len) |
Send brodcast command to peers by type. More... | |
ksnet_arp_data * | ksnCoreSendCmdto (ksnCoreClass *kc, char *to, uint8_t cmd, void *data, size_t data_len) |
Send command by name to peer. More... | |
int | ksnCoreParsePacket (void *packet, size_t packet_len, ksnCorePacketData *rd) |
Parse received data to ksnCoreRecvData structure. More... | |
void | peer_type_cb (uint32_t id, int type, void *data) |
void | ksnCoreCheckNewPeer (ksnCoreClass *kc, ksnCorePacketData *rd) |
Check new peer. More... | |
void | ksnCoreProcessPacket (void *vkc, void *buf, size_t recvlen, __SOCKADDR_ARG remaddr) |
Process teonet packet. More... | |
Variables | |
const char * | localhost = "::1" |
KSNet mesh core module. More... | |
#define kev ((ksnetEvMgrClass *)teo_cfg->ke) |
Referenced by ksnCoreBind().
#define ksn_recvfrom | ( | ku, | |
fd, | |||
buf, | |||
buf_len, | |||
flags, | |||
remaddr, | |||
addrlen | |||
) | ksnTRUDPrecvfrom(ku, fd, buf, buf_len, flags, remaddr, addrlen) |
Referenced by host_cb().
#define ksn_sendto | ( | tu, | |
cmd, | |||
fd, | |||
data, | |||
data_len, | |||
flags, | |||
remaddr, | |||
addrlen | |||
) | ksnTRUDPsendto(tu, 0, 0, 0, cmd, fd, data, data_len, flags, remaddr, addrlen) |
#define ksn_socket | ( | domain, | |
type, | |||
protocol | |||
) | socket(domain, type, protocol) |
#define MODULE _ANSI_GREEN "net_core" _ANSI_GREY |
Referenced by ksnCoreBind(), ksnCoreCheckNewPeer(), ksnCoreProcessPacket(), ksnCoreSendCmdto(), ksnCoreSendto(), and teoBroadcastSend().
#define new_peer rd->from |
Referenced by send_cmd_connected_cb().
#define PACKET_HEADER_ADD_SIZE 2 |
Referenced by ksnCoreCreatePacketFrom(), and ksnCoreParsePacket().
#define rd ((ksnCorePacketData*) data) |
Referenced by cmd_l0_cb(), event_cb(), event_cb_client(), event_cb_server(), ksnCoreProcessPacket(), ksnLNullSendEchoToL0A(), teo::Teonet::TeoDB::process(), teo::Teonet::TeoDB::send(), send_cmd_connected_cb(), sendCmdAnswerToBinaryA(), teo::Teonet::sendToL0A(), teoAsyncTest(), and trudp_send_event_ack_to_app().
#define sendto_encrypt | ( | kc, | |
cmd, | |||
DATA, | |||
D_LEN | |||
) |
Encrypt packet and sent to.
kc | |
cmd | |
DATA | |
D_LEN |
Referenced by ksnCoreSendto().
typedef struct peer_type_req peer_type_req_t |
typedef struct send_by_type_check_t send_by_type_check_t |
void host_cb | ( | EV_P_ ev_io * | w, |
int | revents | ||
) |
Teonet host fd read event callback.
param loop
w | |
revents |
References ksnCoreClass::fd, kc, ksnetEvMgrClass::kc, ke, ksnCoreClass::ke, KSN_BUFFER_DB_SIZE, ksn_recvfrom, ksnCoreProcessPacket(), ksnCoreSetEventTime, and ksnCoreClass::ku.
Referenced by _cmd_tcpp_read_cb(), and ksnCoreInit().
int ksnCoreBind | ( | ksnCoreClass * | kc | ) |
Create and bind UDP socket for client/server.
kc | Pointer to ksnCoreClass |
References DEBUG_VV, fd, ksnCoreClass::fd, ksnCoreClass::ke, kev, ksn_printf, ksnCoreBindRaw(), MESSAGE, MODULE, ksnCoreClass::port, and teonet_cfg::port_inc_f.
Referenced by ksnCoreInit().
int ksnCoreBindRaw | ( | int * | port, |
int | allow_port_increment_f | ||
) |
Create and bind UDP socket for client/server.
[in] | teo_cfg | Pointer to teonet configuration: teonet_cfg |
[out] | port | Pointer to Port number |
Referenced by ksnCoreBind(), and ksnTCPProxyServerClientConnect().
void ksnCoreCheckNewPeer | ( | ksnCoreClass * | kc, |
ksnCorePacketData * | rd | ||
) |
Check new peer.
kc | |
rd |
References ksnCoreClass::addr, ksnCorePacketData::addr, ksnCorePacketData::arp, ksnCorePacketData::cmd, CMD_CONNECT_R, CMD_HOST_INFO, CMD_NONE, cq, ksnCorePacketData::data, ksnCorePacketData::data_len, DEBUG, DEBUG_VV, ksnCorePacketData::from, ksnCQueData::id, ksnCoreClass::ka, ksnetEvMgrClass::kc, ke, ksnCoreClass::ke, ksnetEvMgrClass::kq, ksn_printf, ksnCoreSendto(), ksnCQueAdd(), ksnetArpAdd(), ksnetArpGet(), ksnetArpGetAll(), ksnetEvMgrGetTime(), malloc(), MODULE, peer_type_cb(), ksnCorePacketData::port, teonet_cfg::r_host_addr, teonet_cfg::r_host_name, teonet_cfg::r_port, send_cmd_connect_cb(), send_cmd_connect_cb_b(), ksnetEvMgrClass::teo_cfg, and connect_r_packet::type.
Referenced by ksnCoreProcessPacket().
|
inline |
Create ksnet packet.
kc | Pointer to ksnCore Class object | |
cmd | Command ID | |
data | Pointer to data | |
data_len | Data length | |
[out] | packet_len | Pointer to packet length |
References ksnCoreCreatePacketFrom(), ksnCoreClass::name, and ksnCoreClass::name_len.
Referenced by ksnCoreSendto().
void * ksnCoreCreatePacketFrom | ( | ksnCoreClass * | kc, |
uint8_t | cmd, | ||
char * | from, | ||
size_t | from_len, | ||
const void * | data, | ||
size_t | data_len, | ||
size_t * | packet_len | ||
) |
Create ksnet packet with from field from another host (Resend)
This function created to resend messages between networks in multi network application
kc | Pointer to ksnCore Class object | |
cmd | Command ID | |
from | From name | |
from_len | From name length | |
data | Pointer to data | |
data_len | Data length | |
[out] | packet_len | Pointer to packet length |
References malloc(), and PACKET_HEADER_ADD_SIZE.
Referenced by ksnCoreCreatePacket().
void ksnCoreDestroy | ( | ksnCoreClass * | kc | ) |
Close socket and free memory.
kc | Pointer to ksnCore class object |
References ksnCoreClass::addr, ksnCoreClass::fd, free(), ksnCoreClass::host_w, ksnCoreClass::ka, ksnetEvMgrClass::kc, ksnCoreClass::kco, ke, ksnCoreClass::ke, ksnCommandDestroy(), ksnCryptDestroy(), ksnetArpDestroy(), ksnetArpGetAll(), ksnCoreClass::ku, ksnCoreClass::name, send_cmd_disconnect_cb(), and send_cmd_disconnect_peer_cb().
Referenced by modules_destroy().
ksnCoreClass* ksnCoreInit | ( | void * | ke, |
char * | name, | ||
int | port, | ||
char * | addr | ||
) |
Initialize ksnet core.
Create socket FD and Bind ksnet UDP client/server
ke | Pointer to ksnetEvMgrClass |
name | Host name |
port | Host port. 0 - create system dependent port number |
addr | Host IP. NULL - listen at all IPs |
References ksnCoreClass::addr, ksnCoreClass::fd, host_cb(), ksnCoreClass::host_w, ksnCoreClass::ka, kc, ksnCoreClass::kco, ke, ksnCoreClass::ke, ksnCommandInit(), ksnCoreBind(), ksnCryptInit(), ksnetArpInit(), ksnetArpSetHostPort(), ksnCoreClass::ku, ksnCoreClass::last_check_event, ksnCoreClass::name, ksnCoreClass::name_len, ksnCoreClass::port, teo_malloc(), teo_strdup(), and trudp_event_cb().
Referenced by modules_init().
int ksnCoreParsePacket | ( | void * | packet, |
size_t | packet_len, | ||
ksnCorePacketData * | rd | ||
) |
Parse received data to ksnCoreRecvData structure.
packet | Received packet |
packet_len | Received packet length |
rd | Pointer to ksnCoreRecvData structure |
References ksnCorePacketData::cmd, ksnCorePacketData::data, ksnCorePacketData::data_len, ksnCorePacketData::from, ksnCorePacketData::from_len, PACKET_HEADER_ADD_SIZE, ksnCorePacketData::raw_data, and ksnCorePacketData::raw_data_len.
Referenced by ksnCoreProcessPacket(), and trudp_send_event_ack_to_app().
void ksnCoreProcessPacket | ( | void * | vkc, |
void * | buf, | ||
size_t | recvlen, | ||
__SOCKADDR_ARG | remaddr | ||
) |
Process teonet packet.
vkc | Pointer to ksnCoreClass |
buf | Buffer with packet |
recvlen | Packet length |
remaddr | Address |
References addr_port::addr, ksnCorePacketData::addr, addr_port_free(), ksnCorePacketData::arp, ksnCorePacketData::cmd, CMD_L0, teonet_cfg::crypt_f, data, ksnCorePacketData::data, ksnCorePacketData::data_len, DEBUG_VV, EV_K_RECEIVED, EV_K_RECEIVED_WRONG, ksnetEvMgrClass::event_cb, free(), ksnCorePacketData::from, ksnCorePacketData::from_len, kc, ksnCoreClass::kco, ke, ksnCoreClass::ke, kEventMgrStopped, ksnetEvMgrClass::kl, ksn_printf, ksnCheckEncrypted(), ksnCommandCheck(), ksnCoreCheckNewPeer(), ksnCoreParsePacket(), ksnDecryptPackage(), ksnetEvMgrGetTime(), ksnetEvMgrStatus(), ksnLNulltrudpCheckPaket(), MODULE, addr_port::port, ksnCorePacketData::port, rd, ksnetEvMgrClass::teo_cfg, and wrap_inet_ntop().
Referenced by host_cb(), and trudp_event_cb().
ksnet_arp_data* ksnCoreSendCmdto | ( | ksnCoreClass * | kc, |
char * | to, | ||
uint8_t | cmd, | ||
void * | data, | ||
size_t | data_len | ||
) |
Send command by name to peer.
kc | Pointer to ksnCoreClass |
to | Peer name to send to |
cmd | Command |
data | Commands data |
data_len | Commands data length |
References ksnCoreClass::addr, ksnLNullSPacket::client_name_length, ksnLNullSPacket::cmd, CMD_L0, data, ksnLNullSPacket::data_length, DEBUG, DEBUG_VV, fd, free(), ksnCoreClass::ka, ke, ksnCoreClass::ke, ksnetEvMgrClass::kl, ksnetEvMgrClass::km, ksn_printf, ksnCoreSendto(), ksnetArpGet(), ksnetArpGetAll(), ksnLNullClientIsConnected(), ksnLNullPacketSend(), ksnMultiSendCmdTo(), teonet_cfg::l0_allow_f, malloc(), MODULE, ksnLNullSPacket::payload, send_by_type_check_cb(), and ksnetEvMgrClass::teo_cfg.
Referenced by event_cb(), event_cb_client(), get_cb(), ksnCommandSendCmdConnect(), ksnCommandSendCmdEcho(), ksnCoreSendCmdtoA(), ksnStreamSendTo(), sendCmdT(), teo::Teonet::sendTo(), teoMultiSendCmdToNet(), teoSScrSend(), teoSScrSubscribe(), teoSScrUnSubscribe(), and test_01().
int ksnCoreSendto | ( | ksnCoreClass * | kc, |
char * | addr, | ||
int | port, | ||
uint8_t | cmd, | ||
void * | data, | ||
size_t | data_len | ||
) |
Send data to remote peer IP:Port.
kc | Pointer to KSNet core class object |
addr | IP address of remote peer |
port | Port of remote peer |
cmd | Command ID |
data | Pointer to data |
data_len | Data size |
References CMD_SPLIT, CMD_VPN, DEBUG_VV, ERROR_M, free(), ksnCoreClass::kco, ksnCoreClass::ke, ksnCommandClass::ks, ksn_printf, ksnCoreCreatePacket(), ksnCoreSetEventTime, ksnSplitPacket(), make_addr, MAX_DATA_LEN, MAX_PACKET_LEN, MODULE, and sendto_encrypt.
Referenced by connect_r_host_cb(), ksnCommandSendCmdConnectA(), ksnCoreCheckNewPeer(), ksnCoreSendCmdto(), ksnLNullSendToL0(), open_local_port(), peer_type_cb(), send_cmd_disconnect_cb(), sendCmdAnswerToBinaryA(), and teoBroadcastSend().
void peer_type_cb | ( | uint32_t | id, |
int | type, | ||
void * | data | ||
) |
References CMD_HOST_INFO, cq, data, free(), ksnCQueData::id, ke, ksnetEvMgrClass::kq, ksnCoreSendto(), ksnCQueAdd(), ksnetArpAdd(), and ksnetArpGet().
Referenced by ksnCoreCheckNewPeer().
int send_by_type_check_cb | ( | ksnetArpClass * | ka, |
char * | peer_name, | ||
ksnet_arp_data_ext * | arp, | ||
void * | pd | ||
) |
References malloc().
Referenced by ksnCoreSendCmdto(), and teoBroadcastSend().
int send_cmd_connected_cb | ( | ksnetArpClass * | ka, |
char * | child_peer, | ||
ksnet_arp_data * | arp_data, | ||
void * | data | ||
) |
Send command connected to peer.
ka | Pointer to ksnetArpClass |
child_peer | Child peer name |
arp_data | Child arp data |
data | Pointer to ksnCoreRecvData (included new peer name) |
References ksnetArpClass::ke, ksnCommandSendCmdConnect(), new_peer, and rd.
int send_cmd_disconnect_cb | ( | ksnetArpClass * | ka, |
char * | name, | ||
ksnet_arp_data_ext * | arp_data, | ||
void * | data | ||
) |
Send command disconnect to peer.
ka | |
name | |
arp_data | |
data |
References CMD_DISCONNECTED, ksnetArpClass::ke, and ksnCoreSendto().
Referenced by ksnCoreDestroy(), and send_cmd_disconnect_peer_cb().
int send_cmd_disconnect_peer_cb | ( | ksnetArpClass * | ka, |
char * | name, | ||
ksnet_arp_data_ext * | arp_data, | ||
void * | data | ||
) |
Send command to peer to disconnect peer.
ka | |
name | |
arp_data | |
data |
References ksnetArpGetAll(), and send_cmd_disconnect_cb().
Referenced by ksnCoreDestroy().
void teoBroadcastSend | ( | ksnCoreClass * | kc, |
char * | to, | ||
uint8_t | cmd, | ||
void * | data, | ||
size_t | data_len | ||
) |
Send brodcast command to peers by type.
kc | Pointer to ksnCoreClass |
to | Peer name to send to |
cmd | Command |
data | Commands data |
data_len | Commands data length |
References DEBUG, free(), ksnCoreClass::ka, ke, ksnCoreClass::ke, ksn_printf, ksnCoreSendto(), ksnetArpGetAll(), MODULE, and send_by_type_check_cb().
Referenced by teo::Teonet::BroadcastSend().
const char* localhost = "::1" |
KSNet mesh core module.
Referenced by ksnetArpAddHost().