5 * Revision 1.7 1995/02/17 14:42:21 quinn
6 * Trivial bug in fetch-loop.
8 * Revision 1.6 1995/02/17 14:41:22 quinn
11 * Revision 1.5 1995/02/17 13:58:01 quinn
12 * First kick at present handling
14 * Revision 1.4 1995/02/16 15:33:45 quinn
15 * Fixed bug in KWAQS generator
17 * Revision 1.3 1995/02/16 15:20:45 quinn
18 * Added initialization of response from search
20 * Revision 1.2 1995/02/16 15:14:53 quinn
21 * Fixed KWAQS-generator
23 * Revision 1.1 1995/02/16 14:47:55 quinn
29 * Interface to the Z39.50 toolkit.
44 struct zass /* Z-assoc */
46 NETBOXPROFILE *ass; /* ZDIST association handle */
47 int fd; /* low-level socket (for select only) */
49 int preferredmessagesize;
50 char *buf; /* intermediary buffer */
53 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
55 struct ccl_rpn_attr *i;
56 static char *ops[] = {"and", "or", "not"};
62 strcpy(*p, q->u.t.term);
63 (*p) += strlen(q->u.t.term);
68 for (i = q->u.t.attr_list; i; i = i->next)
70 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
79 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
81 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
82 strcpy(*p, ops[q->kind]);
83 *p += strlen(ops[q->kind]);
86 if (rpn2kwaqs(q->u.p[0], p) < 0)
90 if (rpn2kwaqs(q->u.p[1], p) < 0)
96 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
101 ZASS zass_open(char *host, int port)
108 if (!(p = malloc(sizeof(*p))))
110 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
113 p->maxrecordsize = ZASS_MAXRECORDSIZE;
114 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
115 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
117 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
120 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
122 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
125 p->ass->TimeOutSec = 120;
126 p->ass->TimeOutUSec = 0;
127 strcpy(p->ass->HostName, host);
130 if (netbox_Open(p->ass) != 1)
132 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
135 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
137 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
138 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
141 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
144 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
148 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
151 InitRequest_Destroy(ireq);
152 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
154 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
157 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
158 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
159 p->maxrecordsize)) <= 0)
161 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
164 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
165 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
167 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
170 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
171 if (!InitResponse_GetResult(ires))
173 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
176 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
177 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
178 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
179 InitResponse_Destroy(ires);
183 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
184 char *resname, char *databases)
186 static struct zass_searchent r;
188 DATA_DIR *pdu, *record;
192 if (rpn2kwaqs(query, &p) < 0)
194 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
197 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
198 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
199 0, 0, 0, kwaqs, BIB1_OID);
202 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
205 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
209 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
212 SearchRequest_Destroy(pdu);
213 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
215 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
218 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
219 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
220 a->maxrecordsize)) <= 0)
222 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
225 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
226 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
228 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
231 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
232 r.status = SearchResponse_GetSearchStatus(pdu);
233 r.num = SearchResponse_GetResultCount(pdu);
234 r.status = SearchResponse_GetResultSetStatus(pdu);
237 if ((record = SearchResponse_GetRecords(pdu)))
239 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
243 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
244 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
248 if ((s = OctetString_GetASCIIString(ad)))
250 strcpy(r.errstring, s);
256 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
258 SearchResponse_Destroy(pdu);
264 * Triple indirection - that's kinda heavy. We'll fix it later.
265 * There are worse things around, though. Like ZDist.
267 void get_diagrec(zass_record ***p, DATA_DIR *rec)
271 **p = malloc(sizeof(***p));
273 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
274 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
278 if ((s = OctetString_GetASCIIString(ad)))
280 strcpy((**p)->errstring, s);
284 (**p)->which = ZASS_REC_DIAG;
288 void get_responserecords(zass_record ***p, DATA_DIR *rec)
290 int num, recsyntaxlen, i;
291 DATA_DIR *record, *retrec, *align;
293 POBJECTIDENTIFIER oid;
296 num = ResponseRecords_GetCount(rec);
297 for (i = 1; i <= num; i++)
299 record = ResponseRecords_GetRecord(rec, i);
302 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
305 retrec = NamePlusRecord_GetRetrievalRecord(record);
308 /* check if it's a diagrec */
309 if (record->ptr.child->fldid == 2)
310 get_diagrec(p, record->ptr.child);
313 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
317 ext = RetrievalRecord_GetExternal(retrec);
320 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
323 oid = External_GetDirectReference(ext);
326 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
329 recsyntaxlen = DirectReference_GetLength(oid);
330 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
331 recsyntax[recsyntaxlen] = '\0';
332 **p = malloc(sizeof(***p));
334 if (!strcmp(recsyntax, USMARC_OID))
335 (**p)->which = ZASS_REC_USMARC;
338 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
341 align = External_GetEncodingAligned(ext);
344 gw_log(GW_LOG_WARN, ZASS_TYPE, "AAAARRRGH!! Enough of these log-messages!!!");
347 if (!((**p)->record = malloc(align->count + 1)))
349 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
352 memcpy((**p)->record, align->ptr.data, align->count);
353 (**p)->record[align->count] = '\0';
354 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
361 static void zass_records_free(zass_record *p)
366 * Note that 1== first record.
368 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
371 static struct zass_presentent r = {0, 0, 0, 0};
372 zass_record **rec = &r.records;
379 zass_records_free(r.records);
384 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num,
386 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num, "F",
390 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
393 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
397 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
400 PresentRequest_Destroy(pdu);
401 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
403 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
406 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
407 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
408 a->maxrecordsize)) <= 0)
410 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
413 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
414 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
416 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
419 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
420 r.num = PresentResponse_GetNumberOfRecordsReturned(pdu);
423 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
426 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
429 switch(PresentResponse_GetRecordType(pdu))
431 case RESPONSERECORDS_TAG:
432 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
434 case NONSURROGATEDIAGNOSTIC_TAG:
435 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
438 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
441 PresentResponse_Destroy(pdu);