2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation, in whole or in part, for any purpose, is hereby granted,
15 * 1. This copyright and permission notice appear in all copies of the
16 * software and its documentation. Notices of copyright or attribution
17 * which appear at the beginning of any file must remain unchanged.
19 * 2. The names of EUROPAGATE or the project partners may not be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
23 * 3. Users of this software (implementors and gateway operators) agree to
24 * inform the EUROPAGATE consortium of their use of the software. This
25 * information will be used to evaluate the EUROPAGATE project and the
26 * software, and to plan further developments. The consortium may use
27 * the information in later publications.
29 * 4. Users of this software agree to make their best efforts, when
30 * documenting their use of the software, to acknowledge the EUROPAGATE
31 * consortium, and the role played by the software in their work.
33 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36 * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38 * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39 * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40 * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41 * USE OR PERFORMANCE OF THIS SOFTWARE.
47 * Z39.50 API for the Email gateway - YAZ version
50 * Revision 1.2 1995/07/05 11:08:33 adam
51 * Bug fix in init request.
53 * Revision 1.1 1995/07/03 08:21:31 adam
54 * Yaz layer moved to new sub directory. zaccess aligned with new
57 * Revision 1.10 1995/05/16 09:41:46 adam
58 * LICENSE. Uses new versions of odr_{set,get}buf.
60 * Revision 1.9 1995/05/03 10:15:43 quinn
61 * Fixed bug in the get_record loop - crashed when diagrec was received.
63 * Revision 1.8 1995/04/28 14:18:38 quinn
64 * *** empty log message ***
66 * Revision 1.7 1995/04/21 16:29:32 quinn
69 * Revision 1.6 1995/04/21 12:58:36 adam
72 * Revision 1.5 1995/04/20 15:25:32 quinn
75 * Revision 1.4 1995/04/19 16:02:28 adam
78 * Revision 1.3 1995/04/19 12:55:15 quinn
81 * Revision 1.2 1995/04/19 09:24:02 quinn
82 * Fixed bug in zass_open - variable initialized after use
84 * Revision 1.1 1995/04/17 11:26:53 quinn
85 * Added YAZ version of zaccess
90 * Interface to the YAZ Z39.50 toolkit.
109 struct zass /* Z-assoc */
111 COMSTACK ass; /* comstack association handle */
114 int fd; /* low-level socket (for select only) */
117 int preferredmessagesize;
122 static Z_APDU *get_apdu(struct zass *z, int *complete)
127 if ((res = cs_get(z->ass, &z->inbuf, &z->inbuflen)) <= 0)
129 gw_log(GW_LOG_WARN, ZASS_TYPE, "cs_get failed");
140 odr_reset(z->decode);
141 odr_setbuf(z->decode, z->inbuf, res, 0);
142 if (!z_APDU(z->decode, &ap, 0))
144 gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
145 odr_errlist[odr_geterror(z->decode)]);
155 static int send_apdu(struct zass *z, Z_APDU *a)
160 if (!z_APDU(z->encode, &a, 0))
162 gw_log(GW_LOG_FATAL, ZASS_TYPE, "encoding initreq");
165 buf = odr_getbuf(z->encode, &len, NULL);
166 if (cs_put(z->ass, buf, len) < 0)
168 gw_log(GW_LOG_FATAL, ZASS_TYPE, "cs_put");
171 odr_reset(z->encode); /* release odr_allocated structures */
175 static int send_initreq(struct zass *p, char *auth)
180 Z_IdAuthentication idauth;
182 apdu = zget_APDU (p->encode, Z_APDU_initRequest);
183 req = apdu->u.initRequest;
184 req->preferredMessageSize = &p->preferredmessagesize;
185 req->maximumRecordSize = &p->maxrecordsize;
187 ODR_MASK_SET(req->options, Z_Options_search);
188 ODR_MASK_SET(req->options, Z_Options_present);
189 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
191 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
192 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
196 req->idAuthentication = &idauth;
197 idauth.which = Z_IdAuthentication_open;
198 idauth.u.open = auth;
201 req->idAuthentication = 0;
202 sprintf(name, "%s (YAZ protocol layer)", ZASS_NAME);
203 req->implementationName = name;
204 req->implementationVersion = ZASS_VERSION;
205 if (send_apdu(p, apdu) < 0)
210 int zass_fileno(ZASS a)
215 int zass_openresult(ZASS p, int *complete)
220 if (!(ap = get_apdu(p, complete)))
222 if (ap->which != Z_APDU_initResponse)
224 gw_log(GW_LOG_WARN, ZASS_TYPE, "bad APDU");
227 res = ap->u.initResponse;
230 gw_log(GW_LOG_WARN, ZASS_TYPE, "Negative result on initres");
233 p->preferredmessagesize = *res->preferredMessageSize;
234 p->maxrecordsize = *res->maximumRecordSize;
235 if (res->implementationId)
236 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
237 res->implementationId);
238 if (res->implementationName)
239 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
240 res->implementationName);
241 if (res->implementationVersion)
242 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
243 res->implementationVersion);
244 gw_log(ZASS_DEBUG, ZASS_TYPE, "Initialized ok");
248 ZASS zass_open(char *host, int port, int *complete, char *auth)
254 if (!(p = malloc(sizeof(*p))))
256 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
266 if (!(p->encode = odr_createmem(ODR_ENCODE)) ||
267 !(p->decode = odr_createmem(ODR_DECODE)))
269 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "odr_createmem");
272 p->maxrecordsize = ZASS_MAXRECORDSIZE;
273 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
274 if (!(p->ass = cs_create(tcpip_type, 1, CS_Z3950)))
276 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "cs_create");
281 sprintf(addstr, "%s:%d", host, port);
282 if (!(address = tcpip_strtoaddr(addstr)))
284 gw_log(GW_LOG_FATAL, ZASS_TYPE, "failed to resolve %s", addstr);
287 p->fd = cs_fileno(p->ass);
288 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connecting to %s", addstr);
289 if (cs_connect(p->ass, address) < 0)
291 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to connect to %s", addstr);
294 gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok... initializing");
295 if (send_initreq(p, auth) < 0)
297 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send init");
305 if (zass_openresult(p, complete) < 0)
307 return p; /* all done */
311 * Convert the egate (ccl2rpn) version of RPN to YAZ RPN.
313 static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
315 Z_RPNStructure *r = odr_malloc(o, sizeof(*r));
316 Z_AttributesPlusTerm *t;
317 struct ccl_rpn_attr *i;
319 static int op[] = { Z_Operator_and, Z_Operator_or, Z_Operator_and_not };
324 r->which = Z_RPNStructure_simple;
325 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
326 r->u.simple->which = Z_Operand_APT;
327 r->u.simple->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
328 t->term = odr_malloc(o, sizeof(Z_Term));
329 t->term->which = Z_Term_general;
330 t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
331 t->term->u.general->len = t->term->u.general->size =
333 t->term->u.general->buf = odr_malloc(o, t->term->u.general->size);
334 memcpy(t->term->u.general->buf, q->u.t.term,
335 t->term->u.general->len);
336 t->num_attributes = 0;
337 t->attributeList = odr_malloc(o, sizeof(Z_AttributeElement*) * 100);
338 for (i = q->u.t.attr_list; i && t->num_attributes < 100;
341 Z_AttributeElement *a;
343 t->attributeList[t->num_attributes++] = a =
344 odr_malloc(o, sizeof(*a));
345 a->attributeType = odr_malloc(o, sizeof(int));
346 *a->attributeType = i->type;
349 a->which = Z_AttributeValue_numeric;
350 a->value.numeric = odr_malloc(o, sizeof(*a->value.numeric));
351 *a->value.numeric = i->value;
353 a->attributeValue = odr_malloc(o, sizeof(*a->attributeValue));
354 *a->attributeValue = i->value;
359 r->which = Z_RPNStructure_simple;
360 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
361 r->u.simple->which = Z_Operand_resultSetId;
362 r->u.simple->u.resultSetId = odr_malloc(o, len =
363 strlen(q->u.setname) + 1);
364 memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
366 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
367 r->which = Z_RPNStructure_complex;
368 r->u.complex = odr_malloc(o, sizeof(Z_Complex));
369 if (!(r->u.complex->s1 = rpn2rpn(o, q->u.p[0])) ||
370 !(r->u.complex->s2 = rpn2rpn(o, q->u.p[1])))
372 r->u.complex->operator = odr_malloc(o, sizeof(Z_Operator));
373 r->u.complex->operator->which = op[q->kind];
374 r->u.complex->operator->u.and = "";
377 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Bad operator in RPN");
382 const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
384 static struct zass_searchent r;
386 Z_SearchResponse *res;
388 if (!(apdu = get_apdu(a, complete)))
390 if (apdu->which != Z_APDU_searchResponse)
392 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected searchresponse, got #%d",
396 res = apdu->u.searchResponse;
397 r.status = *res->searchStatus;
398 r.num = *res->resultCount;
399 r.status = res->resultSetStatus ? *res->resultSetStatus : 0;
404 if (res->records->which != Z_Records_NSD)
405 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unexpected record types in SchR");
409 Z_DiagRec *dr = res->records->u.nonSurrogateDiagnostic;
412 if (dr->which != Z_DiagRec_defaultFormat ||
413 !(id = oid_getentbyoid
414 (dr->u.defaultFormat->diagnosticSetId)) ||
415 id->class != CLASS_DIAGSET ||
416 id->value != VAL_BIB1)
417 gw_log(GW_LOG_WARN, ZASS_TYPE,
418 "Missing or unknown diagset - ignoring error!");
421 r.errcode = *dr->u.defaultFormat->condition;
422 if (dr->u.defaultFormat->addinfo)
424 strncpy(r.errstring, dr->u.defaultFormat->addinfo, 512);
425 r.errstring[511] = '\0';
429 if (!(id = oid_getentbyoid(dr->diagnosticSetId)) ||
430 id->class != CLASS_DIAGSET || id->value != VAL_BIB1)
431 gw_log(GW_LOG_WARN, ZASS_TYPE,
432 "Missing or unknown diagset - ignoring error!");
435 r.errcode = *dr->condition;
438 strncpy(r.errstring, dr->addinfo, 512);
439 r.errstring[511] = '\0';
448 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
449 char *resname, char *databases, int *complete)
454 Z_SearchRequest *req;
456 int smallSetUpperBound = 0;
457 int largeSetLowerBound = 1;
458 int mediumSetPresentNumber = 0;
459 int replaceIndicator = 1;
462 apdu = zget_APDU (a->encode, Z_APDU_searchRequest);
463 req = apdu->u.searchRequest;
465 req->smallSetUpperBound = &smallSetUpperBound;
466 req->largeSetLowerBound = &largeSetLowerBound;
467 req->mediumSetPresentNumber = &mediumSetPresentNumber;
468 req->replaceIndicator = &replaceIndicator;
469 req->resultSetName = resname;
470 req->num_databaseNames = 0;
471 req->databaseNames = datab;
472 while (*databases && req->num_databaseNames < 100)
477 while (*p && !isspace(*p))
486 req->databaseNames[req->num_databaseNames] = odr_malloc(a->encode,
487 (p - databases) + 1);
488 strcpy(req->databaseNames[req->num_databaseNames++], databases);
490 databases = p + more;
493 q.which = Z_Query_type_1;
495 bib1.proto = PROTO_Z3950;
496 bib1.class = CLASS_ATTSET;
497 bib1.value = VAL_BIB1;
498 rpnq.attributeSetId = oid_getoidbyent(&bib1);
502 if (!(rpnq.RPNStructure = rpn2rpn(a->encode, query)))
504 if (send_apdu(a, apdu) < 0)
512 return zass_searchresult(a, complete);
516 * Triple indirection - that's kinda heavy. We'll fix it later.
517 * There are worse things around, though. Like ZDist.
519 void get_diagrec(zass_record ***p, Z_DiagRec *r)
521 **p = malloc(sizeof(***p));
524 (**p)->errstring[0] = '\0';
525 if (r->which == Z_DiagRec_defaultFormat)
527 (**p)->errcode = *r->u.defaultFormat->condition;
528 if (r->u.defaultFormat->addinfo)
530 strncpy((**p)->errstring, r->u.defaultFormat->addinfo, 200);
531 (**p)->errstring[200] = 0;
537 (**p)->errcode = *r->condition;
540 strncpy((**p)->errstring, r->addinfo, 200);
541 (**p)->errstring[200] = 0;
544 (**p)->errstring[0] = '\0';
546 (**p)->which = ZASS_REC_DIAG;
550 void get_responserecords(zass_record ***p, Z_NamePlusRecordList *recs)
554 for (i = 0; i < recs->num_records; i++)
556 Z_NamePlusRecord *record;
558 record = recs->records[i];
559 if (record->which == Z_NamePlusRecord_surrogateDiagnostic)
560 get_diagrec(p, record->u.surrogateDiagnostic);
563 Z_DatabaseRecord *r = record->u.databaseRecord;
566 **p = malloc(sizeof(***p));
569 if (!(recform = oid_getentbyoid(r->direct_reference)) ||
570 recform->class != CLASS_RECSYN)
572 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown or bad record syntax");
573 (**p)->which = ZASS_REC_UNKNOWN;
576 switch (recform->value)
578 case VAL_USMARC: (**p)->which = ZASS_REC_USMARC; break;
580 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown recsyn");
581 (**p)->which = ZASS_REC_UNKNOWN;
583 if (r->which != ODR_EXTERNAL_octet)
585 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
590 if (!((**p)->record = malloc(r->u.octet_aligned->len + 1)))
592 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
595 memcpy((**p)->record, r->u.octet_aligned->buf,
596 r->u.octet_aligned->len);
597 (**p)->record[r->u.octet_aligned->len] = '\0';
598 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
599 r->u.octet_aligned->len);
606 static void zass_records_free(zass_record *p)
610 static int send_present(ZASS a, char *name, int start, int num,
614 Z_PresentRequest *req;
619 apdu = zget_APDU (a->encode, Z_APDU_presentRequest);
620 req = apdu->u.presentRequest;
622 req->resultSetId = name;
623 req->resultSetStartPoint = &start;
624 req->numberOfRecordsRequested = #
626 recsyn.proto = PROTO_Z3950;
627 recsyn.class = CLASS_RECSYN;
629 req->preferredRecordSyntax = oid_getoidbyent(&recsyn);
631 req->preferredRecordSyntax = 0;
633 return send_apdu(a, apdu);
637 * Note that 1== first record.
638 * TODO: make this baby operate in nonblocking mode, too.
640 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
641 int num, int *complete)
643 static struct zass_presentent r;
644 zass_record **rec = &r.records;
651 zass_records_free(r.records);
657 Z_PresentResponse *res;
659 gw_log(ZASS_DEBUG, ZASS_TYPE,
660 "Fetching %d records from # %d", num - r.num, start);
661 if (send_present(a, resname, start, num - r.num, VAL_USMARC) < 0)
663 if (!(apdu = get_apdu(a, complete)))
669 if (apdu->which != Z_APDU_presentResponse)
671 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse, got #%d",
675 res = apdu->u.presentResponse;
676 r.presentstatus = *res->presentStatus;
677 r.num += *res->numberOfRecordsReturned;
678 if (*res->numberOfRecordsReturned == 0)
680 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target");
683 r.nextpos = *res->nextResultSetPosition;
685 switch (res->records->which)
687 case Z_Records_DBOSD:
688 get_responserecords(&rec,
689 res->records->u.databaseOrSurDiagnostics);
692 get_diagrec(&rec, res->records->u.nonSurrogateDiagnostic);
695 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
699 while (num - r.num && start);
703 const struct zass_presentent *zass_presentresult(ZASS a, int *complete)