5 * Revision 1.9 1995/02/17 15:17:51 quinn
8 * Revision 1.8 1995/02/17 14:48:41 quinn
9 * 'nother bug in present
11 * Revision 1.7 1995/02/17 14:42:21 quinn
12 * Trivial bug in fetch-loop.
14 * Revision 1.6 1995/02/17 14:41:22 quinn
17 * Revision 1.5 1995/02/17 13:58:01 quinn
18 * First kick at present handling
20 * Revision 1.4 1995/02/16 15:33:45 quinn
21 * Fixed bug in KWAQS generator
23 * Revision 1.3 1995/02/16 15:20:45 quinn
24 * Added initialization of response from search
26 * Revision 1.2 1995/02/16 15:14:53 quinn
27 * Fixed KWAQS-generator
29 * Revision 1.1 1995/02/16 14:47:55 quinn
35 * Interface to the Z39.50 toolkit.
50 struct zass /* Z-assoc */
52 NETBOXPROFILE *ass; /* ZDIST association handle */
53 int fd; /* low-level socket (for select only) */
55 int preferredmessagesize;
56 char *buf; /* intermediary buffer */
59 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
61 struct ccl_rpn_attr *i;
62 static char *ops[] = {"and", "or", "not"};
68 strcpy(*p, q->u.t.term);
69 (*p) += strlen(q->u.t.term);
74 for (i = q->u.t.attr_list; i; i = i->next)
76 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
85 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
87 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
88 strcpy(*p, ops[q->kind]);
89 *p += strlen(ops[q->kind]);
92 if (rpn2kwaqs(q->u.p[0], p) < 0)
96 if (rpn2kwaqs(q->u.p[1], p) < 0)
102 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
107 ZASS zass_open(char *host, int port)
114 if (!(p = malloc(sizeof(*p))))
116 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
119 p->maxrecordsize = ZASS_MAXRECORDSIZE;
120 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
121 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
123 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
126 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
128 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
131 p->ass->TimeOutSec = 120;
132 p->ass->TimeOutUSec = 0;
133 strcpy(p->ass->HostName, host);
136 if (netbox_Open(p->ass) != 1)
138 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
141 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
143 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
144 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
147 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
150 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
154 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
157 InitRequest_Destroy(ireq);
158 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
160 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
163 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
164 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
165 p->maxrecordsize)) <= 0)
167 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
170 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
171 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
173 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
176 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
177 if (!InitResponse_GetResult(ires))
179 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
182 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
183 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
184 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
185 InitResponse_Destroy(ires);
189 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
190 char *resname, char *databases)
192 static struct zass_searchent r;
194 DATA_DIR *pdu, *record;
198 if (rpn2kwaqs(query, &p) < 0)
200 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
203 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
204 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
205 0, 0, 0, kwaqs, BIB1_OID);
208 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
211 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
215 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
218 SearchRequest_Destroy(pdu);
219 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
221 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
224 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
225 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
226 a->maxrecordsize)) <= 0)
228 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
231 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
232 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
234 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
237 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
238 r.status = SearchResponse_GetSearchStatus(pdu);
239 r.num = SearchResponse_GetResultCount(pdu);
240 r.status = SearchResponse_GetResultSetStatus(pdu);
243 if ((record = SearchResponse_GetRecords(pdu)))
245 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
249 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
250 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
254 if ((s = OctetString_GetASCIIString(ad)))
256 strcpy(r.errstring, s);
262 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
264 SearchResponse_Destroy(pdu);
270 * Triple indirection - that's kinda heavy. We'll fix it later.
271 * There are worse things around, though. Like ZDist.
273 void get_diagrec(zass_record ***p, DATA_DIR *rec)
277 **p = malloc(sizeof(***p));
279 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
280 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
284 if ((s = OctetString_GetASCIIString(ad)))
286 strcpy((**p)->errstring, s);
290 (**p)->which = ZASS_REC_DIAG;
294 void get_responserecords(zass_record ***p, DATA_DIR *rec)
296 int num, recsyntaxlen, i;
297 DATA_DIR *record, *retrec, *align;
299 POBJECTIDENTIFIER oid;
302 num = ResponseRecords_GetCount(rec);
303 for (i = 1; i <= num; i++)
305 record = ResponseRecords_GetRecord(rec, i);
308 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
311 retrec = NamePlusRecord_GetRetrievalRecord(record);
314 /* check if it's a diagrec */
315 if (record->ptr.child->fldid == 2)
316 get_diagrec(p, record->ptr.child);
319 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
323 ext = RetrievalRecord_GetExternal(retrec);
326 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
329 oid = External_GetDirectReference(ext);
332 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
335 recsyntaxlen = DirectReference_GetLength(oid);
336 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
337 recsyntax[recsyntaxlen] = '\0';
338 **p = malloc(sizeof(***p));
340 if (!strcmp(recsyntax, USMARC_OID))
341 (**p)->which = ZASS_REC_USMARC;
344 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
347 align = External_GetEncodingAligned(ext);
350 gw_log(GW_LOG_WARN, ZASS_TYPE, "AAAARRRGH!! Enough of these log-messages!!!");
353 if (!((**p)->record = malloc(align->count + 1)))
355 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
358 memcpy((**p)->record, align->ptr.data, align->count);
359 (**p)->record[align->count] = '\0';
360 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
367 static void zass_records_free(zass_record *p)
372 * Note that 1== first record.
374 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
377 static struct zass_presentent r = {0, 0, 0, 0};
378 zass_record **rec = &r.records;
385 zass_records_free(r.records);
390 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
392 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
396 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
399 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
403 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
406 PresentRequest_Destroy(pdu);
407 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
409 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
412 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
413 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
414 a->maxrecordsize)) <= 0)
416 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
419 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
420 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
422 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
425 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
426 r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
429 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
432 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
435 switch(PresentResponse_GetRecordType(pdu))
437 case RESPONSERECORDS_TAG:
438 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
440 case NONSURROGATEDIAGNOSTIC_TAG:
441 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
444 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
446 PresentResponse_Destroy(pdu);