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