4 * Z39.50 API for the Email gateway
7 * Revision 1.14 1995/02/23 08:32:26 adam
10 * Revision 1.12 1995/02/20 20:35:37 quinn
11 * Pull present status from presresp.
13 * Revision 1.11 1995/02/20 18:58:05 quinn
14 * Added hack for record in ANY
16 * Revision 1.10 1995/02/20 18:19:30 quinn
17 * More relaxed about record types.
19 * Revision 1.9 1995/02/17 15:17:51 quinn
22 * Revision 1.8 1995/02/17 14:48:41 quinn
23 * 'nother bug in present
25 * Revision 1.7 1995/02/17 14:42:21 quinn
26 * Trivial bug in fetch-loop.
28 * Revision 1.6 1995/02/17 14:41:22 quinn
31 * Revision 1.5 1995/02/17 13:58:01 quinn
32 * First kick at present handling
34 * Revision 1.4 1995/02/16 15:33:45 quinn
35 * Fixed bug in KWAQS generator
37 * Revision 1.3 1995/02/16 15:20:45 quinn
38 * Added initialization of response from search
40 * Revision 1.2 1995/02/16 15:14:53 quinn
41 * Fixed KWAQS-generator
43 * Revision 1.1 1995/02/16 14:47:55 quinn
49 * Interface to the Z39.50 toolkit.
64 struct zass /* Z-assoc */
66 NETBOXPROFILE *ass; /* ZDIST association handle */
67 int fd; /* low-level socket (for select only) */
69 int preferredmessagesize;
70 char *buf; /* intermediary buffer */
73 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
75 struct ccl_rpn_attr *i;
76 static char *ops[] = {"and", "or", "not"};
82 strcpy(*p, q->u.t.term);
83 (*p) += strlen(q->u.t.term);
88 for (i = q->u.t.attr_list; i; i = i->next)
90 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
99 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
101 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
102 strcpy(*p, ops[q->kind]);
103 *p += strlen(ops[q->kind]);
106 if (rpn2kwaqs(q->u.p[0], p) < 0)
110 if (rpn2kwaqs(q->u.p[1], p) < 0)
116 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
121 ZASS zass_open(char *host, int port)
128 if (!(p = malloc(sizeof(*p))))
130 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
133 p->maxrecordsize = ZASS_MAXRECORDSIZE;
134 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
135 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
137 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
140 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
142 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
145 p->ass->TimeOutSec = 120;
146 p->ass->TimeOutUSec = 0;
147 strcpy(p->ass->HostName, host);
150 if (netbox_Open(p->ass) != 1)
152 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
155 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
157 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
158 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
161 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
164 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
168 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
171 InitRequest_Destroy(ireq);
172 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
174 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
177 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
178 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
179 p->maxrecordsize)) <= 0)
181 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
184 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
185 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
187 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
190 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
191 if (!InitResponse_GetResult(ires))
193 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
196 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
197 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
198 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
199 InitResponse_Destroy(ires);
203 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
204 char *resname, char *databases)
206 static struct zass_searchent r;
208 DATA_DIR *pdu, *record;
212 if (rpn2kwaqs(query, &p) < 0)
214 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
217 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
218 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
219 0, 0, 0, kwaqs, BIB1_OID);
222 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
225 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
229 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
232 SearchRequest_Destroy(pdu);
233 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
235 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
238 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
239 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
240 a->maxrecordsize)) <= 0)
242 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
245 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
246 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
248 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
251 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
252 r.status = SearchResponse_GetSearchStatus(pdu);
253 r.num = SearchResponse_GetResultCount(pdu);
254 r.status = SearchResponse_GetResultSetStatus(pdu);
257 if ((record = SearchResponse_GetRecords(pdu)))
259 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
263 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
264 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
268 if ((s = OctetString_GetASCIIString(ad)))
270 strcpy(r.errstring, s);
276 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
278 SearchResponse_Destroy(pdu);
284 * Triple indirection - that's kinda heavy. We'll fix it later.
285 * There are worse things around, though. Like ZDist.
287 void get_diagrec(zass_record ***p, DATA_DIR *rec)
291 **p = malloc(sizeof(***p));
293 (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
294 if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
298 if ((s = OctetString_GetASCIIString(ad)))
300 strcpy((**p)->errstring, s);
304 (**p)->which = ZASS_REC_DIAG;
308 void get_responserecords(zass_record ***p, DATA_DIR *rec)
310 int num, recsyntaxlen, i;
311 DATA_DIR *record, *retrec, *align;
313 POBJECTIDENTIFIER oid;
316 num = ResponseRecords_GetCount(rec);
317 for (i = 1; i <= num; i++)
319 record = ResponseRecords_GetRecord(rec, i);
322 gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
325 retrec = NamePlusRecord_GetRetrievalRecord(record);
328 /* check if it's a diagrec */
329 if (record->ptr.child->fldid == 2)
330 get_diagrec(p, record->ptr.child);
333 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
337 ext = RetrievalRecord_GetExternal(retrec);
340 gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
343 oid = External_GetDirectReference(ext);
346 gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
349 recsyntaxlen = DirectReference_GetLength(oid);
350 memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
351 recsyntax[recsyntaxlen] = '\0';
352 **p = malloc(sizeof(***p));
354 if (!strcmp(recsyntax, USMARC_OID))
355 (**p)->which = ZASS_REC_USMARC;
358 gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
359 gw_log(GW_LOG_WARN, ZASS_TYPE, "Type was '%d'", (**p)->which);
361 align = External_GetEncodingAligned(ext);
364 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
365 align = External_GetEncodingSingle(ext);
368 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't ANY");
371 align = align->ptr.child;
373 if (!((**p)->record = malloc(align->count + 1)))
375 gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
378 memcpy((**p)->record, align->ptr.data, align->count);
379 (**p)->record[align->count] = '\0';
380 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
387 static void zass_records_free(zass_record *p)
392 * Note that 1== first record.
394 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
397 static struct zass_presentent r = {0, 0, 0, 0};
398 zass_record **rec = &r.records;
405 zass_records_free(r.records);
410 gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
412 pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
416 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
419 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
423 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
426 PresentRequest_Destroy(pdu);
427 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
429 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
432 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
433 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
434 a->maxrecordsize)) <= 0)
436 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
439 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
440 if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
442 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
445 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
446 r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
447 r.presentstatus = PresentResponse_GetPresentStatus(pdu);
450 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
453 r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
455 switch(PresentResponse_GetRecordType(pdu))
457 case RESPONSERECORDS_TAG:
458 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
460 case NONSURROGATEDIAGNOSTIC_TAG:
461 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
464 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
466 PresentResponse_Destroy(pdu);