--- /dev/null
+/*
+ * Europagate, 1995
+ *
+ * $Log: zaccess.c,v $
+ * Revision 1.1 1995/02/16 14:47:55 quinn
+ * First kick.
+ *
+ */
+
+/*
+ * Interface to the Z39.50 toolkit.
+ */
+
+#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", i->type, i->value);
+ *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;
+ }
+}
+
+ZASS zass_open(char *host, int port)
+{
+ struct zass *p;
+ PINITREQUEST ireq;
+ PINITRESPONSE ires;
+ int len;
+
+ 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);
+ ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
+ p->preferredmessagesize, ZASS_ID, ZASS_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 ((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);
+ 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);
+ 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
+ *r.errstring = '\0';
+ }
+ else
+ *r.errstring = '\0';
+ }
+ else
+ gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
+ }
+ SearchResponse_Destroy(pdu);
+
+ return &r;
+}
+
+/*
+ * Note that 1== first record.
+ */
+const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
+ int num)
+{
+ return 0;
+}