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