2017-07-29 20:32:26 +08:00
# include "common.h"
# include "network.h"
2017-09-23 02:40:23 -05:00
# include "connection.h"
2017-09-23 03:35:28 -05:00
# include "misc.h"
2017-07-24 21:18:58 +08:00
# include "log.h"
2017-08-11 12:25:04 +08:00
# include "lib/md5.h"
2017-08-22 11:00:44 -05:00
# include "encrypt.h"
2017-10-30 07:21:27 -05:00
# include "fd_manager.h"
2017-07-26 18:00:45 +08:00
2017-08-27 03:31:22 -05:00
int mtu_warn = 1375 ; //if a packet larger than mtu warn is receviced,there will be a warning
2017-09-24 03:14:08 -05:00
2017-08-05 17:31:07 +08:00
int server_on_raw_recv_pre_ready ( conn_info_t & conn_info , char * ip_port , u32_t tmp_oppsite_const_id ) ;
2017-08-08 15:49:11 +08:00
int server_on_raw_recv_ready ( conn_info_t & conn_info , char * ip_port , char type , char * data , int data_len ) ;
2017-08-05 17:31:07 +08:00
int server_on_raw_recv_handshake1 ( conn_info_t & conn_info , char * ip_port , char * data , int data_len ) ;
2017-07-22 01:19:13 +08:00
2017-08-27 03:31:22 -05:00
int client_on_timer ( conn_info_t & conn_info ) //for client. called when a timer is ready in epoll
2017-07-11 18:01:11 +08:00
{
2017-08-20 16:28:23 +08:00
//keep_iptables_rule();
2017-07-29 00:22:26 +08:00
packet_info_t & send_info = conn_info . raw_info . send_info ;
packet_info_t & recv_info = conn_info . raw_info . recv_info ;
raw_info_t & raw_info = conn_info . raw_info ;
2017-08-04 03:29:53 +08:00
conn_info . blob - > conv_manager . clear_inactive ( ) ;
2017-07-26 06:29:40 +08:00
mylog ( log_trace , " timer! \n " ) ;
2017-08-04 03:29:53 +08:00
2017-08-08 15:49:11 +08:00
mylog ( log_trace , " roller my %d,oppsite %d,%lld \n " , int ( conn_info . my_roller ) , int ( conn_info . oppsite_roller ) , conn_info . last_oppsite_roller_time ) ;
2017-08-04 03:29:53 +08:00
mylog ( log_trace , " <client_on_timer,send_info.ts_ack= %u> \n " , send_info . ts_ack ) ;
2017-08-20 16:28:23 +08:00
2017-08-02 18:57:27 +08:00
if ( conn_info . state . client_current_state = = client_idle )
2017-07-11 18:01:11 +08:00
{
2017-07-30 23:18:37 +08:00
fail_time_counter + + ;
2017-08-05 10:03:40 +08:00
if ( max_fail_time > 0 & & fail_time_counter > max_fail_time )
2017-07-30 23:18:37 +08:00
{
2017-08-04 18:35:51 +08:00
mylog ( log_fatal , " max_fail_time exceed \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-30 23:18:37 +08:00
}
2017-08-04 03:29:53 +08:00
conn_info . blob - > anti_replay . re_init ( ) ;
2017-08-02 18:57:27 +08:00
conn_info . my_id = get_true_random_number_nz ( ) ; ///todo no need to do this everytime
2017-07-26 06:29:40 +08:00
2017-08-17 23:40:17 +08:00
u32_t new_ip = 0 ;
2017-09-23 03:05:23 -05:00
if ( ! force_source_ip & & get_src_adress ( new_ip , remote_ip_uint32 , remote_port ) = = 0 )
2017-08-17 23:40:17 +08:00
{
2017-08-18 00:23:18 +08:00
if ( new_ip ! = source_ip_uint32 )
2017-08-17 23:40:17 +08:00
{
2017-08-18 00:50:56 +08:00
mylog ( log_info , " source ip changed from %s to " , my_ntoa ( source_ip_uint32 ) ) ;
2017-08-18 00:23:18 +08:00
log_bare ( log_info , " %s \n " , my_ntoa ( new_ip ) ) ;
source_ip_uint32 = new_ip ;
2017-08-17 23:40:17 +08:00
send_info . src_ip = new_ip ;
}
}
2017-08-02 18:57:27 +08:00
if ( source_port = = 0 )
2017-07-24 10:54:05 +08:00
{
2017-09-23 03:05:23 -05:00
send_info . src_port = client_bind_to_a_new_port ( bind_fd , local_ip_uint32 ) ;
2017-07-24 10:54:05 +08:00
}
2017-07-26 06:29:40 +08:00
else
{
2017-08-02 18:57:27 +08:00
send_info . src_port = source_port ;
2017-07-26 06:29:40 +08:00
}
2017-07-22 16:19:06 +08:00
2017-08-02 18:57:27 +08:00
if ( raw_mode = = mode_icmp )
2017-07-22 16:19:06 +08:00
{
2017-08-02 18:57:27 +08:00
send_info . dst_port = send_info . src_port ;
2017-07-22 16:19:06 +08:00
}
2017-07-17 23:45:24 +08:00
2017-08-02 18:57:27 +08:00
mylog ( log_info , " using port %d \n " , send_info . src_port ) ;
2017-07-28 00:15:10 +08:00
init_filter ( send_info . src_port ) ;
2017-07-11 18:01:11 +08:00
2017-08-02 18:57:27 +08:00
if ( raw_mode = = mode_icmp | | raw_mode = = mode_udp )
2017-07-22 01:19:13 +08:00
{
2017-08-02 18:57:27 +08:00
conn_info . state . client_current_state = client_handshake1 ;
2017-07-31 20:09:45 +08:00
2017-08-02 18:57:27 +08:00
mylog ( log_info , " state changed from client_idle to client_pre_handshake \n " ) ;
2017-07-22 01:19:13 +08:00
}
2017-08-02 18:57:27 +08:00
if ( raw_mode = = mode_faketcp )
2017-07-22 01:19:13 +08:00
{
2017-08-02 18:57:27 +08:00
conn_info . state . client_current_state = client_tcp_handshake ;
mylog ( log_info , " state changed from client_idle to client_tcp_handshake \n " ) ;
2017-08-04 04:22:16 +08:00
2017-07-22 01:19:13 +08:00
}
2017-08-02 18:57:27 +08:00
conn_info . last_state_time = get_current_time ( ) ;
2017-08-04 05:27:47 +08:00
conn_info . last_hb_sent_time = 0 ;
2017-08-02 18:57:27 +08:00
//dont return;
2017-07-11 18:01:11 +08:00
}
2017-08-03 23:32:19 +08:00
if ( conn_info . state . client_current_state = = client_tcp_handshake ) //send and resend syn
2017-07-11 18:01:11 +08:00
{
2017-07-31 19:50:05 +08:00
assert ( raw_mode = = mode_faketcp ) ;
2017-08-02 18:57:27 +08:00
if ( get_current_time ( ) - conn_info . last_state_time > client_handshake_timeout )
{
conn_info . state . client_current_state = client_idle ;
2017-08-03 23:32:19 +08:00
mylog ( log_info , " state back to client_idle from client_tcp_handshake \n " ) ;
2017-08-02 18:57:27 +08:00
return 0 ;
}
2017-08-04 05:27:47 +08:00
else if ( get_current_time ( ) - conn_info . last_hb_sent_time > client_retry_interval )
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
2017-08-03 23:32:19 +08:00
if ( raw_mode = = mode_faketcp )
{
2017-08-04 05:27:47 +08:00
if ( conn_info . last_hb_sent_time = = 0 )
2017-08-03 23:32:19 +08:00
{
2017-08-04 04:22:16 +08:00
send_info . psh = 0 ;
send_info . syn = 1 ;
send_info . ack = 0 ;
send_info . ts_ack = 0 ;
send_info . seq = get_true_random_number ( ) ;
send_info . ack_seq = get_true_random_number ( ) ;
2017-08-03 23:32:19 +08:00
}
}
2017-08-02 18:57:27 +08:00
2017-08-04 03:29:53 +08:00
send_raw0 ( raw_info , 0 , 0 ) ;
2017-08-02 18:57:27 +08:00
2017-08-04 05:27:47 +08:00
conn_info . last_hb_sent_time = get_current_time ( ) ;
2017-08-03 23:32:19 +08:00
mylog ( log_info , " (re)sent tcp syn \n " ) ;
2017-07-20 16:34:43 +08:00
return 0 ;
2017-07-11 18:01:11 +08:00
}
2017-08-02 18:57:27 +08:00
else
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
return 0 ;
2017-07-11 18:01:11 +08:00
}
2017-08-03 23:32:19 +08:00
return 0 ;
2017-07-11 18:01:11 +08:00
}
2017-08-03 23:32:19 +08:00
else if ( conn_info . state . client_current_state = = client_handshake1 ) //send and resend handshake1
2017-07-11 18:01:11 +08:00
{
2017-07-30 18:09:51 +08:00
if ( get_current_time ( ) - conn_info . last_state_time > client_handshake_timeout )
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
conn_info . state . client_current_state = client_idle ;
2017-08-03 23:32:19 +08:00
mylog ( log_info , " state back to client_idle from client_handshake1 \n " ) ;
2017-07-20 16:34:43 +08:00
return 0 ;
2017-07-30 18:09:51 +08:00
2017-07-11 18:01:11 +08:00
}
2017-08-04 05:27:47 +08:00
else if ( get_current_time ( ) - conn_info . last_hb_sent_time > client_retry_interval )
2017-07-11 18:01:11 +08:00
{
2017-08-03 23:32:19 +08:00
2017-07-26 06:29:40 +08:00
if ( raw_mode = = mode_faketcp )
2017-07-22 01:19:13 +08:00
{
2017-08-04 05:27:47 +08:00
if ( conn_info . last_hb_sent_time = = 0 )
2017-08-03 23:32:19 +08:00
{
2017-08-04 03:29:53 +08:00
send_info . seq + + ;
2017-08-03 23:32:19 +08:00
send_info . ack_seq = recv_info . seq + 1 ;
send_info . ts_ack = recv_info . ts ;
2017-09-06 01:37:14 -05:00
raw_info . reserved_send_seq = send_info . seq ;
2017-08-03 23:32:19 +08:00
}
2017-09-06 01:37:14 -05:00
send_info . seq = raw_info . reserved_send_seq ;
2017-08-02 18:57:27 +08:00
send_info . psh = 0 ;
send_info . syn = 0 ;
send_info . ack = 1 ;
2017-08-04 03:29:53 +08:00
send_raw0 ( raw_info , 0 , 0 ) ;
2017-08-03 23:32:19 +08:00
send_handshake ( raw_info , conn_info . my_id , 0 , const_id ) ;
2017-09-06 01:31:29 -05:00
send_info . seq + = raw_info . send_info . data_len ;
2017-07-22 01:19:13 +08:00
}
2017-08-03 23:32:19 +08:00
else
{
2017-08-04 03:29:53 +08:00
send_handshake ( raw_info , conn_info . my_id , 0 , const_id ) ;
2017-08-03 23:32:19 +08:00
if ( raw_mode = = mode_icmp )
send_info . icmp_seq + + ;
}
2017-08-04 05:27:47 +08:00
conn_info . last_hb_sent_time = get_current_time ( ) ;
2017-08-03 23:32:19 +08:00
mylog ( log_info , " (re)sent handshake1 \n " ) ;
2017-08-02 18:57:27 +08:00
return 0 ;
}
else
{
return 0 ;
2017-07-11 18:01:11 +08:00
}
2017-08-03 23:32:19 +08:00
return 0 ;
2017-07-11 18:01:11 +08:00
}
2017-08-03 23:32:19 +08:00
else if ( conn_info . state . client_current_state = = client_handshake2 )
2017-07-19 16:11:12 +08:00
{
2017-07-30 18:09:51 +08:00
if ( get_current_time ( ) - conn_info . last_state_time > client_handshake_timeout )
2017-07-19 16:11:12 +08:00
{
2017-08-02 18:57:27 +08:00
conn_info . state . client_current_state = client_idle ;
2017-08-03 23:32:19 +08:00
mylog ( log_info , " state back to client_idle from client_handshake2 \n " ) ;
2017-08-02 18:57:27 +08:00
return 0 ;
}
2017-08-04 05:27:47 +08:00
else if ( get_current_time ( ) - conn_info . last_hb_sent_time > client_retry_interval )
2017-08-02 18:57:27 +08:00
{
2017-08-03 23:32:19 +08:00
if ( raw_mode = = mode_faketcp )
{
2017-08-04 05:27:47 +08:00
if ( conn_info . last_hb_sent_time = = 0 )
2017-08-03 23:32:19 +08:00
{
2017-09-06 01:31:29 -05:00
send_info . ack_seq = recv_info . seq + raw_info . recv_info . data_len ;
2017-08-03 23:32:19 +08:00
send_info . ts_ack = recv_info . ts ;
2017-09-06 01:37:14 -05:00
raw_info . reserved_send_seq = send_info . seq ;
2017-08-03 23:32:19 +08:00
}
2017-09-06 01:37:14 -05:00
send_info . seq = raw_info . reserved_send_seq ;
2017-08-03 23:32:19 +08:00
send_handshake ( raw_info , conn_info . my_id , conn_info . oppsite_id , const_id ) ;
2017-09-06 01:31:29 -05:00
send_info . seq + = raw_info . send_info . data_len ;
2017-08-03 23:32:19 +08:00
}
else
{
2017-08-04 03:29:53 +08:00
send_handshake ( raw_info , conn_info . my_id , conn_info . oppsite_id , const_id ) ;
2017-08-03 23:32:19 +08:00
if ( raw_mode = = mode_icmp )
send_info . icmp_seq + + ;
}
2017-08-04 05:27:47 +08:00
conn_info . last_hb_sent_time = get_current_time ( ) ;
2017-08-03 23:32:19 +08:00
mylog ( log_info , " (re)sent handshake2 \n " ) ;
2017-07-20 16:34:43 +08:00
return 0 ;
2017-08-03 23:32:19 +08:00
2017-07-19 16:11:12 +08:00
}
else
{
2017-08-02 18:57:27 +08:00
return 0 ;
2017-07-19 16:11:12 +08:00
}
2017-08-03 23:32:19 +08:00
return 0 ;
2017-07-19 16:11:12 +08:00
}
2017-08-03 23:32:19 +08:00
else if ( conn_info . state . client_current_state = = client_ready )
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
fail_time_counter = 0 ;
2017-08-04 17:12:23 +08:00
mylog ( log_trace , " time %llu,%llu \n " , get_current_time ( ) , conn_info . last_state_time ) ;
2017-07-30 05:53:30 +08:00
if ( get_current_time ( ) - conn_info . last_hb_recv_time > client_conn_timeout )
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
conn_info . state . client_current_state = client_idle ;
2017-07-29 00:22:26 +08:00
conn_info . my_id = get_true_random_number_nz ( ) ;
2017-08-13 02:14:50 +08:00
mylog ( log_info , " state back to client_idle from client_ready bc of server-->client direction timeout \n " ) ;
2017-07-11 18:01:11 +08:00
return 0 ;
}
2017-07-19 06:05:08 +08:00
2017-07-29 00:22:26 +08:00
if ( get_current_time ( ) - conn_info . last_hb_sent_time < heartbeat_interval )
2017-07-19 06:05:08 +08:00
{
return 0 ;
}
2017-08-08 15:49:11 +08:00
if ( get_current_time ( ) - conn_info . last_oppsite_roller_time > client_conn_uplink_timeout )
{
conn_info . state . client_current_state = client_idle ;
conn_info . my_id = get_true_random_number_nz ( ) ;
2017-08-13 02:14:50 +08:00
mylog ( log_info , " state back to client_idle from client_ready bc of client-->server direction timeout \n " ) ;
2017-08-08 15:49:11 +08:00
}
2017-08-03 23:32:19 +08:00
mylog ( log_debug , " heartbeat sent <%x,%x> \n " , conn_info . oppsite_id , conn_info . my_id ) ;
2017-07-12 16:46:02 +08:00
2017-08-08 15:49:11 +08:00
send_safer ( conn_info , ' h ' , " " , 0 ) ; /////////////send
2017-07-23 06:24:32 +08:00
2017-07-29 00:22:26 +08:00
conn_info . last_hb_sent_time = get_current_time ( ) ;
2017-08-03 23:32:19 +08:00
return 0 ;
}
else
{
mylog ( log_fatal , " unknown state,this shouldnt happen. \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-11 18:01:11 +08:00
}
2017-07-23 02:57:32 +08:00
return 0 ;
2017-07-11 18:01:11 +08:00
}
2017-08-27 03:31:22 -05:00
int server_on_timer_multi ( conn_info_t & conn_info , char * ip_port ) //for server. called when a timer is ready in epoll.for server,there will be one timer for every connection
2017-07-26 22:58:22 +08:00
{
2017-08-20 16:28:23 +08:00
//keep_iptables_rule();
2017-07-30 03:46:28 +08:00
mylog ( log_trace , " server timer! \n " ) ;
2017-07-28 19:47:30 +08:00
raw_info_t & raw_info = conn_info . raw_info ;
2017-07-30 13:39:18 +08:00
2017-07-30 23:18:37 +08:00
assert ( conn_info . state . server_current_state = = server_ready ) ;
2017-07-27 02:00:31 +08:00
2017-08-02 18:57:27 +08:00
2017-07-29 02:13:42 +08:00
if ( conn_info . state . server_current_state = = server_ready )
2017-07-27 02:00:31 +08:00
{
2017-08-05 17:31:07 +08:00
conn_info . blob - > conv_manager . clear_inactive ( ip_port ) ;
2017-07-29 02:13:42 +08:00
/*
2017-07-29 00:22:26 +08:00
if ( get_current_time ( ) - conn_info . last_hb_recv_time > heartbeat_timeout )
2017-07-27 02:00:31 +08:00
{
2017-07-29 00:22:26 +08:00
mylog ( log_trace , " %lld %lld \n " , get_current_time ( ) , conn_info . last_state_time ) ;
conn_info . server_current_state = server_nothing ;
2017-07-27 02:00:31 +08:00
2017-07-29 00:22:26 +08:00
//conn_manager.current_ready_ip=0;
//conn_manager.current_ready_port=0;
2017-07-28 00:15:10 +08:00
2017-07-27 02:00:31 +08:00
mylog ( log_info , " changed state to server_nothing \n " ) ;
return 0 ;
2017-07-29 02:13:42 +08:00
} */ //dont need to do this at server,conn_manger will clear expired connections
2017-07-27 02:00:31 +08:00
2017-07-29 00:22:26 +08:00
if ( get_current_time ( ) - conn_info . last_hb_sent_time < heartbeat_interval )
2017-07-27 02:00:31 +08:00
{
return 0 ;
}
2017-08-08 15:49:11 +08:00
send_safer ( conn_info , ' h ' , " " , 0 ) ; /////////////send
2017-07-27 02:00:31 +08:00
2017-07-29 00:22:26 +08:00
conn_info . last_hb_sent_time = get_current_time ( ) ;
2017-07-27 02:00:31 +08:00
2017-08-04 04:22:16 +08:00
mylog ( log_debug , " heart beat sent<%x,%x> \n " , conn_info . my_id , conn_info . oppsite_id ) ;
2017-07-27 02:00:31 +08:00
}
2017-07-30 16:37:49 +08:00
else
{
mylog ( log_fatal , " this shouldnt happen! \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-30 16:37:49 +08:00
}
2017-07-28 00:15:10 +08:00
return 0 ;
2017-07-27 02:00:31 +08:00
2017-07-26 22:58:22 +08:00
}
2017-08-27 03:31:22 -05:00
int client_on_raw_recv ( conn_info_t & conn_info ) //called when raw fd received a packet.
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
char * data ; int data_len ;
packet_info_t & send_info = conn_info . raw_info . send_info ;
packet_info_t & recv_info = conn_info . raw_info . recv_info ;
raw_info_t & raw_info = conn_info . raw_info ;
2017-08-04 03:29:53 +08:00
mylog ( log_trace , " <client_on_raw_recv,send_info.ts_ack= %u> \n " , send_info . ts_ack ) ;
2017-08-02 18:57:27 +08:00
if ( conn_info . state . client_current_state = = client_idle )
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
recv ( raw_recv_fd , 0 , 0 , 0 ) ;
2017-07-11 18:01:11 +08:00
}
2017-08-03 23:32:19 +08:00
else if ( conn_info . state . client_current_state = = client_tcp_handshake ) //received syn ack
2017-07-11 18:01:11 +08:00
{
2017-08-03 23:32:19 +08:00
assert ( raw_mode = = mode_faketcp ) ;
2017-08-04 03:29:53 +08:00
if ( recv_raw0 ( raw_info , data , data_len ) < 0 )
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
return - 1 ;
}
if ( recv_info . src_ip ! = send_info . dst_ip | | recv_info . src_port ! = send_info . dst_port )
{
mylog ( log_debug , " unexpected adress %x %x %d %d \n " , recv_info . src_ip , send_info . dst_ip , recv_info . src_port , send_info . dst_port ) ;
return - 1 ;
}
if ( data_len = = 0 & & raw_info . recv_info . syn = = 1 & & raw_info . recv_info . ack = = 1 )
{
2017-08-03 23:32:19 +08:00
if ( recv_info . ack_seq ! = send_info . seq + 1 )
2017-08-02 18:57:27 +08:00
{
mylog ( log_debug , " seq ack_seq mis match \n " ) ;
return - 1 ;
}
conn_info . state . client_current_state = client_handshake1 ;
2017-08-03 23:32:19 +08:00
mylog ( log_info , " state changed from client_tcp_handshake to client_handshake1 \n " ) ;
2017-08-02 18:57:27 +08:00
conn_info . last_state_time = get_current_time ( ) ;
2017-08-04 05:27:47 +08:00
conn_info . last_hb_sent_time = 0 ;
2017-08-02 20:54:32 +08:00
client_on_timer ( conn_info ) ;
2017-08-02 18:57:27 +08:00
return 0 ;
2017-07-11 18:01:11 +08:00
}
else
{
2017-08-02 18:57:27 +08:00
mylog ( log_debug , " unexpected packet type,expected:syn ack \n " ) ;
return - 1 ;
2017-07-11 18:01:11 +08:00
}
}
2017-08-03 23:32:19 +08:00
else if ( conn_info . state . client_current_state = = client_handshake1 ) //recevied respond of handshake1
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
if ( recv_bare ( raw_info , data , data_len ) ! = 0 )
{
mylog ( log_debug , " recv_bare failed! \n " ) ;
return - 1 ;
}
if ( recv_info . src_ip ! = send_info . dst_ip | | recv_info . src_port ! = send_info . dst_port )
2017-07-19 16:11:12 +08:00
{
2017-08-02 18:57:27 +08:00
mylog ( log_debug , " unexpected adress %x %x %d %d \n " , recv_info . src_ip , send_info . dst_ip , recv_info . src_port , send_info . dst_port ) ;
return - 1 ;
2017-07-19 16:11:12 +08:00
}
2017-08-02 18:57:27 +08:00
if ( data_len < int ( 3 * sizeof ( id_t ) ) )
2017-07-19 16:11:12 +08:00
{
2017-08-02 18:57:27 +08:00
mylog ( log_debug , " too short to be a handshake \n " ) ;
return - 1 ;
2017-07-19 16:11:12 +08:00
}
2017-08-31 10:36:33 -05:00
//id_t tmp_oppsite_id= ntohl(* ((u32_t *)&data[0]));
id_t tmp_oppsite_id ;
memcpy ( & tmp_oppsite_id , & data [ 0 ] , sizeof ( tmp_oppsite_id ) ) ;
tmp_oppsite_id = ntohl ( tmp_oppsite_id ) ;
//id_t tmp_my_id=ntohl(* ((u32_t *)&data[sizeof(id_t)]));
id_t tmp_my_id ;
memcpy ( & tmp_my_id , & data [ sizeof ( id_t ) ] , sizeof ( tmp_my_id ) ) ;
tmp_my_id = ntohl ( tmp_my_id ) ;
//id_t tmp_oppsite_const_id=ntohl(* ((u32_t *)&data[sizeof(id_t)*2]));
id_t tmp_oppsite_const_id ;
memcpy ( & tmp_oppsite_const_id , & data [ sizeof ( id_t ) * 2 ] , sizeof ( tmp_oppsite_const_id ) ) ;
tmp_oppsite_const_id = ntohl ( tmp_oppsite_const_id ) ;
2017-07-19 16:11:12 +08:00
2017-08-02 18:57:27 +08:00
if ( tmp_my_id ! = conn_info . my_id )
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
mylog ( log_debug , " tmp_my_id doesnt match \n " ) ;
return - 1 ;
}
2017-08-03 23:32:19 +08:00
2017-08-04 03:29:53 +08:00
if ( raw_mode = = mode_faketcp )
{
if ( recv_info . ack_seq ! = send_info . seq )
{
mylog ( log_debug , " seq ack_seq mis match \n " ) ;
return - 1 ;
}
if ( recv_info . seq ! = send_info . ack_seq )
{
mylog ( log_debug , " seq ack_seq mis match \n " ) ;
return - 1 ;
}
}
2017-08-02 18:57:27 +08:00
conn_info . oppsite_id = tmp_oppsite_id ;
2017-07-19 00:27:20 +08:00
2017-08-03 23:32:19 +08:00
mylog ( log_info , " changed state from to client_handshake1 to client_handshake2,my_id is %x,oppsite id is %x \n " , conn_info . my_id , conn_info . oppsite_id ) ;
2017-07-19 16:11:12 +08:00
2017-08-02 18:57:27 +08:00
//send_handshake(raw_info,conn_info.my_id,conn_info.oppsite_id,const_id); //////////////send
conn_info . state . client_current_state = client_handshake2 ;
conn_info . last_state_time = get_current_time ( ) ;
2017-08-04 05:27:47 +08:00
conn_info . last_hb_sent_time = 0 ;
2017-08-02 20:54:32 +08:00
client_on_timer ( conn_info ) ;
2017-08-02 18:57:27 +08:00
return 0 ;
}
2017-08-03 23:32:19 +08:00
else if ( conn_info . state . client_current_state = = client_handshake2 | | conn_info . state . client_current_state = = client_ready ) //received heartbeat or data
2017-08-02 18:57:27 +08:00
{
2017-08-08 15:49:11 +08:00
char type ;
if ( recv_safer ( conn_info , type , data , data_len ) ! = 0 )
2017-08-02 18:57:27 +08:00
{
mylog ( log_debug , " recv_safer failed! \n " ) ;
return - 1 ;
}
if ( recv_info . src_ip ! = send_info . dst_ip | | recv_info . src_port ! = send_info . dst_port )
{
mylog ( log_warn , " unexpected adress %x %x %d %d,this shouldnt happen. \n " , recv_info . src_ip , send_info . dst_ip , recv_info . src_port , send_info . dst_port ) ;
return - 1 ;
2017-07-11 18:01:11 +08:00
}
2017-08-02 18:57:27 +08:00
if ( conn_info . state . client_current_state = = client_handshake2 )
2017-08-03 23:32:19 +08:00
{
mylog ( log_info , " changed state from to client_handshake2 to client_ready \n " ) ;
2017-08-02 18:57:27 +08:00
conn_info . state . client_current_state = client_ready ;
2017-08-04 05:27:47 +08:00
conn_info . last_hb_sent_time = 0 ;
conn_info . last_hb_recv_time = get_current_time ( ) ;
2017-08-08 15:49:11 +08:00
conn_info . last_oppsite_roller_time = conn_info . last_hb_recv_time ;
2017-08-04 05:27:47 +08:00
client_on_timer ( conn_info ) ;
2017-08-03 23:32:19 +08:00
}
2017-08-08 15:49:11 +08:00
if ( data_len = = 0 & & type = = ' h ' )
2017-07-19 06:05:08 +08:00
{
2017-08-02 18:57:27 +08:00
mylog ( log_debug , " [hb]heart beat received \n " ) ;
conn_info . last_hb_recv_time = get_current_time ( ) ;
2017-07-19 06:05:08 +08:00
return 0 ;
}
2017-08-08 15:49:11 +08:00
else if ( data_len > = int ( sizeof ( u32_t ) ) & & type = = ' d ' )
2017-08-02 18:57:27 +08:00
{
mylog ( log_trace , " received a data from fake tcp,len:%d \n " , data_len ) ;
2017-07-19 06:05:08 +08:00
2017-08-02 18:57:27 +08:00
conn_info . last_hb_recv_time = get_current_time ( ) ;
2017-07-23 06:24:32 +08:00
2017-08-31 10:36:33 -05:00
//u32_t tmp_conv_id= ntohl(* ((u32_t *)&data[0]));
u32_t tmp_conv_id ;
memcpy ( & tmp_conv_id , & data [ 0 ] , sizeof ( tmp_conv_id ) ) ;
tmp_conv_id = ntohl ( tmp_conv_id ) ;
2017-07-19 06:05:08 +08:00
2017-08-04 03:29:53 +08:00
if ( ! conn_info . blob - > conv_manager . is_conv_used ( tmp_conv_id ) )
2017-08-02 18:57:27 +08:00
{
mylog ( log_info , " unknow conv %d,ignore \n " , tmp_conv_id ) ;
return 0 ;
}
2017-07-11 18:01:11 +08:00
2017-08-04 03:29:53 +08:00
conn_info . blob - > conv_manager . update_active_time ( tmp_conv_id ) ;
2017-07-11 18:01:11 +08:00
2017-08-04 17:12:23 +08:00
u64_t u64 = conn_info . blob - > conv_manager . find_u64_by_conv ( tmp_conv_id ) ;
2017-07-11 18:01:11 +08:00
2017-08-15 13:03:22 +08:00
sockaddr_in tmp_sockaddr = { 0 } ;
2017-07-11 18:01:11 +08:00
2017-08-02 18:57:27 +08:00
tmp_sockaddr . sin_family = AF_INET ;
tmp_sockaddr . sin_addr . s_addr = ( u64 > > 32u ) ;
2017-07-11 18:01:11 +08:00
2017-08-02 18:57:27 +08:00
tmp_sockaddr . sin_port = htons ( uint16_t ( ( u64 < < 32u ) > > 32u ) ) ;
2017-07-11 18:01:11 +08:00
2017-07-28 19:47:30 +08:00
2017-08-08 15:49:11 +08:00
int ret = sendto ( udp_fd , data + sizeof ( u32_t ) , data_len - ( sizeof ( u32_t ) ) , 0 , ( struct sockaddr * ) & tmp_sockaddr , sizeof ( tmp_sockaddr ) ) ;
2017-07-28 19:47:30 +08:00
2017-08-02 18:57:27 +08:00
if ( ret < 0 )
{
mylog ( log_warn , " sento returned %d \n " , ret ) ;
//perror("ret<0");
}
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 ) ;
}
else
{
mylog ( log_warn , " unknown packet,this shouldnt happen. \n " ) ;
return - 1 ;
}
2017-07-28 19:47:30 +08:00
2017-08-02 18:57:27 +08:00
return 0 ;
}
else
{
2017-08-03 23:32:19 +08:00
mylog ( log_fatal , " unknown state,this shouldnt happen. \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-28 19:47:30 +08:00
}
return 0 ;
}
2017-08-21 20:26:55 +08:00
2017-08-27 03:31:22 -05:00
int server_on_raw_recv_multi ( ) //called when server received an raw packet
2017-08-02 18:57:27 +08:00
{
char dummy_buf [ buf_len ] ;
2017-08-03 23:32:19 +08:00
packet_info_t peek_info ;
if ( peek_raw ( peek_info ) < 0 )
2017-07-30 05:53:30 +08:00
{
2017-08-02 18:57:27 +08:00
recv ( raw_recv_fd , 0 , 0 , 0 ) ; //
//struct sockaddr saddr;
2017-08-13 08:18:23 +08:00
//socklen_t saddr_size=sizeof(saddr);
2017-08-02 18:57:27 +08:00
///recvfrom(raw_recv_fd, 0,0, 0 ,&saddr , &saddr_size);//
mylog ( log_trace , " peek_raw failed \n " ) ;
return - 1 ;
2017-08-13 08:18:23 +08:00
} else
{
mylog ( log_trace , " peek_raw success \n " ) ;
2017-07-30 05:53:30 +08:00
}
2017-08-04 17:12:23 +08:00
u32_t ip = peek_info . src_ip ; uint16_t port = peek_info . src_port ;
2017-08-05 17:31:07 +08:00
2017-08-02 18:57:27 +08:00
char ip_port [ 40 ] ;
sprintf ( ip_port , " %s:%d " , my_ntoa ( ip ) , port ) ;
2017-08-05 20:44:48 +08:00
mylog ( log_trace , " [%s]peek_raw \n " , ip_port ) ;
2017-08-02 18:57:27 +08:00
int data_len ; char * data ;
2017-07-23 04:17:06 +08:00
2017-08-03 23:32:19 +08:00
if ( raw_mode = = mode_faketcp & & peek_info . syn = = 1 )
2017-08-02 18:57:27 +08:00
{
2017-08-03 23:32:19 +08:00
if ( ! conn_manager . exist ( ip , port ) | | conn_manager . find_insert ( ip , port ) . state . server_current_state ! = server_ready )
2017-08-05 10:03:40 +08:00
{ //reply any syn ,before state become ready
2017-08-03 23:32:19 +08:00
raw_info_t tmp_raw_info ;
2017-08-04 03:29:53 +08:00
if ( recv_raw0 ( tmp_raw_info , data , data_len ) < 0 )
2017-08-03 23:32:19 +08:00
{
return 0 ;
}
2017-08-02 18:57:27 +08:00
raw_info_t & raw_info = tmp_raw_info ;
packet_info_t & send_info = raw_info . send_info ;
packet_info_t & recv_info = raw_info . recv_info ;
2017-07-31 19:50:05 +08:00
2017-08-02 18:57:27 +08:00
send_info . src_ip = recv_info . dst_ip ;
send_info . src_port = recv_info . dst_port ;
2017-07-31 19:50:05 +08:00
2017-08-02 18:57:27 +08:00
send_info . dst_port = recv_info . src_port ;
send_info . dst_ip = recv_info . src_ip ;
2017-08-21 20:26:55 +08:00
if ( lower_level )
{
handle_lower_level ( raw_info ) ;
}
2017-08-03 23:32:19 +08:00
if ( data_len = = 0 & & raw_info . recv_info . syn = = 1 & & raw_info . recv_info . ack = = 0 )
2017-08-02 18:57:27 +08:00
{
2017-08-03 23:32:19 +08:00
send_info . ack_seq = recv_info . seq + 1 ;
2017-07-19 00:27:20 +08:00
2017-08-03 23:32:19 +08:00
send_info . psh = 0 ;
send_info . syn = 1 ;
send_info . ack = 1 ;
2017-08-04 04:22:16 +08:00
send_info . ts_ack = recv_info . ts ;
2017-08-02 18:57:27 +08:00
2017-08-05 17:31:07 +08:00
mylog ( log_info , " [%s]received syn,sent syn ack back \n " , ip_port ) ;
2017-08-04 03:29:53 +08:00
send_raw0 ( raw_info , 0 , 0 ) ;
2017-08-03 23:32:19 +08:00
return 0 ;
2017-08-02 18:57:27 +08:00
}
2017-07-23 04:17:06 +08:00
}
2017-09-11 08:52:39 -05:00
else
{
recv ( raw_recv_fd , 0 , 0 , 0 ) ;
}
2017-08-03 23:32:19 +08:00
return 0 ;
}
if ( ! conn_manager . exist ( ip , port ) )
{
if ( conn_manager . mp . size ( ) > = max_handshake_conn_num )
{
mylog ( log_info , " [%s]reached max_handshake_conn_num,ignored new handshake \n " , ip_port ) ;
recv ( raw_recv_fd , 0 , 0 , 0 ) ; //
return 0 ;
}
2017-07-23 04:17:06 +08:00
2017-08-03 23:32:19 +08:00
raw_info_t tmp_raw_info ;
2017-08-02 18:57:27 +08:00
2017-08-04 03:29:53 +08:00
if ( raw_mode = = mode_icmp )
{
tmp_raw_info . send_info . dst_port = tmp_raw_info . send_info . src_port = port ;
}
2017-08-03 23:32:19 +08:00
if ( recv_bare ( tmp_raw_info , data , data_len ) < 0 )
2017-07-31 21:30:05 +08:00
{
2017-08-03 23:32:19 +08:00
return 0 ;
2017-07-31 21:30:05 +08:00
}
2017-08-02 18:57:27 +08:00
if ( data_len < int ( 3 * sizeof ( id_t ) ) )
2017-07-12 16:46:02 +08:00
{
2017-08-02 18:57:27 +08:00
mylog ( log_debug , " [%s]too short to be a handshake \n " , ip_port ) ;
return - 1 ;
2017-07-12 16:46:02 +08:00
}
2017-07-21 20:30:27 +08:00
2017-08-31 10:36:33 -05:00
//id_t zero=ntohl(* ((u32_t *)&data[sizeof(id_t)]));
id_t zero ;
memcpy ( & zero , & data [ sizeof ( id_t ) ] , sizeof ( zero ) ) ;
zero = ntohl ( zero ) ;
2017-08-02 18:57:27 +08:00
if ( zero ! = 0 )
2017-07-11 18:01:11 +08:00
{
2017-08-02 18:57:27 +08:00
mylog ( log_debug , " [%s]not a invalid initial handshake \n " , ip_port ) ;
return - 1 ;
}
2017-07-12 16:46:02 +08:00
2017-08-02 18:57:27 +08:00
mylog ( log_info , " [%s]got packet from a new ip \n " , ip_port ) ;
2017-07-28 00:15:10 +08:00
2017-08-02 18:57:27 +08:00
conn_info_t & conn_info = conn_manager . find_insert ( ip , port ) ;
conn_info . raw_info = tmp_raw_info ;
2017-07-12 16:46:02 +08:00
2017-11-01 08:25:50 -05:00
//conn_info.ip_port.ip=ip;
//conn_info.ip_port.port=port;
2017-10-30 07:21:27 -05:00
2017-08-02 18:57:27 +08:00
packet_info_t & send_info = conn_info . raw_info . send_info ;
packet_info_t & recv_info = conn_info . raw_info . recv_info ;
raw_info_t & raw_info = conn_info . raw_info ;
2017-07-23 04:17:06 +08:00
2017-08-03 23:32:19 +08:00
send_info . src_ip = recv_info . dst_ip ;
send_info . src_port = recv_info . dst_port ;
send_info . dst_port = recv_info . src_port ;
send_info . dst_ip = recv_info . src_ip ;
2017-08-21 20:26:55 +08:00
if ( lower_level )
{
handle_lower_level ( raw_info ) ;
}
2017-08-05 10:03:40 +08:00
//id_t tmp_oppsite_id= ntohl(* ((u32_t *)&data[0]));
//mylog(log_info,"[%s]handshake1 received %x\n",ip_port,tmp_oppsite_id);
2017-07-23 04:17:06 +08:00
2017-08-02 18:57:27 +08:00
conn_info . my_id = get_true_random_number_nz ( ) ;
2017-08-02 20:54:32 +08:00
2017-08-02 19:10:38 +08:00
2017-08-03 23:32:19 +08:00
mylog ( log_info , " [%s]created new conn,state: server_handshake1,my_id is %x \n " , ip_port , conn_info . my_id ) ;
2017-08-02 20:54:32 +08:00
2017-08-02 18:57:27 +08:00
conn_info . state . server_current_state = server_handshake1 ;
conn_info . last_state_time = get_current_time ( ) ;
2017-08-03 23:32:19 +08:00
2017-08-05 17:31:07 +08:00
server_on_raw_recv_handshake1 ( conn_info , ip_port , data , data_len ) ;
2017-08-02 18:57:27 +08:00
return 0 ;
2017-07-11 18:01:11 +08:00
}
2017-07-19 16:11:12 +08:00
2017-07-23 04:17:06 +08:00
2017-08-02 18:57:27 +08:00
conn_info_t & conn_info = conn_manager . find_insert ( ip , port ) ; //insert if not exist
packet_info_t & send_info = conn_info . raw_info . send_info ;
packet_info_t & recv_info = conn_info . raw_info . recv_info ;
raw_info_t & raw_info = conn_info . raw_info ;
2017-07-23 04:17:06 +08:00
2017-08-02 18:57:27 +08:00
if ( conn_info . state . server_current_state = = server_handshake1 )
{
2017-08-05 10:03:40 +08:00
if ( recv_bare ( raw_info , data , data_len ) ! = 0 )
2017-07-12 16:46:02 +08:00
{
2017-08-02 18:57:27 +08:00
return - 1 ;
2017-07-12 16:46:02 +08:00
}
2017-09-11 08:52:39 -05:00
return server_on_raw_recv_handshake1 ( conn_info , ip_port , data , data_len ) ;
2017-07-12 16:46:02 +08:00
}
2017-08-02 18:57:27 +08:00
if ( conn_info . state . server_current_state = = server_ready )
{
2017-08-08 15:49:11 +08:00
char type ;
2017-08-13 08:18:23 +08:00
//mylog(log_info,"before recv_safer\n");
2017-08-08 15:49:11 +08:00
if ( recv_safer ( conn_info , type , data , data_len ) ! = 0 ) {
2017-08-05 10:03:40 +08:00
return - 1 ;
}
2017-08-13 08:18:23 +08:00
//mylog(log_info,"after recv_safer\n");
2017-08-08 15:49:11 +08:00
return server_on_raw_recv_ready ( conn_info , ip_port , type , data , data_len ) ;
2017-08-02 18:57:27 +08:00
}
2017-09-11 09:16:39 -05:00
if ( conn_info . state . server_current_state = = server_idle )
{
recv ( raw_recv_fd , 0 , 0 , 0 ) ; //
return 0 ;
}
2017-09-11 08:52:39 -05:00
mylog ( log_fatal , " we should never run to here \n " ) ;
myexit ( - 1 ) ;
return - 1 ;
2017-07-11 18:01:11 +08:00
}
2017-08-02 18:57:27 +08:00
2017-08-05 10:03:40 +08:00
/*
2017-08-03 23:32:19 +08:00
int server_on_raw_recv_handshake1 ( conn_info_t & conn_info , id_t tmp_oppsite_id )
{
packet_info_t & send_info = conn_info . raw_info . send_info ;
packet_info_t & recv_info = conn_info . raw_info . recv_info ;
raw_info_t & raw_info = conn_info . raw_info ;
2017-08-05 10:03:40 +08:00
2017-08-04 03:29:53 +08:00
2017-08-03 23:32:19 +08:00
return 0 ;
2017-08-05 10:03:40 +08:00
} */
2017-08-27 03:31:22 -05:00
int server_on_raw_recv_handshake1 ( conn_info_t & conn_info , char * ip_port , char * data , int data_len ) //called when server received a handshake1 packet from client
2017-08-05 17:31:07 +08:00
{
packet_info_t & send_info = conn_info . raw_info . send_info ;
packet_info_t & recv_info = conn_info . raw_info . recv_info ;
raw_info_t & raw_info = conn_info . raw_info ;
//u32_t ip=conn_info.raw_info.recv_info.src_ip;
//uint16_t port=conn_info.raw_info.recv_info.src_port;
//char ip_port[40];
//sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
if ( data_len < int ( 3 * sizeof ( id_t ) ) )
{
mylog ( log_debug , " [%s] data_len=%d too short to be a handshake \n " , ip_port , data_len ) ;
return - 1 ;
}
2017-08-31 10:36:33 -05:00
//id_t tmp_oppsite_id= ntohl(* ((u32_t *)&data[0]));
id_t tmp_oppsite_id ;
memcpy ( & tmp_oppsite_id , ( u32_t * ) & data [ 0 ] , sizeof ( tmp_oppsite_id ) ) ;
tmp_oppsite_id = ntohl ( tmp_oppsite_id ) ;
//id_t tmp_my_id=ntohl(* ((u32_t *)&data[sizeof(id_t)]));
id_t tmp_my_id ;
memcpy ( & tmp_my_id , & data [ sizeof ( id_t ) ] , sizeof ( tmp_my_id ) ) ;
tmp_my_id = ntohl ( tmp_my_id ) ;
2017-08-05 17:31:07 +08:00
if ( tmp_my_id = = 0 ) //received init handshake again
{
if ( raw_mode = = mode_faketcp )
{
send_info . seq = recv_info . ack_seq ;
2017-09-06 01:31:29 -05:00
send_info . ack_seq = recv_info . seq + raw_info . recv_info . data_len ;
2017-08-05 17:31:07 +08:00
send_info . ts_ack = recv_info . ts ;
}
if ( raw_mode = = mode_icmp )
{
send_info . icmp_seq = recv_info . icmp_seq ;
}
send_handshake ( raw_info , conn_info . my_id , tmp_oppsite_id , const_id ) ; //////////////send
mylog ( log_info , " [%s]changed state to server_handshake1,my_id is %x \n " , ip_port , conn_info . my_id ) ;
}
else if ( tmp_my_id = = conn_info . my_id )
{
conn_info . oppsite_id = tmp_oppsite_id ;
2017-08-31 10:36:33 -05:00
//id_t tmp_oppsite_const_id=ntohl(* ((u32_t *)&data[sizeof(id_t)*2]));
id_t tmp_oppsite_const_id ;
memcpy ( & tmp_oppsite_const_id , & data [ sizeof ( id_t ) * 2 ] , sizeof ( tmp_oppsite_const_id ) ) ;
tmp_oppsite_const_id = ntohl ( tmp_oppsite_const_id ) ;
2017-08-05 17:31:07 +08:00
if ( raw_mode = = mode_faketcp )
{
send_info . seq = recv_info . ack_seq ;
2017-09-06 01:31:29 -05:00
send_info . ack_seq = recv_info . seq + raw_info . recv_info . data_len ;
2017-08-05 17:31:07 +08:00
send_info . ts_ack = recv_info . ts ;
}
if ( raw_mode = = mode_icmp )
{
send_info . icmp_seq = recv_info . icmp_seq ;
}
server_on_raw_recv_pre_ready ( conn_info , ip_port , tmp_oppsite_const_id ) ;
}
else
{
mylog ( log_debug , " [%s]invalid my_id %x,my_id is %x \n " , ip_port , tmp_my_id , conn_info . my_id ) ;
}
return 0 ;
}
2017-08-27 03:31:22 -05:00
int server_on_raw_recv_ready ( conn_info_t & conn_info , char * ip_port , char type , char * data , int data_len ) //called while the state for a connection is server_ready
//receives data and heart beat by recv_safer.
2017-07-11 18:01:11 +08:00
{
2017-07-30 13:39:18 +08:00
2017-07-28 19:47:30 +08:00
raw_info_t & raw_info = conn_info . raw_info ;
packet_info_t & send_info = conn_info . raw_info . send_info ;
packet_info_t & recv_info = conn_info . raw_info . recv_info ;
2017-08-05 17:31:07 +08:00
//char ip_port[40];
2017-07-27 02:00:31 +08:00
2017-08-05 17:31:07 +08:00
//sprintf(ip_port,"%s:%d",my_ntoa(recv_info.src_ip),recv_info.src_port);
2017-08-02 19:10:38 +08:00
2017-07-31 09:25:59 +08:00
2017-08-05 17:31:07 +08:00
/*
2017-07-28 00:15:10 +08:00
if ( recv_info . src_ip ! = send_info . dst_ip
| | recv_info . src_port ! = send_info . dst_port ) {
mylog ( log_debug , " unexpected adress \n " ) ;
return 0 ;
2017-08-05 17:31:07 +08:00
} */
2017-07-27 02:00:31 +08:00
2017-08-08 15:49:11 +08:00
if ( type = = ' h ' & & data_len = = 0 ) {
//u32_t tmp = ntohl(*((u32_t *) &data[sizeof(u32_t)]));
2017-07-30 13:39:18 +08:00
mylog ( log_debug , " [%s][hb]received hb \n " , ip_port ) ;
2017-08-02 18:57:27 +08:00
conn_info . last_hb_recv_time = get_current_time ( ) ;
2017-07-28 00:15:10 +08:00
return 0 ;
2017-08-08 15:49:11 +08:00
} else if ( type = = ' d ' & & data_len > = int ( sizeof ( u32_t ) ) )
2017-07-31 13:26:07 +08:00
{
2017-08-31 10:36:33 -05:00
//u32_t tmp_conv_id = ntohl(*((u32_t *) &data[0]));
id_t tmp_conv_id ;
memcpy ( & tmp_conv_id , & data [ 0 ] , sizeof ( tmp_conv_id ) ) ;
tmp_conv_id = ntohl ( tmp_conv_id ) ;
2017-07-27 02:00:31 +08:00
2017-08-02 18:57:27 +08:00
conn_info . last_hb_recv_time = get_current_time ( ) ;
2017-07-27 02:00:31 +08:00
2017-07-30 16:37:49 +08:00
mylog ( log_trace , " conv:%u \n " , tmp_conv_id ) ;
2017-08-04 03:29:53 +08:00
if ( ! conn_info . blob - > conv_manager . is_conv_used ( tmp_conv_id ) ) {
if ( conn_info . blob - > conv_manager . get_size ( ) > = max_conv_num ) {
2017-07-28 00:15:10 +08:00
mylog ( log_warn ,
2017-08-05 17:31:07 +08:00
" [%s]ignored new conv %x connect bc max_conv_num exceed \n " , ip_port ,
2017-07-28 00:15:10 +08:00
tmp_conv_id ) ;
return 0 ;
}
2017-08-15 13:03:22 +08:00
struct sockaddr_in remote_addr_in = { 0 } ;
2017-07-27 02:00:31 +08:00
2017-07-28 00:15:10 +08:00
socklen_t slen = sizeof ( sockaddr_in ) ;
2017-08-15 13:03:22 +08:00
//memset(&remote_addr_in, 0, sizeof(remote_addr_in));
2017-07-28 00:15:10 +08:00
remote_addr_in . sin_family = AF_INET ;
remote_addr_in . sin_port = htons ( remote_port ) ;
2017-08-18 00:23:18 +08:00
remote_addr_in . sin_addr . s_addr = remote_ip_uint32 ;
2017-07-27 02:00:31 +08:00
2017-07-28 00:15:10 +08:00
int new_udp_fd = socket ( AF_INET , SOCK_DGRAM , IPPROTO_UDP ) ;
if ( new_udp_fd < 0 ) {
2017-08-05 17:31:07 +08:00
mylog ( log_warn , " [%s]create udp_fd error \n " , ip_port ) ;
2017-07-28 00:15:10 +08:00
return - 1 ;
}
2017-07-28 11:31:37 +08:00
setnonblocking ( new_udp_fd ) ;
2017-09-24 03:14:08 -05:00
set_buf_size ( new_udp_fd , socket_buf_size , force_socket_buf ) ;
2017-07-28 00:15:10 +08:00
2017-08-05 17:31:07 +08:00
mylog ( log_debug , " [%s]created new udp_fd %d \n " , ip_port , new_udp_fd ) ;
2017-07-28 00:15:10 +08:00
int ret = connect ( new_udp_fd , ( struct sockaddr * ) & remote_addr_in ,
slen ) ;
if ( ret ! = 0 ) {
mylog ( log_warn , " udp fd connect fail \n " ) ;
close ( new_udp_fd ) ;
return - 1 ;
}
struct epoll_event ev ;
2017-07-27 02:00:31 +08:00
2017-10-30 07:21:27 -05:00
fd64_t new_udp_fd64 = fd_manager . create ( new_udp_fd ) ;
2017-11-01 08:25:50 -05:00
fd_manager . get_info ( new_udp_fd64 ) . p_conn_info = & conn_info ;
2017-10-30 07:21:27 -05:00
mylog ( log_trace , " [%s]u64: %lld \n " , ip_port , new_udp_fd64 ) ;
2017-07-28 00:15:10 +08:00
ev . events = EPOLLIN ;
2017-07-27 02:00:31 +08:00
2017-10-30 07:21:27 -05:00
ev . data . u64 = new_udp_fd64 ;
2017-07-27 02:00:31 +08:00
2017-07-28 00:15:10 +08:00
ret = epoll_ctl ( epollfd , EPOLL_CTL_ADD , new_udp_fd , & ev ) ;
2017-07-27 02:00:31 +08:00
2017-07-28 00:15:10 +08:00
if ( ret ! = 0 ) {
2017-08-05 17:31:07 +08:00
mylog ( log_warn , " [%s]add udp_fd error \n " , ip_port ) ;
2017-07-28 00:15:10 +08:00
close ( new_udp_fd ) ;
return - 1 ;
}
2017-07-27 02:00:31 +08:00
2017-10-30 07:21:27 -05:00
conn_info . blob - > conv_manager . insert_conv ( tmp_conv_id , new_udp_fd64 ) ;
2017-10-30 08:02:23 -05:00
2017-07-31 19:01:50 +08:00
2017-10-30 07:21:27 -05:00
//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;
2017-07-29 02:13:42 +08:00
//pack_u64(conn_info.raw_info.recv_info.src_ip,conn_info.raw_info.recv_info.src_port);
2017-07-27 02:00:31 +08:00
2017-08-04 23:08:45 +08:00
mylog ( log_info , " [%s]new conv conv_id=%x, assigned fd=%d \n " , ip_port ,
2017-07-28 00:15:10 +08:00
tmp_conv_id , new_udp_fd ) ;
2017-07-27 02:00:31 +08:00
2017-07-28 11:31:37 +08:00
2017-07-28 00:15:10 +08:00
}
2017-07-27 02:00:31 +08:00
2017-10-30 08:02:23 -05:00
fd64_t fd64 = conn_info . blob - > conv_manager . find_u64_by_conv ( tmp_conv_id ) ;
2017-07-27 02:00:31 +08:00
2017-08-04 03:29:53 +08:00
conn_info . blob - > conv_manager . update_active_time ( tmp_conv_id ) ;
2017-07-27 02:00:31 +08:00
2017-10-30 08:02:23 -05:00
int fd = fd_manager . to_fd ( fd64 ) ;
2017-07-27 02:00:31 +08:00
2017-08-05 17:31:07 +08:00
mylog ( log_trace , " [%s]received a data from fake tcp,len:%d \n " , ip_port , data_len ) ;
2017-08-08 15:49:11 +08:00
int ret = send ( fd , data + sizeof ( u32_t ) ,
data_len - ( sizeof ( u32_t ) ) , 0 ) ;
2017-07-27 02:00:31 +08:00
2017-08-05 17:31:07 +08:00
mylog ( log_trace , " [%s]%d byte sent ,fd :%d \n " , ip_port , ret , fd ) ;
2017-07-28 00:15:10 +08:00
if ( ret < 0 ) {
mylog ( log_warn , " send returned %d \n " , ret ) ;
//perror("what happened????");
2017-07-27 02:00:31 +08:00
}
2017-07-28 00:15:10 +08:00
return 0 ;
2017-07-27 02:00:31 +08:00
}
2017-07-28 00:15:10 +08:00
return 0 ;
}
2017-08-02 18:57:27 +08:00
2017-08-27 03:31:22 -05:00
int server_on_raw_recv_pre_ready ( conn_info_t & conn_info , char * ip_port , u32_t tmp_oppsite_const_id ) // do prepare work before state change to server ready for a specifc connection
//connection recovery is also handle here
2017-07-30 16:37:49 +08:00
{
2017-08-05 17:31:07 +08:00
//u32_t ip;uint16_t port;
//ip=conn_info.raw_info.recv_info.src_ip;
//port=conn_info.raw_info.recv_info.src_port;
//char ip_port[40];
//sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
2017-07-30 18:09:51 +08:00
2017-08-02 18:57:27 +08:00
mylog ( log_info , " [%s]received handshake oppsite_id:%x my_id:%x \n " , ip_port , conn_info . oppsite_id , conn_info . my_id ) ;
2017-07-30 18:09:51 +08:00
2017-08-02 20:54:32 +08:00
mylog ( log_info , " [%s]oppsite const_id:%x \n " , ip_port , tmp_oppsite_const_id ) ;
2017-07-30 18:09:51 +08:00
if ( conn_manager . const_id_mp . find ( tmp_oppsite_const_id ) = = conn_manager . const_id_mp . end ( ) )
{
//conn_manager.const_id_mp=
if ( conn_manager . ready_num > = max_ready_conn_num )
{
mylog ( log_info , " [%s]max_ready_conn_num,cant turn to ready \n " , ip_port ) ;
2017-08-02 18:57:27 +08:00
conn_info . state . server_current_state = server_idle ;
2017-07-30 18:09:51 +08:00
return 0 ;
}
conn_info . prepare ( ) ;
conn_info . state . server_current_state = server_ready ;
conn_info . oppsite_const_id = tmp_oppsite_const_id ;
conn_manager . ready_num + + ;
conn_manager . const_id_mp [ tmp_oppsite_const_id ] = & conn_info ;
//conn_info.last_state_time=get_current_time(); //dont change this!!!!!!!!!!!!!!!!!!!!!!!!!
//conn_manager.current_ready_ip=ip;
//conn_manager.current_ready_port=port;
//my_id=conn_info.my_id;
//oppsite_id=conn_info.oppsite_id;
conn_info . last_hb_recv_time = get_current_time ( ) ;
conn_info . last_hb_sent_time = conn_info . last_hb_recv_time ; //=get_current_time()
2017-08-08 15:49:11 +08:00
send_safer ( conn_info , ' h ' , " " , 0 ) ; /////////////send
2017-07-30 18:09:51 +08:00
mylog ( log_info , " [%s]changed state to server_ready \n " , ip_port ) ;
2017-08-04 03:29:53 +08:00
conn_info . blob - > anti_replay . re_init ( ) ;
2017-07-30 18:09:51 +08:00
//g_conn_info=conn_info;
int new_timer_fd ;
2017-10-30 08:19:21 -05:00
set_timer_server ( epollfd , new_timer_fd , conn_info . timer_fd64 ) ;
2017-10-30 07:21:27 -05:00
2017-11-01 08:25:50 -05:00
fd_manager . get_info ( conn_info . timer_fd64 ) . p_conn_info = & conn_info ;
2017-10-30 07:21:27 -05:00
//assert(conn_manager.timer_fd_mp.find(new_timer_fd)==conn_manager.timer_fd_mp.end());
//conn_manager.timer_fd_mp[new_timer_fd] = &conn_info;//pack_u64(ip,port);
2017-07-30 18:09:51 +08:00
//timer_fd_mp[new_timer_fd]
/*
if ( oppsite_const_id ! = 0 & & tmp_oppsite_const_id ! = oppsite_const_id ) //TODO MOVE TO READY
{
mylog ( log_info , " cleared all conv bc of const id doesnt match \n " ) ;
conv_manager . clear ( ) ;
} */
//oppsite_const_id=tmp_oppsite_const_id;
}
else
{
conn_info_t & ori_conn_info = * conn_manager . const_id_mp [ tmp_oppsite_const_id ] ;
if ( ori_conn_info . state . server_current_state = = server_ready )
{
if ( conn_info . last_state_time < ori_conn_info . last_state_time )
{
mylog ( log_info , " [%s]conn_info.last_state_time<ori_conn_info.last_state_time. ignored new handshake \n " , ip_port ) ;
2017-08-02 18:57:27 +08:00
conn_info . state . server_current_state = server_idle ;
2017-07-30 18:09:51 +08:00
conn_info . oppsite_const_id = 0 ;
return 0 ;
}
if ( ! conn_manager . exist ( ori_conn_info . raw_info . recv_info . src_ip , ori_conn_info . raw_info . recv_info . src_port ) ) //TODO remove this
{
mylog ( log_fatal , " [%s]this shouldnt happen \n " , ip_port ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-30 18:09:51 +08:00
}
if ( ! conn_manager . exist ( conn_info . raw_info . recv_info . src_ip , conn_info . raw_info . recv_info . src_port ) ) //TODO remove this
{
mylog ( log_fatal , " [%s]this shouldnt happen2 \n " , ip_port ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-30 18:09:51 +08:00
}
conn_info_t * & p_ori = conn_manager . find_insert_p ( ori_conn_info . raw_info . recv_info . src_ip , ori_conn_info . raw_info . recv_info . src_port ) ;
conn_info_t * & p = conn_manager . find_insert_p ( conn_info . raw_info . recv_info . src_ip , conn_info . raw_info . recv_info . src_port ) ;
conn_info_t * tmp = p ;
p = p_ori ;
p_ori = tmp ;
mylog ( log_info , " [%s]grabbed a connection \n " , ip_port ) ;
//ori_conn_info.state.server_current_state=server_ready;
2017-08-04 05:27:47 +08:00
ori_conn_info . recover ( conn_info ) ;
2017-08-08 15:49:11 +08:00
send_safer ( ori_conn_info , ' h ' , " " , 0 ) ;
2017-08-04 05:27:47 +08:00
//ori_conn_info.blob->anti_replay.re_init();
2017-07-30 18:09:51 +08:00
2017-08-02 18:57:27 +08:00
conn_info . state . server_current_state = server_idle ;
2017-07-30 18:09:51 +08:00
conn_info . oppsite_const_id = 0 ;
}
else
{
mylog ( log_fatal , " [%s]this should never happen \n " , ip_port ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-30 18:09:51 +08:00
}
return 0 ;
}
2017-07-30 16:37:49 +08:00
return 0 ;
}
2017-07-23 04:17:06 +08:00
2017-07-21 20:30:27 +08:00
int client_event_loop ( )
2017-07-11 18:01:11 +08:00
{
2017-08-21 20:26:55 +08:00
2017-07-23 01:27:39 +08:00
char buf [ buf_len ] ;
2017-07-29 00:22:26 +08:00
conn_info_t conn_info ;
conn_info . my_id = get_true_random_number_nz ( ) ;
2017-07-29 21:22:13 +08:00
2017-07-30 03:46:28 +08:00
conn_info . prepare ( ) ;
2017-07-29 00:22:26 +08:00
packet_info_t & send_info = conn_info . raw_info . send_info ;
packet_info_t & recv_info = conn_info . raw_info . recv_info ;
2017-07-28 00:15:10 +08:00
2017-08-21 20:26:55 +08:00
if ( lower_level )
{
if ( lower_level_manual )
{
2017-08-26 05:38:33 -05:00
int index ;
init_ifindex ( if_name , index ) ;
2017-08-21 20:26:55 +08:00
//init_ifindex(if_name);
memset ( & send_info . addr_ll , 0 , sizeof ( send_info . addr_ll ) ) ;
send_info . addr_ll . sll_family = AF_PACKET ;
2017-08-26 05:38:33 -05:00
send_info . addr_ll . sll_ifindex = index ;
2017-08-21 20:26:55 +08:00
send_info . addr_ll . sll_halen = ETHER_ADDR_LEN ;
send_info . addr_ll . sll_protocol = htons ( ETH_P_IP ) ;
memcpy ( & send_info . addr_ll . sll_addr , dest_hw_addr , ETHER_ADDR_LEN ) ;
2017-08-21 21:49:29 +08:00
mylog ( log_info , " we are running at lower-level (manual) mode \n " ) ;
2017-08-21 20:26:55 +08:00
}
else
{
2017-08-26 05:38:33 -05:00
u32_t dest_ip ;
string if_name_string ;
string hw_string ;
if ( find_lower_level_info ( remote_ip_uint32 , dest_ip , if_name_string , hw_string ) ! = 0 )
{
mylog ( log_fatal , " auto detect lower-level info failed for %s,specific it manually \n " , remote_ip ) ;
myexit ( - 1 ) ;
}
mylog ( log_info , " we are running at lower-level (auto) mode,%s %s %s \n " , my_ntoa ( dest_ip ) , if_name_string . c_str ( ) , hw_string . c_str ( ) ) ;
u32_t hw [ 6 ] ;
memset ( hw , 0 , sizeof ( hw ) ) ;
sscanf ( hw_string . c_str ( ) , " %x:%x:%x:%x:%x:%x " , & hw [ 0 ] , & hw [ 1 ] , & hw [ 2 ] ,
& hw [ 3 ] , & hw [ 4 ] , & hw [ 5 ] ) ;
mylog ( log_warn ,
" make sure this is correct: if_name=<%s> dest_mac_adress=<%02x:%02x:%02x:%02x:%02x:%02x> \n " ,
if_name_string . c_str ( ) , hw [ 0 ] , hw [ 1 ] , hw [ 2 ] , hw [ 3 ] , hw [ 4 ] , hw [ 5 ] ) ;
for ( int i = 0 ; i < 6 ; i + + ) {
dest_hw_addr [ i ] = uint8_t ( hw [ i ] ) ;
}
//mylog(log_fatal,"--lower-level auto for client hasnt been implemented\n");
int index ;
init_ifindex ( if_name_string . c_str ( ) , index ) ;
memset ( & send_info . addr_ll , 0 , sizeof ( send_info . addr_ll ) ) ;
send_info . addr_ll . sll_family = AF_PACKET ;
send_info . addr_ll . sll_ifindex = index ;
send_info . addr_ll . sll_halen = ETHER_ADDR_LEN ;
send_info . addr_ll . sll_protocol = htons ( ETH_P_IP ) ;
memcpy ( & send_info . addr_ll . sll_addr , dest_hw_addr , ETHER_ADDR_LEN ) ;
//mylog(log_info,"we are running at lower-level (manual) mode\n");
2017-08-21 20:26:55 +08:00
}
}
2017-07-24 02:22:33 +08:00
//printf("?????\n");
2017-08-18 00:23:18 +08:00
if ( source_ip_uint32 = = 0 )
2017-07-24 02:22:33 +08:00
{
2017-07-26 06:29:40 +08:00
mylog ( log_info , " get_src_adress called \n " ) ;
2017-09-23 03:05:23 -05:00
if ( get_src_adress ( source_ip_uint32 , remote_ip_uint32 , remote_port ) ! = 0 )
2017-07-24 02:22:33 +08:00
{
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " the trick to auto get source ip failed,you should specific an ip by --source-ip \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-24 02:22:33 +08:00
}
}
in_addr tmp ;
2017-08-18 00:23:18 +08:00
tmp . s_addr = source_ip_uint32 ;
2017-07-26 06:29:40 +08:00
mylog ( log_info , " source ip = %s \n " , inet_ntoa ( tmp ) ) ;
2017-07-24 02:22:33 +08:00
//printf("done\n");
2017-09-23 03:05:23 -05:00
if ( try_to_list_and_bind ( bind_fd , local_ip_uint32 , source_port ) ! = 0 )
2017-07-24 10:54:05 +08:00
{
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " bind to source_port:%d fail \n " , source_port ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-24 10:54:05 +08:00
}
2017-07-28 00:15:10 +08:00
send_info . src_port = source_port ;
2017-08-18 00:23:18 +08:00
send_info . src_ip = source_ip_uint32 ;
2017-07-23 01:27:39 +08:00
2017-07-11 18:01:11 +08:00
int i , j , k ; int ret ;
2017-08-21 20:26:55 +08:00
2017-07-11 18:01:11 +08:00
2017-07-13 15:40:58 +08:00
//init_filter(source_port);
2017-08-18 00:23:18 +08:00
send_info . dst_ip = remote_ip_uint32 ;
2017-07-28 00:15:10 +08:00
send_info . dst_port = remote_port ;
2017-07-11 18:01:11 +08:00
2017-07-23 22:16:16 +08:00
//g_packet_info.src_ip=source_address_uint32;
2017-07-13 15:40:58 +08:00
//g_packet_info.src_port=source_port;
2017-07-11 18:01:11 +08:00
2017-07-12 16:46:02 +08:00
udp_fd = socket ( AF_INET , SOCK_DGRAM , IPPROTO_UDP ) ;
2017-09-24 03:14:08 -05:00
set_buf_size ( udp_fd , socket_buf_size , force_socket_buf ) ;
2017-07-19 06:05:08 +08:00
2017-07-11 18:01:11 +08:00
int yes = 1 ;
//setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
2017-08-15 13:03:22 +08:00
struct sockaddr_in local_me = { 0 } ;
2017-07-11 18:01:11 +08:00
socklen_t slen = sizeof ( sockaddr_in ) ;
2017-08-15 13:03:22 +08:00
//memset(&local_me, 0, sizeof(local_me));
2017-07-11 18:01:11 +08:00
local_me . sin_family = AF_INET ;
local_me . sin_port = htons ( local_port ) ;
2017-08-18 00:23:18 +08:00
local_me . sin_addr . s_addr = local_ip_uint32 ;
2017-07-13 15:40:58 +08:00
2017-07-11 18:01:11 +08:00
if ( bind ( udp_fd , ( struct sockaddr * ) & local_me , slen ) = = - 1 ) {
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " socket bind error \n " ) ;
2017-07-25 01:54:09 +08:00
//perror("socket bind error");
2017-08-04 11:51:39 +08:00
myexit ( 1 ) ;
2017-07-11 18:01:11 +08:00
}
setnonblocking ( udp_fd ) ;
2017-07-28 19:47:30 +08:00
epollfd = epoll_create1 ( 0 ) ;
2017-07-11 18:01:11 +08:00
const int max_events = 4096 ;
struct epoll_event ev , events [ max_events ] ;
if ( epollfd < 0 ) {
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " epoll return %d \n " , epollfd ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-11 18:01:11 +08:00
}
2017-07-13 15:40:58 +08:00
2017-07-11 18:01:11 +08:00
ev . events = EPOLLIN ;
2017-07-28 11:31:37 +08:00
ev . data . u64 = udp_fd ;
2017-07-11 18:01:11 +08:00
ret = epoll_ctl ( epollfd , EPOLL_CTL_ADD , udp_fd , & ev ) ;
if ( ret ! = 0 ) {
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " add udp_listen_fd error \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-11 18:01:11 +08:00
}
ev . events = EPOLLIN ;
2017-07-28 11:31:37 +08:00
ev . data . u64 = raw_recv_fd ;
2017-07-11 18:01:11 +08:00
ret = epoll_ctl ( epollfd , EPOLL_CTL_ADD , raw_recv_fd , & ev ) ;
if ( ret ! = 0 ) {
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " add raw_fd error \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-11 18:01:11 +08:00
}
////add_timer for fake_tcp_keep_connection_client
//sleep(10);
2017-07-26 17:53:15 +08:00
//memset(&udp_old_addr_in,0,sizeof(sockaddr_in));
2017-07-11 18:01:11 +08:00
int unbind = 1 ;
2017-07-28 11:31:37 +08:00
2017-07-11 18:01:11 +08:00
set_timer ( epollfd , timer_fd ) ;
2017-07-28 00:15:10 +08:00
mylog ( log_debug , " send_raw : from %x %d to %x %d \n " , send_info . src_ip , send_info . src_port , send_info . dst_ip , send_info . dst_port ) ;
2017-10-24 09:04:47 -05:00
int fifo_fd = - 1 ;
if ( fifo_file [ 0 ] ! = 0 )
{
fifo_fd = create_fifo ( fifo_file ) ;
ev . events = EPOLLIN ;
ev . data . u64 = fifo_fd ;
ret = epoll_ctl ( epollfd , EPOLL_CTL_ADD , fifo_fd , & ev ) ;
if ( ret ! = 0 ) {
2017-10-24 09:14:20 -05:00
mylog ( log_fatal , " add fifo_fd to epoll error %s \n " , strerror ( errno ) ) ;
2017-10-24 09:04:47 -05:00
myexit ( - 1 ) ;
}
mylog ( log_info , " fifo_file=%s \n " , fifo_file ) ;
}
2017-07-11 18:01:11 +08:00
while ( 1 ) ////////////////////////
{
2017-08-04 23:08:45 +08:00
if ( about_to_exit ) myexit ( 0 ) ;
2017-07-31 09:25:59 +08:00
epoll_trigger_counter + + ;
2017-07-11 18:01:11 +08:00
int nfds = epoll_wait ( epollfd , events , max_events , 180 * 1000 ) ;
if ( nfds < 0 ) { //allow zero
2017-08-05 10:03:40 +08:00
if ( errno = = EINTR )
{
mylog ( log_info , " epoll interrupted by signal \n " ) ;
2017-10-23 11:38:22 -05:00
//close(fifo_fd);
2017-08-05 10:03:40 +08:00
myexit ( 0 ) ;
}
else
{
mylog ( log_fatal , " epoll_wait return %d \n " , nfds ) ;
myexit ( - 1 ) ;
}
2017-07-11 18:01:11 +08:00
}
2017-07-30 13:39:18 +08:00
int idx ;
for ( idx = 0 ; idx < nfds ; + + idx ) {
2017-08-04 17:12:23 +08:00
if ( events [ idx ] . data . u64 = = ( u64_t ) raw_recv_fd )
2017-07-11 18:01:11 +08:00
{
2017-07-23 04:17:06 +08:00
iphdr * iph ; tcphdr * tcph ;
2017-07-29 00:22:26 +08:00
client_on_raw_recv ( conn_info ) ;
2017-07-11 18:01:11 +08:00
}
2017-08-04 17:12:23 +08:00
else if ( events [ idx ] . data . u64 = = ( u64_t ) timer_fd )
2017-07-11 18:01:11 +08:00
{
2017-08-04 17:12:23 +08:00
u64_t value ;
2017-07-11 18:01:11 +08:00
read ( timer_fd , & value , 8 ) ;
2017-08-02 20:54:32 +08:00
client_on_timer ( conn_info ) ;
2017-07-31 20:09:45 +08:00
mylog ( log_trace , " epoll_trigger_counter: %d \n " , epoll_trigger_counter ) ;
2017-07-31 09:25:59 +08:00
epoll_trigger_counter = 0 ;
2017-07-11 18:01:11 +08:00
}
2017-10-23 11:38:22 -05:00
else if ( events [ idx ] . data . u64 = = ( u64_t ) fifo_fd )
{
int len = read ( fifo_fd , buf , sizeof ( buf ) ) ;
2017-10-24 23:58:41 -05:00
//assert(len>=0);
if ( len < 0 )
{
mylog ( log_warn , " fifo read failed len=%d,errno=%s \n " , len , strerror ( errno ) ) ;
continue ;
}
2017-10-23 11:38:22 -05:00
buf [ len ] = 0 ;
2017-10-24 09:04:47 -05:00
while ( len > = 1 & & buf [ len - 1 ] = = ' \n ' )
buf [ len - 1 ] = 0 ;
mylog ( log_info , " got data from fifo,len=%d,s=[%s] \n " , len , buf ) ;
if ( strcmp ( buf , " reconnect " ) = = 0 )
{
mylog ( log_info , " received command: reconnect \n " ) ;
conn_info . state . client_current_state = client_idle ;
conn_info . my_id = get_true_random_number_nz ( ) ;
}
else
{
mylog ( log_info , " unknown command \n " ) ;
}
2017-10-23 11:38:22 -05:00
}
2017-08-04 17:12:23 +08:00
else if ( events [ idx ] . data . u64 = = ( u64_t ) udp_fd )
2017-07-11 18:01:11 +08:00
{
2017-07-13 15:40:58 +08:00
2017-07-29 20:58:38 +08:00
int recv_len ;
2017-08-15 13:03:22 +08:00
struct sockaddr_in udp_new_addr_in = { 0 } ;
2017-08-13 08:18:23 +08:00
socklen_t udp_new_addr_len = sizeof ( sockaddr_in ) ;
2017-08-12 19:21:24 +08:00
if ( ( recv_len = recvfrom ( udp_fd , buf , max_data_len , 0 ,
2017-08-13 08:18:23 +08:00
( struct sockaddr * ) & udp_new_addr_in , & udp_new_addr_len ) ) = = - 1 ) {
2017-07-28 11:31:37 +08:00
mylog ( log_error , " recv_from error,this shouldnt happen at client \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( 1 ) ;
2017-07-12 16:46:02 +08:00
} ;
2017-08-09 21:29:54 +08:00
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 ) ;
}
2017-07-26 06:29:40 +08:00
mylog ( log_trace , " Received packet from %s:%d,len: %d \n " , inet_ntoa ( udp_new_addr_in . sin_addr ) ,
2017-07-12 16:46:02 +08:00
ntohs ( udp_new_addr_in . sin_port ) , recv_len ) ;
2017-07-13 15:40:58 +08:00
2017-07-21 10:36:27 +08:00
/*
2017-07-11 18:01:11 +08:00
if ( udp_old_addr_in . sin_addr . s_addr = = 0 & & udp_old_addr_in . sin_port = = 0 )
{
memcpy ( & udp_old_addr_in , & udp_new_addr_in , sizeof ( udp_new_addr_in ) ) ;
}
else if ( udp_new_addr_in . sin_addr . s_addr ! = udp_old_addr_in . sin_addr . s_addr
| | udp_new_addr_in . sin_port ! = udp_old_addr_in . sin_port )
{
2017-07-14 21:32:37 +08:00
if ( get_current_time ( ) - last_udp_recv_time < udp_timeout )
2017-07-13 15:40:58 +08:00
{
printf ( " new <ip,port> connected in,ignored,bc last connection is still active \n " ) ;
continue ;
}
else
{
printf ( " new <ip,port> connected in,accpeted \n " ) ;
memcpy ( & udp_old_addr_in , & udp_new_addr_in , sizeof ( udp_new_addr_in ) ) ;
2017-07-19 16:11:12 +08:00
conv_id + + ;
2017-07-13 15:40:58 +08:00
}
2017-07-21 10:36:27 +08:00
} */
//last_udp_recv_time=get_current_time();
2017-08-04 17:12:23 +08:00
u64_t u64 = ( ( u64_t ( udp_new_addr_in . sin_addr . s_addr ) ) < < 32u ) + ntohs ( udp_new_addr_in . sin_port ) ;
u32_t conv ;
2017-07-21 10:36:27 +08:00
2017-08-04 03:29:53 +08:00
if ( ! conn_info . blob - > conv_manager . is_u64_used ( u64 ) )
2017-07-21 10:36:27 +08:00
{
2017-08-04 03:29:53 +08:00
if ( conn_info . blob - > conv_manager . get_size ( ) > = max_conv_num )
2017-07-26 19:47:52 +08:00
{
mylog ( log_warn , " ignored new udp connect bc max_conv_num exceed \n " ) ;
continue ;
}
2017-08-04 03:29:53 +08:00
conv = conn_info . blob - > conv_manager . get_new_conv ( ) ;
conn_info . blob - > conv_manager . insert_conv ( conv , u64 ) ;
2017-08-04 05:27:47 +08:00
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 ) ;
2017-07-11 18:01:11 +08:00
}
2017-07-21 10:36:27 +08:00
else
{
2017-08-04 03:29:53 +08:00
conv = conn_info . blob - > conv_manager . find_conv_by_u64 ( u64 ) ;
2017-07-21 10:36:27 +08:00
}
2017-08-04 03:29:53 +08:00
conn_info . blob - > conv_manager . update_active_time ( conv ) ;
2017-07-13 15:40:58 +08:00
2017-07-29 02:13:42 +08:00
if ( conn_info . state . client_current_state = = client_ready )
2017-07-12 16:46:02 +08:00
{
2017-07-30 23:50:39 +08:00
/*
char buf2 [ 6000 ] ;
int ret1 = send_raw ( conn_info . raw_info , buf2 , 40 ) ;
int ret2 = send_raw ( conn_info . raw_info , buf2 , 500 ) ;
int ret3 = send_raw ( conn_info . raw_info , buf2 , 1000 ) ;
int ret4 = send_raw ( conn_info . raw_info , buf2 , 2000 ) ;
mylog ( log_warn , " ret= %d %d %d %d \n " , ret1 , ret2 , ret3 , ret4 ) ; */
send_data_safer ( conn_info , buf , recv_len , conv ) ;
2017-07-12 16:46:02 +08:00
}
2017-07-11 18:01:11 +08:00
}
2017-07-31 09:25:59 +08:00
else
{
mylog ( log_fatal , " unknown fd,this should never happen \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-31 09:25:59 +08:00
}
2017-07-11 18:01:11 +08:00
}
}
return 0 ;
}
2017-07-13 15:40:58 +08:00
2017-07-21 20:30:27 +08:00
int server_event_loop ( )
2017-07-11 18:01:11 +08:00
{
2017-07-23 01:27:39 +08:00
char buf [ buf_len ] ;
2017-07-11 18:01:11 +08:00
int i , j , k ; int ret ;
2017-07-13 15:40:58 +08:00
2017-08-18 00:23:18 +08:00
bind_address_uint32 = local_ip_uint32 ; //only server has bind adress,client sets it to zero
2017-07-29 20:32:26 +08:00
2017-08-21 23:31:55 +08:00
if ( lower_level )
{
if ( lower_level_manual )
{
2017-08-26 05:38:33 -05:00
init_ifindex ( if_name , ifindex ) ;
2017-08-21 23:31:55 +08:00
mylog ( log_info , " we are running at lower-level (manual) mode \n " ) ;
}
else
{
mylog ( log_info , " we are running at lower-level (auto) mode \n " ) ;
}
}
2017-07-13 15:40:58 +08:00
2017-07-26 06:29:40 +08:00
if ( raw_mode = = mode_faketcp )
2017-07-22 01:19:13 +08:00
{
bind_fd = socket ( AF_INET , SOCK_STREAM , 0 ) ;
}
2017-07-30 16:37:49 +08:00
else if ( raw_mode = = mode_udp | | raw_mode = = mode_icmp ) //bind an adress to avoid collision,for icmp,there is no port,just bind a udp port
2017-07-22 01:19:13 +08:00
{
bind_fd = socket ( AF_INET , SOCK_DGRAM , 0 ) ;
}
2017-07-13 15:40:58 +08:00
2017-08-15 13:03:22 +08:00
struct sockaddr_in temp_bind_addr = { 0 } ;
// bzero(&temp_bind_addr, sizeof(temp_bind_addr));
2017-07-13 15:40:58 +08:00
temp_bind_addr . sin_family = AF_INET ;
temp_bind_addr . sin_port = htons ( local_port ) ;
2017-08-18 00:23:18 +08:00
temp_bind_addr . sin_addr . s_addr = local_ip_uint32 ;
2017-07-13 15:40:58 +08:00
if ( bind ( bind_fd , ( struct sockaddr * ) & temp_bind_addr , sizeof ( temp_bind_addr ) ) ! = 0 )
{
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " bind fail \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-13 15:40:58 +08:00
}
2017-07-30 16:37:49 +08:00
2017-07-26 06:29:40 +08:00
if ( raw_mode = = mode_faketcp )
2017-07-22 01:19:13 +08:00
{
if ( listen ( bind_fd , SOMAXCONN ) ! = 0 )
{
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " listen fail \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-22 01:19:13 +08:00
}
}
2017-07-13 15:40:58 +08:00
2017-08-21 20:26:55 +08:00
//init_raw_socket();
2017-07-30 16:37:49 +08:00
init_filter ( local_port ) ; //bpf filter
2017-07-13 15:40:58 +08:00
2017-07-12 16:46:02 +08:00
epollfd = epoll_create1 ( 0 ) ;
2017-07-11 18:01:11 +08:00
const int max_events = 4096 ;
struct epoll_event ev , events [ max_events ] ;
if ( epollfd < 0 ) {
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " epoll return %d \n " , epollfd ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-11 18:01:11 +08:00
}
ev . events = EPOLLIN ;
2017-07-28 11:31:37 +08:00
ev . data . u64 = raw_recv_fd ;
2017-07-14 21:32:37 +08:00
2017-07-11 18:01:11 +08:00
ret = epoll_ctl ( epollfd , EPOLL_CTL_ADD , raw_recv_fd , & ev ) ;
if ( ret ! = 0 ) {
2017-07-26 06:29:40 +08:00
mylog ( log_fatal , " add raw_fd error \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-11 18:01:11 +08:00
}
int timer_fd ;
2017-07-30 16:37:49 +08:00
2017-07-11 18:01:11 +08:00
set_timer ( epollfd , timer_fd ) ;
2017-07-30 16:37:49 +08:00
2017-08-04 17:12:23 +08:00
u64_t begin_time = 0 ;
u64_t end_time = 0 ;
2017-07-31 11:00:58 +08:00
2017-08-18 23:37:21 +08:00
mylog ( log_info , " now listening at %s:%d \n " , my_ntoa ( local_ip_uint32 ) , local_port ) ;
2017-10-24 09:04:47 -05:00
int fifo_fd = - 1 ;
if ( fifo_file [ 0 ] ! = 0 )
{
fifo_fd = create_fifo ( fifo_file ) ;
ev . events = EPOLLIN ;
ev . data . u64 = fifo_fd ;
ret = epoll_ctl ( epollfd , EPOLL_CTL_ADD , fifo_fd , & ev ) ;
if ( ret ! = 0 ) {
2017-10-24 09:14:20 -05:00
mylog ( log_fatal , " add fifo_fd to epoll error %s \n " , strerror ( errno ) ) ;
2017-10-24 09:04:47 -05:00
myexit ( - 1 ) ;
}
mylog ( log_info , " fifo_file=%s \n " , fifo_file ) ;
}
2017-07-11 18:01:11 +08:00
while ( 1 ) ////////////////////////
{
2017-07-31 09:25:59 +08:00
2017-08-04 23:08:45 +08:00
if ( about_to_exit ) myexit ( 0 ) ;
2017-07-11 18:01:11 +08:00
int nfds = epoll_wait ( epollfd , events , max_events , 180 * 1000 ) ;
if ( nfds < 0 ) { //allow zero
2017-08-05 10:03:40 +08:00
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 ) ;
}
2017-07-11 18:01:11 +08:00
}
2017-07-30 13:39:18 +08:00
int idx ;
for ( idx = 0 ; idx < nfds ; + + idx )
2017-07-11 18:01:11 +08:00
{
2017-07-31 09:25:59 +08:00
//mylog(log_debug,"ndfs: %d \n",nfds);
epoll_trigger_counter + + ;
2017-07-28 11:31:37 +08:00
//printf("%d %d %d %d\n",timer_fd,raw_recv_fd,raw_send_fd,n);
2017-08-04 17:12:23 +08:00
if ( ( events [ idx ] . data . u64 ) = = ( u64_t ) timer_fd )
2017-07-28 11:31:37 +08:00
{
2017-07-31 11:00:58 +08:00
if ( debug_flag ) begin_time = get_current_time ( ) ;
conn_manager . clear_inactive ( ) ;
2017-08-04 17:12:23 +08:00
u64_t dummy ;
2017-07-28 23:24:49 +08:00
read ( timer_fd , & dummy , 8 ) ;
2017-08-02 18:57:27 +08:00
//current_time_rough=get_current_time();
2017-07-31 11:00:58 +08:00
if ( debug_flag )
{
2017-08-04 23:08:45 +08:00
end_time = get_current_time ( ) ;
mylog ( log_debug , " timer_fd,%llu,%llu,%llu \n " , begin_time , end_time , end_time - begin_time ) ;
2017-07-31 11:00:58 +08:00
}
2017-07-31 09:25:59 +08:00
2017-07-31 20:09:45 +08:00
mylog ( log_trace , " epoll_trigger_counter: %d \n " , epoll_trigger_counter ) ;
2017-07-31 09:25:59 +08:00
epoll_trigger_counter = 0 ;
2017-07-28 19:47:30 +08:00
}
2017-08-04 17:12:23 +08:00
else if ( events [ idx ] . data . u64 = = ( u64_t ) raw_recv_fd )
2017-07-30 16:37:49 +08:00
{
2017-07-31 11:00:58 +08:00
if ( debug_flag ) begin_time = get_current_time ( ) ;
2017-07-30 16:37:49 +08:00
server_on_raw_recv_multi ( ) ;
2017-07-31 11:00:58 +08:00
if ( debug_flag )
{
2017-08-04 23:08:45 +08:00
end_time = get_current_time ( ) ;
mylog ( log_debug , " raw_recv_fd,%llu,%llu,%llu \n " , begin_time , end_time , end_time - begin_time ) ;
2017-07-31 11:00:58 +08:00
}
2017-07-30 16:37:49 +08:00
}
2017-10-24 09:04:47 -05:00
else if ( events [ idx ] . data . u64 = = ( u64_t ) fifo_fd )
{
int len = read ( fifo_fd , buf , sizeof ( buf ) ) ;
2017-10-24 23:58:41 -05:00
if ( len < 0 )
{
mylog ( log_warn , " fifo read failed len=%d,errno=%s \n " , len , strerror ( errno ) ) ;
continue ;
}
//assert(len>=0);
2017-10-24 09:04:47 -05:00
buf [ len ] = 0 ;
while ( len > = 1 & & buf [ len - 1 ] = = ' \n ' )
buf [ len - 1 ] = 0 ;
mylog ( log_info , " got data from fifo,len=%d,s=[%s] \n " , len , buf ) ;
mylog ( log_info , " unknown command \n " ) ;
}
2017-10-30 07:21:27 -05:00
else if ( events [ idx ] . data . u64 > u32_t ( - 1 ) )
2017-07-28 19:47:30 +08:00
{
2017-10-30 07:21:27 -05:00
fd64_t fd64 = events [ idx ] . data . u64 ;
2017-10-30 08:02:23 -05:00
if ( ! fd_manager . exist ( fd64 ) )
2017-10-30 07:21:27 -05:00
{
mylog ( log_trace , " fd64 no longer exist \n " ) ;
continue ;
}
assert ( fd_manager . exist_info ( fd64 ) ) ;
2017-11-01 08:25:50 -05:00
conn_info_t * p_conn_info = fd_manager . get_info ( fd64 ) . p_conn_info ;
u32_t ip = p_conn_info - > raw_info . send_info . dst_ip ;
u32_t port = p_conn_info - > raw_info . send_info . dst_port ;
2017-10-30 07:21:27 -05:00
2017-11-01 08:25:50 -05:00
//assert(conn_manager.exist(ip,port));
2017-10-30 07:21:27 -05:00
2017-11-01 08:25:50 -05:00
///conn_info_t* p_conn_info=conn_manager.find_insert_p(ip,port);
2017-10-30 07:21:27 -05:00
if ( fd64 = = p_conn_info - > timer_fd64 ) //////////timer_fd64
{
2017-07-31 11:00:58 +08:00
if ( debug_flag ) begin_time = get_current_time ( ) ;
2017-10-30 08:19:21 -05:00
//int fd=get_u64_l(events[idx].data.u64);
int fd = fd_manager . to_fd ( fd64 ) ;
2017-08-04 17:12:23 +08:00
u64_t dummy ;
2017-07-28 19:47:30 +08:00
read ( fd , & dummy , 8 ) ;
2017-07-29 00:22:26 +08:00
2017-10-30 07:21:27 -05:00
/*if(conn_manager.timer_fd_mp.find(fd)==conn_manager.timer_fd_mp.end()) //this can happen,when fd is a just closed fd
2017-07-28 19:47:30 +08:00
{
2017-07-30 16:37:49 +08:00
mylog ( log_info , " timer_fd no longer exits \n " ) ;
2017-07-28 19:47:30 +08:00
continue ;
2017-10-30 07:21:27 -05:00
} */
//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
2017-08-04 18:35:51 +08:00
assert ( p_conn_info - > state . server_current_state = = server_ready ) ; //TODO remove this for peformance
2017-07-29 02:13:42 +08:00
//conn_info_t &conn_info=conn_manager.find(ip,port);
2017-08-05 17:31:07 +08:00
char ip_port [ 40 ] ;
sprintf ( ip_port , " %s:%d " , my_ntoa ( ip ) , port ) ;
server_on_timer_multi ( * p_conn_info , ip_port ) ;
2017-07-31 09:25:59 +08:00
2017-07-31 11:00:58 +08:00
if ( debug_flag )
{
2017-08-04 23:08:45 +08:00
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 ) ;
2017-07-31 11:00:58 +08:00
}
2017-10-30 07:21:27 -05:00
}
else //udp_fd64
{
//}
//else if ((events[idx].data.u64 >>32u) == 1u)
//{
2017-07-28 11:31:37 +08:00
//uint32_t conv_id=events[n].data.u64>>32u;
2017-07-21 10:36:27 +08:00
2017-07-31 11:00:58 +08:00
if ( debug_flag ) begin_time = get_current_time ( ) ;
2017-07-31 09:25:59 +08:00
2017-10-30 07:21:27 -05:00
//int fd=int((events[idx].data.u64<<32u)>>32u);
2017-07-28 19:47:30 +08:00
2017-10-30 07:21:27 -05:00
/*
2017-07-30 13:39:18 +08:00
if ( conn_manager . udp_fd_mp . find ( fd ) = = conn_manager . udp_fd_mp . end ( ) ) //this can happen,when fd is a just closed fd
2017-07-28 19:47:30 +08:00
{
mylog ( log_debug , " fd no longer exists in udp_fd_mp,udp fd %d \n " , fd ) ;
2017-07-29 00:22:26 +08:00
recv ( fd , 0 , 0 , 0 ) ;
2017-07-28 19:47:30 +08:00
continue ;
2017-10-30 07:21:27 -05:00
} */
//conn_info_t* p_conn_info=conn_manager.udp_fd_mp[fd];
2017-07-30 13:39:18 +08:00
2017-10-30 07:21:27 -05:00
//u32_t ip=p_conn_info->raw_info.recv_info.src_ip;
//u32_t port=p_conn_info->raw_info.recv_info.src_port;
/*if(!conn_manager.exist(ip,port))//TODO remove this for peformance
2017-07-30 13:39:18 +08:00
{
2017-08-04 17:12:23 +08:00
mylog ( log_fatal , " ip port no longer exits 2!!!this shouldnt happen \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-10-30 07:21:27 -05:00
} */
2017-07-30 03:46:28 +08:00
2017-07-30 13:39:18 +08:00
if ( p_conn_info - > state . server_current_state ! = server_ready ) //TODO remove this for peformance
2017-07-28 19:47:30 +08:00
{
2017-08-04 17:12:23 +08:00
mylog ( log_fatal , " p_conn_info->state.server_current_state!=server_ready!!!this shouldnt happen \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-30 13:39:18 +08:00
}
2017-07-29 02:13:42 +08:00
conn_info_t & conn_info = * p_conn_info ;
2017-07-23 22:16:16 +08:00
2017-10-30 07:21:27 -05:00
assert ( conn_info . blob - > conv_manager . is_u64_used ( fd64 ) ) ;
u32_t conv_id = conn_info . blob - > conv_manager . find_conv_by_u64 ( fd64 ) ;
2017-07-21 10:36:27 +08:00
2017-10-30 07:21:27 -05:00
int fd = fd_manager . to_fd ( fd64 ) ;
2017-07-23 22:16:16 +08:00
2017-08-12 19:21:24 +08:00
int recv_len = recv ( fd , buf , max_data_len , 0 ) ;
2017-07-21 10:36:27 +08:00
2017-07-30 05:53:30 +08:00
mylog ( log_trace , " received a packet from udp_fd,len:%d \n " , recv_len ) ;
2017-07-20 16:34:43 +08:00
2017-07-13 15:40:58 +08:00
if ( recv_len < 0 )
{
2017-08-13 08:18:23 +08:00
mylog ( log_debug , " udp fd,recv_len<0 continue,%s \n " , strerror ( errno ) ) ;
2017-07-13 15:40:58 +08:00
continue ;
}
2017-07-21 10:36:27 +08:00
2017-08-09 21:29:54 +08:00
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 ) ;
}
2017-07-30 16:37:49 +08:00
//conn_info.conv_manager->update_active_time(conv_id); server dosnt update from upd side,only update from raw side. (client updates at both side)
2017-07-21 10:36:27 +08:00
2017-07-29 02:13:42 +08:00
if ( conn_info . state . server_current_state = = server_ready )
2017-07-21 10:36:27 +08:00
{
2017-07-29 00:22:26 +08:00
send_data_safer ( conn_info , buf , recv_len , conv_id ) ;
2017-07-23 06:24:32 +08:00
//send_data(g_packet_info_send,buf,recv_len,my_id,oppsite_id,conv_id);
2017-07-30 16:37:49 +08:00
mylog ( log_trace , " send_data_safer ,sent !! \n " ) ;
2017-07-21 10:36:27 +08:00
}
2017-07-31 09:25:59 +08:00
2017-07-31 11:00:58 +08:00
if ( debug_flag )
{
2017-08-04 23:08:45 +08:00
end_time = get_current_time ( ) ;
mylog ( log_debug , " (events[idx].data.u64 >>32u) == 1u,%lld,%lld,%lld \n " , begin_time , end_time , end_time - begin_time ) ;
2017-07-31 11:00:58 +08:00
}
2017-10-30 07:21:27 -05:00
}
2017-07-31 09:25:59 +08:00
}
else
{
mylog ( log_fatal , " unknown fd,this should never happen \n " ) ;
2017-08-04 11:51:39 +08:00
myexit ( - 1 ) ;
2017-07-13 15:40:58 +08:00
}
2017-07-12 16:46:02 +08:00
2017-07-11 18:01:11 +08:00
}
}
return 0 ;
}
2017-08-23 10:47:09 -05:00
2017-08-21 20:26:55 +08:00
/*
int test ( )
{
2017-08-26 05:38:33 -05:00
char ip_str [ 100 ] = " 8.8.8.8 " ;
u32_t ip = inet_addr ( ip_str ) ;
u32_t dest_ip ;
string if_name ;
string hw ;
find_lower_level_info ( ip , dest_ip , if_name , hw ) ;
printf ( " %s %s %s \n " , my_ntoa ( dest_ip ) , if_name . c_str ( ) , hw . c_str ( ) ) ;
exit ( 0 ) ;
return 0 ;
2017-08-21 20:26:55 +08:00
} */
2017-07-26 19:47:52 +08:00
int main ( int argc , char * argv [ ] )
{
2017-10-24 09:04:47 -05:00
//printf("%llu\n",u64_t(-1));
2017-08-26 05:38:33 -05:00
//test();
2017-08-26 08:25:38 -05:00
//printf("%s\n",my_ntoa(0x00ffffff));
2017-08-17 23:40:17 +08:00
//auto a=string_to_vec("a b c d ");
//printf("%d\n",(int)a.size());
2017-08-04 12:46:46 +08:00
//printf("%d %d %d %d",larger_than_u32(1,2),larger_than_u32(2,1),larger_than_u32(0xeeaaeebb,2),larger_than_u32(2,0xeeaaeebb));
2017-07-30 16:37:49 +08:00
//assert(0==1);
2017-07-29 20:32:26 +08:00
dup2 ( 1 , 2 ) ; //redirect stderr to stdout
2017-08-04 18:35:51 +08:00
signal ( SIGINT , signal_handler ) ;
signal ( SIGHUP , signal_handler ) ;
signal ( SIGKILL , signal_handler ) ;
signal ( SIGTERM , signal_handler ) ;
signal ( SIGQUIT , signal_handler ) ;
2017-08-23 07:01:21 -05:00
pre_process_arg ( argc , argv ) ;
2017-07-26 19:20:15 +08:00
2017-08-17 23:40:17 +08:00
if ( geteuid ( ) ! = 0 )
{
mylog ( log_error , " root check failed,make sure you run this program with root,we can try to continue,but it will likely fail \n " ) ;
}
2017-08-18 00:23:18 +08:00
local_ip_uint32 = inet_addr ( local_ip ) ;
remote_ip_uint32 = inet_addr ( remote_ip ) ;
source_ip_uint32 = inet_addr ( source_ip ) ;
2017-07-31 19:01:50 +08:00
2017-07-26 19:20:15 +08:00
2017-08-02 18:57:27 +08:00
//current_time_rough=get_current_time();
2017-07-29 00:22:26 +08:00
2017-07-29 20:32:26 +08:00
init_random_number_fd ( ) ;
srand ( get_true_random_number_nz ( ) ) ;
2017-07-21 20:30:27 +08:00
const_id = get_true_random_number_nz ( ) ;
2017-07-19 06:05:08 +08:00
2017-07-29 20:32:26 +08:00
mylog ( log_info , " const_id:%x \n " , const_id ) ;
2017-07-11 18:01:11 +08:00
2017-07-24 11:31:21 +08:00
char tmp [ 1000 ] = " " ;
2017-07-26 08:51:05 +08:00
strcat ( tmp , key_string ) ;
2017-07-24 11:31:21 +08:00
strcat ( tmp , " key1 " ) ;
2017-07-23 22:16:16 +08:00
2017-07-24 11:31:21 +08:00
md5 ( ( uint8_t * ) tmp , strlen ( tmp ) , ( uint8_t * ) key ) ;
2017-08-01 20:03:29 +08:00
/*
2017-07-24 11:31:21 +08:00
tmp [ 0 ] = 0 ;
2017-07-26 08:51:05 +08:00
strcat ( tmp , key_string ) ;
2017-07-24 11:31:21 +08:00
strcat ( tmp , " key2 " ) ;
2017-08-01 20:03:29 +08:00
md5 ( ( uint8_t * ) tmp , strlen ( tmp ) , ( uint8_t * ) key2 ) ; */
2017-07-24 11:31:21 +08:00
2017-08-05 17:31:07 +08:00
iptables_rule ( ) ;
2017-08-21 20:26:55 +08:00
init_raw_socket ( ) ;
2017-07-26 06:29:40 +08:00
if ( program_mode = = client_mode )
2017-07-11 18:01:11 +08:00
{
2017-07-21 20:30:27 +08:00
client_event_loop ( ) ;
2017-07-11 18:01:11 +08:00
}
else
{
2017-07-21 20:30:27 +08:00
server_event_loop ( ) ;
2017-07-11 18:01:11 +08:00
}
return 0 ;
}