Added missing WINAPI for SHValidateUNC.
[wine/dcerpc.git] / server / socket.c
blob915de42a18c343feef1a064edd156e4e21284a6a
1 /*
2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <sys/uio.h>
18 #include <unistd.h>
20 #include "config.h"
21 #include "server.h"
22 #include "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 struct select_user select; /* select user */
43 unsigned int seq; /* current sequence number */
44 struct header head; /* current msg header */
45 char *data; /* current msg data */
46 int count; /* bytes sent/received so far */
47 int pass_fd; /* fd to pass to and from the client */
48 struct thread *self; /* client thread (opaque pointer) */
49 struct timeout_user *timeout; /* current timeout (opaque pointer) */
53 /* exit code passed to remove_client */
54 #define OUT_OF_MEMORY -1
55 #define BROKEN_PIPE -2
56 #define PROTOCOL_ERROR -3
59 /* signal a client protocol error */
60 static void protocol_error( struct client *client, const char *err, ... )
62 va_list args;
64 va_start( args, err );
65 fprintf( stderr, "Protocol error:%d: ", client->select.fd );
66 vfprintf( stderr, err, args );
67 va_end( args );
70 /* send a message to a client that is ready to receive something */
71 static void do_write( struct client *client, int client_fd )
73 struct iovec vec[2];
74 #ifndef HAVE_MSGHDR_ACCRIGHTS
75 struct cmsg_fd cmsg;
76 #endif
77 struct msghdr msghdr;
78 int ret;
80 /* make sure we have something to send */
81 assert( client->count < client->head.len );
82 /* make sure the client is listening */
83 assert( client->state == READING );
85 msghdr.msg_name = NULL;
86 msghdr.msg_namelen = 0;
87 msghdr.msg_iov = vec;
89 if (client->count < sizeof(client->head))
91 vec[0].iov_base = (char *)&client->head + client->count;
92 vec[0].iov_len = sizeof(client->head) - client->count;
93 vec[1].iov_base = client->data;
94 vec[1].iov_len = client->head.len - sizeof(client->head);
95 msghdr.msg_iovlen = 2;
97 else
99 vec[0].iov_base = client->data + client->count - sizeof(client->head);
100 vec[0].iov_len = client->head.len - client->count;
101 msghdr.msg_iovlen = 1;
104 #ifdef HAVE_MSGHDR_ACCRIGHTS
105 if (client->pass_fd != -1) /* we have an fd to send */
107 msghdr.msg_accrights = (void *)&client->pass_fd;
108 msghdr.msg_accrightslen = sizeof(client->pass_fd);
110 else
112 msghdr.msg_accrights = NULL;
113 msghdr.msg_accrightslen = 0;
115 #else /* HAVE_MSGHDR_ACCRIGHTS */
116 if (client->pass_fd != -1) /* we have an fd to send */
118 cmsg.len = sizeof(cmsg);
119 cmsg.level = SOL_SOCKET;
120 cmsg.type = SCM_RIGHTS;
121 cmsg.fd = client->pass_fd;
122 msghdr.msg_control = &cmsg;
123 msghdr.msg_controllen = sizeof(cmsg);
125 else
127 msghdr.msg_control = NULL;
128 msghdr.msg_controllen = 0;
130 msghdr.msg_flags = 0;
131 #endif /* HAVE_MSGHDR_ACCRIGHTS */
133 ret = sendmsg( client_fd, &msghdr, 0 );
134 if (ret == -1)
136 if (errno != EPIPE) perror("sendmsg");
137 remove_client( client, BROKEN_PIPE );
138 return;
140 if (client->pass_fd != -1) /* We sent the fd, now we can close it */
142 close( client->pass_fd );
143 client->pass_fd = -1;
145 if ((client->count += ret) < client->head.len) return;
147 /* we have finished with this message */
148 if (client->data) free( client->data );
149 client->data = NULL;
150 client->count = 0;
151 client->state = RUNNING;
152 client->seq++;
153 set_select_events( &client->select, READ_EVENT );
157 /* read a message from a client that has something to say */
158 static void do_read( struct client *client, int client_fd )
160 struct iovec vec;
161 int pass_fd = -1;
162 int ret;
164 #ifdef HAVE_MSGHDR_ACCRIGHTS
165 struct msghdr msghdr;
167 msghdr.msg_accrights = (void *)&pass_fd;
168 msghdr.msg_accrightslen = sizeof(int);
169 #else /* HAVE_MSGHDR_ACCRIGHTS */
170 struct msghdr msghdr;
171 struct cmsg_fd cmsg;
173 cmsg.len = sizeof(cmsg);
174 cmsg.level = SOL_SOCKET;
175 cmsg.type = SCM_RIGHTS;
176 cmsg.fd = -1;
177 msghdr.msg_control = &cmsg;
178 msghdr.msg_controllen = sizeof(cmsg);
179 msghdr.msg_flags = 0;
180 #endif /* HAVE_MSGHDR_ACCRIGHTS */
182 msghdr.msg_name = NULL;
183 msghdr.msg_namelen = 0;
184 msghdr.msg_iov = &vec;
185 msghdr.msg_iovlen = 1;
187 if (client->count < sizeof(client->head))
189 vec.iov_base = (char *)&client->head + client->count;
190 vec.iov_len = sizeof(client->head) - client->count;
192 else
194 if (!client->data &&
195 !(client->data = malloc(client->head.len-sizeof(client->head))))
197 remove_client( client, OUT_OF_MEMORY );
198 return;
200 vec.iov_base = client->data + client->count - sizeof(client->head);
201 vec.iov_len = client->head.len - client->count;
204 ret = recvmsg( client_fd, &msghdr, 0 );
205 if (ret == -1)
207 perror("recvmsg");
208 remove_client( client, BROKEN_PIPE );
209 return;
211 #ifndef HAVE_MSGHDR_ACCRIGHTS
212 pass_fd = cmsg.fd;
213 #endif
214 if (pass_fd != -1)
216 /* can only receive one fd per message */
217 if (client->pass_fd != -1) close( client->pass_fd );
218 client->pass_fd = pass_fd;
220 else if (!ret) /* closed pipe */
222 remove_client( client, BROKEN_PIPE );
223 return;
226 if (client->state == RUNNING) client->state = SENDING;
227 assert( client->state == SENDING );
229 client->count += ret;
231 /* received the complete header yet? */
232 if (client->count < sizeof(client->head)) return;
234 /* sanity checks */
235 if (client->head.seq != client->seq)
237 protocol_error( client, "bad sequence %08x instead of %08x\n",
238 client->head.seq, client->seq );
239 remove_client( client, PROTOCOL_ERROR );
240 return;
242 if ((client->head.len < sizeof(client->head)) ||
243 (client->head.len > MAX_MSG_LENGTH + sizeof(client->head)))
245 protocol_error( client, "bad header length %08x\n",
246 client->head.len );
247 remove_client( client, PROTOCOL_ERROR );
248 return;
251 /* received the whole message? */
252 if (client->count == client->head.len)
254 /* done reading the data, call the callback function */
256 int len = client->head.len - sizeof(client->head);
257 char *data = client->data;
258 int passed_fd = client->pass_fd;
259 enum request type = client->head.type;
261 /* clear the info now, as the client may be deleted by the callback */
262 client->head.len = 0;
263 client->head.type = 0;
264 client->count = 0;
265 client->data = NULL;
266 client->pass_fd = -1;
267 client->state = WAITING;
268 client->seq++;
270 call_req_handler( client->self, type, data, len, passed_fd );
271 if (passed_fd != -1) close( passed_fd );
272 if (data) free( data );
276 /* handle a client event */
277 static void client_event( int event, void *private )
279 struct client *client = (struct client *)private;
280 if (event & WRITE_EVENT) do_write( client, client->select.fd );
281 if (event & READ_EVENT) do_read( client, client->select.fd );
285 /*******************************************************************/
286 /* server-side exported functions */
288 /* add a client */
289 struct client *add_client( int fd, struct thread *self )
291 int flags;
292 struct client *client = mem_alloc( sizeof(*client) );
293 if (!client) return NULL;
295 flags = fcntl( fd, F_GETFL, 0 );
296 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
298 client->state = RUNNING;
299 client->select.fd = fd;
300 client->select.func = client_event;
301 client->select.private = client;
302 client->seq = 0;
303 client->head.len = 0;
304 client->head.type = 0;
305 client->count = 0;
306 client->data = NULL;
307 client->self = self;
308 client->timeout = NULL;
309 client->pass_fd = -1;
310 register_select_user( &client->select );
311 set_select_events( &client->select, READ_EVENT );
312 return client;
315 /* remove a client */
316 void remove_client( struct client *client, int exit_code )
318 assert( client );
320 call_kill_handler( client->self, exit_code );
322 if (client->timeout) remove_timeout_user( client->timeout );
323 unregister_select_user( &client->select );
324 close( client->select.fd );
326 /* Purge messages */
327 if (client->data) free( client->data );
328 if (client->pass_fd != -1) close( client->pass_fd );
329 free( client );
332 /* send a reply to a client */
333 int send_reply_v( struct client *client, int type, int pass_fd,
334 struct iovec *vec, int veclen )
336 int i;
337 unsigned int len;
338 char *p;
340 assert( client );
341 assert( client->state == WAITING );
342 assert( !client->data );
344 if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen );
346 for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
347 assert( len < MAX_MSG_LENGTH );
349 if (len && !(client->data = malloc( len ))) return -1;
350 client->count = 0;
351 client->head.len = len + sizeof(client->head);
352 client->head.type = type;
353 client->head.seq = client->seq;
354 client->pass_fd = pass_fd;
356 for (i = 0, p = client->data; i < veclen; i++)
358 memcpy( p, vec[i].iov_base, vec[i].iov_len );
359 p += vec[i].iov_len;
362 client->state = READING;
363 set_select_events( &client->select, WRITE_EVENT );
364 return 0;