salvager: Do not AskOnline nonexistent volumes
[openafs.git] / src / vol / daemon_com.h
1 /*
2  * Copyright 2006-2008, Sine Nomine Associates and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef _AFS_VOL_DAEMON_COM_H
11 #define _AFS_VOL_DAEMON_COM_H 1
12
13 /*
14  * SYNC protocol constants
15  */
16
17 /* SYNC protocol command codes
18  *
19  * command codes 0-65535 are reserved for
20  * global SYNC package command codes
21  */
22 #define SYNC_COM_CODE_USER_BASE 65536
23 #define SYNC_COM_CODE_DECL(code) (SYNC_COM_CODE_USER_BASE+(code))
24
25 /**
26  * general command codes.
27  */
28 enum SYNCOpCode {
29     SYNC_COM_CHANNEL_CLOSE    = 0,      /**< request sync channel shutdown */
30     SYNC_OP_CODE_END
31 };
32
33
34 /* SYNC protocol response codes
35  *
36  * response codes 0-65535 are reserved for
37  * global SYNC package response codes
38  */
39 #define SYNC_RES_CODE_USER_BASE 65536
40 #define SYNC_RES_CODE_DECL(code) (SYNC_RES_CODE_USER_BASE+(code))
41
42 /**
43  * general response codes.
44  */
45 enum SYNCReasonCode {
46     SYNC_OK                   = 0,  /**< sync call returned ok */
47     SYNC_DENIED               = 1,  /**< sync request denied by server */
48     SYNC_COM_ERROR            = 2,  /**< sync protocol communicaions error */
49     SYNC_BAD_COMMAND          = 3,  /**< sync command code not implemented by server */
50     SYNC_FAILED               = 4,  /**< sync server-side procedure failed */
51     SYNC_RESPONSE_CODE_END
52 };
53
54 /* SYNC protocol reason codes
55  *
56  * reason codes 0-65535 are reserved for
57  * global SYNC package reason codes
58  */
59 #define SYNC_REASON_CODE_USER_BASE 65536
60 #define SYNC_REASON_CODE_DECL(code) (SYNC_REASON_CODE_USER_BASE+(code))
61
62 /* general reason codes */
63 #define SYNC_REASON_NONE                 0
64 #define SYNC_REASON_MALFORMED_PACKET     1   /**< command packet was malformed */
65 #define SYNC_REASON_NOMEM                2   /**< sync server out of memory */
66 #define SYNC_REASON_ENCODING_ERROR       3
67 #define SYNC_REASON_PAYLOAD_TOO_BIG      4   /**< payload too big for response packet buffer */
68
69 /* SYNC protocol flags
70  *
71  * flag bits 0-7 are reserved for
72  * global SYNC package flags
73  */
74 #define SYNC_FLAG_CODE_USER_BASE 8
75 #define SYNC_FLAG_CODE_DECL(code) (1 << (SYNC_FLAG_CODE_USER_BASE+(code)))
76
77 /* general flag codes */
78 #define SYNC_FLAG_CHANNEL_SHUTDOWN   0x1
79 #define SYNC_FLAG_DAFS_EXTENSIONS    0x2   /* signal that other end of socket is compiled
80                                             * with demand attach extensions */
81
82 /* SYNC protocol response buffers */
83 #define SYNC_PROTO_MAX_LEN     768  /* maximum size of sync protocol message */
84
85 /* use a large type to get proper buffer alignment so we can safely cast the pointer */
86 #define SYNC_PROTO_BUF_DECL(buf) \
87     afs_int64 _##buf##_l[SYNC_PROTO_MAX_LEN/sizeof(afs_int64)]; \
88     char * buf = (char *)(_##buf##_l)
89
90 #ifdef USE_UNIX_SOCKETS
91 #include <afs/afsutil.h>
92 #include <sys/un.h>
93 #define SYNC_SOCK_DOMAIN AF_UNIX
94 typedef struct sockaddr_un SYNC_sockaddr_t;
95 #else  /* USE_UNIX_SOCKETS */
96 #define SYNC_SOCK_DOMAIN AF_INET
97 typedef struct sockaddr_in SYNC_sockaddr_t;
98 #endif /* USE_UNIX_SOCKETS */
99
100 /**
101  * sync server endpoint address.
102  */
103 typedef struct SYNC_endpoint {
104     int domain;     /**< socket domain */
105     afs_uint16 in;  /**< localhost ipv4 tcp port number */
106     char * un;      /**< unix domain socket filename (not a full path) */
107 } SYNC_endpoint_t;
108
109 #define SYNC_ENDPOINT_DECL(in_port, un_path) \
110     { SYNC_SOCK_DOMAIN, in_port, un_path }
111
112
113 /**
114  * SYNC server state structure.
115  */
116 typedef struct SYNC_server_state {
117     osi_socket fd;              /**< listening socket descriptor */
118     SYNC_endpoint_t endpoint;   /**< server endpoint address */
119     afs_uint32 proto_version;   /**< our protocol version */
120     int bind_retry_limit;       /**< upper limit on times to retry socket bind() */
121     int listen_depth;           /**< socket listen queue depth */
122     char * proto_name;          /**< sync protocol associated with this conn */
123     SYNC_sockaddr_t addr;       /**< server listen socket sockaddr */
124     afs_uint32 pkt_seq;         /**< packet xmit sequence counter */
125     afs_uint32 res_seq;         /**< response xmit sequence counter */
126 } SYNC_server_state_t;
127
128 /**
129  * SYNC client state structure.
130  */
131 typedef struct SYNC_client_state {
132     osi_socket fd;              /**< client socket descriptor */
133     SYNC_endpoint_t endpoint;   /**< address of sync server */
134     afs_uint32 proto_version;   /**< our protocol version */
135     int retry_limit;            /**< max number of times for SYNC_ask to retry */
136     afs_int32 hard_timeout;     /**< upper limit on time to keep trying */
137     char * proto_name;          /**< sync protocol associated with this conn */
138     byte fatal_error;           /**< nonzero if fatal error on this client conn */
139     afs_uint32 pkt_seq;         /**< packet xmit sequence counter */
140     afs_uint32 com_seq;         /**< command xmit sequence counter */
141 } SYNC_client_state;
142
143 /* wire types */
144 /**
145  * on-wire command packet header.
146  */
147 typedef struct SYNC_command_hdr {
148     afs_uint32 proto_version;   /**< sync protocol version */
149     afs_uint32 pkt_seq;         /**< packet sequence number */
150     afs_uint32 com_seq;         /**< command sequence number */
151     afs_int32 programType;      /**< type of program issuing the request */
152     afs_int32 pid;              /**< pid of requestor */
153     afs_int32 tid;              /**< thread id of requestor */
154     afs_int32 command;          /**< request type */
155     afs_int32 reason;           /**< reason for request */
156     afs_uint32 command_len;     /**< entire length of command */
157     afs_uint32 flags;           /**< miscellanous control flags */
158 } SYNC_command_hdr;
159
160 /**
161  * on-wire response packet header.
162  */
163 typedef struct SYNC_response_hdr {
164     afs_uint32 proto_version;   /**< sync protocol version */
165     afs_uint32 pkt_seq;         /**< packet sequence number */
166     afs_uint32 com_seq;         /**< in response to com_seq... */
167     afs_uint32 res_seq;         /**< response sequence number */
168     afs_uint32 response_len;    /**< entire length of response */
169     afs_int32 response;         /**< response code */
170     afs_int32 reason;           /**< reason for response */
171     afs_uint32 flags;           /**< miscellanous control flags */
172 } SYNC_response_hdr;
173
174
175 /* user-visible types */
176 typedef struct SYNC_command {
177     SYNC_command_hdr hdr;
178     struct {
179         afs_uint32 len;
180         void * buf;
181     } payload;
182     afs_int32 recv_len;
183 } SYNC_command;
184
185 typedef struct SYNC_response {
186     SYNC_response_hdr hdr;
187     struct {
188         afs_uint32 len;
189         void * buf;
190     } payload;
191     afs_int32 recv_len;
192 } SYNC_response;
193
194 /* general prototypes */
195 extern osi_socket SYNC_getSock(SYNC_endpoint_t * endpoint);
196 extern void SYNC_getAddr(SYNC_endpoint_t * endpoint, SYNC_sockaddr_t * addr);
197
198 /* client-side prototypes */
199 extern afs_int32 SYNC_ask(SYNC_client_state *, SYNC_command * com, SYNC_response * res);
200 extern int SYNC_connect(SYNC_client_state *);             /* setup the channel */
201 extern int SYNC_disconnect(SYNC_client_state *);          /* just close the socket */
202 extern afs_int32 SYNC_closeChannel(SYNC_client_state *);  /* do a graceful channel close */
203 extern int SYNC_reconnect(SYNC_client_state *);           /* do a reconnect after a protocol error, or from a forked child */
204
205 /* server-side prototypes */
206 extern int SYNC_getCom(SYNC_server_state_t *, osi_socket fd, SYNC_command * com);
207 extern int SYNC_putRes(SYNC_server_state_t *, osi_socket fd, SYNC_response * res);
208 extern int SYNC_verifyProtocolString(char * buf, size_t len);
209 extern void SYNC_cleanupSock(SYNC_server_state_t * state);
210 extern int SYNC_bindSock(SYNC_server_state_t * state);
211
212 #endif /* _AFS_VOL_DAEMON_COM_H */