1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2012 Index Data and Mike Taylor
3 * See the file LICENSE for details.
13 #include <yazpp/z-assoc.h>
14 #include <yaz/otherinfo.h>
15 #include <yaz/oid_db.h>
17 using namespace yazpp_1;
19 int Z_Assoc::yaz_init_func()
22 signal (SIGPIPE, SIG_IGN);
27 int Z_Assoc::yaz_init_flag = Z_Assoc::yaz_init_func();
29 Z_Assoc::Z_Assoc(IPDU_Observable *the_PDU_Observable)
31 m_PDU_Observable = the_PDU_Observable;
32 m_odr_in = odr_createmem (ODR_DECODE);
33 m_odr_out = odr_createmem (ODR_ENCODE);
34 m_odr_print = odr_createmem (ODR_PRINT);
42 void Z_Assoc::set_APDU_log(const char *fname)
44 if (m_APDU_file && m_APDU_file != stderr)
49 delete [] m_APDU_fname;
54 m_APDU_fname = new char[strlen(fname)+1];
55 strcpy (m_APDU_fname, fname);
56 if (!strcmp(fname, "-"))
58 else if (*fname == '\0')
61 m_APDU_file = fopen (fname, "a");
62 odr_setprint(m_odr_print, m_APDU_file);
66 int Z_Assoc::set_APDU_yazlog(int v)
68 int old = m_APDU_yazlog;
73 const char *Z_Assoc::get_APDU_log()
80 m_PDU_Observable->destroy();
81 delete m_PDU_Observable;
82 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
83 odr_destroy (m_odr_out);
84 odr_destroy (m_odr_in);
85 delete [] m_APDU_fname;
89 void Z_Assoc::recv_PDU(const char *buf, int len)
91 yaz_log (m_log, "recv_PDU len=%d", len);
92 Z_GDU *apdu = decode_GDU (buf, len);
99 m_PDU_Observable->shutdown();
104 Z_APDU *Z_Assoc::create_Z_PDU(int type)
106 Z_APDU *apdu = zget_APDU(m_odr_out, type);
107 if (apdu->which == Z_APDU_initRequest)
109 Z_InitRequest * p = apdu->u.initRequest;
110 char *newName = (char*) odr_malloc(m_odr_out, 50);
111 strcpy (newName, p->implementationName);
112 strcat (newName, " YAZ++");
113 p->implementationName = newName;
118 Z_ReferenceId **Z_Assoc::get_referenceIdP(Z_APDU *apdu)
122 case Z_APDU_initRequest:
123 return &apdu->u.initRequest->referenceId;
124 case Z_APDU_initResponse:
125 return &apdu->u.initResponse->referenceId;
126 case Z_APDU_searchRequest:
127 return &apdu->u.searchRequest->referenceId;
128 case Z_APDU_searchResponse:
129 return &apdu->u.searchResponse->referenceId;
130 case Z_APDU_presentRequest:
131 return &apdu->u.presentRequest->referenceId;
132 case Z_APDU_presentResponse:
133 return &apdu->u.presentResponse->referenceId;
134 case Z_APDU_deleteResultSetRequest:
135 return &apdu->u.deleteResultSetRequest->referenceId;
136 case Z_APDU_deleteResultSetResponse:
137 return &apdu->u.deleteResultSetResponse->referenceId;
138 case Z_APDU_accessControlRequest:
139 return &apdu->u.accessControlRequest->referenceId;
140 case Z_APDU_accessControlResponse:
141 return &apdu->u.accessControlResponse->referenceId;
142 case Z_APDU_resourceControlRequest:
143 return &apdu->u.resourceControlRequest->referenceId;
144 case Z_APDU_resourceControlResponse:
145 return &apdu->u.resourceControlResponse->referenceId;
146 case Z_APDU_triggerResourceControlRequest:
147 return &apdu->u.triggerResourceControlRequest->referenceId;
148 case Z_APDU_resourceReportRequest:
149 return &apdu->u.resourceReportRequest->referenceId;
150 case Z_APDU_resourceReportResponse:
151 return &apdu->u.resourceReportResponse->referenceId;
152 case Z_APDU_scanRequest:
153 return &apdu->u.scanRequest->referenceId;
154 case Z_APDU_scanResponse:
155 return &apdu->u.scanResponse->referenceId;
156 case Z_APDU_sortRequest:
157 return &apdu->u.sortRequest->referenceId;
158 case Z_APDU_sortResponse:
159 return &apdu->u.sortResponse->referenceId;
160 case Z_APDU_segmentRequest:
161 return &apdu->u.segmentRequest->referenceId;
162 case Z_APDU_extendedServicesRequest:
163 return &apdu->u.extendedServicesRequest->referenceId;
164 case Z_APDU_extendedServicesResponse:
165 return &apdu->u.extendedServicesResponse->referenceId;
167 return &apdu->u.close->referenceId;
172 void Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
174 Z_ReferenceId **id_from = get_referenceIdP(from);
175 Z_ReferenceId **id_to = get_referenceIdP(to);
176 if (id_from && *id_from && id_to)
178 *id_to = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(**id_to));
179 (*id_to)->size = (*id_to)->len = (*id_from)->len;
180 (*id_to)->buf = (unsigned char*) odr_malloc (m_odr_out, (*id_to)->len);
181 memcpy ((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
187 int Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
189 Z_GDU *gdu = (Z_GDU*) odr_malloc(odr_encode(), sizeof(*gdu));
190 gdu->which = Z_GDU_Z3950;
192 return send_GDU(gdu, plen);
195 int Z_Assoc::send_GDU(Z_GDU *apdu, int *plen)
199 if (encode_GDU(apdu, &buf, &len) > 0)
203 return m_PDU_Observable->send_PDU(buf, len);
208 Z_GDU *Z_Assoc::decode_GDU(const char *buf, int len)
212 odr_reset (m_odr_in);
213 odr_setbuf (m_odr_in, (char*) buf, len, 0);
215 if (!z_GDU(m_odr_in, &apdu, 0, 0))
217 const char *element = odr_getelement(m_odr_in);
218 yaz_log(YLOG_LOG, "PDU decode failed '%s' near byte %ld. Element %s",
219 odr_errmsg(odr_geterror(m_odr_in)),
220 (long) odr_offset(m_odr_in),
221 element && *element ? element : "unknown");
222 yaz_log(YLOG_LOG, "Buffer length: %d", (int) len);
225 WRBUF w = wrbuf_alloc();
226 wrbuf_write_escaped(w, buf, len > 20 ? 20 : len);
227 yaz_log(YLOG_LOG, "Buffer content: %s", wrbuf_cstr(w));
230 yaz_log(YLOG_LOG, "PDU dump:");
231 odr_dumpBER(yaz_log_file(), buf, len);
237 { // use YAZ log FILE
238 FILE *save = m_APDU_file;
240 odr_setprint(m_odr_print, yaz_log_file());
241 z_GDU(m_odr_print, &apdu, 0, "decode");
243 odr_setprint(m_odr_print, save);
247 z_GDU(m_odr_print, &apdu, 0, "decode");
254 int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
256 const char *element = 0;
257 int r = z_GDU(m_odr_out, &apdu, 0, 0);
259 if (!r) // decoding failed. Get the failed element
260 element = odr_getelement(m_odr_out);
262 if (m_APDU_yazlog || !r)
265 yaz_log (YLOG_LOG, "PDU encode failed. Element %s",
266 element ? element : "unknown");
267 FILE *save = m_APDU_file;
268 FILE *yazf = yaz_log_file();
269 odr_setprint(m_odr_print, yazf); // use YAZ log FILE
270 z_GDU(m_odr_print, &apdu, 0, "encode");
272 odr_setprint(m_odr_print, save);
277 fprintf (m_APDU_file, "PDU encode failed. Element %s",
278 element ? element : "unknown");
279 z_GDU(m_odr_print, &apdu, 0, "encode");
282 if (!r) // encoding failed
284 *buf = odr_getbuf (m_odr_out, len, 0);
285 odr_reset (m_odr_out);
289 const char *Z_Assoc::get_hostname()
294 int Z_Assoc::client(const char *addr)
296 delete [] m_hostname;
297 m_hostname = new char[strlen(addr)+1];
298 strcpy (m_hostname, addr);
299 return m_PDU_Observable->connect (this, addr);
302 void Z_Assoc::close()
304 m_PDU_Observable->close_session();
307 int Z_Assoc::server(const char *addr)
309 delete [] m_hostname;
310 m_hostname = new char[strlen(addr)+1];
311 strcpy (m_hostname, addr);
312 return m_PDU_Observable->listen (this, addr);
315 ODR Z_Assoc::odr_encode()
320 ODR Z_Assoc::odr_decode()
324 ODR Z_Assoc::odr_print()
329 void Z_Assoc::timeout(int timeout)
331 m_PDU_Observable->idleTime(timeout);
334 void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
338 case Z_APDU_initRequest:
339 *oip = &apdu->u.initRequest->otherInfo;
341 case Z_APDU_searchRequest:
342 *oip = &apdu->u.searchRequest->otherInfo;
344 case Z_APDU_presentRequest:
345 *oip = &apdu->u.presentRequest->otherInfo;
347 case Z_APDU_sortRequest:
348 *oip = &apdu->u.sortRequest->otherInfo;
350 case Z_APDU_scanRequest:
351 *oip = &apdu->u.scanRequest->otherInfo;
353 case Z_APDU_extendedServicesRequest:
354 *oip = &apdu->u.extendedServicesRequest->otherInfo;
356 case Z_APDU_deleteResultSetRequest:
357 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
359 case Z_APDU_initResponse:
360 *oip = &apdu->u.initResponse->otherInfo;
362 case Z_APDU_searchResponse:
363 *oip = &apdu->u.searchResponse->otherInfo;
365 case Z_APDU_presentResponse:
366 *oip = &apdu->u.presentResponse->otherInfo;
368 case Z_APDU_sortResponse:
369 *oip = &apdu->u.sortResponse->otherInfo;
371 case Z_APDU_scanResponse:
372 *oip = &apdu->u.scanResponse->otherInfo;
374 case Z_APDU_extendedServicesResponse:
375 *oip = &apdu->u.extendedServicesResponse->otherInfo;
377 case Z_APDU_deleteResultSetResponse:
378 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
386 void Z_Assoc::set_otherInformationString(
388 const Odr_oid *oid, int categoryValue, const char *str)
390 Z_OtherInformation **otherInformation;
391 get_otherInfoAPDU(apdu, &otherInformation);
392 if (!otherInformation)
394 set_otherInformationString(otherInformation, oid, categoryValue, str);
398 void Z_Assoc::set_otherInformationString (
399 Z_OtherInformation **otherInformation,
400 const Odr_oid *oid, int categoryValue, const char *str)
402 Z_OtherInformationUnit *oi =
403 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
406 oi->information.characterInfo = odr_strdup (odr_encode(), str);
409 Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
410 Z_OtherInformation **otherInformationP, int createFlag,
411 const Odr_oid *oid, int categoryValue, int deleteFlag)
413 return yaz_oi_update (otherInformationP,
414 (createFlag ? odr_encode() : 0),
415 oid, categoryValue, deleteFlag);
418 Z_ReferenceId* Z_Assoc::getRefID(char* str)
420 Z_ReferenceId* id = NULL;
424 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
425 id->size = id->len = strlen(str);
426 id->buf = (unsigned char *) str;
434 * c-file-style: "Stroustrup"
435 * indent-tabs-mode: nil
437 * vim: shiftwidth=4 tabstop=8 expandtab