Fix.
[yaz-moved-to-github.git] / comstack / xmosi.c
index f9dd6ab..132de6c 100644 (file)
@@ -4,7 +4,46 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: xmosi.c,v $
- * Revision 1.3  1995-06-16 10:30:38  quinn
+ * Revision 1.16  1997-09-17 12:10:30  adam
+ * YAZ version 1.4.
+ *
+ * Revision 1.15  1997/05/14 06:53:34  adam
+ * C++ support.
+ *
+ * Revision 1.14  1996/07/26 12:34:07  quinn
+ * Porting.
+ *
+ * Revision 1.13  1996/07/06  19:58:30  quinn
+ * System headerfiles gathered in yconfig
+ *
+ * Revision 1.12  1996/05/22  08:34:44  adam
+ * Added ifdef USE_XTIMOSI; so that 'make depend' works.
+ *
+ * Revision 1.11  1996/02/23 10:00:41  quinn
+ * WAIS Work
+ *
+ * Revision 1.10  1996/02/10  12:23:13  quinn
+ * Enablie inetd operations fro TCP/IP stack
+ *
+ * Revision 1.9  1996/01/02  08:57:28  quinn
+ * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
+ *
+ * Revision 1.8  1995/11/01  13:54:29  quinn
+ * Minor adjustments
+ *
+ * Revision 1.7  1995/10/30  12:41:17  quinn
+ * Added hostname lookup for server.
+ *
+ * Revision 1.6  1995/09/29  17:12:00  quinn
+ * Smallish
+ *
+ * Revision 1.5  1995/09/28  10:24:32  quinn
+ * Windows changes
+ *
+ * Revision 1.4  1995/09/27  15:02:45  quinn
+ * Modified function heads & prototypes.
+ *
+ * Revision 1.3  1995/06/16  10:30:38  quinn
  * Added REUSEADDR.
  *
  * Revision 1.2  1995/06/15  12:30:07  quinn
  *
  */
 
+#ifdef USE_XTIMOSI
 /*
  * Glue layer for Peter Furniss' xtimosi package.
  */
 #include <string.h>
 #include <assert.h>
 
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#define YNETINCLUDE
+#include <yconfig.h>
 
 #include <comstack.h>
 #include <xmosi.h>
@@ -94,6 +131,8 @@ int mosi_rcvconnect(COMSTACK h);
 int mosi_bind(COMSTACK h, void *address, int mode);
 int mosi_listen(COMSTACK h, char *addrp, int *addrlen);
 COMSTACK mosi_accept(COMSTACK h);
+char *mosi_addrstr(COMSTACK h);
+void *mosi_straddr(COMSTACK h, const char *str);
 
 typedef struct mosi_state
 {
@@ -101,6 +140,7 @@ typedef struct mosi_state
     struct t_call *call;
     int hasread;               /* how many bytes read of current PDU */
     int haswrit;               /* how many bytes have we written */
+    struct netbuf netbuf;
 } mosi_state;
 
 static char *oidtostr(int *o)
@@ -125,7 +165,7 @@ static int addopt(struct netbuf *optbuf, unsigned long level, unsigned long
     char *str;
 
     ent.proto = proto;
-    ent.class = class;
+    ent.oclass = class;
     ent.value = value;
     if (!(oid = oid_getoidbyent(&ent)))
        return -1;
@@ -135,15 +175,18 @@ static int addopt(struct netbuf *optbuf, unsigned long level, unsigned long
     return 0;
 }
 
-COMSTACK mosi_type(int blocking, int protocol)
+COMSTACK mosi_type(int s, int blocking, int protocol)
 {
     COMSTACK r;
     mosi_state *state;
     int flags = O_RDWR;
 
-    if (!(r = malloc(sizeof(*r))))
+    if (s >= 0)
+       return 0;
+
+    if (!(r = xmalloc(sizeof(*r))))
        return 0;
-    if (!(state = r->private = malloc(sizeof(*state))))
+    if (!(state = r->cprivate = xmalloc(sizeof(*state))))
        return 0;
 
     state->call = 0;
@@ -162,6 +205,8 @@ COMSTACK mosi_type(int blocking, int protocol)
     r->f_bind = mosi_bind;
     r->f_listen = mosi_listen;
     r->f_accept = mosi_accept;
+    r->f_addrstr = mosi_addrstr;
+    r->r_straddr = mosi_straddr;
 
     if (!blocking)
        flags |= O_NONBLOCK;
@@ -193,11 +238,11 @@ int hex2oct(char *hex, char *oct)
  * addressing specific to our hack of OSI transport. A sockaddr_in wrapped
  * up in a t_mosiaddr in a netbuf (on a stick).
  */
-struct netbuf *mosi_strtoaddr(const char *str)
+
+int MDF *mosi_strtoaddr_ex(const char *str, struct netbuf *ret)
 {
-    struct netbuf *ret = malloc(sizeof(struct netbuf));
-    struct sockaddr_in *add = malloc(sizeof(struct sockaddr_in));
-    struct t_mosiaddr *mosiaddr = malloc(sizeof(struct t_mosiaddr));
+    struct sockaddr_in *add = xmalloc(sizeof(struct sockaddr_in));
+    struct t_mosiaddr *mosiaddr = xmalloc(sizeof(struct t_mosiaddr));
     struct hostent *hp;
     char *p, *b, buf[512], *nsap;
     short int port = 102;
@@ -264,6 +309,31 @@ struct netbuf *mosi_strtoaddr(const char *str)
     ret->buf = (char*)mosiaddr;
     ret->len = ret->maxlen = 100 /* sizeof(*mosiaddr) */ ;
 
+    return 1;
+}
+
+struct netbuf MDF *mosi_strtoaddr(const char *str)
+{
+    struct netbuf *ret = xmalloc(sizeof(struct netbuf));
+
+    if (!mosi_strtoaddr_ex (str, ret))
+    {
+       xfree (ret);
+       return 0;
+    }
+    return ret;
+}
+
+struct netbuf MDF *mosi_straddr(COMSTACK h, const char *str)
+{
+    mosi_state *st = h->cprivate;
+    struct netbuf *ret = &st->netbuf;
+
+    if (!mosi_strtoaddr_ex (str, ret))
+    {
+       xfree (ret);
+       return 0;
+    }
     return ret;
 }
 
@@ -330,7 +400,7 @@ int mosi_bind(COMSTACK h, void *address, int mode)
 
     if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
     {
-       h->errno = CSYSERR;
+       h->cerrno = CSYSERR;
        return -1;
     }
     if (mode == CS_SERVER)
@@ -347,7 +417,7 @@ int mosi_bind(COMSTACK h, void *address, int mode)
 int mosi_listen(COMSTACK h, char *addp, int *addrlen)
 {
     int res;
-    mosi_state *st = h->private;
+    mosi_state *st = h->cprivate;
 
     if (!(st->call = (struct t_call*) t_alloc(h->iofile, T_CALL_STR,
         T_ALL)))
@@ -366,18 +436,18 @@ COMSTACK mosi_accept(COMSTACK h)
 {
     COMSTACK new;
     void *local;
-    struct mosi_state *st = h->private, *ns;
+    struct mosi_state *st = h->cprivate, *ns;
     int flags = O_RDWR;
 
     if (h->state != CS_INCON)
     {
-       h->errno = CSOUTSTATE;
+       h->cerrno = CSOUTSTATE;
        return 0;
     }
-    if (!(new = malloc(sizeof(*new))))
+    if (!(new = xmalloc(sizeof(*new))))
        return 0;
     *new = *h;
-    if (!(new->private = ns = malloc(sizeof(*ns))))
+    if (!(new->cprivate = ns = xmalloc(sizeof(*ns))))
        return 0;
     *ns = *st;
     if (!h->blocking)
@@ -402,18 +472,18 @@ COMSTACK mosi_accept(COMSTACK h)
 int mosi_get(COMSTACK h, char **buf, int *bufsize)
 {
     int flags = 0, res;
-    mosi_state *ct = h->private;
+    mosi_state *ct = h->cprivate;
     int got;
 
     do
     {
        if (!*bufsize)
        {
-           if (!(*buf = malloc(*bufsize = CS_MOSI_BUFCHUNK)))
+           if (!(*buf = xmalloc(*bufsize = CS_MOSI_BUFCHUNK)))
                return -1;
        }
        else if (*bufsize - ct->hasread < CS_MOSI_BUFCHUNK)
-           if (!(*buf = realloc(*buf, *bufsize *= 2)))
+           if (!(*buf =xrealloc(*buf, *bufsize *= 2)))
                return -1;
 
        if ((res = u_rcv(h->iofile, *buf + ct->hasread, CS_MOSI_BUFCHUNK,
@@ -435,7 +505,7 @@ int mosi_get(COMSTACK h, char **buf, int *bufsize)
 
 int mosi_put(COMSTACK h, char *buf, int size)
 {
-    mosi_state *ct = h->private;
+    mosi_state *ct = h->cprivate;
     int res = u_snd(h->iofile, buf + ct->haswrit, size - ct->haswrit, 0);
 
     if (res == size - ct->haswrit)
@@ -455,9 +525,16 @@ int mosi_put(COMSTACK h, char *buf, int size)
 
 int mosi_close(COMSTACK h)
 {
-    free(h->private);
+    xfree(h->cprivate);
     if (h->iofile >= 0)
        u_close(h->iofile);
-    free(h);
+   xfree(h);
     return 0;
 }    
+
+char *mosi_addrstr(COMSTACK h)
+{
+    return "osi:[UNIMPLEMENTED]";
+}
+
+#endif