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