4 * Z39.50 API for the Email gateway - YAZ version
6 * $Log: zaccess-yaz.c,v $
7 * Revision 1.5 1995/04/20 15:25:32 quinn
10 * Revision 1.4 1995/04/19 16:02:28 adam
13 * Revision 1.3 1995/04/19 12:55:15 quinn
16 * Revision 1.2 1995/04/19 09:24:02 quinn
17 * Fixed bug in zass_open - variable initialized after use
19 * Revision 1.1 1995/04/17 11:26:53 quinn
20 * Added YAZ version of zaccess
25 * Interface to the YAZ Z39.50 toolkit.
44 struct zass /* Z-assoc */
46 COMSTACK ass; /* comstack association handle */
49 int fd; /* low-level socket (for select only) */
52 int preferredmessagesize;
57 static Z_APDU *get_apdu(struct zass *z, int *complete)
62 if ((res = cs_get(z->ass, &z->inbuf, &z->inbuflen)) <= 0)
64 gw_log(GW_LOG_WARN, ZASS_TYPE, "cs_get failed");
76 odr_setbuf(z->decode, z->inbuf, res);
77 if (!z_APDU(z->decode, &ap, 0))
79 gw_log(GW_LOG_WARN, ZASS_TYPE, "decode: %s",
80 odr_errlist[odr_geterror(z->decode)]);
90 static int send_apdu(struct zass *z, Z_APDU *a)
95 if (!z_APDU(z->encode, &a, 0))
97 gw_log(GW_LOG_FATAL, ZASS_TYPE, "encoding initreq");
100 buf = odr_getbuf(z->encode, &len);
101 if (cs_put(z->ass, buf, len) < 0)
103 gw_log(GW_LOG_FATAL, ZASS_TYPE, "cs_put");
106 odr_reset(z->encode); /* release odr_allocated structures */
110 static int send_initreq(struct zass *p, char *auth)
114 Odr_bitmask options, protocolVersion;
116 Z_IdAuthentication idauth;
118 a.which = Z_APDU_initRequest;
119 a.u.initRequest = &init;
120 init.referenceId = 0;
121 init.options = &options;
122 ODR_MASK_ZERO(&options);
123 ODR_MASK_SET(&options, Z_Options_search);
124 ODR_MASK_SET(&options, Z_Options_present);
125 ODR_MASK_SET(&options, Z_Options_delSet);
126 init.protocolVersion = &protocolVersion;
127 ODR_MASK_ZERO(&protocolVersion);
128 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
129 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
130 init.preferredMessageSize = &p->preferredmessagesize;
131 init.maximumRecordSize = &p->maxrecordsize;
135 init.idAuthentication = &idauth;
136 idauth.which = Z_IdAuthentication_open;
137 idauth.u.open = auth;
140 init.idAuthentication = 0;
141 init.idAuthentication = 0;
142 init.implementationId = ZASS_ID;
143 sprintf(name, "%s (YAZ protocol layer)", ZASS_NAME);
144 init.implementationName = name;
145 init.implementationVersion = ZASS_VERSION;
146 init.userInformationField = 0;
147 if (send_apdu(p, &a) < 0)
152 int zass_fileno(ZASS a)
157 int zass_openresult(ZASS p, int *complete)
162 if (!(ap = get_apdu(p, complete)))
164 if (ap->which != Z_APDU_initResponse)
166 gw_log(GW_LOG_WARN, ZASS_TYPE, "bad APDU");
169 res = ap->u.initResponse;
172 gw_log(GW_LOG_WARN, ZASS_TYPE, "Negative result on initres");
175 p->preferredmessagesize = *res->preferredMessageSize;
176 p->maxrecordsize = *res->maximumRecordSize;
177 if (res->implementationId)
178 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
179 res->implementationId);
180 if (res->implementationName)
181 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
182 res->implementationName);
183 if (res->implementationVersion)
184 gw_log(GW_LOG_NOTICE, ZASS_TYPE, "imp. ID : %s",
185 res->implementationVersion);
186 gw_log(ZASS_DEBUG, ZASS_TYPE, "Initialized ok");
190 ZASS zass_open(char *host, int port, int *complete, char *auth)
196 if (!(p = malloc(sizeof(*p))))
198 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "malloc");
208 if (!(p->encode = odr_createmem(ODR_ENCODE)) ||
209 !(p->decode = odr_createmem(ODR_DECODE)))
211 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "odr_createmem");
214 p->maxrecordsize = ZASS_MAXRECORDSIZE;
215 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
216 if (!(p->ass = cs_create(tcpip_type, 1, CS_Z3950)))
218 gw_log(GW_LOG_FATAL|GW_LOG_ERRNO, ZASS_TYPE, "cs_create");
223 sprintf(addstr, "%s:%d", host, port);
224 if (!(address = tcpip_strtoaddr(addstr)))
226 gw_log(GW_LOG_FATAL, ZASS_TYPE, "failed to resolve %s", addstr);
229 p->fd = cs_fileno(p->ass);
230 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connecting to %s", addstr);
231 if (cs_connect(p->ass, address) < 0)
233 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to connect to %s", addstr);
236 gw_log(ZASS_DEBUG, ZASS_TYPE, "connected ok... initializing");
237 if (send_initreq(p, auth) < 0)
239 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send init");
247 if (zass_openresult(p, complete) < 0)
249 return p; /* all done */
253 * Convert the egate (ccl2rpn) version of RPN to YAZ RPN.
255 static Z_RPNStructure *rpn2rpn(ODR o, struct ccl_rpn_node *q)
257 Z_RPNStructure *r = odr_malloc(o, sizeof(*r));
258 Z_AttributesPlusTerm *t;
259 struct ccl_rpn_attr *i;
261 static int op[] = { Z_Operator_and, Z_Operator_or, Z_Operator_and_not };
266 r->which = Z_RPNStructure_simple;
267 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
268 r->u.simple->which = Z_Operand_APT;
269 r->u.simple->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
270 t->term = odr_malloc(o, sizeof(Z_Term));
271 t->term->which = Z_Term_general;
272 t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
273 t->term->u.general->len = t->term->u.general->size =
275 t->term->u.general->buf = odr_malloc(o, t->term->u.general->size);
276 memcpy(t->term->u.general->buf, q->u.t.term,
277 t->term->u.general->len);
278 t->num_attributes = 0;
279 t->attributeList = odr_malloc(o, sizeof(Z_AttributeElement*) * 100);
280 for (i = q->u.t.attr_list; i && t->num_attributes < 100;
283 Z_AttributeElement *a;
285 t->attributeList[t->num_attributes++] = a =
286 odr_malloc(o, sizeof(*a));
287 a->attributeType = odr_malloc(o, sizeof(int));
288 *a->attributeType = i->type;
289 a->attributeValue = odr_malloc(o, sizeof(*a));
290 *a->attributeValue = i->value;
294 r->which = Z_RPNStructure_simple;
295 r->u.simple = odr_malloc(o, sizeof(Z_Operand));
296 r->u.simple->which = Z_Operand_resultSetId;
297 r->u.simple->u.resultSetId = odr_malloc(o, len =
298 strlen(q->u.setname));
299 memcpy(r->u.simple->u.resultSetId, q->u.setname, len);
301 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
302 r->which = Z_RPNStructure_complex;
303 r->u.complex = odr_malloc(o, sizeof(Z_Complex));
304 if (!(r->u.complex->s1 = rpn2rpn(o, q->u.p[0])) ||
305 !(r->u.complex->s2 = rpn2rpn(o, q->u.p[1])))
307 r->u.complex->operator = odr_malloc(o, sizeof(Z_Operator));
308 r->u.complex->operator->which = op[q->kind];
309 r->u.complex->operator->u.and = "";
312 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Bad operator in RPN");
317 const struct zass_searchent *zass_searchresult(ZASS a, int *complete)
319 static struct zass_searchent r;
321 Z_SearchResponse *res;
323 if (!(apdu = get_apdu(a, complete)))
325 if (apdu->which != Z_APDU_searchResponse)
327 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected searchresponse, got #%d",
331 res = apdu->u.searchResponse;
332 r.status = *res->searchStatus;
333 r.num = *res->resultCount;
334 r.status = res->resultSetStatus ? *res->resultSetStatus : 0;
339 if (res->records->which != Z_Records_NSD)
340 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unexpected record types in SchR");
344 Z_DiagRec *dr = res->records->u.nonSurrogateDiagnostic;
346 if (!(id = oid_getentbyoid(dr->diagnosticSetId)) ||
347 id->class != CLASS_DIAGSET || id->value != VAL_BIB1)
348 gw_log(GW_LOG_WARN, ZASS_TYPE,
349 "Missing or unknown diagset - ignoring error!");
352 r.errcode = *dr->condition;
355 strncpy(r.errstring, dr->addinfo, 512);
356 r.errstring[511] = '\0';
364 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
365 char *resname, char *databases, int *complete)
372 int smallSetUpperBound = 0;
373 int largeSetLowerBound = 1;
374 int mediumSetPresentNumber = 0;
375 int replaceIndicator = 1;
378 apdu.which = Z_APDU_searchRequest;
379 apdu.u.searchRequest = &req;
381 req.smallSetUpperBound = &smallSetUpperBound;
382 req.largeSetLowerBound = &largeSetLowerBound;
383 req.mediumSetPresentNumber = &mediumSetPresentNumber;
384 req.replaceIndicator = &replaceIndicator;
385 req.resultSetName = resname;
386 req.num_databaseNames = 0;
387 req.databaseNames = datab;
388 while (*databases && req.num_databaseNames < 100)
393 while (*p && !isspace(*p))
402 req.databaseNames[req.num_databaseNames] = odr_malloc(a->encode,
403 (p - databases) + 1);
404 strcpy(req.databaseNames[req.num_databaseNames++], databases);
406 databases = p + more;
408 req.smallSetElementSetNames = 0;
409 req.mediumSetElementSetNames = 0;
410 req.preferredRecordSyntax = 0;
412 q.which = Z_Query_type_1;
414 bib1.proto = PROTO_Z3950;
415 bib1.class = CLASS_ATTSET;
416 bib1.value = VAL_BIB1;
417 rpnq.attributeSetId = oid_getoidbyent(&bib1);
421 if (!(rpnq.RPNStructure = rpn2rpn(a->encode, query)))
423 if (send_apdu(a, &apdu) < 0)
431 return zass_searchresult(a, complete);
435 * Triple indirection - that's kinda heavy. We'll fix it later.
436 * There are worse things around, though. Like ZDist.
438 void get_diagrec(zass_record ***p, Z_DiagRec *r)
440 **p = malloc(sizeof(***p));
442 (**p)->errcode = *r->condition;
445 strncpy((**p)->errstring, r->addinfo, 200);
446 (**p)->errstring[200] = 0;
449 (**p)->errstring[0] = '\0';
450 (**p)->which = ZASS_REC_DIAG;
454 void get_responserecords(zass_record ***p, Z_NamePlusRecordList *recs)
458 for (i = 0; i < recs->num_records; i++)
460 Z_NamePlusRecord *record;
462 record = recs->records[i];
463 if (record->which == Z_NamePlusRecord_surrogateDiagnostic)
464 get_diagrec(p, record->u.surrogateDiagnostic);
467 Z_DatabaseRecord *r = record->u.databaseRecord;
470 **p = malloc(sizeof(***p));
473 if (!(recform = oid_getentbyoid(r->direct_reference)) ||
474 recform->class != CLASS_RECSYN)
476 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown or bad record syntax");
477 (**p)->which = ZASS_REC_UNKNOWN;
480 switch (recform->value)
482 case VAL_USMARC: (**p)->which = ZASS_REC_USMARC; break;
484 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown recsyn");
485 (**p)->which = ZASS_REC_UNKNOWN;
487 if (r->which != ODR_EXTERNAL_octet)
489 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
494 if (!((**p)->record = malloc(r->u.octet_aligned->len + 1)))
496 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
499 memcpy((**p)->record, r->u.octet_aligned->buf,
500 r->u.octet_aligned->len);
501 (**p)->record[r->u.octet_aligned->len] = '\0';
502 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
503 r->u.octet_aligned->len);
510 static void zass_records_free(zass_record *p)
514 static int send_present(ZASS a, char *name, int start, int num,
518 Z_PresentRequest req;
523 apdu.which = Z_APDU_presentRequest;
524 apdu.u.presentRequest = &req;
526 req.resultSetId = name;
527 req.resultSetStartPoint = &start;
528 req.numberOfRecordsRequested = #
529 req.elementSetNames = 0;
531 recsyn.proto = PROTO_Z3950;
532 recsyn.class = CLASS_RECSYN;
534 req.preferredRecordSyntax = oid_getoidbyent(&recsyn);
536 req.preferredRecordSyntax = 0;
538 return send_apdu(a, &apdu);
542 * Note that 1== first record.
543 * TODO: make this baby operate in nonblocking mode, too.
545 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
546 int num, int *complete)
548 static struct zass_presentent r;
549 zass_record **rec = &r.records;
556 zass_records_free(r.records);
562 Z_PresentResponse *res;
564 gw_log(ZASS_DEBUG, ZASS_TYPE,
565 "Fetching %d records from # %d", num - r.num, start);
566 if (send_present(a, resname, start, num - r.num, VAL_USMARC) < 0)
568 if (!(apdu = get_apdu(a, complete)))
574 if (apdu->which != Z_APDU_presentResponse)
576 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse, got #%d",
580 res = apdu->u.presentResponse;
581 r.presentstatus = *res->presentStatus;
582 r.num += *res->numberOfRecordsReturned;
583 if (*res->numberOfRecordsReturned == 0)
585 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target");
588 r.nextpos = *res->nextResultSetPosition;
590 switch (res->records->which)
592 case Z_Records_DBOSD:
593 get_responserecords(&rec,
594 res->records->u.databaseOrSurDiagnostics);
597 get_diagrec(&rec, res->records->u.nonSurrogateDiagnostic);
600 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
604 while (num - r.num && start);
608 const struct zass_presentent *zass_presentresult(ZASS a, int *complete)