Fix sample PQF
[yaz-moved-to-github.git] / comstack / unix.c
index a3d4e58..4efed27 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 1995-2002, Index Data
+ * Copyright (c) 1995-2003, Index Data
  * See the file LICENSE for details.
  *
- * $Id: unix.c,v 1.1 2002-06-04 11:36:10 adam Exp $
+ * $Id: unix.c,v 1.14 2003-10-08 21:47:15 adam Exp $
  * UNIX socket COMSTACK. By Morten Bøgeskov.
  */
 #ifndef WIN32
 #include <yaz/comstack.h>
 #include <yaz/unix.h>
 #include <yaz/log.h>
+#include <yaz/nmem.h>
 
-/* Chas added the following, so we get the definition of completeBER */
-#include <yaz/odr.h>
-
-int unix_close(COMSTACK h);
-int unix_put(COMSTACK h, char *buf, int size);
-int unix_get(COMSTACK h, char **buf, int *bufsize);
-int unix_connect(COMSTACK h, void *address);
-int unix_more(COMSTACK h);
-int unix_rcvconnect(COMSTACK h);
-int unix_bind(COMSTACK h, void *address, int mode);
-int unix_listen(COMSTACK h, char *raddr, int *addrlen,
+#ifndef YAZ_SOCKLEN_T
+#define YAZ_SOCKLEN_T int
+#endif
+
+static int unix_close(COMSTACK h);
+static int unix_put(COMSTACK h, char *buf, int size);
+static int unix_get(COMSTACK h, char **buf, int *bufsize);
+static int unix_connect(COMSTACK h, void *address);
+static int unix_more(COMSTACK h);
+static int unix_rcvconnect(COMSTACK h);
+static int unix_bind(COMSTACK h, void *address, int mode);
+static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
                int (*check_ip)(void *cd, const char *a, int len, int type),
                void *cd);
-int static unix_set_blocking(COMSTACK p, int blocking);
-
+static int unix_set_blocking(COMSTACK p, int blocking);
 
-COMSTACK unix_accept(COMSTACK h);
-char *unix_addrstr(COMSTACK h);
-void *unix_straddr(COMSTACK h, const char *str);
+static COMSTACK unix_accept(COMSTACK h);
+static char *unix_addrstr(COMSTACK h);
+static void *unix_straddr(COMSTACK h, const char *str);
 
+#ifndef SUN_LEN
+#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
+                     + strlen ((ptr)->sun_path))
+#endif
 #if 0
 #define TRC(x) x
 #else
@@ -94,7 +99,7 @@ COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
                                        xmalloc(sizeof(unix_state)))))
        return 0;
 
-    if (!(p->blocking = blocking))
+    if (!((p->blocking = blocking)&1))
     {
        if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
            return 0;
@@ -132,7 +137,7 @@ COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
     if (protocol == PROTO_WAIS)
        state->complete = completeWAIS;
     else
-       state->complete = completeBER;
+       state->complete = cs_complete_auto;
 
     p->timeout = COMSTACK_DEFAULT_TIMEOUT;
     TRC(fprintf(stderr, "Created new UNIX comstack\n"));
@@ -141,22 +146,21 @@ COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
 }
 
 
-int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
+static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
 {
-    struct hostent *hp;
-    char *p, buf[512];
-    short int port = 210;
-    unsigned tmpadd;
-
+    char *cp;
     if (!unix_init ())
        return 0;
     TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
     add->sun_family = AF_UNIX;
     strncpy(add->sun_path, str, sizeof(add->sun_path));
+    cp = strchr (add->sun_path, ':');
+    if (cp)
+       *cp = '\0';
     return 1;
 }
 
-void *unix_straddr(COMSTACK h, const char *str)
+static void *unix_straddr(COMSTACK h, const char *str)
 {
     unix_state *sp = (unix_state *)h->cprivate;
 
@@ -178,7 +182,7 @@ struct sockaddr_un *unix_strtoaddr(const char *str)
     return &add;
 }
 
-int unix_more(COMSTACK h)
+static int unix_more(COMSTACK h)
 {
     unix_state *sp = (unix_state *)h->cprivate;
 
@@ -191,7 +195,7 @@ int unix_more(COMSTACK h)
  * weird things like spawning subprocesses or threading or some weird junk
  * like that.
  */
-int unix_connect(COMSTACK h, void *address)
+static int unix_connect(COMSTACK h, void *address)
 {
     struct sockaddr_un *add = (struct sockaddr_un *)address;
     int r;
@@ -206,7 +210,7 @@ int unix_connect(COMSTACK h, void *address)
     r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
     if (r < 0)
     {
-       if (errno == EINPROGRESS)
+       if (yaz_errno() == EINPROGRESS)
        {
            h->event = CS_CONNECT;
            h->state = CS_ST_CONNECTING;
@@ -225,9 +229,8 @@ int unix_connect(COMSTACK h, void *address)
 /*
  * nop
  */
-int unix_rcvconnect(COMSTACK h)
+static int unix_rcvconnect(COMSTACK h)
 {
-    unix_state *sp = (unix_state *)h->cprivate;
     TRC(fprintf(stderr, "unix_rcvconnect\n"));
 
     if (h->state == CS_ST_DATAXFER)
@@ -245,10 +248,9 @@ int unix_rcvconnect(COMSTACK h)
 #define CERTF "ztest.pem"
 #define KEYF "ztest.pem"
 
-int unix_bind(COMSTACK h, void *address, int mode)
+static int unix_bind(COMSTACK h, void *address, int mode)
 {
     struct sockaddr *addr = (struct sockaddr *)address;
-    unsigned long one = 1;
     const char * path = ((struct sockaddr_un *)addr)->sun_path;
     struct stat stat_buf;
 
@@ -259,7 +261,7 @@ int unix_bind(COMSTACK h, void *address, int mode)
        int socket_out = -1;
        if(! S_ISSOCK(stat_buf.st_mode)) {
            h->cerrno = CSYSERR;
-           errno = EEXIST; /* Not a socket (File exists) */
+           yaz_set_errno(EEXIST); /* Not a socket (File exists) */
            return -1;
        }
        if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
@@ -269,7 +271,7 @@ int unix_bind(COMSTACK h, void *address, int mode)
        socket_unix.sun_family = AF_UNIX;
        strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
        if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
-           if(errno == ECONNREFUSED) {
+           if(yaz_errno() == ECONNREFUSED) {
                TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
            } else {
                h->cerrno = CSYSERR;
@@ -278,7 +280,7 @@ int unix_bind(COMSTACK h, void *address, int mode)
        } else {
            close(socket_out);
            h->cerrno = CSYSERR;
-           errno = EADDRINUSE;
+           yaz_set_errno(EADDRINUSE);
            return -1;
        }
        unlink(path);
@@ -289,6 +291,7 @@ int unix_bind(COMSTACK h, void *address, int mode)
        h->cerrno = CSYSERR;
        return -1;
     }
+    chmod(path, 0777);
     if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
     {
        h->cerrno = CSYSERR;
@@ -299,16 +302,12 @@ int unix_bind(COMSTACK h, void *address, int mode)
     return 0;
 }
 
-int unix_listen(COMSTACK h, char *raddr, int *addrlen,
-               int (*check_ip)(void *cd, const char *a, int len, int t),
-               void *cd)
+static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
+                    int (*check_ip)(void *cd, const char *a, int len, int t),
+                   void *cd)
 {
     struct sockaddr_un addr;
-#ifdef __cplusplus
-    socklen_t len = SUN_LEN(&addr);
-#else
-    int len = SUN_LEN(&addr);
-#endif
+    YAZ_SOCKLEN_T len = sizeof(addr);
 
     TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
     if (h->state != CS_ST_IDLE)
@@ -320,10 +319,10 @@ int unix_listen(COMSTACK h, char *raddr, int *addrlen,
     if (h->newfd < 0)
     {
        if (
-           errno == EWOULDBLOCK
+           yaz_errno() == EWOULDBLOCK
 #ifdef EAGAIN
 #if EAGAIN != EWOULDBLOCK
-           || errno == EAGAIN
+           || yaz_errno() == EAGAIN
 #endif
 #endif
            )
@@ -340,7 +339,7 @@ int unix_listen(COMSTACK h, char *raddr, int *addrlen,
     return 0;
 }
 
-COMSTACK unix_accept(COMSTACK h)
+static COMSTACK unix_accept(COMSTACK h)
 {
     COMSTACK cnew;
     unix_state *state, *st = (unix_state *)h->cprivate;
@@ -369,8 +368,8 @@ COMSTACK unix_accept(COMSTACK h)
            }
            return 0;
        }
-       if (!cnew->blocking &&
-           (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
+       if (!(cnew->blocking&1) && 
+            (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
            )
        {
            h->cerrno = CSYSERR;
@@ -388,6 +387,7 @@ COMSTACK unix_accept(COMSTACK h)
        state->altsize = state->altlen = 0;
        state->towrite = state->written = -1;
        state->complete = st->complete;
+       memcpy(&state->addr, &st->addr, sizeof(state->addr));
        cnew->state = CS_ST_ACCEPT;
        cnew->event = CS_NONE;
        h->state = CS_ST_IDLE;
@@ -414,7 +414,7 @@ COMSTACK unix_accept(COMSTACK h)
  * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
  * 0=connection closed.
  */
-int unix_get(COMSTACK h, char **buf, int *bufsize)
+static int unix_get(COMSTACK h, char **buf, int *bufsize)
 {
     unix_state *sp = (unix_state *)h->cprivate;
     char *tmpc;
@@ -450,19 +450,19 @@ int unix_get(COMSTACK h, char **buf, int *bufsize)
        TRC(fprintf(stderr, "  recv res=%d, hasread=%d\n", res, hasread));
        if (res < 0)
        {
-           if (errno == EWOULDBLOCK
+           if (yaz_errno() == EWOULDBLOCK
 #ifdef EAGAIN
 #if EAGAIN != EWOULDBLOCK
-               || errno == EAGAIN
+               || yaz_errno() == EAGAIN
 #endif
 #endif
-               || errno == EINPROGRESS
+               || yaz_errno() == EINPROGRESS
                )
            {
                h->io_pending = CS_WANT_READ;
                break;
            }
-           else if (errno == 0)
+           else if (yaz_errno() == 0)
                continue;
            else
                return -1;
@@ -503,7 +503,7 @@ int unix_get(COMSTACK h, char **buf, int *bufsize)
  * In nonblocking mode, you must call again with same buffer while
  * return value is 1.
  */
-int unix_put(COMSTACK h, char *buf, int size)
+static int unix_put(COMSTACK h, char *buf, int size)
 {
     int res;
     struct unix_state *state = (struct unix_state *)h->cprivate;
@@ -534,10 +534,10 @@ int unix_put(COMSTACK h, char *buf, int size)
                 )) < 0)
        {
            if (
-               errno == EWOULDBLOCK
+               yaz_errno() == EWOULDBLOCK
 #ifdef EAGAIN
 #if EAGAIN != EWOULDBLOCK
-               || errno == EAGAIN
+               || yaz_errno() == EAGAIN
 #endif
 #endif
                )
@@ -558,9 +558,7 @@ int unix_put(COMSTACK h, char *buf, int size)
     return 0;
 }
 
-
-
-int unix_close(COMSTACK h)
+static int unix_close(COMSTACK h)
 {
     unix_state *sp = (struct unix_state *)h->cprivate;
 
@@ -576,7 +574,7 @@ int unix_close(COMSTACK h)
     return 0;
 }
 
-char *unix_addrstr(COMSTACK h)
+static char *unix_addrstr(COMSTACK h)
 {
     unix_state *sp = (struct unix_state *)h->cprivate;
     char *buf = sp->buf;
@@ -584,7 +582,7 @@ char *unix_addrstr(COMSTACK h)
     return buf;
 }
 
-int static unix_set_blocking(COMSTACK p, int blocking)
+static int unix_set_blocking(COMSTACK p, int blocking)
 {
     unsigned long flag;