4 * Z39.50 API for the Email gateway - YAZ version
6 * $Log: zaccess-yaz.c,v $
7 * Revision 1.8 1995/04/28 14:18:38 quinn
8 * *** empty log message ***
10 * Revision 1.7 1995/04/21 16:29:32 quinn
13 * Revision 1.6 1995/04/21 12:58:36 adam
16 * Revision 1.5 1995/04/20 15:25:32 quinn
19 * Revision 1.4 1995/04/19 16:02:28 adam
22 * Revision 1.3 1995/04/19 12:55:15 quinn
25 * Revision 1.2 1995/04/19 09:24:02 quinn
26 * Fixed bug in zass_open - variable initialized after use
28 * Revision 1.1 1995/04/17 11:26:53 quinn
29 * Added YAZ version of zaccess
34 * Interface to the YAZ Z39.50 toolkit.
53 struct zass /* Z-assoc */
55 COMSTACK ass; /* comstack association handle */
58 int fd; /* low-level socket (for select only) */
61 int preferredmessagesize;
66 static Z_APDU *get_apdu(struct zass *z, int *complete)
71 if ((res = cs_get(z->ass, &z->inbuf, &z->inbuflen)) <= 0)
73 gw_log(GW_LOG_WARN, ZASS_TYPE, "cs_get failed");
85 odr_setbuf(z->decode, z->inbuf, res);
86 if (!z_APDU(z->decode, &ap, 0))
88 gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
89 odr_errlist[odr_geterror(z->decode)]);
99 static int send_apdu(struct zass *z, Z_APDU *a)
104 if (!z_APDU(z->encode, &a, 0))
106 gw_log(GW_LOG_FATAL, ZASS_TYPE, "encoding initreq");
109 buf = odr_getbuf(z->encode, &len);
110 if (cs_put(z->ass, buf, len) < 0)
112 gw_log(GW_LOG_FATAL, ZASS_TYPE, "cs_put");
115 odr_reset(z->encode); /* release odr_allocated structures */
119 static int send_initreq(struct zass *p, char *auth)
123 Odr_bitmask options, protocolVersion;
125 Z_IdAuthentication idauth;
127 a.which = Z_APDU_initRequest;
128 a.u.initRequest = &init;
129 init.referenceId = 0;
130 init.options = &options;
131 ODR_MASK_ZERO(&options);
132 ODR_MASK_SET(&options, Z_Options_search);
133 ODR_MASK_SET(&options, Z_Options_present);
134 ODR_MASK_SET(&options, Z_Options_delSet);
135 init.protocolVersion = &protocolVersion;
136 ODR_MASK_ZERO(&protocolVersion);
137 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
138 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
139 init.preferredMessageSize = &p->preferredmessagesize;
140 init.maximumRecordSize = &p->maxrecordsize;
144 init.idAuthentication = &idauth;
145 idauth.which = Z_IdAuthentication_open;
146 idauth.u.open = auth;
149 init.idAuthentication = 0;
150 init.implementationId = ZASS_ID;
151 sprintf(name, "%s (YAZ protocol layer)", ZASS_NAME);
152 init.implementationName = name;
153 init.implementationVersion = ZASS_VERSION;
154 init.userInformationField = 0;
155 if (send_apdu(p, &a) < 0)
160 int zass_fileno(ZASS a)
165 int zass_openresult(ZASS p, int *complete)
170 if (!(ap = get_apdu(p, complete)))
172 if (ap->which != Z_APDU_initResponse)
174 gw_log(GW_LOG_WARN, ZASS_TYPE, "bad APDU");
177 res = ap->u.initResponse;
180 gw_log(GW_LOG_WARN, ZASS_TYPE, "Negative result on initres");
183 p->preferredmessagesize = *res->preferredMessageSize;
184 p->maxrecordsize = *res->maximumRecordSize;
185 if (res->implementationId)
186 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
187 res->implementationId);
188 if (res->implementationName)
189 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
190 res->implementationName);
191 if (res->implementationVersion)
192 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
193 res->implementationVersion);
194 gw_log(ZASS_DEBUG, ZASS_TYPE, "Initialized ok");
198 ZASS zass_open(char *host, int port, int *complete, char *auth)
204 if (!(p = malloc(sizeof(*p))))
206 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
216 if (!(p->encode = odr_createmem(ODR_ENCODE)) ||
217 !(p->decode = odr_createmem(ODR_DECODE)))
219 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "odr_createmem");
222 p->maxrecordsize = ZASS_MAXRECORDSIZE;
223 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
224 if (!(p->ass = cs_create(tcpip_type, 1, CS_Z3950)))
226 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "cs_create");
231 sprintf(addstr, "%s:%d", host, port);
232 if (!(address = tcpip_strtoaddr(addstr)))
234 gw_log(GW_LOG_FATAL, ZASS_TYPE, "failed to resolve %s", addstr);
237 p->fd = cs_fileno(p->ass);
238 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connecting to %s", addstr);
239 if (cs_connect(p->ass, address) < 0)
241 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to connect to %s", addstr);
244 gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok... initializing");
245 if (send_initreq(p, auth) < 0)
247 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send init");
255 if (zass_openresult(p, complete) < 0)
257 return p; /* all done */
261 * Convert the egate (ccl2rpn) version of RPN to YAZ RPN.
263 static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
265 Z_RPNStructure *r = odr_malloc(o, sizeof(*r));
266 Z_AttributesPlusTerm *t;
267 struct ccl_rpn_attr *i;
269 static int op[] = { Z_Operator_and, Z_Operator_or, Z_Operator_and_not };
274 r->which = Z_RPNStructure_simple;
275 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
276 r->u.simple->which = Z_Operand_APT;
277 r->u.simple->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
278 t->term = odr_malloc(o, sizeof(Z_Term));
279 t->term->which = Z_Term_general;
280 t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
281 t->term->u.general->len = t->term->u.general->size =
283 t->term->u.general->buf = odr_malloc(o, t->term->u.general->size);
284 memcpy(t->term->u.general->buf, q->u.t.term,
285 t->term->u.general->len);
286 t->num_attributes = 0;
287 t->attributeList = odr_malloc(o, sizeof(Z_AttributeElement*) * 100);
288 for (i = q->u.t.attr_list; i && t->num_attributes < 100;
291 Z_AttributeElement *a;
293 t->attributeList[t->num_attributes++] = a =
294 odr_malloc(o, sizeof(*a));
295 a->attributeType = odr_malloc(o, sizeof(int));
296 *a->attributeType = i->type;
297 a->attributeValue = odr_malloc(o, sizeof(*a));
298 *a->attributeValue = i->value;
302 r->which = Z_RPNStructure_simple;
303 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
304 r->u.simple->which = Z_Operand_resultSetId;
305 r->u.simple->u.resultSetId = odr_malloc(o, len =
306 strlen(q->u.setname) + 1);
307 memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
309 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
310 r->which = Z_RPNStructure_complex;
311 r->u.complex = odr_malloc(o, sizeof(Z_Complex));
312 if (!(r->u.complex->s1 = rpn2rpn(o, q->u.p[0])) ||
313 !(r->u.complex->s2 = rpn2rpn(o, q->u.p[1])))
315 r->u.complex->operator = odr_malloc(o, sizeof(Z_Operator));
316 r->u.complex->operator->which = op[q->kind];
317 r->u.complex->operator->u.and = "";
320 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Bad operator in RPN");
325 const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
327 static struct zass_searchent r;
329 Z_SearchResponse *res;
331 if (!(apdu = get_apdu(a, complete)))
333 if (apdu->which != Z_APDU_searchResponse)
335 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected searchresponse, got #%d",
339 res = apdu->u.searchResponse;
340 r.status = *res->searchStatus;
341 r.num = *res->resultCount;
342 r.status = res->resultSetStatus ? *res->resultSetStatus : 0;
347 if (res->records->which != Z_Records_NSD)
348 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unexpected record types in SchR");
352 Z_DiagRec *dr = res->records->u.nonSurrogateDiagnostic;
354 if (!(id = oid_getentbyoid(dr->diagnosticSetId)) ||
355 id->class != CLASS_DIAGSET || id->value != VAL_BIB1)
356 gw_log(GW_LOG_WARN, ZASS_TYPE,
357 "Missing or unknown diagset - ignoring error!");
360 r.errcode = *dr->condition;
363 strncpy(r.errstring, dr->addinfo, 512);
364 r.errstring[511] = '\0';
372 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
373 char *resname, char *databases, int *complete)
380 int smallSetUpperBound = 0;
381 int largeSetLowerBound = 1;
382 int mediumSetPresentNumber = 0;
383 int replaceIndicator = 1;
386 apdu.which = Z_APDU_searchRequest;
387 apdu.u.searchRequest = &req;
389 req.smallSetUpperBound = &smallSetUpperBound;
390 req.largeSetLowerBound = &largeSetLowerBound;
391 req.mediumSetPresentNumber = &mediumSetPresentNumber;
392 req.replaceIndicator = &replaceIndicator;
393 req.resultSetName = resname;
394 req.num_databaseNames = 0;
395 req.databaseNames = datab;
396 while (*databases && req.num_databaseNames < 100)
401 while (*p && !isspace(*p))
410 req.databaseNames[req.num_databaseNames] = odr_malloc(a->encode,
411 (p - databases) + 1);
412 strcpy(req.databaseNames[req.num_databaseNames++], databases);
414 databases = p + more;
416 req.smallSetElementSetNames = 0;
417 req.mediumSetElementSetNames = 0;
418 req.preferredRecordSyntax = 0;
420 q.which = Z_Query_type_1;
422 bib1.proto = PROTO_Z3950;
423 bib1.class = CLASS_ATTSET;
424 bib1.value = VAL_BIB1;
425 rpnq.attributeSetId = oid_getoidbyent(&bib1);
429 if (!(rpnq.RPNStructure = rpn2rpn(a->encode, query)))
431 if (send_apdu(a, &apdu) < 0)
439 return zass_searchresult(a, complete);
443 * Triple indirection - that's kinda heavy. We'll fix it later.
444 * There are worse things around, though. Like ZDist.
446 void get_diagrec(zass_record ***p, Z_DiagRec *r)
448 **p = malloc(sizeof(***p));
450 (**p)->errcode = *r->condition;
453 strncpy((**p)->errstring, r->addinfo, 200);
454 (**p)->errstring[200] = 0;
457 (**p)->errstring[0] = '\0';
458 (**p)->which = ZASS_REC_DIAG;
462 void get_responserecords(zass_record ***p, Z_NamePlusRecordList *recs)
466 for (i = 0; i < recs->num_records; i++)
468 Z_NamePlusRecord *record;
470 record = recs->records[i];
471 if (record->which == Z_NamePlusRecord_surrogateDiagnostic)
472 get_diagrec(p, record->u.surrogateDiagnostic);
475 Z_DatabaseRecord *r = record->u.databaseRecord;
478 **p = malloc(sizeof(***p));
481 if (!(recform = oid_getentbyoid(r->direct_reference)) ||
482 recform->class != CLASS_RECSYN)
484 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown or bad record syntax");
485 (**p)->which = ZASS_REC_UNKNOWN;
488 switch (recform->value)
490 case VAL_USMARC: (**p)->which = ZASS_REC_USMARC; break;
492 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown recsyn");
493 (**p)->which = ZASS_REC_UNKNOWN;
495 if (r->which != ODR_EXTERNAL_octet)
497 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
502 if (!((**p)->record = malloc(r->u.octet_aligned->len + 1)))
504 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
507 memcpy((**p)->record, r->u.octet_aligned->buf,
508 r->u.octet_aligned->len);
509 (**p)->record[r->u.octet_aligned->len] = '\0';
510 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
511 r->u.octet_aligned->len);
518 static void zass_records_free(zass_record *p)
522 static int send_present(ZASS a, char *name, int start, int num,
526 Z_PresentRequest req;
531 apdu.which = Z_APDU_presentRequest;
532 apdu.u.presentRequest = &req;
534 req.resultSetId = name;
535 req.resultSetStartPoint = &start;
536 req.numberOfRecordsRequested = #
537 req.elementSetNames = 0;
539 recsyn.proto = PROTO_Z3950;
540 recsyn.class = CLASS_RECSYN;
542 req.preferredRecordSyntax = oid_getoidbyent(&recsyn);
544 req.preferredRecordSyntax = 0;
546 return send_apdu(a, &apdu);
550 * Note that 1== first record.
551 * TODO: make this baby operate in nonblocking mode, too.
553 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
554 int num, int *complete)
556 static struct zass_presentent r;
557 zass_record **rec = &r.records;
564 zass_records_free(r.records);
570 Z_PresentResponse *res;
572 gw_log(ZASS_DEBUG, ZASS_TYPE,
573 "Fetching %d records from # %d", num - r.num, start);
574 if (send_present(a, resname, start, num - r.num, VAL_USMARC) < 0)
576 if (!(apdu = get_apdu(a, complete)))
582 if (apdu->which != Z_APDU_presentResponse)
584 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse, got #%d",
588 res = apdu->u.presentResponse;
589 r.presentstatus = *res->presentStatus;
590 r.num += *res->numberOfRecordsReturned;
591 if (*res->numberOfRecordsReturned == 0)
593 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target");
596 r.nextpos = *res->nextResultSetPosition;
598 switch (res->records->which)
600 case Z_Records_DBOSD:
601 get_responserecords(&rec,
602 res->records->u.databaseOrSurDiagnostics);
605 get_diagrec(&rec, res->records->u.nonSurrogateDiagnostic);
608 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
612 while (num - r.num && start);
616 const struct zass_presentent *zass_presentresult(ZASS a, int *complete)