From 82a4ae868db6aef98d5aeebe35986a9296732445 Mon Sep 17 00:00:00 2001 From: leitner Date: Thu, 6 Nov 2003 20:47:59 +0000 Subject: [PATCH] broken lose32 port using mingw (don't use!!!) initial Solaris /dev/poll port --- CHANGES | 2 ++ GNUmakefile | 18 +++++++++++---- buffer/buffer_1.c | 3 +++ dns/dns_random.c | 2 ++ dns/dns_rcrw.c | 3 +++ dns/dns_transmit.c | 4 ++++ io/io_closeonexec.c | 2 ++ io/io_dontwantread.c | 14 ++++++++++++ io/io_dontwantwrite.c | 14 ++++++++++++ io/io_eagain.c | 3 ++- io/io_fd.c | 17 +++++++++++++- io/io_nonblock.c | 15 +++++++++++++ io/io_pipe.c | 6 +++++ io/io_waituntil2.c | 37 +++++++++++++++++++++++++++++++ io/io_wantread.c | 14 ++++++++++++ io/io_wantwrite.c | 14 ++++++++++++ io_internal.h | 15 ++++++++++++- mmap/mmap_private.c | 21 +++++++++++++++++- mmap/mmap_read.c | 19 ++++++++++++++++ mmap/mmap_shared.c | 19 ++++++++++++++++ open/open_append.c | 6 ++++- open/open_excl.c | 6 ++++- open/open_read.c | 6 ++++- open/open_trunc.c | 6 ++++- open/open_write.c | 6 ++++- socket.h | 43 ++++++++++++++++++++++++++++++++++++ socket/socket_accept4.c | 6 ++++- socket/socket_accept6.c | 11 ++++++++- socket/socket_bind4.c | 5 ++++- socket/socket_bind4_reuse.c | 5 ++++- socket/socket_bind6.c | 5 ++++- socket/socket_bind6_reuse.c | 5 ++++- socket/socket_broadcast.c | 5 ++++- socket/socket_connect4.c | 5 ++++- socket/socket_connect6.c | 12 +++++++--- socket/socket_connected.c | 2 ++ socket/socket_getifidx.c | 2 ++ socket/socket_getifname.c | 8 ++++--- socket/socket_listen.c | 6 ++++- socket/socket_local4.c | 5 ++++- socket/socket_local6.c | 11 ++++++++- socket/socket_mchopcount6.c | 7 ++++-- socket/socket_mcjoin4.c | 9 +++++--- socket/socket_mcjoin6.c | 9 +++++--- socket/socket_mcleave4.c | 5 ++++- socket/socket_mcleave6.c | 5 ++++- socket/socket_mcloop4.c | 5 ++++- socket/socket_mcloop6.c | 9 +++++--- socket/socket_mcttl4.c | 5 ++++- socket/socket_recv4.c | 5 ++++- socket/socket_recv6.c | 10 ++++++++- socket/socket_remote4.c | 6 ++++- socket/socket_remote6.c | 10 ++++++++- socket/socket_send4.c | 5 ++++- socket/socket_send6.c | 11 ++++++++- socket/socket_tcp4.c | 7 ++++-- socket/socket_tcp6.c | 8 +++++-- socket/socket_tryreservein.c | 5 ++++- socket/socket_udp4.c | 6 ++++- socket/socket_udp6.c | 10 ++++++--- taia/taia_now.c | 17 ++++++++++++++ trydevpoll.c | 28 +++++++++++++++++++++++ tryscope.c | 9 ++++++++ trysl.c | 5 +++++ unix/iopause.c | 6 ++++- unix/ndelay_off.c | 10 +++++++++ unix/ndelay_on.c | 10 +++++++++ 67 files changed, 569 insertions(+), 61 deletions(-) create mode 100644 trydevpoll.c create mode 100644 tryscope.c diff --git a/CHANGES b/CHANGES index 0d92117..1cc62d9 100644 --- a/CHANGES +++ b/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) diff --git a/GNUmakefile b/GNUmakefile index c8323c1..bf979b6 100644 --- a/GNUmakefile +++ b/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: diff --git a/buffer/buffer_1.c b/buffer/buffer_1.c index e1d9538..94cbddd 100644 --- a/buffer/buffer_1.c +++ b/buffer/buffer_1.c @@ -1,5 +1,8 @@ #include #include "buffer.h" +#ifdef __MINGW32__ +#include +#endif char buffer_1_space[BUFFER_INSIZE]; static buffer it = BUFFER_INIT(write,1,buffer_1_space,sizeof buffer_1_space); diff --git a/dns/dns_random.c b/dns/dns_random.c index 2158ed4..92c064d 100644 --- a/dns/dns_random.c +++ b/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 */ } diff --git a/dns/dns_rcrw.c b/dns/dns_rcrw.c index 26262a1..f194479 100644 --- a/dns/dns_rcrw.c +++ b/dns/dns_rcrw.c @@ -5,6 +5,9 @@ #include "str.h" #include "openreadclose.h" #include "dns.h" +#ifdef __MINGW32__ +#include +#endif static stralloc data = {0}; diff --git a/dns/dns_transmit.c b/dns/dns_transmit.c index 9511511..ae0066f 100644 --- a/dns/dns_transmit.c +++ b/dns/dns_transmit.c @@ -1,5 +1,9 @@ #include +#ifdef WIN32 +#include +#else #include +#endif #include #include #include "socket.h" diff --git a/io/io_closeonexec.c b/io/io_closeonexec.c index a305993..b36d636 100644 --- a/io/io_closeonexec.c +++ b/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 } diff --git a/io/io_dontwantread.c b/io/io_dontwantread.c index 34be35f..5fa4b33 100644 --- a/io/io_dontwantread.c +++ b/io/io_dontwantread.c @@ -11,6 +11,11 @@ #include #include #endif +#ifdef HAVE_DEVPOLL +#include +#include +#include +#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; } diff --git a/io/io_dontwantwrite.c b/io/io_dontwantwrite.c index 769c22b..2122e51 100644 --- a/io/io_dontwantwrite.c +++ b/io/io_dontwantwrite.c @@ -11,6 +11,11 @@ #include #include #endif +#ifdef HAVE_DEVPOLL +#include +#include +#include +#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; } diff --git a/io/io_eagain.c b/io/io_eagain.c index 76f8650..301f10e 100644 --- a/io/io_eagain.c +++ b/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)); diff --git a/io/io_fd.c b/io/io_fd.c index ec613f4..dcbdab5 100644 --- a/io/io_fd.c +++ b/io/io_fd.c @@ -18,17 +18,26 @@ #include #endif #include +#ifdef HAVE_DEVPOLL +#include +#include +#include +#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) { diff --git a/io/io_nonblock.c b/io/io_nonblock.c index 69e1cfb..faf6ff4 100644 --- a/io/io_nonblock.c +++ b/io/io_nonblock.c @@ -3,8 +3,23 @@ #include #include "io_internal.h" +#ifdef __MINGW32__ +#include +#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 } diff --git a/io/io_pipe.c b/io/io_pipe.c index 6f40413..5bd2b79 100644 --- a/io/io_pipe.c +++ b/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]; diff --git a/io/io_waituntil2.c b/io/io_waituntil2.c index 3886beb..01e18a4 100644 --- a/io/io_waituntil2.c +++ b/io/io_waituntil2.c @@ -14,6 +14,11 @@ #include #include #endif +#ifdef HAVE_DEVPOLL +#include +#include +#include +#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; diff --git a/io/io_wantread.c b/io/io_wantread.c index 567cc06..1d6b9cb 100644 --- a/io/io_wantread.c +++ b/io/io_wantread.c @@ -14,6 +14,11 @@ #ifdef HAVE_SIGIO #include #endif +#ifdef HAVE_DEVPOLL +#include +#include +#include +#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; diff --git a/io/io_wantwrite.c b/io/io_wantwrite.c index 12e6b09..2113c95 100644 --- a/io/io_wantwrite.c +++ b/io/io_wantwrite.c @@ -14,6 +14,11 @@ #ifdef HAVE_SIGIO #include #endif +#ifdef HAVE_DEVPOLL +#include +#include +#include +#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; diff --git a/io_internal.h b/io_internal.h index 66b9947..ec38116 100644 --- a/io_internal.h +++ b/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 #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) diff --git a/mmap/mmap_private.c b/mmap/mmap_private.c index e88aabc..4c7ad17 100644 --- a/mmap/mmap_private.c +++ b/mmap/mmap_private.c @@ -1,10 +1,28 @@ #include #include +#ifdef __MINGW32__ +#include +#else #include +#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 } diff --git a/mmap/mmap_read.c b/mmap/mmap_read.c index 4c25c9e..138ef17 100644 --- a/mmap/mmap_read.c +++ b/mmap/mmap_read.c @@ -1,10 +1,28 @@ #include #include +#ifdef __MINGW32__ +#include +#else #include +#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 } diff --git a/mmap/mmap_shared.c b/mmap/mmap_shared.c index d402c76..aaf7b88 100644 --- a/mmap/mmap_shared.c +++ b/mmap/mmap_shared.c @@ -1,10 +1,28 @@ #include #include +#ifdef __MINGW32__ +#include +#else #include +#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 } diff --git a/open/open_append.c b/open/open_append.c index d90fadd..70512c8 100644 --- a/open/open_append.c +++ b/open/open_append.c @@ -2,6 +2,10 @@ #include #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); } diff --git a/open/open_excl.c b/open/open_excl.c index ed44640..0ac42c3 100644 --- a/open/open_excl.c +++ b/open/open_excl.c @@ -2,6 +2,10 @@ #include #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); } diff --git a/open/open_read.c b/open/open_read.c index c0e99f2..9aadf2f 100644 --- a/open/open_read.c +++ b/open/open_read.c @@ -2,6 +2,10 @@ #include #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); } diff --git a/open/open_trunc.c b/open/open_trunc.c index 8f0acf8..4c13770 100644 --- a/open/open_trunc.c +++ b/open/open_trunc.c @@ -2,6 +2,10 @@ #include #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); } diff --git a/open/open_write.c b/open/open_write.c index 037a52d..73781e6 100644 --- a/open/open_write.c +++ b/open/open_write.c @@ -2,6 +2,10 @@ #include #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); } diff --git a/socket.h b/socket.h index f47cfc0..228407d 100644 --- a/socket.h +++ b/socket.h @@ -55,4 +55,47 @@ int socket_sendfile(int out,int in,uint32 offset,uint32 bytes); int noipv6; +#ifdef __MINGW32__ +#include +#include + +#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 diff --git a/socket/socket_accept4.c b/socket/socket_accept4.c index c9c323f..322a86e 100644 --- a/socket/socket_accept4.c +++ b/socket/socket_accept4.c @@ -1,6 +1,9 @@ +#ifndef __MINGW32__ #include #include #include +#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; diff --git a/socket/socket_accept6.c b/socket/socket_accept6.c index 2ff1627..1e3220a 100644 --- a/socket/socket_accept6.c +++ b/socket/socket_accept6.c @@ -1,12 +1,16 @@ #include #include +#ifndef __MINGW32__ #include #include +#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 diff --git a/socket/socket_bind4.c b/socket/socket_bind4.c index 566dbc2..b0bacee 100644 --- a/socket/socket_bind4.c +++ b/socket/socket_bind4.c @@ -1,6 +1,9 @@ +#ifndef __MINGW32__ #include #include #include +#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)); } diff --git a/socket/socket_bind4_reuse.c b/socket/socket_bind4_reuse.c index 056a491..7e1b8c1 100644 --- a/socket/socket_bind4_reuse.c +++ b/socket/socket_bind4_reuse.c @@ -1,9 +1,12 @@ #include +#ifndef __MINGW32__ #include +#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)); } diff --git a/socket/socket_bind6.c b/socket/socket_bind6.c index 551cfe3..2d7f3c0 100644 --- a/socket/socket_bind6.c +++ b/socket/socket_bind6.c @@ -1,8 +1,11 @@ #include #include "haveip6.h" #include +#ifndef __MINGW32__ #include #include +#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; diff --git a/socket/socket_bind6_reuse.c b/socket/socket_bind6_reuse.c index d529631..439c798 100644 --- a/socket/socket_bind6_reuse.c +++ b/socket/socket_bind6_reuse.c @@ -1,9 +1,12 @@ #include +#ifndef __MINGW32__ #include +#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)); } diff --git a/socket/socket_broadcast.c b/socket/socket_broadcast.c index 294cfbd..fb275b3 100644 --- a/socket/socket_broadcast.c +++ b/socket/socket_broadcast.c @@ -1,12 +1,15 @@ #include #include +#ifndef __MINGW32__ #include #include +#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)); } diff --git a/socket/socket_connect4.c b/socket/socket_connect4.c index 141970c..95f7230 100644 --- a/socket/socket_connect4.c +++ b/socket/socket_connect4.c @@ -1,6 +1,9 @@ #include +#ifndef __MINGW32__ #include #include +#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)))); } diff --git a/socket/socket_connect6.c b/socket/socket_connect6.c index 0ad886d..095c0fd 100644 --- a/socket/socket_connect6.c +++ b/socket/socket_connect6.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#endif +#include "windoze.h" #include #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; diff --git a/socket/socket_connected.c b/socket/socket_connected.c index e4f8b7f..ade40b8 100644 --- a/socket/socket_connected.c +++ b/socket/socket_connected.c @@ -1,6 +1,8 @@ #include +#ifndef __MINGW32__ #include #include +#endif #include "socket.h" #include "havesl.h" diff --git a/socket/socket_getifidx.c b/socket/socket_getifidx.c index c71fc48..3e2dda7 100644 --- a/socket/socket_getifidx.c +++ b/socket/socket_getifidx.c @@ -1,6 +1,8 @@ #include +#ifndef __MINGW32__ #include #include +#endif #include "socket.h" #include "haven2i.h" diff --git a/socket/socket_getifname.c b/socket/socket_getifname.c index 72b2d0c..21b4514 100644 --- a/socket/socket_getifname.c +++ b/socket/socket_getifname.c @@ -1,21 +1,23 @@ #include +#ifndef __MINGW32__ #include #include +#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 diff --git a/socket/socket_listen.c b/socket/socket_listen.c index 5018ff0..51e2e17 100644 --- a/socket/socket_listen.c +++ b/socket/socket_listen.c @@ -1,6 +1,10 @@ #include +#ifndef __MINGW32__ #include +#endif +#include "socket.h" +#include "windoze.h" int socket_listen(int s,unsigned int backlog) { - return listen(s, backlog); + return winsock2errno(listen(s, backlog)); } diff --git a/socket/socket_local4.c b/socket/socket_local4.c index 52db123..dcfd71c 100644 --- a/socket/socket_local4.c +++ b/socket/socket_local4.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#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; diff --git a/socket/socket_local6.c b/socket/socket_local6.c index 37689b1..1d708bf 100644 --- a/socket/socket_local6.c +++ b/socket/socket_local6.c @@ -1,13 +1,17 @@ #include #include +#ifndef __MINGW32__ #include #include +#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); diff --git a/socket/socket_mchopcount6.c b/socket/socket_mchopcount6.c index a727108..9c46696 100644 --- a/socket/socket_mchopcount6.c +++ b/socket/socket_mchopcount6.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#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 } diff --git a/socket/socket_mcjoin4.c b/socket/socket_mcjoin4.c index fe4299b..bdfe44a 100644 --- a/socket/socket_mcjoin4.c +++ b/socket/socket_mcjoin4.c @@ -1,15 +1,18 @@ #include #include +#ifndef __MINGW32__ #include #include +#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)); } diff --git a/socket/socket_mcjoin6.c b/socket/socket_mcjoin6.c index 135b059..2ee0ef4 100644 --- a/socket/socket_mcjoin6.c +++ b/socket/socket_mcjoin6.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#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; diff --git a/socket/socket_mcleave4.c b/socket/socket_mcleave4.c index e068c0f..2fed693 100644 --- a/socket/socket_mcleave4.c +++ b/socket/socket_mcleave4.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#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)); } diff --git a/socket/socket_mcleave6.c b/socket/socket_mcleave6.c index d842044..e8c9b33 100644 --- a/socket/socket_mcleave6.c +++ b/socket/socket_mcleave6.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#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; diff --git a/socket/socket_mcloop4.c b/socket/socket_mcloop4.c index eec742d..e670da2 100644 --- a/socket/socket_mcloop4.c +++ b/socket/socket_mcloop4.c @@ -1,11 +1,14 @@ #include #include +#ifndef __MINGW32__ #include #include +#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)); } diff --git a/socket/socket_mcloop6.c b/socket/socket_mcloop6.c index 88cce9b..7c94549 100644 --- a/socket/socket_mcloop6.c +++ b/socket/socket_mcloop6.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#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 } diff --git a/socket/socket_mcttl4.c b/socket/socket_mcttl4.c index 44316de..eff9a73 100644 --- a/socket/socket_mcttl4.c +++ b/socket/socket_mcttl4.c @@ -1,11 +1,14 @@ #include #include +#ifndef __MINGW32__ #include #include +#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)); } diff --git a/socket/socket_recv4.c b/socket/socket_recv4.c index f48f640..8f1065b 100644 --- a/socket/socket_recv4.c +++ b/socket/socket_recv4.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#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; diff --git a/socket/socket_recv6.c b/socket/socket_recv6.c index b04acc2..78bad0c 100644 --- a/socket/socket_recv6.c +++ b/socket/socket_recv6.c @@ -1,12 +1,16 @@ #include #include +#ifndef __MINGW32__ #include #include +#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); diff --git a/socket/socket_remote4.c b/socket/socket_remote4.c index 4ab4028..d36044e 100644 --- a/socket/socket_remote4.c +++ b/socket/socket_remote4.c @@ -1,17 +1,21 @@ #include #include +#ifndef __MINGW32__ #include #include +#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; diff --git a/socket/socket_remote6.c b/socket/socket_remote6.c index 0f3f4a7..fc02ca2 100644 --- a/socket/socket_remote6.c +++ b/socket/socket_remote6.c @@ -1,13 +1,17 @@ #include #include +#ifndef __MINGW32__ #include #include +#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); diff --git a/socket/socket_send4.c b/socket/socket_send4.c index e76a603..f18362d 100644 --- a/socket/socket_send4.c +++ b/socket/socket_send4.c @@ -1,7 +1,10 @@ #include #include +#ifndef __MINGW32__ #include #include +#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)); } diff --git a/socket/socket_send6.c b/socket/socket_send6.c index 977397c..39d00aa 100644 --- a/socket/socket_send6.c +++ b/socket/socket_send6.c @@ -1,13 +1,17 @@ #include #include +#ifndef __MINGW32__ #include #include +#endif +#include "windoze.h" #include #include "byte.h" #include "socket.h" #include "ip6.h" #include "haveip6.h" #include "ip4.h" +#include "havescope.h" int socket_send6(int s,const char *buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id) { @@ -32,7 +36,12 @@ int socket_send6(int s,const char *buf,unsigned int len,const char ip[16],uint16 si.sin6_family = AF_INET6; uint16_pack_big((char *) &si.sin6_port,port); byte_copy((char *) &si.sin6_addr,16,ip); - return sendto(s,buf,len,0,(struct sockaddr *) &si,sizeof si); +#ifdef HAVE_SCOPE_ID + si.sin6_scope_id=scope_id; +#else + si.sin6_scope_id=0; +#endif + return winsock2errno(sendto(s,buf,len,0,(struct sockaddr *) &si,sizeof si)); #else errno=EPROTONOSUPPORT; return -1; diff --git a/socket/socket_tcp4.c b/socket/socket_tcp4.c index b1ab3e7..d36e8a6 100644 --- a/socket/socket_tcp4.c +++ b/socket/socket_tcp4.c @@ -1,9 +1,12 @@ #include +#ifndef __MINGW32__ #include #include - +#endif +#include "windoze.h" #include "socket.h" int socket_tcp4(void) { - return socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); + __winsock_init(); + return winsock2errno(socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)); } diff --git a/socket/socket_tcp6.c b/socket/socket_tcp6.c index c82887d..bef1eba 100644 --- a/socket/socket_tcp6.c +++ b/socket/socket_tcp6.c @@ -1,6 +1,9 @@ #include +#ifndef __MINGW32__ #include #include +#endif +#include "windoze.h" #include #include "haveip6.h" #include "socket.h" @@ -14,8 +17,9 @@ int socket_tcp6(void) #ifdef LIBC_HAS_IP6 int s; + __winsock_init(); if (noipv6) goto compat; - s = socket(PF_INET6,SOCK_STREAM,0); + s = winsock2errno(socket(PF_INET6,SOCK_STREAM,0)); if (s == -1) { if (errno == EINVAL || errno == EAFNOSUPPORT) { compat: @@ -28,7 +32,7 @@ compat: #ifdef IPV6_V6ONLY { int zero=0; - setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&zero,sizeof(zero)); + winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&zero,sizeof(zero))); } #endif return s; diff --git a/socket/socket_tryreservein.c b/socket/socket_tryreservein.c index a481c20..3cfb025 100644 --- a/socket/socket_tryreservein.c +++ b/socket/socket_tryreservein.c @@ -1,11 +1,14 @@ #include +#ifndef __MINGW32__ #include +#endif +#include "windoze.h" #include "socket.h" void socket_tryreservein(int s,int size) { while (size >= 1024) { - if (setsockopt(s,SOL_SOCKET,SO_RCVBUF,&size,sizeof size) == 0) return; + if (winsock2errno(setsockopt(s,SOL_SOCKET,SO_RCVBUF,&size,sizeof size)) == 0) return; size -= (size >> 5); } } diff --git a/socket/socket_udp4.c b/socket/socket_udp4.c index b9892b4..c1f4104 100644 --- a/socket/socket_udp4.c +++ b/socket/socket_udp4.c @@ -1,9 +1,13 @@ #include +#ifndef __MINGW32__ #include #include +#endif +#include "windoze.h" #include "socket.h" int socket_udp4(void) { - return socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP); + __winsock_init(); + return winsock2errno(socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP)); } diff --git a/socket/socket_udp6.c b/socket/socket_udp6.c index 3769b1d..d783d99 100644 --- a/socket/socket_udp6.c +++ b/socket/socket_udp6.c @@ -1,6 +1,9 @@ #include +#ifndef __MINGW32__ #include #include +#endif +#include "windoze.h" #include #include "haveip6.h" #include "socket.h" @@ -14,12 +17,13 @@ int socket_udp6(void) #ifdef LIBC_HAS_IP6 int s; + __winsock_init(); if (noipv6) goto compat; - s = socket(PF_INET6,SOCK_DGRAM,0); + s = winsock2errno(socket(PF_INET6,SOCK_DGRAM,0)); if (s == -1) { if (errno == EINVAL || errno == EAFNOSUPPORT) { compat: - s=socket(AF_INET,SOCK_DGRAM,0); + s=winsock2errno(socket(AF_INET,SOCK_DGRAM,0)); noipv6=1; if (s==-1) return -1; } else @@ -28,7 +32,7 @@ compat: #ifdef IPV6_V6ONLY { int zero=0; - setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&zero,sizeof(zero)); + winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&zero,sizeof(zero))); } #endif return s; diff --git a/taia/taia_now.c b/taia/taia_now.c index ccc260d..54d5837 100644 --- a/taia/taia_now.c +++ b/taia/taia_now.c @@ -1,12 +1,29 @@ #include #include #include "taia.h" +#ifdef __MINGW32__ +#include +#endif void taia_now(struct taia *t) { +#ifdef __MINGW32__ + union { + FILETIME f; + unsigned long long l; + } fnord; + GetSystemTimeAsFileTime(&fnord.f); + /* 64-bit value representing the number of 100-nanosecond intervals + * since January 1, 1601 (UTC) */ + fnord.l-=((long long)(1970-1601))*365*24*60*60; + t->sec.x=fnord.l/10000000; + t->nano=((fnord.l+5)/10)%1000000; + t->atto=0; +#else struct timeval now; gettimeofday(&now,(struct timezone *) 0); tai_unix(&t->sec,now.tv_sec); t->nano = 1000 * now.tv_usec + 500; t->atto = 0; +#endif } diff --git a/trydevpoll.c b/trydevpoll.c new file mode 100644 index 0000000..95e94f8 --- /dev/null +++ b/trydevpoll.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +main() { + int fd=open("/dev/poll",O_RDWR); + struct pollfd p[100]; + int i,r; + dvpoll_t timeout; + p[0].fd=0; + p[0].events=POLLIN; + write(fd,p,sizeof(struct pollfd)); + timeout.dp_timeout=100; /* milliseconds? */ + timeout.dp_nfds=1; + timeout.dp_fds=p; + r=ioctl(fd,DP_POLL,&timeout); + for (i=0; i +#include +#include + +main() { + struct sockaddr_in6 sa; + sa.sin6_family = PF_INET6; + sa.sin6_scope_id = 23; +} diff --git a/trysl.c b/trysl.c index 5a5b355..a617b24 100644 --- a/trysl.c +++ b/trysl.c @@ -1,6 +1,11 @@ #include +#ifdef __MINGW32__ +#include +#include +#else #include #include +#endif main() { socklen_t t; diff --git a/unix/iopause.c b/unix/iopause.c index b8034de..0ae7533 100644 --- a/unix/iopause.c +++ b/unix/iopause.c @@ -1,6 +1,10 @@ #include "taia.h" -#include "select.h" #include "iopause.h" +#ifdef __MINGW32__ +#include +#else +#include "select.h" +#endif void iopause(iopause_fd *x,unsigned int len,struct taia *deadline,struct taia *stamp) { diff --git a/unix/ndelay_off.c b/unix/ndelay_off.c index 0748fd6..b26c89b 100644 --- a/unix/ndelay_off.c +++ b/unix/ndelay_off.c @@ -3,10 +3,20 @@ #include "ndelay.h" +#ifdef __MINGW32__ +#include +#include "windoze.h" +#endif + #ifndef O_NDELAY #define O_NDELAY O_NONBLOCK #endif int ndelay_off(int fd) { +#ifdef __MINGW32__ + unsigned long i=1; + return winsock2errno(ioctlsocket( fd, FIONBIO, &i)); +#else return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) & ~O_NDELAY); +#endif } diff --git a/unix/ndelay_on.c b/unix/ndelay_on.c index fc80eb2..64e9036 100644 --- a/unix/ndelay_on.c +++ b/unix/ndelay_on.c @@ -3,10 +3,20 @@ #include "ndelay.h" +#ifdef __MINGW32__ +#include +#include "windoze.h" +#endif + #ifndef O_NDELAY #define O_NDELAY O_NONBLOCK #endif int ndelay_on(int fd) { +#ifdef __MINGW32__ + unsigned long i=0; + return winsock2errno(ioctlsocket( fd, FIONBIO, &i)); +#else return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) | O_NDELAY); +#endif }