Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tcp_proxy.c File Reference
#include <stdlib.h>
#include <string.h>
#include "tcp_proxy.h"
#include "ev_mgr.h"
#include "net_core.h"
#include "utils/rlutil.h"

Macros

#define MODULE   _ANSI_YELLOW "tcp_proxy" _ANSI_NONE
 
#define kev   ((ksnetEvMgrClass*)tp->ke)
 Pointer to ksnetEvMgrClass. More...
 
#define TCP_PROXY_VERSION   0
 TCP Proxy version. More...
 

Enumerations

enum  ksnTCPProxyCliSer { CLIENT, SERVER }
 

Functions

void host_cb (EV_P_ ev_io *w, int revents)
 Teonet host fd read event callback. More...
 
int send_cmd_disconnect_cb (ksnetArpClass *ka, char *name, ksnet_arp_data *arp_data, void *data)
 
void _cmd_tcpp_read_cb (struct ev_loop *loop, struct ev_io *w, int revents, int cli_ser)
 TCP Proxy client or server client callback. More...
 
void ksnTCPProxyClientStop (ksnTCPProxyClass *tp)
 Stop TCP Proxy client. More...
 
size_t ksnTCPProxyPackageCreate (void *buffer, size_t buffer_length, const char *addr, int port, int cmd, const void *data, size_t data_length)
 Create TCP Proxy package. More...
 
int ksnTCPProxyServerStart (ksnTCPProxyClass *tp)
 Start TCP Proxy server. More...
 
void ksnTCPProxyServerStop (ksnTCPProxyClass *tp)
 Stop TCP Proxy server. More...
 
void ksnTCPProxyServerClientConnect (ksnTCPProxyClass *tp, int fd)
 Connect TCP Proxy Server client. More...
 
void ksnTCPProxyServerClientDisconnect (ksnTCPProxyClass *tp, int fd, int remove_f)
 Disconnect TCP Proxy Server client. More...
 
ssize_t ksnTCPProxySendTo (ksnetEvMgrClass *ke, int cmd, const void *buffer, size_t buffer_len, __CONST_SOCKADDR_ARG addr)
 Send command to TCP Proxy. More...
 
ksnTCPProxyClassksnTCPProxyInit (void *ke)
 Initialize TCP Proxy module. More...
 
void ksnTCPProxyDestroy (ksnTCPProxyClass *tp)
 Destroy TCP Proxy module. More...
 
ssize_t teo_recvfrom (ksnetEvMgrClass *ke, int fd, void *buffer, size_t buffer_len, int flags, __SOCKADDR_ARG addr, socklen_t *__restrict addr_len)
 Receive packet from UDP or TCP Proxy buffer. More...
 
ssize_t teo_sendto (ksnetEvMgrClass *ke, int fd, const void *buffer, size_t buffer_len, int flags, __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
 Send UDP packet by UDP or TCP Proxy. More...
 
int ksnTCPProxyPackageProcess (ksnTCPProxyPacketData *packet, void *data, size_t data_length)
 Process TCP proxy package. More...
 
void cmd_tcppc_read_cb (struct ev_loop *loop, struct ev_io *w, int revents)
 TCP Proxy client callback. More...
 
int ksnTCPProxyClientConnect (ksnTCPProxyClass *tp)
 Connect to TCP Proxy server. More...
 
void cmd_udpp_read_cb (struct ev_loop *loop, struct ev_io *w, int revents)
 UDP client/server Proxy callback. More...
 
void cmd_tcpp_read_cb (struct ev_loop *loop, struct ev_io *w, int revents)
 TCP Proxy server client callback. More...
 
void cmd_tcpp_accept_cb (struct ev_loop *loop, struct ev_ksnet_io *w, int revents, int fd)
 TCP Proxy server accept callback. More...
 

Detailed Description

Author
Kirill Scherba kiril.nosp@m.l@sc.nosp@m.herba.nosp@m..ru

Teonet TCP Proxy module

See example:

See test: test_tcp_proxy.c

Created on September 8, 2015, 1:59 AM

Description:

Macro Definition Documentation

#define TCP_PROXY_VERSION   0

TCP Proxy version.

Referenced by ksnTCPProxyPackageCreate().

Enumeration Type Documentation

Enumerator
CLIENT 
SERVER 

Function Documentation

void _cmd_tcpp_read_cb ( struct ev_loop *  loop,
struct ev_io *  w,
int  revents,
int  cli_ser 
)
void cmd_tcpp_accept_cb ( struct ev_loop *  loop,
struct ev_ksnet_io w,
int  revents,
int  fd 
)

TCP Proxy server accept callback.

Register client, create UDP Proxy client/server, create event watchers with clients callback

Parameters
loopEvent manager loop
wPointer to watcher
reventsEvents
fdFile description of created client connection

References ev_ksnet_io::data, and ksnTCPProxyServerClientConnect().

Referenced by ksnTCPProxyServerStart().

void cmd_tcpp_read_cb ( struct ev_loop *  loop,
struct ev_io *  w,
int  revents 
)

TCP Proxy server client callback.

Get packet from TCP Proxy server client connection and resend it to UDP Proxy

Parameters
loopEvent manager loop
wPointer to watcher
reventsEvents

References _cmd_tcpp_read_cb(), and SERVER.

Referenced by ksnTCPProxyServerClientConnect().

void cmd_tcppc_read_cb ( struct ev_loop *  loop,
struct ev_io *  w,
int  revents 
)

TCP Proxy client callback.

Get packet from TCP Proxy server connection and resend it to read host callback

Parameters
loopEvent manager loop
wPointer to watcher
reventsEvents

References _cmd_tcpp_read_cb(), and CLIENT.

Referenced by ksnTCPProxyClientConnect().

void cmd_udpp_read_cb ( struct ev_loop *  loop,
struct ev_io *  w,
int  revents 
)

UDP client/server Proxy callback.

Get packet from UDP Proxy client connection and resend it to TCP Proxy

Parameters
loopEvent manager loop
wPointer to watcher
reventsEvents

References CMD_TCPP_PROXY, data, DEBUG, DEBUG_VV, kev, KSN_BUFFER_DB_SIZE, ksn_printf, ksnTCPProxyPackageCreate(), ksnTCPProxyClass::map, MODULE, and ksnTCPProxyData::tcp_proxy_fd.

Referenced by ksnTCPProxyServerClientConnect().

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 ksnTCPProxyClientConnect ( ksnTCPProxyClass tp)

Connect to TCP Proxy server.

Get address and port from teonet configuration and connect to R-Host TCP Server

Parameters
tpPointer to ksnTCPProxyClass
Returns
0 - Successfully connected

References ksnTCPProxyPacketData::buffer, cmd_tcppc_read_cb(), DEBUG, ksnTCPProxyClass::fd_client, ksnTCPProxyPacketData::header, kev, ksn_printf, ksnTcpClientCreate(), ksnTCPProxyPacketData::length, MODULE, ksnTCPProxyClass::packet, ksnTCPProxyPacketData::ptr, ksnTCPProxyPacketData::stage, ksnTCPProxyClass::w_client, and WAIT_FOR_START.

Referenced by connect_r_host_cb(), and ksnTCPProxyInit().

void ksnTCPProxyClientStop ( ksnTCPProxyClass tp)

Stop TCP Proxy client.

Stop TCP Proxy client watcher and close fd connection

Parameters
tpPointer to ksnTCPProxyClass

References ksnTCPProxyClass::fd_client, kev, ksnetArpRemoveAll(), and ksnTCPProxyClass::w_client.

Referenced by _cmd_tcpp_read_cb(), and ksnTCPProxyDestroy().

void ksnTCPProxyDestroy ( ksnTCPProxyClass tp)

Destroy TCP Proxy module.

Parameters
tpPointer to ksnTCPProxyClass

References free(), ksnTCPProxyClientStop(), ksnTCPProxyServerStop(), and ksnTCPProxyClass::map.

Referenced by modules_destroy(), test_5_1(), and test_5_2().

ksnTCPProxyClass* ksnTCPProxyInit ( void *  ke)
size_t ksnTCPProxyPackageCreate ( void *  buffer,
size_t  buffer_length,
const char *  addr,
int  port,
int  cmd,
const void *  data,
size_t  data_length 
)

Create TCP Proxy package.

Create TCP Proxy package from peers UDP address and port, data buffer and its length

Parameters
bufferThe buffer to create package in
buffer_lengthPackage data length
addrString with peer UDP address
portUDP port number
cmdTCP Proxy protocol command
dataPackage data
data_lengthPackage data length
Returns
Return size of created package or error
Return values
>0 - size of created package
-1- error: The output buffer less than packet header
-2- error: The output buffer less than packet header + data

References ksnTCPProxyHeader::addr_length, ksnTCPProxyHeader::checksum, ksnTCPProxyHeader::command, get_byte_checksum(), ksnTCPProxyHeader::packet_checksum, ksnTCPProxyHeader::packet_length, ksnTCPProxyHeader::port, TCP_PROXY_VERSION, and ksnTCPProxyHeader::version.

Referenced by cmd_udpp_read_cb(), ksnTCPProxySendTo(), and test_5_2().

int ksnTCPProxyPackageProcess ( ksnTCPProxyPacketData packet,
void *  data,
size_t  data_length 
)

Process TCP proxy package.

Read tcp data from input buffer to tp->buffer until end of tcp proxy package, check checksum, take UDP address, port number and UDP package data.

Parameters
packetPointer to ksnTCPProxyPacketData
dataPointer to received TCP data
data_lengthTCP data length
Returns
Length of received packet, zero or error code. The TCP packets may be received combined to one big buffer. If packet processed and this function return value grate than 0 and less than input buffer size we need run this function again with data_length = 0 to process next part of input buffer.
(see the "5) Check receiving" code of the test_5_2() function of the test_tcp_proxy.c test)
Return values
>0- receiving done, the return value contain length of packet, the packet saved to ksnTCPProxyClass::buffer
0- continue reading current packet
-1- wrong process package stage
-2- wrong packet header checksum
-3- wrong packet checksum

References ksnTCPProxyHeader::addr_length, ksnTCPProxyPacketData::buffer, ksnTCPProxyHeader::checksum, get_byte_checksum(), ksnTCPProxyPacketData::header, ksnTCPProxyPackageProcess(), ksnTCPProxyPacketData::length, ksnTCPProxyHeader::packet_checksum, ksnTCPProxyHeader::packet_length, ksnTCPProxyHeader::port, PROCESS_PACKET, ksnTCPProxyPacketData::ptr, ksnTCPProxyPacketData::stage, WAIT_FOR_END, and WAIT_FOR_START.

Referenced by _cmd_tcpp_read_cb(), ksnTCPProxyPackageProcess(), and test_5_2().

ssize_t ksnTCPProxySendTo ( ksnetEvMgrClass ke,
int  cmd,
const void *  buffer,
size_t  buffer_len,
__CONST_SOCKADDR_ARG  addr 
)

Send command to TCP Proxy.

Parameters
kePointer to ksnetEvMgrClass
cmdTCP Proxy command
buffer
buffer_len
addr
Returns

References ksnTCPProxyClass::fd_client, KSN_BUFFER_DB_SIZE, ksnTCPProxyPackageCreate(), and ksnetEvMgrClass::tp.

Referenced by teo_sendto().

void ksnTCPProxyServerClientConnect ( ksnTCPProxyClass tp,
int  fd 
)
void ksnTCPProxyServerClientDisconnect ( ksnTCPProxyClass tp,
int  fd,
int  remove_f 
)

Disconnect TCP Proxy Server client.

Called when client disconnected or when the TCP Proxy server closing. Close UDP and TCP connections, remove data from TCP Proxy clients map.

Parameters
tpPointer to ksnTCPProxyClass
fdTCP client connection file descriptor
remove_fIf true than remove disconnected record from map

References CONNECT, kev, ksn_printf, ksnTCPProxyClass::map, MODULE, pblMapRemoveFree(), ksnTCPProxyData::udp_proxy_fd, ksnTCPProxyData::w, and ksnTCPProxyData::w_udp.

Referenced by _cmd_tcpp_read_cb(), and ksnTCPProxyServerStop().

int ksnTCPProxyServerStart ( ksnTCPProxyClass tp)

Start TCP Proxy server.

Create and start TCP Proxy server

Parameters
tpPointer to ksnTCPProxyClass
Returns
If return value > 0 than server was created successfully

References cmd_tcpp_accept_cb(), fd, ksnTCPProxyClass::fd, kev, ksn_printf, ksnTcpServerCreate(), MESSAGE, and MODULE.

Referenced by ksnTCPProxyInit().

void ksnTCPProxyServerStop ( ksnTCPProxyClass tp)

Stop TCP Proxy server.

Disconnect all connected clients and and stop TCP Proxy server

Parameters
tpPointer to ksnTCPProxyClass

References fd, ksnTCPProxyClass::fd, kev, ksnTCPProxyServerClientDisconnect(), ksnTcpServerStop(), and ksnTCPProxyClass::map.

Referenced by ksnTCPProxyDestroy().

int send_cmd_disconnect_cb ( ksnetArpClass ka,
char *  name,
ksnet_arp_data *  arp_data,
void *  data 
)
ssize_t teo_recvfrom ( ksnetEvMgrClass ke,
int  fd,
void *  buffer,
size_t  buffer_len,
int  flags,
__SOCKADDR_ARG  addr,
socklen_t *__restrict  addr_len 
)
ssize_t teo_sendto ( ksnetEvMgrClass ke,
int  fd,
const void *  buffer,
size_t  buffer_len,
int  flags,
__CONST_SOCKADDR_ARG  addr,
socklen_t  addr_len 
)

Send UDP packet by UDP or TCP Proxy.

Parameters
kePointer to ksnetEvMgrClass
fd
buffer
buffer_len
flags
addr
addr_len
Returns

References addr_port::addr, addr_port_free(), CMD_TCPP_PROXY, DEBUG_VV, ksnTCPProxyClass::fd_client, ksn_printf, ksnTCPProxySendTo(), MODULE, addr_port::port, teonet_cfg::r_tcp_f, ksnetEvMgrClass::teo_cfg, ksnetEvMgrClass::tp, and wrap_inet_ntop().

Referenced by trudp_event_cb().