2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
14 #include <sys/types.h>
15 #include <sys/socket.h>
22 #include "server/object.h"
24 /* Some versions of glibc don't define this */
32 RUNNING, /* running normally */
33 SENDING, /* sending us a request */
34 WAITING, /* waiting for us to reply */
35 READING /* reading our reply */
38 /* client structure */
41 enum state state; /* client state */
42 unsigned int seq; /* current sequence number */
43 struct header head; /* current msg header */
44 char *data; /* current msg data */
45 int count; /* bytes sent/received so far */
46 int pass_fd; /* fd to pass to and from the client */
47 struct thread *self; /* client thread (opaque pointer) */
51 /* exit code passed to remove_client */
52 #define OUT_OF_MEMORY -1
53 #define BROKEN_PIPE -2
54 #define PROTOCOL_ERROR -3
57 /* signal a client protocol error */
58 static void protocol_error( int client_fd, const char *err, ... )
62 va_start( args, err );
63 fprintf( stderr, "Protocol error:%d: ", client_fd );
64 vfprintf( stderr, err, args );
68 /* send a message to a client that is ready to receive something */
69 static void do_write( struct client *client, int client_fd )
72 #ifndef HAVE_MSGHDR_ACCRIGHTS
73 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS,
76 struct msghdr msghdr = { NULL, 0, vec, 2, };
79 /* make sure we have something to send */
80 assert( client->count < client->head.len );
81 /* make sure the client is listening */
82 assert( client->state == READING );
84 if (client->count < sizeof(client->head))
86 vec[0].iov_base = (char *)&client->head + client->count;
87 vec[0].iov_len = sizeof(client->head) - client->count;
88 vec[1].iov_base = client->data;
89 vec[1].iov_len = client->head.len - sizeof(client->head);
93 vec[0].iov_base = client->data + client->count - sizeof(client->head);
94 vec[0].iov_len = client->head.len - client->count;
95 msghdr.msg_iovlen = 1;
97 if (client->pass_fd != -1) /* we have an fd to send */
99 #ifdef HAVE_MSGHDR_ACCRIGHTS
100 msghdr.msg_accrights = (void *)&client->pass_fd;
101 msghdr.msg_accrightslen = sizeof(client->pass_fd);
103 msghdr.msg_control = &cmsg;
104 msghdr.msg_controllen = sizeof(cmsg);
107 ret = sendmsg( client_fd, &msghdr, 0 );
110 if (errno != EPIPE) perror("sendmsg");
111 remove_client( client_fd, BROKEN_PIPE );
114 if (client->pass_fd != -1) /* We sent the fd, now we can close it */
116 close( client->pass_fd );
117 client->pass_fd = -1;
119 if ((client->count += ret) < client->head.len) return;
121 /* we have finished with this message */
122 if (client->data) free( client->data );
125 client->state = RUNNING;
127 set_select_events( client_fd, READ_EVENT );
131 /* read a message from a client that has something to say */
132 static void do_read( struct client *client, int client_fd )
136 #ifdef HAVE_MSGHDR_ACCRIGHTS
137 struct msghdr msghdr = { NULL, 0, &vec, 1, (void*)&pass_fd, sizeof(int) };
139 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
140 struct msghdr msghdr = { NULL, 0, &vec, 1, &cmsg, sizeof(cmsg), 0 };
144 if (client->count < sizeof(client->head))
146 vec.iov_base = (char *)&client->head + client->count;
147 vec.iov_len = sizeof(client->head) - client->count;
152 !(client->data = malloc(client->head.len-sizeof(client->head))))
154 remove_client( client_fd, OUT_OF_MEMORY );
157 vec.iov_base = client->data + client->count - sizeof(client->head);
158 vec.iov_len = client->head.len - client->count;
161 ret = recvmsg( client_fd, &msghdr, 0 );
165 remove_client( client_fd, BROKEN_PIPE );
168 #ifndef HAVE_MSGHDR_ACCRIGHTS
173 /* can only receive one fd per message */
174 if (client->pass_fd != -1) close( client->pass_fd );
175 client->pass_fd = pass_fd;
177 else if (!ret) /* closed pipe */
179 remove_client( client_fd, BROKEN_PIPE );
183 if (client->state == RUNNING) client->state = SENDING;
184 assert( client->state == SENDING );
186 client->count += ret;
188 /* received the complete header yet? */
189 if (client->count < sizeof(client->head)) return;
192 if (client->head.seq != client->seq)
194 protocol_error( client_fd, "bad sequence %08x instead of %08x\n",
195 client->head.seq, client->seq );
196 remove_client( client_fd, PROTOCOL_ERROR );
199 if ((client->head.len < sizeof(client->head)) ||
200 (client->head.len > MAX_MSG_LENGTH + sizeof(client->head)))
202 protocol_error( client_fd, "bad header length %08x\n",
204 remove_client( client_fd, PROTOCOL_ERROR );
208 /* received the whole message? */
209 if (client->count == client->head.len)
211 /* done reading the data, call the callback function */
213 int len = client->head.len - sizeof(client->head);
214 char *data = client->data;
215 int passed_fd = client->pass_fd;
216 enum request type = client->head.type;
218 /* clear the info now, as the client may be deleted by the callback */
219 client->head.len = 0;
220 client->head.type = 0;
223 client->pass_fd = -1;
224 client->state = WAITING;
227 call_req_handler( client->self, type, data, len, passed_fd );
228 if (passed_fd != -1) close( passed_fd );
229 if (data) free( data );
233 /* handle a client timeout */
234 static void client_timeout( int client_fd, void *private )
236 struct client *client = (struct client *)private;
237 set_select_timeout( client_fd, 0 ); /* Remove the timeout */
238 call_timeout_handler( client->self );
241 /* handle a client event */
242 static void client_event( int client_fd, int event, void *private )
244 struct client *client = (struct client *)private;
245 if (event & WRITE_EVENT)
246 do_write( client, client_fd );
247 if (event & READ_EVENT)
248 do_read( client, client_fd );
251 static const struct select_ops client_ops =
257 /*******************************************************************/
258 /* server-side exported functions */
261 int add_client( int client_fd, struct thread *self )
263 struct client *client = malloc( sizeof(*client) );
264 if (!client) return -1;
266 client->state = RUNNING;
268 client->head.len = 0;
269 client->head.type = 0;
273 client->pass_fd = -1;
275 if (add_select_user( client_fd, READ_EVENT, &client_ops, client ) == -1)
283 /* remove a client */
284 void remove_client( int client_fd, int exit_code )
286 struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
289 call_kill_handler( client->self, exit_code );
291 remove_select_user( client_fd );
295 if (client->data) free( client->data );
296 if (client->pass_fd != -1) close( client->pass_fd );
300 /* send a reply to a client */
301 int send_reply_v( int client_fd, int type, int pass_fd,
302 struct iovec *vec, int veclen )
307 struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
310 assert( client->state == WAITING );
311 assert( !client->data );
313 if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen );
315 for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
316 assert( len < MAX_MSG_LENGTH );
318 if (len && !(client->data = malloc( len ))) return -1;
320 client->head.len = len + sizeof(client->head);
321 client->head.type = type;
322 client->head.seq = client->seq;
323 client->pass_fd = pass_fd;
325 for (i = 0, p = client->data; i < veclen; i++)
327 memcpy( p, vec[i].iov_base, vec[i].iov_len );
331 client->state = READING;
332 set_select_events( client_fd, WRITE_EVENT );