dafs-vnode-locking-20080204
[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
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 };
31
32
33 /* SYNC protocol response codes
34  *
35  * response codes 0-65535 are reserved for 
36  * global SYNC package response codes
37  */
38 #define SYNC_RES_CODE_USER_BASE 65536
39 #define SYNC_RES_CODE_DECL(code) (SYNC_RES_CODE_USER_BASE+(code))
40
41 /**
42  * general response codes.
43  */
44 enum SYNCReasonCode {
45     SYNC_OK                   = 0,  /**< sync call returned ok */
46     SYNC_DENIED               = 1,  /**< sync request denied by server */
47     SYNC_COM_ERROR            = 2,  /**< sync protocol communicaions error */
48     SYNC_BAD_COMMAND          = 3,  /**< sync command code not implemented by server */
49     SYNC_FAILED               = 4,  /**< sync server-side procedure failed */
50 };
51
52 /* SYNC protocol reason codes
53  *
54  * reason codes 0-65535 are reserved for
55  * global SYNC package reason codes
56  */
57 #define SYNC_REASON_CODE_USER_BASE 65536
58 #define SYNC_REASON_CODE_DECL(code) (SYNC_REASON_CODE_USER_BASE+(code))
59
60 /* general reason codes */
61 #define SYNC_REASON_NONE                 0
62 #define SYNC_REASON_MALFORMED_PACKET     1
63 #define SYNC_REASON_NOMEM                2
64 #define SYNC_REASON_ENCODING_ERROR       3
65
66 /* SYNC protocol flags
67  *
68  * flag bits 0-7 are reserved for
69  * global SYNC package flags
70  */
71 #define SYNC_FLAG_CODE_USER_BASE 8
72 #define SYNC_FLAG_CODE_DECL(code) (1 << (SYNC_FLAG_CODE_USER_BASE+(code)))
73
74 /* general flag codes */
75 #define SYNC_FLAG_CHANNEL_SHUTDOWN   0x1
76 #define SYNC_FLAG_DAFS_EXTENSIONS    0x2   /* signal that other end of socket is compiled
77                                             * with demand attach extensions */
78
79 /* SYNC protocol response buffers */
80 #define SYNC_PROTO_MAX_LEN     768  /* maximum size of sync protocol message */
81
82 /* use a large type to get proper buffer alignment so we can safely cast the pointer */
83 #define SYNC_PROTO_BUF_DECL(buf) \
84     afs_int64 _##buf##_l[SYNC_PROTO_MAX_LEN/sizeof(afs_int64)]; \
85     char * buf = (char *)(_##buf##_l)
86
87 #ifdef USE_UNIX_SOCKETS
88 #include <afs/afsutil.h>
89 #include <sys/un.h>
90 #define SYNC_SOCK_DOMAIN AF_UNIX
91 typedef struct sockaddr_un SYNC_sockaddr_t;
92 #else  /* USE_UNIX_SOCKETS */
93 #define SYNC_SOCK_DOMAIN AF_INET
94 typedef struct sockaddr_in SYNC_sockaddr_t;
95 #endif /* USE_UNIX_SOCKETS */
96
97 /**
98  * sync server endpoint address.
99  */
100 typedef struct SYNC_endpoint {
101     int domain;     /**< socket domain */
102     afs_uint16 in;  /**< localhost ipv4 tcp port number */
103     char * un;      /**< unix domain socket filename (not a full path) */
104 } SYNC_endpoint_t;
105
106 #define SYNC_ENDPOINT_DECL(in_port, un_path) \
107     { SYNC_SOCK_DOMAIN, in_port, un_path }
108
109
110 /**
111  * SYNC server state structure.
112  */
113 typedef struct SYNC_server_state {
114     int fd;                     /**< listening socket descriptor */
115     SYNC_endpoint_t endpoint;   /**< server endpoint address */
116     afs_uint32 proto_version;   /**< our protocol version */
117     int bind_retry_limit;       /**< upper limit on times to retry socket bind() */
118     int listen_depth;           /**< socket listen queue depth */
119     char * proto_name;          /**< sync protocol associated with this conn */
120     SYNC_sockaddr_t addr;       /**< server listen socket sockaddr */
121 } SYNC_server_state_t;
122
123 /**
124  * SYNC client state structure.
125  */
126 typedef struct SYNC_client_state {
127     int fd;                     /**< client socket descriptor */
128     SYNC_endpoint_t endpoint;   /**< address of sync server */
129     afs_uint32 proto_version;   /**< our protocol version */
130     int retry_limit;            /**< max number of times for SYNC_ask to retry */
131     afs_int32 hard_timeout;     /**< upper limit on time to keep trying */
132     char * proto_name;          /**< sync protocol associated with this conn */
133     byte fatal_error;           /**< nonzer if fatal error on this client conn */
134 } SYNC_client_state;
135
136 /* wire types */
137 typedef struct SYNC_command_hdr {
138     afs_uint32 proto_version;   /* sync protocol version */
139     afs_int32 programType;      /* type of program issuing the request */
140     afs_int32 command;          /* request type */
141     afs_int32 reason;           /* reason for request */
142     afs_uint32 command_len;     /* entire length of command */
143     afs_uint32 flags;
144 } SYNC_command_hdr;
145
146 typedef struct SYNC_response_hdr {
147     afs_uint32 proto_version;    /* sync protocol version */
148     afs_uint32 response_len;    /* entire length of response */
149     afs_int32 response;         /* response code */
150     afs_int32 reason;           /* reason for response */
151     afs_uint32 flags;
152 } SYNC_response_hdr;
153
154
155 /* user-visible types */
156 typedef struct SYNC_command {
157     SYNC_command_hdr hdr;
158     struct {
159         afs_uint32 len;
160         void * buf;
161     } payload;
162     afs_int32 recv_len;
163 } SYNC_command;
164
165 typedef struct SYNC_response {
166     SYNC_response_hdr hdr;
167     struct {
168         afs_uint32 len;
169         void * buf;
170     } payload;
171     afs_int32 recv_len;
172 } SYNC_response;
173
174 /* general prototypes */
175 extern int SYNC_getSock(SYNC_endpoint_t * endpoint);
176 extern void SYNC_getAddr(SYNC_endpoint_t * endpoint, SYNC_sockaddr_t * addr);
177
178 /* client-side prototypes */
179 extern afs_int32 SYNC_ask(SYNC_client_state *, SYNC_command * com, SYNC_response * res);
180 extern int SYNC_connect(SYNC_client_state *);             /* setup the channel */
181 extern int SYNC_disconnect(SYNC_client_state *);          /* just close the socket */
182 extern afs_int32 SYNC_closeChannel(SYNC_client_state *);  /* do a graceful channel close */
183 extern int SYNC_reconnect(SYNC_client_state *);           /* do a reconnect after a protocol error, or from a forked child */
184
185 /* server-side prototypes */
186 extern int SYNC_getCom(int fd, SYNC_command * com);
187 extern int SYNC_putRes(int fd, SYNC_response * res);
188 extern int SYNC_verifyProtocolString(char * buf, size_t len);
189 extern void SYNC_cleanupSock(SYNC_server_state_t * state);
190 extern int SYNC_bindSock(SYNC_server_state_t * state);
191
192 #endif /* _AFS_VOL_DAEMON_COM_H */