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 : "unknown");
222 yaz_log(YLOG_LOG, "PDU dump:");
223 odr_dumpBER(yaz_log_file(), buf, len);
229 { // use YAZ log FILE
230 FILE *save = m_APDU_file;
232 odr_setprint(m_odr_print, yaz_log_file());
233 z_GDU(m_odr_print, &apdu, 0, "decode");
235 odr_setprint(m_odr_print, save);
239 z_GDU(m_odr_print, &apdu, 0, "decode");
246 int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
248 const char *element = 0;
249 int r = z_GDU(m_odr_out, &apdu, 0, 0);
251 if (!r) // decoding failed. Get the failed element
252 element = odr_getelement(m_odr_out);
254 if (m_APDU_yazlog || !r)
257 yaz_log (YLOG_LOG, "PDU encode failed. Element %s",
258 element ? element : "unknown");
259 FILE *save = m_APDU_file;
260 FILE *yazf = yaz_log_file();
261 odr_setprint(m_odr_print, yazf); // use YAZ log FILE
262 z_GDU(m_odr_print, &apdu, 0, "encode");
264 odr_setprint(m_odr_print, save);
269 fprintf (m_APDU_file, "PDU encode failed. Element %s",
270 element ? element : "unknown");
271 z_GDU(m_odr_print, &apdu, 0, "encode");
274 if (!r) // encoding failed
276 *buf = odr_getbuf (m_odr_out, len, 0);
277 odr_reset (m_odr_out);
281 const char *Z_Assoc::get_hostname()
286 int Z_Assoc::client(const char *addr)
288 delete [] m_hostname;
289 m_hostname = new char[strlen(addr)+1];
290 strcpy (m_hostname, addr);
291 return m_PDU_Observable->connect (this, addr);
294 void Z_Assoc::close()
296 m_PDU_Observable->close_session();
299 int Z_Assoc::server(const char *addr)
301 delete [] m_hostname;
302 m_hostname = new char[strlen(addr)+1];
303 strcpy (m_hostname, addr);
304 return m_PDU_Observable->listen (this, addr);
307 ODR Z_Assoc::odr_encode()
312 ODR Z_Assoc::odr_decode()
316 ODR Z_Assoc::odr_print()
321 void Z_Assoc::timeout(int timeout)
323 m_PDU_Observable->idleTime(timeout);
326 void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
330 case Z_APDU_initRequest:
331 *oip = &apdu->u.initRequest->otherInfo;
333 case Z_APDU_searchRequest:
334 *oip = &apdu->u.searchRequest->otherInfo;
336 case Z_APDU_presentRequest:
337 *oip = &apdu->u.presentRequest->otherInfo;
339 case Z_APDU_sortRequest:
340 *oip = &apdu->u.sortRequest->otherInfo;
342 case Z_APDU_scanRequest:
343 *oip = &apdu->u.scanRequest->otherInfo;
345 case Z_APDU_extendedServicesRequest:
346 *oip = &apdu->u.extendedServicesRequest->otherInfo;
348 case Z_APDU_deleteResultSetRequest:
349 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
351 case Z_APDU_initResponse:
352 *oip = &apdu->u.initResponse->otherInfo;
354 case Z_APDU_searchResponse:
355 *oip = &apdu->u.searchResponse->otherInfo;
357 case Z_APDU_presentResponse:
358 *oip = &apdu->u.presentResponse->otherInfo;
360 case Z_APDU_sortResponse:
361 *oip = &apdu->u.sortResponse->otherInfo;
363 case Z_APDU_scanResponse:
364 *oip = &apdu->u.scanResponse->otherInfo;
366 case Z_APDU_extendedServicesResponse:
367 *oip = &apdu->u.extendedServicesResponse->otherInfo;
369 case Z_APDU_deleteResultSetResponse:
370 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
378 void Z_Assoc::set_otherInformationString(
380 const Odr_oid *oid, int categoryValue, const char *str)
382 Z_OtherInformation **otherInformation;
383 get_otherInfoAPDU(apdu, &otherInformation);
384 if (!otherInformation)
386 set_otherInformationString(otherInformation, oid, categoryValue, str);
390 void Z_Assoc::set_otherInformationString (
391 Z_OtherInformation **otherInformation,
392 const Odr_oid *oid, int categoryValue, const char *str)
394 Z_OtherInformationUnit *oi =
395 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
398 oi->information.characterInfo = odr_strdup (odr_encode(), str);
401 Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
402 Z_OtherInformation **otherInformationP, int createFlag,
403 const Odr_oid *oid, int categoryValue, int deleteFlag)
405 return yaz_oi_update (otherInformationP,
406 (createFlag ? odr_encode() : 0),
407 oid, categoryValue, deleteFlag);
410 Z_ReferenceId* Z_Assoc::getRefID(char* str)
412 Z_ReferenceId* id = NULL;
416 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
417 id->size = id->len = strlen(str);
418 id->buf = (unsigned char *) str;
426 * c-file-style: "Stroustrup"
427 * indent-tabs-mode: nil
429 * vim: shiftwidth=4 tabstop=8 expandtab