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