Fixed crash that occurs when decoding fails.
[yazpp-moved-to-github.git] / src / yaz-z-assoc.cpp
1 /* This file is part of the yazpp toolkit.
2  * Copyright (C) 1998-2008 Index Data and Mike Taylor
3  * See the file LICENSE for details.
4  */
5
6 #include <assert.h>
7 #include <signal.h>
8
9 #include <yaz/log.h>
10 #include <yazpp/z-assoc.h>
11 #include <yaz/otherinfo.h>
12 #include <yaz/oid_db.h>
13
14 using namespace yazpp_1;
15
16 int Z_Assoc::yaz_init_func()
17 {
18 #ifndef WIN32
19     signal (SIGPIPE, SIG_IGN);
20 #endif
21     return 1;
22 }
23
24 int Z_Assoc::yaz_init_flag =  Z_Assoc::yaz_init_func();  
25
26 Z_Assoc::Z_Assoc(IPDU_Observable *the_PDU_Observable)
27 {
28     m_PDU_Observable = the_PDU_Observable;
29     m_odr_in = odr_createmem (ODR_DECODE);
30     m_odr_out = odr_createmem (ODR_ENCODE);
31     m_odr_print = odr_createmem (ODR_PRINT);
32     m_log = YLOG_DEBUG;
33     m_APDU_file = 0;
34     m_APDU_fname = 0;
35     m_hostname = 0;
36     m_APDU_yazlog = 0;
37 }
38
39 void Z_Assoc::set_APDU_log(const char *fname)
40 {
41     if (m_APDU_file && m_APDU_file != stderr)
42     {
43         fclose (m_APDU_file);
44         m_APDU_file = 0;
45     }
46     delete [] m_APDU_fname;
47     m_APDU_fname = 0;
48
49     if (fname) 
50     {
51         m_APDU_fname = new char[strlen(fname)+1];
52         strcpy (m_APDU_fname, fname);
53         if (!strcmp(fname, "-"))
54             m_APDU_file = stderr;
55         else if (*fname == '\0')
56             m_APDU_file = 0;
57         else
58             m_APDU_file = fopen (fname, "a");
59         odr_setprint(m_odr_print, m_APDU_file);
60     }
61 }
62
63 int Z_Assoc::set_APDU_yazlog(int v)
64 {
65     int old = m_APDU_yazlog;
66     m_APDU_yazlog = v;
67     return old;
68 }
69
70 const char *Z_Assoc::get_APDU_log()
71 {
72     return m_APDU_fname;
73 }
74
75 Z_Assoc::~Z_Assoc()
76 {
77     m_PDU_Observable->destroy();  
78     delete m_PDU_Observable;
79     odr_destroy (m_odr_print);     // note: also runs fclose on m_APDU_file ..
80     odr_destroy (m_odr_out);
81     odr_destroy (m_odr_in);
82     delete [] m_APDU_fname;
83     delete [] m_hostname;
84 }
85
86 void Z_Assoc::recv_PDU(const char *buf, int len)
87 {
88     yaz_log (m_log, "recv_PDU len=%d", len);
89     Z_GDU *apdu = decode_GDU (buf, len);
90     if (apdu)
91     {
92         recv_GDU (apdu, len);
93     }
94     else
95     {
96         close();
97     }
98 }
99
100 Z_APDU *Z_Assoc::create_Z_PDU(int type)
101 {
102     Z_APDU *apdu = zget_APDU(m_odr_out, type);
103     if (apdu->which == Z_APDU_initRequest)
104     {
105         Z_InitRequest * p = apdu->u.initRequest;
106         char *newName = (char*) odr_malloc(m_odr_out, 50);
107         strcpy (newName, p->implementationName);
108         strcat (newName, " YAZ++");
109         p->implementationName = newName;
110     }
111     return apdu;
112 }
113
114 Z_ReferenceId **Z_Assoc::get_referenceIdP(Z_APDU *apdu)
115 {
116     switch (apdu->which)
117     {
118     case  Z_APDU_initRequest:
119         return &apdu->u.initRequest->referenceId; 
120     case  Z_APDU_initResponse:
121         return &apdu->u.initResponse->referenceId;
122     case  Z_APDU_searchRequest:
123         return &apdu->u.searchRequest->referenceId;
124     case  Z_APDU_searchResponse:
125         return &apdu->u.searchResponse->referenceId;
126     case  Z_APDU_presentRequest:
127         return &apdu->u.presentRequest->referenceId;
128     case  Z_APDU_presentResponse:
129         return &apdu->u.presentResponse->referenceId;
130     case  Z_APDU_deleteResultSetRequest:
131         return &apdu->u.deleteResultSetRequest->referenceId;
132     case  Z_APDU_deleteResultSetResponse:
133         return &apdu->u.deleteResultSetResponse->referenceId;
134     case  Z_APDU_accessControlRequest:
135         return &apdu->u.accessControlRequest->referenceId;
136     case  Z_APDU_accessControlResponse:
137         return &apdu->u.accessControlResponse->referenceId;
138     case  Z_APDU_resourceControlRequest:
139         return &apdu->u.resourceControlRequest->referenceId;
140     case  Z_APDU_resourceControlResponse:
141         return &apdu->u.resourceControlResponse->referenceId;
142     case  Z_APDU_triggerResourceControlRequest:
143         return &apdu->u.triggerResourceControlRequest->referenceId;
144     case  Z_APDU_resourceReportRequest:
145         return &apdu->u.resourceReportRequest->referenceId;
146     case  Z_APDU_resourceReportResponse:
147         return &apdu->u.resourceReportResponse->referenceId;
148     case  Z_APDU_scanRequest:
149         return &apdu->u.scanRequest->referenceId;
150     case  Z_APDU_scanResponse:
151         return &apdu->u.scanResponse->referenceId;
152     case  Z_APDU_sortRequest:
153         return &apdu->u.sortRequest->referenceId;
154     case  Z_APDU_sortResponse:
155         return &apdu->u.sortResponse->referenceId;
156     case  Z_APDU_segmentRequest:
157         return &apdu->u.segmentRequest->referenceId;
158     case  Z_APDU_extendedServicesRequest:
159         return &apdu->u.extendedServicesRequest->referenceId;
160     case  Z_APDU_extendedServicesResponse:
161         return &apdu->u.extendedServicesResponse->referenceId;
162     case  Z_APDU_close:
163         return &apdu->u.close->referenceId;
164     }
165     return 0;
166 }
167
168 void Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
169 {
170     Z_ReferenceId **id_from = get_referenceIdP(from);
171     Z_ReferenceId **id_to = get_referenceIdP(to);
172     if (id_from && *id_from && id_to)
173     {
174         *id_to = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(**id_to));
175         (*id_to)->size = (*id_to)->len = (*id_from)->len;
176         (*id_to)->buf = (unsigned char*) odr_malloc (m_odr_out, (*id_to)->len);
177         memcpy ((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
178     }
179     else if (id_to)
180         *id_to = 0;
181 }
182
183 int Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
184 {
185     Z_GDU *gdu = (Z_GDU*) odr_malloc(odr_encode(), sizeof(*gdu));
186     gdu->which = Z_GDU_Z3950;
187     gdu->u.z3950 = apdu;
188     return send_GDU(gdu, plen);
189 }
190
191 int Z_Assoc::send_GDU(Z_GDU *apdu, int *plen)
192 {
193     char *buf;
194     int len;
195     if (encode_GDU(apdu, &buf, &len) > 0)
196     {
197         if (plen)
198             *plen = len;
199         return m_PDU_Observable->send_PDU(buf, len);
200     }
201     return -1;
202 }
203
204 Z_GDU *Z_Assoc::decode_GDU(const char *buf, int len)
205 {
206     Z_GDU *apdu;
207
208     odr_reset (m_odr_in);
209     odr_setbuf (m_odr_in, (char*) buf, len, 0);
210
211     if (!z_GDU(m_odr_in, &apdu, 0, 0))
212     {
213         const char *element = odr_getelement(m_odr_in);
214         yaz_log(YLOG_LOG, "PDU decode failed '%s' near byte %ld. Element %s",
215                 odr_errmsg(odr_geterror(m_odr_in)),
216                 (long) odr_offset(m_odr_in),
217                 element ? element : "unknown");
218         yaz_log(YLOG_LOG, "PDU dump:");
219         odr_dumpBER(yaz_log_file(), buf, len);
220         return 0;
221     }
222     else
223     {
224         if (m_APDU_yazlog)
225         {   // use YAZ log FILE
226             FILE *save = m_APDU_file;
227
228             odr_setprint(m_odr_print, yaz_log_file());
229             z_GDU(m_odr_print, &apdu, 0, "decode");
230             m_APDU_file = save;
231             odr_setprint(m_odr_print, save);
232         }
233         if (m_APDU_file)
234         {
235             z_GDU(m_odr_print, &apdu, 0, "decode");
236             fflush(m_APDU_file);
237         }
238         return apdu;
239     }
240 }
241
242 int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
243 {
244     const char *element = 0;
245     int r = z_GDU(m_odr_out, &apdu, 0, 0);
246
247     if (!r) // decoding failed. Get the failed element
248         element = odr_getelement(m_odr_out);
249     
250     if (m_APDU_yazlog || !r)
251     {
252         if (!r)
253             yaz_log (YLOG_LOG, "PDU encode failed. Element %s",
254                      element ? element : "unknown");
255         FILE *save = m_APDU_file;
256         FILE *yazf = yaz_log_file();
257         odr_setprint(m_odr_print, yazf); // use YAZ log FILE
258         z_GDU(m_odr_print, &apdu, 0, "encode");
259         m_APDU_file = save;
260         odr_setprint(m_odr_print, save);
261     }
262     if (m_APDU_file)
263     {
264         if (!r)
265             fprintf (m_APDU_file, "PDU encode failed. Element %s",
266                      element ? element : "unknown");
267         z_GDU(m_odr_print, &apdu, 0, "encode");
268         fflush(m_APDU_file);
269     }
270     if (!r)  // encoding failed
271         return -1;
272     *buf = odr_getbuf (m_odr_out, len, 0);
273     odr_reset (m_odr_out);
274     return *len;
275 }
276
277 const char *Z_Assoc::get_hostname()
278 {
279     return m_hostname;
280 }
281
282 int Z_Assoc::client(const char *addr)
283 {
284     delete [] m_hostname;
285     m_hostname = new char[strlen(addr)+1];
286     strcpy (m_hostname, addr);
287     return m_PDU_Observable->connect (this, addr);
288 }
289
290 void Z_Assoc::close()
291 {
292     m_PDU_Observable->close_session();
293 }
294
295 int Z_Assoc::server(const char *addr)
296 {
297     delete [] m_hostname;
298     m_hostname = new char[strlen(addr)+1];
299     strcpy (m_hostname, addr);
300     return m_PDU_Observable->listen (this, addr);
301 }
302
303 ODR Z_Assoc::odr_encode()
304 {
305     return m_odr_out;
306 }
307
308 ODR Z_Assoc::odr_decode()
309 {
310     return m_odr_in;
311 }
312 ODR Z_Assoc::odr_print()
313 {
314     return m_odr_print;
315 }
316
317 void Z_Assoc::timeout(int timeout)
318 {
319     m_PDU_Observable->idleTime(timeout);
320 }
321
322 void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
323 {
324     switch (apdu->which)
325     {
326     case Z_APDU_initRequest:
327         *oip = &apdu->u.initRequest->otherInfo;
328         break;
329     case Z_APDU_searchRequest:
330         *oip = &apdu->u.searchRequest->otherInfo;
331         break;
332     case Z_APDU_presentRequest:
333         *oip = &apdu->u.presentRequest->otherInfo;
334         break;
335     case Z_APDU_sortRequest:
336         *oip = &apdu->u.sortRequest->otherInfo;
337         break;
338     case Z_APDU_scanRequest:
339         *oip = &apdu->u.scanRequest->otherInfo;
340         break;
341     case Z_APDU_extendedServicesRequest:
342         *oip = &apdu->u.extendedServicesRequest->otherInfo;
343         break;
344     case Z_APDU_deleteResultSetRequest:
345         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
346         break;
347     case Z_APDU_initResponse:
348         *oip = &apdu->u.initResponse->otherInfo;
349         break;
350     case Z_APDU_searchResponse:
351         *oip = &apdu->u.searchResponse->otherInfo;
352         break;
353     case Z_APDU_presentResponse:
354         *oip = &apdu->u.presentResponse->otherInfo;
355         break;
356     case Z_APDU_sortResponse:
357         *oip = &apdu->u.sortResponse->otherInfo;
358         break;
359     case Z_APDU_scanResponse:
360         *oip = &apdu->u.scanResponse->otherInfo;
361         break;
362     case Z_APDU_extendedServicesResponse:
363         *oip = &apdu->u.extendedServicesResponse->otherInfo;
364         break;
365     case Z_APDU_deleteResultSetResponse:
366         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
367         break;
368     default:
369         *oip = 0;
370         break;
371     }
372 }
373
374 void Z_Assoc::set_otherInformationString(
375     Z_APDU *apdu,
376     const Odr_oid *oid, int categoryValue, const char *str)
377 {
378     Z_OtherInformation **otherInformation;
379     get_otherInfoAPDU(apdu, &otherInformation);
380     if (!otherInformation)
381         return;
382     set_otherInformationString(otherInformation, oid, categoryValue, str);
383 }
384
385
386 void Z_Assoc::set_otherInformationString (
387     Z_OtherInformation **otherInformation,
388     const Odr_oid *oid, int categoryValue, const char *str)
389 {
390     Z_OtherInformationUnit *oi =
391         update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
392     if (!oi)
393         return;
394     oi->information.characterInfo = odr_strdup (odr_encode(), str);
395 }
396
397 Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
398     Z_OtherInformation **otherInformationP, int createFlag,
399     const Odr_oid *oid, int categoryValue, int deleteFlag)
400 {
401     return yaz_oi_update (otherInformationP,
402                           (createFlag ? odr_encode() : 0),
403                           oid, categoryValue, deleteFlag);
404 }
405
406 Z_ReferenceId* Z_Assoc::getRefID(char* str)
407 {
408     Z_ReferenceId* id = NULL;
409
410     if (str)
411     {
412         id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
413         id->size = id->len = strlen(str);
414         id->buf = (unsigned char *) str;
415     }
416     return id;
417 }
418
419 /*
420  * Local variables:
421  * c-basic-offset: 4
422  * indent-tabs-mode: nil
423  * End:
424  * vim: shiftwidth=4 tabstop=8 expandtab
425  */
426