5 * Revision 1.10 1995/02/20 18:19:30 quinn
6 * More relaxed about record types.
8 * Revision 1.9 1995/02/17 15:17:51 quinn
11 * Revision 1.8 1995/02/17 14:48:41 quinn
12 * 'nother bug in present
14 * Revision 1.7 1995/02/17 14:42:21 quinn
15 * Trivial bug in fetch-loop.
17 * Revision 1.6 1995/02/17 14:41:22 quinn
20 * Revision 1.5 1995/02/17 13:58:01 quinn
21 * First kick at present handling
23 * Revision 1.4 1995/02/16 15:33:45 quinn
24 * Fixed bug in KWAQS generator
26 * Revision 1.3 1995/02/16 15:20:45 quinn
27 * Added initialization of response from search
29 * Revision 1.2 1995/02/16 15:14:53 quinn
30 * Fixed KWAQS-generator
32 * Revision 1.1 1995/02/16 14:47:55 quinn
38 * Interface to the Z39.50 toolkit.
53 struct zass /* Z-assoc */
55 NETBOXPROFILE *ass; /* ZDIST association handle */
56 int fd; /* low-level socket (for select only) */
58 int preferredmessagesize;
59 char *buf; /* intermediary buffer */
62 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
64 struct ccl_rpn_attr *i;
65 static char *ops[] = {"and", "or", "not"};
71 strcpy(*p, q->u.t.term);
72 (*p) += strlen(q->u.t.term);
77 for (i = q->u.t.attr_list; i; i = i->next)
79 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
88 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
90 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
91 strcpy(*p, ops[q->kind]);
92 *p += strlen(ops[q->kind]);
95 if (rpn2kwaqs(q->u.p[0], p) < 0)
99 if (rpn2kwaqs(q->u.p[1], p) < 0)
105 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
110 ZASS zass_open(char *host, int port)
117 if (!(p = malloc(sizeof(*p))))
119 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
122 p->maxrecordsize = ZASS_MAXRECORDSIZE;
123 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
124 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
126 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
129 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
131 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
134 p->ass->TimeOutSec = 120;
135 p->ass->TimeOutUSec = 0;
136 strcpy(p->ass->HostName, host);
139 if (netbox_Open(p->ass) != 1)
141 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
144 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
146 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
147 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
150 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
153 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
157 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
160 InitRequest_Destroy(ireq);
161 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
163 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
166 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
167 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
168 p->maxrecordsize)) <= 0)
170 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
173 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
174 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
176 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
179 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
180 if (!InitResponse_GetResult(ires))
182 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
185 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
186 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
187 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
188 InitResponse_Destroy(ires);
192 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
193 char *resname, char *databases)
195 static struct zass_searchent r;
197 DATA_DIR *pdu, *record;
201 if (rpn2kwaqs(query, &p) < 0)
203 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
206 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
207 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
208 0, 0, 0, kwaqs, BIB1_OID);
211 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
214 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
218 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
221 SearchRequest_Destroy(pdu);
222 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
224 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
227 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
228 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
229 a->maxrecordsize)) <= 0)
231 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
234 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
235 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
237 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
240 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
241 r.status = SearchResponse_GetSearchStatus(pdu);
242 r.num = SearchResponse_GetResultCount(pdu);
243 r.status = SearchResponse_GetResultSetStatus(pdu);
246 if ((record = SearchResponse_GetRecords(pdu)))
248 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
252 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
253 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
257 if ((s = OctetString_GetASCIIString(ad)))
259 strcpy(r.errstring, s);
265 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
267 SearchResponse_Destroy(pdu);
273 * Triple indirection - that's kinda heavy. We'll fix it later.
274 * There are worse things around, though. Like ZDist.
276 void get_diagrec(zass_record ***p, DATA_DIR *rec)
280 **p = malloc(sizeof(***p));
282 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
283 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
287 if ((s = OctetString_GetASCIIString(ad)))
289 strcpy((**p)->errstring, s);
293 (**p)->which = ZASS_REC_DIAG;
297 void get_responserecords(zass_record ***p, DATA_DIR *rec)
299 int num, recsyntaxlen, i;
300 DATA_DIR *record, *retrec, *align;
302 POBJECTIDENTIFIER oid;
305 num = ResponseRecords_GetCount(rec);
306 for (i = 1; i <= num; i++)
308 record = ResponseRecords_GetRecord(rec, i);
311 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
314 retrec = NamePlusRecord_GetRetrievalRecord(record);
317 /* check if it's a diagrec */
318 if (record->ptr.child->fldid == 2)
319 get_diagrec(p, record->ptr.child);
322 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
326 ext = RetrievalRecord_GetExternal(retrec);
329 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
332 oid = External_GetDirectReference(ext);
335 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
338 recsyntaxlen = DirectReference_GetLength(oid);
339 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
340 recsyntax[recsyntaxlen] = '\0';
341 **p = malloc(sizeof(***p));
343 if (!strcmp(recsyntax, USMARC_OID))
344 (**p)->which = ZASS_REC_USMARC;
347 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
348 gw_log(GW_LOG_WARN, ZASS_TYPE, "Type was '%d'", (**p)->which);
350 align = External_GetEncodingAligned(ext);
353 gw_log(GW_LOG_WARN, ZASS_TYPE, "AAAARRRGH!! Enough of these log-messages!!!");
356 if (!((**p)->record = malloc(align->count + 1)))
358 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
361 memcpy((**p)->record, align->ptr.data, align->count);
362 (**p)->record[align->count] = '\0';
363 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
370 static void zass_records_free(zass_record *p)
375 * Note that 1== first record.
377 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
380 static struct zass_presentent r = {0, 0, 0, 0};
381 zass_record **rec = &r.records;
388 zass_records_free(r.records);
393 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
395 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
399 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
402 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
406 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
409 PresentRequest_Destroy(pdu);
410 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
412 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
415 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
416 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
417 a->maxrecordsize)) <= 0)
419 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
422 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
423 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
425 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
428 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
429 r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
432 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
435 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
437 switch(PresentResponse_GetRecordType(pdu))
439 case RESPONSERECORDS_TAG:
440 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
442 case NONSURROGATEDIAGNOSTIC_TAG:
443 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
446 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
448 PresentResponse_Destroy(pdu);