Zdist layer moved to new sub directory.
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 3 Jul 1995 08:21:42 +0000 (08:21 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 3 Jul 1995 08:21:42 +0000 (08:21 +0000)
zlayer-zdist/Makefile [new file with mode: 0644]
zlayer-zdist/zaccess.c [new file with mode: 0644]

diff --git a/zlayer-zdist/Makefile b/zlayer-zdist/Makefile
new file mode 100644 (file)
index 0000000..ae4b383
--- /dev/null
@@ -0,0 +1,48 @@
+# Makefile for Email gateway Z39.50 interface
+# Europagate, 1995
+#
+# Makefile for the Zdist interface
+# $Id: Makefile,v 1.1 1995/07/03 08:21:42 adam Exp $
+# 
+SHELL=/bin/sh
+
+ZDEFS=-DLOW_TO_HIGH -Dfar=
+ZPRE=/home/proj/zdist/zdist102b1-1/libz3950
+ZINC=-I$(ZPRE)
+ZLIB=$(ZPRE)/libz3950.a
+
+INCLUDE=-I. -I../include $(ZINC)
+LIB=../lib/libzass.a
+PO=zaccess-yaz.o
+CPP=$(CC) -E
+DEFS=$(INCLUDE)
+
+all: $(LIB)
+
+$(LIB): $(PO)
+       rm -f $(LIB)
+       ar qc $(LIB) $(PO)
+       ranlib $(LIB)
+
+.c.o:
+       $(CC) -c $(DEFS) $(CFLAGS) $(ZDEFS) $<
+
+clean:
+       rm -f *.log *.[oa] core mon.out gmon.out errlist *~
+
+depend: depend2
+
+depend1:
+       sed '/^#Depend/q' <Makefile >Makefile.tmp
+       $(CPP) $(DEFS) -M *.c >>Makefile.tmp
+       mv -f Makefile.tmp Makefile
+
+depend2:
+       $(CPP) $(DEFS) -M *.c >.depend  
+
+#GNU make style depend
+ifeq (.depend,$(wildcard .depend))
+include .depend
+endif
+
+#Depend --- DOT NOT DELETE THIS LINE
diff --git a/zlayer-zdist/zaccess.c b/zlayer-zdist/zaccess.c
new file mode 100644 (file)
index 0000000..ae181e7
--- /dev/null
@@ -0,0 +1,554 @@
+/*
+ * Copyright (c) 1995, the EUROPAGATE consortium (see below).
+ *
+ * The EUROPAGATE consortium members are:
+ *
+ *    University College Dublin
+ *    Danmarks Teknologiske Videnscenter
+ *    An Chomhairle Leabharlanna
+ *    Consejo Superior de Investigaciones Cientificas
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation, in whole or in part, for any purpose, is hereby granted,
+ * provided that:
+ *
+ * 1. This copyright and permission notice appear in all copies of the
+ * software and its documentation. Notices of copyright or attribution
+ * which appear at the beginning of any file must remain unchanged.
+ *
+ * 2. The names of EUROPAGATE or the project partners may not be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * 3. Users of this software (implementors and gateway operators) agree to
+ * inform the EUROPAGATE consortium of their use of the software. This
+ * information will be used to evaluate the EUROPAGATE project and the
+ * software, and to plan further developments. The consortium may use
+ * the information in later publications.
+ * 
+ * 4. Users of this software agree to make their best efforts, when
+ * documenting their use of the software, to acknowledge the EUROPAGATE
+ * consortium, and the role played by the software in their work.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
+ * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
+ * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
+ * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
+ * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+/*
+ * Europagate, 1995
+ *
+ * Z39.50 API for the Email gateway
+ *
+ * $Log: zaccess.c,v $
+ * Revision 1.1  1995/07/03 08:21:43  adam
+ * Zdist layer moved to new sub directory.
+ *
+ * Revision 1.17  1995/05/16  09:41:47  adam
+ * LICENSE. Uses new versions of odr_{set,get}buf.
+ *
+ * Revision 1.16  1995/04/20  15:25:34  quinn
+ * Asynch. API
+ *
+ * Revision 1.15  1995/04/17  11:26:55  quinn
+ * Added YAZ version of zaccess
+ *
+ * Revision 1.14  1995/02/23  08:32:26  adam
+ * Changed header.
+ *
+ * Revision 1.12  1995/02/20  20:35:37  quinn
+ * Pull present status from presresp.
+ *
+ * Revision 1.11  1995/02/20  18:58:05  quinn
+ * Added hack for record in ANY
+ *
+ * Revision 1.10  1995/02/20  18:19:30  quinn
+ * More relaxed about record types.
+ *
+ * Revision 1.9  1995/02/17  15:17:51  quinn
+ * Bug fix
+ *
+ * Revision 1.8  1995/02/17  14:48:41  quinn
+ * 'nother bug in present
+ *
+ * Revision 1.7  1995/02/17  14:42:21  quinn
+ * Trivial bug in fetch-loop.
+ *
+ * Revision 1.6  1995/02/17  14:41:22  quinn
+ * Debugging.
+ *
+ * Revision 1.5  1995/02/17  13:58:01  quinn
+ * First kick at present handling
+ *
+ * Revision 1.4  1995/02/16  15:33:45  quinn
+ * Fixed bug in KWAQS generator
+ *
+ * Revision 1.3  1995/02/16  15:20:45  quinn
+ * Added initialization of response from search
+ *
+ * Revision 1.2  1995/02/16  15:14:53  quinn
+ * Fixed KWAQS-generator
+ *
+ * Revision 1.1  1995/02/16  14:47:55  quinn
+ * First kick.
+ *
+ */
+
+/*
+ * Interface to the Z39.50 toolkit. Primary function is to hide Zdist, or
+ * whatever lower-layer we decide to use later. The decision to add a
+ * layer atop the toolkit was twofold: It vastly simplifies the
+ * implementation of the protocol persistence, and it hides Zdist. The
+ * latter is useful after Zdist has gone and changed their fine API after
+ * we went through all the trouble of documenting it in our Design. Don't
+ * want that to happen again.
+ *
+ * For the time being at least, we'll have these routines hang (or err) if
+ * they get a WOULDBLOCK on a write. That seems safe since, under normal
+ * circumstances, the network buffers should always be able to absorb
+ * the small request packages.
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+
+#include <z3950.h>
+#include <z3950sup.h>
+#include <zutil.h>
+
+#include <gw-log.h>
+
+#include <ccl.h>
+#include <zaccess.h>
+
+struct zass    /* Z-assoc */
+{
+    NETBOXPROFILE *ass;              /* ZDIST association handle */
+    int fd;                         /* low-level socket (for select only) */
+    int maxrecordsize;
+    int preferredmessagesize;
+    char *buf;                      /* intermediary buffer */
+};
+
+int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
+{
+    struct ccl_rpn_attr *i;
+    static char *ops[] = {"and", "or", "not"};
+
+    assert(!CCL_RPN_AND);
+    switch (q->kind)
+    {
+       case CCL_RPN_TERM:
+           strcpy(*p, q->u.t.term);
+           (*p) += strlen(q->u.t.term);
+           if (q->u.t.attr_list)
+           {
+               strcat(*p, "[");
+               (*p)++;
+               for (i = q->u.t.attr_list; i; i = i->next)
+               {
+                   sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
+                       "," : "");
+                   *p += strlen(*p);
+               }
+               strcat(*p, "]");
+               (*p)++;
+           }
+           return 0;
+       case CCL_RPN_SET:
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
+           return -1;
+       case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
+           strcpy(*p, ops[q->kind]);
+           *p += strlen(ops[q->kind]);
+           strcat(*p, "(");
+           (*p)++;
+           if (rpn2kwaqs(q->u.p[0], p) < 0)
+               return -1;
+           strcat(*p, ",");
+           (*p)++;
+           if (rpn2kwaqs(q->u.p[1], p) < 0)
+               return -1;
+           strcat(*p, ")");
+           (*p)++;
+           return 0;
+       default:
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
+           return -1;
+    }
+}
+
+int zass_openresult(ZASS p, int *complete)
+{
+    int len;
+    PINITRESPONSE ires;
+
+    if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
+       p->maxrecordsize)) <= 0)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
+       return 0;
+    }
+    ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
+    if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
+       return 0;
+    }
+    gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
+    if (!InitResponse_GetResult(ires))
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
+       return 0;
+    }
+    gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
+    p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
+    p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
+    InitResponse_Destroy(ires);
+    *complete = 1;
+    return 0;
+}
+
+ZASS zass_open(char *host, int port, int *complete)
+{
+    struct zass *p;
+    PINITREQUEST ireq;
+    PINITRESPONSE ires;
+    int len;
+    char name[512];
+
+    if (!(p = malloc(sizeof(*p))))
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
+       return 0;
+    }
+    p->maxrecordsize = ZASS_MAXRECORDSIZE;
+    p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
+    if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
+       return 0;
+    }
+    if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
+       return 0;
+    }
+    p->ass->TimeOutSec = 120;
+    p->ass->TimeOutUSec = 0;
+    strcpy(p->ass->HostName, host);
+    p->ass->Port = port;
+
+    if (netbox_Open(p->ass) != 1)
+    {
+       gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
+       return 0;
+    }
+    gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d",
+        p->ass->HostName, p->ass->Port);
+    sprintf(name, "%s (ZDIST protocol layer)", ZASS_NAME);
+    ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
+       p->preferredmessagesize, ZASS_ID, name, ZASS_VERSION, 0);
+    if (!ireq)
+    {
+       gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
+       return 0;
+    }
+    zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
+       p->maxrecordsize);
+    if (len <= 0)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
+       return 0;
+    }
+    InitRequest_Destroy(ireq);
+    if (netbox_SendBuffer(p->ass, p->buf, len) != len)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
+       return 0;
+    }
+    gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
+
+    if (zass_openresult(p, complete) < 0 && (!complete || *complete))
+       return 0;
+    else
+       return p;
+
+}
+
+const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
+    char *resname, char *databases)
+{
+    static struct zass_searchent r;
+    char kwaqs[512], *p;
+    DATA_DIR *pdu, *record;
+    int len;
+
+    p = kwaqs;
+    if (rpn2kwaqs(query, &p) < 0)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
+       return 0;
+    }
+    gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
+    pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
+       0, 0, 0, kwaqs, BIB1_OID);
+    if (!pdu)
+    {
+       gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
+       return 0;
+    }
+    zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
+       a->maxrecordsize);
+    if (len <= 0)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
+       return 0;
+    }
+    SearchRequest_Destroy(pdu);
+    if (netbox_SendBuffer(a->ass, a->buf, len) != len)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
+       return 0;
+    }
+    gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
+    if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
+       a->maxrecordsize)) <= 0)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
+       return 0;
+    }
+    pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
+    if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
+    {
+       gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
+       return 0;
+    }
+    gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
+    r.status = SearchResponse_GetSearchStatus(pdu);
+    r.num = SearchResponse_GetResultCount(pdu);
+    r.status = SearchResponse_GetResultSetStatus(pdu);
+    r.errcode = -1;
+    *r.errstring = '\0';
+    if ((record = SearchResponse_GetRecords(pdu)))
+    {
+       if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
+       {
+           DATA_DIR *ad;
+
+           r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
+           if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
+           {
+               char *s;
+
+               if ((s = OctetString_GetASCIIString(ad)))
+               {
+                   strcpy(r.errstring, s);
+                   FREE(s);
+               }
+           }
+       }
+       else
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
+    }
+    SearchResponse_Destroy(pdu);
+
+    return &r;
+}
+
+/*
+ * Triple indirection - that's kinda heavy. We'll fix it later.
+ * There are worse things around, though. Like ZDist.
+ */
+void get_diagrec(zass_record ***p, DATA_DIR *rec)
+{
+    DATA_DIR *ad;
+
+    **p = malloc(sizeof(***p));
+    (**p)->next = 0;
+    (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
+    if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
+    {
+       char *s;
+
+       if ((s = OctetString_GetASCIIString(ad)))
+       {
+           strcpy((**p)->errstring, s);
+           FREE(s);
+       }
+    }
+    (**p)->which = ZASS_REC_DIAG;
+    *p = &(**p)->next;
+}
+
+void get_responserecords(zass_record ***p, DATA_DIR *rec)
+{
+    int num, recsyntaxlen, i;
+    DATA_DIR *record, *retrec, *align;
+    PEXTERNAL ext;
+    POBJECTIDENTIFIER oid;
+    char recsyntax[256];
+
+    num = ResponseRecords_GetCount(rec);
+    for (i = 1; i <= num; i++)
+    {
+       record = ResponseRecords_GetRecord(rec, i);
+       if (!record)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
+           return;
+       }
+       retrec = NamePlusRecord_GetRetrievalRecord(record);
+       if (!retrec)
+       {
+           /* check if it's a diagrec */
+           if (record->ptr.child->fldid == 2)
+               get_diagrec(p, record->ptr.child);
+           else
+           {
+               gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
+               return;
+           }
+       }
+       ext = RetrievalRecord_GetExternal(retrec);
+       if (!ext)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
+           return;
+       }
+       oid = External_GetDirectReference(ext);
+       if (!oid)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
+           return;
+       }
+       recsyntaxlen = DirectReference_GetLength(oid);
+       memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
+       recsyntax[recsyntaxlen] = '\0';
+       **p = malloc(sizeof(***p));
+       (**p)->next = 0;
+       if (!strcmp(recsyntax, USMARC_OID))
+           (**p)->which = ZASS_REC_USMARC;
+       else
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Type was '%d'", (**p)->which);
+       }
+       align = External_GetEncodingAligned(ext);
+       if (!align)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
+           align = External_GetEncodingSingle(ext);
+           if (!align)
+           {
+               gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't ANY");
+               return;
+           }
+           align = align->ptr.child;
+       }
+       if (!((**p)->record = malloc(align->count + 1)))
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
+           return;
+       }
+       memcpy((**p)->record, align->ptr.data, align->count);
+       (**p)->record[align->count] = '\0';
+       gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
+           align->count);
+
+       (*p) = &(**p)->next;
+    }
+}
+
+static void zass_records_free(zass_record *p)
+{
+}
+
+/*
+ * Note that 1== first record.
+ */
+const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
+    int num)
+{
+    static struct zass_presentent r = {0, 0, 0, 0};
+    zass_record **rec = &r.records;
+    DATA_DIR *pdu;
+    int len;
+
+    r.num = 0;
+    if (r.records)
+    {
+       zass_records_free(r.records);
+       r.records = 0;
+    }
+    do
+    {
+       gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
+           start);
+       pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
+           USMARC_OID);
+       if (!pdu)
+       {
+           gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
+           return 0;
+       }
+       zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
+           a->maxrecordsize);
+       if (len <= 0)
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
+           return 0;
+       }
+       PresentRequest_Destroy(pdu);
+       if (netbox_SendBuffer(a->ass, a->buf, len) != len)
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
+           return 0;
+       }
+       gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
+       if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
+           a->maxrecordsize)) <= 0)
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
+           return 0;
+       }
+       pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
+       if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
+           return 0;
+       }
+       gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
+       r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
+       r.presentstatus = PresentResponse_GetPresentStatus(pdu);
+       if (r.num == 0)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
+           return 0;
+       }
+       r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
+       start = r.nextpos;
+       switch(PresentResponse_GetRecordType(pdu))
+       {
+           case RESPONSERECORDS_TAG:
+               get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
+               break;
+           case NONSURROGATEDIAGNOSTIC_TAG:
+               get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
+               break;
+           default:
+               gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
+       }
+       PresentResponse_Destroy(pdu);
+    }
+    while (num - r.num && start);
+    *rec = 0;
+       
+    return &r;
+}