From 8c21ff55ab7c1dae58a693e299eaf15fc87a6f06 Mon Sep 17 00:00:00 2001 From: "U-DESKTOP-T772REH\\wangyu" Date: Tue, 19 Jun 2018 18:06:28 +0800 Subject: [PATCH] works under mingw, but not finish yet --- common.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ common.h | 53 +++++++++++++++++++++------- lib/fec.cpp | 2 +- libev/ev.c | 2 +- log.h | 37 +------------------- main.cpp | 3 ++ makefile | 4 +++ misc.cpp | 12 +++---- my_ev_common.h | 8 +++++ 9 files changed, 158 insertions(+), 56 deletions(-) diff --git a/common.cpp b/common.cpp index cb0f8ee..9bc8522 100644 --- a/common.cpp +++ b/common.cpp @@ -27,6 +27,86 @@ working_mode_t working_mode=tunnel_mode; int socket_buf_size=1024*1024; +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 struct my_random_t @@ -297,6 +377,7 @@ u64_t hton64(u64_t a) }*/ void setnonblocking(int sock) { +#if !defined(__MINGW32__) int opts; opts = fcntl(sock, F_GETFL); @@ -311,7 +392,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 } /* @@ -591,6 +679,7 @@ int round_up_div(int a,int b) int create_fifo(char * file) { +#if !defined(__MINGW32__) if(mkfifo (file, 0666)!=0) { if(errno==EEXIST) @@ -624,6 +713,10 @@ int create_fifo(char * file) setnonblocking(fifo_fd); return fifo_fd; +#else + assert(0==1&&"not supported\n"); + return 0; +#endif } diff --git a/common.h b/common.h index 83b9b1c..bbcb5f2 100644 --- a/common.h +++ b/common.h @@ -17,8 +17,6 @@ #include #include -#include -#include //for socket ofcourse #include #include #include //for exit(0); @@ -27,19 +25,26 @@ //#include //#include //Provides declarations for ip header //#include -#include #include -#include #include #include -#include //#include -#include -#include +//#include #include #include #include +#if defined(__MINGW32__) +#include +#include +typedef int socklen_t; +#else +#include +#include +#include +#include +#endif + #include #include @@ -58,6 +63,30 @@ typedef int i32_t; typedef unsigned short u16_t; typedef short i16_t; +#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(); +int init_ws(); + +#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 + + struct my_itimerspec { struct timespec it_interval; /* Timer interval */ struct timespec it_value; /* Initial expiration */ @@ -181,11 +210,11 @@ struct fd_info_t }; struct pseudo_header { - u_int32_t source_address; - u_int32_t dest_address; - u_int8_t placeholder; - u_int8_t protocol; - u_int16_t tcp_length; + u32_t source_address; + u32_t dest_address; + unsigned char placeholder; + unsigned char protocol; + unsigned short tcp_length; }; u64_t get_current_time(); diff --git a/lib/fec.cpp b/lib/fec.cpp index f8e1f25..982a7f1 100644 --- a/lib/fec.cpp +++ b/lib/fec.cpp @@ -50,7 +50,7 @@ typedef unsigned long u_long; /* * compatibility stuff */ -#ifdef MSDOS /* but also for others, e.g. sun... */ +#if defined(MSDOS)||defined(__MINGW32__) /* but also for others, e.g. sun... */ #define NEED_BCOPY #define bcmp(a,b,n) memcmp(a,b,n) #endif diff --git a/libev/ev.c b/libev/ev.c index 3b81376..5d7492d 100644 --- a/libev/ev.c +++ b/libev/ev.c @@ -2473,7 +2473,7 @@ evpipe_write (EV_P_ EV_ATOMIC_T *flag) #ifdef _WIN32 WSABUF buf; DWORD sent; - 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/log.h b/log.h index 37e290d..3eefc75 100755 --- a/log.h +++ b/log.h @@ -2,51 +2,16 @@ #ifndef _LOG_MYLOG_H_ #define _LOG_MYLOG_H_ + #include #include -#include -#include #include #include #include #include - -#include -//#include"aes.h" - -#include - -#include -#include -#include - - -#include //for socket ofcourse -#include -#include //for exit(0); -#include //For errno - the error number -#include //Provides declarations for tcp header -#include -#include //Provides declarations for ip header -//#include -#include -#include -#include -#include -#include - -#include #include #include -//#include -#include - -#include -#include -#include -#include -#include using namespace std; diff --git a/main.cpp b/main.cpp index b417683..f59975a 100644 --- a/main.cpp +++ b/main.cpp @@ -95,12 +95,15 @@ void sigint_cb(struct ev_loop *l, ev_signal *w, int revents) int main(int argc, char *argv[]) { + init_ws(); //unit_test(); 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); +#endif ev_signal signal_watcher_sigterm; ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM); diff --git a/makefile b/makefile index d43cdd0..d865ac1 100755 --- a/makefile +++ b/makefile @@ -23,6 +23,10 @@ cygwin:git_version rm -f ${NAME} ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -ggdb -static -O3 -D_GNU_SOURCE +mingw:git_version + rm -f ${NAME} + ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -ggdb -static -O2 -lws2_32 + mac:git_version rm -f ${NAME} ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -ggdb -O3 diff --git a/misc.cpp b/misc.cpp index da08439..4b91cf2 100644 --- a/misc.cpp +++ b/misc.cpp @@ -345,13 +345,13 @@ int unit_test() assert((void*)&test111.u64==(void*)&test111.arry[0]); //printf("%llx,%llx\n",&ttt.u64,&ttt.arry[0]); - printf("%llx\n",get_fake_random_number_64()); - printf("%llx\n",get_fake_random_number_64()); - printf("%llx\n",get_fake_random_number_64()); +// printf("%lld\n",get_fake_random_number_64()); +// printf("%lld\n",get_fake_random_number_64()); +// printf("%lld\n",get_fake_random_number_64()); - printf("%x\n",get_fake_random_number()); - printf("%x\n",get_fake_random_number()); - printf("%x\n",get_fake_random_number()); +// printf("%x\n",get_fake_random_number()); +// printf("%x\n",get_fake_random_number()); +// printf("%x\n",get_fake_random_number()); char buf[10]; get_fake_random_chars(buf,10); diff --git a/my_ev_common.h b/my_ev_common.h index 0755f45..5129fd4 100644 --- a/my_ev_common.h +++ b/my_ev_common.h @@ -3,3 +3,11 @@ #define EV_COMMON void *data; unsigned long long u64; #define EV_COMPAT3 0 //#define EV_VERIFY 2 + +#if defined(__MINGW32__) +# 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