]> arthur.barton.de Git - netatalk.git/blob - include/atalk/dsi.h
Merge 2-1
[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
14 #include <netinet/in.h>
15 #include <atalk/afp.h>
16 #include <atalk/server_child.h>
17 #include <netatalk/endian.h>
18
19 #ifdef __OpenBSD__
20 #include <sys/socket.h>
21 #endif
22
23 /* What a DSI packet looks like:
24  0                               32
25  |-------------------------------|
26  |flags  |command| requestID     |
27  |-------------------------------|
28  |error code/enclosed data offset|
29  |-------------------------------|
30  |total data length              |
31  |-------------------------------|
32  |reserved field                 |
33  |-------------------------------|
34
35  CONVENTION: anything with a dsi_ prefix is kept in network byte order.
36 */
37
38 /* these need to be kept in sync w/ AFPTRANS_* in <atalk/afp.h>. 
39  * convention: AFPTRANS_* = (1 << DSI_*) */
40 typedef enum {
41   DSI_MIN = 1,
42   DSI_TCPIP = 1,
43   DSI_MAX = 1
44 } dsi_proto;
45
46 #define DSI_BLOCKSIZ 16
47 struct dsi_block {
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 */
54 };
55
56 #define DSI_CMDSIZ        8192 
57 #define DSI_DATASIZ       8192
58 /* child and parent processes might interpret a couple of these
59  * differently. */
60 typedef struct DSI {
61   dsi_proto protocol;
62   struct dsi_block header;
63   struct sockaddr_storage server, client;
64   
65   struct itimerval timer;
66
67   int      in_write;      /* in the middle of writing multiple packets, signal handlers
68                            * can't write to the socket 
69                           */
70   int      msg_request;   /* pending message to the client */
71   int      down_request;  /* pending SIGUSR1 down in 5 mn */
72
73   u_int32_t attn_quantum, datasize, server_quantum;
74   u_int16_t serverID, clientID;
75   char      *status;
76   u_int8_t  commands[DSI_CMDSIZ], data[DSI_DATASIZ];
77   size_t statuslen;
78   size_t datalen, cmdlen;
79   off_t  read_count, write_count;
80   int asleep; /* client won't reply AFP 0x7a ? */
81   /* inited = initialized?, child = a child?, noreply = send reply? */
82   char child, inited, noreply;
83   const char *program; 
84   int socket, serversock;
85
86   /* protocol specific open/close, send/receive
87    * send/receive fill in the header and use dsi->commands.
88    * write/read just write/read data */
89   pid_t  (*proto_open)(struct DSI *);
90   void   (*proto_close)(struct DSI *);
91
92   /* url registered with slpd */
93 #ifdef USE_SRVLOC
94   char srvloc_url[512];
95 #endif 
96
97 #ifdef USE_ZEROCONF
98   int zeroconf_registered;
99 #endif
100
101   /* buffer for OSX deadlock */
102   char *buffer;
103   char *start;
104   char *eof;
105   char *end;
106   int  maxsize;
107
108 } DSI;
109   
110 /* DSI flags */
111 #define DSIFL_REQUEST    0x00
112 #define DSIFL_REPLY      0x01
113 #define DSIFL_MAX        0x01
114
115 /* DSI session options */
116 #define DSIOPT_SERVQUANT 0x00   /* server request quantum */
117 #define DSIOPT_ATTNQUANT 0x01   /* attention quantum */
118
119 /* DSI Commands */
120 #define DSIFUNC_CLOSE   1       /* DSICloseSession */
121 #define DSIFUNC_CMD     2       /* DSICommand */
122 #define DSIFUNC_STAT    3       /* DSIGetStatus */
123 #define DSIFUNC_OPEN    4       /* DSIOpenSession */
124 #define DSIFUNC_TICKLE  5       /* DSITickle */
125 #define DSIFUNC_WRITE   6       /* DSIWrite */
126 #define DSIFUNC_ATTN    8       /* DSIAttention */
127 #define DSIFUNC_MAX     8       /* largest command */
128
129 /* DSI Error codes: most of these aren't used. */
130 #define DSIERR_OK       0x0000
131 #define DSIERR_BADVERS  0xfbd6
132 #define DSIERR_BUFSMALL 0xfbd5
133 #define DSIERR_NOSESS   0xfbd4
134 #define DSIERR_NOSERV   0xfbd3
135 #define DSIERR_PARM     0xfbd2
136 #define DSIERR_SERVBUSY 0xfbd1
137 #define DSIERR_SESSCLOS 0xfbd0
138 #define DSIERR_SIZERR   0xfbcf
139 #define DSIERR_TOOMANY  0xfbce
140 #define DSIERR_NOACK    0xfbcd
141
142 /* server and client quanta */
143 #define DSI_DEFQUANT        2           /* default attention quantum size */
144 #define DSI_SERVQUANT_MAX   0xffffffff  /* server quantum */
145 #define DSI_SERVQUANT_MIN   32000       /* minimum server quantum */
146 #define DSI_SERVQUANT_DEF   0x0004A2E0L /* default server quantum */
147
148 /* default port number */
149 #define DSI_AFPOVERTCP_PORT 548
150
151 /* basic initialization: dsi_init.c */
152 extern DSI *dsi_init (const dsi_proto /*protocol*/,
153                           const char * /*program*/, 
154                           const char * /*host*/, const char * /*address*/,
155                           const char * /*port*/, const int /*proxy*/,
156                           const u_int32_t /* server quantum */);
157 extern void dsi_setstatus (DSI *, char *, const size_t);
158
159 /* in dsi_getsess.c */
160 extern DSI *dsi_getsession (DSI *, server_child *, const int);
161 extern void dsi_kill (int);
162
163
164 /* DSI Commands: individual files */
165 extern void dsi_opensession (DSI *);
166 extern int  dsi_attention (DSI *, AFPUserBytes);
167 extern int  dsi_cmdreply (DSI *, const int);
168 extern int dsi_tickle (DSI *);
169 extern void dsi_getstatus (DSI *);
170 extern void dsi_close (DSI *);
171 extern void dsi_sleep (DSI *, const int );
172
173 #define DSI_NOWAIT 1
174 /* low-level stream commands -- in dsi_stream.c */
175 extern ssize_t dsi_stream_write (DSI *, void *, const size_t, const int mode);
176 extern size_t dsi_stream_read (DSI *, void *, const size_t);
177 extern int dsi_stream_send (DSI *, void *, size_t);
178 extern int dsi_stream_receive (DSI *, void *, const size_t, size_t *);
179
180 #ifdef WITH_SENDFILE
181 extern ssize_t dsi_stream_read_file(DSI *, int, off_t off, const size_t len);
182 #endif
183
184 /* client writes -- dsi_write.c */
185 extern size_t dsi_writeinit (DSI *, void *, const size_t);
186 extern size_t dsi_write (DSI *, void *, const size_t);
187 extern void   dsi_writeflush (DSI *);
188 #define dsi_wrtreply(a,b)  dsi_cmdreply(a,b)
189
190 /* client reads -- dsi_read.c */
191 extern ssize_t dsi_readinit (DSI *, void *, const size_t, const size_t,
192                                  const int);
193 extern ssize_t dsi_read (DSI *, void *, const size_t);
194 extern void dsi_readdone (DSI *);
195
196 /* some useful macros */
197 #define dsi_serverID(x)   ((x)->serverID++)
198 #define dsi_send(x)       do { \
199     (x)->header.dsi_len = htonl((x)->cmdlen); \
200     dsi_stream_send((x), (x)->commands, (x)->cmdlen); \
201 } while (0)
202 #define dsi_receive(x)    (dsi_stream_receive((x), (x)->commands, \
203                                               DSI_CMDSIZ, &(x)->cmdlen))
204 #endif /* atalk/dsi.h */