2 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
11 #include <sys/socket.h>
13 #include <arpa/inet.h>
14 #include <netinet/in.h>
16 #include <atalk/afp.h>
17 #include <atalk/server_child.h>
18 #include <atalk/globals.h>
20 /* What a DSI packet looks like:
22 |-------------------------------|
23 |flags |command| requestID |
24 |-------------------------------|
25 |error code/enclosed data offset|
26 |-------------------------------|
28 |-------------------------------|
30 |-------------------------------|
32 CONVENTION: anything with a dsi_ prefix is kept in network byte order.
35 /* these need to be kept in sync w/ AFPTRANS_* in <atalk/afp.h>.
36 * convention: AFPTRANS_* = (1 << DSI_*) */
43 #define DSI_BLOCKSIZ 16
45 uint8_t dsi_flags; /* packet type: request or reply */
46 uint8_t dsi_command; /* command */
47 uint16_t dsi_requestID; /* request ID */
49 uint32_t dsi_code; /* error code */
50 uint32_t dsi_doff; /* data offset */
52 uint32_t dsi_len; /* total data length */
53 uint32_t dsi_reserved; /* reserved field */
56 #define DSI_DATASIZ 65536
58 /* child and parent processes might interpret a couple of these
61 struct DSI *next; /* multiple listening addresses */
66 struct dsi_block header;
67 struct sockaddr_storage server, client;
68 struct itimerval timer;
69 int tickle; /* tickle count */
70 int in_write; /* in the middle of writing multiple packets,
71 signal handlers can't write to the socket */
72 int msg_request; /* pending message to the client */
73 int down_request; /* pending SIGUSR1 down in 5 mn */
75 uint32_t attn_quantum, datasize, server_quantum;
76 uint16_t serverID, clientID;
77 uint8_t *commands; /* DSI recieve buffer */
78 uint8_t data[DSI_DATASIZ]; /* DSI reply buffer */
79 size_t datalen, cmdlen;
80 off_t read_count, write_count;
81 uint32_t flags; /* DSI flags like DSI_SLEEPING, DSI_DISCONNECTED */
82 int socket; /* AFP session socket */
83 int serversock; /* listening socket */
85 /* DSI readahead buffer used for buffered reads in dsi_peek */
86 size_t dsireadbuf; /* size of the DSI readahead buffer used in dsi_peek() */
87 char *buffer; /* buffer start */
88 char *start; /* current buffer head */
89 char *eof; /* end of currently used buffer */
93 char *bonjourname; /* server name as UTF8 maxlen MAXINSTANCENAMELEN */
94 int zeroconf_registered;
97 /* protocol specific open/close, send/receive
98 * send/receive fill in the header and use dsi->commands.
99 * write/read just write/read data */
100 pid_t (*proto_open)(struct DSI *);
101 void (*proto_close)(struct DSI *);
105 #define DSIFL_REQUEST 0x00
106 #define DSIFL_REPLY 0x01
107 #define DSIFL_MAX 0x01
109 /* DSI session options */
110 #define DSIOPT_SERVQUANT 0x00 /* server request quantum */
111 #define DSIOPT_ATTNQUANT 0x01 /* attention quantum */
112 #define DSIOPT_REPLCSIZE 0x02 /* AFP replaycache size supported by the server (that's us) */
115 #define DSIFUNC_CLOSE 1 /* DSICloseSession */
116 #define DSIFUNC_CMD 2 /* DSICommand */
117 #define DSIFUNC_STAT 3 /* DSIGetStatus */
118 #define DSIFUNC_OPEN 4 /* DSIOpenSession */
119 #define DSIFUNC_TICKLE 5 /* DSITickle */
120 #define DSIFUNC_WRITE 6 /* DSIWrite */
121 #define DSIFUNC_ATTN 8 /* DSIAttention */
122 #define DSIFUNC_MAX 8 /* largest command */
124 /* DSI Error codes: most of these aren't used. */
125 #define DSIERR_OK 0x0000
126 #define DSIERR_BADVERS 0xfbd6
127 #define DSIERR_BUFSMALL 0xfbd5
128 #define DSIERR_NOSESS 0xfbd4
129 #define DSIERR_NOSERV 0xfbd3
130 #define DSIERR_PARM 0xfbd2
131 #define DSIERR_SERVBUSY 0xfbd1
132 #define DSIERR_SESSCLOS 0xfbd0
133 #define DSIERR_SIZERR 0xfbcf
134 #define DSIERR_TOOMANY 0xfbce
135 #define DSIERR_NOACK 0xfbcd
137 /* server and client quanta */
138 #define DSI_DEFQUANT 2 /* default attention quantum size */
139 #define DSI_SERVQUANT_MAX 0xffffffff /* server quantum */
140 #define DSI_SERVQUANT_MIN 32000 /* minimum server quantum */
141 #define DSI_SERVQUANT_DEF 0x100000L /* default server quantum (1 MB) */
143 /* default port number */
144 #define DSI_AFPOVERTCP_PORT 548
146 /* DSI session State flags */
147 #define DSI_DATA (1 << 0) /* we have received a DSI command */
148 #define DSI_RUNNING (1 << 1) /* we have received a AFP command */
149 #define DSI_SLEEPING (1 << 2) /* we're sleeping after FPZzz */
150 #define DSI_EXTSLEEP (1 << 3) /* we're sleeping after FPZzz */
151 #define DSI_DISCONNECTED (1 << 4) /* we're in diconnected state after a socket error */
152 #define DSI_DIE (1 << 5) /* SIGUSR1, going down in 5 minutes */
153 #define DSI_NOREPLY (1 << 6) /* in dsi_write we generate our own replies */
154 #define DSI_RECONSOCKET (1 << 7) /* we have a new socket from primary reconnect */
155 #define DSI_RECONINPROG (1 << 8) /* used in the new session in reconnect */
156 #define DSI_AFP_LOGGED_OUT (1 << 9) /* client called afp_logout, quit on next EOF from socket */
158 /* basic initialization: dsi_init.c */
159 extern DSI *dsi_init(AFPObj *obj, const char *hostname, const char *address, const char *port);
160 extern void dsi_setstatus (DSI *, char *, const size_t);
161 extern int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address, const char *port);
162 extern void dsi_free(DSI *dsi);
164 /* in dsi_getsess.c */
165 extern int dsi_getsession (DSI *, server_child_t *, const int, afp_child_t **);
166 extern void dsi_kill (int);
169 /* DSI Commands: individual files */
170 extern void dsi_opensession (DSI *);
171 extern int dsi_attention (DSI *, AFPUserBytes);
172 extern int dsi_cmdreply (DSI *, const int);
173 extern int dsi_tickle (DSI *);
174 extern void dsi_getstatus (DSI *);
175 extern void dsi_close (DSI *);
178 #define DSI_MSG_MORE 2
180 /* low-level stream commands -- in dsi_stream.c */
181 extern ssize_t dsi_stream_write (DSI *, void *, const size_t, const int mode);
182 extern size_t dsi_stream_read (DSI *, void *, const size_t);
183 extern int dsi_stream_send (DSI *, void *, size_t);
184 extern int dsi_stream_receive (DSI *);
185 extern int dsi_disconnect(DSI *dsi);
188 extern ssize_t dsi_stream_read_file(DSI *, int, off_t off, const size_t len, const int err);
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); \
210 #endif /* atalk/dsi.h */