]> arthur.barton.de Git - netatalk.git/blob - include/atalk/dsi.h
Remove bdb env on exit
[netatalk.git] / include / atalk / dsi.h
1 /*
2  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3  * All rights reserved.
4  */
5
6 #ifndef _ATALK_DSI_H 
7 #define _ATALK_DSI_H
8
9 #include <sys/cdefs.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <signal.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15
16 #include <atalk/afp.h>
17 #include <atalk/server_child.h>
18 #include <atalk/globals.h>
19 #include <netatalk/endian.h>
20
21
22 /* What a DSI packet looks like:
23  0                               32
24  |-------------------------------|
25  |flags  |command| requestID     |
26  |-------------------------------|
27  |error code/enclosed data offset|
28  |-------------------------------|
29  |total data length              |
30  |-------------------------------|
31  |reserved field                 |
32  |-------------------------------|
33
34  CONVENTION: anything with a dsi_ prefix is kept in network byte order.
35 */
36
37 /* these need to be kept in sync w/ AFPTRANS_* in <atalk/afp.h>. 
38  * convention: AFPTRANS_* = (1 << DSI_*) */
39 typedef enum {
40   DSI_MIN = 1,
41   DSI_TCPIP = 1,
42   DSI_MAX = 1
43 } dsi_proto;
44
45 #define DSI_BLOCKSIZ 16
46 struct dsi_block {
47   u_int8_t dsi_flags;       /* packet type: request or reply */
48   u_int8_t dsi_command;     /* command */
49   u_int16_t dsi_requestID;  /* request ID */
50   u_int32_t dsi_code;       /* error code or data offset */
51   u_int32_t dsi_len;        /* total data length */
52   u_int32_t dsi_reserved;   /* reserved field */
53 };
54
55 #define DSI_CMDSIZ        8192 
56 #define DSI_DATASIZ       8192
57
58 /* child and parent processes might interpret a couple of these
59  * differently. */
60 typedef struct DSI {
61   AFPObj *AFPobj;
62   dsi_proto protocol;
63   struct dsi_block header;
64   struct sockaddr_storage server, client;
65   struct itimerval timer;
66   int      tickle;        /* tickle count */
67   int      in_write;      /* in the middle of writing multiple packets,
68                              signal handlers can't write to the socket */
69   int      msg_request;   /* pending message to the client */
70   int      down_request;  /* pending SIGUSR1 down in 5 mn */
71
72   u_int32_t attn_quantum, datasize, server_quantum;
73   u_int16_t serverID, clientID;
74   char      *status;
75   u_int8_t  commands[DSI_CMDSIZ], data[DSI_DATASIZ];
76   size_t statuslen;
77   size_t datalen, cmdlen;
78   off_t  read_count, write_count;
79   uint32_t flags;             /* DSI flags like DSI_SLEEPING, DSI_DISCONNECTED */
80   const char *program; 
81   int socket, serversock;
82
83   /* protocol specific open/close, send/receive
84    * send/receive fill in the header and use dsi->commands.
85    * write/read just write/read data */
86   pid_t  (*proto_open)(struct DSI *);
87   void   (*proto_close)(struct DSI *);
88
89   /* url registered with slpd */
90 #ifdef USE_SRVLOC
91   char srvloc_url[512];
92 #endif 
93
94 #ifdef USE_ZEROCONF
95   char *bonjourname;      /* server name as UTF8 maxlen MAXINSTANCENAMELEN */
96   int zeroconf_registered;
97 #endif
98
99   /* DSI readahead buffer used for buffered reads in dsi_peek */
100   size_t dsireadbuf; /* size of the DSI readahead buffer used in dsi_peek() */
101   char *buffer;
102   char *start;
103   char *eof;
104   char *end;
105 } DSI;
106   
107 /* DSI flags */
108 #define DSIFL_REQUEST    0x00
109 #define DSIFL_REPLY      0x01
110 #define DSIFL_MAX        0x01
111
112 /* DSI session options */
113 #define DSIOPT_SERVQUANT 0x00   /* server request quantum */
114 #define DSIOPT_ATTNQUANT 0x01   /* attention quantum */
115 #define DSIOPT_REPLCSIZE 0x02   /* AFP replaycache size supported by the server (that's us) */
116
117 /* DSI Commands */
118 #define DSIFUNC_CLOSE   1       /* DSICloseSession */
119 #define DSIFUNC_CMD     2       /* DSICommand */
120 #define DSIFUNC_STAT    3       /* DSIGetStatus */
121 #define DSIFUNC_OPEN    4       /* DSIOpenSession */
122 #define DSIFUNC_TICKLE  5       /* DSITickle */
123 #define DSIFUNC_WRITE   6       /* DSIWrite */
124 #define DSIFUNC_ATTN    8       /* DSIAttention */
125 #define DSIFUNC_MAX     8       /* largest command */
126
127 /* DSI Error codes: most of these aren't used. */
128 #define DSIERR_OK       0x0000
129 #define DSIERR_BADVERS  0xfbd6
130 #define DSIERR_BUFSMALL 0xfbd5
131 #define DSIERR_NOSESS   0xfbd4
132 #define DSIERR_NOSERV   0xfbd3
133 #define DSIERR_PARM     0xfbd2
134 #define DSIERR_SERVBUSY 0xfbd1
135 #define DSIERR_SESSCLOS 0xfbd0
136 #define DSIERR_SIZERR   0xfbcf
137 #define DSIERR_TOOMANY  0xfbce
138 #define DSIERR_NOACK    0xfbcd
139
140 /* server and client quanta */
141 #define DSI_DEFQUANT        2           /* default attention quantum size */
142 #define DSI_SERVQUANT_MAX   0xffffffff  /* server quantum */
143 #define DSI_SERVQUANT_MIN   32000       /* minimum server quantum */
144 #define DSI_SERVQUANT_DEF   0x0004A2E0L /* default server quantum */
145
146 /* default port number */
147 #define DSI_AFPOVERTCP_PORT 548
148
149 /* DSI session State flags */
150 #define DSI_DATA             (1 << 0) /* we have received a DSI command */
151 #define DSI_RUNNING          (1 << 1) /* we have received a AFP command */
152 #define DSI_SLEEPING         (1 << 2) /* we're sleeping after FPZzz */
153 #define DSI_EXTSLEEP         (1 << 3) /* we're sleeping after FPZzz */
154 #define DSI_DISCONNECTED     (1 << 4) /* we're in diconnected state after a socket error */
155 #define DSI_DIE              (1 << 5) /* SIGUSR1, going down in 5 minutes */
156 #define DSI_NOREPLY          (1 << 6) /* in dsi_write we generate our own replies */
157 #define DSI_RECONSOCKET      (1 << 7) /* we have a new socket from primary reconnect */
158 #define DSI_RECONINPROG      (1 << 8) /* used in the new session in reconnect */
159 #define DSI_AFP_LOGGED_OUT   (1 << 9) /* client called afp_logout, quit on next EOF from socket */
160 #if 0
161 #define DSI_GOT_ECONNRESET   (1 << 10) /* got ECONNRESET from client => exit */
162 #endif
163
164 /* basic initialization: dsi_init.c */
165 extern DSI *dsi_init (const dsi_proto /*protocol*/,
166                           const char * /*program*/, 
167                           const char * /*host*/, const char * /*address*/,
168                           const char * /*port*/, const int /*proxy*/,
169                           const u_int32_t /* server quantum */);
170 extern void dsi_setstatus (DSI *, char *, const size_t);
171
172 /* in dsi_getsess.c */
173 extern afp_child_t *dsi_getsession (DSI *, server_child *, const int);
174 extern void dsi_kill (int);
175
176
177 /* DSI Commands: individual files */
178 extern void dsi_opensession (DSI *);
179 extern int  dsi_attention (DSI *, AFPUserBytes);
180 extern int  dsi_cmdreply (DSI *, const int);
181 extern int dsi_tickle (DSI *);
182 extern void dsi_getstatus (DSI *);
183 extern void dsi_close (DSI *);
184
185 #define DSI_NOWAIT 1
186 /* low-level stream commands -- in dsi_stream.c */
187 extern ssize_t dsi_stream_write (DSI *, void *, const size_t, const int mode);
188 extern size_t dsi_stream_read (DSI *, void *, const size_t);
189 extern int dsi_stream_send (DSI *, void *, size_t);
190 extern int dsi_stream_receive (DSI *);
191 extern int dsi_disconnect(DSI *dsi);
192
193 #ifdef WITH_SENDFILE
194 extern ssize_t dsi_stream_read_file(DSI *, int, off_t off, const size_t len);
195 #endif
196
197 /* client writes -- dsi_write.c */
198 extern size_t dsi_writeinit (DSI *, void *, const size_t);
199 extern size_t dsi_write (DSI *, void *, const size_t);
200 extern void   dsi_writeflush (DSI *);
201 #define dsi_wrtreply(a,b)  dsi_cmdreply(a,b)
202
203 /* client reads -- dsi_read.c */
204 extern ssize_t dsi_readinit (DSI *, void *, const size_t, const size_t,
205                                  const int);
206 extern ssize_t dsi_read (DSI *, void *, const size_t);
207 extern void dsi_readdone (DSI *);
208
209 /* some useful macros */
210 #define dsi_serverID(x)   ((x)->serverID++)
211 #define dsi_send(x)       do { \
212     (x)->header.dsi_len = htonl((x)->cmdlen); \
213     dsi_stream_send((x), (x)->commands, (x)->cmdlen); \
214 } while (0)
215
216 #endif /* atalk/dsi.h */