2 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
10 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <atalk/afp.h>
16 #include <atalk/server_child.h>
17 #include <netatalk/endian.h>
20 #include <sys/socket.h>
23 /* What a DSI packet looks like:
25 |-------------------------------|
26 |flags |command| requestID |
27 |-------------------------------|
28 |error code/enclosed data offset|
29 |-------------------------------|
31 |-------------------------------|
33 |-------------------------------|
35 CONVENTION: anything with a dsi_ prefix is kept in network byte order.
38 /* these need to be kept in sync w/ AFPTRANS_* in <atalk/afp.h>.
39 * convention: AFPTRANS_* = (1 << DSI_*) */
46 #define DSI_BLOCKSIZ 16
48 u_int8_t dsi_flags; /* packet type: request or reply */
49 u_int8_t dsi_command; /* command */
50 u_int16_t dsi_requestID; /* request ID */
51 u_int32_t dsi_code; /* error code or data offset */
52 u_int32_t dsi_len; /* total data length */
53 u_int32_t dsi_reserved; /* reserved field */
56 #define DSI_CMDSIZ 8192
57 #define DSI_DATASIZ 8192
58 /* child and parent processes might interpret a couple of these
62 struct dsi_block header;
63 struct sockaddr_storage server, client;
64 struct itimerval timer;
65 int tickle; /* tickle count */
66 int in_write; /* in the middle of writing multiple packets,
67 signal handlers can't write to the socket */
68 int msg_request; /* pending message to the client */
69 int down_request; /* pending SIGUSR1 down in 5 mn */
71 u_int32_t attn_quantum, datasize, server_quantum;
72 u_int16_t serverID, clientID;
74 u_int8_t commands[DSI_CMDSIZ], data[DSI_DATASIZ];
76 size_t datalen, cmdlen;
77 off_t read_count, write_count;
78 uint32_t flags; /* DSI flags like DSI_SLEEPING, DSI_DISCONNECTED */
80 int socket, serversock;
82 /* protocol specific open/close, send/receive
83 * send/receive fill in the header and use dsi->commands.
84 * write/read just write/read data */
85 pid_t (*proto_open)(struct DSI *);
86 void (*proto_close)(struct DSI *);
88 /* url registered with slpd */
94 int zeroconf_registered;
97 /* buffer for OSX deadlock */
107 #define DSIFL_REQUEST 0x00
108 #define DSIFL_REPLY 0x01
109 #define DSIFL_MAX 0x01
111 /* DSI session options */
112 #define DSIOPT_SERVQUANT 0x00 /* server request quantum */
113 #define DSIOPT_ATTNQUANT 0x01 /* attention quantum */
114 #define DSIOPT_REPLCSIZE 0x02 /* AFP replaycache size supported by the server (that's us) */
117 #define DSIFUNC_CLOSE 1 /* DSICloseSession */
118 #define DSIFUNC_CMD 2 /* DSICommand */
119 #define DSIFUNC_STAT 3 /* DSIGetStatus */
120 #define DSIFUNC_OPEN 4 /* DSIOpenSession */
121 #define DSIFUNC_TICKLE 5 /* DSITickle */
122 #define DSIFUNC_WRITE 6 /* DSIWrite */
123 #define DSIFUNC_ATTN 8 /* DSIAttention */
124 #define DSIFUNC_MAX 8 /* largest command */
126 /* DSI Error codes: most of these aren't used. */
127 #define DSIERR_OK 0x0000
128 #define DSIERR_BADVERS 0xfbd6
129 #define DSIERR_BUFSMALL 0xfbd5
130 #define DSIERR_NOSESS 0xfbd4
131 #define DSIERR_NOSERV 0xfbd3
132 #define DSIERR_PARM 0xfbd2
133 #define DSIERR_SERVBUSY 0xfbd1
134 #define DSIERR_SESSCLOS 0xfbd0
135 #define DSIERR_SIZERR 0xfbcf
136 #define DSIERR_TOOMANY 0xfbce
137 #define DSIERR_NOACK 0xfbcd
139 /* server and client quanta */
140 #define DSI_DEFQUANT 2 /* default attention quantum size */
141 #define DSI_SERVQUANT_MAX 0xffffffff /* server quantum */
142 #define DSI_SERVQUANT_MIN 32000 /* minimum server quantum */
143 #define DSI_SERVQUANT_DEF 0x0004A2E0L /* default server quantum */
145 /* default port number */
146 #define DSI_AFPOVERTCP_PORT 548
148 /* DSI session State flags */
149 #define DSI_DATA (1 << 0) /* we have received a DSI command */
150 #define DSI_RUNNING (1 << 1) /* we have received a AFP command */
151 #define DSI_SLEEPING (1 << 2) /* we're sleeping after FPZzz */
152 #define DSI_EXTSLEEP (1 << 3) /* we're sleeping after FPZzz */
153 #define DSI_DISCONNECTED (1 << 4) /* we're in diconnected state after a socket error */
154 #define DSI_DIE (1 << 5) /* SIGUSR1, going down in 5 minutes */
155 #define DSI_NOREPLY (1 << 6) /* in dsi_write we generate our own replies */
156 #define DSI_RECONSOCKET (1 << 7) /* we have a new socket from primary reconnect */
157 #define DSI_RECONINPROG (1 << 8) /* used in the new session in reconnect */
159 /* basic initialization: dsi_init.c */
160 extern DSI *dsi_init (const dsi_proto /*protocol*/,
161 const char * /*program*/,
162 const char * /*host*/, const char * /*address*/,
163 const char * /*port*/, const int /*proxy*/,
164 const u_int32_t /* server quantum */);
165 extern void dsi_setstatus (DSI *, char *, const size_t);
167 /* in dsi_getsess.c */
168 extern afp_child_t *dsi_getsession (DSI *, server_child *, const int);
169 extern void dsi_kill (int);
172 /* DSI Commands: individual files */
173 extern void dsi_opensession (DSI *);
174 extern int dsi_attention (DSI *, AFPUserBytes);
175 extern int dsi_cmdreply (DSI *, const int);
176 extern int dsi_tickle (DSI *);
177 extern void dsi_getstatus (DSI *);
178 extern void dsi_close (DSI *);
181 /* low-level stream commands -- in dsi_stream.c */
182 extern ssize_t dsi_stream_write (DSI *, void *, const size_t, const int mode);
183 extern size_t dsi_stream_read (DSI *, void *, const size_t);
184 extern int dsi_stream_send (DSI *, void *, size_t);
185 extern int dsi_stream_receive (DSI *, void *, const size_t, size_t *);
188 extern ssize_t dsi_stream_read_file(DSI *, int, off_t off, const size_t len);
191 /* client writes -- dsi_write.c */
192 extern size_t dsi_writeinit (DSI *, void *, const size_t);
193 extern size_t dsi_write (DSI *, void *, const size_t);
194 extern void dsi_writeflush (DSI *);
195 #define dsi_wrtreply(a,b) dsi_cmdreply(a,b)
197 /* client reads -- dsi_read.c */
198 extern ssize_t dsi_readinit (DSI *, void *, const size_t, const size_t,
200 extern ssize_t dsi_read (DSI *, void *, const size_t);
201 extern void dsi_readdone (DSI *);
203 /* some useful macros */
204 #define dsi_serverID(x) ((x)->serverID++)
205 #define dsi_send(x) do { \
206 (x)->header.dsi_len = htonl((x)->cmdlen); \
207 dsi_stream_send((x), (x)->commands, (x)->cmdlen); \
209 #define dsi_receive(x) (dsi_stream_receive((x), (x)->commands, \
210 DSI_CMDSIZ, &(x)->cmdlen))
211 #endif /* atalk/dsi.h */