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