Asynch. API
[egate.git] / zlayer / zaccess.c
1 /*
2  * Europagate, 1995
3  *
4  * Z39.50 API for the Email gateway
5  *
6  * $Log: zaccess.c,v $
7  * Revision 1.16  1995/04/20 15:25:34  quinn
8  * Asynch. API
9  *
10  * Revision 1.15  1995/04/17  11:26:55  quinn
11  * Added YAZ version of zaccess
12  *
13  * Revision 1.14  1995/02/23  08:32:26  adam
14  * Changed header.
15  *
16  * Revision 1.12  1995/02/20  20:35:37  quinn
17  * Pull present status from presresp.
18  *
19  * Revision 1.11  1995/02/20  18:58:05  quinn
20  * Added hack for record in ANY
21  *
22  * Revision 1.10  1995/02/20  18:19:30  quinn
23  * More relaxed about record types.
24  *
25  * Revision 1.9  1995/02/17  15:17:51  quinn
26  * Bug fix
27  *
28  * Revision 1.8  1995/02/17  14:48:41  quinn
29  * 'nother bug in present
30  *
31  * Revision 1.7  1995/02/17  14:42:21  quinn
32  * Trivial bug in fetch-loop.
33  *
34  * Revision 1.6  1995/02/17  14:41:22  quinn
35  * Debugging.
36  *
37  * Revision 1.5  1995/02/17  13:58:01  quinn
38  * First kick at present handling
39  *
40  * Revision 1.4  1995/02/16  15:33:45  quinn
41  * Fixed bug in KWAQS generator
42  *
43  * Revision 1.3  1995/02/16  15:20:45  quinn
44  * Added initialization of response from search
45  *
46  * Revision 1.2  1995/02/16  15:14:53  quinn
47  * Fixed KWAQS-generator
48  *
49  * Revision 1.1  1995/02/16  14:47:55  quinn
50  * First kick.
51  *
52  */
53
54 /*
55  * Interface to the Z39.50 toolkit. Primary function is to hide Zdist, or
56  * whatever lower-layer we decide to use later. The decision to add a
57  * layer atop the toolkit was twofold: It vastly simplifies the
58  * implementation of the protocol persistence, and it hides Zdist. The
59  * latter is useful after Zdist has gone and changed their fine API after
60  * we went through all the trouble of documenting it in our Design. Don't
61  * want that to happen again.
62  *
63  * For the time being at least, we'll have these routines hang (or err) if
64  * they get a WOULDBLOCK on a write. That seems safe since, under normal
65  * circumstances, the network buffers should always be able to absorb
66  * the small request packages.
67  */
68
69 #include <stdlib.h>
70 #include <assert.h>
71
72 #include <z3950.h>
73 #include <z3950sup.h>
74 #include <zutil.h>
75
76 #include <gw-log.h>
77
78 #include <ccl.h>
79 #include <zaccess.h>
80
81 struct zass    /* Z-assoc */
82 {
83     NETBOXPROFILE *ass;              /* ZDIST association handle */
84     int fd;                         /* low-level socket (for select only) */
85     int maxrecordsize;
86     int preferredmessagesize;
87     char *buf;                      /* intermediary buffer */
88 };
89
90 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
91 {
92     struct ccl_rpn_attr *i;
93     static char *ops[] = {"and", "or", "not"};
94
95     assert(!CCL_RPN_AND);
96     switch (q->kind)
97     {
98         case CCL_RPN_TERM:
99             strcpy(*p, q->u.t.term);
100             (*p) += strlen(q->u.t.term);
101             if (q->u.t.attr_list)
102             {
103                 strcat(*p, "[");
104                 (*p)++;
105                 for (i = q->u.t.attr_list; i; i = i->next)
106                 {
107                     sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
108                         "," : "");
109                     *p += strlen(*p);
110                 }
111                 strcat(*p, "]");
112                 (*p)++;
113             }
114             return 0;
115         case CCL_RPN_SET:
116             gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
117             return -1;
118         case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
119             strcpy(*p, ops[q->kind]);
120             *p += strlen(ops[q->kind]);
121             strcat(*p, "(");
122             (*p)++;
123             if (rpn2kwaqs(q->u.p[0], p) < 0)
124                 return -1;
125             strcat(*p, ",");
126             (*p)++;
127             if (rpn2kwaqs(q->u.p[1], p) < 0)
128                 return -1;
129             strcat(*p, ")");
130             (*p)++;
131             return 0;
132         default:
133             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
134             return -1;
135     }
136 }
137
138 int zass_openresult(ZASS p, int *complete)
139 {
140     int len;
141     PINITRESPONSE ires;
142
143     if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
144         p->maxrecordsize)) <= 0)
145     {
146         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
147         return 0;
148     }
149     ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
150     if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
151     {
152         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
153         return 0;
154     }
155     gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
156     if (!InitResponse_GetResult(ires))
157     {
158         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
159         return 0;
160     }
161     gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
162     p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
163     p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
164     InitResponse_Destroy(ires);
165     *complete = 1;
166     return 0;
167 }
168
169 ZASS zass_open(char *host, int port, int *complete)
170 {
171     struct zass *p;
172     PINITREQUEST ireq;
173     PINITRESPONSE ires;
174     int len;
175     char name[512];
176
177     if (!(p = malloc(sizeof(*p))))
178     {
179         gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
180         return 0;
181     }
182     p->maxrecordsize = ZASS_MAXRECORDSIZE;
183     p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
184     if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
185     {
186         gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
187         return 0;
188     }
189     if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
190     {
191         gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
192         return 0;
193     }
194     p->ass->TimeOutSec = 120;
195     p->ass->TimeOutUSec = 0;
196     strcpy(p->ass->HostName, host);
197     p->ass->Port = port;
198
199     if (netbox_Open(p->ass) != 1)
200     {
201         gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
202         return 0;
203     }
204     gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d",
205          p->ass->HostName, p->ass->Port);
206     sprintf(name, "%s (ZDIST protocol layer)", ZASS_NAME);
207     ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
208         p->preferredmessagesize, ZASS_ID, name, ZASS_VERSION, 0);
209     if (!ireq)
210     {
211         gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
212         return 0;
213     }
214     zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
215         p->maxrecordsize);
216     if (len <= 0)
217     {
218         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
219         return 0;
220     }
221     InitRequest_Destroy(ireq);
222     if (netbox_SendBuffer(p->ass, p->buf, len) != len)
223     {
224         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
225         return 0;
226     }
227     gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
228
229     if (zass_openresult(p, complete) < 0 && (!complete || *complete))
230         return 0;
231     else
232         return p;
233
234 }
235
236 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
237     char *resname, char *databases)
238 {
239     static struct zass_searchent r;
240     char kwaqs[512], *p;
241     DATA_DIR *pdu, *record;
242     int len;
243
244     p = kwaqs;
245     if (rpn2kwaqs(query, &p) < 0)
246     {
247         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
248         return 0;
249     }
250     gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
251     pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
252         0, 0, 0, kwaqs, BIB1_OID);
253     if (!pdu)
254     {
255         gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
256         return 0;
257     }
258     zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
259         a->maxrecordsize);
260     if (len <= 0)
261     {
262         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
263         return 0;
264     }
265     SearchRequest_Destroy(pdu);
266     if (netbox_SendBuffer(a->ass, a->buf, len) != len)
267     {
268         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
269         return 0;
270     }
271     gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
272     if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
273         a->maxrecordsize)) <= 0)
274     {
275         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
276         return 0;
277     }
278     pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
279     if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
280     {
281         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
282         return 0;
283     }
284     gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
285     r.status = SearchResponse_GetSearchStatus(pdu);
286     r.num = SearchResponse_GetResultCount(pdu);
287     r.status = SearchResponse_GetResultSetStatus(pdu);
288     r.errcode = -1;
289     *r.errstring = '\0';
290     if ((record = SearchResponse_GetRecords(pdu)))
291     {
292         if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
293         {
294             DATA_DIR *ad;
295
296             r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
297             if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
298             {
299                 char *s;
300
301                 if ((s = OctetString_GetASCIIString(ad)))
302                 {
303                     strcpy(r.errstring, s);
304                     FREE(s);
305                 }
306             }
307         }
308         else
309             gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
310     }
311     SearchResponse_Destroy(pdu);
312
313     return &r;
314 }
315
316 /*
317  * Triple indirection - that's kinda heavy. We'll fix it later.
318  * There are worse things around, though. Like ZDist.
319  */
320 void get_diagrec(zass_record ***p, DATA_DIR *rec)
321 {
322     DATA_DIR *ad;
323
324     **p = malloc(sizeof(***p));
325     (**p)->next = 0;
326     (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
327     if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
328     {
329         char *s;
330
331         if ((s = OctetString_GetASCIIString(ad)))
332         {
333             strcpy((**p)->errstring, s);
334             FREE(s);
335         }
336     }
337     (**p)->which = ZASS_REC_DIAG;
338     *p = &(**p)->next;
339 }
340
341 void get_responserecords(zass_record ***p, DATA_DIR *rec)
342 {
343     int num, recsyntaxlen, i;
344     DATA_DIR *record, *retrec, *align;
345     PEXTERNAL ext;
346     POBJECTIDENTIFIER oid;
347     char recsyntax[256];
348
349     num = ResponseRecords_GetCount(rec);
350     for (i = 1; i <= num; i++)
351     {
352         record = ResponseRecords_GetRecord(rec, i);
353         if (!record)
354         {
355             gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
356             return;
357         }
358         retrec = NamePlusRecord_GetRetrievalRecord(record);
359         if (!retrec)
360         {
361             /* check if it's a diagrec */
362             if (record->ptr.child->fldid == 2)
363                 get_diagrec(p, record->ptr.child);
364             else
365             {
366                 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
367                 return;
368             }
369         }
370         ext = RetrievalRecord_GetExternal(retrec);
371         if (!ext)
372         {
373             gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
374             return;
375         }
376         oid = External_GetDirectReference(ext);
377         if (!oid)
378         {
379             gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
380             return;
381         }
382         recsyntaxlen = DirectReference_GetLength(oid);
383         memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
384         recsyntax[recsyntaxlen] = '\0';
385         **p = malloc(sizeof(***p));
386         (**p)->next = 0;
387         if (!strcmp(recsyntax, USMARC_OID))
388             (**p)->which = ZASS_REC_USMARC;
389         else
390         {
391             gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
392             gw_log(GW_LOG_WARN, ZASS_TYPE, "Type was '%d'", (**p)->which);
393         }
394         align = External_GetEncodingAligned(ext);
395         if (!align)
396         {
397             gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
398             align = External_GetEncodingSingle(ext);
399             if (!align)
400             {
401                 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't ANY");
402                 return;
403             }
404             align = align->ptr.child;
405         }
406         if (!((**p)->record = malloc(align->count + 1)))
407         {
408             gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
409             return;
410         }
411         memcpy((**p)->record, align->ptr.data, align->count);
412         (**p)->record[align->count] = '\0';
413         gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
414             align->count);
415
416         (*p) = &(**p)->next;
417     }
418 }
419
420 static void zass_records_free(zass_record *p)
421 {
422 }
423
424 /*
425  * Note that 1== first record.
426  */
427 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
428     int num)
429 {
430     static struct zass_presentent r = {0, 0, 0, 0};
431     zass_record **rec = &r.records;
432     DATA_DIR *pdu;
433     int len;
434
435     r.num = 0;
436     if (r.records)
437     {
438         zass_records_free(r.records);
439         r.records = 0;
440     }
441     do
442     {
443         gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
444             start);
445         pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
446             USMARC_OID);
447         if (!pdu)
448         {
449             gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
450             return 0;
451         }
452         zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
453             a->maxrecordsize);
454         if (len <= 0)
455         {
456             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
457             return 0;
458         }
459         PresentRequest_Destroy(pdu);
460         if (netbox_SendBuffer(a->ass, a->buf, len) != len)
461         {
462             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
463             return 0;
464         }
465         gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
466         if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
467             a->maxrecordsize)) <= 0)
468         {
469             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
470             return 0;
471         }
472         pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
473         if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
474         {
475             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
476             return 0;
477         }
478         gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
479         r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
480         r.presentstatus = PresentResponse_GetPresentStatus(pdu);
481         if (r.num == 0)
482         {
483             gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
484             return 0;
485         }
486         r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
487         start = r.nextpos;
488         switch(PresentResponse_GetRecordType(pdu))
489         {
490             case RESPONSERECORDS_TAG:
491                 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
492                 break;
493             case NONSURROGATEDIAGNOSTIC_TAG:
494                 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
495                 break;
496             default:
497                 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
498         }
499         PresentResponse_Destroy(pdu);
500     }
501     while (num - r.num && start);
502     *rec = 0;
503         
504     return &r;
505 }