5 * Revision 1.11 1995/02/20 18:58:05 quinn
6 * Added hack for record in ANY
8 * Revision 1.10 1995/02/20 18:19:30 quinn
9 * More relaxed about record types.
11 * Revision 1.9 1995/02/17 15:17:51 quinn
14 * Revision 1.8 1995/02/17 14:48:41 quinn
15 * 'nother bug in present
17 * Revision 1.7 1995/02/17 14:42:21 quinn
18 * Trivial bug in fetch-loop.
20 * Revision 1.6 1995/02/17 14:41:22 quinn
23 * Revision 1.5 1995/02/17 13:58:01 quinn
24 * First kick at present handling
26 * Revision 1.4 1995/02/16 15:33:45 quinn
27 * Fixed bug in KWAQS generator
29 * Revision 1.3 1995/02/16 15:20:45 quinn
30 * Added initialization of response from search
32 * Revision 1.2 1995/02/16 15:14:53 quinn
33 * Fixed KWAQS-generator
35 * Revision 1.1 1995/02/16 14:47:55 quinn
41 * Interface to the Z39.50 toolkit.
56 struct zass /* Z-assoc */
58 NETBOXPROFILE *ass; /* ZDIST association handle */
59 int fd; /* low-level socket (for select only) */
61 int preferredmessagesize;
62 char *buf; /* intermediary buffer */
65 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
67 struct ccl_rpn_attr *i;
68 static char *ops[] = {"and", "or", "not"};
74 strcpy(*p, q->u.t.term);
75 (*p) += strlen(q->u.t.term);
80 for (i = q->u.t.attr_list; i; i = i->next)
82 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
91 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
93 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
94 strcpy(*p, ops[q->kind]);
95 *p += strlen(ops[q->kind]);
98 if (rpn2kwaqs(q->u.p[0], p) < 0)
102 if (rpn2kwaqs(q->u.p[1], p) < 0)
108 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
113 ZASS zass_open(char *host, int port)
120 if (!(p = malloc(sizeof(*p))))
122 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
125 p->maxrecordsize = ZASS_MAXRECORDSIZE;
126 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
127 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
129 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
132 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
134 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
137 p->ass->TimeOutSec = 120;
138 p->ass->TimeOutUSec = 0;
139 strcpy(p->ass->HostName, host);
142 if (netbox_Open(p->ass) != 1)
144 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
147 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
149 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
150 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
153 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
156 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
160 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
163 InitRequest_Destroy(ireq);
164 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
166 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
169 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
170 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
171 p->maxrecordsize)) <= 0)
173 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
176 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
177 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
179 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
182 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
183 if (!InitResponse_GetResult(ires))
185 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
188 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
189 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
190 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
191 InitResponse_Destroy(ires);
195 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
196 char *resname, char *databases)
198 static struct zass_searchent r;
200 DATA_DIR *pdu, *record;
204 if (rpn2kwaqs(query, &p) < 0)
206 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
209 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
210 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
211 0, 0, 0, kwaqs, BIB1_OID);
214 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
217 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
221 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
224 SearchRequest_Destroy(pdu);
225 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
227 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
230 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
231 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
232 a->maxrecordsize)) <= 0)
234 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
237 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
238 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
240 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
243 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
244 r.status = SearchResponse_GetSearchStatus(pdu);
245 r.num = SearchResponse_GetResultCount(pdu);
246 r.status = SearchResponse_GetResultSetStatus(pdu);
249 if ((record = SearchResponse_GetRecords(pdu)))
251 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
255 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
256 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
260 if ((s = OctetString_GetASCIIString(ad)))
262 strcpy(r.errstring, s);
268 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
270 SearchResponse_Destroy(pdu);
276 * Triple indirection - that's kinda heavy. We'll fix it later.
277 * There are worse things around, though. Like ZDist.
279 void get_diagrec(zass_record ***p, DATA_DIR *rec)
283 **p = malloc(sizeof(***p));
285 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
286 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
290 if ((s = OctetString_GetASCIIString(ad)))
292 strcpy((**p)->errstring, s);
296 (**p)->which = ZASS_REC_DIAG;
300 void get_responserecords(zass_record ***p, DATA_DIR *rec)
302 int num, recsyntaxlen, i;
303 DATA_DIR *record, *retrec, *align;
305 POBJECTIDENTIFIER oid;
308 num = ResponseRecords_GetCount(rec);
309 for (i = 1; i <= num; i++)
311 record = ResponseRecords_GetRecord(rec, i);
314 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
317 retrec = NamePlusRecord_GetRetrievalRecord(record);
320 /* check if it's a diagrec */
321 if (record->ptr.child->fldid == 2)
322 get_diagrec(p, record->ptr.child);
325 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
329 ext = RetrievalRecord_GetExternal(retrec);
332 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
335 oid = External_GetDirectReference(ext);
338 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
341 recsyntaxlen = DirectReference_GetLength(oid);
342 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
343 recsyntax[recsyntaxlen] = '\0';
344 **p = malloc(sizeof(***p));
346 if (!strcmp(recsyntax, USMARC_OID))
347 (**p)->which = ZASS_REC_USMARC;
350 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
351 gw_log(GW_LOG_WARN, ZASS_TYPE, "Type was '%d'", (**p)->which);
353 align = External_GetEncodingAligned(ext);
356 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
357 align = External_GetEncodingSingle(ext);
360 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't ANY");
363 align = align->ptr.child;
365 if (!((**p)->record = malloc(align->count + 1)))
367 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
370 memcpy((**p)->record, align->ptr.data, align->count);
371 (**p)->record[align->count] = '\0';
372 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
379 static void zass_records_free(zass_record *p)
384 * Note that 1== first record.
386 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
389 static struct zass_presentent r = {0, 0, 0, 0};
390 zass_record **rec = &r.records;
397 zass_records_free(r.records);
402 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
404 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
408 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
411 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
415 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
418 PresentRequest_Destroy(pdu);
419 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
421 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
424 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
425 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
426 a->maxrecordsize)) <= 0)
428 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
431 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
432 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
434 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
437 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
438 r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
441 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
444 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
446 switch(PresentResponse_GetRecordType(pdu))
448 case RESPONSERECORDS_TAG:
449 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
451 case NONSURROGATEDIAGNOSTIC_TAG:
452 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
455 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
457 PresentResponse_Destroy(pdu);