Browse Source

broken lose32 port using mingw (don't use!!!)

initial Solaris /dev/poll port
master
Felix von Leitner 18 years ago
parent
commit
d0cc758bf8
  1. 2
      CHANGES
  2. 18
      GNUmakefile
  3. 3
      buffer/buffer_1.c
  4. 2
      dns/dns_random.c
  5. 3
      dns/dns_rcrw.c
  6. 4
      dns/dns_transmit.c
  7. 2
      io/io_closeonexec.c
  8. 14
      io/io_dontwantread.c
  9. 14
      io/io_dontwantwrite.c
  10. 3
      io/io_eagain.c
  11. 17
      io/io_fd.c
  12. 15
      io/io_nonblock.c
  13. 6
      io/io_pipe.c
  14. 37
      io/io_waituntil2.c
  15. 14
      io/io_wantread.c
  16. 14
      io/io_wantwrite.c
  17. 15
      io_internal.h
  18. 21
      mmap/mmap_private.c
  19. 19
      mmap/mmap_read.c
  20. 19
      mmap/mmap_shared.c
  21. 6
      open/open_append.c
  22. 6
      open/open_excl.c
  23. 6
      open/open_read.c
  24. 6
      open/open_trunc.c
  25. 6
      open/open_write.c
  26. 43
      socket.h
  27. 6
      socket/socket_accept4.c
  28. 11
      socket/socket_accept6.c
  29. 5
      socket/socket_bind4.c
  30. 5
      socket/socket_bind4_reuse.c
  31. 5
      socket/socket_bind6.c
  32. 5
      socket/socket_bind6_reuse.c
  33. 5
      socket/socket_broadcast.c
  34. 5
      socket/socket_connect4.c
  35. 12
      socket/socket_connect6.c
  36. 2
      socket/socket_connected.c
  37. 2
      socket/socket_getifidx.c
  38. 8
      socket/socket_getifname.c
  39. 6
      socket/socket_listen.c
  40. 5
      socket/socket_local4.c
  41. 11
      socket/socket_local6.c
  42. 7
      socket/socket_mchopcount6.c
  43. 9
      socket/socket_mcjoin4.c
  44. 9
      socket/socket_mcjoin6.c
  45. 5
      socket/socket_mcleave4.c
  46. 5
      socket/socket_mcleave6.c
  47. 5
      socket/socket_mcloop4.c
  48. 9
      socket/socket_mcloop6.c
  49. 5
      socket/socket_mcttl4.c
  50. 5
      socket/socket_recv4.c
  51. 10
      socket/socket_recv6.c
  52. 6
      socket/socket_remote4.c
  53. 10
      socket/socket_remote6.c
  54. 5
      socket/socket_send4.c
  55. 11
      socket/socket_send6.c
  56. 7
      socket/socket_tcp4.c
  57. 8
      socket/socket_tcp6.c
  58. 5
      socket/socket_tryreservein.c
  59. 6
      socket/socket_udp4.c
  60. 10
      socket/socket_udp6.c
  61. 17
      taia/taia_now.c
  62. 28
      trydevpoll.c
  63. 9
      tryscope.c
  64. 5
      trysl.c
  65. 6
      unix/iopause.c
  66. 10
      unix/ndelay_off.c
  67. 10
      unix/ndelay_on.c

2
CHANGES

@ -14,6 +14,8 @@
need to try using mmap on OpenBSD and NetBSD to get zero-copy TCP.
guard against same event being signalled twice (confused our list
handling)
add support for Solaris /dev/poll
add lose32 support (broken, please don't use!)
0.16:
add buffer_fromsa (make buffer from stralloc)

18
GNUmakefile

@ -75,7 +75,7 @@ $(DNS_OBJS): dns.h stralloc.h taia.h tai.h uint64.h iopause.h
$(CASE_OBJS): case.h
$(ARRAY_OBJS): uint64.h array.h
$(MULT_OBJS): uint64.h uint32.h uint16.h safemult.h
$(IO_OBJS): uint64.h array.h io.h io_internal.h taia.h tai.h haveepoll.h havekqueue.h havesigio.h havebsdsf.h
$(IO_OBJS): uint64.h array.h io.h io_internal.h taia.h tai.h haveepoll.h havekqueue.h havesigio.h havebsdsf.h havedevpoll.h
iob_addbuf.o iob_addfile.o iob_new.o iob_reset.o iob_send.o: iob_internal.h iob.h
@ -132,7 +132,7 @@ t: t.o libowfat.a
clean:
rm -f *.o *.a *.da *.bbg *.bb core t haveip6.h haven2i.h \
havesl.h haveinline.h iopause.h select.h havekqueue.h haveepoll.h \
libepoll havesigio.h havebsdsf.h Makefile dep
libepoll havesigio.h havebsdsf.h havescope.h havedevpoll.h Makefile dep
INCLUDES=buffer.h byte.h fmt.h ip4.h ip6.h mmap.h scan.h socket.h str.h stralloc.h \
uint16.h uint32.h uint64.h open.h textcode.h tai.h taia.h dns.h iopause.h case.h \
@ -164,6 +164,11 @@ haveip6.h: tryip6.c
if $(DIET) $(CC) $(CFLAGS) -c tryip6.c >/dev/null 2>&1; then echo "#define LIBC_HAS_IP6"; fi > $@
-rm -f tryip6.o
havescope.h: tryscope.c
-rm -f $@
if $(DIET) $(CC) $(CFLAGS) -c tryscope.c >/dev/null 2>&1; then echo "#define LIBC_HAS_SCOPE_ID"; fi > $@
-rm -f tryscope.o
haven2i.h: tryn2i.c
-rm -f $@
if $(DIET) $(CC) $(CFLAGS) -o t tryn2i.c >/dev/null 2>&1; then echo "#define HAVE_N2I"; fi > $@
@ -195,6 +200,11 @@ haveepoll.h: tryepoll.c
if $(DIET) $(CC) $(CFLAGS) -o tryepoll tryepoll.c -lepoll >/dev/null 2>&1; then echo "#define HAVE_EPOLL 2"; fi; fi > $@
-rm -f tryepoll
havedevpoll.h: trydevpoll.c
-rm -f $@
if $(DIET) $(CC) $(CFLAGS) -c trydevpoll.c >/dev/null 2>&1; then echo "#define HAVE_DEVPOLL"; fi > $@
-rm -f trydevpoll.o
libepoll: haveepoll.h
if test "z`cat haveepoll.h`" = "z#define HAVE_EPOLL 2"; then echo -lepoll; fi > $@
@ -216,7 +226,7 @@ select.h: select.h1 select.h2 trysysel.c
socket_accept6.o socket_connect6.o socket_local6.o socket_mchopcount6.o \
socket_mcjoin6.o socket_mcleave6.o socket_mcloop6.o socket_recv6.o \
socket_remote6.o socket_send6.o socket_tcp6.o socket_udp6.o: haveip6.h
socket_remote6.o socket_send6.o socket_tcp6.o socket_udp6.o: haveip6.h havescope.h
socket_getifidx.o socket_getifname.o: haven2i.h
@ -226,7 +236,7 @@ socket_remote6.o: havesl.h
fmt_xlong.o scan_xlong.o fmt_ip6_flat.o $(TEXTCODE_OBJS): haveinline.h
dep: haveip6.h haven2i.h havesl.h haveinline.h iopause.h select.h haveepoll.h havekqueue.h
dep: haveip6.h haven2i.h havesl.h haveinline.h iopause.h select.h haveepoll.h havekqueue.h havedevpoll.h
gcc -I. -MM $(wildcard */*.c) t.c > dep
libdep:

3
buffer/buffer_1.c

@ -1,5 +1,8 @@
#include <unistd.h>
#include "buffer.h"
#ifdef __MINGW32__
#include <io.h>
#endif
char buffer_1_space[BUFFER_INSIZE];
static buffer it = BUFFER_INIT(write,1,buffer_1_space,sizeof buffer_1_space);

2
dns/dns_random.c

@ -45,7 +45,9 @@ void dns_random_init(const char data[128])
uint32_unpack(tpack + 4 * i,in + 4 + i);
in[8] = getpid();
#ifndef __MINGW32__
in[9] = getppid();
#endif
/* more space in 10 and 11, but this is probably enough */
}

3
dns/dns_rcrw.c

@ -5,6 +5,9 @@
#include "str.h"
#include "openreadclose.h"
#include "dns.h"
#ifdef __MINGW32__
#include <winsock2.h>
#endif
static stralloc data = {0};

4
dns/dns_transmit.c

@ -1,5 +1,9 @@
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include "socket.h"

2
io/io_closeonexec.c

@ -4,5 +4,7 @@
#include "io_internal.h"
void io_closeonexec(int64 d) {
#ifndef __MINGW32__
fcntl(d,F_SETFL,fcntl(d,F_GETFL,0) | FD_CLOEXEC);
#endif
}

14
io/io_dontwantread.c

@ -11,6 +11,11 @@
#include <inttypes.h>
#include <sys/epoll.h>
#endif
#ifdef HAVE_DEVPOLL
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/devpoll.h>
#endif
void io_dontwantread(int64 d) {
int newfd;
@ -35,6 +40,15 @@ void io_dontwantread(int64 d) {
ts.tv_sec=0; ts.tv_nsec=0;
kevent(io_master,&kev,1,0,0,&ts);
}
#endif
#ifdef HAVE_DEVPOLL
if (io_waitmode==DEVPOLL) {
struct pollfd x;
x.fd=d;
x.events=0;
if (e->wantwrite) x.events|=POLLOUT;
write(io_master,&x,sizeof(x));
}
#endif
e->wantread=0;
}

14
io/io_dontwantwrite.c

@ -11,6 +11,11 @@
#include <inttypes.h>
#include <sys/epoll.h>
#endif
#ifdef HAVE_DEVPOLL
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/devpoll.h>
#endif
void io_dontwantwrite(int64 d) {
int newfd;
@ -35,6 +40,15 @@ void io_dontwantwrite(int64 d) {
ts.tv_sec=0; ts.tv_nsec=0;
kevent(io_master,&kev,1,0,0,&ts);
}
#endif
#ifdef HAVE_DEVPOLL
if (io_waitmode==DEVPOLL) {
struct pollfd x;
x.fd=d;
x.events=0;
if (e->wantread) x.events|=POLLIN;
write(io_master,&x,sizeof(x));
}
#endif
e->wantwrite=0;
}

3
io/io_eagain.c

@ -3,7 +3,8 @@
void io_eagain(int64 d) {
io_entry* e=array_get(&io_fds,sizeof(io_entry),d);
if (e) {
e->canread=0;
if (e->wantread) e->canread=0;
if (e->wantwrite) e->canwrite=0;
#ifdef HAVE_SIGIO
if (d==alt_firstread) {
debug_printf(("io_eagain: dequeueing %lld from alt read queue (next is %ld)\n",d,e->next_read));

17
io/io_fd.c

@ -18,17 +18,26 @@
#include <sys/epoll.h>
#endif
#include <unistd.h>
#ifdef HAVE_DEVPOLL
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/devpoll.h>
#endif
/* put d on internal data structure, return 1 on success, 0 on error */
int io_fd(int64 d) {
long r;
io_entry* e;
#ifndef __MINGW32__
long r;
if ((r=fcntl(d,F_GETFL,0)) == -1)
return 0; /* file descriptor not open */
#endif
if (!(e=array_allocate(&io_fds,sizeof(io_entry),d))) return 0;
byte_zero(e,sizeof(io_entry));
e->inuse=1;
#ifndef __MINGW32__
if (r&O_NDELAY) e->nonblock=1;
#endif
e->next_read=e->next_write=-1;
if (io_waitmode==UNDECIDED) {
first_readable=first_writeable=-1;
@ -42,6 +51,12 @@ int io_fd(int64 d) {
if (io_master!=-1) io_waitmode=KQUEUE;
}
#endif
#if defined(HAVE_DEVPOLL)
if (io_waitmode==UNDECIDED) {
io_master=open("/dev/poll",O_RDWR);
if (io_master!=-1) io_waitmode=DEVPOLL;
}
#endif
#if defined(HAVE_SIGIO)
alt_firstread=alt_firstwrite=-1;
if (io_waitmode==UNDECIDED) {

15
io/io_nonblock.c

@ -3,8 +3,23 @@
#include <errno.h>
#include "io_internal.h"
#ifdef __MINGW32__
#include <winsock2.h>
#include "windoze.h"
#endif
#ifndef O_NDELAY
#define O_NDELAY O_NONBLOCK
#endif
void io_nonblock(int64 d) {
io_entry* e=array_get(&io_fds,sizeof(io_entry),d);
#ifdef __MINGW32__
unsigned long i=1;
if (ioctlsocket( d, FIONBIO, &i)==0)
if (e) e->nonblock=1;
#else
if (fcntl(d,F_SETFL,fcntl(d,F_GETFL,0) | O_NDELAY)==0)
if (e) e->nonblock=1;
#endif
}

6
io/io_pipe.c

@ -2,9 +2,15 @@
#include "io_internal.h"
int io_pipe(int64* d) {
#ifdef __MINGW32__
HANDLE fds[2];
if (CreatePipe(fds,fds+1,0,0)==0)
return 0;
#else
int fds[2];
if (pipe(fds)==-1)
return 0;
#endif
if (io_fd(fds[1])) {
if (io_fd(fds[0])) {
d[0]=fds[0];

37
io/io_waituntil2.c

@ -14,6 +14,11 @@
#include <inttypes.h>
#include <sys/epoll.h>
#endif
#ifdef HAVE_DEVPOLL
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/devpoll.h>
#endif
int64 io_waituntil2(int64 milliseconds) {
struct pollfd* p;
@ -79,6 +84,38 @@ int64 io_waituntil2(int64 milliseconds) {
return n;
}
#endif
#ifdef HAVE_DEVPOLL
if (io_waitmode==DEVPOLL) {
dvpoll_t timeout;
struct pollfd y[100];
int n;
timeout.dp_timeout=milliseconds;
timeout.dp_nfds=100;
timeout.dp_fds=y;
if ((n=ioctl(io_master,DP_POLL,&timeout))==-1) return -1;
for (i=n-1; i>=0; --i) {
io_entry* e=array_get(&io_fds,sizeof(io_entry),y[--n].fd);
if (e) {
if (y[n].revents&(POLLERR|POLLHUP)) {
/* error; signal whatever app is looking for */
if (e->wantread) y[n].revents=POLLIN; else
if (e->wantwrite) y[n].revents=POLLOUT;
}
if (!e->canread && (y[n].revents==POLLIN)) {
e->canread=1;
e->next_read=first_readable;
first_readable=y[n].fd;
}
if (!e->canwrite && (y[n].revents==POLLOUT)) {
e->canwrite=1;
e->next_write=first_writeable;
first_writeable=y[i].fd;
}
}
}
return n;
}
#endif
#ifdef HAVE_SIGIO
if (io_waitmode==_SIGIO) {
siginfo_t info;

14
io/io_wantread.c

@ -14,6 +14,11 @@
#ifdef HAVE_SIGIO
#include <sys/poll.h>
#endif
#ifdef HAVE_DEVPOLL
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/devpoll.h>
#endif
void io_wantread(int64 d) {
int newfd;
@ -39,6 +44,15 @@ void io_wantread(int64 d) {
kevent(io_master,&kev,1,0,0,&ts);
}
#endif
#ifdef HAVE_DEVPOLL
if (io_waitmode==DEVPOLL) {
struct pollfd x;
x.fd=d;
x.events=POLLIN;
if (e->wantwrite) x.events|=POLLOUT;
write(io_master,&x,sizeof(x));
}
#endif
#ifdef HAVE_SIGIO
if (io_waitmode==_SIGIO) {
struct pollfd p;

14
io/io_wantwrite.c

@ -14,6 +14,11 @@
#ifdef HAVE_SIGIO
#include <sys/poll.h>
#endif
#ifdef HAVE_DEVPOLL
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/devpoll.h>
#endif
void io_wantwrite(int64 d) {
int newfd;
@ -39,6 +44,15 @@ void io_wantwrite(int64 d) {
kevent(io_master,&kev,1,0,0,&ts);
}
#endif
#ifdef HAVE_DEVPOLL
if (io_waitmode==DEVPOLL) {
struct pollfd x;
x.fd=d;
x.events=POLLOUT;
if (e->wantread) x.events|=POLLIN;
write(io_master,&x,sizeof(x));
}
#endif
#ifdef HAVE_SIGIO
if (io_waitmode==_SIGIO) {
struct pollfd p;

15
io_internal.h

@ -2,12 +2,18 @@
#include "array.h"
#include "haveepoll.h"
#include "havekqueue.h"
#include "havedevpoll.h"
#include "havesigio.h"
#ifdef HAVE_SIGIO
#define _GNU_SOURCE
#include <signal.h>
#endif
#ifdef __MINGW32__
#include "socket.h"
extern HANDLE io_comport;
#endif
typedef struct {
unsigned int wantread:1;
unsigned int wantwrite:1;
@ -19,6 +25,10 @@ typedef struct {
long next_read;
long next_write;
void* cookie;
#ifdef __MINGW32__
OVERLAPPED o;
HANDLE fd;
#endif
} io_entry;
extern array io_fds;
@ -40,9 +50,12 @@ enum {
#ifdef HAVE_SIGIO
,_SIGIO
#endif
#ifdef HAVE_DEVPOLL
,DEVPOLL
#endif
} io_waitmode;
#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL) || defined(HAVE_DEVPOLL)
extern int io_master;
#endif
#if defined(HAVE_SIGIO)

21
mmap/mmap_private.c

@ -1,10 +1,28 @@
#include <sys/types.h>
#include <unistd.h>
#ifdef __MINGW32__
#include <windows.h>
#else
#include <sys/mman.h>
#endif
#include "open.h"
#include "mmap.h"
extern char* mmap_private(const char* filename,unsigned long* filesize) {
char* mmap_private(const char* filename,unsigned long* filesize) {
#ifdef __MINGW32__
HANDLE fd,m;
char* map;
fd=CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (fd==INVALID_HANDLE_VALUE)
return 0;
m=CreateFileMapping(fd,0,PAGE_WRITECOPY,0,0,NULL);
map=0;
if (m)
map=MapViewOfFile(m,FILE_MAP_COPY,0,0,0);
CloseHandle(m);
CloseHandle(fd);
return map;
#else
int fd=open_read(filename);
char *map;
if (fd>=0) {
@ -16,4 +34,5 @@ extern char* mmap_private(const char* filename,unsigned long* filesize) {
return map;
}
return 0;
#endif
}

19
mmap/mmap_read.c

@ -1,10 +1,28 @@
#include <sys/types.h>
#include <unistd.h>
#ifdef __MINGW32__
#include <windows.h>
#else
#include <sys/mman.h>
#endif
#include "open.h"
#include "mmap.h"
extern char* mmap_read(const char* filename,unsigned long* filesize) {
#ifdef __MINGW32__
HANDLE fd,m;
char* map;
fd=CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (fd==INVALID_HANDLE_VALUE)
return 0;
m=CreateFileMapping(fd,0,PAGE_READONLY,0,0,NULL);
map=0;
if (m)
map=MapViewOfFile(m,FILE_MAP_READ,0,0,0);
CloseHandle(m);
CloseHandle(fd);
return map;
#else
int fd=open_read(filename);
char *map;
if (fd>=0) {
@ -16,4 +34,5 @@ extern char* mmap_read(const char* filename,unsigned long* filesize) {
return map;
}
return 0;
#endif
}

19
mmap/mmap_shared.c

@ -1,10 +1,28 @@
#include <sys/types.h>
#include <unistd.h>
#ifdef __MINGW32__
#include <windows.h>
#else
#include <sys/mman.h>
#endif
#include "open.h"
#include "mmap.h"
extern char* mmap_shared(const char* filename,unsigned long* filesize) {
#ifdef __MINGW32__
HANDLE fd,m;
char* map;
fd=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (fd==INVALID_HANDLE_VALUE)
return 0;
m=CreateFileMapping(fd,0,PAGE_READWRITE,0,0,NULL);
map=0;
if (m)
map=MapViewOfFile(m,FILE_MAP_WRITE,0,0,0);
CloseHandle(m);
CloseHandle(fd);
return map;
#else
int fd=open_read(filename);
char *map;
if (fd>=0) {
@ -16,4 +34,5 @@ extern char* mmap_shared(const char* filename,unsigned long* filesize) {
return map;
}
return 0;
#endif
}

6
open/open_append.c

@ -2,6 +2,10 @@
#include <fcntl.h>
#include "open.h"
extern int open_append(const char *filename) {
#ifndef O_NDELAY
#define O_NDELAY 0
#endif
int open_append(const char *filename) {
return open(filename,O_WRONLY|O_NDELAY|O_APPEND|O_CREAT,0600);
}

6
open/open_excl.c

@ -2,6 +2,10 @@
#include <fcntl.h>
#include "open.h"
extern int open_excl(const char *filename) {
#ifndef O_NDELAY
#define O_NDELAY 0
#endif
int open_excl(const char *filename) {
return open(filename,O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT|O_EXCL,0600);
}

6
open/open_read.c

@ -2,6 +2,10 @@
#include <fcntl.h>
#include "open.h"
extern int open_read(const char *filename) {
#ifndef O_NDELAY
#define O_NDELAY 0
#endif
int open_read(const char *filename) {
return open(filename,O_RDONLY|O_NDELAY);
}

6
open/open_trunc.c

@ -2,6 +2,10 @@
#include <fcntl.h>
#include "open.h"
extern int open_trunc(const char *filename) {
#ifndef O_NDELAY
#define O_NDELAY 0
#endif
int open_trunc(const char *filename) {
return open(filename,O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT,0644);
}

6
open/open_write.c

@ -2,6 +2,10 @@
#include <fcntl.h>
#include "open.h"
extern int open_write(const char *filename) {
#ifndef O_NDELAY
#define O_NDELAY 0
#endif
int open_write(const char *filename) {
return open(filename,O_WRONLY|O_CREAT|O_NDELAY,0644);
}

43
socket.h

@ -55,4 +55,47 @@ int socket_sendfile(int out,int in,uint32 offset,uint32 bytes);
int noipv6;
#ifdef __MINGW32__
#include <winsock2.h>
#include <ws2tcpip.h>
#define EWOULDBLOCK WSAEWOULDBLOCK
#define EINPROGRESS WSAEINPROGRESS
#define EALREADY WSAEALREADY
#define ENOTSOCK WSAENOTSOCK
#define EDESTADDRREQ WSAEDESTADDRREQ
#define EMSGSIZE WSAEMSGSIZE
#define EPROTOTYPE WSAEPROTOTYPE
#define ENOPROTOOPT WSAENOPROTOOPT
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
#define EOPNOTSUPP WSAEOPNOTSUPP
#define EPFNOSUPPORT WSAEPFNOSUPPORT
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define EADDRINUSE WSAEADDRINUSE
#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
#define ENETDOWN WSAENETDOWN
#define ENETUNREACH WSAENETUNREACH
#define ENETRESET WSAENETRESET
#define ECONNABORTED WSAECONNABORTED
#define ECONNRESET WSAECONNRESET
#define ENOBUFS WSAENOBUFS
#define EISCONN WSAEISCONN
#define ENOTCONN WSAENOTCONN
#define ESHUTDOWN WSAESHUTDOWN
#define ETOOMANYREFS WSAETOOMANYREFS
#define ETIMEDOUT WSAETIMEDOUT
#define ECONNREFUSED WSAECONNREFUSED
#define ELOOP WSAELOOP
#define EHOSTDOWN WSAEHOSTDOWN
#define EHOSTUNREACH WSAEHOSTUNREACH
#define EPROCLIM WSAEPROCLIM
#define EUSERS WSAEUSERS
#define EDQUOT WSAEDQUOT
#define ESTALE WSAESTALE
#define EREMOTE WSAEREMOTE
#define EDISCON WSAEDISCON
#endif
#endif

6
socket/socket_accept4.c

@ -1,6 +1,9 @@
#ifndef __MINGW32__
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "havesl.h"
@ -8,7 +11,8 @@ int socket_accept4(int s,char *ip,uint16 *port) {
struct sockaddr_in si;
socklen_t len = sizeof si;
int fd;
if ((fd=accept(s,(struct sockaddr*) &si,&len))<0) return -1;
if ((fd=accept(s,(struct sockaddr*) &si,&len))==-1)
return winsock2errno(-1);
*(uint32*)ip = *(uint32*)&si.sin_addr;
uint16_unpack_big((char *) &si.sin_port,port);
return fd;

11
socket/socket_accept6.c

@ -1,12 +1,16 @@
#include <sys/param.h>
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "socket.h"
#include "ip6.h"
#include "haveip6.h"
#include "havesl.h"
#include "havescope.h"
int socket_accept6(int s,char* ip,uint16* port,uint32* scope_id)
{
@ -19,7 +23,8 @@ int socket_accept6(int s,char* ip,uint16* port,uint32* scope_id)
int fd;
fd = accept(s,(struct sockaddr *) &sa,&dummy);
if (fd == -1) return -1;
if (fd == -1)
return winsock2errno(-1);
#ifdef LIBC_HAS_IP6
if (sa.sin6_family==AF_INET) {
@ -33,7 +38,11 @@ int socket_accept6(int s,char* ip,uint16* port,uint32* scope_id)
}
if (ip) byte_copy(ip,16,(char *) &sa.sin6_addr);
if (port) uint16_unpack_big((char *) &sa.sin6_port,port);
#ifdef HAVE_SCOPE_ID
if (scope_id) *scope_id=sa.sin6_scope_id;
#else
if (scope_id) *scope_id=0;
#endif
return fd;
#else

5
socket/socket_bind4.c

@ -1,6 +1,9 @@
#ifndef __MINGW32__
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "uint16.h"
#include "uint32.h"
@ -15,5 +18,5 @@ int socket_bind4(int s,const char *ip,uint16 port) {
*(uint32*)&si.sin_addr = *(uint32*)ip;
else
si.sin_addr.s_addr=INADDR_ANY;
return bind(s,(struct sockaddr*)&si,sizeof si);
return winsock2errno(bind(s,(struct sockaddr*)&si,sizeof si));
}

5
socket/socket_bind4_reuse.c

@ -1,9 +1,12 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#endif
#include "socket.h"
#include "windoze.h"
int socket_bind4_reuse(int s,const char *ip,uint16 port) {
int one=1;
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&one,sizeof one);
return socket_bind4(s,ip,port);
return winsock2errno(socket_bind4(s,ip,port));
}

5
socket/socket_bind6.c

@ -1,8 +1,11 @@
#include <errno.h>
#include "haveip6.h"
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "ip6.h"
#include "byte.h"
#include "socket.h"
@ -32,7 +35,7 @@ int socket_bind6(int s,const char ip[16],uint16 port,uint32 scope_id)
byte_copy((char *) &sa.sin6_addr,16,ip);
sa.sin6_scope_id=scope_id;
return bind(s,(struct sockaddr *) &sa,sizeof sa);
return winsock2errno(bind(s,(struct sockaddr *) &sa,sizeof sa));
#else
errno=EPROTONOSUPPORT;
return -1;

5
socket/socket_bind6_reuse.c

@ -1,9 +1,12 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#endif
#include "socket.h"
#include "windoze.h"
int socket_bind6_reuse(int s,const char *ip,uint16 port,uint32 scope_id) {
int one=1;
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&one,sizeof one);
return socket_bind6(s,ip,port,scope_id);
return winsock2errno(socket_bind6(s,ip,port,scope_id));
}

5
socket/socket_broadcast.c

@ -1,12 +1,15 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "socket.h"
#include "windoze.h"
int socket_broadcast(int s)
{
int opt = 1;
return setsockopt(s,SOL_SOCKET,SO_BROADCAST,&opt,sizeof opt);
return winsock2errno(setsockopt(s,SOL_SOCKET,SO_BROADCAST,&opt,sizeof opt));
}

5
socket/socket_connect4.c

@ -1,6 +1,9 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "socket.h"
@ -13,5 +16,5 @@ int socket_connect4(int s,const char *ip,uint16 port) {
si.sin_family=AF_INET;
uint16_pack_big((char*) &si.sin_port,port);
*((uint32*)&si.sin_addr) = *((uint32*)ip);
return (connect(s,(struct sockaddr*)&si,sizeof(si)));
return (winsock2errno(connect(s,(struct sockaddr*)&si,sizeof(si))));
}

12
socket/socket_connect6.c

@ -1,7 +1,10 @@
#include <sys/param.h>
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include <errno.h>
#include "byte.h"
#include "socket.h"
@ -9,6 +12,7 @@
#include "haveip6.h"
#include "uint32.h"
#include "ip4.h"
#include "havescope.h"
int socket_connect6(int s,const char ip[16],uint16 port,uint32 scope_id)
{
@ -18,19 +22,21 @@ int socket_connect6(int s,const char ip[16],uint16 port,uint32 scope_id)
if (noipv6) {
#endif
if (ip6_isv4mapped(ip))
return socket_connect4(s,ip+12,port);
return winsock2errno(socket_connect4(s,ip+12,port));
if (byte_equal(ip,16,V6loopback))
return socket_connect4(s,ip4loopback,port);
return winsock2errno(socket_connect4(s,ip4loopback,port));
#ifdef LIBC_HAS_IP6
}
byte_zero(&sa,sizeof sa);
sa.sin6_family = PF_INET6;
uint16_pack_big((char *) &sa.sin6_port,port);
sa.sin6_flowinfo = 0;
#ifdef HAVE_SCOPE_ID
sa.sin6_scope_id = scope_id;
#endif
byte_copy((char *) &sa.sin6_addr,16,ip);
return connect(s,(struct sockaddr *) &sa,sizeof sa);
return winsock2errno(connect(s,(struct sockaddr *) &sa,sizeof sa));
#else
errno=EPROTONOSUPPORT;
return -1;

2
socket/socket_connected.c

@ -1,6 +1,8 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "socket.h"
#include "havesl.h"

2
socket/socket_getifidx.c

@ -1,6 +1,8 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <net/if.h>
#endif
#include "socket.h"
#include "haven2i.h"

8
socket/socket_getifname.c

@ -1,21 +1,23 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <net/if.h>
#endif
#include "socket.h"
#include "haven2i.h"
#ifdef HAVE_N2I
static char ifname[IFNAMSIZ];
const char* socket_getifname(uint32 interface) {
char *tmp=if_indextoname(interface,ifname);
const char* socket_getifname(uint32 _interface) {
char *tmp=if_indextoname(_interface,ifname);
if (tmp)
return tmp;
else
return "[unknown]";
}
#else
const char* socket_getifname(uint32 interface) {
const char* socket_getifname(uint32 _interface) {
return "[unknown]";
}
#endif

6
socket/socket_listen.c

@ -1,6 +1,10 @@
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#endif
#include "socket.h"
#include "windoze.h"
int socket_listen(int s,unsigned int backlog) {
return listen(s, backlog);
return winsock2errno(listen(s, backlog));
}

5
socket/socket_local4.c

@ -1,7 +1,10 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "socket.h"
#include "havesl.h"
@ -11,7 +14,7 @@ int socket_local4(int s,char ip[4],uint16 *port)
struct sockaddr_in si;
socklen_t len = sizeof si;
if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return -1;
if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return winsock2errno(-1);
if (ip) *(uint32*)ip = *(uint32*)&si.sin_addr;
if (port) uint16_unpack_big((char *) &si.sin_port,port);
return 0;

11
socket/socket_local6.c

@ -1,13 +1,17 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "socket.h"
#include "ip6.h"
#include "haveip6.h"
#include "uint32.h"
#include "havesl.h"
#include "havescope.h"
int socket_local6(int s,char ip[16],uint16 *port,uint32 *scope_id)
{
@ -18,7 +22,7 @@ int socket_local6(int s,char ip[16],uint16 *port,uint32 *scope_id)
#endif
socklen_t len = sizeof si;
if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return -1;
if (getsockname(s,(struct sockaddr *) &si,&len) == -1) return winsock2errno(-1);
#ifdef LIBC_HAS_IP6
if (si.sin6_family==AF_INET) {
struct sockaddr_in *si4=(struct sockaddr_in*)&si;
@ -31,7 +35,12 @@ int socket_local6(int s,char ip[16],uint16 *port,uint32 *scope_id)
}
if (ip) byte_copy(ip,16,(char *) &si.sin6_addr);
if (port) uint16_unpack_big((char *) &si.sin6_port,port);
#ifdef HAVE_SCOPE_ID
if (scope_id) *scope_id=si.sin6_scope_id;
#else
if (scope_id) *scope_id=0;
#endif
#else
if (ip) {
byte_copy(ip,12,V4mappedprefix);

7
socket/socket_mchopcount6.c

@ -1,7 +1,10 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "byte.h"
#include "haveip6.h"
@ -14,8 +17,8 @@
int socket_mchopcount6(int s,char TTL)
{
#ifdef LIBC_HAS_IP6
return setsockopt(s,IPPROTO_IPV6,IPV6_MULTICAST_HOPS,&TTL,sizeof TTL);
return winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_MULTICAST_HOPS,&TTL,sizeof TTL));
#else
return socket_mcttl4(s,TTL);
return winsock2errno(socket_mcttl4(s,TTL));
#endif
}

9
socket/socket_mcjoin4.c

@ -1,15 +1,18 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "byte.h"
int socket_mcjoin4(int s,const char ip[4],const char interface[4])
int socket_mcjoin4(int s,const char ip[4],const char _interface[4])
{
struct ip_mreq opt;
byte_copy(&opt.imr_multiaddr.s_addr,4,ip);
byte_copy(&opt.imr_interface.s_addr,4,interface);
return setsockopt(s,IPPROTO_IP,IP_ADD_MEMBERSHIP,&opt,sizeof opt);
byte_copy(&opt.imr_interface.s_addr,4,_interface);
return winsock2errno(setsockopt(s,IPPROTO_IP,IP_ADD_MEMBERSHIP,&opt,sizeof opt));
}

9
socket/socket_mcjoin6.c

@ -1,7 +1,10 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "byte.h"
#include "haveip6.h"
@ -16,7 +19,7 @@
#endif
#endif
int socket_mcjoin6(int s,const char ip[16],int interface)
int socket_mcjoin6(int s,const char ip[16],int _interface)
{
#ifdef LIBC_HAS_IP6
struct ipv6_mreq opt;
@ -25,8 +28,8 @@ int socket_mcjoin6(int s,const char ip[16],int interface)
return socket_mcjoin4(s,ip+12,ip);
#ifdef LIBC_HAS_IP6
byte_copy(&opt.ipv6mr_multiaddr,16,ip);
opt.ipv6mr_interface=interface;
return setsockopt(s,IPPROTO_IPV6,IPV6_ADD_MEMBERSHIP,&opt,sizeof opt);
opt.ipv6mr_interface=_interface;
return winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_ADD_MEMBERSHIP,&opt,sizeof opt));
#else
errno=EPROTONOSUPPORT;
return -1;

5
socket/socket_mcleave4.c

@ -1,7 +1,10 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "byte.h"
@ -10,6 +13,6 @@ int socket_mcleave4(int s,const char ip[4])
struct ip_mreq opt;
byte_copy(&opt.imr_multiaddr.s_addr,4,ip);
byte_zero(&opt.imr_interface.s_addr,4);
return setsockopt(s,IPPROTO_IP,IP_DROP_MEMBERSHIP,&opt,sizeof opt);
return winsock2errno(setsockopt(s,IPPROTO_IP,IP_DROP_MEMBERSHIP,&opt,sizeof opt));
}

5
socket/socket_mcleave6.c

@ -1,7 +1,10 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "byte.h"
#include "haveip6.h"
@ -26,7 +29,7 @@ int socket_mcleave6(int s,const char ip[16])
#ifdef LIBC_HAS_IP6
byte_copy(&opt.ipv6mr_multiaddr,16,ip);
opt.ipv6mr_interface=0;
return setsockopt(s,IPPROTO_IPV6,IPV6_DROP_MEMBERSHIP,&opt,sizeof opt);
return winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_DROP_MEMBERSHIP,&opt,sizeof opt));
#else
errno=EPROTONOSUPPORT;
return -1;

5
socket/socket_mcloop4.c

@ -1,11 +1,14 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
int socket_mcloop4(int s,char loop)
{
return setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof loop);
return winsock2errno(setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof loop));
}

9
socket/socket_mcloop6.c

@ -1,7 +1,10 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "haveip6.h"
@ -9,11 +12,11 @@ int socket_mcloop6(int s,char loop)
{
#ifdef LIBC_HAS_IP6
if (noipv6)
return setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof loop);
return winsock2errno(setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof loop));
else
return setsockopt(s,IPPROTO_IPV6,IPV6_MULTICAST_LOOP,&loop,sizeof loop);
return winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_MULTICAST_LOOP,&loop,sizeof loop));
#else
return setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof loop);
return winsock2errno(setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof loop));
#endif
}

5
socket/socket_mcttl4.c

@ -1,11 +1,14 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
int socket_mcttl4(int s,char TTL)
{
return setsockopt(s,IPPROTO_IP,IP_MULTICAST_TTL,&TTL,sizeof TTL);
return winsock2errno(setsockopt(s,IPPROTO_IP,IP_MULTICAST_TTL,&TTL,sizeof TTL));
}

5
socket/socket_recv4.c

@ -1,7 +1,10 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "socket.h"
#include "havesl.h"
@ -10,7 +13,7 @@ int socket_recv4(int s,char *buf,unsigned int len,char ip[4],uint16 *port) {
socklen_t Len = sizeof si;
int r;
if ((r = recvfrom(s,buf,len,0,(struct sockaddr *) &si,&Len))<0) return -1;
if ((r = recvfrom(s,buf,len,0,(struct sockaddr *) &si,&Len))<0) return winsock2errno(-1);
if (ip) *(uint32*)ip = *(uint32*)&si.sin_addr;
if (port) uint16_unpack_big((char *) &si.sin_port,port);
return r;

10
socket/socket_recv6.c

@ -1,12 +1,16 @@
#include <sys/param.h>
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "socket.h"
#include "ip6.h"
#include "haveip6.h"
#include "havesl.h"
#include "havescope.h"
int socket_recv6(int s,char *buf,unsigned int len,char ip[16],uint16 *port,uint32 *scope_id)
{
@ -19,7 +23,7 @@ int socket_recv6(int s,char *buf,unsigned int len,char ip[16],uint16 *port,uint3
int r;
byte_zero(&si,Len);
if ((r = recvfrom(s,buf,len,0,(struct sockaddr *) &si,&Len))<0) return -1;
if ((r = recvfrom(s,buf,len,0,(struct sockaddr *) &si,&Len))<0) return winsock2errno(-1);
#ifdef LIBC_HAS_IP6
if (noipv6) {
@ -33,7 +37,11 @@ int socket_recv6(int s,char *buf,unsigned int len,char ip[16],uint16 *port,uint3
}
if (ip) byte_copy(ip,16,(char *) &si.sin6_addr);
if (port) uint16_unpack_big((char *) &si.sin6_port,port);
#ifdef HAVE_SCOPE_ID
if (scope_id) *scope_id=si.sin6_scope_id;
#else
if (scope_id) *scope_id=0;
#endif
#else
if (ip) {
byte_copy(ip,12,(char *)V4mappedprefix);

6
socket/socket_remote4.c

@ -1,17 +1,21 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "socket.h"
#include "havesl.h"
#include "havescope.h"
int socket_remote4(int s,char ip[4],uint16 *port)
{
struct sockaddr_in si;
socklen_t len = sizeof si;
if (getpeername(s,(struct sockaddr *) &si,&len) == -1) return -1;
if (getpeername(s,(struct sockaddr *) &si,&len) == -1) return winsock2errno(-1);
if (ip) *(uint32*)ip = *(uint32*)&si.sin_addr;
if (port) uint16_unpack_big((char *) &si.sin_port,port);
return 0;

10
socket/socket_remote6.c

@ -1,13 +1,17 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "socket.h"
#include "ip6.h"
#include "haveip6.h"
#include "uint32.h"
#include "havesl.h"
#include "havescope.h"
int socket_remote6(int s,char ip[16],uint16 *port,uint32 *scope_id)
{
@ -18,7 +22,7 @@ int socket_remote6(int s,char ip[16],uint16 *port,uint32 *scope_id)
#endif
socklen_t len = sizeof si;
if (getpeername(s,(struct sockaddr *) &si,&len) == -1) return -1;
if (getpeername(s,(struct sockaddr *) &si,&len) == -1) return winsock2errno(-1);
#ifdef LIBC_HAS_IP6
if (si.sin6_family==AF_INET) {
struct sockaddr_in *si4=(struct sockaddr_in*)&si;
@ -31,7 +35,11 @@ int socket_remote6(int s,char ip[16],uint16 *port,uint32 *scope_id)
}
if (ip) byte_copy(ip,16,(char *) &si.sin6_addr);
if (port) uint16_unpack_big((char *) &si.sin6_port,port);
#ifdef HAVE_SCOPE_ID
if (scope_id) *scope_id=si.sin6_scope_id;
#else
if (scope_id) *scope_id=0;
#endif
#else
if (ip) {
byte_copy(ip,12,V4mappedprefix);

5
socket/socket_send4.c

@ -1,7 +1,10 @@
#include <sys/types.h>
#include <sys/param.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include "byte.h"
#include "socket.h"
@ -12,5 +15,5 @@ int socket_send4(int s,const char *buf,unsigned int len,const char ip[4],uint16
si.sin_family = AF_INET;
uint16_pack_big((char*) &si.sin_port,port);
*((uint32*)&si.sin_addr) = *((uint32*)ip);
return sendto(s,buf,len,0,(struct sockaddr *) &si,sizeof si);
return winsock2errno(sendto(s,buf,len,0,(struct sockaddr *) &si,sizeof si));
}<