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.1 1995/07/03 08:21:31 adam
51 * Yaz layer moved to new sub directory. zaccess aligned with new
54 * Revision 1.10 1995/05/16 09:41:46 adam
55 * LICENSE. Uses new versions of odr_{set,get}buf.
57 * Revision 1.9 1995/05/03 10:15:43 quinn
58 * Fixed bug in the get_record loop - crashed when diagrec was received.
60 * Revision 1.8 1995/04/28 14:18:38 quinn
61 * *** empty log message ***
63 * Revision 1.7 1995/04/21 16:29:32 quinn
66 * Revision 1.6 1995/04/21 12:58:36 adam
69 * Revision 1.5 1995/04/20 15:25:32 quinn
72 * Revision 1.4 1995/04/19 16:02:28 adam
75 * Revision 1.3 1995/04/19 12:55:15 quinn
78 * Revision 1.2 1995/04/19 09:24:02 quinn
79 * Fixed bug in zass_open - variable initialized after use
81 * Revision 1.1 1995/04/17 11:26:53 quinn
82 * Added YAZ version of zaccess
87 * Interface to the YAZ Z39.50 toolkit.
106 struct zass /* Z-assoc */
108 COMSTACK ass; /* comstack association handle */
111 int fd; /* low-level socket (for select only) */
114 int preferredmessagesize;
119 static Z_APDU *get_apdu(struct zass *z, int *complete)
124 if ((res = cs_get(z->ass, &z->inbuf, &z->inbuflen)) <= 0)
126 gw_log(GW_LOG_WARN, ZASS_TYPE, "cs_get failed");
137 odr_reset(z->decode);
138 odr_setbuf(z->decode, z->inbuf, res, 0);
139 if (!z_APDU(z->decode, &ap, 0))
141 gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
142 odr_errlist[odr_geterror(z->decode)]);
152 static int send_apdu(struct zass *z, Z_APDU *a)
157 if (!z_APDU(z->encode, &a, 0))
159 gw_log(GW_LOG_FATAL, ZASS_TYPE, "encoding initreq");
162 buf = odr_getbuf(z->encode, &len, NULL);
163 if (cs_put(z->ass, buf, len) < 0)
165 gw_log(GW_LOG_FATAL, ZASS_TYPE, "cs_put");
168 odr_reset(z->encode); /* release odr_allocated structures */
172 static int send_initreq(struct zass *p, char *auth)
177 Z_IdAuthentication idauth;
179 apdu = zget_APDU (p->encode, Z_APDU_initRequest);
180 req = apdu->u.initRequest;
181 req->preferredMessageSize = &p->preferredmessagesize;
182 req->maximumRecordSize = &p->maxrecordsize;
186 req->idAuthentication = &idauth;
187 idauth.which = Z_IdAuthentication_open;
188 idauth.u.open = auth;
191 req->idAuthentication = 0;
192 sprintf(name, "%s (YAZ protocol layer)", ZASS_NAME);
193 req->implementationName = name;
194 req->implementationVersion = ZASS_VERSION;
195 if (send_apdu(p, apdu) < 0)
200 int zass_fileno(ZASS a)
205 int zass_openresult(ZASS p, int *complete)
210 if (!(ap = get_apdu(p, complete)))
212 if (ap->which != Z_APDU_initResponse)
214 gw_log(GW_LOG_WARN, ZASS_TYPE, "bad APDU");
217 res = ap->u.initResponse;
220 gw_log(GW_LOG_WARN, ZASS_TYPE, "Negative result on initres");
223 p->preferredmessagesize = *res->preferredMessageSize;
224 p->maxrecordsize = *res->maximumRecordSize;
225 if (res->implementationId)
226 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
227 res->implementationId);
228 if (res->implementationName)
229 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
230 res->implementationName);
231 if (res->implementationVersion)
232 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
233 res->implementationVersion);
234 gw_log(ZASS_DEBUG, ZASS_TYPE, "Initialized ok");
238 ZASS zass_open(char *host, int port, int *complete, char *auth)
244 if (!(p = malloc(sizeof(*p))))
246 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
256 if (!(p->encode = odr_createmem(ODR_ENCODE)) ||
257 !(p->decode = odr_createmem(ODR_DECODE)))
259 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "odr_createmem");
262 p->maxrecordsize = ZASS_MAXRECORDSIZE;
263 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
264 if (!(p->ass = cs_create(tcpip_type, 1, CS_Z3950)))
266 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "cs_create");
271 sprintf(addstr, "%s:%d", host, port);
272 if (!(address = tcpip_strtoaddr(addstr)))
274 gw_log(GW_LOG_FATAL, ZASS_TYPE, "failed to resolve %s", addstr);
277 p->fd = cs_fileno(p->ass);
278 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connecting to %s", addstr);
279 if (cs_connect(p->ass, address) < 0)
281 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to connect to %s", addstr);
284 gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok... initializing");
285 if (send_initreq(p, auth) < 0)
287 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send init");
295 if (zass_openresult(p, complete) < 0)
297 return p; /* all done */
301 * Convert the egate (ccl2rpn) version of RPN to YAZ RPN.
303 static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
305 Z_RPNStructure *r = odr_malloc(o, sizeof(*r));
306 Z_AttributesPlusTerm *t;
307 struct ccl_rpn_attr *i;
309 static int op[] = { Z_Operator_and, Z_Operator_or, Z_Operator_and_not };
314 r->which = Z_RPNStructure_simple;
315 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
316 r->u.simple->which = Z_Operand_APT;
317 r->u.simple->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
318 t->term = odr_malloc(o, sizeof(Z_Term));
319 t->term->which = Z_Term_general;
320 t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
321 t->term->u.general->len = t->term->u.general->size =
323 t->term->u.general->buf = odr_malloc(o, t->term->u.general->size);
324 memcpy(t->term->u.general->buf, q->u.t.term,
325 t->term->u.general->len);
326 t->num_attributes = 0;
327 t->attributeList = odr_malloc(o, sizeof(Z_AttributeElement*) * 100);
328 for (i = q->u.t.attr_list; i && t->num_attributes < 100;
331 Z_AttributeElement *a;
333 t->attributeList[t->num_attributes++] = a =
334 odr_malloc(o, sizeof(*a));
335 a->attributeType = odr_malloc(o, sizeof(int));
336 *a->attributeType = i->type;
339 a->which = Z_AttributeValue_numeric;
340 a->value.numeric = odr_malloc(o, sizeof(*a->value.numeric));
341 *a->value.numeric = i->value;
343 a->attributeValue = odr_malloc(o, sizeof(*a->attributeValue));
344 *a->attributeValue = i->value;
349 r->which = Z_RPNStructure_simple;
350 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
351 r->u.simple->which = Z_Operand_resultSetId;
352 r->u.simple->u.resultSetId = odr_malloc(o, len =
353 strlen(q->u.setname) + 1);
354 memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
356 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
357 r->which = Z_RPNStructure_complex;
358 r->u.complex = odr_malloc(o, sizeof(Z_Complex));
359 if (!(r->u.complex->s1 = rpn2rpn(o, q->u.p[0])) ||
360 !(r->u.complex->s2 = rpn2rpn(o, q->u.p[1])))
362 r->u.complex->operator = odr_malloc(o, sizeof(Z_Operator));
363 r->u.complex->operator->which = op[q->kind];
364 r->u.complex->operator->u.and = "";
367 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Bad operator in RPN");
372 const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
374 static struct zass_searchent r;
376 Z_SearchResponse *res;
378 if (!(apdu = get_apdu(a, complete)))
380 if (apdu->which != Z_APDU_searchResponse)
382 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected searchresponse, got #%d",
386 res = apdu->u.searchResponse;
387 r.status = *res->searchStatus;
388 r.num = *res->resultCount;
389 r.status = res->resultSetStatus ? *res->resultSetStatus : 0;
394 if (res->records->which != Z_Records_NSD)
395 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unexpected record types in SchR");
399 Z_DiagRec *dr = res->records->u.nonSurrogateDiagnostic;
402 if (dr->which != Z_DiagRec_defaultFormat ||
403 !(id = oid_getentbyoid
404 (dr->u.defaultFormat->diagnosticSetId)) ||
405 id->class != CLASS_DIAGSET ||
406 id->value != VAL_BIB1)
407 gw_log(GW_LOG_WARN, ZASS_TYPE,
408 "Missing or unknown diagset - ignoring error!");
411 r.errcode = *dr->u.defaultFormat->condition;
412 if (dr->u.defaultFormat->addinfo)
414 strncpy(r.errstring, dr->u.defaultFormat->addinfo, 512);
415 r.errstring[511] = '\0';
419 if (!(id = oid_getentbyoid(dr->diagnosticSetId)) ||
420 id->class != CLASS_DIAGSET || id->value != VAL_BIB1)
421 gw_log(GW_LOG_WARN, ZASS_TYPE,
422 "Missing or unknown diagset - ignoring error!");
425 r.errcode = *dr->condition;
428 strncpy(r.errstring, dr->addinfo, 512);
429 r.errstring[511] = '\0';
438 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
439 char *resname, char *databases, int *complete)
444 Z_SearchRequest *req;
446 int smallSetUpperBound = 0;
447 int largeSetLowerBound = 1;
448 int mediumSetPresentNumber = 0;
449 int replaceIndicator = 1;
452 apdu = zget_APDU (a->encode, Z_APDU_searchRequest);
453 req = apdu->u.searchRequest;
455 req->smallSetUpperBound = &smallSetUpperBound;
456 req->largeSetLowerBound = &largeSetLowerBound;
457 req->mediumSetPresentNumber = &mediumSetPresentNumber;
458 req->replaceIndicator = &replaceIndicator;
459 req->resultSetName = resname;
460 req->num_databaseNames = 0;
461 req->databaseNames = datab;
462 while (*databases && req->num_databaseNames < 100)
467 while (*p && !isspace(*p))
476 req->databaseNames[req->num_databaseNames] = odr_malloc(a->encode,
477 (p - databases) + 1);
478 strcpy(req->databaseNames[req->num_databaseNames++], databases);
480 databases = p + more;
483 q.which = Z_Query_type_1;
485 bib1.proto = PROTO_Z3950;
486 bib1.class = CLASS_ATTSET;
487 bib1.value = VAL_BIB1;
488 rpnq.attributeSetId = oid_getoidbyent(&bib1);
492 if (!(rpnq.RPNStructure = rpn2rpn(a->encode, query)))
494 if (send_apdu(a, apdu) < 0)
502 return zass_searchresult(a, complete);
506 * Triple indirection - that's kinda heavy. We'll fix it later.
507 * There are worse things around, though. Like ZDist.
509 void get_diagrec(zass_record ***p, Z_DiagRec *r)
511 **p = malloc(sizeof(***p));
514 (**p)->errstring[0] = '\0';
515 if (r->which == Z_DiagRec_defaultFormat)
517 (**p)->errcode = *r->u.defaultFormat->condition;
518 if (r->u.defaultFormat->addinfo)
520 strncpy((**p)->errstring, r->u.defaultFormat->addinfo, 200);
521 (**p)->errstring[200] = 0;
527 (**p)->errcode = *r->condition;
530 strncpy((**p)->errstring, r->addinfo, 200);
531 (**p)->errstring[200] = 0;
534 (**p)->errstring[0] = '\0';
536 (**p)->which = ZASS_REC_DIAG;
540 void get_responserecords(zass_record ***p, Z_NamePlusRecordList *recs)
544 for (i = 0; i < recs->num_records; i++)
546 Z_NamePlusRecord *record;
548 record = recs->records[i];
549 if (record->which == Z_NamePlusRecord_surrogateDiagnostic)
550 get_diagrec(p, record->u.surrogateDiagnostic);
553 Z_DatabaseRecord *r = record->u.databaseRecord;
556 **p = malloc(sizeof(***p));
559 if (!(recform = oid_getentbyoid(r->direct_reference)) ||
560 recform->class != CLASS_RECSYN)
562 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown or bad record syntax");
563 (**p)->which = ZASS_REC_UNKNOWN;
566 switch (recform->value)
568 case VAL_USMARC: (**p)->which = ZASS_REC_USMARC; break;
570 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown recsyn");
571 (**p)->which = ZASS_REC_UNKNOWN;
573 if (r->which != ODR_EXTERNAL_octet)
575 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
580 if (!((**p)->record = malloc(r->u.octet_aligned->len + 1)))
582 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
585 memcpy((**p)->record, r->u.octet_aligned->buf,
586 r->u.octet_aligned->len);
587 (**p)->record[r->u.octet_aligned->len] = '\0';
588 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
589 r->u.octet_aligned->len);
596 static void zass_records_free(zass_record *p)
600 static int send_present(ZASS a, char *name, int start, int num,
604 Z_PresentRequest *req;
609 apdu = zget_APDU (a->encode, Z_APDU_presentRequest);
610 req = apdu->u.presentRequest;
612 req->resultSetId = name;
613 req->resultSetStartPoint = &start;
614 req->numberOfRecordsRequested = #
616 recsyn.proto = PROTO_Z3950;
617 recsyn.class = CLASS_RECSYN;
619 req->preferredRecordSyntax = oid_getoidbyent(&recsyn);
621 req->preferredRecordSyntax = 0;
623 return send_apdu(a, apdu);
627 * Note that 1== first record.
628 * TODO: make this baby operate in nonblocking mode, too.
630 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
631 int num, int *complete)
633 static struct zass_presentent r;
634 zass_record **rec = &r.records;
641 zass_records_free(r.records);
647 Z_PresentResponse *res;
649 gw_log(ZASS_DEBUG, ZASS_TYPE,
650 "Fetching %d records from # %d", num - r.num, start);
651 if (send_present(a, resname, start, num - r.num, VAL_USMARC) < 0)
653 if (!(apdu = get_apdu(a, complete)))
659 if (apdu->which != Z_APDU_presentResponse)
661 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse, got #%d",
665 res = apdu->u.presentResponse;
666 r.presentstatus = *res->presentStatus;
667 r.num += *res->numberOfRecordsReturned;
668 if (*res->numberOfRecordsReturned == 0)
670 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target");
673 r.nextpos = *res->nextResultSetPosition;
675 switch (res->records->which)
677 case Z_Records_DBOSD:
678 get_responserecords(&rec,
679 res->records->u.databaseOrSurDiagnostics);
682 get_diagrec(&rec, res->records->u.nonSurrogateDiagnostic);
685 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
689 while (num - r.num && start);
693 const struct zass_presentent *zass_presentresult(ZASS a, int *complete)