5 * Revision 1.12 1995/02/20 20:35:37 quinn
6 * Pull present status from presresp.
8 * Revision 1.11 1995/02/20 18:58:05 quinn
9 * Added hack for record in ANY
11 * Revision 1.10 1995/02/20 18:19:30 quinn
12 * More relaxed about record types.
14 * Revision 1.9 1995/02/17 15:17:51 quinn
17 * Revision 1.8 1995/02/17 14:48:41 quinn
18 * 'nother bug in present
20 * Revision 1.7 1995/02/17 14:42:21 quinn
21 * Trivial bug in fetch-loop.
23 * Revision 1.6 1995/02/17 14:41:22 quinn
26 * Revision 1.5 1995/02/17 13:58:01 quinn
27 * First kick at present handling
29 * Revision 1.4 1995/02/16 15:33:45 quinn
30 * Fixed bug in KWAQS generator
32 * Revision 1.3 1995/02/16 15:20:45 quinn
33 * Added initialization of response from search
35 * Revision 1.2 1995/02/16 15:14:53 quinn
36 * Fixed KWAQS-generator
38 * Revision 1.1 1995/02/16 14:47:55 quinn
44 * Interface to the Z39.50 toolkit.
59 struct zass /* Z-assoc */
61 NETBOXPROFILE *ass; /* ZDIST association handle */
62 int fd; /* low-level socket (for select only) */
64 int preferredmessagesize;
65 char *buf; /* intermediary buffer */
68 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
70 struct ccl_rpn_attr *i;
71 static char *ops[] = {"and", "or", "not"};
77 strcpy(*p, q->u.t.term);
78 (*p) += strlen(q->u.t.term);
83 for (i = q->u.t.attr_list; i; i = i->next)
85 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
94 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
96 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
97 strcpy(*p, ops[q->kind]);
98 *p += strlen(ops[q->kind]);
101 if (rpn2kwaqs(q->u.p[0], p) < 0)
105 if (rpn2kwaqs(q->u.p[1], p) < 0)
111 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
116 ZASS zass_open(char *host, int port)
123 if (!(p = malloc(sizeof(*p))))
125 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
128 p->maxrecordsize = ZASS_MAXRECORDSIZE;
129 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
130 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
132 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
135 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
137 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
140 p->ass->TimeOutSec = 120;
141 p->ass->TimeOutUSec = 0;
142 strcpy(p->ass->HostName, host);
145 if (netbox_Open(p->ass) != 1)
147 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
150 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
152 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
153 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
156 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
159 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
163 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
166 InitRequest_Destroy(ireq);
167 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
169 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
172 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
173 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
174 p->maxrecordsize)) <= 0)
176 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
179 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
180 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
182 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
185 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
186 if (!InitResponse_GetResult(ires))
188 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
191 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
192 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
193 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
194 InitResponse_Destroy(ires);
198 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
199 char *resname, char *databases)
201 static struct zass_searchent r;
203 DATA_DIR *pdu, *record;
207 if (rpn2kwaqs(query, &p) < 0)
209 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
212 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
213 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
214 0, 0, 0, kwaqs, BIB1_OID);
217 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
220 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
224 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
227 SearchRequest_Destroy(pdu);
228 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
230 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
233 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
234 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
235 a->maxrecordsize)) <= 0)
237 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
240 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
241 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
243 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
246 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
247 r.status = SearchResponse_GetSearchStatus(pdu);
248 r.num = SearchResponse_GetResultCount(pdu);
249 r.status = SearchResponse_GetResultSetStatus(pdu);
252 if ((record = SearchResponse_GetRecords(pdu)))
254 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
258 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
259 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
263 if ((s = OctetString_GetASCIIString(ad)))
265 strcpy(r.errstring, s);
271 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
273 SearchResponse_Destroy(pdu);
279 * Triple indirection - that's kinda heavy. We'll fix it later.
280 * There are worse things around, though. Like ZDist.
282 void get_diagrec(zass_record ***p, DATA_DIR *rec)
286 **p = malloc(sizeof(***p));
288 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
289 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
293 if ((s = OctetString_GetASCIIString(ad)))
295 strcpy((**p)->errstring, s);
299 (**p)->which = ZASS_REC_DIAG;
303 void get_responserecords(zass_record ***p, DATA_DIR *rec)
305 int num, recsyntaxlen, i;
306 DATA_DIR *record, *retrec, *align;
308 POBJECTIDENTIFIER oid;
311 num = ResponseRecords_GetCount(rec);
312 for (i = 1; i <= num; i++)
314 record = ResponseRecords_GetRecord(rec, i);
317 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
320 retrec = NamePlusRecord_GetRetrievalRecord(record);
323 /* check if it's a diagrec */
324 if (record->ptr.child->fldid == 2)
325 get_diagrec(p, record->ptr.child);
328 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
332 ext = RetrievalRecord_GetExternal(retrec);
335 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
338 oid = External_GetDirectReference(ext);
341 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
344 recsyntaxlen = DirectReference_GetLength(oid);
345 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
346 recsyntax[recsyntaxlen] = '\0';
347 **p = malloc(sizeof(***p));
349 if (!strcmp(recsyntax, USMARC_OID))
350 (**p)->which = ZASS_REC_USMARC;
353 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
354 gw_log(GW_LOG_WARN, ZASS_TYPE, "Type was '%d'", (**p)->which);
356 align = External_GetEncodingAligned(ext);
359 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
360 align = External_GetEncodingSingle(ext);
363 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't ANY");
366 align = align->ptr.child;
368 if (!((**p)->record = malloc(align->count + 1)))
370 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
373 memcpy((**p)->record, align->ptr.data, align->count);
374 (**p)->record[align->count] = '\0';
375 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
382 static void zass_records_free(zass_record *p)
387 * Note that 1== first record.
389 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
392 static struct zass_presentent r = {0, 0, 0, 0};
393 zass_record **rec = &r.records;
400 zass_records_free(r.records);
405 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
407 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
411 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
414 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
418 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
421 PresentRequest_Destroy(pdu);
422 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
424 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
427 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
428 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
429 a->maxrecordsize)) <= 0)
431 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
434 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
435 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
437 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
440 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
441 r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
442 r.presentstatus = PresentResponse_GetPresentStatus(pdu);
445 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
448 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
450 switch(PresentResponse_GetRecordType(pdu))
452 case RESPONSERECORDS_TAG:
453 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
455 case NONSURROGATEDIAGNOSTIC_TAG:
456 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
459 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
461 PresentResponse_Destroy(pdu);