2 * Copyright (c) 1995-2003, Index Data.
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,
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.
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.
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
26 * $Id: comstack.h,v 1.9 2003-03-03 19:57:35 adam Exp $
32 #include <yaz/yconfig.h>
40 # else /* #ifdef WIN32 */
41 # include <sys/types.h>
42 # include <sys/time.h>
43 # include <sys/wait.h>
45 # include <netinet/in.h>
46 # include <sys/socket.h>
48 # include <arpa/inet.h>
51 # include <sys/select.h>
59 #endif /* ifndef _VMS_ */
62 #include <yaz/xmalloc.h>
66 #define COMSTACK_DEFAULT_TIMEOUT -1 /* not used yet */
69 typedef struct comstack *COMSTACK;
70 typedef COMSTACK (*CS_TYPE)(int s, int blocking, int protocol, void *vp);
75 int cerrno; /* current error code of this stack */
76 char *stackerr;/* current lower-layer error string, or null if none */
77 int iofile; /* UNIX file descriptor for iochannel */
78 int timeout; /* how long to wait for trailing blocks (ignored for now) */
79 void *cprivate;/* state info for lower stack */
80 int more; /* connection has extra data in buffer */
81 int state; /* current state */
85 #define CS_ST_OUTCON 3
86 #define CS_ST_DATAXFER 4
87 #define CS_ST_ACCEPT 5
88 #define CS_ST_CONNECTING 6
89 int newfd; /* storing new descriptor between listen and accept */
90 int blocking; /* is this link (supposed to be) blocking? */
91 unsigned io_pending; /* flag to signal read / write op is incomplete */
92 int event; /* current event */
98 enum oid_proto protocol; /* what application protocol are we talking? */
99 int (*f_put)(COMSTACK handle, char *buf, int size);
100 int (*f_get)(COMSTACK handle, char **buf, int *bufsize);
101 int (*f_more)(COMSTACK handle);
102 int (*f_connect)(COMSTACK handle, void *address);
103 int (*f_rcvconnect)(COMSTACK handle);
104 int (*f_bind)(COMSTACK handle, void *address, int mode);
107 int (*f_listen)(COMSTACK h, char *raddr, int *addrlen,
108 int (*check_ip)(void *cd, const char *a, int len, int type),
110 COMSTACK (*f_accept)(COMSTACK handle);
111 int (*f_close)(COMSTACK handle);
112 char *(*f_addrstr)(COMSTACK handle);
113 void *(*f_straddr)(COMSTACK handle, const char *str);
114 int (*f_set_blocking)(COMSTACK handle, int blocking);
117 #define cs_put(handle, buf, size) ((*(handle)->f_put)(handle, buf, size))
118 #define cs_get(handle, buf, size) ((*(handle)->f_get)(handle, buf, size))
119 #define cs_more(handle) ((*(handle)->f_more)(handle))
120 #define cs_connect(handle, address) ((*(handle)->f_connect)(handle, address))
121 #define cs_rcvconnect(handle) ((*(handle)->f_rcvconnect)(handle))
122 #define cs_bind(handle, ad, mo) ((*(handle)->f_bind)(handle, ad, mo))
123 #define cs_listen(handle, ap, al) ((*(handle)->f_listen)(handle, ap, al, 0, 0))
124 #define cs_listen_check(handle, ap, al, cf, cd) ((*(handle)->f_listen)(handle, ap, al, cf, cd))
125 #define cs_accept(handle) ((*(handle)->f_accept)(handle))
126 #define cs_close(handle) ((*(handle)->f_close)(handle))
127 #define cs_create(type, blocking, proto) ((*type)(-1, blocking, proto, 0))
128 #define cs_createbysocket(sock, type, blocking, proto) \
129 ((*type)(sock, blocking, proto, 0))
130 #define cs_type(handle) ((handle)->type)
131 #define cs_fileno(handle) ((handle)->iofile)
132 #define cs_stackerr(handle) ((handle)->stackerr)
133 #define cs_getstate(handle) ((handle)->getstate)
134 #define cs_errno(handle) ((handle)->cerrno)
135 #define cs_getproto(handle) ((handle)->protocol)
136 #define cs_addrstr(handle) ((*(handle)->f_addrstr)(handle))
137 #define cs_straddr(handle, str) ((*(handle)->f_straddr)(handle, str))
138 #define cs_want_read(handle) ((handle)->io_pending & CS_WANT_READ)
139 #define cs_want_write(handle) ((handle)->io_pending & CS_WANT_WRITE)
140 #define cs_set_blocking(handle,blocking) ((handle)->f_set_blocking(handle, blocking)
142 #define CS_WANT_READ 1
143 #define CS_WANT_WRITE 2
145 YAZ_EXPORT int cs_look (COMSTACK);
146 YAZ_EXPORT const char *cs_strerror(COMSTACK h);
147 YAZ_EXPORT const char *cs_errmsg(int n);
148 YAZ_EXPORT COMSTACK cs_create_host(const char *type_and_host,
149 int blocking, void **vp);
150 YAZ_EXPORT int cs_complete_auto(const unsigned char *buf, int len);
164 /* backwards compatibility */
165 #define CS_SR PROTO_SR
166 #define CS_Z3950 PROTO_Z3950