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