4 * Z39.50 API for the Email gateway
7 * Revision 1.15 1995/04/17 11:26:55 quinn
8 * Added YAZ version of zaccess
10 * Revision 1.14 1995/02/23 08:32:26 adam
13 * Revision 1.12 1995/02/20 20:35:37 quinn
14 * Pull present status from presresp.
16 * Revision 1.11 1995/02/20 18:58:05 quinn
17 * Added hack for record in ANY
19 * Revision 1.10 1995/02/20 18:19:30 quinn
20 * More relaxed about record types.
22 * Revision 1.9 1995/02/17 15:17:51 quinn
25 * Revision 1.8 1995/02/17 14:48:41 quinn
26 * 'nother bug in present
28 * Revision 1.7 1995/02/17 14:42:21 quinn
29 * Trivial bug in fetch-loop.
31 * Revision 1.6 1995/02/17 14:41:22 quinn
34 * Revision 1.5 1995/02/17 13:58:01 quinn
35 * First kick at present handling
37 * Revision 1.4 1995/02/16 15:33:45 quinn
38 * Fixed bug in KWAQS generator
40 * Revision 1.3 1995/02/16 15:20:45 quinn
41 * Added initialization of response from search
43 * Revision 1.2 1995/02/16 15:14:53 quinn
44 * Fixed KWAQS-generator
46 * Revision 1.1 1995/02/16 14:47:55 quinn
52 * Interface to the Z39.50 toolkit.
67 struct zass /* Z-assoc */
69 NETBOXPROFILE *ass; /* ZDIST association handle */
70 int fd; /* low-level socket (for select only) */
72 int preferredmessagesize;
73 char *buf; /* intermediary buffer */
76 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
78 struct ccl_rpn_attr *i;
79 static char *ops[] = {"and", "or", "not"};
85 strcpy(*p, q->u.t.term);
86 (*p) += strlen(q->u.t.term);
91 for (i = q->u.t.attr_list; i; i = i->next)
93 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
102 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
104 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
105 strcpy(*p, ops[q->kind]);
106 *p += strlen(ops[q->kind]);
109 if (rpn2kwaqs(q->u.p[0], p) < 0)
113 if (rpn2kwaqs(q->u.p[1], p) < 0)
119 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
124 ZASS zass_open(char *host, int port)
132 if (!(p = malloc(sizeof(*p))))
134 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
137 p->maxrecordsize = ZASS_MAXRECORDSIZE;
138 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
139 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
141 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
144 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
146 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
149 p->ass->TimeOutSec = 120;
150 p->ass->TimeOutUSec = 0;
151 strcpy(p->ass->HostName, host);
154 if (netbox_Open(p->ass) != 1)
156 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
159 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
161 sprintf(name, "%s (ZDIST protocol layer)", ZASS_NAME);
162 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
163 p->preferredmessagesize, ZASS_ID, name, ZASS_VERSION, 0);
166 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
169 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
173 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
176 InitRequest_Destroy(ireq);
177 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
179 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
182 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
183 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
184 p->maxrecordsize)) <= 0)
186 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
189 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
190 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
192 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
195 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
196 if (!InitResponse_GetResult(ires))
198 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
201 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
202 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
203 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
204 InitResponse_Destroy(ires);
208 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
209 char *resname, char *databases)
211 static struct zass_searchent r;
213 DATA_DIR *pdu, *record;
217 if (rpn2kwaqs(query, &p) < 0)
219 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
222 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
223 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
224 0, 0, 0, kwaqs, BIB1_OID);
227 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
230 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
234 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
237 SearchRequest_Destroy(pdu);
238 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
240 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
243 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
244 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
245 a->maxrecordsize)) <= 0)
247 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
250 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
251 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
253 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
256 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
257 r.status = SearchResponse_GetSearchStatus(pdu);
258 r.num = SearchResponse_GetResultCount(pdu);
259 r.status = SearchResponse_GetResultSetStatus(pdu);
262 if ((record = SearchResponse_GetRecords(pdu)))
264 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
268 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
269 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
273 if ((s = OctetString_GetASCIIString(ad)))
275 strcpy(r.errstring, s);
281 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
283 SearchResponse_Destroy(pdu);
289 * Triple indirection - that's kinda heavy. We'll fix it later.
290 * There are worse things around, though. Like ZDist.
292 void get_diagrec(zass_record ***p, DATA_DIR *rec)
296 **p = malloc(sizeof(***p));
298 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
299 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
303 if ((s = OctetString_GetASCIIString(ad)))
305 strcpy((**p)->errstring, s);
309 (**p)->which = ZASS_REC_DIAG;
313 void get_responserecords(zass_record ***p, DATA_DIR *rec)
315 int num, recsyntaxlen, i;
316 DATA_DIR *record, *retrec, *align;
318 POBJECTIDENTIFIER oid;
321 num = ResponseRecords_GetCount(rec);
322 for (i = 1; i <= num; i++)
324 record = ResponseRecords_GetRecord(rec, i);
327 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
330 retrec = NamePlusRecord_GetRetrievalRecord(record);
333 /* check if it's a diagrec */
334 if (record->ptr.child->fldid == 2)
335 get_diagrec(p, record->ptr.child);
338 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
342 ext = RetrievalRecord_GetExternal(retrec);
345 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
348 oid = External_GetDirectReference(ext);
351 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
354 recsyntaxlen = DirectReference_GetLength(oid);
355 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
356 recsyntax[recsyntaxlen] = '\0';
357 **p = malloc(sizeof(***p));
359 if (!strcmp(recsyntax, USMARC_OID))
360 (**p)->which = ZASS_REC_USMARC;
363 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
364 gw_log(GW_LOG_WARN, ZASS_TYPE, "Type was '%d'", (**p)->which);
366 align = External_GetEncodingAligned(ext);
369 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
370 align = External_GetEncodingSingle(ext);
373 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't ANY");
376 align = align->ptr.child;
378 if (!((**p)->record = malloc(align->count + 1)))
380 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
383 memcpy((**p)->record, align->ptr.data, align->count);
384 (**p)->record[align->count] = '\0';
385 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
392 static void zass_records_free(zass_record *p)
397 * Note that 1== first record.
399 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
402 static struct zass_presentent r = {0, 0, 0, 0};
403 zass_record **rec = &r.records;
410 zass_records_free(r.records);
415 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
417 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
421 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
424 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
428 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
431 PresentRequest_Destroy(pdu);
432 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
434 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
437 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
438 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
439 a->maxrecordsize)) <= 0)
441 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
444 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
445 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
447 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
450 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
451 r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
452 r.presentstatus = PresentResponse_GetPresentStatus(pdu);
455 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
458 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
460 switch(PresentResponse_GetRecordType(pdu))
462 case RESPONSERECORDS_TAG:
463 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
465 case NONSURROGATEDIAGNOSTIC_TAG:
466 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
469 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
471 PresentResponse_Destroy(pdu);
473 while (num - r.num && start);