Doxyfile file description. Indentation. No change of code.
[yaz-moved-to-github.git] / include / yaz / comstack.h
1 /*
2  * Copyright (c) 1995-2004, Index Data.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation, in whole or in part, for any purpose, is hereby granted,
6  * provided that:
7  *
8  * 1. This copyright and permission notice appear in all copies of the
9  * software and its documentation. Notices of copyright or attribution
10  * which appear at the beginning of any file must remain unchanged.
11  *
12  * 2. The name of Index Data or the individual authors may not be used to
13  * endorse or promote products derived from this software without specific
14  * prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
20  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
22  * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * $Id: comstack.h,v 1.17 2004-10-15 00:18:59 adam Exp $
27  */
28
29 /** 
30  * \file comstack.h
31  * \brief Header for COMSTACK
32  */
33
34 #ifndef COMSTACK_H
35 #define COMSTACK_H
36
37 #include <yaz/yconfig.h>
38
39 #ifndef _VMS_
40
41 # ifdef WIN32
42
43 #  include <winsock.h>
44
45 # else /* #ifdef WIN32 */
46 #  include <sys/types.h>
47 #  include <sys/time.h>
48 #  include <sys/wait.h>
49
50 #  include <netinet/in.h>
51 #  include <sys/socket.h>
52 #  include <netdb.h>
53 #  include <arpa/inet.h>
54
55 #  ifdef _AIX
56 #   include <sys/select.h>
57 #  endif
58
59 #  ifndef O_BINARY
60 #   define O_BINARY 0
61 #  endif
62
63 # endif
64 #endif /* ifndef _VMS_ */
65
66 #include <yaz/oid.h>
67 #include <yaz/xmalloc.h>
68
69 YAZ_BEGIN_CDECL
70
71 #define COMSTACK_DEFAULT_TIMEOUT -1  /* not used yet */
72
73 struct comstack;
74 typedef struct comstack *COMSTACK;
75 typedef COMSTACK (*CS_TYPE)(int s, int blocking, int protocol, void *vp);
76
77 struct comstack
78 {
79     CS_TYPE type;
80     int cerrno;     /* current error code of this stack */
81     char *stackerr;/* current lower-layer error string, or null if none */
82     int iofile;    /* UNIX file descriptor for iochannel */
83     int timeout;   /* how long to wait for trailing blocks (ignored for now) */
84     void *cprivate;/* state info for lower stack */
85     int more;      /* connection has extra data in buffer */
86     int state;     /* current state */
87 #define CS_ST_UNBND      0
88 #define CS_ST_IDLE       1
89 #define CS_ST_INCON      2
90 #define CS_ST_OUTCON     3
91 #define CS_ST_DATAXFER   4
92 #define CS_ST_ACCEPT     5
93 #define CS_ST_CONNECTING 6
94     int newfd;     /* storing new descriptor between listen and accept */
95     int blocking;  /* is this link (supposed to be) blocking? */
96     unsigned io_pending; /* flag to signal read / write op is incomplete */
97     int event;     /* current event */
98 #define CS_NONE       0
99 #define CS_CONNECT    1
100 #define CS_DISCON     2
101 #define CS_LISTEN     3
102 #define CS_DATA       4
103     enum oid_proto protocol;  /* what application protocol are we talking? */
104     int (*f_put)(COMSTACK handle, char *buf, int size);
105     int (*f_get)(COMSTACK handle, char **buf, int *bufsize);
106     int (*f_more)(COMSTACK handle);
107     int (*f_connect)(COMSTACK handle, void *address);
108     int (*f_rcvconnect)(COMSTACK handle);
109     int (*f_bind)(COMSTACK handle, void *address, int mode);
110 #define CS_CLIENT 0
111 #define CS_SERVER 1
112     int (*f_listen)(COMSTACK h, char *raddr, int *addrlen,
113                    int (*check_ip)(void *cd, const char *a, int len, int type),
114                    void *cd);
115     COMSTACK (*f_accept)(COMSTACK handle);
116     int (*f_close)(COMSTACK handle);
117     char *(*f_addrstr)(COMSTACK handle);
118     void *(*f_straddr)(COMSTACK handle, const char *str);
119     int (*f_set_blocking)(COMSTACK handle, int blocking);
120 };
121
122 #define cs_put(handle, buf, size) ((*(handle)->f_put)(handle, buf, size))
123 #define cs_get(handle, buf, size) ((*(handle)->f_get)(handle, buf, size))
124 #define cs_more(handle) ((*(handle)->f_more)(handle))
125 #define cs_connect(handle, address) ((*(handle)->f_connect)(handle, address))
126 #define cs_rcvconnect(handle) ((*(handle)->f_rcvconnect)(handle))
127 #define cs_bind(handle, ad, mo) ((*(handle)->f_bind)(handle, ad, mo))
128 #define cs_listen(handle, ap, al) ((*(handle)->f_listen)(handle, ap, al, 0, 0))
129 #define cs_listen_check(handle, ap, al, cf, cd) ((*(handle)->f_listen)(handle, ap, al, cf, cd))
130 #define cs_accept(handle) ((*(handle)->f_accept)(handle))
131 #define cs_close(handle) ((*(handle)->f_close)(handle))
132 #define cs_create(type, blocking, proto) ((*type)(-1, blocking, proto, 0))
133 #define cs_createbysocket(sock, type, blocking, proto) \
134         ((*type)(sock, blocking, proto, 0))
135 #define cs_type(handle) ((handle)->type)
136 #define cs_fileno(handle) ((handle)->iofile)
137 #define cs_stackerr(handle) ((handle)->stackerr)
138 #define cs_getstate(handle) ((handle)->getstate)
139 #define cs_errno(handle) ((handle)->cerrno)
140 #define cs_getproto(handle) ((handle)->protocol)
141 #define cs_addrstr(handle) ((*(handle)->f_addrstr)(handle))
142 #define cs_straddr(handle, str) ((*(handle)->f_straddr)(handle, str))
143 #define cs_want_read(handle) ((handle)->io_pending & CS_WANT_READ)
144 #define cs_want_write(handle) ((handle)->io_pending & CS_WANT_WRITE)
145 #define cs_set_blocking(handle,blocking) ((handle)->f_set_blocking(handle, blocking))
146                                           
147 #define CS_WANT_READ 1
148 #define CS_WANT_WRITE 2
149
150 YAZ_EXPORT int cs_look (COMSTACK);
151 YAZ_EXPORT const char *cs_strerror(COMSTACK h);
152 YAZ_EXPORT const char *cs_errmsg(int n);
153 YAZ_EXPORT COMSTACK cs_create_host(const char *type_and_host, 
154                                    int blocking, void **vp);
155 YAZ_EXPORT void cs_get_host_args(const char *type_and_host, const char **args);
156 YAZ_EXPORT int cs_complete_auto(const unsigned char *buf, int len);
157 YAZ_EXPORT void *cs_get_ssl(COMSTACK cs);
158 YAZ_EXPORT int cs_set_ssl_ctx(COMSTACK cs, void *ctx);
159 YAZ_EXPORT int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname);
160 YAZ_EXPORT int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len);
161                                           
162 /*
163  * error management.
164  */
165                                           
166 #define CSNONE     0
167 #define CSYSERR    1
168 #define CSOUTSTATE 2
169 #define CSNODATA   3
170 #define CSWRONGBUF 4
171 #define CSDENY     5
172 #define CSERRORSSL 6
173 #define CSLASTERROR CSERRORSSL  /* must be the value of last CS error */
174
175 /* backwards compatibility */
176 #define CS_SR     PROTO_SR
177 #define CS_Z3950  PROTO_Z3950
178
179 YAZ_END_CDECL
180
181 #endif