Windows-support changes
authorSebastian Hammer <quinn@indexdata.com>
Thu, 28 Sep 1995 10:12:26 +0000 (10:12 +0000)
committerSebastian Hammer <quinn@indexdata.com>
Thu, 28 Sep 1995 10:12:26 +0000 (10:12 +0000)
comstack/tcpip.c
include/comstack.h
include/tcpip.h
odr/ber_int.c

index effa27b..5ad5cf0 100644 (file)
@@ -4,7 +4,10 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: tcpip.c,v $
  * 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
  * Modified function heads & prototypes.
  *
  * Revision 1.2  1995/06/15  12:30:06  quinn
 
 #include <comstack.h>
 #include <tcpip.h>
 
 #include <comstack.h>
 #include <tcpip.h>
+
+#ifndef WINDOWS
 #include <sys/time.h>
 #include <sys/time.h>
+#endif
 
 int tcpip_close(COMSTACK h);
 int tcpip_put(COMSTACK h, char *buf, int size);
 
 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
 
 #define TRC(X)
 #endif
 
+static int initialized = 0;
+
 typedef struct tcpip_state
 {
     char *altbuf; /* alternate buffer for surplus data */
 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;
     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)
     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;
        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)
     if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
+#endif
        return 0;
 
     p->iofile = s;
        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->state = CS_UNBND;
     p->event = CS_NONE;
-    p->errno = 0;
+    p->cerrno = 0;
     p->stackerr = 0;
 
     state->altbuf = 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)
 {
  */
 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)
     {
     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)
        if (errno == EINPROGRESS)
+#endif
            return 1;
        return -1;
     }
            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)
     {
     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)
     {
        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)
     {
        return -1;
     }
     if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
     {
-       h->errno = CSYSERR;
+       h->cerrno = CSYSERR;
        return -1;
     }
     h->state = CS_IDLE;
        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)
     {
     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)
     {
        return -1;
     }
     if ((h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len)) < 0)
     {
+#ifdef WINDOWS
+       if (errno == WSAEWOULDBLOCK)
+#else
        if (errno == EWOULDBLOCK)
        if (errno == EWOULDBLOCK)
-           h->errno = CSNODATA;
+#endif
+
+           h->cerrno = CSNODATA;
        else
        else
-           h->errno = CSYSERR;
+           h->cerrno = CSYSERR;
        return -1;
     }
     if (addrlen && *addrlen > sizeof(struct sockaddr_in))
        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)
     {
     TRC(fprintf(stderr, "tcpip_accept\n"));
     if (h->state != CS_INCON)
     {
-       h->errno = CSOUTSTATE;
+       h->cerrno = CSOUTSTATE;
        return 0;
     }
     if (!(new = malloc(sizeof(*new))))
     {
        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))))
     {
        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;
     }
        return 0;
     }
+#ifdef WINDOWS
+    if (!new->blocking && ioctlsocket(s, FIONBIO, &tru) < 0)
+#else
     if (!new->blocking && fcntl(new->iofile, F_SETFL, O_NONBLOCK) < 0)
     if (!new->blocking && fcntl(new->iofile, F_SETFL, O_NONBLOCK) < 0)
+#endif
        return 0;
     state->altbuf = 0;
     state->altsize = state->altlen = 0;
        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;
        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)
            if (errno == EWOULDBLOCK)
+#endif
                break;
            else
                return -1;
                break;
            else
                return -1;
@@ -403,20 +450,24 @@ int tcpip_put(COMSTACK h, char *buf, int size)
     }
     else if (state->towrite != size)
     {
     }
     else if (state->towrite != size)
     {
-       h->errno = CSWRONGBUF;
+       h->cerrno = CSWRONGBUF;
        return -1;
     }
     while (state->towrite > state->written)
     {
        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)
            if (errno == EAGAIN)
+#endif
            {
                TRC(fprintf(stderr, "  Flow control stop\n"));
                return 1;
            }
            {
                TRC(fprintf(stderr, "  Flow control stop\n"));
                return 1;
            }
-           h->errno = CSYSERR;
+           h->cerrno = CSYSERR;
            return -1;
        }
        state->written += res;
            return -1;
        }
        state->written += res;
index b931987..e6a5627 100644 (file)
  * OF THIS SOFTWARE.
  *
  * $Log: comstack.h,v $
  * 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
  * 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;
 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) */
     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_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);
 #define cs_getproto(handle) ((handle)->protocol)
 
 const char MDF *cs_strerror(COMSTACK h);
index ddb2a5a..1eb2f48 100644 (file)
  * OF THIS SOFTWARE.
  *
  * $Log: tcpip.h,v $
  * 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
  * Modified function heads & prototypes.
  *
  * Revision 1.2  1995/05/16  08:50:39  quinn
 
 #include <yconfig.h>
 #include <sys/types.h>
 
 #include <yconfig.h>
 #include <sys/types.h>
+#ifdef WINDOWS
+#include <winsock.h>
+#else
 #include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#endif
 
 struct sockaddr_in MDF *tcpip_strtoaddr(const char *str);
 
 
 struct sockaddr_in MDF *tcpip_strtoaddr(const char *str);
 
index e6a6bfd..7e51ff6 100644 (file)
@@ -4,7 +4,10 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: ber_int.c,v $
  * 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
  * Modified function heads & prototypes.
  *
  * Revision 1.7  1995/05/16  08:50:44  quinn
 
 #include <odr.h>
 #include <sys/types.h>
 
 #include <odr.h>
 #include <sys/types.h>
+#ifdef WINDOWS
+#include <winsock.h>
+#else
 #include <netinet/in.h>  /* for htons... */
 #include <netinet/in.h>  /* for htons... */
+#endif
 #include <string.h>
 
 static int ber_encinteger(ODR o, int val);
 #include <string.h>
 
 static int ber_encinteger(ODR o, int val);