diff options
Diffstat (limited to 'access/src/udp_server.cpp')
| -rw-r--r-- | access/src/udp_server.cpp | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/access/src/udp_server.cpp b/access/src/udp_server.cpp index bcf8547..c3bacc8 100644 --- a/access/src/udp_server.cpp +++ b/access/src/udp_server.cpp @@ -1,7 +1,8 @@ #include "mgw_utils.h" #include "mgw_socket.h" #include "udp_server.h" -#include "nat.h" +#include "snat.h" +#include "dnat.h" #include "mgw_tun.h" void * thread_udp_server(void *args) @@ -9,7 +10,8 @@ void * thread_udp_server(void *args) struct udp_server_args *_args = (struct udp_server_args *)args; void *logger = _args->logger; const char *profile = _args->profile; - struct nat_handle *_nat_handle = _args->_nat_handle; + struct snat_handle *_snat_handle = _args->_snat_handle; + struct dnat_handle *_dnat_handle = _args->_dnat_handle; struct mgw_tun_handle *tun_handle = _args->tun_handle; struct field_stat_handle *fs_handle = _args->fs_handle; FREE(&args); @@ -32,7 +34,7 @@ void * thread_udp_server(void *args) exit(EXIT_FAILURE); } char buff[MGW_PACKET_MAX]; - struct timespec start_time, end_time; + struct timespec start_time; while(1) { //EAGAIN if socket is no-blocking, eg. fcntl() @@ -43,18 +45,40 @@ void * thread_udp_server(void *args) MGW_LOG_ERROR(logger, "mgw_socket: Failed at recv udp data, errno is %d, %s", errno, strerror(errno)); continue; } - FS_operate(fs_handle->handle, fs_handle->field_recv_from_mrl, 0, FS_OP_ADD, 1); + FS_operate(fs_handle->handle, fs_handle->field_rx_from_mrl, 0, FS_OP_ADD, 1); MGW_LOG_INFO(logger, "mgw_socket: Succeed to recv udp data, len is %d", len); - int rtn = nat_dest_convert(_nat_handle, buff, len); - if(rtn == NAT_COVERT_FAILURE) + clock_gettime(CLOCK_MONOTONIC, &start_time); + int rtn = snat_rx_convert(_snat_handle, buff, len); + mgw_utils_fs_latency_cala(fs_handle->handle, start_time, fs_handle->snat_rx_latency); + if(rtn == MGW_DROP) + { + continue; + } + if(rtn == MGW_FORWORD) + { + int rtn = mgw_tun_write(tun_handle, buff, len); + if(rtn >0 && rtn == len) + { + FS_operate(fs_handle->handle, fs_handle->field_tun_write, 0, FS_OP_ADD, 1); + } + continue; + } + //MGW_BYPASS, dnat_rx_convert + clock_gettime(CLOCK_MONOTONIC, &start_time); + rtn = dnat_rx_convert(_dnat_handle, buff, len); + mgw_utils_fs_latency_cala(fs_handle->handle, start_time, fs_handle->dnat_rx_latency); + if(rtn == MGW_DROP) + { + continue; + } + if(rtn == MGW_FORWORD) { + int rtn = mgw_tun_write(tun_handle, buff, len); + if(rtn >0 && rtn == len) + { + FS_operate(fs_handle->handle, fs_handle->field_tun_write, 0, FS_OP_ADD, 1); + } continue; } - //write to tun - mgw_tun_write(tun_handle, buff, len); - FS_operate(fs_handle->handle, fs_handle->field_write_to_tun, 0, FS_OP_ADD, 1); - clock_gettime(CLOCK_MONOTONIC, &end_time); - long long cost_time = (end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_nsec - start_time.tv_nsec) / 1000; - FS_operate(fs_handle->handle, fs_handle->dnat_latency, 0, FS_OP_SET, cost_time); } }
\ No newline at end of file |
