* Z39.50 API for the Email gateway - YAZ version
*
* $Log: zaccess-yaz.c,v $
- * Revision 1.2 1995/04/19 09:24:02 quinn
+ * Revision 1.9 1995/05/03 10:15:43 quinn
+ * Fixed bug in the get_record loop - crashed when diagrec was received.
+ *
+ * Revision 1.8 1995/04/28 14:18:38 quinn
+ * *** empty log message ***
+ *
+ * Revision 1.7 1995/04/21 16:29:32 quinn
+ * Fixed bugs.
+ *
+ * Revision 1.6 1995/04/21 12:58:36 adam
+ * Bug fix.
+ *
+ * Revision 1.5 1995/04/20 15:25:32 quinn
+ * Asynch. API
+ *
+ * Revision 1.4 1995/04/19 16:02:28 adam
+ * Record type hack.
+ *
+ * Revision 1.3 1995/04/19 12:55:15 quinn
+ * Added auth.
+ *
+ * Revision 1.2 1995/04/19 09:24:02 quinn
* Fixed bug in zass_open - variable initialized after use
*
* Revision 1.1 1995/04/17 11:26:53 quinn
* Added YAZ version of zaccess
*
- *
*/
/*
- * Interface to the Z39.50 toolkit.
+ * Interface to the YAZ Z39.50 toolkit.
*/
#include <stdlib.h>
ODR encode;
ODR decode;
int fd; /* low-level socket (for select only) */
+ int nonblocking;
int maxrecordsize;
int preferredmessagesize;
- char *outbuf; /* intermediary buffer */
char *inbuf;
int inbuflen;
};
-static Z_APDU *get_apdu(struct zass *z)
+static Z_APDU *get_apdu(struct zass *z, int *complete)
{
int res;
Z_APDU *ap;
if ((res = cs_get(z->ass, &z->inbuf, &z->inbuflen)) <= 0)
{
gw_log(GW_LOG_WARN, ZASS_TYPE, "cs_get failed");
+ if (complete)
+ *complete = 1;
return 0;
}
+ else if (res == 1)
+ {
+ if (complete)
+ *complete = 0;
+ return 0;
+ }
odr_reset(z->decode);
odr_setbuf(z->decode, z->inbuf, res);
if (!z_APDU(z->decode, &ap, 0))
{
gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
odr_errlist[odr_geterror(z->decode)]);
+ if (complete)
+ *complete = 0;
return 0;
}
+ if (complete)
+ *complete = 1;
return ap;
}
gw_log(GW_LOG_FATAL, ZASS_TYPE, "cs_put");
return -1;
}
- odr_reset(z->encode);
+ odr_reset(z->encode); /* release odr_allocated structures */
return 0;
}
-static int send_initreq(struct zass *p)
+static int send_initreq(struct zass *p, char *auth)
{
Z_APDU a;
Z_InitRequest init;
Odr_bitmask options, protocolVersion;
char name[512];
+ Z_IdAuthentication idauth;
a.which = Z_APDU_initRequest;
a.u.initRequest = &init;
ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
init.preferredMessageSize = &p->preferredmessagesize;
init.maximumRecordSize = &p->maxrecordsize;
- init.idAuthentication = 0;
+
+ if (auth)
+ {
+ init.idAuthentication = &idauth;
+ idauth.which = Z_IdAuthentication_open;
+ idauth.u.open = auth;
+ }
+ else
+ init.idAuthentication = 0;
init.implementationId = ZASS_ID;
sprintf(name, "%s (YAZ protocol layer)", ZASS_NAME);
init.implementationName = name;
return 0;
}
-static int receive_initres(struct zass *p)
+int zass_fileno(ZASS a)
+{
+ return a->fd;
+}
+
+int zass_openresult(ZASS p, int *complete)
{
Z_APDU *ap;
Z_InitResponse *res;
- if (!(ap = get_apdu(p)))
+ if (!(ap = get_apdu(p, complete)))
return -1;
if (ap->which != Z_APDU_initResponse)
{
return 0;
}
-ZASS zass_open(char *host, int port)
+ZASS zass_open(char *host, int port, int *complete, char *auth)
{
struct zass *p;
char addstr[512];
gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
return 0;
}
+ if (complete)
+ {
+ *complete = 1;
+ p->nonblocking = 1;
+ }
+ else
+ p->nonblocking = 0;
if (!(p->encode = odr_createmem(ODR_ENCODE)) ||
!(p->decode = odr_createmem(ODR_DECODE)))
{
}
p->maxrecordsize = ZASS_MAXRECORDSIZE;
p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
- if (!(p->outbuf = malloc(p->maxrecordsize + 1024)))
- {
- gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
- return 0;
- }
- odr_setbuf(p->encode, p->outbuf, p->maxrecordsize + 1024);
if (!(p->ass = cs_create(tcpip_type, 1, CS_Z3950)))
{
gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "cs_create");
return 0;
}
+ p->inbuf = 0;
+ p->inbuflen = 0;
sprintf(addstr, "%s:%d", host, port);
if (!(address = tcpip_strtoaddr(addstr)))
{
gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to connect to %s", addstr);
return 0;
}
- gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok");
- p->inbuf = 0;
- p->inbuflen = 0;
- if (send_initreq(p) < 0 || receive_initres(p) < 0)
+ gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok... initializing");
+ if (send_initreq(p, auth) < 0)
{
- gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to initialize");
+ gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send init");
return 0;
}
- gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent init request");
- return p;
+ if (p->nonblocking)
+ {
+ *complete = 0;
+ return p;
+ }
+ if (zass_openresult(p, complete) < 0)
+ return 0;
+ return p; /* all done */
}
+/*
+ * Convert the egate (ccl2rpn) version of RPN to YAZ RPN.
+ */
static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
{
Z_RPNStructure *r = odr_malloc(o, sizeof(*r));
r->u.simple = odr_malloc(o, sizeof(Z_Operand));
r->u.simple->which = Z_Operand_resultSetId;
r->u.simple->u.resultSetId = odr_malloc(o, len =
- strlen(q->u.setname));
+ strlen(q->u.setname) + 1);
memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
return r;
case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
}
}
-static const struct zass_searchent *search_result(ZASS a)
+const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
{
static struct zass_searchent r;
Z_APDU *apdu;
Z_SearchResponse *res;
- if (!(apdu = get_apdu(a)))
+ if (!(apdu = get_apdu(a, complete)))
return 0;
if (apdu->which != Z_APDU_searchResponse)
{
}
const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
- char *resname, char *databases)
+ char *resname, char *databases, int *complete)
{
Z_Query q;
Z_RPNQuery rpnq;
bib1.class = CLASS_ATTSET;
bib1.value = VAL_BIB1;
rpnq.attributeSetId = oid_getoidbyent(&bib1);
+
+ if (complete)
+ *complete = 1;
if (!(rpnq.RPNStructure = rpn2rpn(a->encode, query)))
return 0;
if (send_apdu(a, &apdu) < 0)
return 0;
-
- return search_result(a);
+ if (complete)
+ {
+ *complete = 0;
+ return 0;
+ }
+ else
+ return zass_searchresult(a, complete);
}
/*
gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
r->u.octet_aligned->len);
}
+ (*p) = &(**p)->next;
}
- (*p) = &(**p)->next;
}
}
{
Z_APDU apdu;
Z_PresentRequest req;
+#if 0
oident recsyn;
+#endif
apdu.which = Z_APDU_presentRequest;
apdu.u.presentRequest = &req;
req.resultSetStartPoint = &start;
req.numberOfRecordsRequested = #
req.elementSetNames = 0;
+#if 0
recsyn.proto = PROTO_Z3950;
recsyn.class = CLASS_RECSYN;
recsyn.value = form;
req.preferredRecordSyntax = oid_getoidbyent(&recsyn);
+#else
+ req.preferredRecordSyntax = 0;
+#endif
return send_apdu(a, &apdu);
}
/*
* Note that 1== first record.
+ * TODO: make this baby operate in nonblocking mode, too.
*/
const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
- int num)
+ int num, int *complete)
{
static struct zass_presentent r;
zass_record **rec = &r.records;
+ if (complete)
+ *complete = 1;
r.num = 0;
if (r.records)
{
"Fetching %d records from # %d", num - r.num, start);
if (send_present(a, resname, start, num - r.num, VAL_USMARC) < 0)
return 0;
- if (!(apdu = get_apdu(a)))
+ if (!(apdu = get_apdu(a, complete)))
+ {
+ if (complete)
+ *complete = 1;
return 0;
+ }
if (apdu->which != Z_APDU_presentResponse)
{
gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse, got #%d",
while (num - r.num && start);
return &r;
}
+
+const struct zass_presentent *zass_presentresult(ZASS a, int *complete)
+{
+ *complete = 1;
+ return 0;
+}