Fix a bug WRT APDU logging
[yazpp-moved-to-github.git] / src / yaz-z-assoc.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-z-assoc.cpp,v 1.31 2003-10-23 13:49:58 adam Exp $
6  */
7
8 #include <assert.h>
9 #include <signal.h>
10
11 #include <yaz/log.h>
12 #include <yaz++/z-assoc.h>
13 #include <yaz/otherinfo.h>
14
15 int Yaz_Z_Assoc::yaz_init_func()
16 {
17 #ifndef WIN32
18     signal (SIGPIPE, SIG_IGN);
19 #endif
20     return 1;
21 }
22
23 int Yaz_Z_Assoc::yaz_init_flag =  Yaz_Z_Assoc::yaz_init_func();  
24
25 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
26 {
27     m_PDU_Observable = the_PDU_Observable;
28     m_odr_in = odr_createmem (ODR_DECODE);
29     m_odr_out = odr_createmem (ODR_ENCODE);
30     m_odr_print = odr_createmem (ODR_PRINT);
31     m_log = LOG_DEBUG;
32     m_APDU_file = 0;
33     m_APDU_fname = 0;
34     m_hostname = 0;
35     m_APDU_yazlog = 0;
36 }
37
38 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
39 {
40     if (m_APDU_file && m_APDU_file != stderr)
41     {
42         fclose (m_APDU_file);
43         m_APDU_file = 0;
44     }
45     delete [] m_APDU_fname;
46     m_APDU_fname = 0;
47
48     if (fname) 
49     {
50         m_APDU_fname = new char[strlen(fname)+1];
51         strcpy (m_APDU_fname, fname);
52         if (!strcmp(fname, "-"))
53             m_APDU_file = stderr;
54         else if (*fname == '\0')
55             m_APDU_file = 0;
56         else
57             m_APDU_file = fopen (fname, "a");
58         odr_setprint(m_odr_print, m_APDU_file);
59     }
60 }
61
62 int Yaz_Z_Assoc::set_APDU_yazlog(int v)
63 {
64     int old = m_APDU_yazlog;
65     m_APDU_yazlog = v;
66     return old;
67 }
68
69 const char *Yaz_Z_Assoc::get_APDU_log()
70 {
71     return m_APDU_fname;
72 }
73
74 Yaz_Z_Assoc::~Yaz_Z_Assoc()
75 {
76     m_PDU_Observable->destroy();  
77     delete m_PDU_Observable;
78     odr_destroy (m_odr_print);     // note: also runs fclose on m_APDU_file ..
79     odr_destroy (m_odr_out);
80     odr_destroy (m_odr_in);
81     delete [] m_APDU_fname;
82     delete [] m_hostname;
83 }
84
85 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
86 {
87     yaz_log (m_log, "recv_PDU len=%d", len);
88     Z_APDU *apdu = decode_Z_PDU (buf, len);
89     if (apdu)
90     {
91         recv_Z_PDU (apdu, len);
92     }
93     else
94     {
95         close();
96         failNotify();
97     }
98 }
99
100 Z_APDU *Yaz_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 **Yaz_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 Yaz_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 Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
184 {
185     char *buf;
186     int len;
187     if (encode_Z_PDU(apdu, &buf, &len) > 0)
188     {
189         if (plen)
190             *plen = len;
191         return m_PDU_Observable->send_PDU(buf, len);
192     }
193     return -1;
194 }
195
196 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
197 {
198     Z_APDU *apdu;
199
200     odr_reset (m_odr_in);
201     odr_setbuf (m_odr_in, (char*) buf, len, 0);
202
203     if (!z_APDU(m_odr_in, &apdu, 0, 0))
204     {
205         yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
206              odr_errmsg(odr_geterror(m_odr_in)),
207              odr_offset(m_odr_in));
208         yaz_log(LOG_LOG, "PDU dump:");
209         odr_dumpBER(yaz_log_file(), buf, len);
210         return 0;
211     }
212     else
213     {
214         if (m_APDU_yazlog)
215         {   // use YAZ log FILE
216             FILE *save = m_APDU_file;
217
218             odr_setprint(m_odr_print, yaz_log_file());
219             z_APDU(m_odr_print, &apdu, 0, "decode");
220             m_APDU_file = save;
221             odr_setprint(m_odr_print, save);
222         }
223         if (m_APDU_file)
224         {
225             z_APDU(m_odr_print, &apdu, 0, "decode");
226             fflush(m_APDU_file);
227         }
228         return apdu;
229     }
230 }
231
232 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
233 {
234     if (m_APDU_yazlog)
235     {
236         FILE *save = m_APDU_file;
237         odr_setprint(m_odr_print, yaz_log_file()); // use YAZ log FILE
238         z_APDU(m_odr_print, &apdu, 0, "encode");
239         m_APDU_file = save;
240         odr_setprint(m_odr_print, save);
241     }
242     if (m_APDU_file)
243     {
244         z_APDU(m_odr_print, &apdu, 0, "encode");
245         fflush(m_APDU_file);
246     }
247     if (!z_APDU(m_odr_out, &apdu, 0, 0))
248     {
249         if (m_APDU_file)
250             fprintf (m_APDU_file, "PDU encode failed (above)");
251         yaz_log (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
252         return -1;
253     }
254     *buf = odr_getbuf (m_odr_out, len, 0);
255     odr_reset (m_odr_out);
256     return *len;
257 }
258
259 const char *Yaz_Z_Assoc::get_hostname()
260 {
261     return m_hostname;
262 }
263
264 int Yaz_Z_Assoc::client(const char *addr)
265 {
266     delete [] m_hostname;
267     m_hostname = new char[strlen(addr)+1];
268     strcpy (m_hostname, addr);
269     return m_PDU_Observable->connect (this, addr);
270 }
271
272 void Yaz_Z_Assoc::close()
273 {
274     m_PDU_Observable->close ();
275 }
276
277 int Yaz_Z_Assoc::server(const char *addr)
278 {
279     delete [] m_hostname;
280     m_hostname = new char[strlen(addr)+1];
281     strcpy (m_hostname, addr);
282     return m_PDU_Observable->listen (this, addr);
283 }
284
285 ODR Yaz_Z_Assoc::odr_encode()
286 {
287     return m_odr_out;
288 }
289
290 ODR Yaz_Z_Assoc::odr_decode()
291 {
292     return m_odr_in;
293 }
294 ODR Yaz_Z_Assoc::odr_print()
295 {
296     return m_odr_print;
297 }
298
299 void Yaz_Z_Assoc::timeout(int timeout)
300 {
301     m_PDU_Observable->idleTime(timeout);
302 }
303
304 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
305 {
306     switch (apdu->which)
307     {
308     case Z_APDU_initRequest:
309         *oip = &apdu->u.initRequest->otherInfo;
310         break;
311     case Z_APDU_searchRequest:
312         *oip = &apdu->u.searchRequest->otherInfo;
313         break;
314     case Z_APDU_presentRequest:
315         *oip = &apdu->u.presentRequest->otherInfo;
316         break;
317     case Z_APDU_sortRequest:
318         *oip = &apdu->u.sortRequest->otherInfo;
319         break;
320     case Z_APDU_scanRequest:
321         *oip = &apdu->u.scanRequest->otherInfo;
322         break;
323     case Z_APDU_extendedServicesRequest:
324         *oip = &apdu->u.extendedServicesRequest->otherInfo;
325         break;
326     case Z_APDU_deleteResultSetRequest:
327         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
328         break;
329     case Z_APDU_initResponse:
330         *oip = &apdu->u.initResponse->otherInfo;
331         break;
332     case Z_APDU_searchResponse:
333         *oip = &apdu->u.searchResponse->otherInfo;
334         break;
335     case Z_APDU_presentResponse:
336         *oip = &apdu->u.presentResponse->otherInfo;
337         break;
338     case Z_APDU_sortResponse:
339         *oip = &apdu->u.sortResponse->otherInfo;
340         break;
341     case Z_APDU_scanResponse:
342         *oip = &apdu->u.scanResponse->otherInfo;
343         break;
344     case Z_APDU_extendedServicesResponse:
345         *oip = &apdu->u.extendedServicesResponse->otherInfo;
346         break;
347     case Z_APDU_deleteResultSetResponse:
348         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
349         break;
350     default:
351         *oip = 0;
352         break;
353     }
354 }
355
356 void Yaz_Z_Assoc::set_otherInformationString (
357     Z_APDU *apdu,
358     int oidval, int categoryValue,
359     const char *str)
360 {
361     Z_OtherInformation **otherInformation;
362     get_otherInfoAPDU(apdu, &otherInformation);
363     if (!otherInformation)
364         return;
365     set_otherInformationString(otherInformation, oidval, categoryValue, str);
366 }
367
368 void Yaz_Z_Assoc::set_otherInformationString (
369     Z_OtherInformation **otherInformation,
370     int oidval, int categoryValue,
371     const char *str)
372 {
373     int oid[OID_SIZE];
374     struct oident ent;
375     ent.proto = PROTO_Z3950;
376     ent.oclass = CLASS_USERINFO;
377     ent.value = (oid_value) oidval;
378     if (!oid_ent_to_oid (&ent, oid))
379         return ;
380     set_otherInformationString(otherInformation, oid, categoryValue, str);
381 }
382
383 void Yaz_Z_Assoc::set_otherInformationString (
384     Z_OtherInformation **otherInformation,
385     int *oid, int categoryValue, const char *str)
386 {
387     Z_OtherInformationUnit *oi =
388         update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
389     if (!oi)
390         return;
391     oi->information.characterInfo = odr_strdup (odr_encode(), str);
392 }
393
394 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
395     Z_OtherInformation **otherInformationP, int createFlag,
396     int *oid, int categoryValue, int deleteFlag)
397 {
398     return yaz_oi_update (otherInformationP,
399                           (createFlag ? odr_encode() : 0),
400                           oid, categoryValue, deleteFlag);
401 }
402
403 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
404 {
405     Z_ReferenceId* id = NULL;
406
407     if (str)
408     {
409         id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
410         id->size = id->len = strlen(str);
411         id->buf = (unsigned char *) str;
412     }
413     return id;
414 }
415