From 44e9f5cb8b6bdc69b8e0ee5923f8f7adfc06c7a6 Mon Sep 17 00:00:00 2001 From: wangyu- Date: Mon, 25 Sep 2017 10:38:39 -0500 Subject: [PATCH] just commit --- classic.cpp | 1274 ---------------------------------------------- classic.h | 18 - common.cpp | 50 +- common.h | 9 +- conn_manager.cpp | 135 ----- conn_manager.h | 52 -- main.cpp | 1025 +++++++++++++++---------------------- makefile | 2 +- packet.cpp | 66 ++- packet.h | 28 +- 10 files changed, 535 insertions(+), 2124 deletions(-) delete mode 100644 classic.cpp delete mode 100644 classic.h delete mode 100644 conn_manager.cpp delete mode 100644 conn_manager.h diff --git a/classic.cpp b/classic.cpp deleted file mode 100644 index bea6fde..0000000 --- a/classic.cpp +++ /dev/null @@ -1,1274 +0,0 @@ -/* - * classic.cpp - * - * Created on: Sep 15, 2017 - * Author: root - */ - - - -#include "common.h" -#include "log.h" -#include "git_version.h" - -namespace classic -{ - -using namespace std; - -typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64 -typedef long long i64_t; - -typedef unsigned int u32_t; -typedef int i32_t; - -typedef u64_t anti_replay_seq_t; -int disable_replay_filter=0; -int dup_num=1; -int dup_delay_min=20; //0.1ms -int dup_delay_max=20; -//int dup_first_delay=9000; //0.1ms - -int jitter_min=0; -int jitter_max=0; - -int iv_min=2; -int iv_max=16;//< 256; -int random_number_fd=-1; - -int remote_fd=-1; -int local_fd=-1; -int is_client = 0, is_server = 0; -int local_listen_fd=-1; - -int disable_conv_clear=0; -int mtu_warn=1350; -u32_t remote_address_uint32=0; - -char local_address[100], remote_address[100]; -int local_port = -1, remote_port = -1; -int multi_process_mode=0; -const u32_t anti_replay_buff_size=10000; - -char key_string[1000]= "secret key"; - -int random_drop=0; - -u64_t last_report_time=0; -int report_interval=0; - -u64_t packet_send_count=0; -u64_t dup_packet_send_count=0; -u64_t packet_recv_count=0; -u64_t dup_packet_recv_count=0; -int max_pending_packet=0; - -int random_between(u32_t a,u32_t b) -{ - if(a>b) - { - mylog(log_fatal,"min >max?? %d %d\n",a ,b); - myexit(1); - } - if(a==b)return a; - else return a+get_true_random_number()%(b+1-a); -} - - -int VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV; - -struct anti_replay_t -{ - u64_t max_packet_received; - - u64_t replay_buffer[anti_replay_buff_size]; - unordered_set st; - u32_t const_id; - u32_t anti_replay_seq; - int index; - anti_replay_seq_t get_new_seq_for_send() - { - anti_replay_seq_t res=const_id; - res<<=32u; - anti_replay_seq++; - res|=anti_replay_seq; - return res; - } - void prepare() - { - anti_replay_seq=get_true_random_number();//random first seq - const_id=get_true_random_number_nz(); - } - anti_replay_t() - { - memset(replay_buffer,0,sizeof(replay_buffer)); - st.rehash(anti_replay_buff_size*10); - max_packet_received=0; - index=0; - } - - int is_vaild(u64_t seq) - { - //if(disable_replay_filter) return 1; - if(seq==0) - { - mylog(log_debug,"seq=0\n"); - return 0; - } - if(st.find(seq)!=st.end() ) - { - mylog(log_trace,"seq %llx exist\n",seq); - return 0; - } - - if(replay_buffer[index]!=0) - { - assert(st.find(replay_buffer[index])!=st.end()); - st.erase(replay_buffer[index]); - } - replay_buffer[index]=seq; - st.insert(seq); - index++; - if(index==int(anti_replay_buff_size)) index=0; - - return 1; //for complier check - } -}anti_replay; -struct conn_manager_t //TODO change map to unordered map -{ - //typedef hash_map map; - unordered_map u64_to_fd; //conv and u64 are both supposed to be uniq - unordered_map fd_to_u64; - - unordered_map fd_last_active_time; - - unordered_map::iterator clear_it; - - unordered_map::iterator it; - unordered_map::iterator old_it; - - //void (*clear_function)(uint64_t u64) ; - - long long last_clear_time; - list clear_list; - conn_manager_t() - { - clear_it=fd_last_active_time.begin(); - long long last_clear_time=0; - rehash(); - //clear_function=0; - } - ~conn_manager_t() - { - clear(); - } - int get_size() - { - return fd_to_u64.size(); - } - void rehash() - { - u64_to_fd.rehash(10007); - fd_to_u64.rehash(10007); - fd_last_active_time.rehash(10007); - } - void clear() - { - if(disable_conv_clear) return ; - - for(it=fd_to_u64.begin();it!=fd_to_u64.end();it++) - { - //int fd=int((it->second<<32u)>>32u); - close( it->first); - } - u64_to_fd.clear(); - fd_to_u64.clear(); - fd_last_active_time.clear(); - - clear_it=fd_last_active_time.begin(); - - } - int exist_fd(u32_t fd) - { - return fd_to_u64.find(fd)!=fd_to_u64.end(); - } - int exist_u64(u64_t u64) - { - return u64_to_fd.find(u64)!=u64_to_fd.end(); - } - u32_t find_fd_by_u64(u64_t u64) - { - return u64_to_fd[u64]; - } - u64_t find_u64_by_fd(u32_t fd) - { - return fd_to_u64[fd]; - } - int update_active_time(u32_t fd) - { - return fd_last_active_time[fd]=get_current_time(); - } - int insert_fd(u32_t fd,u64_t u64) - { - u64_to_fd[u64]=fd; - fd_to_u64[fd]=u64; - fd_last_active_time[fd]=get_current_time(); - return 0; - } - int erase_fd(u32_t fd) - { - if(disable_conv_clear) return 0; - u64_t u64=fd_to_u64[fd]; - - u32_t ip= (u64 >> 32u); - - int port= uint16_t((u64 << 32u) >> 32u); - - mylog(log_info,"fd %d cleared,assocated adress %s,%d\n",fd,my_ntoa(ip),port); - - close(fd); - - fd_to_u64.erase(fd); - u64_to_fd.erase(u64); - fd_last_active_time.erase(fd); - return 0; - } - void check_clear_list() - { - while(!clear_list.empty()) - { - int fd=*clear_list.begin(); - clear_list.pop_front(); - erase_fd(fd); - } - } - int clear_inactive() - { - if(get_current_time()-last_clear_time>conv_clear_interval) - { - last_clear_time=get_current_time(); - return clear_inactive0(); - } - return 0; - } - int clear_inactive0() - { - if(disable_conv_clear) return 0; - - - //map::iterator it; - int cnt=0; - it=clear_it; - int size=fd_last_active_time.size(); - int num_to_clean=size/conv_clear_ratio+conv_clear_min; //clear 1/10 each time,to avoid latency glitch - - u64_t current_time=get_current_time(); - for(;;) - { - if(cnt>=num_to_clean) break; - if(fd_last_active_time.begin()==fd_last_active_time.end()) break; - - if(it==fd_last_active_time.end()) - { - it=fd_last_active_time.begin(); - } - - if( current_time -it->second >conv_timeout ) - { - //mylog(log_info,"inactive conv %u cleared \n",it->first); - old_it=it; - it++; - u32_t fd= old_it->first; - erase_fd(old_it->first); - - - } - else - { - it++; - } - cnt++; - } - return 0; - } -}conn_manager; - -typedef u64_t my_time_t; - -struct delay_data -{ - int fd; - int times_left; - char * data; - int len; - u64_t u64; -}; -int delay_timer_fd; - -int sendto_u64 (int fd,char * buf, int len,int flags, u64_t u64) -{ - - if(is_server) - { - dup_packet_send_count++; - } - if(is_server&&random_drop!=0) - { - if(get_true_random_number()%10000<(u32_t)random_drop) - { - return 0; - } - } - - sockaddr_in tmp_sockaddr; - - memset(&tmp_sockaddr,0,sizeof(tmp_sockaddr)); - tmp_sockaddr.sin_family = AF_INET; - tmp_sockaddr.sin_addr.s_addr = (u64 >> 32u); - - tmp_sockaddr.sin_port = htons(uint16_t((u64 << 32u) >> 32u)); - - return sendto(fd, buf, - len , 0, - (struct sockaddr *) &tmp_sockaddr, - sizeof(tmp_sockaddr)); -} - -int send_fd (int fd,char * buf, int len,int flags) -{ - if(is_client) - { - dup_packet_send_count++; - } - if(is_client&&random_drop!=0) - { - if(get_true_random_number()%10000<(u32_t)random_drop) - { - return 0; - } - } - return send(fd,buf,len,flags); -} - -multimap delay_mp; - -int add_to_delay_mp(int fd,int times_left,u32_t delay,char * buf,int len,u64_t u64) -{ - if(max_pending_packet!=0&&int(delay_mp.size()) >=max_pending_packet) - { - mylog(log_warn,"max pending packet reached,ignored\n"); - return 0; - } - delay_data tmp; - tmp.data = buf; - tmp.fd = fd; - tmp.times_left = times_left; - tmp.len = len; - tmp.u64=u64; - my_time_t tmp_time=get_current_time_us(); - tmp_time+=delay*100; - delay_mp.insert(make_pair(tmp_time,tmp)); - - return 0; -} -int add_and_new(int fd,int times_left,u32_t delay,char * buf,int len,u64_t u64) -{ - if(times_left<=0) return -1; - - char * str= (char *)malloc(len); - memcpy(str,buf,len); - add_to_delay_mp(fd,times_left,delay,str,len,u64); - return 0; -} - -multimap new_delay_mp; - - - -void handler(int num) { - int status; - int pid; - while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { - if (WIFEXITED(status)) { - //printf("The child exit with code %d",WEXITSTATUS(status)); - } - } - -} -void encrypt_0(char * input,int &len,char *key) -{ - int i,j; - if(key[0]==0) return; - for(i=0,j=0;i 65535||in_len<0) - return -1; - int iv_len=iv_min+rand()%(iv_max-iv_min); - get_true_random_chars(output,iv_len); - memcpy(output+iv_len,input,in_len); - - output[iv_len+in_len]=(uint8_t)iv_len; - - output[iv_len+in_len]^=output[0]; - output[iv_len+in_len]^=key_string[0]; - - for(i=0,j=0,k=1;i 65535||in_len<0) - { - mylog(log_debug,"in_len > 65535||in_len<0 , %d",in_len); - return -1; - } - int iv_len= int ((uint8_t)(input[in_len-1]^input[0]^key_string[0]) ); - out_len=in_len-1-iv_len; - if(out_len<0) - { - mylog(log_debug,"%d %d\n",in_len,out_len); - return -1; - } - for(i=0,j=0,k=1;i::iterator it; - while(1) - { - int ret=0; - it=delay_mp.begin(); - if(it==delay_mp.end()) break; - - current_time=get_current_time_us(); - if(it->first < current_time||it->first ==current_time) - { - if (is_client) { - if (conn_manager.exist_fd(it->second.fd)) { - u64_t u64 = conn_manager.find_u64_by_fd(it->second.fd); - if (u64 != it->second.u64) { - it->second.times_left = 0; //fd has been deleted and recreated - // 偷懒的做法 - } else { - char new_data[buf_len]; - int new_len = 0; - do_obscure(it->second.data, it->second.len, - new_data, new_len); - ret = send_fd(it->second.fd, new_data, new_len, 0); - } - } else { - it->second.times_left = 0; - } - } else { - - if (conn_manager.exist_fd(it->second.fd)) { - u64_t u64 = conn_manager.find_u64_by_fd(it->second.fd); - if (u64 != it->second.u64) { - it->second.times_left = 0;//fd has been deleted and recreated - // 偷懒的做法 - } else { - char new_data[buf_len]; - int new_len = 0; - do_obscure(it->second.data, it->second.len, - new_data, new_len); - sendto_u64(local_listen_fd, new_data, new_len, 0, - u64); - } - } else { - it->second.times_left = 0; - } - } - if (ret < 0) { - mylog(log_debug, "send return %d at @300", ret); - } - - - if(it->second.times_left>1) - { - //delay_mp.insert(pair(current_time)); - add_to_delay_mp(it->second.fd,it->second.times_left-1,random_between(dup_delay_min,dup_delay_max),it->second.data,it->second.len,it->second.u64); - } - else - { - free(it->second.data); - } - delay_mp.erase(it); - } - else - { - break; - } - - } - if(!delay_mp.empty()) - { - itimerspec its; - memset(&its.it_interval,0,sizeof(its.it_interval)); - its.it_value.tv_sec=delay_mp.begin()->first/1000000llu; - its.it_value.tv_nsec=(delay_mp.begin()->first%1000000llu)*1000llu; - timerfd_settime(delay_timer_fd,TFD_TIMER_ABSTIME,&its,0); - } - } -} -int create_new_udp(int &new_udp_fd) -{ - struct sockaddr_in remote_addr_in; - - socklen_t slen = sizeof(sockaddr_in); - memset(&remote_addr_in, 0, sizeof(remote_addr_in)); - remote_addr_in.sin_family = AF_INET; - remote_addr_in.sin_port = htons(remote_port); - remote_addr_in.sin_addr.s_addr = remote_address_uint32; - - new_udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (new_udp_fd < 0) { - mylog(log_warn, "create udp_fd error\n"); - return -1; - } - setnonblocking(new_udp_fd); - set_buf_size(new_udp_fd); - - mylog(log_debug, "created new udp_fd %d\n", new_udp_fd); - int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen); - if (ret != 0) { - mylog(log_warn, "udp fd connect fail %d %s\n",ret,strerror(errno)); - close(new_udp_fd); - return -1; - } - - - return 0; -} -int set_timer(int epollfd,int &timer_fd) -{ - int ret; - epoll_event ev; - - itimerspec its; - memset(&its,0,sizeof(its)); - - if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0) - { - mylog(log_fatal,"timer_fd create error\n"); - myexit(1); - } - its.it_interval.tv_sec=(timer_interval/1000); - its.it_interval.tv_nsec=(timer_interval%1000)*1000ll*1000ll; - its.it_value.tv_nsec=1; //imidiately - timerfd_settime(timer_fd,0,&its,0); - - - ev.events = EPOLLIN; - ev.data.fd = timer_fd; - - ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev); - if (ret < 0) { - mylog(log_fatal,"epoll_ctl return %d\n", ret); - myexit(-1); - } - return 0; -} -int event_loop() -{ - struct sockaddr_in local_me, local_other; - local_listen_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - int yes = 1; - //setsockopt(local_listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); - set_buf_size(local_listen_fd,4*1024*1024); - setnonblocking(local_listen_fd); - - //char data[buf_len]; - //char *data=data0; - socklen_t slen = sizeof(sockaddr_in); - memset(&local_me, 0, sizeof(local_me)); - local_me.sin_family = AF_INET; - local_me.sin_port = htons(local_port); - local_me.sin_addr.s_addr = inet_addr(local_address); - if (bind(local_listen_fd, (struct sockaddr*) &local_me, slen) == -1) - { - mylog(log_fatal,"socket bind error"); - myexit(1); - } - - int epollfd = epoll_create1(0); - const int max_events = 4096; - struct epoll_event ev, events[max_events]; - if (epollfd < 0) - { - mylog(log_fatal,"epoll created return %d\n", epollfd); - myexit(-1); - } - ev.events = EPOLLIN; - ev.data.fd = local_listen_fd; - int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, local_listen_fd, &ev); - - if(ret!=0) - { - mylog(log_fatal,"epoll created return %d\n", epollfd); - myexit(-1); - } - int clear_timer_fd=-1; - set_timer(epollfd,clear_timer_fd); - - - - if ((delay_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0) - { - mylog(log_fatal,"timer_fd create error"); - myexit(1); - } - ev.events = EPOLLIN; - ev.data.fd = delay_timer_fd; - - itimerspec zero_its; - memset(&zero_its, 0, sizeof(zero_its)); - - timerfd_settime(delay_timer_fd, TFD_TIMER_ABSTIME, &zero_its, 0); - epoll_ctl(epollfd, EPOLL_CTL_ADD, delay_timer_fd, &ev); - if (ret < 0) - { - mylog(log_fatal,"epoll_ctl return %d\n", ret); - myexit(-1); - } - - for (;;) - { - int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000); //3mins - if (nfds < 0) - { - mylog(log_fatal,"epoll_wait return %d\n", nfds); - myexit(-1); - } - int n; - int clear_triggered=0; - for (n = 0; n < nfds; ++n) - { - if (events[n].data.fd == local_listen_fd) //data income from local end - { - - char data[buf_len]; - int data_len; - - slen = sizeof(sockaddr_in); - if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0, - (struct sockaddr *) &local_other, &slen)) == -1) //<--first packet from a new ip:port turple - { - - mylog(log_error,"recv_from error,errno %s,this shouldnt happen,but lets try to pretend it didnt happen",strerror(errno)); - //myexit(1); - continue; - } - mylog(log_trace, "received data from listen fd,%s:%d, len=%d\n", my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port),data_len); - if(data_len>mtu_warn) - { - mylog(log_warn,"huge packet,data len=%d (>%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn); - } - data[data_len] = 0; //for easier debug - u64_t u64=pack_u64(local_other.sin_addr.s_addr,ntohs(local_other.sin_port)); - - if(!conn_manager.exist_u64(u64)) - { - - if(int(conn_manager.fd_to_u64.size())>=max_conv_num) - { - mylog(log_info,"new connection from %s:%d ,but ignored,bc of max_conv_num reached\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port)); - continue; - } - int new_udp_fd; - if(create_new_udp(new_udp_fd)!=0) - { - mylog(log_info,"new connection from %s:%d ,but create udp fd failed\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port)); - continue; - } - struct epoll_event ev; - - mylog(log_trace, "u64: %lld\n", u64); - ev.events = EPOLLIN; - - ev.data.fd = new_udp_fd; - - ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev); - if (ret != 0) { - mylog(log_info,"new connection from %s:%d ,but add to epoll failed\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port)); - close(new_udp_fd); - continue; - } - mylog(log_info,"new connection from %s:%d ,created new udp fd %d\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port),new_udp_fd); - conn_manager.insert_fd(new_udp_fd,u64); - } - int new_udp_fd=conn_manager.find_fd_by_u64(u64); - conn_manager.update_active_time(new_udp_fd); - int ret; - if(is_client) - { - add_seq(data,data_len); - if(jitter_max==0) - { - char new_data[buf_len]; - int new_len=0; - do_obscure(data, data_len, new_data, new_len); - ret = send_fd(new_udp_fd, new_data,new_len, 0); - if (ret < 0) { - mylog(log_warn, "send returned %d ,errno:%s\n", ret,strerror(errno)); - } - add_and_new(new_udp_fd, dup_num - 1,random_between(dup_delay_min,dup_delay_max), data, data_len,u64); - } - else - { - add_and_new(new_udp_fd, dup_num,random_between(jitter_min,jitter_max), data, data_len,u64); - } - packet_send_count++; - } - else - { - char new_data[buf_len]; - int new_len; - - if (de_obscure(data, data_len, new_data, new_len) != 0) { - mylog(log_trace,"de_obscure failed \n"); - continue; - } - //dup_packet_recv_count++; - if (remove_seq(new_data, new_len) != 0) { - mylog(log_trace,"remove_seq failed \n"); - continue; - } - //packet_recv_count++; - ret = send_fd(new_udp_fd, new_data,new_len, 0); - if (ret < 0) { - mylog(log_warn, "send returned %d,%s\n", ret,strerror(errno)); - //perror("what happened????"); - } - } - - - } - else if(events[n].data.fd == clear_timer_fd) - { - clear_triggered=1; - if(report_interval!=0 &&get_current_time()-last_report_time>u64_t(report_interval)*1000) - { - last_report_time=get_current_time(); - if(is_client) - mylog(log_info,"client-->server: %llu,%llu(include dup); server-->client %llu,%lld(include dup)\n",packet_send_count, - dup_packet_send_count,packet_recv_count,dup_packet_recv_count); - else - mylog(log_info,"client-->server: %llu,%llu(include dup); server-->client %llu,%lld(include dup)\n",packet_recv_count,dup_packet_recv_count,packet_send_count, - dup_packet_send_count); - } - } - else if (events[n].data.fd == delay_timer_fd) - { - uint64_t value; - read(delay_timer_fd, &value, 8); - //printf("",delay_mp.size()); - //fflush(stdout); - } - else - { - int udp_fd=events[n].data.fd; - if(!conn_manager.exist_fd(udp_fd)) continue; - - char data[buf_len]; - int data_len =recv(udp_fd,data,max_data_len,0); - mylog(log_trace, "received data from udp fd %d, len=%d\n", udp_fd,data_len); - if(data_len<0) - { - if(errno==ECONNREFUSED) - { - //conn_manager.clear_list.push_back(udp_fd); - mylog(log_debug, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,udp_fd,strerror(errno)); - } - - mylog(log_warn, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,udp_fd,strerror(errno)); - continue; - } - if(data_len>mtu_warn) - { - mylog(log_warn,"huge packet,data len=%d (>%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn); - } - - assert(conn_manager.exist_fd(udp_fd)); - - conn_manager.update_active_time(udp_fd); - - u64_t u64=conn_manager.find_u64_by_fd(udp_fd); - - if(is_client) - { - char new_data[buf_len]; - int new_len; - if (de_obscure(data, data_len, new_data, new_len) != 0) { - mylog(log_debug,"data_len=%d \n",data_len); - continue; - } - - //dup_packet_recv_count++; - if (remove_seq(new_data, new_len) != 0) { - mylog(log_debug,"remove_seq error \n"); - continue; - } - //packet_recv_count++; - ret = sendto_u64(local_listen_fd, new_data, - new_len , 0,u64); - if (ret < 0) { - mylog(log_warn, "sento returned %d,%s\n", ret,strerror(errno)); - //perror("ret<0"); - } - } - else - { - add_seq(data,data_len); - - if(jitter_max==0) - { - char new_data[buf_len]; - int new_len=0; - do_obscure(data, data_len, new_data, new_len); - ret = sendto_u64(local_listen_fd, new_data, - new_len , 0,u64); - add_and_new(udp_fd, dup_num - 1,random_between(dup_delay_min,dup_delay_max), data, data_len,u64); - if (ret < 0) { - mylog(log_warn, "sento returned %d,%s\n", ret,strerror(errno)); - //perror("ret<0"); - } - } - else - { - add_and_new(udp_fd, dup_num,random_between(jitter_min,jitter_max), data, data_len,u64); - } - packet_send_count++; - - - } - - //mylog(log_trace, "%s :%d\n", inet_ntoa(tmp_sockaddr.sin_addr), - // ntohs(tmp_sockaddr.sin_port)); - //mylog(log_trace, "%d byte sent\n", ret); - - } - } - check_delay_map(); - conn_manager.check_clear_list(); - if(clear_triggered) // 删除操作在epoll event的最后进行,防止event cache中的fd失效。 - { - u64_t value; - read(clear_timer_fd, &value, 8); - mylog(log_trace, "timer!\n"); - conn_manager.clear_inactive(); - } - } - myexit(0); - return 0; -} -void print_help() -{ - char git_version_buf[100]={0}; - strncpy(git_version_buf,gitversion,10); - - printf("UDPspeeder\n"); - printf("git version:%s ",git_version_buf); - printf("build date:%s %s\n",__DATE__,__TIME__); - printf("repository: https://github.com/wangyu-/UDPspeeder\n"); - printf("\n"); - printf("usage:\n"); - printf(" run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]\n"); - printf(" run as server : ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options]\n"); - printf("\n"); - printf("common option,must be same on both sides:\n"); - printf(" -k,--key key for simple xor encryption,default:\"secret key\"\n"); - - printf("main options:\n"); - printf(" -d duplicated packet number, -d 0 means no duplicate. default value:0\n"); - printf(" -t duplicated packet delay time, unit: 0.1ms,default value:20(2ms)\n"); - printf(" -j simulated jitter.randomly delay first packet for 0~jitter_value*0.1 ms,to\n"); - printf(" create simulated jitter.default value:0.do not use if you dont\n"); - printf(" know what it means\n"); - printf(" --report turn on udp send/recv report,and set a time interval for reporting,unit:s\n"); - printf("advanced options:\n"); - printf(" -t tmin:tmax simliar to -t above,but delay randomly between tmin and tmax\n"); - printf(" -j jmin:jmax simliar to -j above,but create jitter randomly between jmin and jmax\n"); - printf(" --random-drop simulate packet loss ,unit:0.01%%\n"); - printf(" --disable-filter disable duplicate packet filter.\n"); - printf(" -m max pending packets,to prevent the program from eating up all your memory,\n"); - printf(" default value:0(disabled).\n"); - printf("other options:\n"); - printf(" --log-level 0:never 1:fatal 2:error 3:warn \n"); - printf(" 4:info (default) 5:debug 6:trace\n"); - printf(" --log-position enable file name,function name,line number in log\n"); - printf(" --disable-color disable log color\n"); - printf(" --sock-buf buf size for socket,>=10 and <=10240,unit:kbyte,default:1024\n"); - //printf(" -p use multi-process mode instead of epoll.very costly,only for test,do dont use\n"); - printf(" -h,--help print this help message\n"); - - //printf("common options,these options must be same on both side\n"); -} -void process_arg(int argc, char *argv[]) -{ - int i, j, k; - int opt; - static struct option long_options[] = - { - {"log-level", required_argument, 0, 1}, - {"log-position", no_argument, 0, 1}, - {"disable-color", no_argument, 0, 1}, - {"disable-filter", no_argument, 0, 1}, - {"sock-buf", required_argument, 0, 1}, - {"random-drop", required_argument, 0, 1}, - {"report", required_argument, 0, 1}, - {NULL, 0, 0, 0} - }; - int option_index = 0; - if (argc == 1) - { - print_help(); - myexit( -1); - } - for (i = 0; i < argc; i++) - { - if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) - { - print_help(); - myexit(0); - } - } - for (i = 0; i < argc; i++) - { - if(strcmp(argv[i],"--log-level")==0) - { - if(i1000\n"); - myexit(-1); - } - break; - - case 'j': - if (strchr(optarg, ':') == 0) - { - int jitter; - sscanf(optarg,"%d\n",&jitter); - if(jitter<0 ||jitter>1000*100) - { - mylog(log_fatal,"jitter must be between 0 and 100,000(10 second)\n"); - myexit(-1); - } - jitter_min=0; - jitter_max=jitter; - } - else - { - sscanf(optarg,"%d:%d\n",&jitter_min,&jitter_max); - if(jitter_min<0 ||jitter_max<0||jitter_min>jitter_max) - { - mylog(log_fatal," must satisfy 0<=jmin<=jmax\n"); - myexit(-1); - } - } - break; - case 't': - if (strchr(optarg, ':') == 0) - { - int dup_delay=-1; - sscanf(optarg,"%d\n",&dup_delay); - if(dup_delay<1||dup_delay>1000*100) - { - mylog(log_fatal,"dup_delay must be between 1 and 100,000(10 second)\n"); - myexit(-1); - } - dup_delay_min=dup_delay_max=dup_delay; - } - else - { - sscanf(optarg,"%d:%d\n",&dup_delay_min,&dup_delay_max); - if(dup_delay_min<1 ||dup_delay_max<1||dup_delay_min>dup_delay_max) - { - mylog(log_fatal," must satisfy 1<=dmin<=dmax\n"); - myexit(-1); - } - } - break; - case 'd': - dup_num=-1; - sscanf(optarg,"%d\n",&dup_num); - if(dup_num<0 ||dup_num>5) - { - mylog(log_fatal,"dup_num must be between 0 and 5\n"); - myexit(-1); - } - dup_num+=1; - break; - case 'c': - is_client = 1; - break; - case 's': - is_server = 1; - break; - case 'l': - no_l = 0; - if (strchr(optarg, ':') != 0) - { - sscanf(optarg, "%[^:]:%d", local_address, &local_port); - } - else - { - mylog(log_fatal," -r ip:port\n"); - myexit(1); - strcpy(local_address, "127.0.0.1"); - sscanf(optarg, "%d", &local_port); - } - break; - case 'r': - no_r = 0; - if (strchr(optarg, ':') != 0) - { - //printf("in :\n"); - //printf("%s\n",optarg); - sscanf(optarg, "%[^:]:%d", remote_address, &remote_port); - //printf("%d\n",remote_port); - } - else - { - mylog(log_fatal," -r ip:port\n"); - myexit(1); - strcpy(remote_address, "127.0.0.1"); - sscanf(optarg, "%d", &remote_port); - } - break; - case 'h': - break; - case 1: - if(strcmp(long_options[option_index].name,"log-level")==0) - { - } - else if(strcmp(long_options[option_index].name,"disable-filter")==0) - { - disable_replay_filter=1; - //enable_log_color=0; - } - else if(strcmp(long_options[option_index].name,"disable-color")==0) - { - //enable_log_color=0; - } - else if(strcmp(long_options[option_index].name,"log-position")==0) - { - enable_log_position=1; - } - else if(strcmp(long_options[option_index].name,"random-drop")==0) - { - sscanf(optarg,"%d",&random_drop); - if(random_drop<0||random_drop>10000) - { - mylog(log_fatal,"random_drop must be between 0 10000 \n"); - myexit(-1); - } - } - else if(strcmp(long_options[option_index].name,"report")==0) - { - sscanf(optarg,"%d",&report_interval); - - if(report_interval<=0) - { - mylog(log_fatal,"report_interval must be >0 \n"); - myexit(-1); - } - } - else if(strcmp(long_options[option_index].name,"sock-buf")==0) - { - int tmp=-1; - sscanf(optarg,"%d",&tmp); - if(10<=tmp&&tmp<=10*1024) - { - socket_buf_size=tmp*1024; - } - else - { - mylog(log_fatal,"sock-buf value must be between 1 and 10240 (kbyte) \n"); - myexit(-1); - } - } - else - { - mylog(log_fatal,"unknown option\n"); - myexit(-1); - } - break; - default: - mylog(log_fatal,"unknown option <%x>", opt); - myexit(-1); - } - } - - if (no_l) - mylog(log_fatal,"error: -i not found\n"); - if (no_r) - mylog(log_fatal,"error: -o not found\n"); - if (no_l || no_r) - myexit(-1); - if (is_client == 0 && is_server == 0) - { - mylog(log_fatal,"-s -c hasnt been set\n"); - myexit(-1); - } - if (is_client == 1 && is_server == 1) - { - mylog(log_fatal,"-s -c cant be both set\n"); - myexit(-1); - } -} -int main(int argc, char *argv[]) -{ - assert(sizeof(u64_t)==8); - assert(sizeof(i64_t)==8); - assert(sizeof(u32_t)==4); - assert(sizeof(i32_t)==4); - dup2(1, 2); //redirect stderr to stdout - int i, j, k; - process_arg(argc,argv); - init_random_number_fd(); - anti_replay.prepare(); - - remote_address_uint32=inet_addr(remote_address); - - if(!multi_process_mode) - { - event_loop(); - } - else - { - } - - - return 0; -} - - -}//namespace end diff --git a/classic.h b/classic.h deleted file mode 100644 index 8459ce4..0000000 --- a/classic.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * classic.h - * - * Created on: Sep 15, 2017 - * Author: root - */ - -#ifndef CLASSIC_H_ -#define CLASSIC_H_ - - -namespace classic -{ -int main(int argc, char *argv[]); -} - - -#endif /* CLASSIC_H_ */ diff --git a/common.cpp b/common.cpp index 6d4b72c..749f48b 100644 --- a/common.cpp +++ b/common.cpp @@ -14,11 +14,11 @@ int about_to_exit=0; raw_mode_t raw_mode=mode_faketcp; unordered_map raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}}; -int socket_buf_size=1024*1024; + int max_pending_packet=0; static int random_number_fd=-1; char iptables_rule[200]=""; -int is_client = 0, is_server = 0; +//int is_client = 0, is_server = 0; program_mode_t program_mode=unset_mode;//0 unset; 1client 2server @@ -204,23 +204,37 @@ unsigned short csum(const unsigned short *ptr,int nbytes) { return(answer); } -int set_buf_size(int fd,int size) +int set_buf_size(int fd,int socket_buf_size,int force_socket_buf) { - //int socket_buf_size=1024*1024; - if(setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0) - //if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) - { - printf("set SO_SNDBUF fail\n"); - exit(1); - } - //if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) - if(setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &socket_buf_size, sizeof(socket_buf_size))<0) - { - printf("set SO_RCVBUF fail\n"); - exit(1); - } + if(force_socket_buf) + { + if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) + { + mylog(log_fatal,"SO_SNDBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno)); + myexit(1); + } + if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) + { + mylog(log_fatal,"SO_RCVBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno)); + myexit(1); + } + } + else + { + if(setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0) + { + mylog(log_fatal,"SO_SNDBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno)); + myexit(1); + } + if(setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &socket_buf_size, sizeof(socket_buf_size))<0) + { + mylog(log_fatal,"SO_RCVBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno)); + myexit(1); + } + } return 0; } + void myexit(int a) { if(enable_log_color) @@ -374,7 +388,7 @@ int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval) } return 0; } - +/* int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port) { struct sockaddr_in remote_addr_in; @@ -401,4 +415,4 @@ int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port) return -1; } return 0; -} +}*/ diff --git a/common.h b/common.h index 3b4be13..8c780fc 100644 --- a/common.h +++ b/common.h @@ -45,8 +45,6 @@ #include - - #include #include #include @@ -116,9 +114,8 @@ extern raw_mode_t raw_mode; enum program_mode_t {unset_mode=0,client_mode,server_mode}; extern program_mode_t program_mode; extern unordered_map raw_mode_tostring ; -extern int socket_buf_size; + extern int max_pending_packet; -extern int is_client, is_server; typedef u32_t id_t; @@ -149,7 +146,7 @@ u64_t hton64(u64_t a); bool larger_than_u16(uint16_t a,uint16_t b); bool larger_than_u32(u32_t a,u32_t b); void setnonblocking(int sock); -int set_buf_size(int fd,int size=socket_buf_size); +int set_buf_size(int fd,int socket_buf_size,int force_socket_buf=0); unsigned short csum(const unsigned short *ptr,int nbytes); @@ -167,6 +164,8 @@ int random_between(u32_t a,u32_t b); int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval); +/* int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port); +*/ #endif /* COMMON_H_ */ diff --git a/conn_manager.cpp b/conn_manager.cpp deleted file mode 100644 index bc7c889..0000000 --- a/conn_manager.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/* - * conn_manager.cpp - * - * Created on: Sep 15, 2017 - * Author: root - */ - -#include "conn_manager.h" - -int disable_conv_clear=0; - -conn_manager_t_not_used::conn_manager_t_not_used() { - clear_it = fd_last_active_time.begin(); - long long last_clear_time = 0; - rehash(); - //clear_function=0; -} -conn_manager_t_not_used::~conn_manager_t_not_used() { - clear(); -} -int conn_manager_t_not_used::get_size() { - return fd_to_u64.size(); -} -void conn_manager_t_not_used::rehash() { - u64_to_fd.rehash(10007); - fd_to_u64.rehash(10007); - fd_last_active_time.rehash(10007); -} -void conn_manager_t_not_used::clear() { - if (disable_conv_clear) - return; - - for (it = fd_to_u64.begin(); it != fd_to_u64.end(); it++) { - //int fd=int((it->second<<32u)>>32u); - close(it->first); - } - u64_to_fd.clear(); - fd_to_u64.clear(); - fd_last_active_time.clear(); - - clear_it = fd_last_active_time.begin(); - -} -int conn_manager_t_not_used::exist_fd(u32_t fd) { - return fd_to_u64.find(fd) != fd_to_u64.end(); -} -int conn_manager_t_not_used::exist_u64(u64_t u64) { - return u64_to_fd.find(u64) != u64_to_fd.end(); -} -u32_t conn_manager_t_not_used::find_fd_by_u64(u64_t u64) { - return u64_to_fd[u64]; -} -u64_t conn_manager_t_not_used::find_u64_by_fd(u32_t fd) { - return fd_to_u64[fd]; -} -int conn_manager_t_not_used::update_active_time(u32_t fd) { - return fd_last_active_time[fd] = get_current_time(); -} -int conn_manager_t_not_used::insert_fd(u32_t fd, u64_t u64) { - u64_to_fd[u64] = fd; - fd_to_u64[fd] = u64; - fd_last_active_time[fd] = get_current_time(); - return 0; -} -int conn_manager_t_not_used::erase_fd(u32_t fd) { - if (disable_conv_clear) - return 0; - u64_t u64 = fd_to_u64[fd]; - - u32_t ip = (u64 >> 32u); - - int port = uint16_t((u64 << 32u) >> 32u); - - mylog(log_info, "fd %d cleared,assocated adress %s,%d\n", fd, my_ntoa(ip), - port); - - close(fd); - - fd_to_u64.erase(fd); - u64_to_fd.erase(u64); - fd_last_active_time.erase(fd); - return 0; -} -/* -void conn_manager_t::check_clear_list() { - while (!clear_list.empty()) { - int fd = *clear_list.begin(); - clear_list.pop_front(); - erase_fd(fd); - } -}*/ -int conn_manager_t_not_used::clear_inactive() { - if (get_current_time() - last_clear_time > conv_clear_interval) { - last_clear_time = get_current_time(); - return clear_inactive0(); - } - return 0; -} -int conn_manager_t_not_used::clear_inactive0() { - if (disable_conv_clear) - return 0; - - //map::iterator it; - int cnt = 0; - it = clear_it; - int size = fd_last_active_time.size(); - int num_to_clean = size / conv_clear_ratio + conv_clear_min; //clear 1/10 each time,to avoid latency glitch - - u64_t current_time = get_current_time(); - for (;;) { - if (cnt >= num_to_clean) - break; - if (fd_last_active_time.begin() == fd_last_active_time.end()) - break; - - if (it == fd_last_active_time.end()) { - it = fd_last_active_time.begin(); - } - - if (current_time - it->second > conv_timeout) { - //mylog(log_info,"inactive conv %u cleared \n",it->first); - old_it = it; - it++; - u32_t fd = old_it->first; - erase_fd(old_it->first); - - } else { - it++; - } - cnt++; - } - return 0; -} - - diff --git a/conn_manager.h b/conn_manager.h deleted file mode 100644 index 275ad22..0000000 --- a/conn_manager.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * conn_manager.h - * - * Created on: Sep 15, 2017 - * Author: root - */ - -#ifndef CONN_MANAGER_H_ -#define CONN_MANAGER_H_ - -#include "common.h" -#include "log.h" - -extern int disable_conv_clear; - -struct conn_manager_t_not_used //TODO change map to unordered map -{ - //typedef hash_map map; - unordered_map u64_to_fd; //conv and u64 are both supposed to be uniq - unordered_map fd_to_u64; - - unordered_map fd_last_active_time; - - unordered_map::iterator clear_it; - - unordered_map::iterator it; - unordered_map::iterator old_it; - - //void (*clear_function)(uint64_t u64) ; - - long long last_clear_time; - //list clear_list; - conn_manager_t_not_used(); - ~conn_manager_t_not_used(); - int get_size(); - void rehash(); - void clear(); - int exist_fd(u32_t fd); - int exist_u64(u64_t u64); - u32_t find_fd_by_u64(u64_t u64); - u64_t find_u64_by_fd(u32_t fd); - int update_active_time(u32_t fd); - int insert_fd(u32_t fd,u64_t u64); - int erase_fd(u32_t fd); - //void check_clear_list(); - int clear_inactive(); - int clear_inactive0(); - -}; - - -#endif /* CONN_MANAGER_H_ */ diff --git a/main.cpp b/main.cpp index 2c7c4a9..a868a2f 100644 --- a/main.cpp +++ b/main.cpp @@ -3,9 +3,9 @@ #include "git_version.h" #include "lib/rs.h" #include "packet.h" -//#include "conn_manager.h" +#include "connection.h" +#include "fd_manager.h" #include "delay_manager.h" -#include "classic.h" using namespace std; @@ -15,8 +15,6 @@ typedef long long i64_t; typedef unsigned int u32_t; typedef int i32_t; - - int dup_num=1; int dup_delay_min=20; //0.1ms int dup_delay_max=20; @@ -27,8 +25,8 @@ int jitter_max=0; //int random_number_fd=-1; int mtu_warn=1350; -u32_t remote_address_uint32=0; -char local_address[100], remote_address[100]; +u32_t local_ip_uint32,remote_ip_uint32=0; +char local_ip[100], remote_ip[100]; int local_port = -1, remote_port = -1; u64_t last_report_time=0; @@ -36,660 +34,468 @@ int report_interval=0; //conn_manager_t conn_manager; delay_manager_t delay_manager; +fd_manager_t fd_manager; const int disable_conv_clear=0; -struct conv_manager_t // manage the udp connections -{ - //typedef hash_map map; - unordered_map u64_to_conv; //conv and u64 are both supposed to be uniq - unordered_map conv_to_u64; - unordered_map conv_last_active_time; +int socket_buf_size=1024*1024; - unordered_map::iterator clear_it; - - unordered_map::iterator it; - unordered_map::iterator old_it; - - //void (*clear_function)(uint64_t u64) ; - - long long last_clear_time; - - conv_manager_t() - { - clear_it=conv_last_active_time.begin(); - long long last_clear_time=0; - //clear_function=0; - } - ~conv_manager_t() - { - clear(); - } - int get_size() - { - return conv_to_u64.size(); - } - void reserve() - { - u64_to_conv.reserve(10007); - conv_to_u64.reserve(10007); - conv_last_active_time.reserve(10007); - } - void clear() - { - ///// if(disable_conv_clear) return ; - - if(program_mode==server_mode) - { - for(it=conv_to_u64.begin();it!=conv_to_u64.end();it++) - { - //int fd=int((it->second<<32u)>>32u); - ////// server_clear_function( it->second);//////////////todo - } - } - u64_to_conv.clear(); - conv_to_u64.clear(); - conv_last_active_time.clear(); - - clear_it=conv_last_active_time.begin(); - - } - u32_t get_new_conv() - { - u32_t conv=get_true_random_number_nz(); - while(conv_to_u64.find(conv)!=conv_to_u64.end()) - { - conv=get_true_random_number_nz(); - } - return conv; - } - int is_conv_used(u32_t conv) - { - return conv_to_u64.find(conv)!=conv_to_u64.end(); - } - int is_u64_used(u64_t u64) - { - return u64_to_conv.find(u64)!=u64_to_conv.end(); - } - u32_t find_conv_by_u64(u64_t u64) - { - return u64_to_conv[u64]; - } - u64_t find_u64_by_conv(u32_t conv) - { - return conv_to_u64[conv]; - } - int update_active_time(u32_t conv) - { - return conv_last_active_time[conv]=get_current_time(); - } - int insert_conv(u32_t conv,u64_t u64) - { - u64_to_conv[u64]=conv; - conv_to_u64[conv]=u64; - conv_last_active_time[conv]=get_current_time(); - return 0; - } - int erase_conv(u32_t conv) - { - if(disable_conv_clear) return 0; - u64_t u64=conv_to_u64[conv]; - if(program_mode==server_mode) - { - //server_clear_function(u64); - } - conv_to_u64.erase(conv); - u64_to_conv.erase(u64); - conv_last_active_time.erase(conv); - return 0; - } - int clear_inactive(char * ip_port=0) - { - if(get_current_time()-last_clear_time>conv_clear_interval) - { - last_clear_time=get_current_time(); - return clear_inactive0(ip_port); - } - return 0; - } - int clear_inactive0(char * ip_port) - { - if(disable_conv_clear) return 0; - - - //map::iterator it; - int cnt=0; - it=clear_it; - int size=conv_last_active_time.size(); - int num_to_clean=size/conv_clear_ratio+conv_clear_min; //clear 1/10 each time,to avoid latency glitch - - num_to_clean=min(num_to_clean,size); - - u64_t current_time=get_current_time(); - for(;;) - { - if(cnt>=num_to_clean) break; - if(conv_last_active_time.begin()==conv_last_active_time.end()) break; - - if(it==conv_last_active_time.end()) - { - it=conv_last_active_time.begin(); - } - - if( current_time -it->second >conv_timeout ) - { - //mylog(log_info,"inactive conv %u cleared \n",it->first); - old_it=it; - it++; - u32_t conv= old_it->first; - erase_conv(old_it->first); - if(ip_port==0) - { - mylog(log_info,"conv %x cleared\n",conv); - } - else - { - mylog(log_info,"[%s]conv %x cleared\n",ip_port,conv); - } - } - else - { - it++; - } - cnt++; - } - return 0; - } -};//g_conv_manager; - -struct conn_info_t //stores info for a raw connection.for client ,there is only one connection,for server there can be thousand of connection since server can -//handle multiple clients -{ - conv_manager_t conv_manager; -}; -struct conn_manager_t //manager for connections. for client,we dont need conn_manager since there is only one connection.for server we use one conn_manager for all connections -{ - - u32_t ready_num; - - unordered_map udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search - unordered_map timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy - - unordered_map const_id_mp; - - unordered_map mp; //put it at end so that it de-consturcts first - - unordered_map::iterator clear_it; - - long long last_clear_time; - - conn_manager_t() - { - ready_num=0; - mp.reserve(10007); - clear_it=mp.begin(); - timer_fd_mp.reserve(10007); - const_id_mp.reserve(10007); - udp_fd_mp.reserve(100007); - last_clear_time=0; - //current_ready_ip=0; - // current_ready_port=0; - } - int exist(u32_t ip,uint16_t port) - { - u64_t u64=0; - u64=ip; - u64<<=32u; - u64|=port; - if(mp.find(u64)!=mp.end()) - { - return 1; - } - return 0; - } - /* - int insert(uint32_t ip,uint16_t port) - { - uint64_t u64=0; - u64=ip; - u64<<=32u; - u64|=port; - mp[u64]; - return 0; - }*/ - conn_info_t *& find_insert_p(u32_t ip,uint16_t port) //be aware,the adress may change after rehash - { - u64_t u64=0; - u64=ip; - u64<<=32u; - u64|=port; - unordered_map::iterator it=mp.find(u64); - if(it==mp.end()) - { - mp[u64]=new conn_info_t; - } - return mp[u64]; - } - conn_info_t & find_insert(u32_t ip,uint16_t port) //be aware,the adress may change after rehash - { - u64_t u64=0; - u64=ip; - u64<<=32u; - u64|=port; - unordered_map::iterator it=mp.find(u64); - if(it==mp.end()) - { - mp[u64]=new conn_info_t; - } - return *mp[u64]; - } - int erase(unordered_map::iterator erase_it) - { - if(erase_it->second->state.server_current_state==server_ready) - { - ready_num--; - assert(i32_t(ready_num)!=-1); - assert(erase_it->second!=0); - assert(erase_it->second->timer_fd !=0); - assert(erase_it->second->oppsite_const_id!=0); - assert(const_id_mp.find(erase_it->second->oppsite_const_id)!=const_id_mp.end()); - assert(timer_fd_mp.find(erase_it->second->timer_fd)!=timer_fd_mp.end()); - - const_id_mp.erase(erase_it->second->oppsite_const_id); - timer_fd_mp.erase(erase_it->second->timer_fd); - close(erase_it->second->timer_fd);// close will auto delte it from epoll - delete(erase_it->second); - mp.erase(erase_it->first); - } - else - { - assert(erase_it->second->blob==0); - assert(erase_it->second->timer_fd ==0); - assert(erase_it->second->oppsite_const_id==0); - delete(erase_it->second); - mp.erase(erase_it->first); - } - return 0; - } -int clear_inactive() -{ - if(get_current_time()-last_clear_time>conn_clear_interval) - { - last_clear_time=get_current_time(); - return clear_inactive0(); - } - return 0; -} -int clear_inactive0() -{ - unordered_map::iterator it; - unordered_map::iterator old_it; - - if(disable_conn_clear) return 0; - - //map::iterator it; - int cnt=0; - it=clear_it; - int size=mp.size(); - int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 1/10 each time,to avoid latency glitch - - mylog(log_trace,"mp.size() %d\n", size); - - num_to_clean=min(num_to_clean,(int)mp.size()); - u64_t current_time=get_current_time(); - - for(;;) - { - if(cnt>=num_to_clean) break; - if(mp.begin()==mp.end()) break; - - if(it==mp.end()) - { - it=mp.begin(); - } - - if(it->second->state.server_current_state==server_ready &¤t_time - it->second->last_hb_recv_time <=server_conn_timeout) - { - it++; - } - else if(it->second->state.server_current_state!=server_ready&& current_time - it->second->last_state_time <=server_handshake_timeout ) - { - it++; - } - else if(it->second->blob!=0&&it->second->blob->conv_manager.get_size() >0) - { - assert(it->second->state.server_current_state==server_ready); - it++; - } - else - { - mylog(log_info,"[%s:%d]inactive conn cleared \n",my_ntoa(it->second->raw_info.recv_info.src_ip),it->second->raw_info.recv_info.src_port); - old_it=it; - it++; - erase(old_it); - } - cnt++; - } - return 0; -} - -}conn_manager; int VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV; - - -int event_loop() +int init_listen_socket() { - struct sockaddr_in local_me, local_other; - local_listen_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - int yes = 1; - //setsockopt(local_listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); - set_buf_size(local_listen_fd,4*1024*1024); - setnonblocking(local_listen_fd); + local_listen_fd =socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + + + int yes = 1; + //setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); + + struct sockaddr_in local_me={0}; - //char data[buf_len]; - //char *data=data0; socklen_t slen = sizeof(sockaddr_in); - memset(&local_me, 0, sizeof(local_me)); + //memset(&local_me, 0, sizeof(local_me)); local_me.sin_family = AF_INET; local_me.sin_port = htons(local_port); - local_me.sin_addr.s_addr = inet_addr(local_address); - if (bind(local_listen_fd, (struct sockaddr*) &local_me, slen) == -1) - { - mylog(log_fatal,"socket bind error"); + local_me.sin_addr.s_addr = local_ip_uint32; + + if (bind(local_listen_fd, (struct sockaddr*) &local_me, slen) == -1) { + mylog(log_fatal,"socket bind error\n"); + //perror("socket bind error"); myexit(1); } + setnonblocking(local_listen_fd); + set_buf_size(local_listen_fd,socket_buf_size); + + return 0; +} +int new_connected_socket(int &fd,u32_t ip,int port) +{ + char ip_port[40]; + sprintf(ip_port,"%s:%d",my_ntoa(ip),port); + + struct sockaddr_in remote_addr_in = { 0 }; + + socklen_t slen = sizeof(sockaddr_in); + //memset(&remote_addr_in, 0, sizeof(remote_addr_in)); + remote_addr_in.sin_family = AF_INET; + remote_addr_in.sin_port = htons(port); + remote_addr_in.sin_addr.s_addr = ip; + + fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (fd < 0) { + mylog(log_warn, "[%s]create udp_fd error\n", ip_port); + return -1; + } + setnonblocking(new_udp_fd); + set_buf_size(fd, socket_buf_size); + + mylog(log_debug, "[%s]created new udp_fd %d\n", ip_port, new_udp_fd); + int ret = connect(fd, (struct sockaddr *) &remote_addr_in, slen); + if (ret != 0) { + mylog(log_warn, "[%s]fd connect fail\n",ip_port); + close(fd); + return -1; + } + return 0; +} +int client_event_loop() +{ + //char buf[buf_len]; + int i, j, k;int ret; + int yes = 1; + int epoll_fd; + int remote_fd; + + conn_info_t conn_info; + + init_listen_socket(); + + epoll_fd = epoll_create1(0); - int epollfd = epoll_create1(0); const int max_events = 4096; struct epoll_event ev, events[max_events]; - if (epollfd < 0) - { - mylog(log_fatal,"epoll created return %d\n", epollfd); + if (epoll_fd < 0) { + mylog(log_fatal,"epoll return %d\n", epoll_fd); myexit(-1); } + ev.events = EPOLLIN; - ev.data.fd = local_listen_fd; - int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, local_listen_fd, &ev); - - if(ret!=0) - { - mylog(log_fatal,"epoll created return %d\n", epollfd); + ev.data.u64 = local_listen_fd; + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, local_listen_fd, &ev); + if (ret!=0) { + mylog(log_fatal,"add udp_listen_fd error\n"); myexit(-1); } - int clear_timer_fd=-1; - set_timer_ms(epollfd,clear_timer_fd,timer_interval); + assert(new_connected_socket(remote_fd,remote_ip_uint32,remote_port)==0); ev.events = EPOLLIN; - ev.data.fd = delay_manager.timer_fd(); + ev.data.u64 = remote_fd; - epoll_ctl(epollfd, EPOLL_CTL_ADD, delay_manager.timer_fd, &ev); - - - if (ret < 0) - { - mylog(log_fatal,"epoll_ctl return %d\n", ret); + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, remote_fd, &ev); + if (ret!= 0) { + mylog(log_fatal,"add raw_fd error\n"); myexit(-1); } - for (;;) + while(1)//////////////////////// { - int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000); //3mins - if (nfds < 0) - { - mylog(log_fatal,"epoll_wait return %d\n", nfds); - myexit(-1); - } - int n; - int clear_triggered=0; - for (n = 0; n < nfds; ++n) - { - if (events[n].data.fd == local_listen_fd) //data income from local end + if(about_to_exit) myexit(0); + + int nfds = epoll_wait(epoll_fd, events, max_events, 180 * 1000); + if (nfds < 0) { //allow zero + if(errno==EINTR ) { - - char data[buf_len]; - int data_len; - - slen = sizeof(sockaddr_in); - if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0, - (struct sockaddr *) &local_other, &slen)) == -1) //<--first packet from a new ip:port turple - { - - mylog(log_error,"recv_from error,errno %s,this shouldnt happen,but lets try to pretend it didnt happen",strerror(errno)); - //myexit(1); - continue; - } - mylog(log_trace, "received data from listen fd,%s:%d, len=%d\n", my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port),data_len); - if(data_len>mtu_warn) - { - mylog(log_warn,"huge packet,data len=%d (>%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn); - } - data[data_len] = 0; //for easier debug - u64_t u64=pack_u64(local_other.sin_addr.s_addr,ntohs(local_other.sin_port)); - - if(!conn_manager.exist_u64(u64)) - { - - if(int(conn_manager.fd_to_u64.size())>=max_conv_num) - { - mylog(log_info,"new connection from %s:%d ,but ignored,bc of max_conv_num reached\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port)); - continue; - } - int new_udp_fd; - if(create_new_udp(new_udp_fd,remote_address_uint32,remote_port)!=0) - { - mylog(log_info,"new connection from %s:%d ,but create udp fd failed\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port)); - continue; - } - struct epoll_event ev; - - mylog(log_trace, "u64: %lld\n", u64); - ev.events = EPOLLIN; - - ev.data.fd = new_udp_fd; - - ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev); - if (ret != 0) { - mylog(log_info,"new connection from %s:%d ,but add to epoll failed\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port)); - close(new_udp_fd); - continue; - } - mylog(log_info,"new connection from %s:%d ,created new udp fd %d\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port),new_udp_fd); - conn_manager.insert_fd(new_udp_fd,u64); - } - - int new_udp_fd=conn_manager.find_fd_by_u64(u64); - conn_manager.update_active_time(new_udp_fd); - int ret; - if(is_client) - { - add_seq(data,data_len); - my_time_t sum=0; - - for(int i=0;i\n",i); - char new_data[buf_len]; - int new_len=0; - do_obscure(data, data_len, new_data, new_len); - delay_data_t tmp; - tmp.type=enum_send_fd; - tmp.data=new_data; - tmp.len=new_len; - tmp.dest.fd=new_udp_fd; - if(i==0) - sum+=random_between(jitter_min,jitter_max)*100; - else - sum+=random_between(dup_delay_min,dup_delay_max)*100; - delay_manager.add(sum,tmp); - } - /* - if(jitter_max==0) - { - char new_data[buf_len]; - int new_len=0; - do_obscure(data, data_len, new_data, new_len); - ret = send_fd(new_udp_fd, new_data,new_len, 0); - if (ret < 0) { - mylog(log_warn, "send returned %d ,errno:%s\n", ret,strerror(errno)); - } - add_and_new(new_udp_fd, dup_num - 1,random_between(dup_delay_min,dup_delay_max), data, data_len,u64); - } - else - { - add_and_new(new_udp_fd, dup_num,random_between(jitter_min,jitter_max), data, data_len,u64); - }*/ - packet_send_count++; - } - else - { - printf("i got a packet\n"); - char new_data[buf_len]; - int new_len; - - if (de_obscure(data, data_len, new_data, new_len) != 0) { - printf("failed 1\n"); - mylog(log_trace,"de_obscure failed \n"); - continue; - } - //dup_packet_recv_count++; - if (remove_seq(new_data, new_len) != 0) { - printf("failed 2\n"); - mylog(log_trace,"remove_seq failed \n"); - continue; - } - //packet_recv_count++; - ret = send_fd(new_udp_fd, new_data,new_len, 0); - if (ret < 0) { - mylog(log_warn, "send returned %d,%s\n", ret,strerror(errno)); - //perror("what happened????"); - } - } - - - } - else if(events[n].data.fd == clear_timer_fd) - { - clear_triggered=1; - if(report_interval!=0 &&get_current_time()-last_report_time>u64_t(report_interval)*1000) - { - last_report_time=get_current_time(); - if(is_client) - mylog(log_info,"client-->server: %llu,%llu(include dup); server-->client %llu,%lld(include dup)\n",packet_send_count, - dup_packet_send_count,packet_recv_count,dup_packet_recv_count); - else - mylog(log_info,"client-->server: %llu,%llu(include dup); server-->client %llu,%lld(include dup)\n",packet_recv_count,dup_packet_recv_count,packet_send_count, - dup_packet_send_count); - } - } - else if (events[n].data.fd == delay_manager.timer_fd) - { - uint64_t value; - read(delay_manager.timer_fd, &value, 8); - //printf("",delay_mp.size()); - //fflush(stdout); + mylog(log_info,"epoll interrupted by signal\n"); + myexit(0); } else { - int udp_fd=events[n].data.fd; - if(!conn_manager.exist_fd(udp_fd)) continue; - + mylog(log_fatal,"epoll_wait return %d\n", nfds); + myexit(-1); + } + } + int idx; + for (idx = 0; idx < nfds; ++idx) { + if (events[idx].data.u64 == (u64_t)remote_fd) + { char data[buf_len]; - int data_len =recv(udp_fd,data,max_data_len,0); - mylog(log_trace, "received data from udp fd %d, len=%d\n", udp_fd,data_len); + int data_len =recv(remote_fd,data,max_data_len,0); + mylog(log_trace, "received data from udp fd %d, len=%d\n", remote_fd,data_len); if(data_len<0) { if(errno==ECONNREFUSED) { //conn_manager.clear_list.push_back(udp_fd); - mylog(log_debug, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,udp_fd,strerror(errno)); + mylog(log_debug, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,remote_fd,strerror(errno)); } - mylog(log_warn, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,udp_fd,strerror(errno)); + mylog(log_warn, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,remote_fd,strerror(errno)); continue; } if(data_len>mtu_warn) { mylog(log_warn,"huge packet,data len=%d (>%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn); } + u32_t conv; + char *new_data; + int new_len; + get_conv(conv,data,data_len,new_data,new_len); + if(!conn_info.conv_manager.is_conv_used(conv))continue; + u64_t u64=conn_info.conv_manager.conv_to_u64(conv); + u32_t ip=get_u64_h(u64); + int port=get_u64_l(u64); + dest_t dest; + dest.type=type_ip_port; + dest.inner.ip_port.ip=ip; + dest.inner.ip_port.port=port; + my_send(dest,new_data,new_len); + //sendto_ip_port(ip,port,new_data,new_len,0); - assert(conn_manager.exist_fd(udp_fd)); + //////////////////todo + } + /* + else if(events[idx].data.u64 ==(u64_t)timer_fd) + { + u64_t value; + read(timer_fd, &value, 8); + client_on_timer(conn_info); - conn_manager.update_active_time(udp_fd); + mylog(log_trace,"epoll_trigger_counter: %d \n",epoll_trigger_counter); + epoll_trigger_counter=0; + }*/ + else if (events[idx].data.u64 == (u64_t)local_listen_fd) + { + char data[buf_len]; + int data_len; + struct sockaddr_in udp_new_addr_in={0}; + socklen_t udp_new_addr_len = sizeof(sockaddr_in); + if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0, + (struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) { + mylog(log_error,"recv_from error,this shouldnt happen at client\n"); + myexit(1); + }; - u64_t u64=conn_manager.find_u64_by_fd(udp_fd); - - if(is_client) + if(data_len>=mtu_warn) { - char new_data[buf_len]; - int new_len; - if (de_obscure(data, data_len, new_data, new_len) != 0) { - mylog(log_debug,"data_len=%d \n",data_len); - continue; - } + mylog(log_warn,"huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",recv_len,mtu_warn); + } + mylog(log_trace,"Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr), + ntohs(udp_new_addr_in.sin_port),data_len); - //dup_packet_recv_count++; - if (remove_seq(new_data, new_len) != 0) { - mylog(log_debug,"remove_seq error \n"); + u64_t u64=((u64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port); + u32_t conv; + + if(!conn_info.conv_manager.is_u64_used(u64)) + { + if(conn_info.conv_manager.get_size() >=max_conv_num) + { + mylog(log_warn,"ignored new udp connect bc max_conv_num exceed\n"); continue; } - //packet_recv_count++; - ret = sendto_u64(u64, new_data, - new_len , 0); - if (ret < 0) { - mylog(log_warn, "sento returned %d,%s\n", ret,strerror(errno)); - //perror("ret<0"); - } + conv=conn_info.conv_manager.get_new_conv(); + conn_info.conv_manager.insert_conv(conv,u64); + mylog(log_info,"new packet from %s:%d,conv_id=%x\n",inet_ntoa(udp_new_addr_in.sin_addr),ntohs(udp_new_addr_in.sin_port),conv); } else { - add_seq(data,data_len); - - my_time_t sum=0; - for(int i=0;i\n",i); - char new_data[buf_len]; - int new_len=0; - do_obscure(data, data_len, new_data, new_len); - delay_data_t tmp; - tmp.type=enum_sendto_u64; - tmp.data=new_data; - tmp.len=new_len; - tmp.dest.u64=u64; - if(i==0) - sum+=random_between(jitter_min,jitter_max)*100; - else - sum+=random_between(dup_delay_min,dup_delay_max)*100; - delay_manager.add(sum,tmp); - } - packet_send_count++; - - + conv=conn_info.conv_manager.find_conv_by_u64(u64); } - //mylog(log_trace, "%s :%d\n", inet_ntoa(tmp_sockaddr.sin_addr), - // ntohs(tmp_sockaddr.sin_port)); - //mylog(log_trace, "%d byte sent\n", ret); + conn_info.conv_manager.update_active_time(conv); + char *new_data; + int new_len; + put_conv(conv,data,data_len,new_data,new_len); + dest_t dest; + dest.type=type_fd; + dest.inner.fd=remote_fd; + my_send(dest,new_data,new_len); + //send_fd(remote_fd,new_data,new_len,0); + //send_data_safer(conn_info,buf,recv_len,conv); + ///////////////////todo + } + else + { + mylog(log_fatal,"unknown fd,this should never happen\n"); + myexit(-1); } } - delay_manager.check(); - //conn_manager.check_clear_list(); - if(clear_triggered) // 删除操作在epoll event的最后进行,防止event cache中的fd失效。 - { - u64_t value; - read(clear_timer_fd, &value, 8); - mylog(log_trace, "timer!\n"); - conn_manager.clear_inactive(); - } } - myexit(0); return 0; } + +int server_event_loop() +{ + //char buf[buf_len]; + int i, j, k;int ret; + int yes = 1; + int epoll_fd; + int remote_fd; + + conn_info_t conn_info; + + init_listen_socket(); + + epoll_fd = epoll_create1(0); + + const int max_events = 4096; + struct epoll_event ev, events[max_events]; + if (epoll_fd < 0) { + mylog(log_fatal,"epoll return %d\n", epoll_fd); + myexit(-1); + } + + ev.events = EPOLLIN; + ev.data.u64 = local_listen_fd; + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, local_listen_fd, &ev); + if (ret!=0) { + mylog(log_fatal,"add udp_listen_fd error\n"); + myexit(-1); + } + + + mylog(log_info,"now listening at %s:%d\n",my_ntoa(local_ip_uint32),local_port); + while(1)//////////////////////// + { + + if(about_to_exit) myexit(0); + + int nfds = epoll_wait(epoll_fd, events, max_events, 180 * 1000); + if (nfds < 0) { //allow zero + if(errno==EINTR ) + { + mylog(log_info,"epoll interrupted by signal\n"); + myexit(0); + } + else + { + mylog(log_fatal,"epoll_wait return %d\n", nfds); + myexit(-1); + } + } + int idx; + for (idx = 0; idx < nfds; ++idx) + { + /* + if ((events[idx].data.u64 ) == (u64_t)timer_fd) + { + conn_manager.clear_inactive(); + u64_t dummy; + read(timer_fd, &dummy, 8); + //current_time_rough=get_current_time(); + } + else */if (events[idx].data.u64 == (u64_t)local_listen_fd) + { + //int recv_len; + char data[buf_len]; + int data_len; + struct sockaddr_in udp_new_addr_in={0}; + socklen_t udp_new_addr_len = sizeof(sockaddr_in); + if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0, + (struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) { + mylog(log_error,"recv_from error,this shouldnt happen at client\n"); + myexit(1); + }; + + if(data_len>=mtu_warn) + { + mylog(log_warn,"huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",recv_len,mtu_warn); + } + mylog(log_trace,"Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr), + ntohs(udp_new_addr_in.sin_port),data_len); + + uint32_t ip=udp_new_addr_in.sin_addr.s_addr; + int port=udp_new_addr_in.sin_port; + if(!conn_manager.exist(ip,port)) + { + conn_info_t &conn_info=conn_manager.find_insert(ip,port); + conn_info.conv_manager.reserve(); + } + conn_info_t &conn_info=conn_manager.find_insert(ip,port); + + u32_t conv; + char *new_data; + int new_len; + get_conv(conv,data,data_len,new_data,new_len); + + /* + id_t tmp_conv_id; + memcpy(&tmp_conv_id,&data_[0],sizeof(tmp_conv_id)); + tmp_conv_id=ntohl(tmp_conv_id);*/ + + if (!conn_info.conv_manager.is_conv_used(conv)) + { + int new_udp_fd; + new_connected_socket(new_udp_fd,remote_ip_uint32,remote_port); + + if (ret != 0) { + mylog(log_warn, "[%s:%d]add udp_fd error\n",my_ntoa(ip),port); + close(new_udp_fd); + return -1; + } + + fd64_t fd64 = fd_manager.insert_fd(new_udp_fd); + ev.events = EPOLLIN; + ev.data.u64 = fd64; + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_udp_fd, &ev); + + conn_info.conv_manager.insert_conv(conv, fd64); + assert(conn_manager.udp_fd_mp.find(new_udp_fd)==conn_manager.udp_fd_mp.end()); + + conn_manager.udp_fd_mp[new_udp_fd] = &conn_info; + } + fd64_t fd64= conn_info.conv_manager.find_u64_by_conv(conv); + //int fd=fd_manager.fd64_to_fd(fd64); + dest_t dest; + dest.type=type_fd64; + dest.inner.fd64=fd64; + my_send(dest,new_data,new_len); + //int fd = int((u64 << 32u) >> 32u); + //////////////////////////////todo + + //u64_t u64=((u64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port); + } + /* + else if ((events[idx].data.u64 >>32u) == 2u) + { + if(debug_flag)begin_time=get_current_time(); + int fd=get_u64_l(events[idx].data.u64); + u64_t dummy; + read(fd, &dummy, 8); + + if(conn_manager.timer_fd_mp.find(fd)==conn_manager.timer_fd_mp.end()) //this can happen,when fd is a just closed fd + { + mylog(log_info,"timer_fd no longer exits\n"); + continue; + } + conn_info_t* p_conn_info=conn_manager.timer_fd_mp[fd]; + u32_t ip=p_conn_info->raw_info.recv_info.src_ip; + u32_t port=p_conn_info->raw_info.recv_info.src_port; + assert(conn_manager.exist(ip,port));//TODO remove this for peformance + + assert(p_conn_info->state.server_current_state == server_ready); //TODO remove this for peformance + + //conn_info_t &conn_info=conn_manager.find(ip,port); + char ip_port[40]; + + sprintf(ip_port,"%s:%d",my_ntoa(ip),port); + + server_on_timer_multi(*p_conn_info,ip_port); + + if(debug_flag) + { + end_time=get_current_time(); + mylog(log_debug,"(events[idx].data.u64 >>32u) == 2u ,%llu,%llu,%llu \n",begin_time,end_time,end_time-begin_time); + } + }*/ + else if (events[idx].data.u64 >u32_t(-1)) + { + //uint32_t conv_id=events[n].data.u64>>32u; + + int fd=int((events[idx].data.u64<<32u)>>32u); + + if(conn_manager.udp_fd_mp.find(fd)==conn_manager.udp_fd_mp.end()) //this can happen,when fd is a just closed fd + { + mylog(log_debug,"fd no longer exists in udp_fd_mp,udp fd %d\n",fd); + recv(fd,0,0,0); + continue; + } + conn_info_t* p_conn_info=conn_manager.udp_fd_mp[fd]; + + u64_t u64=conn_manager.udp_fd_mp[fd]; + u32_t ip=get_u64_h(u64); + u32_t port=get_u64_l(u64); + if(!conn_manager.exist(ip,port))//TODO remove this for peformance + { + mylog(log_fatal,"ip port no longer exits 2!!!this shouldnt happen\n"); + myexit(-1); + } + + conn_info_t &conn_info=*p_conn_info; + + if(!conn_info.conv_manager.is_u64_used(fd)) + { + mylog(log_debug,"conv no longer exists,udp fd %d\n",fd); + int recv_len=recv(fd,0,0,0); ///////////TODO ,delete this + continue; + } + + u32_t conv_id=conn_info.conv_manager.find_conv_by_u64(fd); + + int recv_len=recv(fd,buf,max_data_len,0); + + mylog(log_trace,"received a packet from udp_fd,len:%d\n",recv_len); + + if(recv_len<0) + { + mylog(log_debug,"udp fd,recv_len<0 continue,%s\n",strerror(errno)); + + continue; + } + + if(recv_len>=mtu_warn) + { + mylog(log_warn,"huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",recv_len,mtu_warn); + } + + ////////todo send data + } + else + { + mylog(log_fatal,"unknown fd,this should never happen\n"); + myexit(-1); + } + + } + } + return 0; +} + + int unit_test() { int i,j,k; @@ -770,6 +576,7 @@ void print_help() } void process_arg(int argc, char *argv[]) { + int is_client=0,is_server=0; int i, j, k; int opt; static struct option long_options[] = @@ -935,13 +742,13 @@ void process_arg(int argc, char *argv[]) no_l = 0; if (strchr(optarg, ':') != 0) { - sscanf(optarg, "%[^:]:%d", local_address, &local_port); + sscanf(optarg, "%[^:]:%d", local_ip, &local_port); } else { mylog(log_fatal," -r ip:port\n"); myexit(1); - strcpy(local_address, "127.0.0.1"); + strcpy(local_ip, "127.0.0.1"); sscanf(optarg, "%d", &local_port); } break; @@ -951,14 +758,14 @@ void process_arg(int argc, char *argv[]) { //printf("in :\n"); //printf("%s\n",optarg); - sscanf(optarg, "%[^:]:%d", remote_address, &remote_port); + sscanf(optarg, "%[^:]:%d", remote_ip, &remote_port); //printf("%d\n",remote_port); } else { mylog(log_fatal," -r ip:port\n"); myexit(1); - strcpy(remote_address, "127.0.0.1"); + strcpy(remote_ip, "127.0.0.1"); sscanf(optarg, "%d", &remote_port); } break; @@ -1042,16 +849,25 @@ void process_arg(int argc, char *argv[]) mylog(log_fatal,"-s -c cant be both set\n"); myexit(-1); } + if(is_client==1) + { + program_mode=client_mode; + } + else + { + program_mode=server_mode; + } } int main(int argc, char *argv[]) { + /* if(argc==1||argc==0) { printf("this_program classic\n"); printf("this_program fec\n"); return 0; - } + }*/ /* if(argc>=2&&strcmp(argv[1],"fec")!=0) { @@ -1069,11 +885,18 @@ int main(int argc, char *argv[]) delay_manager.capacity=max_pending_packet; init_random_number_fd(); - remote_address_uint32=inet_addr(remote_address); + local_ip_uint32=inet_addr(local_ip); + remote_ip_uint32=inet_addr(remote_ip); - event_loop(); - + if(program_mode==client_mode) + { + client_event_loop(); + } + else + { + server_event_loop(); + } return 0; } diff --git a/makefile b/makefile index aa32528..7737dfe 100755 --- a/makefile +++ b/makefile @@ -8,7 +8,7 @@ cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++ #cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers -SOURCES=main.cpp log.cpp common.cpp lib/fec.c lib/rs.c packet.cpp conn_manager.cpp classic.cpp delay_manager.cpp +SOURCES=main.cpp log.cpp common.cpp lib/fec.c lib/rs.c packet.cpp conn_manager.cpp delay_manager.cpp NAME=speeder TARGETS=amd64 arm mips24kc_be x86 mips24kc_le diff --git a/packet.cpp b/packet.cpp index 1971fa7..cd12c70 100644 --- a/packet.cpp +++ b/packet.cpp @@ -8,6 +8,7 @@ #include "common.h" #include "log.h" +#include "packet.h" int iv_min=2; int iv_max=16;//< 256; @@ -194,38 +195,25 @@ int de_obscure(const char * input, int in_len,char *output,int &out_len) } -int sendto_fd_u64 (int fd,u64_t u64,char * buf, int len,int flags) +int sendto_fd_ip_port (int fd,u32_t ip,int port,char * buf, int len,int flags) { - /* - if(is_server) - { - dup_packet_send_count++; - } - if(is_server&&random_drop!=0) - { - if(get_true_random_number()%10000<(u32_t)random_drop) - { - return 0; - } - }*/ - sockaddr_in tmp_sockaddr; memset(&tmp_sockaddr,0,sizeof(tmp_sockaddr)); tmp_sockaddr.sin_family = AF_INET; - tmp_sockaddr.sin_addr.s_addr = (u64 >> 32u); + tmp_sockaddr.sin_addr.s_addr = ip; - tmp_sockaddr.sin_port = htons(uint16_t((u64 << 32u) >> 32u)); + tmp_sockaddr.sin_port = htons(uint16_t(port)); return sendto(fd, buf, len , 0, (struct sockaddr *) &tmp_sockaddr, sizeof(tmp_sockaddr)); } -int sendto_u64 (u64_t u64,char * buf, int len,int flags) +int sendto_ip_port (u32_t ip,int port,char * buf, int len,int flags) { - return sendto_fd_u64(local_listen_fd,u64,buf,len,flags); + return sendto_fd_ip_port(local_listen_fd,ip,port,buf,len,flags); } int send_fd (int fd,char * buf, int len,int flags) { @@ -243,4 +231,46 @@ int send_fd (int fd,char * buf, int len,int flags) }*/ return send(fd,buf,len,flags); } +//enum delay_type_t {none=0,enum_sendto_u64,enum_send_fd,client_to_local,client_to_remote,server_to_local,server_to_remote}; +int my_send(dest_t &dest,char *data,int len) +{ + switch(dest.type) + { + case type_ip_port: + return sendto_ip_port(dest.inner.ip_port.ip,dest.inner.ip_port.port,data,len,0); + break; + case type_fd64: + if(!fd_manager.fd64_exist(dest.inner.fd64)) return -1; + int fd=fd_manager.fd64_to_fd(dest.inner.fd64); + return send_fd(fd,data,len,0); + break; + case type_fd: + send_fd(dest.inner.fd,data,len,0); + break; + default: + assert(0==1); + } + return 0; +} + +int put_conv(u32_t conv,char * input,int len_in,char *&output,int &len_out) +{ + static char buf[buf_len]; + output=buf; + u32_t n_conv=htonl(conv); + memcpy(output,&n_conv,sizeof(n_conv)); + memcpy(output+sizeof(n_conv),input,len_in); + len_out=len_in+(int)(sizeof(n_conv)); + return 0; +} +int get_conv(u32_t &conv,char *input,int len_in,char *&output,int &len_out ) +{ + u32_t n_conv; + memcpy(&n_conv,input,sizeof(n_conv)); + conv=ntohl(n_conv); + output=input+sizeof(n_conv); + len_out=len_in-(int)(sizeof(n_conv)); + if(len_out<0) return -1; + return 0; +} diff --git a/packet.h b/packet.h index 356176a..343541c 100644 --- a/packet.h +++ b/packet.h @@ -9,6 +9,7 @@ #define PACKET_H_ #include "common.h" +#include "fd_manager.h" extern int iv_min; extern int iv_max;//< 256; @@ -22,6 +23,29 @@ extern int disable_replay_filter; extern int random_drop; extern int local_listen_fd; + +enum dest_type{none=0,type_ip_port,type_fd64,type_fd}; + + +struct ip_port_t +{ + u32_t ip; + int port; +}; +union inner_t +{ + ip_port_t ip_port; + int fd; + fd64_t fd64; +}; +struct dest_t +{ + dest_type type; + inner_t inner; +}; + +int my_send(dest_t &dest,char *data,int len); + void encrypt_0(char * input,int &len,char *key); void decrypt_0(char * input,int &len,char *key); int add_seq(char * data,int &data_len ); @@ -29,8 +53,8 @@ int remove_seq(char * data,int &data_len); int do_obscure(const char * input, int in_len,char *output,int &out_len); int de_obscure(const char * input, int in_len,char *output,int &out_len); -int sendto_fd_u64 (int fd,u64_t u64,char * buf, int len,int flags); -int sendto_u64 (u64_t u64,char * buf, int len,int flags); +//int sendto_fd_u64 (int fd,u64_t u64,char * buf, int len,int flags); +int sendto_ip_port (u32_t ip,int port,char * buf, int len,int flags); int send_fd (int fd,char * buf, int len,int flags); #endif /* PACKET_H_ */