Reformat: delete trailing whitespace
[yazpp-moved-to-github.git] / src / yaz-z-assoc.cpp
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.
4  */
5
6 #if HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9 #include <assert.h>
10 #include <signal.h>
11
12 #include <yaz/log.h>
13 #include <yazpp/z-assoc.h>
14 #include <yaz/otherinfo.h>
15 #include <yaz/oid_db.h>
16
17 using namespace yazpp_1;
18
19 int Z_Assoc::yaz_init_func()
20 {
21 #ifndef WIN32
22     signal (SIGPIPE, SIG_IGN);
23 #endif
24     return 1;
25 }
26
27 int Z_Assoc::yaz_init_flag =  Z_Assoc::yaz_init_func();
28
29 Z_Assoc::Z_Assoc(IPDU_Observable *the_PDU_Observable)
30 {
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);
35     m_log = YLOG_DEBUG;
36     m_APDU_file = 0;
37     m_APDU_fname = 0;
38     m_hostname = 0;
39     m_APDU_yazlog = 0;
40 }
41
42 void Z_Assoc::set_APDU_log(const char *fname)
43 {
44     if (m_APDU_file && m_APDU_file != stderr)
45     {
46         fclose (m_APDU_file);
47         m_APDU_file = 0;
48     }
49     delete [] m_APDU_fname;
50     m_APDU_fname = 0;
51
52     if (fname)
53     {
54         m_APDU_fname = new char[strlen(fname)+1];
55         strcpy (m_APDU_fname, fname);
56         if (!strcmp(fname, "-"))
57             m_APDU_file = stderr;
58         else if (*fname == '\0')
59             m_APDU_file = 0;
60         else
61             m_APDU_file = fopen (fname, "a");
62         odr_setprint(m_odr_print, m_APDU_file);
63     }
64 }
65
66 int Z_Assoc::set_APDU_yazlog(int v)
67 {
68     int old = m_APDU_yazlog;
69     m_APDU_yazlog = v;
70     return old;
71 }
72
73 const char *Z_Assoc::get_APDU_log()
74 {
75     return m_APDU_fname;
76 }
77
78 Z_Assoc::~Z_Assoc()
79 {
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;
86     delete [] m_hostname;
87 }
88
89 void Z_Assoc::recv_PDU(const char *buf, int len)
90 {
91     yaz_log (m_log, "recv_PDU len=%d", len);
92     Z_GDU *apdu = decode_GDU (buf, len);
93     if (apdu)
94     {
95         recv_GDU (apdu, len);
96     }
97     else
98     {
99         m_PDU_Observable->shutdown();
100         failNotify();
101     }
102 }
103
104 Z_APDU *Z_Assoc::create_Z_PDU(int type)
105 {
106     Z_APDU *apdu = zget_APDU(m_odr_out, type);
107     if (apdu->which == Z_APDU_initRequest)
108     {
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;
114     }
115     return apdu;
116 }
117
118 Z_ReferenceId **Z_Assoc::get_referenceIdP(Z_APDU *apdu)
119 {
120     switch (apdu->which)
121     {
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;
166     case  Z_APDU_close:
167         return &apdu->u.close->referenceId;
168     }
169     return 0;
170 }
171
172 void Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
173 {
174     Z_ReferenceId **id_from = get_referenceIdP(from);
175     Z_ReferenceId **id_to = get_referenceIdP(to);
176     if (id_from && *id_from && id_to)
177     {
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);
182     }
183     else if (id_to)
184         *id_to = 0;
185 }
186
187 int Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
188 {
189     Z_GDU *gdu = (Z_GDU*) odr_malloc(odr_encode(), sizeof(*gdu));
190     gdu->which = Z_GDU_Z3950;
191     gdu->u.z3950 = apdu;
192     return send_GDU(gdu, plen);
193 }
194
195 int Z_Assoc::send_GDU(Z_GDU *apdu, int *plen)
196 {
197     char *buf;
198     int len;
199     if (encode_GDU(apdu, &buf, &len) > 0)
200     {
201         if (plen)
202             *plen = len;
203         return m_PDU_Observable->send_PDU(buf, len);
204     }
205     return -1;
206 }
207
208 Z_GDU *Z_Assoc::decode_GDU(const char *buf, int len)
209 {
210     Z_GDU *apdu;
211
212     odr_reset (m_odr_in);
213     odr_setbuf (m_odr_in, (char*) buf, len, 0);
214
215     if (!z_GDU(m_odr_in, &apdu, 0, 0))
216     {
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);
223         if (len > 0)
224         {
225             WRBUF w = wrbuf_alloc();
226             wrbuf_write_escaped(w, buf, len > 1024 ? 1024 : len);
227             yaz_log(YLOG_LOG, "Buffer bytes: %s", wrbuf_cstr(w));
228             wrbuf_destroy(w);
229         }
230         yaz_log(YLOG_LOG, "PDU dump:");
231         odr_dumpBER(yaz_log_file(), buf, len);
232         return 0;
233     }
234     else
235     {
236         if (m_APDU_yazlog)
237         {   // use YAZ log FILE
238             FILE *save = m_APDU_file;
239
240             odr_setprint(m_odr_print, yaz_log_file());
241             z_GDU(m_odr_print, &apdu, 0, "decode");
242             m_APDU_file = save;
243             odr_setprint(m_odr_print, save);
244         }
245         if (m_APDU_file)
246         {
247             z_GDU(m_odr_print, &apdu, 0, "decode");
248             fflush(m_APDU_file);
249         }
250         return apdu;
251     }
252 }
253
254 int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
255 {
256     const char *element = 0;
257     int r = z_GDU(m_odr_out, &apdu, 0, 0);
258
259     if (!r) // decoding failed. Get the failed element
260         element = odr_getelement(m_odr_out);
261
262     if (m_APDU_yazlog || !r)
263     {
264         if (!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");
271         m_APDU_file = save;
272         odr_setprint(m_odr_print, save);
273     }
274     if (m_APDU_file)
275     {
276         if (!r)
277             fprintf (m_APDU_file, "PDU encode failed. Element %s",
278                      element ? element : "unknown");
279         z_GDU(m_odr_print, &apdu, 0, "encode");
280         fflush(m_APDU_file);
281     }
282     if (!r)  // encoding failed
283         return -1;
284     *buf = odr_getbuf (m_odr_out, len, 0);
285     odr_reset (m_odr_out);
286     return *len;
287 }
288
289 const char *Z_Assoc::get_hostname()
290 {
291     return m_hostname;
292 }
293
294 int Z_Assoc::client(const char *addr)
295 {
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);
300 }
301
302 void Z_Assoc::close()
303 {
304     m_PDU_Observable->close_session();
305 }
306
307 int Z_Assoc::server(const char *addr)
308 {
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);
313 }
314
315 ODR Z_Assoc::odr_encode()
316 {
317     return m_odr_out;
318 }
319
320 ODR Z_Assoc::odr_decode()
321 {
322     return m_odr_in;
323 }
324 ODR Z_Assoc::odr_print()
325 {
326     return m_odr_print;
327 }
328
329 void Z_Assoc::timeout(int timeout)
330 {
331     m_PDU_Observable->idleTime(timeout);
332 }
333
334 void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
335 {
336     switch (apdu->which)
337     {
338     case Z_APDU_initRequest:
339         *oip = &apdu->u.initRequest->otherInfo;
340         break;
341     case Z_APDU_searchRequest:
342         *oip = &apdu->u.searchRequest->otherInfo;
343         break;
344     case Z_APDU_presentRequest:
345         *oip = &apdu->u.presentRequest->otherInfo;
346         break;
347     case Z_APDU_sortRequest:
348         *oip = &apdu->u.sortRequest->otherInfo;
349         break;
350     case Z_APDU_scanRequest:
351         *oip = &apdu->u.scanRequest->otherInfo;
352         break;
353     case Z_APDU_extendedServicesRequest:
354         *oip = &apdu->u.extendedServicesRequest->otherInfo;
355         break;
356     case Z_APDU_deleteResultSetRequest:
357         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
358         break;
359     case Z_APDU_initResponse:
360         *oip = &apdu->u.initResponse->otherInfo;
361         break;
362     case Z_APDU_searchResponse:
363         *oip = &apdu->u.searchResponse->otherInfo;
364         break;
365     case Z_APDU_presentResponse:
366         *oip = &apdu->u.presentResponse->otherInfo;
367         break;
368     case Z_APDU_sortResponse:
369         *oip = &apdu->u.sortResponse->otherInfo;
370         break;
371     case Z_APDU_scanResponse:
372         *oip = &apdu->u.scanResponse->otherInfo;
373         break;
374     case Z_APDU_extendedServicesResponse:
375         *oip = &apdu->u.extendedServicesResponse->otherInfo;
376         break;
377     case Z_APDU_deleteResultSetResponse:
378         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
379         break;
380     default:
381         *oip = 0;
382         break;
383     }
384 }
385
386 void Z_Assoc::set_otherInformationString(
387     Z_APDU *apdu,
388     const Odr_oid *oid, int categoryValue, const char *str)
389 {
390     Z_OtherInformation **otherInformation;
391     get_otherInfoAPDU(apdu, &otherInformation);
392     if (!otherInformation)
393         return;
394     set_otherInformationString(otherInformation, oid, categoryValue, str);
395 }
396
397
398 void Z_Assoc::set_otherInformationString (
399     Z_OtherInformation **otherInformation,
400     const Odr_oid *oid, int categoryValue, const char *str)
401 {
402     Z_OtherInformationUnit *oi =
403         update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
404     if (!oi)
405         return;
406     oi->information.characterInfo = odr_strdup (odr_encode(), str);
407 }
408
409 Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
410     Z_OtherInformation **otherInformationP, int createFlag,
411     const Odr_oid *oid, int categoryValue, int deleteFlag)
412 {
413     return yaz_oi_update (otherInformationP,
414                           (createFlag ? odr_encode() : 0),
415                           oid, categoryValue, deleteFlag);
416 }
417
418 Z_ReferenceId* Z_Assoc::getRefID(char* str)
419 {
420     Z_ReferenceId* id = NULL;
421
422     if (str)
423     {
424         id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
425         id->size = id->len = strlen(str);
426         id->buf = (unsigned char *) str;
427     }
428     return id;
429 }
430
431 /*
432  * Local variables:
433  * c-basic-offset: 4
434  * c-file-style: "Stroustrup"
435  * indent-tabs-mode: nil
436  * End:
437  * vim: shiftwidth=4 tabstop=8 expandtab
438  */
439