Blame src/socket.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file socket.c
Packit 4a16fb
 * \brief Socket helper routines
Packit 4a16fb
 * \author Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 * \date 2003
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Socket helper routines
Packit 4a16fb
 *  Copyright (c) 2003 by Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
  
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <stddef.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <sys/socket.h>
Packit 4a16fb
#include <sys/uio.h>
Packit 4a16fb
#include <sys/un.h>
Packit 4a16fb
#include <netinet/in.h>
Packit 4a16fb
#include <sys/ioctl.h>
Packit 4a16fb
#include <net/if.h>
Packit 4a16fb
#include <netdb.h>
Packit 4a16fb
#include "local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
int snd_send_fd(int sock, void *data, size_t len, int fd)
Packit 4a16fb
{
Packit 4a16fb
	int ret;
Packit 4a16fb
	size_t cmsg_len = CMSG_LEN(sizeof(int));
Packit 4a16fb
	struct cmsghdr *cmsg = alloca(cmsg_len);
Packit 4a16fb
	int *fds = (int *) CMSG_DATA(cmsg);
Packit 4a16fb
	struct msghdr msghdr;
Packit 4a16fb
	struct iovec vec;
Packit 4a16fb
Packit 4a16fb
	vec.iov_base = (void *)&dat;;
Packit 4a16fb
	vec.iov_len = len;
Packit 4a16fb
Packit 4a16fb
	cmsg->cmsg_len = cmsg_len;
Packit 4a16fb
	cmsg->cmsg_level = SOL_SOCKET;
Packit 4a16fb
	cmsg->cmsg_type = SCM_RIGHTS;
Packit 4a16fb
	*fds = fd;
Packit 4a16fb
Packit 4a16fb
	msghdr.msg_name = NULL;
Packit 4a16fb
	msghdr.msg_namelen = 0;
Packit 4a16fb
	msghdr.msg_iov = &vec;
Packit 4a16fb
 	msghdr.msg_iovlen = 1;
Packit 4a16fb
	msghdr.msg_control = cmsg;
Packit 4a16fb
	msghdr.msg_controllen = cmsg_len;
Packit 4a16fb
	msghdr.msg_flags = 0;
Packit 4a16fb
Packit 4a16fb
	ret = sendmsg(sock, &msghdr, 0 );
Packit 4a16fb
	if (ret < 0) {
Packit 4a16fb
		SYSERR("sendmsg failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return ret;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int snd_receive_fd(int sock, void *data, size_t len, int *fd)
Packit 4a16fb
{
Packit 4a16fb
	int ret;
Packit 4a16fb
	size_t cmsg_len = CMSG_LEN(sizeof(int));
Packit 4a16fb
	struct cmsghdr *cmsg = alloca(cmsg_len);
Packit 4a16fb
	int *fds = (int *) CMSG_DATA(cmsg);
Packit 4a16fb
	struct msghdr msghdr;
Packit 4a16fb
	struct iovec vec;
Packit 4a16fb
Packit 4a16fb
	vec.iov_base = (void *)&dat;;
Packit 4a16fb
	vec.iov_len = len;
Packit 4a16fb
Packit 4a16fb
	cmsg->cmsg_len = cmsg_len;
Packit 4a16fb
	cmsg->cmsg_level = SOL_SOCKET;
Packit 4a16fb
	cmsg->cmsg_type = SCM_RIGHTS;
Packit 4a16fb
	*fds = -1;
Packit 4a16fb
Packit 4a16fb
	msghdr.msg_name = NULL;
Packit 4a16fb
	msghdr.msg_namelen = 0;
Packit 4a16fb
	msghdr.msg_iov = &vec;
Packit 4a16fb
	msghdr.msg_iovlen = 1;
Packit 4a16fb
	msghdr.msg_control = cmsg;
Packit 4a16fb
	msghdr.msg_controllen = cmsg_len;
Packit 4a16fb
	msghdr.msg_flags = 0;
Packit 4a16fb
Packit 4a16fb
	ret = recvmsg(sock, &msghdr, 0);
Packit 4a16fb
	if (ret < 0) {
Packit 4a16fb
		SYSERR("recvmsg failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	*fd = *fds;
Packit 4a16fb
	return ret;
Packit 4a16fb
}
Packit 4a16fb
#endif