Teonet library
0.4.7
|
#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... | |
ksnTCPProxyClass * | ksnTCPProxyInit (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... | |
See example:
See test: test_tcp_proxy.c
Created on September 8, 2015, 1:59 AM
#define kev ((ksnetEvMgrClass*)tp->ke) |
#define MODULE _ANSI_YELLOW "tcp_proxy" _ANSI_NONE |
#define TCP_PROXY_VERSION 0 |
TCP Proxy version.
Referenced by ksnTCPProxyPackageCreate().
enum ksnTCPProxyCliSer |
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.
Get packet from TCP Proxy server client connection and resend it to UDP Proxy
loop | Event manager loop |
w | Pointer to watcher |
revents | Events |
cli_ser |
References ksnTCPProxyHeader::addr_length, ksnTCPProxyPacketData::buffer, CMD_TCPP_PROXY, ksnTCPProxyHeader::command, data, DEBUG, DEBUG_VV, ksnTCPProxyClass::fd, ksnTCPProxyPacketData::header, host_cb(), kev, KSN_BUFFER_SIZE, ksn_printf, ksnTCPProxyClientStop(), ksnTCPProxyPackageProcess(), ksnTCPProxyServerClientDisconnect(), make_addr, ksnTCPProxyClass::map, MODULE, ksnTCPProxyData::packet, ksnTCPProxyClass::packet, ksnTCPProxyHeader::packet_length, ksnTCPProxyHeader::port, ksnTCPProxyPacketData::ptr, SERVER, and ksnTCPProxyData::udp_proxy_fd.
Referenced by cmd_tcpp_read_cb(), and cmd_tcppc_read_cb().
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
loop | Event manager loop |
w | Pointer to watcher |
revents | Events |
fd | File 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
loop | Event manager loop |
w | Pointer to watcher |
revents | Events |
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
loop | Event manager loop |
w | Pointer to watcher |
revents | Events |
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
loop | Event manager loop |
w | Pointer to watcher |
revents | Events |
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
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
tp | Pointer to ksnTCPProxyClass |
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
tp | Pointer 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.
tp | Pointer to ksnTCPProxyClass |
References free(), ksnTCPProxyClientStop(), ksnTCPProxyServerStop(), and ksnTCPProxyClass::map.
Referenced by modules_destroy(), test_5_1(), and test_5_2().
ksnTCPProxyClass* ksnTCPProxyInit | ( | void * | ke | ) |
Initialize TCP Proxy module.
ke | Pointer to the ksnTCPProxyClass |
References ksnTCPProxyClass::fd, ksnTCPProxyClass::fd_client, ke, ksnTCPProxyClass::ke, ksnTCPProxyClientConnect(), ksnTCPProxyServerStart(), malloc(), and ksnTCPProxyClass::map.
Referenced by modules_init(), test_5_1(), and test_5_2().
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
buffer | The buffer to create package in |
buffer_length | Package data length |
addr | String with peer UDP address |
port | UDP port number |
cmd | TCP Proxy protocol command |
data | Package data |
data_length | Package data length |
> | 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.
packet | Pointer to ksnTCPProxyPacketData |
data | Pointer to received TCP data |
data_length | TCP data length |
>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.
ke | Pointer to ksnetEvMgrClass |
cmd | TCP Proxy command |
buffer | |
buffer_len | |
addr |
References ksnTCPProxyClass::fd_client, KSN_BUFFER_DB_SIZE, ksnTCPProxyPackageCreate(), and ksnetEvMgrClass::tp.
Referenced by teo_sendto().
void ksnTCPProxyServerClientConnect | ( | ksnTCPProxyClass * | tp, |
int | fd | ||
) |
Connect TCP Proxy Server client.
Called when tcp client connected to TCP Proxy Server. Create UDP Proxy and register this client in TCP Proxy map
tp | Pointer to ksnTCPProxyClass |
fd | TCP client connection file descriptor |
References ksnTCPProxyPacketData::buffer, cmd_tcpp_read_cb(), cmd_udpp_read_cb(), CONNECT, data, fd, ksnTCPProxyPacketData::header, kev, ksn_printf, ksnCoreBindRaw(), ksnTCPProxyPacketData::length, ksnTCPProxyClass::map, MODULE, ksnTCPProxyData::packet, ksnTCPProxyPacketData::ptr, ksnTCPProxyPacketData::stage, ksnTCPProxyData::tcp_proxy_fd, ksnTCPProxyData::udp_proxy_fd, ksnTCPProxyData::udp_proxy_port, ksnTCPProxyData::w, ksnTCPProxyData::w_udp, and WAIT_FOR_START.
Referenced by cmd_tcpp_accept_cb().
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.
tp | Pointer to ksnTCPProxyClass |
fd | TCP client connection file descriptor |
remove_f | If 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
tp | Pointer to ksnTCPProxyClass |
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
tp | Pointer 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 | ||
) |
Receive packet from UDP or TCP Proxy buffer.
ke | Pointer to ksnetEvMgrClass |
fd | |
buffer | |
buffer_len | |
flags | |
addr | |
addr_len |
>0 | - Success |
-2 | - Buffer too small error |
-3 | - Wrong address error |
References addr_port::addr, ksnTCPProxyHeader::addr_length, addr_port_free(), ksnTCPProxyPacketData::buffer, DEBUG_VV, ksnTCPProxyClass::fd_client, ksnTCPProxyPacketData::header, kEventMgrStopped, ksn_printf, ksnetEvMgrStatus(), make_addr, MODULE, ksnTCPProxyClass::packet, ksnTCPProxyHeader::packet_length, addr_port::port, ksnTCPProxyHeader::port, teonet_cfg::r_tcp_f, ksnetEvMgrClass::teo_cfg, ksnetEvMgrClass::tp, and wrap_inet_ntop().
Referenced by trudp_process_receive().
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.
ke | Pointer to ksnetEvMgrClass |
fd | |
buffer | |
buffer_len | |
flags | |
addr | |
addr_len |
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().