94b8638281b388fb87fcd65dda9c93acf5dcef11
[yazpp-moved-to-github.git] / src / yaz-z-assoc.cpp
1 /*
2  * Copyright (c) 1998-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-z-assoc.cpp,v $
6  * Revision 1.19  2001-03-27 14:47:45  adam
7  * New server facility scheme.
8  *
9  * Revision 1.18  2001/03/26 14:43:49  adam
10  * New threaded PDU association.
11  *
12  * Revision 1.17  2001/01/04 14:25:25  heikki
13  * No SIGPIPES in windows...
14  *
15  * Revision 1.16  2000/12/14 16:00:39  adam
16  * Ignoring signal SIGPIPE.
17  *
18  * Revision 1.15  2000/10/11 11:58:17  adam
19  * Moved header files to include/yaz++. Switched to libtool and automake.
20  * Configure script creates yaz++-config script.
21  *
22  * Revision 1.14  2000/09/12 16:40:33  heikki
23  * minor
24  *
25  * Revision 1.13  2000/09/08 10:23:42  adam
26  * Added skeleton of yaz-z-server.
27  *
28  * Revision 1.12  2000/09/05 13:57:28  adam
29  * Fixed get_otherInfoAPDU to return otherInfo for extended services.
30  *
31  * Revision 1.11  2000/09/04 08:59:16  adam
32  * Changed call to logging functions (yaz_ added).
33  *
34  * Revision 1.10  2000/09/04 08:29:22  adam
35  * Fixed memory leak(s). Added re-use of associations, rather than
36  * re-init, when maximum number of targets are in use.
37  *
38  * Revision 1.9  2000/08/10 08:42:42  adam
39  * Fixes for {set,get}_APDU_log.
40  *
41  * Revision 1.8  2000/08/07 14:19:59  adam
42  * Fixed serious bug regarding timeouts. Improved logging for proxy.
43  *
44  * Revision 1.7  2000/05/10 11:36:58  ian
45  * Added default parameters for refid to request functions.
46  * Added default parameter for result set name to search and present request.
47  * Commented out forced logging of PDU contents.
48  * Added send_deleteResultSetRequest
49  *
50  * Revision 1.6  1999/12/06 13:52:45  adam
51  * Modified for new location of YAZ header files. Experimental threaded
52  * operation.
53  *
54  * Revision 1.5  1999/11/10 10:02:34  adam
55  * Work on proxy.
56  *
57  * Revision 1.4  1999/09/13 12:53:44  adam
58  * Proxy removes OtherInfo Proxy Address and Session ID. Other
59  * Otherinfo remains untouched.
60  *
61  * Revision 1.3  1999/04/21 12:09:01  adam
62  * Many improvements. Modified to proxy server to work with "sessions"
63  * based on cookies.
64  *
65  * Revision 1.2  1999/04/20 10:30:05  adam
66  * Implemented various stuff for client and proxy. Updated calls
67  * to ODR to reflect new name parameter.
68  *
69  * Revision 1.1  1999/04/09 11:46:57  adam
70  * Added object Yaz_Z_Assoc. Much more functional client.
71  *
72  */
73
74 #include <assert.h>
75 #include <signal.h>
76
77 #include <yaz/log.h>
78 #include <yaz++/yaz-z-assoc.h>
79 #include <yaz/otherinfo.h>
80
81 int Yaz_Z_Assoc::yaz_init_func()
82 {
83 #ifndef WIN32
84     signal (SIGPIPE, SIG_IGN);
85 #endif
86     nmem_init();
87     return 1;
88 }
89
90 int Yaz_Z_Assoc::yaz_init_flag =  Yaz_Z_Assoc::yaz_init_func();  
91
92 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
93 {
94     m_PDU_Observable = the_PDU_Observable;
95     m_odr_in = odr_createmem (ODR_DECODE);
96     m_odr_out = odr_createmem (ODR_ENCODE);
97     m_odr_print = odr_createmem (ODR_PRINT);
98     m_log = LOG_DEBUG;
99     m_APDU_file = 0;
100     m_APDU_fname = 0;
101     m_hostname = 0;
102 }
103
104 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
105 {
106     if (m_APDU_file && m_APDU_file != stderr)
107     {
108         fclose (m_APDU_file);
109         m_APDU_file = 0;
110     }
111     delete [] m_APDU_fname;
112     m_APDU_fname = 0;
113
114     if (fname) 
115     {
116         m_APDU_fname = new char[strlen(fname)+1];
117         strcpy (m_APDU_fname, fname);
118         if (*fname && strcmp(fname, "-"))
119             m_APDU_file = fopen (fname, "a");
120         else
121             m_APDU_file = stderr;
122         odr_setprint(m_odr_print, m_APDU_file);
123     }
124 }
125
126 const char *Yaz_Z_Assoc::get_APDU_log()
127 {
128     return m_APDU_fname;
129 }
130
131 Yaz_Z_Assoc::~Yaz_Z_Assoc()
132 {
133     m_PDU_Observable->destroy();  
134     delete m_PDU_Observable;
135     odr_destroy (m_odr_print);     // note: also runs fclose on m_APDU_file ..
136     odr_destroy (m_odr_out);
137     odr_destroy (m_odr_in);
138     delete [] m_APDU_fname;
139     delete [] m_hostname;
140 }
141
142 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
143 {
144     logf (m_log, "recv_PDU len=%d", len);
145     Z_APDU *apdu = decode_Z_PDU (buf, len);
146     if (apdu)
147     {
148         recv_Z_PDU (apdu);
149     }
150     else
151     {
152         close();
153     }
154 }
155
156 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
157 {
158     Z_APDU *apdu = zget_APDU(m_odr_out, type);
159     if (apdu->which == Z_APDU_initRequest)
160     {
161         Z_InitRequest * p = apdu->u.initRequest;
162         char *newName = (char*) odr_malloc(m_odr_out, 50);
163         strcpy (newName, p->implementationName);
164         strcat (newName, " YAZ++");
165         p->implementationName = newName;
166     }
167     return apdu;
168 }
169
170 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
171 {
172     char *buf;
173     int len;
174     if (encode_Z_PDU(apdu, &buf, &len) > 0)
175         return m_PDU_Observable->send_PDU(buf, len);
176     return -1;
177 }
178
179 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
180 {
181     Z_APDU *apdu;
182
183     odr_reset (m_odr_in);
184     odr_setbuf (m_odr_in, (char*) buf, len, 0);
185
186     if (!z_APDU(m_odr_in, &apdu, 0, 0))
187     {
188         logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
189              odr_errmsg(odr_geterror(m_odr_in)),
190              odr_offset(m_odr_in));
191         logf(LOG_LOG, "PDU dump:");
192         odr_dumpBER(yaz_log_file(), buf, len);
193         return 0;
194     }
195     else
196     {
197         if (m_APDU_file)
198             z_APDU(m_odr_print, &apdu, 0, "decode");
199         return apdu;
200     }
201 }
202
203 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
204 {
205     if (m_APDU_file)
206         z_APDU(m_odr_print, &apdu, 0, "encode");
207     if (!z_APDU(m_odr_out, &apdu, 0, 0))
208     {
209         logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
210         return -1;
211     }
212     *buf = odr_getbuf (m_odr_out, len, 0);
213     odr_reset (m_odr_out);
214     return *len;
215 }
216
217 const char *Yaz_Z_Assoc::get_hostname()
218 {
219     return m_hostname;
220 }
221
222 void Yaz_Z_Assoc::client(const char *addr)
223 {
224     delete [] m_hostname;
225     m_hostname = new char[strlen(addr)+1];
226     strcpy (m_hostname, addr);
227     m_PDU_Observable->connect (this, addr);
228 }
229
230 void Yaz_Z_Assoc::close()
231 {
232     m_PDU_Observable->close ();
233 }
234
235 void Yaz_Z_Assoc::server(const char *addr)
236 {
237     delete [] m_hostname;
238     m_hostname = new char[strlen(addr)+1];
239     strcpy (m_hostname, addr);
240     m_PDU_Observable->listen (this, addr);
241 }
242
243 ODR Yaz_Z_Assoc::odr_encode()
244 {
245     return m_odr_out;
246 }
247
248 ODR Yaz_Z_Assoc::odr_decode()
249 {
250     return m_odr_in;
251 }
252 ODR Yaz_Z_Assoc::odr_print()
253 {
254     return m_odr_print;
255 }
256
257 void Yaz_Z_Assoc::timeout(int timeout)
258 {
259     m_PDU_Observable->idleTime(timeout);
260 }
261
262 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
263 {
264     switch (apdu->which)
265     {
266     case Z_APDU_initRequest:
267         *oip = &apdu->u.initRequest->otherInfo;
268         break;
269     case Z_APDU_searchRequest:
270         *oip = &apdu->u.searchRequest->otherInfo;
271         break;
272     case Z_APDU_presentRequest:
273         *oip = &apdu->u.presentRequest->otherInfo;
274         break;
275     case Z_APDU_sortRequest:
276         *oip = &apdu->u.sortRequest->otherInfo;
277         break;
278     case Z_APDU_scanRequest:
279         *oip = &apdu->u.scanRequest->otherInfo;
280         break;
281     case Z_APDU_extendedServicesRequest:
282         *oip = &apdu->u.extendedServicesRequest->otherInfo;
283         break;
284     case Z_APDU_deleteResultSetRequest:
285         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
286         break;
287     case Z_APDU_initResponse:
288         *oip = &apdu->u.initResponse->otherInfo;
289         break;
290     case Z_APDU_searchResponse:
291         *oip = &apdu->u.searchResponse->otherInfo;
292         break;
293     case Z_APDU_presentResponse:
294         *oip = &apdu->u.presentResponse->otherInfo;
295         break;
296     case Z_APDU_sortResponse:
297         *oip = &apdu->u.sortResponse->otherInfo;
298         break;
299     case Z_APDU_scanResponse:
300         *oip = &apdu->u.scanResponse->otherInfo;
301         break;
302     case Z_APDU_extendedServicesResponse:
303         *oip = &apdu->u.extendedServicesResponse->otherInfo;
304         break;
305     case Z_APDU_deleteResultSetResponse:
306         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
307         break;
308     default:
309         *oip = 0;
310         break;
311     }
312 }
313
314 void Yaz_Z_Assoc::set_otherInformationString (
315     Z_APDU *apdu,
316     int oidval, int categoryValue,
317     const char *str)
318 {
319     Z_OtherInformation **otherInformation;
320     get_otherInfoAPDU(apdu, &otherInformation);
321     if (!otherInformation)
322         return;
323     set_otherInformationString(otherInformation, oidval, categoryValue, str);
324 }
325
326 void Yaz_Z_Assoc::set_otherInformationString (
327     Z_OtherInformation **otherInformation,
328     int oidval, int categoryValue,
329     const char *str)
330 {
331     int oid[OID_SIZE];
332     struct oident ent;
333     ent.proto = PROTO_Z3950;
334     ent.oclass = CLASS_USERINFO;
335     ent.value = (oid_value) oidval;
336     if (!oid_ent_to_oid (&ent, oid))
337         return ;
338     set_otherInformationString(otherInformation, oid, categoryValue, str);
339 }
340
341 void Yaz_Z_Assoc::set_otherInformationString (
342     Z_OtherInformation **otherInformation,
343     int *oid, int categoryValue, const char *str)
344 {
345     Z_OtherInformationUnit *oi =
346         update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
347     if (!oi)
348         return;
349     oi->information.characterInfo = odr_strdup (odr_encode(), str);
350 }
351
352 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
353     Z_OtherInformation **otherInformationP, int createFlag,
354     int *oid, int categoryValue, int deleteFlag)
355 {
356     return yaz_oi_update (otherInformationP,
357                           (createFlag ? odr_encode() : 0),
358                           oid, categoryValue, deleteFlag);
359 }
360
361 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
362 {
363     Z_ReferenceId* id = NULL;
364
365     if (str)
366     {
367         id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
368         id->size = id->len = strlen(str);
369         id->buf = (unsigned char *) str;
370     }
371     return id;
372 }
373