5 * Revision 1.2 1995/02/16 15:14:53 quinn
6 * Fixed KWAQS-generator
8 * Revision 1.1 1995/02/16 14:47:55 quinn
14 * Interface to the Z39.50 toolkit.
29 struct zass /* Z-assoc */
31 NETBOXPROFILE *ass; /* ZDIST association handle */
32 int fd; /* low-level socket (for select only) */
34 int preferredmessagesize;
35 char *buf; /* intermediary buffer */
38 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
40 struct ccl_rpn_attr *i;
41 static char *ops[] = {"and", "or", "not"};
47 strcpy(*p, q->u.t.term);
48 (*p) += strlen(q->u.t.term);
53 for (i = q->u.t.attr_list; i; i = i->next)
55 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
64 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
66 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
67 strcpy(*p, ops[q->kind]);
68 *p += strlen(ops[q->kind]);
71 if (rpn2kwaqs(q->u.p[0], p) < 0)
75 if (rpn2kwaqs(q->u.p[1], p) < 0)
81 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
86 ZASS zass_open(char *host, int port)
93 if (!(p = malloc(sizeof(*p))))
95 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
98 p->maxrecordsize = ZASS_MAXRECORDSIZE;
99 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
100 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
102 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
105 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
107 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
110 p->ass->TimeOutSec = 120;
111 p->ass->TimeOutUSec = 0;
112 strcpy(p->ass->HostName, host);
115 if (netbox_Open(p->ass) != 1)
117 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
120 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
122 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
123 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
126 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
129 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
133 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
136 InitRequest_Destroy(ireq);
137 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
139 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
142 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
143 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
144 p->maxrecordsize)) <= 0)
146 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
149 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
150 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
152 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
155 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
156 if (!InitResponse_GetResult(ires))
158 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
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);
168 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
169 char *resname, char *databases)
171 static struct zass_searchent r;
173 DATA_DIR *pdu, *record;
177 if (rpn2kwaqs(query, &p) < 0)
179 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
182 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
183 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
184 0, 0, 0, kwaqs, BIB1_OID);
187 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
190 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
194 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
197 SearchRequest_Destroy(pdu);
198 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
200 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
203 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
204 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
205 a->maxrecordsize)) <= 0)
207 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
210 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
211 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
213 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
216 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
217 r.status = SearchResponse_GetSearchStatus(pdu);
218 r.num = SearchResponse_GetResultCount(pdu);
219 r.status = SearchResponse_GetResultSetStatus(pdu);
220 if ((record = SearchResponse_GetRecords(pdu)))
222 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
226 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
227 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
231 if ((s = OctetString_GetASCIIString(ad)))
233 strcpy(r.errstring, s);
243 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
245 SearchResponse_Destroy(pdu);
251 * Note that 1== first record.
253 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,