Import Upstream version 1.8.5
[hcoop/debian/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 AFS_LINUX26_ENV
91 /* Some Linux kernels have a bug where we are not woken up immediately from a
92 * select() when data is available. Work around this by having a low select()
93 * timeout, so we don't hang in those situations. */
94 # define SYNC_SELECT_TIMEOUT 10
95 #else
96 # define SYNC_SELECT_TIMEOUT 86400
97 #endif
98
99 #ifdef USE_UNIX_SOCKETS
100 #include <afs/afsutil.h>
101 #include <sys/un.h>
102 #define SYNC_SOCK_DOMAIN AF_UNIX
103 typedef struct sockaddr_un SYNC_sockaddr_t;
104 #else /* USE_UNIX_SOCKETS */
105 #define SYNC_SOCK_DOMAIN AF_INET
106 typedef struct sockaddr_in SYNC_sockaddr_t;
107 #endif /* USE_UNIX_SOCKETS */
108
109 /**
110 * sync server endpoint address.
111 */
112 typedef struct SYNC_endpoint {
113 int domain; /**< socket domain */
114 afs_uint16 in; /**< localhost ipv4 tcp port number */
115 char * un; /**< unix domain socket filename (not a full path) */
116 } SYNC_endpoint_t;
117
118 #define SYNC_ENDPOINT_DECL(in_port, un_path) \
119 { SYNC_SOCK_DOMAIN, in_port, un_path }
120
121
122 /**
123 * SYNC server state structure.
124 */
125 typedef struct SYNC_server_state {
126 osi_socket fd; /**< listening socket descriptor */
127 SYNC_endpoint_t endpoint; /**< server endpoint address */
128 afs_uint32 proto_version; /**< our protocol version */
129 int bind_retry_limit; /**< upper limit on times to retry socket bind() */
130 int listen_depth; /**< socket listen queue depth */
131 char * proto_name; /**< sync protocol associated with this conn */
132 SYNC_sockaddr_t addr; /**< server listen socket sockaddr */
133 afs_uint32 pkt_seq; /**< packet xmit sequence counter */
134 afs_uint32 res_seq; /**< response xmit sequence counter */
135 } SYNC_server_state_t;
136
137 /**
138 * SYNC client state structure.
139 */
140 typedef struct SYNC_client_state {
141 osi_socket fd; /**< client socket descriptor */
142 SYNC_endpoint_t endpoint; /**< address of sync server */
143 afs_uint32 proto_version; /**< our protocol version */
144 int retry_limit; /**< max number of times for SYNC_ask to retry */
145 afs_int32 hard_timeout; /**< upper limit on time to keep trying */
146 char * proto_name; /**< sync protocol associated with this conn */
147 afs_uint32 pkt_seq; /**< packet xmit sequence counter */
148 afs_uint32 com_seq; /**< command xmit sequence counter */
149 } SYNC_client_state;
150
151 /* wire types */
152 /**
153 * on-wire command packet header.
154 */
155 typedef struct SYNC_command_hdr {
156 afs_uint32 proto_version; /**< sync protocol version */
157 afs_uint32 pkt_seq; /**< packet sequence number */
158 afs_uint32 com_seq; /**< command sequence number */
159 afs_int32 programType; /**< type of program issuing the request */
160 afs_int32 pid; /**< pid of requestor */
161 afs_int32 tid; /**< thread id of requestor */
162 afs_int32 command; /**< request type */
163 afs_int32 reason; /**< reason for request */
164 afs_uint32 command_len; /**< entire length of command */
165 afs_uint32 flags; /**< miscellanous control flags */
166 } SYNC_command_hdr;
167
168 /**
169 * on-wire response packet header.
170 */
171 typedef struct SYNC_response_hdr {
172 afs_uint32 proto_version; /**< sync protocol version */
173 afs_uint32 pkt_seq; /**< packet sequence number */
174 afs_uint32 com_seq; /**< in response to com_seq... */
175 afs_uint32 res_seq; /**< response sequence number */
176 afs_uint32 response_len; /**< entire length of response */
177 afs_int32 response; /**< response code */
178 afs_int32 reason; /**< reason for response */
179 afs_uint32 flags; /**< miscellanous control flags */
180 } SYNC_response_hdr;
181
182
183 /* user-visible types */
184 typedef struct SYNC_command {
185 SYNC_command_hdr hdr;
186 struct {
187 afs_uint32 len;
188 void * buf;
189 } payload;
190 afs_int32 recv_len;
191 } SYNC_command;
192
193 typedef struct SYNC_response {
194 SYNC_response_hdr hdr;
195 struct {
196 afs_uint32 len;
197 void * buf;
198 } payload;
199 afs_int32 recv_len;
200 } SYNC_response;
201
202 /* general prototypes */
203 extern osi_socket SYNC_getSock(SYNC_endpoint_t * endpoint);
204 extern void SYNC_getAddr(SYNC_endpoint_t * endpoint, SYNC_sockaddr_t * addr);
205
206 /* client-side prototypes */
207 extern afs_int32 SYNC_ask(SYNC_client_state *, SYNC_command * com, SYNC_response * res);
208 extern int SYNC_connect(SYNC_client_state *); /* setup the channel */
209 extern int SYNC_disconnect(SYNC_client_state *); /* just close the socket */
210 extern afs_int32 SYNC_closeChannel(SYNC_client_state *); /* do a graceful channel close */
211 extern int SYNC_reconnect(SYNC_client_state *); /* do a reconnect after a protocol error, or from a forked child */
212
213 /* server-side prototypes */
214 extern int SYNC_getCom(SYNC_server_state_t *, osi_socket fd, SYNC_command * com);
215 extern int SYNC_putRes(SYNC_server_state_t *, osi_socket fd, SYNC_response * res);
216 extern int SYNC_verifyProtocolString(char * buf, size_t len);
217 extern void SYNC_cleanupSock(SYNC_server_state_t * state);
218 extern int SYNC_bindSock(SYNC_server_state_t * state);
219
220 #endif /* _AFS_VOL_DAEMON_COM_H */