Fixed bug in inetd code. The server listened on tcp:@:9999 even
[yaz-moved-to-github.git] / include / comstack.h
1 /*
2  * Copyright (c) 1995, 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  * $Log: comstack.h,v $
27  * Revision 1.19  1997-09-29 07:16:14  adam
28  * Array cs_errlist no longer global.
29  *
30  * Revision 1.18  1997/09/17 12:10:31  adam
31  * YAZ version 1.4.
32  *
33  * Revision 1.17  1997/09/01 08:49:47  adam
34  * New windows NT/95 port using MSV5.0. To export DLL functions the
35  * YAZ_EXPORT modifier was added. Defined in yconfig.h.
36  *
37  * Revision 1.16  1997/05/14 06:53:37  adam
38  * C++ support.
39  *
40  * Revision 1.15  1996/07/06 19:58:32  quinn
41  * System headerfiles gathered in yconfig
42  *
43  * Revision 1.14  1996/02/10  12:23:41  quinn
44  * Enable inetd operations fro TCP/IP stack
45  *
46  * Revision 1.13  1995/11/01  13:54:33  quinn
47  * Minor adjustments
48  *
49  * Revision 1.12  1995/10/30  12:41:27  quinn
50  * Added hostname lookup for server.
51  *
52  * Revision 1.11  1995/09/29  17:12:01  quinn
53  * Smallish
54  *
55  * Revision 1.10  1995/09/29  17:01:48  quinn
56  * More Windows work
57  *
58  * Revision 1.9  1995/09/28  10:12:36  quinn
59  * Windows-support changes
60  *
61  * Revision 1.8  1995/09/27  15:02:46  quinn
62  * Modified function heads & prototypes.
63  *
64  * Revision 1.7  1995/06/19  12:38:24  quinn
65  * Reorganized include-files. Added small features.
66  *
67  * Revision 1.6  1995/05/30  10:54:51  quinn
68  * Added some backwards compatibility to the comstack (CS_SR->PROTO_SR)
69  *
70  * Revision 1.5  1995/05/29  08:11:31  quinn
71  * Moved oid from odr/asn to util.
72  *
73  * Revision 1.4  1995/05/16  08:50:29  quinn
74  * License, documentation, and memory fixes
75  *
76  * Revision 1.3  1995/04/20  15:12:44  quinn
77  * Cosmetic
78  *
79  * Revision 1.2  1995/04/17  11:28:17  quinn
80  * Smallish
81  *
82  * Revision 1.1  1995/03/30  09:39:40  quinn
83  * Moved .h files to include directory
84  *
85  * Revision 1.11  1995/03/27  08:36:05  quinn
86  * Some work on nonblocking operation in xmosi.c and rfct.c.
87  * Added protocol parameter to cs_create()
88  *
89  * Revision 1.10  1995/03/20  09:47:12  quinn
90  * Added server-side support to xmosi.c
91  * Fixed possible problems in rfct
92  * Other little mods
93  *
94  * Revision 1.9  1995/03/15  15:36:27  quinn
95  * Mods to support nonblocking I/O
96  *
97  * Revision 1.8  1995/03/14  17:00:07  quinn
98  * Bug-fixes - added tracing info to tcpip.c
99  *
100  * Revision 1.7  1995/03/14  10:28:35  quinn
101  * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
102  *
103  * Revision 1.6  1995/03/07  16:29:45  quinn
104  * Various fixes.
105  *
106  * Revision 1.5  1995/03/07  10:39:31  quinn
107  * Added cs_fileno
108  *
109  * Revision 1.4  1995/03/06  16:49:29  adam
110  * COMSTACK type inspection possible with cs_type.
111  *
112  * Revision 1.3  1995/02/14  11:54:48  quinn
113  * Beginning to add full CCL.
114  *
115  * Revision 1.2  1995/02/10  18:58:10  quinn
116  * Fixed tcpip_get (formerly tcpip_read).
117  * Turned tst (cli) into a proper, event-driven thing.
118  *
119  * Revision 1.1  1995/02/09  15:51:51  quinn
120  * Works better now.
121  *
122  */
123
124 #ifndef COMSTACK_H
125 #define COMSTACK_H
126
127 #define YNETINCLUDE
128 #include <yconfig.h>
129 #include <oid.h>
130 #include <xmalloc.h>
131
132 #ifdef __cplusplus
133 extern "C" {
134 #endif
135
136 #define COMSTACK_DEFAULT_TIMEOUT -1  /* not used yet */
137
138 struct comstack;
139 typedef struct comstack *COMSTACK;
140 typedef COMSTACK (*CS_TYPE)(int s, int blocking, int protocol);
141
142 struct comstack
143 {
144     CS_TYPE type;
145     int cerrno;     /* current error code of this stack */
146     char *stackerr;/* current lower-layer error string, or null if none */
147     int iofile;    /* UNIX file descriptor for iochannel */
148     int timeout;   /* how long to wait for trailing blocks (ignored for now) */
149     void *cprivate;/* state info for lower stack */
150     int more;      /* connection has extra data in buffer */
151     int state;     /* current state */
152 #define CS_UNBND      0
153 #define CS_IDLE       1
154 #define CS_INCON      2
155 #define CS_OUTCON     3
156 #define CS_DATAXFER   4
157     int newfd;     /* storing new descriptor between listen and accept */
158     int blocking;  /* is this link (supposed to be) blocking? */
159     int event;     /* current event */
160 #define CS_NONE       0
161 #define CS_CONNECT    1
162 #define CS_DISCON     2
163 #define CS_LISTEN     3
164 #define CS_DATA       4
165     enum oid_proto protocol;  /* what application protocol are we talking? */
166     int (*f_look)(COMSTACK handle);
167     int (*f_put)(COMSTACK handle, char *buf, int size);
168     int (*f_get)(COMSTACK handle, char **buf, int *bufsize);
169     int (*f_more)(COMSTACK handle);
170     int (*f_connect)(COMSTACK handle, void *address);
171     int (*f_rcvconnect)(COMSTACK handle);
172     int (*f_bind)(COMSTACK handle, void *address, int mode);
173 #define CS_CLIENT 0
174 #define CS_SERVER 1
175     int (*f_listen)(COMSTACK handle, char *addrp, int *addrlen);
176     COMSTACK (*f_accept)(COMSTACK handle);
177     int (*f_close)(COMSTACK handle);
178     char *(*f_addrstr)(COMSTACK handle);
179     void *(*f_straddr)(COMSTACK handle, const char *str);
180 };
181
182 #define cs_put(handle, buf, size) ((*(handle)->f_put)(handle, buf, size))
183 #define cs_get(handle, buf, size) ((*(handle)->f_get)(handle, buf, size))
184 #define cs_more(handle) ((*(handle)->f_more)(handle))
185 #define cs_connect(handle, address) ((*(handle)->f_connect)(handle, address))
186 #define cs_rcvconnect(handle) ((*(handle)->f_rcvconnect)(handle))
187 #define cs_bind(handle, ad, mo) ((*(handle)->f_bind)(handle, ad, mo))
188 #define cs_listen(handle, ap, al) ((*(handle)->f_listen)(handle, ap, al))
189 #define cs_accept(handle) ((*(handle)->f_accept)(handle))
190 #define cs_close(handle) ((*(handle)->f_close)(handle))
191 #define cs_create(type, blocking, proto) ((*type)(-1, blocking, proto))
192 #define cs_createbysocket(sock, type, blocking, proto) \
193         ((*type)(sock, blocking, proto))
194 #define cs_type(handle) ((handle)->type)
195 #define cs_fileno(handle) ((handle)->iofile)
196 #define cs_stackerr(handle) ((handle)->stackerr)
197 #define cs_getstate(handle) ((handle)->getstate)
198 #define cs_errno(handle) ((handle)->cerrno)
199 #define cs_getproto(handle) ((handle)->protocol)
200 #define cs_addrstr(handle) ((*(handle)->f_addrstr)(handle))
201 #define cs_straddr(handle, str) ((*(handle)->f_straddr)(handle, str))
202
203 YAZ_EXPORT const char *cs_strerror(COMSTACK h);
204 YAZ_EXPORT const char *cs_errmsg(int n);
205
206 /*
207  * error management.
208  */
209
210 #define CSNONE     0
211 #define CSYSERR    1
212 #define CSOUTSTATE 2
213 #define CSNODATA   3
214 #define CSWRONGBUF 4
215
216 /* backwards compatibility */
217 #define CS_SR     PROTO_SR
218 #define CS_Z3950  PROTO_Z3950
219
220 #ifdef __cplusplus
221 }
222 #endif
223
224 #endif