1/** 2 * \file socket.c 3 * \brief Socket helper routines 4 * \author Abramo Bagnara <abramo@alsa-project.org> 5 * \date 2003 6 */ 7/* 8 * Socket helper routines 9 * Copyright (c) 2003 by Abramo Bagnara <abramo@alsa-project.org> 10 * 11 * 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. 16 * 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. 21 * 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 25 * 26 */ 27 28#include "local.h" 29#include <stdio.h> 30#include <stdlib.h> 31#include <stddef.h> 32#include <unistd.h> 33#include <string.h> 34#include <sys/socket.h> 35#include <sys/uio.h> 36#include <sys/un.h> 37#include <netinet/in.h> 38#include <sys/ioctl.h> 39#include <net/if.h> 40#include <netdb.h> 41 42#ifndef DOC_HIDDEN 43int snd_send_fd(int sock, void *data, size_t len, int fd) 44{ 45 int ret; 46 size_t cmsg_len = CMSG_LEN(sizeof(int)); 47 struct cmsghdr *cmsg = alloca(cmsg_len); 48 int *fds = (int *) CMSG_DATA(cmsg); 49 struct msghdr msghdr; 50 struct iovec vec; 51 52 vec.iov_base = (void *)&data; 53 vec.iov_len = len; 54 55 cmsg->cmsg_len = cmsg_len; 56 cmsg->cmsg_level = SOL_SOCKET; 57 cmsg->cmsg_type = SCM_RIGHTS; 58 *fds = fd; 59 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; 66 msghdr.msg_flags = 0; 67 68 ret = sendmsg(sock, &msghdr, 0 ); 69 if (ret < 0) { 70 SYSERR("sendmsg failed"); 71 return -errno; 72 } 73 return ret; 74} 75 76int snd_receive_fd(int sock, void *data, size_t len, int *fd) 77{ 78 int ret; 79 size_t cmsg_len = CMSG_LEN(sizeof(int)); 80 struct cmsghdr *cmsg = alloca(cmsg_len); 81 int *fds = (int *) CMSG_DATA(cmsg); 82 struct msghdr msghdr; 83 struct iovec vec; 84 85 vec.iov_base = (void *)&data; 86 vec.iov_len = len; 87 88 cmsg->cmsg_len = cmsg_len; 89 cmsg->cmsg_level = SOL_SOCKET; 90 cmsg->cmsg_type = SCM_RIGHTS; 91 *fds = -1; 92 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; 99 msghdr.msg_flags = 0; 100 101 ret = recvmsg(sock, &msghdr, 0); 102 if (ret < 0) { 103 SYSERR("recvmsg failed"); 104 return -errno; 105 } 106 *fd = *fds; 107 return ret; 108} 109#endif 110