5 * Revision 1.5 1995/02/17 13:58:01 quinn
6 * First kick at present handling
8 * Revision 1.4 1995/02/16 15:33:45 quinn
9 * Fixed bug in KWAQS generator
11 * Revision 1.3 1995/02/16 15:20:45 quinn
12 * Added initialization of response from search
14 * Revision 1.2 1995/02/16 15:14:53 quinn
15 * Fixed KWAQS-generator
17 * Revision 1.1 1995/02/16 14:47:55 quinn
23 * Interface to the Z39.50 toolkit.
38 struct zass /* Z-assoc */
40 NETBOXPROFILE *ass; /* ZDIST association handle */
41 int fd; /* low-level socket (for select only) */
43 int preferredmessagesize;
44 char *buf; /* intermediary buffer */
47 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
49 struct ccl_rpn_attr *i;
50 static char *ops[] = {"and", "or", "not"};
56 strcpy(*p, q->u.t.term);
57 (*p) += strlen(q->u.t.term);
62 for (i = q->u.t.attr_list; i; i = i->next)
64 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
73 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
75 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
76 strcpy(*p, ops[q->kind]);
77 *p += strlen(ops[q->kind]);
80 if (rpn2kwaqs(q->u.p[0], p) < 0)
84 if (rpn2kwaqs(q->u.p[1], p) < 0)
90 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
95 ZASS zass_open(char *host, int port)
102 if (!(p = malloc(sizeof(*p))))
104 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
107 p->maxrecordsize = ZASS_MAXRECORDSIZE;
108 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
109 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
111 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
114 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
116 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
119 p->ass->TimeOutSec = 120;
120 p->ass->TimeOutUSec = 0;
121 strcpy(p->ass->HostName, host);
124 if (netbox_Open(p->ass) != 1)
126 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
129 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
131 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
132 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
135 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
138 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
142 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
145 InitRequest_Destroy(ireq);
146 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
148 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
151 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
152 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
153 p->maxrecordsize)) <= 0)
155 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
158 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
159 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
161 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
164 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
165 if (!InitResponse_GetResult(ires))
167 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
170 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
171 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
172 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
173 InitResponse_Destroy(ires);
177 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
178 char *resname, char *databases)
180 static struct zass_searchent r;
182 DATA_DIR *pdu, *record;
186 if (rpn2kwaqs(query, &p) < 0)
188 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
191 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
192 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
193 0, 0, 0, kwaqs, BIB1_OID);
196 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
199 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
203 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
206 SearchRequest_Destroy(pdu);
207 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
209 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
212 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
213 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
214 a->maxrecordsize)) <= 0)
216 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
219 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
220 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
222 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
225 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
226 r.status = SearchResponse_GetSearchStatus(pdu);
227 r.num = SearchResponse_GetResultCount(pdu);
228 r.status = SearchResponse_GetResultSetStatus(pdu);
231 if ((record = SearchResponse_GetRecords(pdu)))
233 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
237 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
238 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
242 if ((s = OctetString_GetASCIIString(ad)))
244 strcpy(r.errstring, s);
250 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
252 SearchResponse_Destroy(pdu);
258 * Triple indirection - that's kinda heavy. We'll fix it later.
259 * There are worse things around, though. Like ZDist.
261 void get_diagrec(zass_record ***p, DATA_DIR *rec)
265 **p = malloc(sizeof(***p));
267 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
268 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
272 if ((s = OctetString_GetASCIIString(ad)))
274 strcpy((**p)->errstring, s);
278 (**p)->which = ZASS_REC_DIAG;
282 void get_responserecords(zass_record ***p, DATA_DIR *rec)
284 int num, recsyntaxlen, i;
285 DATA_DIR *record, *retrec, *align;
287 POBJECTIDENTIFIER oid;
290 num = ResponseRecords_GetCount(rec);
291 for (i = 1; i <= num; i++)
293 record = ResponseRecords_GetRecord(rec, i);
296 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
299 retrec = NamePlusRecord_GetRetrievalRecord(record);
302 /* check if it's a diagrec */
303 if (record->ptr.child->fldid == 2)
304 get_diagrec(p, record->ptr.child);
307 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
311 ext = RetrievalRecord_GetExternal(retrec);
314 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
317 oid = External_GetDirectReference(ext);
320 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
323 recsyntaxlen = DirectReference_GetLength(oid);
324 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
325 recsyntax[recsyntaxlen] = '\0';
326 **p = malloc(sizeof(***p));
328 if (!strcmp(recsyntax, USMARC_OID))
329 (**p)->which = ZASS_REC_USMARC;
332 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
335 align = External_GetEncodingAligned(ext);
338 gw_log(GW_LOG_WARN, ZASS_TYPE, "AAAARRRGH!! Enough of these log-messages!!!");
341 if (!((**p)->record = malloc(align->count + 1)))
343 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
346 memcpy((**p)->record, align->ptr.data, align->count);
347 (**p)->record[align->count] = '\0';
348 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
355 static void zass_records_free(zass_record *p)
360 * Note that 1== first record.
362 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
365 static struct zass_presentent r = {0, 0, 0, 0};
366 zass_record **rec = &r.records;
373 zass_records_free(r.records);
378 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num,
380 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num, 0, 0);
383 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
386 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
390 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
393 PresentRequest_Destroy(pdu);
394 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
396 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
399 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
400 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
401 a->maxrecordsize)) <= 0)
403 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
406 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
407 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
409 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
412 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
413 r.num = PresentResponse_GetNumberOfRecordsReturned(pdu);
416 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
419 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
422 switch(PresentResponse_GetRecordType(pdu))
424 case RESPONSERECORDS_TAG:
425 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
426 case NONSURROGATEDIAGNOSTIC_TAG:
427 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
429 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
434 PresentResponse_Destroy(pdu);