1 /* assuan-uds.c - Assuan unix domain socket utilities
2 * Copyright (C) 2006 Free Software Foundation, Inc.
4 * This file is part of Assuan.
6 * Assuan is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
29 #ifndef HAVE_W32_SYSTEM
30 #include <sys/socket.h>
43 #include "assuan-defs.h"
45 #ifdef USE_DESCRIPTOR_PASSING
46 /* Provide replacement for missing CMSG maccros. We assume that
47 size_t matches the alignment requirement. */
48 #define MY_ALIGN(n) ((((n))+ sizeof(size_t)-1) & (size_t)~(sizeof(size_t)-1))
50 #define CMSG_SPACE(n) (MY_ALIGN(sizeof(struct cmsghdr)) + MY_ALIGN((n)))
53 #define CMSG_LEN(n) (MY_ALIGN(sizeof(struct cmsghdr)) + (n))
56 #define CMSG_FIRSTHDR(mhdr) \
57 ((size_t)(mhdr)->msg_controllen >= sizeof (struct cmsghdr) \
58 ? (struct cmsghdr*) (mhdr)->msg_control : (struct cmsghdr*)NULL)
61 #define CMSG_DATA(cmsg) ((unsigned char*)((struct cmsghdr*)(cmsg)+1))
63 #endif /*USE_DESCRIPTOR_PASSING*/
66 /* Read from a unix domain socket using sendmsg.
68 FIXME: We don't need the buffering. It is a leftover from the time
69 when we used datagrams. */
71 uds_reader (assuan_context_t ctx
, void *buf
, size_t buflen
)
73 #ifndef HAVE_W32_SYSTEM
74 int len
= ctx
->uds
.buffersize
;
76 if (!ctx
->uds
.bufferallocated
)
78 ctx
->uds
.buffer
= xtrymalloc (2048);
80 return _assuan_error (ASSUAN_Out_Of_Core
);
81 ctx
->uds
.bufferallocated
= 2048;
84 while (!len
) /* No data is buffered. */
88 #ifdef USE_DESCRIPTOR_PASSING
91 char control
[CMSG_SPACE(sizeof (int))];
93 struct cmsghdr
*cmptr
;
94 #endif /*USE_DESCRIPTOR_PASSING*/
96 memset (&msg
, 0, sizeof (msg
));
100 msg
.msg_iov
= &iovec
;
102 iovec
.iov_base
= ctx
->uds
.buffer
;
103 iovec
.iov_len
= ctx
->uds
.bufferallocated
;
104 #ifdef USE_DESCRIPTOR_PASSING
105 msg
.msg_control
= control_u
.control
;
106 msg
.msg_controllen
= sizeof (control_u
.control
);
109 len
= _assuan_simple_recvmsg (ctx
, &msg
);
115 ctx
->uds
.buffersize
= len
;
116 ctx
->uds
.bufferoffset
= 0;
118 #ifdef USE_DESCRIPTOR_PASSING
119 cmptr
= CMSG_FIRSTHDR (&msg
);
120 if (cmptr
&& cmptr
->cmsg_len
== CMSG_LEN (sizeof(int)))
122 if (cmptr
->cmsg_level
!= SOL_SOCKET
123 || cmptr
->cmsg_type
!= SCM_RIGHTS
)
124 _assuan_log_printf ("unexpected ancillary data received\n");
127 int fd
= *((int*)CMSG_DATA (cmptr
));
129 if (ctx
->uds
.pendingfdscount
>= DIM (ctx
->uds
.pendingfds
))
131 _assuan_log_printf ("too many descriptors pending - "
132 "closing received descriptor %d\n", fd
);
136 ctx
->uds
.pendingfds
[ctx
->uds
.pendingfdscount
++] = fd
;
139 #endif /*USE_DESCRIPTOR_PASSING*/
142 /* Return some data to the user. */
144 if (len
> buflen
) /* We have more than the user requested. */
147 memcpy (buf
, (char*)ctx
->uds
.buffer
+ ctx
->uds
.bufferoffset
, len
);
148 ctx
->uds
.buffersize
-= len
;
149 assert (ctx
->uds
.buffersize
>= 0);
150 ctx
->uds
.bufferoffset
+= len
;
151 assert (ctx
->uds
.bufferoffset
<= ctx
->uds
.bufferallocated
);
154 #else /*HAVE_W32_SYSTEM*/
155 int res
= recvfrom (HANDLE2SOCKET(ctx
->inbound
.fd
), buf
, buflen
, 0, NULL
, NULL
);
157 errno
= _assuan_sock_wsa2errno (WSAGetLastError ());
159 #endif /*HAVE_W32_SYSTEM*/
163 /* Write to the domain server. */
165 uds_writer (assuan_context_t ctx
, const void *buf
, size_t buflen
)
167 #ifndef HAVE_W32_SYSTEM
172 memset (&msg
, 0, sizeof (msg
));
177 msg
.msg_iov
= &iovec
;
178 iovec
.iov_base
= (void*)buf
;
179 iovec
.iov_len
= buflen
;
181 len
= _assuan_simple_sendmsg (ctx
, &msg
);
184 #else /*HAVE_W32_SYSTEM*/
185 int res
= sendto (HANDLE2SOCKET(ctx
->outbound
.fd
), buf
, buflen
, 0,
186 (struct sockaddr
*)&ctx
->serveraddr
,
187 sizeof (struct sockaddr_in
));
189 errno
= _assuan_sock_wsa2errno (WSAGetLastError ());
191 #endif /*HAVE_W32_SYSTEM*/
195 static assuan_error_t
196 uds_sendfd (assuan_context_t ctx
, assuan_fd_t fd
)
198 #ifdef USE_DESCRIPTOR_PASSING
203 char control
[CMSG_SPACE(sizeof (int))];
205 struct cmsghdr
*cmptr
;
209 /* We need to send some real data so that a read won't return 0
210 which will be taken as an EOF. It also helps with debugging. */
211 snprintf (buffer
, sizeof(buffer
)-1, "# descriptor %d is in flight\n", fd
);
212 buffer
[sizeof(buffer
)-1] = 0;
214 memset (&msg
, 0, sizeof (msg
));
219 msg
.msg_iov
= &iovec
;
220 iovec
.iov_base
= buffer
;
221 iovec
.iov_len
= strlen (buffer
);
223 msg
.msg_control
= control_u
.control
;
224 msg
.msg_controllen
= sizeof (control_u
.control
);
225 cmptr
= CMSG_FIRSTHDR (&msg
);
226 cmptr
->cmsg_len
= CMSG_LEN(sizeof(int));
227 cmptr
->cmsg_level
= SOL_SOCKET
;
228 cmptr
->cmsg_type
= SCM_RIGHTS
;
229 *((int*)CMSG_DATA (cmptr
)) = fd
;
231 len
= _assuan_simple_sendmsg (ctx
, &msg
);
234 _assuan_log_printf ("uds_sendfd: %s\n", strerror (errno
));
235 return _assuan_error (ASSUAN_Write_Error
);
240 return _assuan_error (ASSUAN_Not_Implemented
);
245 static assuan_error_t
246 uds_receivefd (assuan_context_t ctx
, assuan_fd_t
*fd
)
248 #ifdef USE_DESCRIPTOR_PASSING
251 if (!ctx
->uds
.pendingfdscount
)
253 _assuan_log_printf ("no pending file descriptors!\n");
254 return _assuan_error (ASSUAN_General_Error
);
256 assert (ctx
->uds
.pendingfdscount
<= DIM(ctx
->uds
.pendingfds
));
258 *fd
= ctx
->uds
.pendingfds
[0];
259 for (i
=1; i
< ctx
->uds
.pendingfdscount
; i
++)
260 ctx
->uds
.pendingfds
[i
-1] = ctx
->uds
.pendingfds
[i
];
261 ctx
->uds
.pendingfdscount
--;
265 return _assuan_error (ASSUAN_Not_Implemented
);
270 /* Close all pending fds. */
272 _assuan_uds_close_fds (assuan_context_t ctx
)
276 for (i
= 0; i
< ctx
->uds
.pendingfdscount
; i
++)
277 _assuan_close (ctx
->uds
.pendingfds
[i
]);
278 ctx
->uds
.pendingfdscount
= 0;
281 /* Deinitialize the unix domain socket I/O functions. */
283 _assuan_uds_deinit (assuan_context_t ctx
)
285 /* First call the finish_handler which should close descriptors etc. */
286 ctx
->finish_handler (ctx
);
290 assert (ctx
->uds
.bufferallocated
);
291 ctx
->uds
.bufferallocated
= 0;
292 xfree (ctx
->uds
.buffer
);
295 _assuan_uds_close_fds (ctx
);
299 /* Helper function to initialize a context for domain I/O. */
301 _assuan_init_uds_io (assuan_context_t ctx
)
303 static struct assuan_io io
= { uds_reader
, uds_writer
,
304 uds_sendfd
, uds_receivefd
};
308 ctx
->uds
.bufferoffset
= 0;
309 ctx
->uds
.buffersize
= 0;
310 ctx
->uds
.bufferallocated
= 0;
311 ctx
->uds
.pendingfdscount
= 0;