5 * Revision 1.1 1995/02/16 14:47:55 quinn
11 * Interface to the Z39.50 toolkit.
26 struct zass /* Z-assoc */
28 NETBOXPROFILE *ass; /* ZDIST association handle */
29 int fd; /* low-level socket (for select only) */
31 int preferredmessagesize;
32 char *buf; /* intermediary buffer */
35 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
37 struct ccl_rpn_attr *i;
38 static char *ops[] = {"and", "or", "not"};
44 strcpy(*p, q->u.t.term);
45 (*p) += strlen(q->u.t.term);
50 for (i = q->u.t.attr_list; i; i = i->next)
52 sprintf(*p, "%d,%d", i->type, i->value);
60 gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
62 case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
63 strcpy(*p, ops[q->kind]);
64 *p += strlen(ops[q->kind]);
67 if (rpn2kwaqs(q->u.p[0], p) < 0)
71 if (rpn2kwaqs(q->u.p[1], p) < 0)
77 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
82 ZASS zass_open(char *host, int port)
89 if (!(p = malloc(sizeof(*p))))
91 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
94 p->maxrecordsize = ZASS_MAXRECORDSIZE;
95 p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
96 if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
98 gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
101 if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
103 gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
106 p->ass->TimeOutSec = 120;
107 p->ass->TimeOutUSec = 0;
108 strcpy(p->ass->HostName, host);
111 if (netbox_Open(p->ass) != 1)
113 gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
116 gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d", p->ass->HostName,
118 ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
119 p->preferredmessagesize, ZASS_ID, ZASS_NAME, ZASS_VERSION, 0);
122 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
125 zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
129 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
132 InitRequest_Destroy(ireq);
133 if (netbox_SendBuffer(p->ass, p->buf, len) != len)
135 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
138 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
139 if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
140 p->maxrecordsize)) <= 0)
142 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
145 ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
146 if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
148 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
151 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
152 if (!InitResponse_GetResult(ires))
154 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
157 gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
158 p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
159 p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
160 InitResponse_Destroy(ires);
164 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
165 char *resname, char *databases)
167 static struct zass_searchent r;
169 DATA_DIR *pdu, *record;
173 if (rpn2kwaqs(query, &p) < 0)
175 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
178 gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
179 pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
180 0, 0, 0, kwaqs, BIB1_OID);
183 gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
186 zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
190 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
193 SearchRequest_Destroy(pdu);
194 if (netbox_SendBuffer(a->ass, a->buf, len) != len)
196 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
199 gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
200 if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
201 a->maxrecordsize)) <= 0)
203 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
206 pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
207 if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
209 gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
212 gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
213 r.status = SearchResponse_GetSearchStatus(pdu);
214 r.num = SearchResponse_GetResultCount(pdu);
215 r.status = SearchResponse_GetResultSetStatus(pdu);
216 if ((record = SearchResponse_GetRecords(pdu)))
218 if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
222 r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
223 if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
227 if ((s = OctetString_GetASCIIString(ad)))
229 strcpy(r.errstring, s);
239 gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
241 SearchResponse_Destroy(pdu);
247 * Note that 1== first record.
249 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,