2 * Copyright 2006-2008, Sine Nomine Associates and others.
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
10 #ifndef _AFS_VOL_DAEMON_COM_H
11 #define _AFS_VOL_DAEMON_COM_H 1
14 * SYNC protocol constants
17 /* SYNC protocol command codes
19 * command codes 0-65535 are reserved for
20 * global SYNC package command codes
22 #define SYNC_COM_CODE_USER_BASE 65536
23 #define SYNC_COM_CODE_DECL(code) (SYNC_COM_CODE_USER_BASE+(code))
26 * general command codes.
29 SYNC_COM_CHANNEL_CLOSE = 0, /**< request sync channel shutdown */
34 /* SYNC protocol response codes
36 * response codes 0-65535 are reserved for
37 * global SYNC package response codes
39 #define SYNC_RES_CODE_USER_BASE 65536
40 #define SYNC_RES_CODE_DECL(code) (SYNC_RES_CODE_USER_BASE+(code))
43 * general response codes.
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
54 /* SYNC protocol reason codes
56 * reason codes 0-65535 are reserved for
57 * global SYNC package reason codes
59 #define SYNC_REASON_CODE_USER_BASE 65536
60 #define SYNC_REASON_CODE_DECL(code) (SYNC_REASON_CODE_USER_BASE+(code))
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 */
69 /* SYNC protocol flags
71 * flag bits 0-7 are reserved for
72 * global SYNC package flags
74 #define SYNC_FLAG_CODE_USER_BASE 8
75 #define SYNC_FLAG_CODE_DECL(code) (1 << (SYNC_FLAG_CODE_USER_BASE+(code)))
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 */
82 /* SYNC protocol response buffers */
83 #define SYNC_PROTO_MAX_LEN 768 /* maximum size of sync protocol message */
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)
90 #ifdef USE_UNIX_SOCKETS
91 #include <afs/afsutil.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 */
101 * sync server endpoint address.
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) */
109 #define SYNC_ENDPOINT_DECL(in_port, un_path) \
110 { SYNC_SOCK_DOMAIN, in_port, un_path }
114 * SYNC server state structure.
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;
129 * SYNC client state structure.
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 */
145 * on-wire command packet header.
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 */
161 * on-wire response packet header.
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 */
175 /* user-visible types */
176 typedef struct SYNC_command {
177 SYNC_command_hdr hdr;
185 typedef struct SYNC_response {
186 SYNC_response_hdr hdr;
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);
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 */
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);
212 #endif /* _AFS_VOL_DAEMON_COM_H */