COMSTACK API changes: cs_stackerr, cs_addrstr.
[yaz-moved-to-github.git] / include / yaz / comstack.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 comstack.h
30  * \brief Header for COMSTACK
31  */
32
33 #ifndef COMSTACK_H
34 #define COMSTACK_H
35
36 #include <yaz/yconfig.h>
37 #include <yaz/oid_util.h>
38 #include <yaz/xmalloc.h>
39
40 YAZ_BEGIN_CDECL
41
42 #define COMSTACK_DEFAULT_TIMEOUT -1  /* not used yet */
43
44 struct comstack;
45 typedef struct comstack *COMSTACK;
46 typedef COMSTACK (*CS_TYPE)(int s, int flags, int protocol, void *vp);
47
48 struct comstack
49 {
50     CS_TYPE type;
51     int cerrno;     /* current error code of this stack */
52     int iofile;    /* UNIX file descriptor for iochannel */
53     int timeout;   /* how long to wait for trailing blocks (ignored for now) */
54     void *cprivate;/* state info for lower stack */
55     int max_recv_bytes;      /* max size of incoming package */
56     int state;     /* current state */
57 #define CS_ST_UNBND      0
58 #define CS_ST_IDLE       1
59 #define CS_ST_INCON      2
60 #define CS_ST_OUTCON     3
61 #define CS_ST_DATAXFER   4
62 #define CS_ST_ACCEPT     5
63 #define CS_ST_CONNECTING 6
64     int newfd;     /* storing new descriptor between listen and accept */
65     int flags;     /* flags, blocking etc.. CS_FLAGS_..  */
66     unsigned io_pending; /* flag to signal read / write op is incomplete */
67     int event;     /* current event */
68 #define CS_NONE       0
69 #define CS_CONNECT    1
70 #define CS_DISCON     2
71 #define CS_LISTEN     3
72 #define CS_DATA       4
73     enum oid_proto protocol;  /* what application protocol are we talking? */
74     int (*f_put)(COMSTACK handle, char *buf, int size);
75     int (*f_get)(COMSTACK handle, char **buf, int *bufsize);
76     int (*f_more)(COMSTACK handle);
77     int (*f_connect)(COMSTACK handle, void *address);
78     int (*f_rcvconnect)(COMSTACK handle);
79     int (*f_bind)(COMSTACK handle, void *address, int mode);
80 #define CS_CLIENT 0
81 #define CS_SERVER 1
82     int (*f_listen)(COMSTACK h, char *raddr, int *addrlen,
83                    int (*check_ip)(void *cd, const char *a, int len, int type),
84                    void *cd);
85     COMSTACK (*f_accept)(COMSTACK handle);
86     void (*f_close)(COMSTACK handle);
87     const char *(*f_addrstr)(COMSTACK handle);
88     void *(*f_straddr)(COMSTACK handle, const char *str);
89     int (*f_set_blocking)(COMSTACK handle, int blocking);
90     void *user;       /* user defined data associated with COMSTACK */
91 };
92
93 #define cs_put(handle, buf, size) ((*(handle)->f_put)(handle, buf, size))
94 #define cs_get(handle, buf, size) ((*(handle)->f_get)(handle, buf, size))
95 #define cs_more(handle) ((*(handle)->f_more)(handle))
96 #define cs_connect(handle, address) ((*(handle)->f_connect)(handle, address))
97 #define cs_rcvconnect(handle) ((*(handle)->f_rcvconnect)(handle))
98 #define cs_bind(handle, ad, mo) ((*(handle)->f_bind)(handle, ad, mo))
99 #define cs_listen(handle, ap, al) ((*(handle)->f_listen)(handle, ap, al, 0, 0))
100 #define cs_listen_check(handle, ap, al, cf, cd) ((*(handle)->f_listen)(handle, ap, al, cf, cd))
101 #define cs_accept(handle) ((*(handle)->f_accept)(handle))
102 #define cs_close(handle) ((*(handle)->f_close)(handle))
103 #define cs_create(type, blocking, proto) ((*type)(-1, blocking, proto, 0))
104 #define cs_createbysocket(sock, type, blocking, proto) \
105         ((*type)(sock, blocking, proto, 0))
106 #define cs_type(handle) ((handle)->type)
107 #define cs_fileno(handle) ((handle)->iofile)
108 #define cs_getstate(handle) ((handle)->getstate)
109 #define cs_errno(handle) ((handle)->cerrno)
110 #define cs_getproto(handle) ((handle)->protocol)
111 #define cs_addrstr(handle) ((*(handle)->f_addrstr)(handle))
112 #define cs_straddr(handle, str) ((*(handle)->f_straddr)(handle, str))
113 #define cs_want_read(handle) ((handle)->io_pending & CS_WANT_READ)
114 #define cs_want_write(handle) ((handle)->io_pending & CS_WANT_WRITE)
115 #define cs_set_blocking(handle,blocking) ((handle)->f_set_blocking(handle, blocking))
116                                           
117 #define CS_WANT_READ 1
118 #define CS_WANT_WRITE 2
119
120 YAZ_EXPORT int cs_look (COMSTACK);
121 YAZ_EXPORT const char *cs_strerror(COMSTACK h);
122 YAZ_EXPORT const char *cs_errmsg(int n);
123 YAZ_EXPORT COMSTACK cs_create_host(const char *type_and_host, 
124                                    int blocking, void **vp);
125 YAZ_EXPORT void cs_get_host_args(const char *type_and_host, const char **args);
126 YAZ_EXPORT int cs_complete_auto_head(const char *buf, int len);
127 YAZ_EXPORT int cs_complete_auto(const char *buf, int len);
128 YAZ_EXPORT void *cs_get_ssl(COMSTACK cs);
129 YAZ_EXPORT int cs_set_ssl_ctx(COMSTACK cs, void *ctx);
130 YAZ_EXPORT int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname);
131 YAZ_EXPORT int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len);
132 YAZ_EXPORT void cs_set_max_recv_bytes(COMSTACK cs, int max_recv_bytes);
133 YAZ_EXPORT int completeWAIS(const char *buf, int len);
134
135 YAZ_EXPORT void cs_print_session_info(COMSTACK cs);
136
137 /*
138  * error management.
139  */
140                                           
141 #define CSNONE     0
142 #define CSYSERR    1
143 #define CSOUTSTATE 2
144 #define CSNODATA   3
145 #define CSWRONGBUF 4
146 #define CSDENY     5
147 #define CSERRORSSL 6
148 #define CSBUFSIZE  7
149 #define CSLASTERROR CSBUFSIZE  /* must be the value of last CS error */
150
151 /* backwards compatibility */
152 #define CS_SR     PROTO_SR
153 #define CS_Z3950  PROTO_Z3950
154
155 #define CS_FLAGS_BLOCKING 1
156 #define CS_FLAGS_NUMERICHOST 2
157
158 YAZ_END_CDECL
159
160 #endif
161 /*
162  * Local variables:
163  * c-basic-offset: 4
164  * c-file-style: "Stroustrup"
165  * indent-tabs-mode: nil
166  * End:
167  * vim: shiftwidth=4 tabstop=8 expandtab
168  */
169