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