From 53ad5730b3f296df5e8e1ebffce2dbc9d4bdc47f Mon Sep 17 00:00:00 2001 From: wangyu Date: Sun, 23 Jul 2017 04:17:06 +0800 Subject: [PATCH] refactor,udp tcp works --- main.cpp | 210 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 153 insertions(+), 57 deletions(-) diff --git a/main.cpp b/main.cpp index 2dd6c55..c98448a 100755 --- a/main.cpp +++ b/main.cpp @@ -50,7 +50,7 @@ using namespace std; const int mode_tcp=0; const int mode_udp=1; const int mode_icmp=2; -int raw_mode=mode_udp; +int raw_mode=mode_tcp; char local_address[100], remote_address[100],source_address[100]; int local_port = -1, remote_port = -1; @@ -918,7 +918,7 @@ int recv_raw_ip(packet_info_t &info,char * &payload,int &payloadlen) if(recv_len<0) { - printf("recv_len %d",recv_len); + printf("recv_len %d\n",recv_len); return -1; } if(recv_len\n",oppsite_id,my_id); @@ -2119,7 +2181,7 @@ int keep_connection_client() //for client if(debug_mode)printf("heartbeat sent <%x,%x>\n",oppsite_id,my_id); - send_hb(g_packet_info_send,my_id,oppsite_id,const_id); + send_hb(g_packet_info_send,my_id,oppsite_id,const_id);/////////////send last_hb_sent_time=get_current_time(); } return 0; @@ -2145,7 +2207,7 @@ int keep_connection_server() else { retry_counter--; - send_raw(g_packet_info_send,0,0); + send_bare(g_packet_info_send,0,0); /////////////send last_state_time=get_current_time(); printf("resend syn ack\n"); } @@ -2160,7 +2222,7 @@ int keep_connection_server() else { retry_counter--; - send_hb(g_packet_info_send,my_id,random(),const_id); + send_handshake(g_packet_info_send,my_id,random(),const_id); /////////////send last_state_time=get_current_time(); printf("half heart beat sent<%x>\n",my_id); } @@ -2186,7 +2248,7 @@ int keep_connection_server() return 0; } - send_hb(g_packet_info_send,my_id,oppsite_id,const_id); + send_hb(g_packet_info_send,my_id,oppsite_id,const_id); /////////////send last_hb_sent_time=get_current_time(); if(debug_mode) printf("heart beat sent<%x>\n",my_id); @@ -2223,10 +2285,22 @@ int set_timer(int epollfd,int &timer_fd) return 0; } -int client_on_raw_recv(packet_info_t &info,char * data,int data_len) + +int client_on_raw_recv(packet_info_t &info) { + char* data;int data_len; + + if(client_current_state==client_syn_sent ) { + + + if(recv_bare(info,data,data_len)!=0) + { + return -1; + } + + if (raw_mode==mode_tcp&&!(info.syn==1&&info.ack==1&&data_len==0)) return 0; if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port) @@ -2253,6 +2327,13 @@ int client_on_raw_recv(packet_info_t &info,char * data,int data_len) } if(client_current_state==client_ack_sent ) { + + + if(recv_bare(info,data,data_len)!=0) + { + return -1; + } + if(raw_mode==mode_tcp&& (info.syn==1||info.ack!=1 ||data_len==0)) { printf("unexpected syn ack or other zero lenght packet\n"); @@ -2264,18 +2345,19 @@ int client_on_raw_recv(packet_info_t &info,char * data,int data_len) return 0; } + /* if(data_len