Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbejy@wpi.edu>
[wine/multimedia.git] / server / socket.c
blobee0b023e1bbe4acadb83624ea49c9aa007585b13
1 /*
2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/uio.h>
17 #include <unistd.h>
19 #include "config.h"
20 #include "server.h"
22 #include "server/object.h"
24 /* Some versions of glibc don't define this */
25 #ifndef SCM_RIGHTS
26 #define SCM_RIGHTS 1
27 #endif
29 /* client state */
30 enum state
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 */
39 struct client
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, ... )
60 va_list args;
62 va_start( args, err );
63 fprintf( stderr, "Protocol error:%d: ", client_fd );
64 vfprintf( stderr, err, args );
65 va_end( 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 )
71 struct iovec vec[2];
72 #ifndef HAVE_MSGHDR_ACCRIGHTS
73 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS,
74 client->pass_fd };
75 #endif
76 struct msghdr msghdr = { NULL, 0, vec, 2, };
77 int ret;
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);
91 else
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);
102 #else
103 msghdr.msg_control = &cmsg;
104 msghdr.msg_controllen = sizeof(cmsg);
105 #endif
107 ret = sendmsg( client_fd, &msghdr, 0 );
108 if (ret == -1)
110 if (errno != EPIPE) perror("sendmsg");
111 remove_client( client_fd, BROKEN_PIPE );
112 return;
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 );
123 client->data = NULL;
124 client->count = 0;
125 client->state = RUNNING;
126 client->seq++;
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 )
134 struct iovec vec;
135 int pass_fd = -1;
136 #ifdef HAVE_MSGHDR_ACCRIGHTS
137 struct msghdr msghdr = { NULL, 0, &vec, 1, (void*)&pass_fd, sizeof(int) };
138 #else
139 struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
140 struct msghdr msghdr = { NULL, 0, &vec, 1, &cmsg, sizeof(cmsg), 0 };
141 #endif
142 int ret;
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;
149 else
151 if (!client->data &&
152 !(client->data = malloc(client->head.len-sizeof(client->head))))
154 remove_client( client_fd, OUT_OF_MEMORY );
155 return;
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 );
162 if (ret == -1)
164 perror("recvmsg");
165 remove_client( client_fd, BROKEN_PIPE );
166 return;
168 #ifndef HAVE_MSGHDR_ACCRIGHTS
169 pass_fd = cmsg.fd;
170 #endif
171 if (pass_fd != -1)
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 );
180 return;
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;
191 /* sanity checks */
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 );
197 return;
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",
203 client->head.len );
204 remove_client( client_fd, PROTOCOL_ERROR );
205 return;
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;
221 client->count = 0;
222 client->data = NULL;
223 client->pass_fd = -1;
224 client->state = WAITING;
225 client->seq++;
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 =
253 client_event,
254 client_timeout
257 /*******************************************************************/
258 /* server-side exported functions */
260 /* add a client */
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;
267 client->seq = 0;
268 client->head.len = 0;
269 client->head.type = 0;
270 client->count = 0;
271 client->data = NULL;
272 client->self = self;
273 client->pass_fd = -1;
275 if (add_select_user( client_fd, READ_EVENT, &client_ops, client ) == -1)
277 free( client );
278 return -1;
280 return client_fd;
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 );
287 assert( client );
289 call_kill_handler( client->self, exit_code );
291 remove_select_user( client_fd );
292 close( client_fd );
294 /* Purge messages */
295 if (client->data) free( client->data );
296 if (client->pass_fd != -1) close( client->pass_fd );
297 free( client );
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 )
304 int i;
305 unsigned int len;
306 char *p;
307 struct client *client = (struct client *)get_select_private_data( &client_ops, client_fd );
309 assert( client );
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;
319 client->count = 0;
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 );
328 p += vec[i].iov_len;
331 client->state = READING;
332 set_select_events( client_fd, WRITE_EVENT );
333 return 0;