72a3d412b1cdd61570d6fa7c3078bdc4fdc4f86e
[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.32 2003-12-16 14:17:01 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_GDU *apdu = decode_GDU (buf, len);
89     if (apdu)
90     {
91         recv_GDU (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     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 Yaz_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 *Yaz_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         yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
214              odr_errmsg(odr_geterror(m_odr_in)),
215              odr_offset(m_odr_in));
216         yaz_log(LOG_LOG, "PDU dump:");
217         odr_dumpBER(yaz_log_file(), buf, len);
218         return 0;
219     }
220     else
221     {
222         if (m_APDU_yazlog)
223         {   // use YAZ log FILE
224             FILE *save = m_APDU_file;
225
226             odr_setprint(m_odr_print, yaz_log_file());
227             z_GDU(m_odr_print, &apdu, 0, "decode");
228             m_APDU_file = save;
229             odr_setprint(m_odr_print, save);
230         }
231         if (m_APDU_file)
232         {
233             z_GDU(m_odr_print, &apdu, 0, "decode");
234             fflush(m_APDU_file);
235         }
236         return apdu;
237     }
238 }
239
240 int Yaz_Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
241 {
242     if (m_APDU_yazlog)
243     {
244         FILE *save = m_APDU_file;
245         odr_setprint(m_odr_print, yaz_log_file()); // use YAZ log FILE
246         z_GDU(m_odr_print, &apdu, 0, "encode");
247         m_APDU_file = save;
248         odr_setprint(m_odr_print, save);
249     }
250     if (m_APDU_file)
251     {
252         z_GDU(m_odr_print, &apdu, 0, "encode");
253         fflush(m_APDU_file);
254     }
255     if (!z_GDU(m_odr_out, &apdu, 0, 0))
256     {
257         if (m_APDU_file)
258             fprintf (m_APDU_file, "PDU encode failed (above)");
259         yaz_log (LOG_LOG, "yaz_Z_Assoc::encode_Z_GDU failed");
260         return -1;
261     }
262     *buf = odr_getbuf (m_odr_out, len, 0);
263     odr_reset (m_odr_out);
264     return *len;
265 }
266
267 const char *Yaz_Z_Assoc::get_hostname()
268 {
269     return m_hostname;
270 }
271
272 int Yaz_Z_Assoc::client(const char *addr)
273 {
274     delete [] m_hostname;
275     m_hostname = new char[strlen(addr)+1];
276     strcpy (m_hostname, addr);
277     return m_PDU_Observable->connect (this, addr);
278 }
279
280 void Yaz_Z_Assoc::close()
281 {
282     m_PDU_Observable->close ();
283 }
284
285 int Yaz_Z_Assoc::server(const char *addr)
286 {
287     delete [] m_hostname;
288     m_hostname = new char[strlen(addr)+1];
289     strcpy (m_hostname, addr);
290     return m_PDU_Observable->listen (this, addr);
291 }
292
293 ODR Yaz_Z_Assoc::odr_encode()
294 {
295     return m_odr_out;
296 }
297
298 ODR Yaz_Z_Assoc::odr_decode()
299 {
300     return m_odr_in;
301 }
302 ODR Yaz_Z_Assoc::odr_print()
303 {
304     return m_odr_print;
305 }
306
307 void Yaz_Z_Assoc::timeout(int timeout)
308 {
309     m_PDU_Observable->idleTime(timeout);
310 }
311
312 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
313 {
314     switch (apdu->which)
315     {
316     case Z_APDU_initRequest:
317         *oip = &apdu->u.initRequest->otherInfo;
318         break;
319     case Z_APDU_searchRequest:
320         *oip = &apdu->u.searchRequest->otherInfo;
321         break;
322     case Z_APDU_presentRequest:
323         *oip = &apdu->u.presentRequest->otherInfo;
324         break;
325     case Z_APDU_sortRequest:
326         *oip = &apdu->u.sortRequest->otherInfo;
327         break;
328     case Z_APDU_scanRequest:
329         *oip = &apdu->u.scanRequest->otherInfo;
330         break;
331     case Z_APDU_extendedServicesRequest:
332         *oip = &apdu->u.extendedServicesRequest->otherInfo;
333         break;
334     case Z_APDU_deleteResultSetRequest:
335         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
336         break;
337     case Z_APDU_initResponse:
338         *oip = &apdu->u.initResponse->otherInfo;
339         break;
340     case Z_APDU_searchResponse:
341         *oip = &apdu->u.searchResponse->otherInfo;
342         break;
343     case Z_APDU_presentResponse:
344         *oip = &apdu->u.presentResponse->otherInfo;
345         break;
346     case Z_APDU_sortResponse:
347         *oip = &apdu->u.sortResponse->otherInfo;
348         break;
349     case Z_APDU_scanResponse:
350         *oip = &apdu->u.scanResponse->otherInfo;
351         break;
352     case Z_APDU_extendedServicesResponse:
353         *oip = &apdu->u.extendedServicesResponse->otherInfo;
354         break;
355     case Z_APDU_deleteResultSetResponse:
356         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
357         break;
358     default:
359         *oip = 0;
360         break;
361     }
362 }
363
364 void Yaz_Z_Assoc::set_otherInformationString (
365     Z_APDU *apdu,
366     int oidval, int categoryValue,
367     const char *str)
368 {
369     Z_OtherInformation **otherInformation;
370     get_otherInfoAPDU(apdu, &otherInformation);
371     if (!otherInformation)
372         return;
373     set_otherInformationString(otherInformation, oidval, categoryValue, str);
374 }
375
376 void Yaz_Z_Assoc::set_otherInformationString (
377     Z_OtherInformation **otherInformation,
378     int oidval, int categoryValue,
379     const char *str)
380 {
381     int oid[OID_SIZE];
382     struct oident ent;
383     ent.proto = PROTO_Z3950;
384     ent.oclass = CLASS_USERINFO;
385     ent.value = (oid_value) oidval;
386     if (!oid_ent_to_oid (&ent, oid))
387         return ;
388     set_otherInformationString(otherInformation, oid, categoryValue, str);
389 }
390
391 void Yaz_Z_Assoc::set_otherInformationString (
392     Z_OtherInformation **otherInformation,
393     int *oid, int categoryValue, const char *str)
394 {
395     Z_OtherInformationUnit *oi =
396         update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
397     if (!oi)
398         return;
399     oi->information.characterInfo = odr_strdup (odr_encode(), str);
400 }
401
402 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
403     Z_OtherInformation **otherInformationP, int createFlag,
404     int *oid, int categoryValue, int deleteFlag)
405 {
406     return yaz_oi_update (otherInformationP,
407                           (createFlag ? odr_encode() : 0),
408                           oid, categoryValue, deleteFlag);
409 }
410
411 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
412 {
413     Z_ReferenceId* id = NULL;
414
415     if (str)
416     {
417         id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
418         id->size = id->len = strlen(str);
419         id->buf = (unsigned char *) str;
420     }
421     return id;
422 }
423