From 8efa8d0416a5c568739f0686bd50af4253667cc6 Mon Sep 17 00:00:00 2001 From: Sebastian Hammer Date: Thu, 16 Feb 1995 14:47:55 +0000 Subject: [PATCH] First kick. --- zlayer/Makefile | 55 ++++++++++++ zlayer/zaccess.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 308 insertions(+) create mode 100644 zlayer/Makefile create mode 100644 zlayer/zaccess.c diff --git a/zlayer/Makefile b/zlayer/Makefile new file mode 100644 index 0000000..0aee2b5 --- /dev/null +++ b/zlayer/Makefile @@ -0,0 +1,55 @@ +# Makefile for Email gateway utilities +# Europagate, 1995 +# +# $Log: Makefile,v $ +# Revision 1.1 1995/02/16 14:47:55 quinn +# First kick. +# +# + +ZPRE=/home/proj/zdist/zdist102b1-1/libz3950 +ZINC=-I$(ZPRE) +ZLIB=$(ZPRE)/libz3950.a + +SHELL=/bin/sh +INCLUDE=-I. -I../include $(ZINC) +CFLAGS=-g -Wall -pedantic -ansi +TPROG1=test +LIB=../lib/zaccess.a +PO=zaccess.o +CPP=cc -E +DEFS=$(INCLUDE) +ZDEFS=-DLOW_TO_HIGH -Dfar= + +all: $(LIB) + +$(TPROG1): $(TPROG1).o $(LIB) + $(CC) $(CFLAGS) -o $(TPROG1) $(TPROG1).o $(LIB) + +$(LIB): $(PO) + rm -f $(LIB) + ar qc $(LIB) $(PO) + ranlib $(LIB) + +.c.o: + $(CC) -c $(DEFS) $(CFLAGS) $(ZDEFS) $< + +clean: + rm -f *.log *.[oa] $(TPROG1) $(TPROG2) core mon.out gmon.out errlist *~ + +depend: depend2 + +depend1: + mv Makefile Makefile.tmp + sed '/^#Depend/q' Makefile + $(CPP) $(INCLUDE) -M *.c >>Makefile + -rm Makefile.tmp + +depend2: + $(CPP) $(INCLUDE) -M *.c >.depend + +ifeq (.depend,$(wildcard .depend)) +include .depend +endif + +#Depend --- DOT NOT DELETE THIS LINE diff --git a/zlayer/zaccess.c b/zlayer/zaccess.c new file mode 100644 index 0000000..ea237a9 --- /dev/null +++ b/zlayer/zaccess.c @@ -0,0 +1,253 @@ +/* + * 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 +#include + +#include +#include +#include + +#include + +#include +#include + +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; +} -- 1.7.10.4