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
49 * $Log: zaccess-yaz.c,v $
50 * Revision 1.11 1995/07/03 08:20:50 adam
51 * Old directory. To be deleted.
53 * Revision 1.10 1995/05/16 09:41:46 adam
54 * LICENSE. Uses new versions of odr_{set,get}buf.
56 * Revision 1.9 1995/05/03 10:15:43 quinn
57 * Fixed bug in the get_record loop - crashed when diagrec was received.
59 * Revision 1.8 1995/04/28 14:18:38 quinn
60 * *** empty log message ***
62 * Revision 1.7 1995/04/21 16:29:32 quinn
65 * Revision 1.6 1995/04/21 12:58:36 adam
68 * Revision 1.5 1995/04/20 15:25:32 quinn
71 * Revision 1.4 1995/04/19 16:02:28 adam
74 * Revision 1.3 1995/04/19 12:55:15 quinn
77 * Revision 1.2 1995/04/19 09:24:02 quinn
78 * Fixed bug in zass_open - variable initialized after use
80 * Revision 1.1 1995/04/17 11:26:53 quinn
81 * Added YAZ version of zaccess
86 * Interface to the YAZ Z39.50 toolkit.
105 struct zass /* Z-assoc */
107 COMSTACK ass; /* comstack association handle */
110 int fd; /* low-level socket (for select only) */
113 int preferredmessagesize;
118 static Z_APDU *get_apdu(struct zass *z, int *complete)
123 if ((res = cs_get(z->ass, &z->inbuf, &z->inbuflen)) <= 0)
125 gw_log(GW_LOG_WARN, ZASS_TYPE, "cs_get failed");
136 odr_reset(z->decode);
137 odr_setbuf(z->decode, z->inbuf, res, 0);
138 if (!z_APDU(z->decode, &ap, 0))
140 gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
141 odr_errlist[odr_geterror(z->decode)]);
151 static int send_apdu(struct zass *z, Z_APDU *a)
156 if (!z_APDU(z->encode, &a, 0))
158 gw_log(GW_LOG_FATAL, ZASS_TYPE, "encoding initreq");
161 buf = odr_getbuf(z->encode, &len, NULL);
162 if (cs_put(z->ass, buf, len) < 0)
164 gw_log(GW_LOG_FATAL, ZASS_TYPE, "cs_put");
167 odr_reset(z->encode); /* release odr_allocated structures */
171 static int send_initreq(struct zass *p, char *auth)
176 Z_IdAuthentication idauth;
178 apdu = zget_APDU (p->encode, Z_APDU_initRequest);
179 req = apdu->u.initRequest;
180 req->preferredMessageSize = &p->preferredmessagesize;
181 req->maximumRecordSize = &p->maxrecordsize;
185 req->idAuthentication = &idauth;
186 idauth.which = Z_IdAuthentication_open;
187 idauth.u.open = auth;
190 req->idAuthentication = 0;
191 sprintf(name, "%s (YAZ protocol layer)", ZASS_NAME);
192 req->implementationName = name;
193 req->implementationVersion = ZASS_VERSION;
194 if (send_apdu(p, apdu) < 0)
199 int zass_fileno(ZASS a)
204 int zass_openresult(ZASS p, int *complete)
209 if (!(ap = get_apdu(p, complete)))
211 if (ap->which != Z_APDU_initResponse)
213 gw_log(GW_LOG_WARN, ZASS_TYPE, "bad APDU");
216 res = ap->u.initResponse;
219 gw_log(GW_LOG_WARN, ZASS_TYPE, "Negative result on initres");
222 p->preferredmessagesize = *res->preferredMessageSize;
223 p->maxrecordsize = *res->maximumRecordSize;
224 if (res->implementationId)
225 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
226 res->implementationId);
227 if (res->implementationName)
228 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
229 res->implementationName);
230 if (res->implementationVersion)
231 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
232 res->implementationVersion);
233 gw_log(ZASS_DEBUG, ZASS_TYPE, "Initialized ok");
237 ZASS zass_open(char *host, int port, int *complete, char *auth)
243 if (!(p = malloc(sizeof(*p))))
245 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
255 if (!(p->encode = odr_createmem(ODR_ENCODE)) ||
256 !(p->decode = odr_createmem(ODR_DECODE)))
258 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "odr_createmem");
261 p->maxrecordsize = ZASS_MAXRECORDSIZE;
262 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
263 if (!(p->ass = cs_create(tcpip_type, 1, CS_Z3950)))
265 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "cs_create");
270 sprintf(addstr, "%s:%d", host, port);
271 if (!(address = tcpip_strtoaddr(addstr)))
273 gw_log(GW_LOG_FATAL, ZASS_TYPE, "failed to resolve %s", addstr);
276 p->fd = cs_fileno(p->ass);
277 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connecting to %s", addstr);
278 if (cs_connect(p->ass, address) < 0)
280 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to connect to %s", addstr);
283 gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok... initializing");
284 if (send_initreq(p, auth) < 0)
286 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send init");
294 if (zass_openresult(p, complete) < 0)
296 return p; /* all done */
300 * Convert the egate (ccl2rpn) version of RPN to YAZ RPN.
302 static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
304 Z_RPNStructure *r = odr_malloc(o, sizeof(*r));
305 Z_AttributesPlusTerm *t;
306 struct ccl_rpn_attr *i;
308 static int op[] = { Z_Operator_and, Z_Operator_or, Z_Operator_and_not };
313 r->which = Z_RPNStructure_simple;
314 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
315 r->u.simple->which = Z_Operand_APT;
316 r->u.simple->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
317 t->term = odr_malloc(o, sizeof(Z_Term));
318 t->term->which = Z_Term_general;
319 t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
320 t->term->u.general->len = t->term->u.general->size =
322 t->term->u.general->buf = odr_malloc(o, t->term->u.general->size);
323 memcpy(t->term->u.general->buf, q->u.t.term,
324 t->term->u.general->len);
325 t->num_attributes = 0;
326 t->attributeList = odr_malloc(o, sizeof(Z_AttributeElement*) * 100);
327 for (i = q->u.t.attr_list; i && t->num_attributes < 100;
330 Z_AttributeElement *a;
332 t->attributeList[t->num_attributes++] = a =
333 odr_malloc(o, sizeof(*a));
334 a->attributeType = odr_malloc(o, sizeof(int));
335 *a->attributeType = i->type;
338 a->which = Z_AttributeValue_numeric;
339 a->value.numeric = odr_malloc(o, sizeof(*a->value.numeric));
340 *a->value.numeric = i->value;
342 a->attributeValue = odr_malloc(o, sizeof(*a->attributeValue));
343 *a->attributeValue = i->value;
348 r->which = Z_RPNStructure_simple;
349 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
350 r->u.simple->which = Z_Operand_resultSetId;
351 r->u.simple->u.resultSetId = odr_malloc(o, len =
352 strlen(q->u.setname) + 1);
353 memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
355 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
356 r->which = Z_RPNStructure_complex;
357 r->u.complex = odr_malloc(o, sizeof(Z_Complex));
358 if (!(r->u.complex->s1 = rpn2rpn(o, q->u.p[0])) ||
359 !(r->u.complex->s2 = rpn2rpn(o, q->u.p[1])))
361 r->u.complex->operator = odr_malloc(o, sizeof(Z_Operator));
362 r->u.complex->operator->which = op[q->kind];
363 r->u.complex->operator->u.and = "";
366 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Bad operator in RPN");
371 const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
373 static struct zass_searchent r;
375 Z_SearchResponse *res;
377 if (!(apdu = get_apdu(a, complete)))
379 if (apdu->which != Z_APDU_searchResponse)
381 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected searchresponse, got #%d",
385 res = apdu->u.searchResponse;
386 r.status = *res->searchStatus;
387 r.num = *res->resultCount;
388 r.status = res->resultSetStatus ? *res->resultSetStatus : 0;
393 if (res->records->which != Z_Records_NSD)
394 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unexpected record types in SchR");
398 Z_DiagRec *dr = res->records->u.nonSurrogateDiagnostic;
401 if (dr->which != Z_DiagRec_defaultFormat ||
402 !(id = oid_getentbyoid
403 (dr->u.defaultFormat->diagnosticSetId)) ||
404 id->class != CLASS_DIAGSET ||
405 id->value != VAL_BIB1)
406 gw_log(GW_LOG_WARN, ZASS_TYPE,
407 "Missing or unknown diagset - ignoring error!");
410 r.errcode = *dr->u.defaultFormat->condition;
411 if (dr->u.defaultFormat->addinfo)
413 strncpy(r.errstring, dr->u.defaultFormat->addinfo, 512);
414 r.errstring[511] = '\0';
418 if (!(id = oid_getentbyoid(dr->diagnosticSetId)) ||
419 id->class != CLASS_DIAGSET || id->value != VAL_BIB1)
420 gw_log(GW_LOG_WARN, ZASS_TYPE,
421 "Missing or unknown diagset - ignoring error!");
424 r.errcode = *dr->condition;
427 strncpy(r.errstring, dr->addinfo, 512);
428 r.errstring[511] = '\0';
437 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
438 char *resname, char *databases, int *complete)
443 Z_SearchRequest *req;
445 int smallSetUpperBound = 0;
446 int largeSetLowerBound = 1;
447 int mediumSetPresentNumber = 0;
448 int replaceIndicator = 1;
451 apdu = zget_APDU (a->encode, Z_APDU_searchRequest);
452 req = apdu->u.searchRequest;
454 req->smallSetUpperBound = &smallSetUpperBound;
455 req->largeSetLowerBound = &largeSetLowerBound;
456 req->mediumSetPresentNumber = &mediumSetPresentNumber;
457 req->replaceIndicator = &replaceIndicator;
458 req->resultSetName = resname;
459 req->num_databaseNames = 0;
460 req->databaseNames = datab;
461 while (*databases && req->num_databaseNames < 100)
466 while (*p && !isspace(*p))
475 req->databaseNames[req->num_databaseNames] = odr_malloc(a->encode,
476 (p - databases) + 1);
477 strcpy(req->databaseNames[req->num_databaseNames++], databases);
479 databases = p + more;
482 q.which = Z_Query_type_1;
484 bib1.proto = PROTO_Z3950;
485 bib1.class = CLASS_ATTSET;
486 bib1.value = VAL_BIB1;
487 rpnq.attributeSetId = oid_getoidbyent(&bib1);
491 if (!(rpnq.RPNStructure = rpn2rpn(a->encode, query)))
493 if (send_apdu(a, apdu) < 0)
501 return zass_searchresult(a, complete);
505 * Triple indirection - that's kinda heavy. We'll fix it later.
506 * There are worse things around, though. Like ZDist.
508 void get_diagrec(zass_record ***p, Z_DiagRec *r)
510 **p = malloc(sizeof(***p));
513 (**p)->errstring[0] = '\0';
514 if (r->which == Z_DiagRec_defaultFormat)
516 (**p)->errcode = *r->u.defaultFormat->condition;
517 if (r->u.defaultFormat->addinfo)
519 strncpy((**p)->errstring, r->u.defaultFormat->addinfo, 200);
520 (**p)->errstring[200] = 0;
526 (**p)->errcode = *r->condition;
529 strncpy((**p)->errstring, r->addinfo, 200);
530 (**p)->errstring[200] = 0;
533 (**p)->errstring[0] = '\0';
535 (**p)->which = ZASS_REC_DIAG;
539 void get_responserecords(zass_record ***p, Z_NamePlusRecordList *recs)
543 for (i = 0; i < recs->num_records; i++)
545 Z_NamePlusRecord *record;
547 record = recs->records[i];
548 if (record->which == Z_NamePlusRecord_surrogateDiagnostic)
549 get_diagrec(p, record->u.surrogateDiagnostic);
552 Z_DatabaseRecord *r = record->u.databaseRecord;
555 **p = malloc(sizeof(***p));
558 if (!(recform = oid_getentbyoid(r->direct_reference)) ||
559 recform->class != CLASS_RECSYN)
561 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown or bad record syntax");
562 (**p)->which = ZASS_REC_UNKNOWN;
565 switch (recform->value)
567 case VAL_USMARC: (**p)->which = ZASS_REC_USMARC; break;
569 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown recsyn");
570 (**p)->which = ZASS_REC_UNKNOWN;
572 if (r->which != ODR_EXTERNAL_octet)
574 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
579 if (!((**p)->record = malloc(r->u.octet_aligned->len + 1)))
581 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
584 memcpy((**p)->record, r->u.octet_aligned->buf,
585 r->u.octet_aligned->len);
586 (**p)->record[r->u.octet_aligned->len] = '\0';
587 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
588 r->u.octet_aligned->len);
595 static void zass_records_free(zass_record *p)
599 static int send_present(ZASS a, char *name, int start, int num,
603 Z_PresentRequest *req;
608 apdu = zget_APDU (a->encode, Z_APDU_presentRequest);
609 req = apdu->u.presentRequest;
611 req->resultSetId = name;
612 req->resultSetStartPoint = &start;
613 req->numberOfRecordsRequested = #
615 recsyn.proto = PROTO_Z3950;
616 recsyn.class = CLASS_RECSYN;
618 req->preferredRecordSyntax = oid_getoidbyent(&recsyn);
620 req->preferredRecordSyntax = 0;
622 return send_apdu(a, apdu);
626 * Note that 1== first record.
627 * TODO: make this baby operate in nonblocking mode, too.
629 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
630 int num, int *complete)
632 static struct zass_presentent r;
633 zass_record **rec = &r.records;
640 zass_records_free(r.records);
646 Z_PresentResponse *res;
648 gw_log(ZASS_DEBUG, ZASS_TYPE,
649 "Fetching %d records from # %d", num - r.num, start);
650 if (send_present(a, resname, start, num - r.num, VAL_USMARC) < 0)
652 if (!(apdu = get_apdu(a, complete)))
658 if (apdu->which != Z_APDU_presentResponse)
660 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse, got #%d",
664 res = apdu->u.presentResponse;
665 r.presentstatus = *res->presentStatus;
666 r.num += *res->numberOfRecordsReturned;
667 if (*res->numberOfRecordsReturned == 0)
669 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target");
672 r.nextpos = *res->nextResultSetPosition;
674 switch (res->records->which)
676 case Z_Records_DBOSD:
677 get_responserecords(&rec,
678 res->records->u.databaseOrSurDiagnostics);
681 get_diagrec(&rec, res->records->u.nonSurrogateDiagnostic);
684 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
688 while (num - r.num && start);
692 const struct zass_presentent *zass_presentresult(ZASS a, int *complete)