| ... | ... |
@@ -1,5 +1,6 @@ |
| 1 | 1 |
#if !defined(__linux__) && !defined(ANDROID) |
| 2 | 2 |
#include <winsock2.h> |
| 3 |
+#include <synchapi.h> |
|
| 3 | 4 |
typedef SOCKET socket_t; /* https://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c */ |
| 4 | 5 |
#ifndef SHUT_WR |
| 5 | 6 |
#define SHUT_WR SD_SEND |
| ... | ... |
@@ -147,4 +148,10 @@ win32pipe_write(int fd, char *buf, int count) |
| 147 | 148 |
return(send(fd,buf,count,0)); |
| 148 | 149 |
} |
| 149 | 150 |
|
| 151 |
+int |
|
| 152 |
+win32pipe_sleep(int ms) |
|
| 153 |
+{
|
|
| 154 |
+ Sleep(ms); |
|
| 155 |
+ return(0); |
|
| 156 |
+} |
|
| 150 | 157 |
|
| ... | ... |
@@ -8,6 +8,9 @@ typedef SOCKET socket_t; /* https://stackoverflow.com/questions/10817252/why-is- |
| 8 | 8 |
#define SHUT_RD SD_RECEIVE |
| 9 | 9 |
#endif |
| 10 | 10 |
#define close(s) closesocket(s) |
| 11 |
+#ifndef messagebox |
|
| 12 |
+#define messagebox(str) MessageBoxA(NULL,str,"win32_pipe",0); |
|
| 13 |
+#endif |
|
| 11 | 14 |
#else |
| 12 | 15 |
#include <sys/socket.h> |
| 13 | 16 |
#include <netinet/in.h> |
| ... | ... |
@@ -73,32 +76,57 @@ win32pipe_pipe(int fds[2]) |
| 73 | 76 |
socket_t serverfd,workfd,clientfd; |
| 74 | 77 |
struct sockaddr_in s; |
| 75 | 78 |
int sizes; |
| 79 |
+ unsigned char *ip; |
|
| 80 |
+ char *errstr; |
|
| 76 | 81 |
win32pipe_init(); |
| 77 | 82 |
FILLIPV4ADDR(s,AF_INET,htonl(INADDR_ANY),0); |
| 83 |
+ ip=(unsigned char *) &(s.sin_addr.s_addr); |
|
| 84 |
+ ip[0]=127,ip[1]=0,ip[2]=0,ip[3]=1; |
|
| 78 | 85 |
serverfd=workfd=clientfd=INVALID_SOCKET; |
| 79 |
- if((serverfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET |
|
| 86 |
+ if((errstr="Couldn't create server socket")==NULL |
|
| 87 |
+ || (serverfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET |
|
| 88 |
+ || (errstr="Couldn't bind server socket")==NULL |
|
| 80 | 89 |
|| bind(serverfd,(struct sockaddr *)&s,sizeof(s))!=0 |
| 90 |
+ || (errstr="Couldn't listen on server socket")==NULL |
|
| 81 | 91 |
|| listen(serverfd,4)==-1 |
| 82 | 92 |
|| (sizes=sizeof(s))!=sizeof(s) |
| 93 |
+ || (errstr="Couldn't get server socket name")==NULL |
|
| 83 | 94 |
|| getsockname(serverfd,(struct sockaddr *)&s,&sizes)==-1 |
| 95 |
+ || (errstr="Couldn't create client socket")==NULL |
|
| 84 | 96 |
|| (clientfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET |
| 97 |
+ || (errstr="Couldn't connect client socket to server socket")==NULL |
|
| 85 | 98 |
|| connect(clientfd,(struct sockaddr *)&s,sizeof(s))==-1 |
| 99 |
+ || (errstr="Couldn't accept client socket connection to server socket")==NULL |
|
| 86 | 100 |
|| (workfd=accept(serverfd,(struct sockaddr *)&s,&sizes))==-1 |
| 87 | 101 |
) {
|
| 102 |
+ {
|
|
| 103 |
+ char errbuf[4096]; |
|
| 104 |
+ unsigned char *ip; |
|
| 105 |
+ int port; |
|
| 106 |
+ if(strcmp(errstr,"Couldn't connect client socket to server socket")==0) {
|
|
| 107 |
+ ip=(unsigned char *) &(s.sin_addr.s_addr); |
|
| 108 |
+ port=ntohs(s.sin_port); |
|
| 109 |
+ snprintf(errbuf,sizeof(errbuf),"%s\nWinsock error: %ld\nRemote address:%i.%i.%i.%i\nRemote port:%i" |
|
| 110 |
+ ,errstr |
|
| 111 |
+ ,WSAGetLastError() |
|
| 112 |
+ ,(int)(ip[0]),(int)(ip[1]),(int)(ip[2]),(int)(ip[3]) |
|
| 113 |
+ ,port |
|
| 114 |
+ ); |
|
| 115 |
+ } else {
|
|
| 116 |
+ snprintf(errbuf,sizeof(errbuf),"%s\nWinsock error: %ld",errstr,WSAGetLastError()); |
|
| 117 |
+ } |
|
| 118 |
+ errbuf[sizeof(errbuf)-1]='\0'; |
|
| 119 |
+ messagebox(errbuf); |
|
| 120 |
+ } |
|
| 121 |
+ |
|
| 88 | 122 |
if(serverfd!=-1) |
| 89 | 123 |
close(serverfd),serverfd=-1; |
| 90 | 124 |
if(clientfd!=-1) |
| 91 | 125 |
close(clientfd),clientfd=-1; |
| 92 | 126 |
if(workfd!=-1) |
| 93 | 127 |
close(workfd),workfd=-1; |
| 94 |
-#if 1 |
|
| 95 |
-fprintf(stderr,"win32_pipe error creating socket\n"); |
|
| 96 |
-#endif |
|
| 97 | 128 |
return(-1); |
| 98 | 129 |
} |
| 99 |
-#if 1 |
|
| 100 |
-fprintf(stderr,"win32_pipe socket generated\n"); |
|
| 101 |
-#endif |
|
| 102 | 130 |
close(serverfd),serverfd=-1; |
| 103 | 131 |
shutdown(workfd,SHUT_WR); |
| 104 | 132 |
shutdown(clientfd,SHUT_RD); |
| ... | ... |
@@ -1,4 +1,20 @@ |
| 1 |
+#if !defined(__linux__) && !defined(ANDROID) |
|
| 1 | 2 |
#include <winsock2.h> |
| 3 |
+typedef SOCKET socket_t; /* https://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c */ |
|
| 4 |
+#ifndef SHUT_WR |
|
| 5 |
+#define SHUT_WR SD_SEND |
|
| 6 |
+#endif |
|
| 7 |
+#ifndef SHUT_RD |
|
| 8 |
+#define SHUT_RD SD_RECEIVE |
|
| 9 |
+#endif |
|
| 10 |
+#define close(s) closesocket(s) |
|
| 11 |
+#else |
|
| 12 |
+#include <sys/socket.h> |
|
| 13 |
+#include <netinet/in.h> |
|
| 14 |
+#include <fcntl.h> |
|
| 15 |
+typedef int socket_t; |
|
| 16 |
+#define INVALID_SOCKET -1 |
|
| 17 |
+#endif |
|
| 2 | 18 |
|
| 3 | 19 |
#ifndef FILLIPV4ADDR |
| 4 | 20 |
#define FILLIPV4ADDR(a,family,addr,port) \ |
| ... | ... |
@@ -8,37 +24,66 @@ |
| 8 | 24 |
s.sin_port=htons(port) |
| 9 | 25 |
#endif |
| 10 | 26 |
|
| 11 |
-#ifndef SHUT_WR |
|
| 12 |
-#define SHUT_WR SD_SEND |
|
| 27 |
+ |
|
| 28 |
+#if 1 |
|
| 29 |
+#include <stdio.h> |
|
| 13 | 30 |
#endif |
| 14 | 31 |
|
| 15 |
-#ifndef SHUT_RD |
|
| 16 |
-#define SHUT_RD SD_RECEIVE |
|
| 32 |
+static int * |
|
| 33 |
+win32pipe_initvalue(void) |
|
| 34 |
+{
|
|
| 35 |
+ static int init=0; |
|
| 36 |
+ return(&init); |
|
| 37 |
+} |
|
| 38 |
+ |
|
| 39 |
+int |
|
| 40 |
+win32pipe_init(void) |
|
| 41 |
+{
|
|
| 42 |
+ int *init; |
|
| 43 |
+ init=win32pipe_initvalue(); |
|
| 44 |
+ if(*init!=0) |
|
| 45 |
+ return(0); |
|
| 46 |
+ *init=1; |
|
| 47 |
+#if !defined(__linux__) && !defined(ANDROID) |
|
| 48 |
+ {
|
|
| 49 |
+ WSADATA wsaData; |
|
| 50 |
+ return(WSAStartup(0x202,&wsaData)); |
|
| 51 |
+ } |
|
| 52 |
+#else |
|
| 53 |
+ return(0); |
|
| 17 | 54 |
#endif |
| 55 |
+} |
|
| 18 | 56 |
|
| 19 |
-#define close(s) closesocket(s) |
|
| 57 |
+void |
|
| 58 |
+win32pipe_fini(void) |
|
| 59 |
+{
|
|
| 60 |
+ int *init; |
|
| 61 |
+ init=win32pipe_initvalue(); |
|
| 62 |
+ if(*init==0) |
|
| 63 |
+ return; |
|
| 64 |
+#if !defined(__linux__) && !defined(ANDROID) |
|
| 65 |
+ WSACleanup(); |
|
| 66 |
+#endif |
|
| 67 |
+ *init=0; |
|
| 68 |
+} |
|
| 20 | 69 |
|
| 21 | 70 |
int |
| 22 |
-win32_pipe(int fds[2]) |
|
| 71 |
+win32pipe_pipe(int fds[2]) |
|
| 23 | 72 |
{
|
| 24 |
- int serverfd,workfd,clientfd; |
|
| 25 |
- struct sockaddr_in c,s; |
|
| 26 |
- int sizes,sizec; |
|
| 73 |
+ socket_t serverfd,workfd,clientfd; |
|
| 74 |
+ struct sockaddr_in s; |
|
| 75 |
+ int sizes; |
|
| 76 |
+ win32pipe_init(); |
|
| 27 | 77 |
FILLIPV4ADDR(s,AF_INET,htonl(INADDR_ANY),0); |
| 28 |
- serverfd=workfd=clientfd=-1; |
|
| 29 |
- if((serverfd=socket(AF_INET,SOCK_STREAM,0))==-1 |
|
| 78 |
+ serverfd=workfd=clientfd=INVALID_SOCKET; |
|
| 79 |
+ if((serverfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET |
|
| 30 | 80 |
|| bind(serverfd,(struct sockaddr *)&s,sizeof(s))!=0 |
| 31 |
- || listen(serverfd,1)==-1 |
|
| 81 |
+ || listen(serverfd,4)==-1 |
|
| 32 | 82 |
|| (sizes=sizeof(s))!=sizeof(s) |
| 33 | 83 |
|| getsockname(serverfd,(struct sockaddr *)&s,&sizes)==-1 |
| 34 |
- || memcpy(&c,&s,sizeof(c))==NULL |
|
| 35 |
- || (clientfd=socket(AF_INET,SOCK_STREAM,0))==-1 |
|
| 36 |
- || connect(clientfd,(struct sockaddr *)&c,sizeof(c))==-1 |
|
| 37 |
- || (sizes=sizeof(s))!=sizeof(s) |
|
| 84 |
+ || (clientfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET |
|
| 85 |
+ || connect(clientfd,(struct sockaddr *)&s,sizeof(s))==-1 |
|
| 38 | 86 |
|| (workfd=accept(serverfd,(struct sockaddr *)&s,&sizes))==-1 |
| 39 |
- || (sizec=sizeof(c))!=sizeof(c) |
|
| 40 |
- || getsockname(workfd,(struct sockaddr *)&c,&sizec)==-1 |
|
| 41 |
- || memcmp(&c,&s,sizeof(c))!=0 |
|
| 42 | 87 |
) {
|
| 43 | 88 |
if(serverfd!=-1) |
| 44 | 89 |
close(serverfd),serverfd=-1; |
| ... | ... |
@@ -46,7 +91,14 @@ win32_pipe(int fds[2]) |
| 46 | 91 |
close(clientfd),clientfd=-1; |
| 47 | 92 |
if(workfd!=-1) |
| 48 | 93 |
close(workfd),workfd=-1; |
| 94 |
+#if 1 |
|
| 95 |
+fprintf(stderr,"win32_pipe error creating socket\n"); |
|
| 96 |
+#endif |
|
| 97 |
+ return(-1); |
|
| 49 | 98 |
} |
| 99 |
+#if 1 |
|
| 100 |
+fprintf(stderr,"win32_pipe socket generated\n"); |
|
| 101 |
+#endif |
|
| 50 | 102 |
close(serverfd),serverfd=-1; |
| 51 | 103 |
shutdown(workfd,SHUT_WR); |
| 52 | 104 |
shutdown(clientfd,SHUT_RD); |
| ... | ... |
@@ -55,3 +107,16 @@ win32_pipe(int fds[2]) |
| 55 | 107 |
return(0); |
| 56 | 108 |
} |
| 57 | 109 |
|
| 110 |
+int |
|
| 111 |
+win32pipe_read(int fd, char *buf, int count) |
|
| 112 |
+{
|
|
| 113 |
+ return(recv(fd,buf,count,0)); |
|
| 114 |
+} |
|
| 115 |
+ |
|
| 116 |
+int |
|
| 117 |
+win32pipe_write(int fd, char *buf, int count) |
|
| 118 |
+{
|
|
| 119 |
+ return(send(fd,buf,count,0)); |
|
| 120 |
+} |
|
| 121 |
+ |
|
| 122 |
+ |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,57 @@ |
| 1 |
+#include <winsock2.h> |
|
| 2 |
+ |
|
| 3 |
+#ifndef FILLIPV4ADDR |
|
| 4 |
+#define FILLIPV4ADDR(a,family,addr,port) \ |
|
| 5 |
+ memset(&(a),0,sizeof(struct sockaddr_in)),\ |
|
| 6 |
+ s.sin_family=family,\ |
|
| 7 |
+ s.sin_addr.s_addr=addr,\ |
|
| 8 |
+ s.sin_port=htons(port) |
|
| 9 |
+#endif |
|
| 10 |
+ |
|
| 11 |
+#ifndef SHUT_WR |
|
| 12 |
+#define SHUT_WR SD_SEND |
|
| 13 |
+#endif |
|
| 14 |
+ |
|
| 15 |
+#ifndef SHUT_RD |
|
| 16 |
+#define SHUT_RD SD_RECEIVE |
|
| 17 |
+#endif |
|
| 18 |
+ |
|
| 19 |
+#define close(s) closesocket(s) |
|
| 20 |
+ |
|
| 21 |
+int |
|
| 22 |
+win32_pipe(int fds[2]) |
|
| 23 |
+{
|
|
| 24 |
+ int serverfd,workfd,clientfd; |
|
| 25 |
+ struct sockaddr_in c,s; |
|
| 26 |
+ int sizes,sizec; |
|
| 27 |
+ FILLIPV4ADDR(s,AF_INET,htonl(INADDR_ANY),0); |
|
| 28 |
+ serverfd=workfd=clientfd=-1; |
|
| 29 |
+ if((serverfd=socket(AF_INET,SOCK_STREAM,0))==-1 |
|
| 30 |
+ || bind(serverfd,(struct sockaddr *)&s,sizeof(s))!=0 |
|
| 31 |
+ || listen(serverfd,1)==-1 |
|
| 32 |
+ || (sizes=sizeof(s))!=sizeof(s) |
|
| 33 |
+ || getsockname(serverfd,(struct sockaddr *)&s,&sizes)==-1 |
|
| 34 |
+ || memcpy(&c,&s,sizeof(c))==NULL |
|
| 35 |
+ || (clientfd=socket(AF_INET,SOCK_STREAM,0))==-1 |
|
| 36 |
+ || connect(clientfd,(struct sockaddr *)&c,sizeof(c))==-1 |
|
| 37 |
+ || (sizes=sizeof(s))!=sizeof(s) |
|
| 38 |
+ || (workfd=accept(serverfd,(struct sockaddr *)&s,&sizes))==-1 |
|
| 39 |
+ || (sizec=sizeof(c))!=sizeof(c) |
|
| 40 |
+ || getsockname(workfd,(struct sockaddr *)&c,&sizec)==-1 |
|
| 41 |
+ || memcmp(&c,&s,sizeof(c))!=0 |
|
| 42 |
+ ) {
|
|
| 43 |
+ if(serverfd!=-1) |
|
| 44 |
+ close(serverfd),serverfd=-1; |
|
| 45 |
+ if(clientfd!=-1) |
|
| 46 |
+ close(clientfd),clientfd=-1; |
|
| 47 |
+ if(workfd!=-1) |
|
| 48 |
+ close(workfd),workfd=-1; |
|
| 49 |
+ } |
|
| 50 |
+ close(serverfd),serverfd=-1; |
|
| 51 |
+ shutdown(workfd,SHUT_WR); |
|
| 52 |
+ shutdown(clientfd,SHUT_RD); |
|
| 53 |
+ fds[0]=workfd,workfd=-1; |
|
| 54 |
+ fds[1]=clientfd,clientfd=-1; |
|
| 55 |
+ return(0); |
|
| 56 |
+} |
|
| 57 |
+ |