From aa45b978839c268db6eccafc6a13452d99a7929c Mon Sep 17 00:00:00 2001 From: Sebastian Hammer Date: Thu, 28 Sep 1995 10:12:26 +0000 Subject: [PATCH] Windows-support changes --- comstack/tcpip.c | 89 +++++++++++++++++++++++++++++++++++++++++----------- include/comstack.h | 9 ++++-- include/tcpip.h | 9 +++++- odr/ber_int.c | 9 +++++- 4 files changed, 92 insertions(+), 24 deletions(-) diff --git a/comstack/tcpip.c b/comstack/tcpip.c index effa27b..5ad5cf0 100644 --- a/comstack/tcpip.c +++ b/comstack/tcpip.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: tcpip.c,v $ - * Revision 1.3 1995-09-27 15:02:45 quinn + * Revision 1.4 1995-09-28 10:12:26 quinn + * Windows-support changes + * + * Revision 1.3 1995/09/27 15:02:45 quinn * Modified function heads & prototypes. * * Revision 1.2 1995/06/15 12:30:06 quinn @@ -88,7 +91,10 @@ #include #include + +#ifndef WINDOWS #include +#endif int tcpip_close(COMSTACK h); int tcpip_put(COMSTACK h, char *buf, int size); @@ -108,6 +114,8 @@ int completeBER(unsigned char *buf, int len); #define TRC(X) #endif +static int initialized = 0; + typedef struct tcpip_state { char *altbuf; /* alternate buffer for surplus data */ @@ -123,17 +131,38 @@ COMSTACK MDF tcpip_type(int blocking, int protocol) COMSTACK p; struct protoent *proto; tcpip_state *state; - int s; + int s, tru = 1; + if (!initialized) + { +#ifdef WINDOWS + WORD requested; + WSADATA wd; + + requested = MAKEWORD(1, 1); + if (WSAStartup(requested, &wd)) + return 0; +#endif + initialized = 1; + } + +#ifndef WINDOWS if (!(proto = getprotobyname("tcp"))) return 0; if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0) +#else + if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) +#endif return 0; if (!(p = malloc(sizeof(struct comstack)))) return 0; if (!(state = p->private = malloc(sizeof(tcpip_state)))) return 0; +#ifdef WINDOWS + if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0) +#else if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0) +#endif return 0; p->iofile = s; @@ -152,7 +181,7 @@ COMSTACK MDF tcpip_type(int blocking, int protocol) p->state = CS_UNBND; p->event = CS_NONE; - p->errno = 0; + p->cerrno = 0; p->stackerr = 0; state->altbuf = 0; @@ -201,8 +230,9 @@ int tcpip_more(COMSTACK h) } /* - * connect(2) will block - nothing we can do short of doing weird things - * like spawning subprocesses or threading or some weird junk like that. + * connect(2) will block (sometimes) - nothing we can do short of doing + * weird things like spawning subprocesses or threading or some weird junk + * like that. */ int tcpip_connect(COMSTACK h, void *address) { @@ -211,7 +241,11 @@ int tcpip_connect(COMSTACK h, void *address) TRC(fprintf(stderr, "tcpip_connect\n")); if (connect(h->iofile, (struct sockaddr *) add, sizeof(*add)) < 0) { +#ifdef WINDOWS + if (errno == WSAEWOULDBLOCK) +#else if (errno == EINPROGRESS) +#endif return 1; return -1; } @@ -236,17 +270,17 @@ int tcpip_bind(COMSTACK h, void *address, int mode) TRC(fprintf(stderr, "tcpip_bind\n")); if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { - h->errno = CSYSERR; + h->cerrno = CSYSERR; return -1; } if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0) { - h->errno = CSYSERR; + h->cerrno = CSYSERR; return -1; } if (mode == CS_SERVER && listen(h->iofile, 3) < 0) { - h->errno = CSYSERR; + h->cerrno = CSYSERR; return -1; } h->state = CS_IDLE; @@ -261,15 +295,20 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen) TRC(fprintf(stderr, "tcpip_listen\n")); if (h->state != CS_IDLE) { - h->errno = CSOUTSTATE; + h->cerrno = CSOUTSTATE; return -1; } if ((h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len)) < 0) { +#ifdef WINDOWS + if (errno == WSAEWOULDBLOCK) +#else if (errno == EWOULDBLOCK) - h->errno = CSNODATA; +#endif + + h->cerrno = CSNODATA; else - h->errno = CSYSERR; + h->cerrno = CSYSERR; return -1; } if (addrlen && *addrlen > sizeof(struct sockaddr_in)) @@ -288,22 +327,26 @@ COMSTACK tcpip_accept(COMSTACK h) TRC(fprintf(stderr, "tcpip_accept\n")); if (h->state != CS_INCON) { - h->errno = CSOUTSTATE; + h->cerrno = CSOUTSTATE; return 0; } if (!(new = malloc(sizeof(*new)))) { - h->errno = CSYSERR; + h->cerrno = CSYSERR; return 0; } memcpy(new, h, sizeof(*h)); new->iofile = h->newfd; if (!(state = new->private = malloc(sizeof(tcpip_state)))) { - h->errno = CSYSERR; + h->cerrno = CSYSERR; return 0; } +#ifdef WINDOWS + if (!new->blocking && ioctlsocket(s, FIONBIO, &tru) < 0) +#else if (!new->blocking && fcntl(new->iofile, F_SETFL, O_NONBLOCK) < 0) +#endif return 0; state->altbuf = 0; state->altsize = state->altlen = 0; @@ -350,8 +393,12 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize) else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK) if (!(*buf = realloc(*buf, *bufsize *= 2))) return -1; - if ((res = read(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK)) < 0) + if ((res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0)) < 0) +#ifdef WINDOWS + if (errno == WSAEWOULDBLOCK) +#else if (errno == EWOULDBLOCK) +#endif break; else return -1; @@ -403,20 +450,24 @@ int tcpip_put(COMSTACK h, char *buf, int size) } else if (state->towrite != size) { - h->errno = CSWRONGBUF; + h->cerrno = CSWRONGBUF; return -1; } while (state->towrite > state->written) { - if ((res = write(h->iofile, buf + state->written, size - - state->written)) < 0) + if ((res = send(h->iofile, buf + state->written, size - + state->written, 0)) < 0) { +#ifdef WINDOWS + if (errno == WSAEWOULDBLOCK) +#else if (errno == EAGAIN) +#endif { TRC(fprintf(stderr, " Flow control stop\n")); return 1; } - h->errno = CSYSERR; + h->cerrno = CSYSERR; return -1; } state->written += res; diff --git a/include/comstack.h b/include/comstack.h index b931987..e6a5627 100644 --- a/include/comstack.h +++ b/include/comstack.h @@ -24,7 +24,10 @@ * OF THIS SOFTWARE. * * $Log: comstack.h,v $ - * Revision 1.8 1995-09-27 15:02:46 quinn + * Revision 1.9 1995-09-28 10:12:36 quinn + * Windows-support changes + * + * Revision 1.8 1995/09/27 15:02:46 quinn * Modified function heads & prototypes. * * Revision 1.7 1995/06/19 12:38:24 quinn @@ -103,7 +106,7 @@ typedef COMSTACK (*CS_TYPE)(int blocking, int protocol); struct comstack { CS_TYPE type; - int errno; /* current error code of this stack */ + int cerrno; /* current error code of this stack */ char *stackerr;/* current lower-layer error string, or null if none */ int iofile; /* UNIX file descriptor for iochannel */ int timeout; /* how long to wait for trailing blocks (ignored for now) */ @@ -152,7 +155,7 @@ struct comstack #define cs_fileno(handle) ((handle)->iofile) #define cs_stackerr(handle) ((handle)->stackerr) #define cs_getstate(handle) ((handle)->getstate) -#define cs_errno(handle) ((handle)->errno) +#define cs_errno(handle) ((handle)->cerrno) #define cs_getproto(handle) ((handle)->protocol) const char MDF *cs_strerror(COMSTACK h); diff --git a/include/tcpip.h b/include/tcpip.h index ddb2a5a..1eb2f48 100644 --- a/include/tcpip.h +++ b/include/tcpip.h @@ -24,7 +24,10 @@ * OF THIS SOFTWARE. * * $Log: tcpip.h,v $ - * Revision 1.3 1995-09-27 15:02:53 quinn + * Revision 1.4 1995-09-28 10:12:36 quinn + * Windows-support changes + * + * Revision 1.3 1995/09/27 15:02:53 quinn * Modified function heads & prototypes. * * Revision 1.2 1995/05/16 08:50:39 quinn @@ -50,10 +53,14 @@ #include #include +#ifdef WINDOWS +#include +#else #include #include #include #include +#endif struct sockaddr_in MDF *tcpip_strtoaddr(const char *str); diff --git a/odr/ber_int.c b/odr/ber_int.c index e6a6bfd..7e51ff6 100644 --- a/odr/ber_int.c +++ b/odr/ber_int.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: ber_int.c,v $ - * Revision 1.8 1995-09-27 15:02:55 quinn + * Revision 1.9 1995-09-28 10:12:39 quinn + * Windows-support changes + * + * Revision 1.8 1995/09/27 15:02:55 quinn * Modified function heads & prototypes. * * Revision 1.7 1995/05/16 08:50:44 quinn @@ -33,7 +36,11 @@ #include #include +#ifdef WINDOWS +#include +#else #include /* for htons... */ +#endif #include static int ber_encinteger(ODR o, int val); -- 1.7.10.4