Lots of changes. They aren't visible though.
[egate.git] / zlayer / zaccess.c
index 374feda..eef4187 100644 (file)
@@ -1,8 +1,92 @@
 /*
+ * 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.3  1995/02/16 15:20:45  quinn
+ * 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
  */
 
 /*
- * Interface to the Z39.50 toolkit.
+ * 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>
@@ -56,7 +151,7 @@ int rpn2kwaqs(struct ccl_rpn_node *q, char **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, "]");
@@ -86,12 +181,44 @@ int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
     }
 }
 
-ZASS zass_open(char *host, int port)
+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))))
     {
@@ -120,10 +247,11 @@ ZASS zass_open(char *host, int port)
        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);
+    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, ZASS_NAME, ZASS_VERSION, 0);
+       p->preferredmessagesize, ZASS_ID, name, ZASS_VERSION, 0);
     if (!ireq)
     {
        gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
@@ -143,29 +271,12 @@ ZASS zass_open(char *host, int port)
        return 0;
     }
     gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
-    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.");
+
+    if (zass_openresult(p, complete) < 0 && (!complete || *complete))
        return 0;
-    }
-    gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
-    p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
-    p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
-    InitResponse_Destroy(ires);
-    return p;
+    else
+       return p;
+
 }
 
 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
@@ -249,10 +360,192 @@ const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
 }
 
 /*
+ * 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)
 {
-    return 0;
+    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;
 }