2 * Copyright (c) 2005 Andrey Simonenko
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/socket.h>
41 #include "uc_common.h"
42 #include "t_generic.h"
43 #include "t_cmsg_len.h"
47 t_cmsg_len_client(int fd)
51 struct cmsghdr *cmsghdr;
53 size_t size, cmsg_size;
57 if (uc_sync_recv() < 0)
62 cmsg_size = CMSG_SPACE(sizeof(struct cmsgcred));
63 cmsg_data = malloc(cmsg_size);
64 if (cmsg_data == NULL) {
68 uc_msghdr_init_client(&msghdr, iov, cmsg_data, cmsg_size,
69 SCM_CREDS, sizeof(struct cmsgcred));
70 cmsghdr = CMSG_FIRSTHDR(&msghdr);
72 if (uc_socket_connect(fd) < 0)
75 size = msghdr.msg_iov != NULL ? msghdr.msg_iov->iov_len : 0;
77 for (socklen = 0; socklen < CMSG_LEN(0); ++socklen) {
78 cmsghdr->cmsg_len = socklen;
79 uc_dbgmsg("send: data size %zu", size);
80 uc_dbgmsg("send: msghdr.msg_controllen %u",
81 (u_int)msghdr.msg_controllen);
82 uc_dbgmsg("send: cmsghdr.cmsg_len %u",
83 (u_int)cmsghdr->cmsg_len);
84 if (sendmsg(fd, &msghdr, 0) < 0) {
85 uc_dbgmsg("sendmsg(2) failed: %s; retrying",
89 uc_logmsgx("sent message with cmsghdr.cmsg_len %u < %u",
90 (u_int)cmsghdr->cmsg_len, (u_int)CMSG_LEN(0));
93 if (socklen == CMSG_LEN(0))
96 if (uc_sync_send() < 0) {
106 t_cmsg_len_server(int fd1)
110 if (uc_sync_send() < 0)
115 if (uc_cfg.sock_type == SOCK_STREAM) {
116 fd2 = uc_socket_accept(fd1);
122 if (uc_sync_recv() < 0)
127 if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
128 if (uc_socket_close(fd2) < 0)
136 return (t_generic(t_cmsg_len_client, t_cmsg_len_server));