5 * Revision 1.8 1995/02/17 14:48:41 quinn
6 * 'nother bug in present
8 * Revision 1.7 1995/02/17 14:42:21 quinn
9 * Trivial bug in fetch-loop.
11 * Revision 1.6 1995/02/17 14:41:22 quinn
14 * Revision 1.5 1995/02/17 13:58:01 quinn
15 * First kick at present handling
17 * Revision 1.4 1995/02/16 15:33:45 quinn
18 * Fixed bug in KWAQS generator
20 * Revision 1.3 1995/02/16 15:20:45 quinn
21 * Added initialization of response from search
23 * Revision 1.2 1995/02/16 15:14:53 quinn
24 * Fixed KWAQS-generator
26 * Revision 1.1 1995/02/16 14:47:55 quinn
32 * Interface to the Z39.50 toolkit.
47 struct zass /* Z-assoc */
49 NETBOXPROFILE *ass; /* ZDIST association handle */
50 int fd; /* low-level socket (for select only) */
52 int preferredmessagesize;
53 char *buf; /* intermediary buffer */
56 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
58 struct ccl_rpn_attr *i;
59 static char *ops[] = {"and", "or", "not"};
65 strcpy(*p, q->u.t.term);
66 (*p) += strlen(q->u.t.term);
71 for (i = q->u.t.attr_list; i; i = i->next)
73 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
82 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
84 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
85 strcpy(*p, ops[q->kind]);
86 *p += strlen(ops[q->kind]);
89 if (rpn2kwaqs(q->u.p[0], p) < 0)
93 if (rpn2kwaqs(q->u.p[1], p) < 0)
99 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
104 ZASS zass_open(char *host, int port)
111 if (!(p = malloc(sizeof(*p))))
113 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
116 p->maxrecordsize = ZASS_MAXRECORDSIZE;
117 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
118 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
120 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
123 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
125 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
128 p->ass->TimeOutSec = 120;
129 p->ass->TimeOutUSec = 0;
130 strcpy(p->ass->HostName, host);
133 if (netbox_Open(p->ass) != 1)
135 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
138 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
140 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
141 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
144 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
147 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
151 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
154 InitRequest_Destroy(ireq);
155 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
157 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
160 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
161 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
162 p->maxrecordsize)) <= 0)
164 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
167 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
168 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
170 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
173 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
174 if (!InitResponse_GetResult(ires))
176 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
179 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
180 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
181 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
182 InitResponse_Destroy(ires);
186 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
187 char *resname, char *databases)
189 static struct zass_searchent r;
191 DATA_DIR *pdu, *record;
195 if (rpn2kwaqs(query, &p) < 0)
197 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
200 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
201 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
202 0, 0, 0, kwaqs, BIB1_OID);
205 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
208 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
212 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
215 SearchRequest_Destroy(pdu);
216 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
218 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
221 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
222 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
223 a->maxrecordsize)) <= 0)
225 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
228 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
229 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
231 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
234 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
235 r.status = SearchResponse_GetSearchStatus(pdu);
236 r.num = SearchResponse_GetResultCount(pdu);
237 r.status = SearchResponse_GetResultSetStatus(pdu);
240 if ((record = SearchResponse_GetRecords(pdu)))
242 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
246 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
247 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
251 if ((s = OctetString_GetASCIIString(ad)))
253 strcpy(r.errstring, s);
259 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
261 SearchResponse_Destroy(pdu);
267 * Triple indirection - that's kinda heavy. We'll fix it later.
268 * There are worse things around, though. Like ZDist.
270 void get_diagrec(zass_record ***p, DATA_DIR *rec)
274 **p = malloc(sizeof(***p));
276 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
277 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
281 if ((s = OctetString_GetASCIIString(ad)))
283 strcpy((**p)->errstring, s);
287 (**p)->which = ZASS_REC_DIAG;
291 void get_responserecords(zass_record ***p, DATA_DIR *rec)
293 int num, recsyntaxlen, i;
294 DATA_DIR *record, *retrec, *align;
296 POBJECTIDENTIFIER oid;
299 num = ResponseRecords_GetCount(rec);
300 for (i = 1; i <= num; i++)
302 record = ResponseRecords_GetRecord(rec, i);
305 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
308 retrec = NamePlusRecord_GetRetrievalRecord(record);
311 /* check if it's a diagrec */
312 if (record->ptr.child->fldid == 2)
313 get_diagrec(p, record->ptr.child);
316 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
320 ext = RetrievalRecord_GetExternal(retrec);
323 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
326 oid = External_GetDirectReference(ext);
329 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
332 recsyntaxlen = DirectReference_GetLength(oid);
333 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
334 recsyntax[recsyntaxlen] = '\0';
335 **p = malloc(sizeof(***p));
337 if (!strcmp(recsyntax, USMARC_OID))
338 (**p)->which = ZASS_REC_USMARC;
341 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
344 align = External_GetEncodingAligned(ext);
347 gw_log(GW_LOG_WARN, ZASS_TYPE, "AAAARRRGH!! Enough of these log-messages!!!");
350 if (!((**p)->record = malloc(align->count + 1)))
352 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
355 memcpy((**p)->record, align->ptr.data, align->count);
356 (**p)->record[align->count] = '\0';
357 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
364 static void zass_records_free(zass_record *p)
369 * Note that 1== first record.
371 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
374 static struct zass_presentent r = {0, 0, 0, 0};
375 zass_record **rec = &r.records;
382 zass_records_free(r.records);
387 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num,
389 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num, "F",
393 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
396 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
400 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
403 PresentRequest_Destroy(pdu);
404 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
406 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
409 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
410 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
411 a->maxrecordsize)) <= 0)
413 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
416 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
417 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
419 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
422 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
423 r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
426 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
429 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
432 switch(PresentResponse_GetRecordType(pdu))
434 case RESPONSERECORDS_TAG:
435 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
437 case NONSURROGATEDIAGNOSTIC_TAG:
438 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
441 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
443 PresentResponse_Destroy(pdu);