Split ICU wrapper library into several sources
[yaz-moved-to-github.git] / include / yaz / backend.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /** 
29  * \file backend.h
30  * \brief Header for GFS
31  *
32  * This header includes all public definitions for the
33  * Generic Frontend Server (GFS).
34  */
35
36 #ifndef BACKEND_H
37 #define BACKEND_H
38
39 #include <yaz/yconfig.h>
40 #include <yaz/proto.h>
41 #include <yaz/srw.h>
42 #include <yaz/oid_db.h>
43
44 YAZ_BEGIN_CDECL
45     
46 typedef struct request *bend_request;
47 typedef struct association *bend_association;
48
49 /** \brief Information for Z39.50/SRU search handler */
50 typedef struct {
51     char *setname;             /* name to give to this set */
52     int replace_set;           /* replace set, if it already exists */
53     int num_bases;             /* number of databases in list */
54     char **basenames;          /* databases to search */
55     Z_ReferenceId *referenceId;/* reference ID */
56     Z_Query *query;            /* query structure */
57     ODR stream;                /* encode stream */
58     ODR decode;                /* decode stream */
59     ODR print;                 /* print stream */
60
61     bend_request request;
62     bend_association association;
63     int *fd;
64     Odr_int hits;                  /* number of hits */
65     int errcode;               /* 0==OK */
66     char *errstring;           /* system error string or NULL */
67     Z_OtherInformation *search_info; /* additional search info */
68     char *srw_sortKeys;        /* holds SRU/SRW sortKeys info */
69     char *srw_setname;         /* holds SRU/SRW generated resultsetID */
70     int *srw_setnameIdleTime;  /* holds SRU/SRW life-time */
71     int estimated_hit_count;   /* if hit count is estimated */
72     int partial_resultset;     /* if result set is partial */
73     Z_SRW_extra_arg *extra_args; /* extra URL arguments */
74     char *extra_response_data;   /* extra XML response. */
75 } bend_search_rr;
76
77 /** \brief Information for present handler. Does not replace bend_fetch. */
78 typedef struct {
79     char *setname;             /* set name */
80     int start;
81     int number;                /* record number */
82     Odr_oid *format;           /* format, transfer syntax (OID) */
83     Z_ReferenceId *referenceId;/* reference ID */
84     Z_RecordComposition *comp; /* Formatting instructions */
85     ODR stream;                /* encoding stream - memory source if required */
86     ODR print;                 /* printing stream */
87     bend_request request;
88     bend_association association;
89
90     int hits;                  /* number of hits */
91     int errcode;               /* 0==OK */
92     char *errstring;           /* system error string or NULL */
93 } bend_present_rr;
94
95 /** \brief Information for fetch record handler */
96 typedef struct bend_fetch_rr {
97     char *setname;             /* set name */
98     int number;                /* record number */
99     Z_ReferenceId *referenceId;/* reference ID */
100     Odr_oid *request_format;        /* format, transfer syntax (OID) */
101     Z_RecordComposition *comp; /* Formatting instructions */
102     ODR stream;                /* encoding stream - memory source if req */
103     ODR print;                 /* printing stream */
104
105     char *basename;            /* name of database that provided record */
106     int len;                   /* length of record or -1 if structured */
107     char *record;              /* record */
108     int last_in_set;           /* is it?  */
109     Odr_oid *output_format;        /* response format/syntax (OID) */
110     int errcode;               /* 0==success */
111     char *errstring;           /* system error string or NULL */
112     int surrogate_flag;        /* surrogate diagnostic */
113     char *schema;              /* string record schema input/output */
114 } bend_fetch_rr;
115
116 /** \brief Information for scan entry */
117 struct scan_entry {
118     char *term;         /* the returned scan term */
119     Odr_int occurrences;/* no of occurrences or -1 if error (see below) */
120     int errcode;        /* Bib-1 diagnostic code; only used when occur.= -1 */
121     char *errstring;    /* Additional string */
122     char *display_term;
123 };
124
125 typedef enum {
126     BEND_SCAN_SUCCESS,  /* ok */
127     BEND_SCAN_PARTIAL   /* not all entries could be found */
128 } bend_scan_status;
129
130 /** \brief Information for SRU / Z39.50 scan handler */
131 typedef struct bend_scan_rr {
132     int num_bases;      /* number of elements in databaselist */
133     char **basenames;   /* databases to search */
134     Odr_oid *attributeset;
135     Z_ReferenceId *referenceId; /* reference ID */
136     Z_AttributesPlusTerm *term;
137     ODR stream;         /* encoding stream - memory source if required */
138     ODR print;          /* printing stream */
139
140     Odr_int *step_size;     /* step size */
141     Odr_int term_position;  /* desired index of term in result list/returned */
142     int num_entries;    /* number of entries requested/returned */
143
144     /* scan term entries. The called handler does not have
145        to allocate this. Size of entries is num_entries (see above) */
146     struct scan_entry *entries;
147     bend_scan_status status;
148     int errcode;
149     char *errstring;
150     char *scanClause;   /* CQL scan clause */
151     char *setname;      /* Scan in result set (NULL if omitted) */
152 } bend_scan_rr;
153
154 /** \brief Information for SRU record update handler */
155 typedef struct bend_update_rr {
156     int num_bases;      /* number of elements in databaselist */
157     char **basenames;   /* databases to search */
158     Z_ReferenceId *referenceId; /* reference ID */
159     ODR stream;         /* encoding stream - memory source if required */
160     ODR print;          /* printing stream */
161     char *operation;
162     char *operation_status;
163     char *record_id;
164     Z_SRW_recordVersion *record_versions;
165     int num_versions;
166     char *record_packing;
167     char *record_schema;
168     char *record_data;
169     char *extra_record_data;
170     char *extra_request_data;
171     char *extra_response_data;
172     char *uri;
173     char *message;
174     char *details;
175 } bend_update_rr;
176
177 /** \brief Information for Z39.50 delete result set handler */
178 typedef struct bend_delete_rr {
179     int function;
180     int num_setnames;
181     char **setnames;
182     Z_ReferenceId *referenceId;
183     int delete_status;    /* status for the whole operation */
184     Odr_int *statuses;    /* status each set - indexed as setnames */
185     ODR stream;
186     ODR print; 
187 } bend_delete_rr;
188
189 /** \brief Information for Z39.50 sort handler */
190 typedef struct bend_sort_rr
191 {
192     int num_input_setnames;
193     char **input_setnames;
194     char *output_setname;
195     Z_SortKeySpecList *sort_sequence;
196     ODR stream;
197     ODR print;
198     Z_ReferenceId *referenceId;/* reference ID */
199
200     int sort_status;
201     int errcode;
202     char *errstring;
203 } bend_sort_rr;
204
205 /** \brief Information for Z39.50 extended services handler */
206 typedef struct bend_esrequest_rr
207 {
208     int ItemNo;
209     Z_ExtendedServicesRequest *esr;
210     
211     ODR stream;                /* encoding stream */
212     ODR decode;                /* decoding stream */
213     ODR print;                 /* printing stream */
214     Z_ReferenceId *referenceId;/* reference ID */
215     bend_request request;
216     bend_association association;
217     int errcode;               /* 0==success, -1==accepted, >0 = failure */
218     char *errstring;           /* system error string or NULL */
219     Z_TaskPackage *taskPackage;
220 } bend_esrequest_rr;
221
222 /** \brief Information for Z39.50 segment handler */
223 typedef struct bend_segment_rr {
224     Z_Segment *segment;
225     ODR stream;
226     ODR decode;
227     ODR print;
228     bend_request request;
229     bend_association association;
230 } bend_segment_rr;
231
232 /** \brief Information for SRU Explain handler */
233 typedef struct {
234     ODR stream;
235     ODR decode;
236     ODR print;
237     char *explain_buf;
238     char *database;
239     char *schema;
240     void *server_node_ptr;
241 } bend_explain_rr;
242
243 /** \brief Information for the Init handler
244
245 This includes both request
246 information (to be read) and response information which should be
247 set by the bend_init handler 
248 */
249 typedef struct bend_initrequest
250 {
251     /** \brief user/name/password to be read */
252     Z_IdAuthentication *auth; 
253     /** \brief encoding stream (for results) */
254     ODR stream;
255     /** \brief printing stream */
256     ODR print;
257     /** \brief decoding stream (use stream for results) */
258     ODR decode; 
259     /** \brief reference ID */
260     Z_ReferenceId *referenceId;
261     /** \brief peer address of client */
262     char *peer_name;           
263     
264     /** \brief character set and language negotiation 
265
266     see include/yaz/z-charneg.h 
267     */
268     Z_CharSetandLanguageNegotiation *charneg_request;
269
270     /** \brief character negotiation response */
271     Z_External *charneg_response;
272
273     /** \brief character set (encoding) for query terms 
274         
275     This is NULL by default. It should be set to the native character
276     set that the backend assumes for query terms */
277     char *query_charset;      
278
279     /** \brief whehter query_charset also applies to recors 
280     
281     Is 0 (No) by default. Set to 1 (yes) if records is in the same
282     character set as queries. If in doubt, use 0 (No).
283     */
284     int records_in_same_charset;
285
286     char *implementation_id;
287     char *implementation_name;
288     char *implementation_version;
289
290     /** \brief Z39.50 sort handler */
291     int (*bend_sort)(void *handle, bend_sort_rr *rr);
292     /** \brief SRU/Z39.50 search handler */
293     int (*bend_search)(void *handle, bend_search_rr *rr);
294     /** \brief SRU/Z39.50 fetch handler */
295     int (*bend_fetch)(void *handle, bend_fetch_rr *rr);
296     /** \brief SRU/Z39.50 present handler */
297     int (*bend_present)(void *handle, bend_present_rr *rr);
298     /** \brief Z39.50 extended services handler */
299     int (*bend_esrequest) (void *handle, bend_esrequest_rr *rr);
300     /** \brief Z39.50 delete result set handler */
301     int (*bend_delete)(void *handle, bend_delete_rr *rr);
302     /** \brief Z39.50 scan handler */
303     int (*bend_scan)(void *handle, bend_scan_rr *rr);
304     /** \brief Z39.50 segment facility handler */
305     int (*bend_segment)(void *handle, bend_segment_rr *rr);
306     /** \brief SRU explain handler */
307     int (*bend_explain)(void *handle, bend_explain_rr *rr);
308     /** \brief SRU scan handler */
309     int (*bend_srw_scan)(void *handle, bend_scan_rr *rr);
310     /** \brief SRU record update handler */
311     int (*bend_srw_update)(void *handle, bend_update_rr *rr);
312
313     /** \brief whether named result sets are supported (0=disable, 1=enable) */
314     int named_result_sets;
315 } bend_initrequest;
316
317 /** \brief result for init handler (must be filled by handler) */
318 typedef struct bend_initresult
319 {
320     int errcode;               /* 0==OK */
321     char *errstring;           /* system error string or NULL */
322     void *handle;              /* private handle to the backend module */
323 } bend_initresult;
324
325 YAZ_EXPORT void bend_request_send (bend_association a, bend_request req,
326                                    Z_APDU *res);
327
328 YAZ_EXPORT bend_request bend_request_mk (bend_association a);
329
330 YAZ_EXPORT void bend_request_destroy (bend_request *req);
331
332 YAZ_EXPORT Z_ReferenceId *bend_request_getid (ODR odr, bend_request req);
333 YAZ_EXPORT int bend_backend_respond (bend_association a, bend_request req);
334 YAZ_EXPORT void bend_request_setdata(bend_request r, void *p);
335 YAZ_EXPORT void *bend_request_getdata(bend_request r);
336
337 /** \brief control block for server */
338 typedef struct statserv_options_block
339 {
340     int dynamic;                  /* fork on incoming requests */
341     int threads;                  /* use threads */
342     int one_shot;                 /* one session then exit(1) */
343     char apdufile[ODR_MAXNAME+1]; /* file for pretty-printed PDUs */
344     char logfile[ODR_MAXNAME+1];  /* file for diagnostic output */
345     char default_listen[1024];    /* 0 == no default listen */
346     enum oid_proto default_proto; /* PROTO_SR or PROTO_Z3950 */ 
347     int idle_timeout;             /* how many minutes to wait before closing */
348     int maxrecordsize;            /* maximum value for negotiation */
349     char configname[ODR_MAXNAME+1];  /* given to the backend in bend_init */
350     char setuid[ODR_MAXNAME+1];     /* setuid to this user after binding */
351     void (*bend_start)(struct statserv_options_block *p);
352     void (*bend_stop)(struct statserv_options_block *p);
353     int (*options_func)(int argc, char **argv);
354     int (*check_ip)(void *cd, const char *addr, int len, int type);
355     char daemon_name[128];
356     int inetd;                    /* Do we use the inet deamon or not */
357
358     void *handle;                 /* Handle */
359     bend_initresult *(*bend_init)(bend_initrequest *r);
360     void (*bend_close)(void *handle);
361 #ifdef WIN32
362     /* We only have these members for the windows version */
363     /* They seemed a bit large to have them there in general */
364     char service_name[128];         /* NT Service Name */
365     char app_name[128];             /* Application Name */
366     char service_dependencies[128]; /* The services we are dependent on */
367     char service_display_name[128]; /* The service display name */
368 #endif /* WIN32 */
369     struct bend_soap_handler *soap_handlers;
370     char pid_fname[128];            /* pid fname */
371     int background;                 /* auto daemon */
372     char cert_fname[128];           /* SSL certificate fname */
373     char xml_config[128];           /* XML config filename */
374 } statserv_options_block;
375
376 YAZ_EXPORT int statserv_main(
377     int argc, char **argv,
378     bend_initresult *(*bend_init)(bend_initrequest *r),
379     void (*bend_close)(void *handle));
380 YAZ_EXPORT statserv_options_block *statserv_getcontrol(void);
381 YAZ_EXPORT void statserv_setcontrol(statserv_options_block *block);
382 YAZ_EXPORT int check_ip_tcpd(void *cd, const char *addr, int len, int type);
383
384 YAZ_EXPORT int bend_assoc_is_alive(bend_association assoc);
385
386 YAZ_END_CDECL
387
388 #endif
389 /*
390  * Local variables:
391  * c-basic-offset: 4
392  * c-file-style: "Stroustrup"
393  * indent-tabs-mode: nil
394  * End:
395  * vim: shiftwidth=4 tabstop=8 expandtab
396  */
397