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