4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
10 "$Id: symsocket.c,v 1.13 2008/03/07 14:13:40 james Exp $";
13 * $Log: symsocket.c,v $
14 * Revision 1.13 2008/03/07 14:13:40 james
15 * *** empty log message ***
17 * Revision 1.12 2008/03/07 13:16:02 james
18 * *** empty log message ***
20 * Revision 1.11 2008/03/07 12:37:04 james
21 * *** empty log message ***
23 * Revision 1.10 2008/03/03 18:16:16 james
24 * *** empty log message ***
26 * Revision 1.9 2008/03/03 18:15:19 james
27 * *** empty log message ***
29 * Revision 1.8 2008/03/03 06:04:42 james
30 * *** empty log message ***
32 * Revision 1.7 2008/03/02 10:37:56 james
33 * *** empty log message ***
35 * Revision 1.6 2008/02/27 00:27:22 james
36 * *** empty log message ***
38 * Revision 1.5 2008/02/20 22:54:22 staffcvs
39 * *** empty log message ***
41 * Revision 1.4 2008/02/20 18:31:53 james
42 * *** empty log message ***
44 * Revision 1.3 2008/02/14 02:46:44 james
45 * *** empty log message ***
47 * Revision 1.2 2008/02/14 00:57:58 james
48 * *** empty log message ***
50 * Revision 1.1 2008/02/13 18:05:06 james
51 * *** empty log message ***
56 #include <sys/socket.h>
59 #define BUF_SIZE 65536
63 wrap_recv (int fd, void *buf, int len)
67 red = recv (fd, buf, len, 0);
71 if ((red < 0) && (errno == EAGAIN))
78 wrap_send (int fd, void *buf, int len)
84 writ = send (fd, buf, len, MSG_NOSIGNAL);
89 if ((writ < 0) && (errno == EAGAIN))
96 socket_free (Socket * s)
101 slide_free (s->read_buf);
103 slide_free (s->write_buf);
104 if (s->path_to_unlink) {
105 unlink (s->path_to_unlink);
106 free (s->path_to_unlink);
113 socket_free_parent (Socket * s)
118 slide_free (s->read_buf);
120 slide_free (s->write_buf);
121 if (s->path_to_unlink)
122 free (s->path_to_unlink);
130 socket_listen (char *path)
133 struct sockaddr_un *sun;
140 fd = socket (PF_UNIX, SOCK_STREAM, 0);
145 n = strlen (path) + sizeof (struct sockaddr_un);
146 sun = (struct sockaddr_un *) xmalloc (n);
149 sun->sun_family = AF_UNIX;
150 strcpy (sun->sun_path, path);
152 if (bind (fd, (struct sockaddr *) sun, SUN_LEN (sun)) < 0) {
160 if (listen (fd, 5) < 0) {
165 set_nonblocking (fd);
167 ret = (Socket *) xmalloc (sizeof (Socket));
168 memset (ret, 0, sizeof (Socket));
169 ret->read_buf = NULL;
170 ret->write_buf = NULL;
173 ret->path_to_unlink = strdup (path);
180 socket_accept (Socket * l)
185 struct sockaddr_un sun;
187 len = sizeof (struct sockaddr_un);
189 fd = accept (l->fd, (struct sockaddr *) &sun, &len);
195 ret = (Socket *) xmalloc (sizeof (Socket));
196 memset (ret, 0, sizeof (Socket));
198 set_nonblocking (fd);
201 ret->read_buf = slide_new (BUF_SIZE);
202 ret->write_buf = slide_new (BUF_SIZE);
209 /* Blocking for now */
211 socket_connect (char *path)
215 struct sockaddr_un *sun;
219 fd = socket (PF_UNIX, SOCK_STREAM, 0);
224 n = strlen (path) + sizeof (struct sockaddr_un);
225 sun = (struct sockaddr_un *) xmalloc (n);
228 sun->sun_family = AF_UNIX;
229 strcpy (sun->sun_path, path);
231 if (connect (fd, (struct sockaddr *) sun, SUN_LEN (sun))) {
239 set_nonblocking (fd);
241 ret = (Socket *) xmalloc (sizeof (Socket));
242 memset (ret, 0, sizeof (Socket));
245 ret->read_buf = slide_new (BUF_SIZE);
246 ret->write_buf = slide_new (BUF_SIZE);
253 socket_consume_msg (Socket * s)
260 ipc_consume_message_in_slide (s->read_buf);
261 s->msg = ipc_check_for_message_in_slide (s->read_buf);
266 socket_pre_select (Socket * s, fd_set * rfds, fd_set * wfds)
272 if (SOCKET_IS_LISTENER (s)) {
273 FD_SET (s->fd, rfds);
277 if (!SLIDE_EMPTY (s->write_buf))
278 FD_SET (s->fd, wfds);
280 if (!SLIDE_FULL (s->read_buf))
281 FD_SET (s->fd, rfds);
286 socket_post_select (Socket * s, fd_set * rfds, fd_set * wfds)
293 if ((!SLIDE_EMPTY (s->write_buf)) && FD_ISSET (s->fd, wfds)) {
295 (SLIDE_BYTES (s->write_buf) >
296 MAX_TXN) ? MAX_TXN : SLIDE_BYTES (s->write_buf);
297 n = wrap_send (s->fd, SLIDE_RPTR (s->write_buf), n);
299 slide_consume (s->write_buf, n);
304 if (!SLIDE_FULL (s->read_buf) && FD_ISSET (s->fd, rfds)) {
306 (SLIDE_SPACE (s->read_buf) >
307 MAX_TXN) ? MAX_TXN : SLIDE_SPACE (s->read_buf);
308 n = wrap_recv (s->fd, SLIDE_WPTR (s->read_buf), n);
310 slide_added (s->read_buf, n);
315 s->msg = ipc_check_for_message_in_slide (s->read_buf);
323 socket_write (Socket * s, void *buf, int len)
327 slide_expand (s->write_buf, len);
328 memcpy (SLIDE_WPTR (s->write_buf), buf, len);
329 slide_added (s->write_buf, len);
332 (SLIDE_BYTES (s->write_buf) >
333 MAX_TXN) ? MAX_TXN : SLIDE_BYTES (s->write_buf);
334 n = wrap_send (s->fd, SLIDE_RPTR (s->write_buf), n);
336 uint8_t *c = SLIDE_RPTR (s->write_buf);
340 slide_consume (s->write_buf, n);