3 * \brief Socket helper routines
4 * \author Abramo Bagnara <abramo@alsa-project.org>
8 * Socket helper routines
9 * Copyright (c) 2003 by Abramo Bagnara <abramo@alsa-project.org>
12 * This library is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation; either version 2.1 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <sys/ioctl.h>
43 int snd_send_fd(int sock, void *data, size_t len, int fd)
46 size_t cmsg_len = CMSG_LEN(sizeof(int));
47 struct cmsghdr *cmsg = alloca(cmsg_len);
48 int *fds = (int *) CMSG_DATA(cmsg);
52 vec.iov_base = (void *)&data;
55 cmsg->cmsg_len = cmsg_len;
56 cmsg->cmsg_level = SOL_SOCKET;
57 cmsg->cmsg_type = SCM_RIGHTS;
60 msghdr.msg_name = NULL;
61 msghdr.msg_namelen = 0;
62 msghdr.msg_iov = &vec;
63 msghdr.msg_iovlen = 1;
64 msghdr.msg_control = cmsg;
65 msghdr.msg_controllen = cmsg_len;
68 ret = sendmsg(sock, &msghdr, 0 );
70 SYSERR("sendmsg failed");
76 int snd_receive_fd(int sock, void *data, size_t len, int *fd)
79 size_t cmsg_len = CMSG_LEN(sizeof(int));
80 struct cmsghdr *cmsg = alloca(cmsg_len);
81 int *fds = (int *) CMSG_DATA(cmsg);
85 vec.iov_base = (void *)&data;
88 cmsg->cmsg_len = cmsg_len;
89 cmsg->cmsg_level = SOL_SOCKET;
90 cmsg->cmsg_type = SCM_RIGHTS;
93 msghdr.msg_name = NULL;
94 msghdr.msg_namelen = 0;
95 msghdr.msg_iov = &vec;
96 msghdr.msg_iovlen = 1;
97 msghdr.msg_control = cmsg;
98 msghdr.msg_controllen = cmsg_len;
101 ret = recvmsg(sock, &msghdr, 0);
103 SYSERR("recvmsg failed");