Browse Source

works under mingw, but not finish yet

U-DESKTOP-T772REH\wangyu 7 years ago
parent
commit
8c21ff55ab
9 changed files with 158 additions and 56 deletions
  1. 93 0
      common.cpp
  2. 41 12
      common.h
  3. 1 1
      lib/fec.cpp
  4. 1 1
      libev/ev.c
  5. 1 36
      log.h
  6. 3 0
      main.cpp
  7. 4 0
      makefile
  8. 6 6
      misc.cpp
  9. 8 0
      my_ev_common.h

+ 93 - 0
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
 }
 
 

+ 41 - 12
common.h

@@ -17,8 +17,6 @@
 
 #include<unistd.h>
 #include<errno.h>
-#include <sys/wait.h>
-#include <sys/socket.h>    //for socket ofcourse
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h> //for exit(0);
@@ -27,19 +25,26 @@
 //#include <netinet/udp.h>
 //#include <netinet/ip.h>    //Provides declarations for ip header
 //#include <netinet/if_ether.h>
-#include <arpa/inet.h>
 #include <fcntl.h>
-#include <arpa/inet.h>
 #include <sys/time.h>
 #include <time.h>
-#include <sys/ioctl.h>
 //#include <netinet/in.h>
-#include <net/if.h>
-#include <arpa/inet.h>
+//#include <net/if.h>
 #include <stdarg.h>
 #include <assert.h>
 #include <my_ev.h>
 
+#if defined(__MINGW32__)
+#include <winsock2.h>
+#include <Ws2tcpip.h >
+typedef int socklen_t;
+#else
+#include <sys/socket.h> 
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <arpa/inet.h>
+#endif
+
 
 #include<unordered_map>
 #include<unordered_set>
@@ -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();

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 36
log.h

@@ -2,51 +2,16 @@
 #ifndef _LOG_MYLOG_H_
 #define _LOG_MYLOG_H_
 
+
 #include<stdio.h>
 #include<string.h>
-#include<sys/socket.h>
-#include<arpa/inet.h>
 #include<stdlib.h>
 #include<getopt.h>
 #include <unistd.h>
 #include<errno.h>
-
-#include <fcntl.h>
-//#include"aes.h"
-
-#include <sys/wait.h>
-
-#include<map>
-#include<string>
-#include<vector>
-
-
-#include <sys/socket.h>    //for socket ofcourse
-#include <sys/types.h>
-#include <stdlib.h> //for exit(0);
-#include <errno.h> //For errno - the error number
-#include <netinet/tcp.h>   //Provides declarations for tcp header
-#include <netinet/udp.h>
-#include <netinet/ip.h>    //Provides declarations for ip header
-//#include <netinet/if_ether.h>
-#include <arpa/inet.h>
-#include <fcntl.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <arpa/inet.h>
-
-#include <sys/time.h>
 #include <time.h>
 
 #include <set>
-//#include <encrypt.h>
-#include <inttypes.h>
-
-#include <sys/ioctl.h>
-#include <netinet/in.h>
-#include <net/if.h>
-#include <arpa/inet.h>
-#include <stdarg.h>
 
 using namespace std;
 

+ 3 - 0
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);

+ 4 - 0
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

+ 6 - 6
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);

+ 8 - 0
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