From f75eb798dce69b3db5a3220af699854b2058a4f1 Mon Sep 17 00:00:00 2001 From: "U-DESKTOP-T772REH\\wangyu" Date: Mon, 18 Jun 2018 21:50:57 +0800 Subject: [PATCH] mingw compiles but not work yet --- common.cpp | 199 +++++++++++++++++++++++++++++++++++++++++++++---- common.h | 43 ++++++++++- libev/ev.c | 3 +- main.cpp | 15 +++- makefile | 4 + misc.cpp | 2 +- my_ev_common.h | 13 ++++ network.cpp | 7 +- 8 files changed, 260 insertions(+), 26 deletions(-) diff --git a/common.cpp b/common.cpp index cdb34f4..257e6d3 100644 --- a/common.cpp +++ b/common.cpp @@ -9,9 +9,93 @@ #include "log.h" #include "misc.h" +#include +#include static int random_number_fd=-1; +int init_ws() +{ +#if defined(__MINGW32__) + WORD wVersionRequested; + WSADATA wsaData; + int err; + + /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ + wVersionRequested = MAKEWORD(2, 2); + + err = WSAStartup(wVersionRequested, &wsaData); + if (err != 0) { + /* Tell the user that we could not find a usable */ + /* Winsock DLL. */ + printf("WSAStartup failed with error: %d\n", err); + exit(-1); + } + + /* Confirm that the WinSock DLL supports 2.2.*/ + /* Note that if the DLL supports versions greater */ + /* than 2.2 in addition to 2.2, it will still return */ + /* 2.2 in wVersion since that is the version we */ + /* requested. */ + + if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { + /* Tell the user that we could not find a usable */ + /* WinSock DLL. */ + printf("Could not find a usable version of Winsock.dll\n"); + WSACleanup(); + exit(-1); + } + else + { + printf("The Winsock 2.2 dll was found okay"); + } + + int tmp[]={0,100,200,300,500,800,1000,2000,3000,4000,-1}; + int succ=0; + for(int i=1;tmp[i]!=-1;i++) + { + if(_setmaxstdio(100)==-1) break; + else succ=i; + } + printf(", _setmaxstdio() was set to %d\n",tmp[succ]); +#endif +return 0; +} + +#if defined(__MINGW32__) +char *get_sock_error() +{ + static char buf[1000]; + int e=WSAGetLastError(); + wchar_t *s = NULL; + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, e, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPWSTR)&s, 0, NULL); + sprintf(buf, "%d:%S", e,s); + int len=strlen(buf); + if(len>0&&buf[len-1]=='\n') buf[len-1]=0; + LocalFree(s); + return buf; +} +int get_sock_errno() +{ + return WSAGetLastError(); +} +#else +char *get_sock_error() +{ + static char buf[1000]; + sprintf(buf, "%d:%s", errno,strerror(errno)); + return buf; +} +int get_sock_errno() +{ + return errno; +} +#endif + + u64_t get_current_time() { timespec tmp_time; @@ -42,40 +126,103 @@ char * my_ntoa(u32_t ip) return inet_ntoa(a); } -void init_random_number_fd() +#if !defined(__MINGW32__) +struct random_fd_t { - - random_number_fd=open("/dev/urandom",O_RDONLY); - - if(random_number_fd==-1) + int random_number_fd; + random_fd_t() { - mylog(log_fatal,"error open /dev/urandom\n"); - myexit(-1); + random_number_fd=open("/dev/urandom",O_RDONLY); + + if(random_number_fd==-1) + { + mylog(log_fatal,"error open /dev/urandom\n"); + myexit(-1); + } + setnonblocking(random_number_fd); } - setnonblocking(random_number_fd); -} + int get_fd() + { + return random_number_fd; + } +}random_fd; +#else +struct my_random_t +{ + std::random_device rd; + std::mt19937 gen; + std::uniform_int_distribution dis64; + std::uniform_int_distribution dis32; + + std::uniform_int_distribution dis8; + + my_random_t() + { + std::mt19937 gen_tmp(rd()); + gen=gen_tmp; + gen.discard(700000); //magic + } + u64_t gen64() + { + return dis64(gen); + } + u32_t gen32() + { + return dis32(gen); + } + + unsigned char gen8() + { + return dis8(gen); + } + /*int random_number_fd; + random_fd_t() + { + random_number_fd=open("/dev/urandom",O_RDONLY); + if(random_number_fd==-1) + { + mylog(log_fatal,"error open /dev/urandom\n"); + myexit(-1); + } + setnonblocking(random_number_fd); + } + int get_fd() + { + return random_number_fd; + }*/ +}my_random; +#endif + u64_t get_true_random_number_64() { +#if !defined(__MINGW32__) u64_t ret; - int size=read(random_number_fd,&ret,sizeof(ret)); + int size=read(random_fd.get_fd(),&ret,sizeof(ret)); if(size!=sizeof(ret)) { mylog(log_fatal,"get random number failed %d\n",size); + myexit(-1); } - return ret; +#else + return my_random.gen64(); //fake random number +#endif } u32_t get_true_random_number() { +#if !defined(__MINGW32__) u32_t ret; - int size=read(random_number_fd,&ret,sizeof(ret)); + int size=read(random_fd.get_fd(),&ret,sizeof(ret)); if(size!=sizeof(ret)) { mylog(log_fatal,"get random number failed %d\n",size); myexit(-1); } return ret; +#else + return my_random.gen64(); //fake random number +#endif } u32_t get_true_random_number_nz() //nz for non-zero { @@ -86,6 +233,7 @@ u32_t get_true_random_number_nz() //nz for non-zero } return ret; } + inline int is_big_endian() { int i=1; @@ -109,6 +257,7 @@ u64_t hton64(u64_t a) } void setnonblocking(int sock) { +#if !defined(__MINGW32__) int opts; opts = fcntl(sock, F_GETFL); @@ -123,7 +272,14 @@ void setnonblocking(int sock) { //perror("fcntl(sock,SETFL,opts)"); myexit(1); } +#else + int iResult; + u_long iMode = 1; + iResult = ioctlsocket(sock, FIONBIO, &iMode); + if (iResult != NO_ERROR) + printf("ioctlsocket failed with error: %d\n", iResult); +#endif } /* @@ -154,8 +310,11 @@ unsigned short csum(const unsigned short *ptr,int nbytes) {//works both for big int set_buf_size(int fd,int socket_buf_size,int force_socket_buf) { - /*if(force_socket_buf) + if(force_socket_buf) { +#if defined(__MINGW32__) + assert(0==1); +#else if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) { mylog(log_fatal,"SO_SNDBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno)); @@ -166,8 +325,10 @@ int set_buf_size(int fd,int socket_buf_size,int force_socket_buf) mylog(log_fatal,"SO_RCVBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno)); myexit(1); } +#endif + } - else*/ + else { if(setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0) { @@ -180,6 +341,7 @@ int set_buf_size(int fd,int socket_buf_size,int force_socket_buf) myexit(1); } } + return 0; } @@ -387,7 +549,10 @@ int read_file(const char * file,string &output) } return 0; } + int run_command(string command0,char * &output,int flag) { + assert(0==1 && "not implemented\n"); +#if 0 FILE *in; @@ -441,6 +606,7 @@ int run_command(string command0,char * &output,int flag) { return -4; } +#endif return 0; } @@ -561,6 +727,7 @@ vector parse_conf_line(const string& s0) int create_fifo(char * file) { +#if !defined(__MINGW32__) if(mkfifo (file, 0666)!=0) { if(errno==EEXIST) @@ -594,6 +761,10 @@ int create_fifo(char * file) setnonblocking(fifo_fd); return fifo_fd; +#else + assert(0==1&&"not implemented"); + return 0; +#endif } void ip_port_t::from_u64(u64_t u64) diff --git a/common.h b/common.h index d76a402..6f64693 100644 --- a/common.h +++ b/common.h @@ -19,8 +19,7 @@ #include //#include //#include -#include //for socket ofcourse -#include + #include #include //for exit(0); #include //For errno - the error number @@ -38,7 +37,6 @@ #include #include //#include -#include //#include //#include //#include @@ -48,7 +46,7 @@ //#include #include -#ifndef __CYGWIN__ +#if !defined(__CYGWIN__) && !defined(__MINGW32__) #include #else #include @@ -61,6 +59,19 @@ #include +#if defined(__MINGW32__) +#include +typedef unsigned char u_int8_t; +typedef unsigned short u_int16_t; +typedef unsigned int u_int32_t; +typedef int socklen_t; +#else +#include //for socket ofcourse +#include +#include +#endif + + #include #include #include @@ -100,6 +111,29 @@ using namespace std; #endif +#if defined(__MINGW32__) +#define setsockopt(a,b,c,d,e) setsockopt(a,b,c,(const char *)(d),e) +#endif + +char *get_sock_error(); +int get_sock_errno(); + +#if defined(__MINGW32__) +typedef SOCKET my_fd_t; +inline int sock_close(my_fd_t fd) +{ + return closesocket(fd); +} +#else +typedef int my_fd_t; +inline int sock_close(my_fd_t fd) +{ + return close(fd); +} + +#endif + + typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64 typedef long long i64_t; @@ -173,6 +207,7 @@ struct queue_t } }; +int init_ws(); u64_t get_current_time(); u64_t pack_u64(u32_t a,u32_t b); diff --git a/libev/ev.c b/libev/ev.c index 3b81376..fe03dac 100644 --- a/libev/ev.c +++ b/libev/ev.c @@ -2473,7 +2473,8 @@ evpipe_write (EV_P_ EV_ATOMIC_T *flag) #ifdef _WIN32 WSABUF buf; DWORD sent; - buf.buf = &buf; +// buf.buf=&buf; + buf.buf = (char *)&buf; buf.len = 1; WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0); #else diff --git a/main.cpp b/main.cpp index 133abb9..41ce52d 100755 --- a/main.cpp +++ b/main.cpp @@ -1033,9 +1033,12 @@ void sigint_cb(struct ev_loop *l, ev_signal *w, int revents) myexit(0); } - +#include int main(int argc, char *argv[]) { + + init_ws(); + int i=0; //libnet_t *l; /* the libnet context */ @@ -1051,9 +1054,13 @@ int main(int argc, char *argv[]) //signal(SIGQUIT, signal_handler); struct ev_loop* loop=ev_default_loop(0); + printf("%x %x\n",ev_supported_backends(),ev_backend(loop)); + +#if !defined(__MINGW32__) ev_signal signal_watcher_sigpipe; ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE); ev_signal_start(loop, &signal_watcher_sigpipe); +#endif ev_signal signal_watcher_sigterm; ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM); @@ -1065,7 +1072,7 @@ int main(int argc, char *argv[]) pre_process_arg(argc,argv); - +#if !defined(__MINGW32__) if(geteuid() != 0) { mylog(log_warn,"root check failed, it seems like you are using a non-root account. we can try to continue, but it may fail. If you want to run udp2raw as non-root, you have to add iptables rule manually, and grant udp2raw CAP_NET_RAW capability, check README.md in repo for more info.\n"); @@ -1074,7 +1081,7 @@ int main(int argc, char *argv[]) { mylog(log_warn,"you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n"); } - +#endif local_ip_uint32=inet_addr(local_ip); source_ip_uint32=inet_addr(source_ip); @@ -1082,7 +1089,7 @@ int main(int argc, char *argv[]) mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_ip); remote_ip_uint32=inet_addr(remote_ip); - init_random_number_fd(); + //init_random_number_fd(); srand(get_true_random_number_nz()); const_id=get_true_random_number_nz(); diff --git a/makefile b/makefile index 8b93b88..02f575f 100755 --- a/makefile +++ b/makefile @@ -32,6 +32,10 @@ cygwin:git_version rm -f ${NAME} ${cc_local} -o ${NAME} -I. ${SOURCES} pcap_wrapper.cpp ${FLAGS} -lrt -ggdb -static -O2 -D_GNU_SOURCE +mingw:git_version + rm -f ${NAME} + ${cc_local} -o ${NAME} -I. ${SOURCES} pcap_wrapper.cpp ${FLAGS} -ggdb -static -O2 -lws2_32 + linux:git_version rm -f ${NAME} ${cc_local} -o ${NAME} -I. ${SOURCES} ${PCAP} ${LIBNET} ${FLAGS} -lrt -ggdb -static -O2 diff --git a/misc.cpp b/misc.cpp index ffe2b53..55516f3 100644 --- a/misc.cpp +++ b/misc.cpp @@ -533,7 +533,7 @@ void process_arg(int argc, char *argv[]) //process all options else if(strcmp(long_options[option_index].name,"lower-level")==0) { assert(0==1); - process_lower_level_arg(); + //process_lower_level_arg(); //lower_level=1; //strcpy(lower_level_arg,optarg); } diff --git a/my_ev_common.h b/my_ev_common.h index 0755f45..aa1916f 100644 --- a/my_ev_common.h +++ b/my_ev_common.h @@ -2,4 +2,17 @@ #define EV_STANDALONE 1 #define EV_COMMON void *data; unsigned long long u64; #define EV_COMPAT3 0 + +//#include +#if defined(__MINGW32__) +//#define EV_USE_SELECT 1 +#define EV_SELECT_IS_WINSOCKET 1 + +# define EV_FD_TO_WIN32_HANDLE(fd) (fd) +# define EV_WIN32_HANDLE_TO_FD(handle) (handle) +# define EV_WIN32_CLOSE_FD(fd) closesocket (fd) +# define FD_SETSIZE 4096 + +#endif //#define EV_VERIFY 2 + diff --git a/network.cpp b/network.cpp index bbb2a9d..b87002b 100644 --- a/network.cpp +++ b/network.cpp @@ -586,6 +586,8 @@ int init_ifindex(const char * if_name,int &index) */ return 0; } + +#if 0 bool interface_has_arp(const char * interface) { struct ifreq ifr; // int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP); @@ -600,6 +602,7 @@ bool interface_has_arp(const char * interface) { //close(sock); return !(ifr.ifr_flags & IFF_NOARP); } +#endif struct route_info_t { string if_name; @@ -1273,7 +1276,7 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { tcph->urg = 0; //tcph->window = htons((uint16_t)(1024)); - tcph->window = htons((uint16_t) (receive_window_lower_bound + random() % receive_window_random_range)); + tcph->window = htons((uint16_t) (receive_window_lower_bound + get_true_random_number() % receive_window_random_range)); tcph->check = 0; //leave checksum 0 now, filled later by pseudo header tcph->urg_ptr = 0; @@ -2016,7 +2019,7 @@ int after_send_raw0(raw_info_t &raw_info) send_info.seq += raw_info.send_info.data_len; //////////////////modify } else if (seq_mode == 2) { - if (random() % 5 == 3) + if (get_true_random_number() % 5 == 3) send_info.seq += raw_info.send_info.data_len; //////////////////modify } else if(seq_mode==3||seq_mode==4)