5 * Revision 1.3 1995/02/16 15:20:45 quinn
6 * Added initialization of response from search
8 * Revision 1.2 1995/02/16 15:14:53 quinn
9 * Fixed KWAQS-generator
11 * Revision 1.1 1995/02/16 14:47:55 quinn
17 * Interface to the Z39.50 toolkit.
32 struct zass /* Z-assoc */
34 NETBOXPROFILE *ass; /* ZDIST association handle */
35 int fd; /* low-level socket (for select only) */
37 int preferredmessagesize;
38 char *buf; /* intermediary buffer */
41 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
43 struct ccl_rpn_attr *i;
44 static char *ops[] = {"and", "or", "not"};
50 strcpy(*p, q->u.t.term);
51 (*p) += strlen(q->u.t.term);
56 for (i = q->u.t.attr_list; i; i = i->next)
58 sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
67 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
69 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
70 strcpy(*p, ops[q->kind]);
71 *p += strlen(ops[q->kind]);
74 if (rpn2kwaqs(q->u.p[0], p) < 0)
78 if (rpn2kwaqs(q->u.p[1], p) < 0)
84 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
89 ZASS zass_open(char *host, int port)
96 if (!(p = malloc(sizeof(*p))))
98 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
101 p->maxrecordsize = ZASS_MAXRECORDSIZE;
102 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
103 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
105 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
108 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
110 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
113 p->ass->TimeOutSec = 120;
114 p->ass->TimeOutUSec = 0;
115 strcpy(p->ass->HostName, host);
118 if (netbox_Open(p->ass) != 1)
120 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
123 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
125 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
126 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
129 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
132 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
136 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
139 InitRequest_Destroy(ireq);
140 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
142 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
145 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
146 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
147 p->maxrecordsize)) <= 0)
149 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
152 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
153 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
155 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
158 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
159 if (!InitResponse_GetResult(ires))
161 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
164 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
165 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
166 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
167 InitResponse_Destroy(ires);
171 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
172 char *resname, char *databases)
174 static struct zass_searchent r;
176 DATA_DIR *pdu, *record;
180 if (rpn2kwaqs(query, &p) < 0)
182 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
185 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
186 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
187 0, 0, 0, kwaqs, BIB1_OID);
190 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
193 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
197 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
200 SearchRequest_Destroy(pdu);
201 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
203 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
206 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
207 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
208 a->maxrecordsize)) <= 0)
210 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
213 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
214 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
216 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
219 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
220 r.status = SearchResponse_GetSearchStatus(pdu);
221 r.num = SearchResponse_GetResultCount(pdu);
222 r.status = SearchResponse_GetResultSetStatus(pdu);
225 if ((record = SearchResponse_GetRecords(pdu)))
227 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
231 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
232 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
236 if ((s = OctetString_GetASCIIString(ad)))
238 strcpy(r.errstring, s);
244 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
246 SearchResponse_Destroy(pdu);
252 * Note that 1== first record.
254 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,