4 * Z39.50 API for the Email gateway - YAZ version
6 * $Log: zaccess-yaz.c,v $
7 * Revision 1.7 1995/04/21 16:29:32 quinn
10 * Revision 1.6 1995/04/21 12:58:36 adam
13 * Revision 1.5 1995/04/20 15:25:32 quinn
16 * Revision 1.4 1995/04/19 16:02:28 adam
19 * Revision 1.3 1995/04/19 12:55:15 quinn
22 * Revision 1.2 1995/04/19 09:24:02 quinn
23 * Fixed bug in zass_open - variable initialized after use
25 * Revision 1.1 1995/04/17 11:26:53 quinn
26 * Added YAZ version of zaccess
31 * Interface to the YAZ Z39.50 toolkit.
50 struct zass /* Z-assoc */
52 COMSTACK ass; /* comstack association handle */
55 int fd; /* low-level socket (for select only) */
58 int preferredmessagesize;
63 static Z_APDU *get_apdu(struct zass *z, int *complete)
68 if ((res = cs_get(z->ass, &z->inbuf, &z->inbuflen)) <= 0)
70 gw_log(GW_LOG_WARN, ZASS_TYPE, "cs_get failed");
82 odr_setbuf(z->decode, z->inbuf, res);
83 if (!z_APDU(z->decode, &ap, 0))
85 gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
86 odr_errlist[odr_geterror(z->decode)]);
96 static int send_apdu(struct zass *z, Z_APDU *a)
101 if (!z_APDU(z->encode, &a, 0))
103 gw_log(GW_LOG_FATAL, ZASS_TYPE, "encoding initreq");
106 buf = odr_getbuf(z->encode, &len);
107 if (cs_put(z->ass, buf, len) < 0)
109 gw_log(GW_LOG_FATAL, ZASS_TYPE, "cs_put");
112 odr_reset(z->encode); /* release odr_allocated structures */
116 static int send_initreq(struct zass *p, char *auth)
120 Odr_bitmask options, protocolVersion;
122 Z_IdAuthentication idauth;
124 a.which = Z_APDU_initRequest;
125 a.u.initRequest = &init;
126 init.referenceId = 0;
127 init.options = &options;
128 ODR_MASK_ZERO(&options);
129 ODR_MASK_SET(&options, Z_Options_search);
130 ODR_MASK_SET(&options, Z_Options_present);
131 ODR_MASK_SET(&options, Z_Options_delSet);
132 init.protocolVersion = &protocolVersion;
133 ODR_MASK_ZERO(&protocolVersion);
134 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
135 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
136 init.preferredMessageSize = &p->preferredmessagesize;
137 init.maximumRecordSize = &p->maxrecordsize;
141 init.idAuthentication = &idauth;
142 idauth.which = Z_IdAuthentication_open;
143 idauth.u.open = auth;
146 init.idAuthentication = 0;
147 init.idAuthentication = 0;
148 init.implementationId = ZASS_ID;
149 sprintf(name, "%s (YAZ protocol layer)", ZASS_NAME);
150 init.implementationName = name;
151 init.implementationVersion = ZASS_VERSION;
152 init.userInformationField = 0;
153 if (send_apdu(p, &a) < 0)
158 int zass_fileno(ZASS a)
163 int zass_openresult(ZASS p, int *complete)
168 if (!(ap = get_apdu(p, complete)))
170 if (ap->which != Z_APDU_initResponse)
172 gw_log(GW_LOG_WARN, ZASS_TYPE, "bad APDU");
175 res = ap->u.initResponse;
178 gw_log(GW_LOG_WARN, ZASS_TYPE, "Negative result on initres");
181 p->preferredmessagesize = *res->preferredMessageSize;
182 p->maxrecordsize = *res->maximumRecordSize;
183 if (res->implementationId)
184 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
185 res->implementationId);
186 if (res->implementationName)
187 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
188 res->implementationName);
189 if (res->implementationVersion)
190 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
191 res->implementationVersion);
192 gw_log(ZASS_DEBUG, ZASS_TYPE, "Initialized ok");
196 ZASS zass_open(char *host, int port, int *complete, char *auth)
202 if (!(p = malloc(sizeof(*p))))
204 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
214 if (!(p->encode = odr_createmem(ODR_ENCODE)) ||
215 !(p->decode = odr_createmem(ODR_DECODE)))
217 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "odr_createmem");
220 p->maxrecordsize = ZASS_MAXRECORDSIZE;
221 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
222 if (!(p->ass = cs_create(tcpip_type, 1, CS_Z3950)))
224 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "cs_create");
229 sprintf(addstr, "%s:%d", host, port);
230 if (!(address = tcpip_strtoaddr(addstr)))
232 gw_log(GW_LOG_FATAL, ZASS_TYPE, "failed to resolve %s", addstr);
235 p->fd = cs_fileno(p->ass);
236 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connecting to %s", addstr);
237 if (cs_connect(p->ass, address) < 0)
239 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to connect to %s", addstr);
242 gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok... initializing");
243 if (send_initreq(p, auth) < 0)
245 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send init");
253 if (zass_openresult(p, complete) < 0)
255 return p; /* all done */
259 * Convert the egate (ccl2rpn) version of RPN to YAZ RPN.
261 static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
263 Z_RPNStructure *r = odr_malloc(o, sizeof(*r));
264 Z_AttributesPlusTerm *t;
265 struct ccl_rpn_attr *i;
267 static int op[] = { Z_Operator_and, Z_Operator_or, Z_Operator_and_not };
272 r->which = Z_RPNStructure_simple;
273 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
274 r->u.simple->which = Z_Operand_APT;
275 r->u.simple->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
276 t->term = odr_malloc(o, sizeof(Z_Term));
277 t->term->which = Z_Term_general;
278 t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
279 t->term->u.general->len = t->term->u.general->size =
281 t->term->u.general->buf = odr_malloc(o, t->term->u.general->size);
282 memcpy(t->term->u.general->buf, q->u.t.term,
283 t->term->u.general->len);
284 t->num_attributes = 0;
285 t->attributeList = odr_malloc(o, sizeof(Z_AttributeElement*) * 100);
286 for (i = q->u.t.attr_list; i && t->num_attributes < 100;
289 Z_AttributeElement *a;
291 t->attributeList[t->num_attributes++] = a =
292 odr_malloc(o, sizeof(*a));
293 a->attributeType = odr_malloc(o, sizeof(int));
294 *a->attributeType = i->type;
295 a->attributeValue = odr_malloc(o, sizeof(*a));
296 *a->attributeValue = i->value;
300 r->which = Z_RPNStructure_simple;
301 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
302 r->u.simple->which = Z_Operand_resultSetId;
303 r->u.simple->u.resultSetId = odr_malloc(o, len =
304 strlen(q->u.setname) + 1);
305 memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
307 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
308 r->which = Z_RPNStructure_complex;
309 r->u.complex = odr_malloc(o, sizeof(Z_Complex));
310 if (!(r->u.complex->s1 = rpn2rpn(o, q->u.p[0])) ||
311 !(r->u.complex->s2 = rpn2rpn(o, q->u.p[1])))
313 r->u.complex->operator = odr_malloc(o, sizeof(Z_Operator));
314 r->u.complex->operator->which = op[q->kind];
315 r->u.complex->operator->u.and = "";
318 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Bad operator in RPN");
323 const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
325 static struct zass_searchent r;
327 Z_SearchResponse *res;
329 if (!(apdu = get_apdu(a, complete)))
331 if (apdu->which != Z_APDU_searchResponse)
333 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected searchresponse, got #%d",
337 res = apdu->u.searchResponse;
338 r.status = *res->searchStatus;
339 r.num = *res->resultCount;
340 r.status = res->resultSetStatus ? *res->resultSetStatus : 0;
345 if (res->records->which != Z_Records_NSD)
346 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unexpected record types in SchR");
350 Z_DiagRec *dr = res->records->u.nonSurrogateDiagnostic;
352 if (!(id = oid_getentbyoid(dr->diagnosticSetId)) ||
353 id->class != CLASS_DIAGSET || id->value != VAL_BIB1)
354 gw_log(GW_LOG_WARN, ZASS_TYPE,
355 "Missing or unknown diagset - ignoring error!");
358 r.errcode = *dr->condition;
361 strncpy(r.errstring, dr->addinfo, 512);
362 r.errstring[511] = '\0';
370 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
371 char *resname, char *databases, int *complete)
378 int smallSetUpperBound = 0;
379 int largeSetLowerBound = 1;
380 int mediumSetPresentNumber = 0;
381 int replaceIndicator = 1;
384 apdu.which = Z_APDU_searchRequest;
385 apdu.u.searchRequest = &req;
387 req.smallSetUpperBound = &smallSetUpperBound;
388 req.largeSetLowerBound = &largeSetLowerBound;
389 req.mediumSetPresentNumber = &mediumSetPresentNumber;
390 req.replaceIndicator = &replaceIndicator;
391 req.resultSetName = resname;
392 req.num_databaseNames = 0;
393 req.databaseNames = datab;
394 while (*databases && req.num_databaseNames < 100)
399 while (*p && !isspace(*p))
408 req.databaseNames[req.num_databaseNames] = odr_malloc(a->encode,
409 (p - databases) + 1);
410 strcpy(req.databaseNames[req.num_databaseNames++], databases);
412 databases = p + more;
414 req.smallSetElementSetNames = 0;
415 req.mediumSetElementSetNames = 0;
416 req.preferredRecordSyntax = 0;
418 q.which = Z_Query_type_1;
420 bib1.proto = PROTO_Z3950;
421 bib1.class = CLASS_ATTSET;
422 bib1.value = VAL_BIB1;
423 rpnq.attributeSetId = oid_getoidbyent(&bib1);
427 if (!(rpnq.RPNStructure = rpn2rpn(a->encode, query)))
429 if (send_apdu(a, &apdu) < 0)
437 return zass_searchresult(a, complete);
441 * Triple indirection - that's kinda heavy. We'll fix it later.
442 * There are worse things around, though. Like ZDist.
444 void get_diagrec(zass_record ***p, Z_DiagRec *r)
446 **p = malloc(sizeof(***p));
448 (**p)->errcode = *r->condition;
451 strncpy((**p)->errstring, r->addinfo, 200);
452 (**p)->errstring[200] = 0;
455 (**p)->errstring[0] = '\0';
456 (**p)->which = ZASS_REC_DIAG;
460 void get_responserecords(zass_record ***p, Z_NamePlusRecordList *recs)
464 for (i = 0; i < recs->num_records; i++)
466 Z_NamePlusRecord *record;
468 record = recs->records[i];
469 if (record->which == Z_NamePlusRecord_surrogateDiagnostic)
470 get_diagrec(p, record->u.surrogateDiagnostic);
473 Z_DatabaseRecord *r = record->u.databaseRecord;
476 **p = malloc(sizeof(***p));
479 if (!(recform = oid_getentbyoid(r->direct_reference)) ||
480 recform->class != CLASS_RECSYN)
482 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown or bad record syntax");
483 (**p)->which = ZASS_REC_UNKNOWN;
486 switch (recform->value)
488 case VAL_USMARC: (**p)->which = ZASS_REC_USMARC; break;
490 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown recsyn");
491 (**p)->which = ZASS_REC_UNKNOWN;
493 if (r->which != ODR_EXTERNAL_octet)
495 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
500 if (!((**p)->record = malloc(r->u.octet_aligned->len + 1)))
502 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
505 memcpy((**p)->record, r->u.octet_aligned->buf,
506 r->u.octet_aligned->len);
507 (**p)->record[r->u.octet_aligned->len] = '\0';
508 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
509 r->u.octet_aligned->len);
516 static void zass_records_free(zass_record *p)
520 static int send_present(ZASS a, char *name, int start, int num,
524 Z_PresentRequest req;
529 apdu.which = Z_APDU_presentRequest;
530 apdu.u.presentRequest = &req;
532 req.resultSetId = name;
533 req.resultSetStartPoint = &start;
534 req.numberOfRecordsRequested = #
535 req.elementSetNames = 0;
537 recsyn.proto = PROTO_Z3950;
538 recsyn.class = CLASS_RECSYN;
540 req.preferredRecordSyntax = oid_getoidbyent(&recsyn);
542 req.preferredRecordSyntax = 0;
544 return send_apdu(a, &apdu);
548 * Note that 1== first record.
549 * TODO: make this baby operate in nonblocking mode, too.
551 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
552 int num, int *complete)
554 static struct zass_presentent r;
555 zass_record **rec = &r.records;
562 zass_records_free(r.records);
568 Z_PresentResponse *res;
570 gw_log(ZASS_DEBUG, ZASS_TYPE,
571 "Fetching %d records from # %d", num - r.num, start);
572 if (send_present(a, resname, start, num - r.num, VAL_USMARC) < 0)
574 if (!(apdu = get_apdu(a, complete)))
580 if (apdu->which != Z_APDU_presentResponse)
582 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse, got #%d",
586 res = apdu->u.presentResponse;
587 r.presentstatus = *res->presentStatus;
588 r.num += *res->numberOfRecordsReturned;
589 if (*res->numberOfRecordsReturned == 0)
591 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target");
594 r.nextpos = *res->nextResultSetPosition;
596 switch (res->records->which)
598 case Z_Records_DBOSD:
599 get_responserecords(&rec,
600 res->records->u.databaseOrSurDiagnostics);
603 get_diagrec(&rec, res->records->u.nonSurrogateDiagnostic);
606 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
610 while (num - r.num && start);
614 const struct zass_presentent *zass_presentresult(ZASS a, int *complete)