Happy new year
[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 : "unknown");
222         yaz_log(YLOG_LOG, "PDU dump:");
223         odr_dumpBER(yaz_log_file(), buf, len);
224         return 0;
225     }
226     else
227     {
228         if (m_APDU_yazlog)
229         {   // use YAZ log FILE
230             FILE *save = m_APDU_file;
231
232             odr_setprint(m_odr_print, yaz_log_file());
233             z_GDU(m_odr_print, &apdu, 0, "decode");
234             m_APDU_file = save;
235             odr_setprint(m_odr_print, save);
236         }
237         if (m_APDU_file)
238         {
239             z_GDU(m_odr_print, &apdu, 0, "decode");
240             fflush(m_APDU_file);
241         }
242         return apdu;
243     }
244 }
245
246 int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
247 {
248     const char *element = 0;
249     int r = z_GDU(m_odr_out, &apdu, 0, 0);
250
251     if (!r) // decoding failed. Get the failed element
252         element = odr_getelement(m_odr_out);
253     
254     if (m_APDU_yazlog || !r)
255     {
256         if (!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");
263         m_APDU_file = save;
264         odr_setprint(m_odr_print, save);
265     }
266     if (m_APDU_file)
267     {
268         if (!r)
269             fprintf (m_APDU_file, "PDU encode failed. Element %s",
270                      element ? element : "unknown");
271         z_GDU(m_odr_print, &apdu, 0, "encode");
272         fflush(m_APDU_file);
273     }
274     if (!r)  // encoding failed
275         return -1;
276     *buf = odr_getbuf (m_odr_out, len, 0);
277     odr_reset (m_odr_out);
278     return *len;
279 }
280
281 const char *Z_Assoc::get_hostname()
282 {
283     return m_hostname;
284 }
285
286 int Z_Assoc::client(const char *addr)
287 {
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);
292 }
293
294 void Z_Assoc::close()
295 {
296     m_PDU_Observable->close_session();
297 }
298
299 int Z_Assoc::server(const char *addr)
300 {
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);
305 }
306
307 ODR Z_Assoc::odr_encode()
308 {
309     return m_odr_out;
310 }
311
312 ODR Z_Assoc::odr_decode()
313 {
314     return m_odr_in;
315 }
316 ODR Z_Assoc::odr_print()
317 {
318     return m_odr_print;
319 }
320
321 void Z_Assoc::timeout(int timeout)
322 {
323     m_PDU_Observable->idleTime(timeout);
324 }
325
326 void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
327 {
328     switch (apdu->which)
329     {
330     case Z_APDU_initRequest:
331         *oip = &apdu->u.initRequest->otherInfo;
332         break;
333     case Z_APDU_searchRequest:
334         *oip = &apdu->u.searchRequest->otherInfo;
335         break;
336     case Z_APDU_presentRequest:
337         *oip = &apdu->u.presentRequest->otherInfo;
338         break;
339     case Z_APDU_sortRequest:
340         *oip = &apdu->u.sortRequest->otherInfo;
341         break;
342     case Z_APDU_scanRequest:
343         *oip = &apdu->u.scanRequest->otherInfo;
344         break;
345     case Z_APDU_extendedServicesRequest:
346         *oip = &apdu->u.extendedServicesRequest->otherInfo;
347         break;
348     case Z_APDU_deleteResultSetRequest:
349         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
350         break;
351     case Z_APDU_initResponse:
352         *oip = &apdu->u.initResponse->otherInfo;
353         break;
354     case Z_APDU_searchResponse:
355         *oip = &apdu->u.searchResponse->otherInfo;
356         break;
357     case Z_APDU_presentResponse:
358         *oip = &apdu->u.presentResponse->otherInfo;
359         break;
360     case Z_APDU_sortResponse:
361         *oip = &apdu->u.sortResponse->otherInfo;
362         break;
363     case Z_APDU_scanResponse:
364         *oip = &apdu->u.scanResponse->otherInfo;
365         break;
366     case Z_APDU_extendedServicesResponse:
367         *oip = &apdu->u.extendedServicesResponse->otherInfo;
368         break;
369     case Z_APDU_deleteResultSetResponse:
370         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
371         break;
372     default:
373         *oip = 0;
374         break;
375     }
376 }
377
378 void Z_Assoc::set_otherInformationString(
379     Z_APDU *apdu,
380     const Odr_oid *oid, int categoryValue, const char *str)
381 {
382     Z_OtherInformation **otherInformation;
383     get_otherInfoAPDU(apdu, &otherInformation);
384     if (!otherInformation)
385         return;
386     set_otherInformationString(otherInformation, oid, categoryValue, str);
387 }
388
389
390 void Z_Assoc::set_otherInformationString (
391     Z_OtherInformation **otherInformation,
392     const Odr_oid *oid, int categoryValue, const char *str)
393 {
394     Z_OtherInformationUnit *oi =
395         update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
396     if (!oi)
397         return;
398     oi->information.characterInfo = odr_strdup (odr_encode(), str);
399 }
400
401 Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
402     Z_OtherInformation **otherInformationP, int createFlag,
403     const Odr_oid *oid, int categoryValue, int deleteFlag)
404 {
405     return yaz_oi_update (otherInformationP,
406                           (createFlag ? odr_encode() : 0),
407                           oid, categoryValue, deleteFlag);
408 }
409
410 Z_ReferenceId* Z_Assoc::getRefID(char* str)
411 {
412     Z_ReferenceId* id = NULL;
413
414     if (str)
415     {
416         id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
417         id->size = id->len = strlen(str);
418         id->buf = (unsigned char *) str;
419     }
420     return id;
421 }
422
423 /*
424  * Local variables:
425  * c-basic-offset: 4
426  * c-file-style: "Stroustrup"
427  * indent-tabs-mode: nil
428  * End:
429  * vim: shiftwidth=4 tabstop=8 expandtab
430  */
431