5 * Revision 1.4 1995/02/16 15:33:45 quinn
6 * Fixed bug in KWAQS generator
8 * Revision 1.3 1995/02/16 15:20:45 quinn
9 * Added initialization of response from search
11 * Revision 1.2 1995/02/16 15:14:53 quinn
12 * Fixed KWAQS-generator
14 * Revision 1.1 1995/02/16 14:47:55 quinn
20 * Interface to the Z39.50 toolkit.
35 struct zass /* Z-assoc */
37 NETBOXPROFILE *ass; /* ZDIST association handle */
38 int fd; /* low-level socket (for select only) */
40 int preferredmessagesize;
41 char *buf; /* intermediary buffer */
44 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
46 struct ccl_rpn_attr *i;
47 static char *ops[] = {"and", "or", "not"};
53 strcpy(*p, q->u.t.term);
54 (*p) += strlen(q->u.t.term);
59 for (i = q->u.t.attr_list; i; i = i->next)
61 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
70 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
72 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
73 strcpy(*p, ops[q->kind]);
74 *p += strlen(ops[q->kind]);
77 if (rpn2kwaqs(q->u.p[0], p) < 0)
81 if (rpn2kwaqs(q->u.p[1], p) < 0)
87 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
92 ZASS zass_open(char *host, int port)
99 if (!(p = malloc(sizeof(*p))))
101 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
104 p->maxrecordsize = ZASS_MAXRECORDSIZE;
105 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
106 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
108 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
111 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
113 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
116 p->ass->TimeOutSec = 120;
117 p->ass->TimeOutUSec = 0;
118 strcpy(p->ass->HostName, host);
121 if (netbox_Open(p->ass) != 1)
123 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
126 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
128 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
129 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
132 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
135 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
139 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
142 InitRequest_Destroy(ireq);
143 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
145 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
148 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
149 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
150 p->maxrecordsize)) <= 0)
152 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
155 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
156 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
158 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
161 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
162 if (!InitResponse_GetResult(ires))
164 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
167 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
168 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
169 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
170 InitResponse_Destroy(ires);
174 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
175 char *resname, char *databases)
177 static struct zass_searchent r;
179 DATA_DIR *pdu, *record;
183 if (rpn2kwaqs(query, &p) < 0)
185 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
188 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
189 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
190 0, 0, 0, kwaqs, BIB1_OID);
193 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
196 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
200 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
203 SearchRequest_Destroy(pdu);
204 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
206 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
209 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
210 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
211 a->maxrecordsize)) <= 0)
213 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
216 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
217 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
219 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
222 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
223 r.status = SearchResponse_GetSearchStatus(pdu);
224 r.num = SearchResponse_GetResultCount(pdu);
225 r.status = SearchResponse_GetResultSetStatus(pdu);
228 if ((record = SearchResponse_GetRecords(pdu)))
230 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
234 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
235 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
239 if ((s = OctetString_GetASCIIString(ad)))
241 strcpy(r.errstring, s);
247 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
249 SearchResponse_Destroy(pdu);
255 * Note that 1== first record.
257 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,