diff --git a/main.cpp b/main.cpp index 4d11cd2..0989fad 100644 --- a/main.cpp +++ b/main.cpp @@ -77,10 +77,42 @@ static void print_help() //printf("common options,these options must be same on both side\n"); } +void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents) +{ + mylog(log_info, "got sigpipe, ignored"); +} + +void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents) +{ + mylog(log_info, "got sigterm, exit"); + myexit(0); +} + +void sigint_cb(struct ev_loop *l, ev_signal *w, int revents) +{ + mylog(log_info, "got sigint, exit"); + myexit(0); +} int main(int argc, char *argv[]) { working_mode=tun_dev_mode; + struct ev_loop* loop=ev_default_loop(0); +#if !defined(__MINGW32__) + ev_signal signal_watcher_sigpipe; + ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE); + ev_signal_start(loop, &signal_watcher_sigpipe); +#else + enable_log_color=0; +#endif + + ev_signal signal_watcher_sigterm; + ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM); + ev_signal_start(loop, &signal_watcher_sigterm); + + ev_signal signal_watcher_sigint; + ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT); + ev_signal_start(loop, &signal_watcher_sigint); assert(sizeof(u64_t)==8); assert(sizeof(i64_t)==8); diff --git a/makefile b/makefile index 2bbf5d6..11db2e3 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 -ggdb -I. -IUDPspeeder -isystem UDPspeeder/libev ${OPT} -SOURCES=`ls UDPspeeder/*.cpp UDPspeeder/lib/*.cpp|grep -v main.cpp|grep -v tunnel.cpp` main.cpp tun_dev.cpp +SOURCES=`ls UDPspeeder/*.cpp UDPspeeder/lib/*.cpp|grep -v main.cpp|grep -v tunnel.cpp` main.cpp tun_dev.cpp tun_dev_client.cpp tun_dev_server.cpp #INCLUDE= -I. -IUDPspeeder diff --git a/tun_dev.cpp b/tun_dev.cpp index 0998fa0..92e60f4 100644 --- a/tun_dev.cpp +++ b/tun_dev.cpp @@ -149,7 +149,7 @@ int from_fec_to_normal2(conn_info_t & conn_info,dest_t &dest,char * data,int len return 0; } -int do_mssfix(char * s,int len) +int do_mssfix(char * s,int len)//currently only for ipv4 { if(mssfix==0) { diff --git a/tun_dev_client.cpp b/tun_dev_client.cpp index a003c91..740eed9 100644 --- a/tun_dev_client.cpp +++ b/tun_dev_client.cpp @@ -1,5 +1,199 @@ #include "tun_dev.h" +int got_feed_back=0; + +dest_t udp_dest; +dest_t tun_dest; + + +static void remote_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) +{ + assert(!(revents&EV_ERROR)); + + conn_info_t & conn_info= *((conn_info_t*)watcher->data); + + char data[buf_len]; + int len; + + fd64_t fd64=watcher->u64; + int fd=fd_manager.to_fd(fd64); + + len=recv(fd,data,max_data_len+1,0); + + if(len==max_data_len+1) + { + mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len); + return; + } + + if(len<0) + { + mylog(log_warn,"recv return %d,errno=%s\n",len,strerror(errno)); + return; + } + + if(de_cook(data,len)<0) + { + mylog(log_warn,"de_cook(data,len)failed \n"); + return; + + } + + char header=0; + if(get_header(header,data,len)!=0) + { + mylog(log_warn,"get_header failed\n"); + return; + } + + if(header==header_keep_alive) + { + mylog(log_trace,"got keep_alive packet\n"); + return; + } + + if(header==header_reject) + { + if(keep_reconnect==0) + { + mylog(log_fatal,"server restarted or switched to handle another client,exited\n"); + myexit(-1); + } + else + { + if(got_feed_back==1) + mylog(log_warn,"server restarted or switched to handle another client,but keep-reconnect enabled,trying to reconnect\n"); + got_feed_back=0; + } + return; + } + else if(header==header_normal) + { + if(got_feed_back==0) + mylog(log_info,"connection accepted by server\n"); + got_feed_back=1; + } + else + { + mylog(log_warn,"invalid header %d %d\n",int(header),len); + return; + } + + mylog(log_trace,"Received packet from udp,len: %d\n",len); + + from_fec_to_normal2(conn_info,tun_dest,data,len); + + +} + +static void tun_fd_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) +{ + assert(!(revents&EV_ERROR)); + + conn_info_t & conn_info= *((conn_info_t*)watcher->data); + + char data[buf_len]; + int len; + int tun_fd=watcher->fd; + + len=read(tun_fd,data,max_data_len+1); + + if(len==max_data_len+1) + { + mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len); + return; + } + + if(len<0) + { + mylog(log_warn,"read from tun_fd return %d,errno=%s\n",len,strerror(errno)); + return; + } + + do_mssfix(data,len); + + mylog(log_trace,"Received packet from tun,len: %d\n",len); + + char header=(got_feed_back==0?header_new_connect:header_normal); + from_normal_to_fec2(conn_info,udp_dest,data,len,header); +} + +static void delay_manager_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents) +{ + assert(!(revents&EV_ERROR)); +} + +static void fec_encode_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents) +{ + assert(!(revents&EV_ERROR)); + + mylog(log_trace,"fec_encode_cb() called\n"); + + conn_info_t & conn_info= *((conn_info_t*)watcher->data); + + ///int ret; + ///fd64_t fd64=watcher->u64; + ///mylog(log_trace,"events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64()\n"); + + ///uint64_t value; + ///if(!fd_manager.exist(fd64)) //fd64 has been closed + ///{ + /// mylog(log_trace,"!fd_manager.exist(fd64)"); + /// return; + ///} + ///if((ret=read(fd_manager.to_fd(fd64), &value, 8))!=8) + ///{ + /// mylog(log_trace,"(ret=read(fd_manager.to_fd(fd64), &value, 8))!=8,ret=%d\n",ret); + /// return; + ///} + ///if(value==0) + ///{ + /// mylog(log_debug,"value==0\n"); + /// return; + ///} + ///assert(value==1); + + char header=(got_feed_back==0?header_new_connect:header_normal); + from_normal_to_fec2(conn_info,udp_dest,0,0,header); +} + +static void conn_timer_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents) +{ + assert(!(revents&EV_ERROR)); + + conn_info_t & conn_info= *((conn_info_t*)watcher->data); + + ///uint64_t value; + ///read(conn_info.timer.get_timer_fd(), &value, 8); + ///mylog(log_trace,"events[idx].data.u64==(u64_t)conn_info.timer.get_timer_fd()\n"); + + mylog(log_trace,"conn_timer_cb() called\n"); + conn_info.stat.report_as_client(); + if(got_feed_back) do_keep_alive(udp_dest); +} + +static void fifo_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) +{ + assert(!(revents&EV_ERROR)); + int fifo_fd=watcher->fd; + + char buf[buf_len]; + int len=read (fifo_fd, buf, sizeof (buf)); + if(len<0) + { + mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno)); + return; + } + buf[len]=0; + handle_command(buf); +} + +static void prepare_cb(struct ev_loop *loop, struct ev_prepare *watcher, int revents) +{ + assert(!(revents&EV_ERROR)); + + delay_manager.check(); +} int tun_dev_client_event_loop() { @@ -11,6 +205,9 @@ int tun_dev_client_event_loop() int remote_fd; fd64_t remote_fd64; + conn_info_t *conn_info_p=new conn_info_t; + conn_info_t &conn_info=*conn_info_p; //huge size of conn_info,do not allocate on stack + tun_fd=get_tun_fd(tun_dev); assert(tun_fd>0); @@ -19,6 +216,13 @@ int tun_dev_client_event_loop() assert(set_tun(tun_dev,htonl((ntohl(sub_net_uint32)&0xFFFFFF00)|2),htonl((ntohl(sub_net_uint32)&0xFFFFFF00 )|1),tun_mtu)==0); + tun_dest.type=type_write_fd; + tun_dest.inner.fd=tun_fd; + + udp_dest.cook=1; + udp_dest.type=type_fd64; + udp_dest.inner.fd64=remote_fd64; + ///epoll_fd = epoll_create1(0); ///assert(epoll_fd>0); @@ -29,261 +233,111 @@ int tun_dev_client_event_loop() /// myexit(-1); ///} + struct ev_loop * loop= ev_default_loop(0); assert(loop != NULL); + conn_info.loop=loop; - ev.events = EPOLLIN; - ev.data.u64 = remote_fd64; - ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, remote_fd, &ev); - if (ret!=0) { - mylog(log_fatal,"add remote_fd64 error\n"); - myexit(-1); - } + ///ev.events = EPOLLIN; + ///ev.data.u64 = remote_fd64; + ///ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, remote_fd, &ev); + ///if (ret!=0) { + /// mylog(log_fatal,"add remote_fd64 error\n"); + /// myexit(-1); + ///} - ev.events = EPOLLIN; - ev.data.u64 = tun_fd; - ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, tun_fd, &ev); - if (ret!=0) { - mylog(log_fatal,"add tun_fd error\n"); - myexit(-1); - } + struct ev_io remote_watcher; + remote_watcher.data=&conn_info; + remote_watcher.u64=remote_fd64; + ev_io_init(&remote_watcher, remote_cb, remote_fd, EV_READ); + ev_io_start(loop, &remote_watcher); - ev.events = EPOLLIN; - ev.data.u64 = delay_manager.get_timer_fd(); - mylog(log_debug,"delay_manager.get_timer_fd()=%d\n",delay_manager.get_timer_fd()); - ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, delay_manager.get_timer_fd(), &ev); - if (ret!= 0) { - mylog(log_fatal,"add delay_manager.get_timer_fd() error\n"); - myexit(-1); - } + ///ev.events = EPOLLIN; + ///ev.data.u64 = tun_fd; + ///ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, tun_fd, &ev); + ///if (ret!=0) { + /// mylog(log_fatal,"add tun_fd error\n"); + /// myexit(-1); + ///} + struct ev_io tun_fd_watcher; + tun_fd_watcher.data=&conn_info; - conn_info_t *conn_info_p=new conn_info_t; - conn_info_t &conn_info=*conn_info_p; //huge size of conn_info,do not allocate on stack + ev_io_init(&tun_fd_watcher, tun_fd_cb, tun_fd, EV_READ); + ev_io_start(loop, &tun_fd_watcher); - u64_t tmp_timer_fd64=conn_info.fec_encode_manager.get_timer_fd64(); - ev.events = EPOLLIN; - ev.data.u64 = tmp_timer_fd64; - mylog(log_debug,"conn_info.fec_encode_manager.get_timer_fd64()=%llu\n",conn_info.fec_encode_manager.get_timer_fd64()); - ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_manager.to_fd(tmp_timer_fd64), &ev); - if (ret!= 0) { - mylog(log_fatal,"add fec_encode_manager.get_timer_fd64() error\n"); - myexit(-1); - } + ///ev.events = EPOLLIN; + ///ev.data.u64 = delay_manager.get_timer_fd(); + + ///mylog(log_debug,"delay_manager.get_timer_fd()=%d\n",delay_manager.get_timer_fd()); + ///ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, delay_manager.get_timer_fd(), &ev); + ///if (ret!= 0) { + /// mylog(log_fatal,"add delay_manager.get_timer_fd() error\n"); + /// myexit(-1); + ///} + + delay_manager.set_loop_and_cb(loop,delay_manager_cb); + + + ///u64_t tmp_timer_fd64=conn_info.fec_encode_manager.get_timer_fd64(); + ///ev.events = EPOLLIN; + ///ev.data.u64 = tmp_timer_fd64; + + ///mylog(log_debug,"conn_info.fec_encode_manager.get_timer_fd64()=%llu\n",conn_info.fec_encode_manager.get_timer_fd64()); + ///ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_manager.to_fd(tmp_timer_fd64), &ev); + ///if (ret!= 0) { + /// mylog(log_fatal,"add fec_encode_manager.get_timer_fd64() error\n"); + /// myexit(-1); + ///} + + conn_info.fec_encode_manager.set_data(&conn_info); + conn_info.fec_encode_manager.set_loop_and_cb(loop,fec_encode_cb); - conn_info.timer.add_fd_to_epoll(epoll_fd); - conn_info.timer.set_timer_repeat_us(timer_interval*1000); + //conn_info.timer.add_fd_to_epoll(epoll_fd); + // conn_info.timer.data=&conn_info; + + conn_info.timer.data=&conn_info; + ev_init(&conn_info.timer,conn_timer_cb); + ev_timer_set(&conn_info.timer, 0, timer_interval/1000.0 ); + ev_timer_start(loop,&conn_info.timer); + + + struct ev_io fifo_watcher; int fifo_fd=-1; if(fifo_file[0]!=0) { fifo_fd=create_fifo(fifo_file); - ev.events = EPOLLIN; - ev.data.u64 = fifo_fd; + ///ev.events = EPOLLIN; + ///ev.data.u64 = fifo_fd; + + ///ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fifo_fd, &ev); + ///if (ret!= 0) { + /// mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno)); + /// myexit(-1); + ///} + ev_io_init(&fifo_watcher, fifo_cb, fifo_fd, EV_READ); + ev_io_start(loop, &fifo_watcher); - ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fifo_fd, &ev); - if (ret!= 0) { - mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno)); - myexit(-1); - } mylog(log_info,"fifo_file=%s\n",fifo_file); } - dest_t udp_dest; - udp_dest.cook=1; - udp_dest.type=type_fd64; - udp_dest.inner.fd64=remote_fd64; + ev_prepare prepare_watcher; + ev_init(&prepare_watcher,prepare_cb); + ev_prepare_start(loop,&prepare_watcher); - dest_t tun_dest; - tun_dest.type=type_write_fd; - tun_dest.inner.fd=tun_fd; - int got_feed_back=0; - - while(1)//////////////////////// - { + ev_run(loop, 0); - 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,continue\n"); - //myexit(0); - } - else - { - mylog(log_fatal,"epoll_wait return %d,%s\n", nfds,strerror(errno)); - myexit(-1); - } - } - int idx; - for (idx = 0; idx < nfds; ++idx) - { - if(events[idx].data.u64==(u64_t)conn_info.timer.get_timer_fd()) - { - uint64_t value; - read(conn_info.timer.get_timer_fd(), &value, 8); - mylog(log_trace,"events[idx].data.u64==(u64_t)conn_info.timer.get_timer_fd()\n"); - conn_info.stat.report_as_client(); - if(got_feed_back) do_keep_alive(udp_dest); - } - - else if(events[idx].data.u64==conn_info.fec_encode_manager.get_timer_fd64()) - { - fd64_t fd64=events[idx].data.u64; - mylog(log_trace,"events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64()\n"); - - uint64_t value; - if(!fd_manager.exist(fd64)) //fd64 has been closed - { - mylog(log_trace,"!fd_manager.exist(fd64)"); - continue; - } - if((ret=read(fd_manager.to_fd(fd64), &value, 8))!=8) - { - mylog(log_trace,"(ret=read(fd_manager.to_fd(fd64), &value, 8))!=8,ret=%d\n",ret); - continue; - } - if(value==0) - { - mylog(log_debug,"value==0\n"); - continue; - } - assert(value==1); - - char header=(got_feed_back==0?header_new_connect:header_normal); - from_normal_to_fec2(conn_info,udp_dest,0,0,header); - } - else if(events[idx].data.u64==(u64_t)tun_fd) - { - len=read(tun_fd,data,max_data_len+1); - - if(len==max_data_len+1) - { - mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len); - continue; - } - - if(len<0) - { - mylog(log_warn,"read from tun_fd return %d,errno=%s\n",len,strerror(errno)); - continue; - } - - do_mssfix(data,len); - - mylog(log_trace,"Received packet from tun,len: %d\n",len); - - char header=(got_feed_back==0?header_new_connect:header_normal); - from_normal_to_fec2(conn_info,udp_dest,data,len,header); - - } - else if(events[idx].data.u64==(u64_t)remote_fd64) - { - fd64_t fd64=events[idx].data.u64; - int fd=fd_manager.to_fd(fd64); - - len=recv(fd,data,max_data_len+1,0); - - if(len==max_data_len+1) - { - mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len); - continue; - } - - if(len<0) - { - mylog(log_warn,"recv return %d,errno=%s\n",len,strerror(errno)); - continue; - } - - if(de_cook(data,len)<0) - { - mylog(log_warn,"de_cook(data,len)failed \n"); - continue; - - } - - char header=0; - if(get_header(header,data,len)!=0) - { - mylog(log_warn,"get_header failed\n"); - continue; - } - - if(header==header_keep_alive) - { - mylog(log_trace,"got keep_alive packet\n"); - continue; - } - - if(header==header_reject) - { - if(keep_reconnect==0) - { - mylog(log_fatal,"server restarted or switched to handle another client,exited\n"); - myexit(-1); - } - else - { - if(got_feed_back==1) - mylog(log_warn,"server restarted or switched to handle another client,but keep-reconnect enabled,trying to reconnect\n"); - got_feed_back=0; - } - continue; - } - else if(header==header_normal) - { - if(got_feed_back==0) - mylog(log_info,"connection accepted by server\n"); - got_feed_back=1; - } - else - { - mylog(log_warn,"invalid header %d %d\n",int(header),len); - continue; - } - - mylog(log_trace,"Received packet from udp,len: %d\n",len); - - from_fec_to_normal2(conn_info,tun_dest,data,len); - - } - else if (events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()) - { - uint64_t value; - read(delay_manager.get_timer_fd(), &value, 8); - mylog(log_trace,"events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()\n"); - - } - else if (events[idx].data.u64 == (u64_t)fifo_fd) - { - char buf[buf_len]; - int len=read (fifo_fd, buf, sizeof (buf)); - if(len<0) - { - mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno)); - continue; - } - buf[len]=0; - handle_command(buf); - } - else - { - assert(0==1); - } - } - delay_manager.check(); - } + mylog(log_warn,"ev_run returned\n"); + myexit(0); return 0; diff --git a/tun_dev_server.cpp b/tun_dev_server.cpp index dd262dc..9168dff 100644 --- a/tun_dev_server.cpp +++ b/tun_dev_server.cpp @@ -2,6 +2,7 @@ int tun_dev_server_event_loop() { + /* char data[buf_len]; int len; int i,j,k,ret; @@ -311,7 +312,7 @@ int tun_dev_server_event_loop() } delay_manager.check(); } - +*/ return 0; }