6e9943e52a110f4993666b6ac0bfddcc4ef64af7
[yaz-moved-to-github.git] / ziffy / yaz.c
1 /*
2  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
3  * yaz.c - decoding and printing utility based on the YAZ Toolkit
4  *
5  * Copyright (c) 1998-2001 R. Carbone <rocco@ntop.org>
6  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23
24 /*
25  * Operating System include files
26  */
27 #include <stdio.h>
28 #include <sys/time.h>
29
30 /*
31  * YAZ include files
32  */
33 #include "yaz/odr.h"
34 #include "yaz/proto.h"
35
36 #include "apdu.h"
37
38
39 void please_yaz_help_me (z3950apdu * hook)
40 {
41   extern unsigned char * z3950;
42   extern int z3950_size;
43
44   /*
45    * Variable to keep the Z39.50 APDUs. The definitions are in the
46    * the structures defined by the YAZ Toolkit.
47    */
48   Z_APDU * apdu = NULL;
49
50   /*
51    * Decoding/Printing streams
52    */
53   ODR printing;
54   ODR decode;
55
56   /*
57    * The stream used for decoding
58    */
59 #define MAXBERSIZE (2048 * 2048)
60   unsigned char berbuffer [MAXBERSIZE];
61
62   /*
63    * Allocate a stream for input data
64    */
65   decode = odr_createmem (ODR_DECODE);
66   if (! decode)
67     {
68       printf ("Not enough memory to create an input stream\n");
69       return;
70     }
71
72   /*
73    * Allocate a stream for printing data
74    */
75   printing = odr_createmem (ODR_PRINT);
76   if (! printing)
77     {
78       printf ("Not enough memory to create a printing stream\n");
79       odr_destroy (decode);
80       return;
81     }
82
83   /*
84    * Initialize the decoding routines
85    */
86   memcpy (berbuffer, z3950, z3950_size);
87
88   odr_setbuf (decode, (char *) berbuffer, z3950_size, 0);
89
90   /*
91    * Perform BER decoding
92    */
93   if (z_APDU (decode, & apdu, 0, 0))
94     {
95       ++ z3950_apduno;
96
97       if (z3950flag)
98         printf ("Z3950:  ----- Z39.50 APDU -----\n"),
99           printf ("Z3950:  APDU %ld arrived at %s\n", z3950_apduno,
100                   timestamp (hook -> t, ABS_FMT)),
101           printf ("Z3950:  Total size  = %d\n", z3950_size),
102           fflush (stdout);
103
104       /*
105        * save the time the last apdu was displayed
106        */
107       if (z3950_apduno == 1)
108         gettimeofday (& first_apdu, NULL);
109
110       /*
111        * print standard summary information accordingly to the format
112        *
113        * id   time     source:port ->   destination:port    type
114        */
115       printf ("Z3950: %5ld %s %s:%d -> %s:%d %s\n",
116               z3950_apduno, timestamp (hook -> t, DELTA_FMT),
117               hook -> calling, hook -> srcport, hook -> called, hook -> dstport,
118               hook -> name),
119         fflush (stdout);
120
121       gettimeofday (& last_apdu, NULL);
122
123 #if (0)
124       fmemdmp (stdout, z3950, z3950_size, "Z39.50 APDU");
125 #endif
126
127       /*
128        * Yup! We have the APDU now. Try to print it
129        */
130       odr_setbuf (printing, (char *) berbuffer, z3950_size, 0);
131       fflush (stdout);
132
133       z_APDU (printing, & apdu, 0, 0);
134       fflush (stderr);
135
136       odr_reset (printing);
137       printing -> buf = NULL;
138     }
139
140   /*
141    * release memory previously allocated
142    */
143   odr_destroy (decode);
144   odr_destroy (printing);
145 }