Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
net_core.c File Reference
#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...
 
ksnCoreClassksnCoreInit (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...
 

Macro Definition Documentation

#define kev   ((ksnetEvMgrClass *)teo_cfg->ke)

Referenced by ksnCoreBind().

#define ksn_bind (   fd,
  addr,
  addr_len 
)    bind(fd, addr, addr_len)
#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 new_peer   rd->from

Referenced by send_cmd_connected_cb().

#define PACKET_HEADER_ADD_SIZE   2
#define sendto_encrypt (   kc,
  cmd,
  DATA,
  D_LEN 
)
Value:
{ \
if(((ksnetEvMgrClass*)kc->ke)->teo_cfg.crypt_f) { \
size_t data_len; \
char *buffer = NULL; /*[KSN_BUFFER_DB_SIZE];*/ \
void *data = ksnEncryptPackage(kc->kcr, DATA, D_LEN, buffer, &data_len); \
retval = ksn_sendto(kc->ku, cmd, kc->fd, data, data_len, 0, \
(struct sockaddr *)&remaddr, addrlen); \
free(data); \
} \
else { \
retval = ksn_sendto(kc->ku, cmd, kc->fd, DATA, D_LEN, 0, \
(struct sockaddr *)&remaddr, addrlen); \
} \
}
if(!(yy_init))
Definition: lexer-filter.c:653
const char data[]
Make it with: gcc -o post-callback post-callback.c -lcurl.
Definition: post-callback.c:23
#define kc
Definition: hotkeys.c:72
#define ksn_sendto(tu, cmd, fd, data, data_len, flags, remaddr, addrlen)
Definition: net_core.c:58
KSNet event manager functions data.
Definition: ev_mgr.h:245
void free(void *)
void * ksnEncryptPackage(ksnCryptClass *kcr, void *package, size_t package_len, void *buffer, size_t *encrypt_len)
Encrypt KSNet package.
Definition: crypt.c:232

Encrypt packet and sent to.

Parameters
kc
cmd
DATA
D_LEN
Returns

Referenced by ksnCoreSendto().

Typedef Documentation

typedef struct peer_type_req peer_type_req_t
typedef struct send_by_type_check_t send_by_type_check_t

Function Documentation

void host_cb ( EV_P_ ev_io *  w,
int  revents 
)

Teonet host fd read event callback.

param loop

Parameters
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.

Parameters
kcPointer to ksnCoreClass
Returns
0 if successfully created and bind

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.

Parameters
[in]teo_cfgPointer to teonet configuration: teonet_cfg
[out]portPointer to Port number
Returns
File descriptor or error if return value < 0

Referenced by ksnCoreBind(), and ksnTCPProxyServerClientConnect().

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 * 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

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

References malloc(), and PACKET_HEADER_ADD_SIZE.

Referenced by ksnCoreCreatePacket().

ksnCoreClass* ksnCoreInit ( void *  ke,
char *  name,
int  port,
char *  addr 
)

Initialize ksnet core.

Create socket FD and Bind ksnet UDP client/server

Parameters
kePointer to ksnetEvMgrClass
nameHost name
portHost port. 0 - create system dependent port number
addrHost IP. NULL - listen at all IPs
Returns
Pointer to ksnCoreClass or NULL if error

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.

Parameters
packetReceived packet
packet_lenReceived packet length
rdPointer 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().

int ksnCoreSendto ( ksnCoreClass kc,
char *  addr,
int  port,
uint8_t  cmd,
void *  data,
size_t  data_len 
)

Send data to remote peer IP:Port.

Parameters
kcPointer to KSNet core class object
addrIP address of remote peer
portPort of remote peer
cmdCommand ID
dataPointer to data
data_lenData size
Returns
Return 0 if success; -1 if data length is too lage (more than MAX_PACKET_LEN)

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 
)
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.

Parameters
kaPointer to ksnetArpClass
child_peerChild peer name
arp_dataChild arp data
dataPointer 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.

Parameters
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.

Parameters
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.

Parameters
kcPointer to ksnCoreClass
toPeer name to send to
cmdCommand
dataCommands data
data_lenCommands data length
Returns
Pointer to ksnet_arp_data or NULL if to peer is absent

References DEBUG, free(), ksnCoreClass::ka, ke, ksnCoreClass::ke, ksn_printf, ksnCoreSendto(), ksnetArpGetAll(), MODULE, and send_by_type_check_cb().

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

Variable Documentation

const char* localhost = "::1"

KSNet mesh core module.

Referenced by ksnetArpAddHost().