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