1243730Srwatson/*-
2243730Srwatson * Copyright (c) 2009-2010 The FreeBSD Foundation
3243730Srwatson * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4243730Srwatson * All rights reserved.
5243730Srwatson *
6243730Srwatson * This software was developed by Pawel Jakub Dawidek under sponsorship from
7243730Srwatson * the FreeBSD Foundation.
8243730Srwatson *
9243730Srwatson * Redistribution and use in source and binary forms, with or without
10243730Srwatson * modification, are permitted provided that the following conditions
11243730Srwatson * are met:
12243730Srwatson * 1. Redistributions of source code must retain the above copyright
13243730Srwatson *    notice, this list of conditions and the following disclaimer.
14243730Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15243730Srwatson *    notice, this list of conditions and the following disclaimer in the
16243730Srwatson *    documentation and/or other materials provided with the distribution.
17243730Srwatson *
18243730Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19243730Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20243730Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21243730Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22243730Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23243730Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24243730Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25243730Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26243730Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27243730Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28243730Srwatson * SUCH DAMAGE.
29243730Srwatson *
30243730Srwatson * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/proto_common.c#1 $
31243730Srwatson */
32243730Srwatson
33243730Srwatson#include <sys/types.h>
34243730Srwatson#include <sys/socket.h>
35243730Srwatson
36243730Srwatson#include <errno.h>
37243730Srwatson#include <fcntl.h>
38243730Srwatson#include <stdbool.h>
39243730Srwatson#include <stdlib.h>
40243730Srwatson#include <strings.h>
41243730Srwatson#include <unistd.h>
42243730Srwatson
43243730Srwatson#include <compat/compat.h>
44243730Srwatson
45243730Srwatson#include "pjdlog.h"
46243730Srwatson#include "proto_impl.h"
47243730Srwatson
48243730Srwatson/* Maximum size of packet we want to use when sending data. */
49243730Srwatson#ifndef MAX_SEND_SIZE
50243730Srwatson#define	MAX_SEND_SIZE	32768
51243730Srwatson#endif
52243730Srwatson
53243730Srwatsonstatic bool
54243730Srwatsonblocking_socket(int sock)
55243730Srwatson{
56243730Srwatson	int flags;
57243730Srwatson
58243730Srwatson	flags = fcntl(sock, F_GETFL);
59243730Srwatson	PJDLOG_ASSERT(flags >= 0);
60243730Srwatson	return ((flags & O_NONBLOCK) == 0);
61243730Srwatson}
62243730Srwatson
63243730Srwatsonstatic int
64243730Srwatsonproto_descriptor_send(int sock, int fd)
65243730Srwatson{
66243730Srwatson	unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
67243730Srwatson	struct msghdr msg;
68243730Srwatson	struct cmsghdr *cmsg;
69243730Srwatson
70243730Srwatson	PJDLOG_ASSERT(sock >= 0);
71243730Srwatson	PJDLOG_ASSERT(fd >= 0);
72243730Srwatson
73243730Srwatson	bzero(&msg, sizeof(msg));
74243730Srwatson	bzero(&ctrl, sizeof(ctrl));
75243730Srwatson
76243730Srwatson	msg.msg_iov = NULL;
77243730Srwatson	msg.msg_iovlen = 0;
78243730Srwatson	msg.msg_control = ctrl;
79243730Srwatson	msg.msg_controllen = sizeof(ctrl);
80243730Srwatson
81243730Srwatson	cmsg = CMSG_FIRSTHDR(&msg);
82243730Srwatson	cmsg->cmsg_level = SOL_SOCKET;
83243730Srwatson	cmsg->cmsg_type = SCM_RIGHTS;
84243730Srwatson	cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
85243730Srwatson	bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
86243730Srwatson
87243730Srwatson	if (sendmsg(sock, &msg, 0) == -1)
88243730Srwatson		return (errno);
89243730Srwatson
90243730Srwatson	return (0);
91243730Srwatson}
92243730Srwatson
93243730Srwatsonint
94243730Srwatsonproto_common_send(int sock, const unsigned char *data, size_t size, int fd)
95243730Srwatson{
96243730Srwatson	ssize_t done;
97243730Srwatson	size_t sendsize;
98243730Srwatson	int errcount = 0;
99243730Srwatson
100243730Srwatson	PJDLOG_ASSERT(sock >= 0);
101243730Srwatson
102243730Srwatson	if (data == NULL) {
103243730Srwatson		/* The caller is just trying to decide about direction. */
104243730Srwatson
105243730Srwatson		PJDLOG_ASSERT(size == 0);
106243730Srwatson
107243730Srwatson		if (shutdown(sock, SHUT_RD) == -1)
108243730Srwatson			return (errno);
109243730Srwatson		return (0);
110243730Srwatson	}
111243730Srwatson
112243730Srwatson	PJDLOG_ASSERT(data != NULL);
113243730Srwatson	PJDLOG_ASSERT(size > 0);
114243730Srwatson
115243730Srwatson	do {
116243730Srwatson		sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
117243730Srwatson		done = send(sock, data, sendsize, MSG_NOSIGNAL);
118243730Srwatson		if (done == 0) {
119243730Srwatson			return (ENOTCONN);
120243730Srwatson		} else if (done < 0) {
121243730Srwatson			if (errno == EINTR)
122243730Srwatson				continue;
123243730Srwatson			if (errno == ENOBUFS) {
124243730Srwatson				/*
125243730Srwatson				 * If there are no buffers we retry.
126243730Srwatson				 * After each try we increase delay before the
127243730Srwatson				 * next one and we give up after fifteen times.
128243730Srwatson				 * This gives 11s of total wait time.
129243730Srwatson				 */
130243730Srwatson				if (errcount == 15) {
131243730Srwatson					pjdlog_warning("Getting ENOBUFS errors for 11s on send(), giving up.");
132243730Srwatson				} else {
133243730Srwatson					if (errcount == 0)
134243730Srwatson						pjdlog_warning("Got ENOBUFS error on send(), retrying for a bit.");
135243730Srwatson					errcount++;
136243730Srwatson					usleep(100000 * errcount);
137243730Srwatson					continue;
138243730Srwatson				}
139243730Srwatson			}
140243730Srwatson			/*
141243730Srwatson			 * If this is blocking socket and we got EAGAIN, this
142243730Srwatson			 * means the request timed out. Translate errno to
143243730Srwatson			 * ETIMEDOUT, to give administrator a hint to
144243730Srwatson			 * eventually increase timeout.
145243730Srwatson			 */
146243730Srwatson			if (errno == EAGAIN && blocking_socket(sock))
147243730Srwatson				errno = ETIMEDOUT;
148243730Srwatson			return (errno);
149243730Srwatson		}
150243730Srwatson		data += done;
151243730Srwatson		size -= done;
152243730Srwatson	} while (size > 0);
153243730Srwatson	if (errcount > 0) {
154243730Srwatson		pjdlog_info("Data sent successfully after %d ENOBUFS error%s.",
155243730Srwatson		    errcount, errcount == 1 ? "" : "s");
156243730Srwatson	}
157243730Srwatson
158243730Srwatson	if (fd == -1)
159243730Srwatson		return (0);
160243730Srwatson	return (proto_descriptor_send(sock, fd));
161243730Srwatson}
162243730Srwatson
163243730Srwatsonstatic int
164243730Srwatsonproto_descriptor_recv(int sock, int *fdp)
165243730Srwatson{
166243730Srwatson	unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];
167243730Srwatson	struct msghdr msg;
168243730Srwatson	struct cmsghdr *cmsg;
169243730Srwatson
170243730Srwatson	PJDLOG_ASSERT(sock >= 0);
171243730Srwatson	PJDLOG_ASSERT(fdp != NULL);
172243730Srwatson
173243730Srwatson	bzero(&msg, sizeof(msg));
174243730Srwatson	bzero(&ctrl, sizeof(ctrl));
175243730Srwatson
176243730Srwatson	msg.msg_iov = NULL;
177243730Srwatson	msg.msg_iovlen = 0;
178243730Srwatson	msg.msg_control = ctrl;
179243730Srwatson	msg.msg_controllen = sizeof(ctrl);
180243730Srwatson
181243730Srwatson	if (recvmsg(sock, &msg, 0) == -1)
182243730Srwatson		return (errno);
183243730Srwatson
184243730Srwatson	cmsg = CMSG_FIRSTHDR(&msg);
185243730Srwatson	if (cmsg->cmsg_level != SOL_SOCKET ||
186243730Srwatson	    cmsg->cmsg_type != SCM_RIGHTS) {
187243730Srwatson		return (EINVAL);
188243730Srwatson	}
189243730Srwatson	bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
190243730Srwatson
191243730Srwatson	return (0);
192243730Srwatson}
193243730Srwatson
194243730Srwatsonint
195243730Srwatsonproto_common_recv(int sock, unsigned char *data, size_t size, int *fdp)
196243730Srwatson{
197243730Srwatson	ssize_t done;
198243730Srwatson
199243730Srwatson	PJDLOG_ASSERT(sock >= 0);
200243730Srwatson
201243730Srwatson	if (data == NULL) {
202243730Srwatson		/* The caller is just trying to decide about direction. */
203243730Srwatson
204243730Srwatson		PJDLOG_ASSERT(size == 0);
205243730Srwatson
206243730Srwatson		if (shutdown(sock, SHUT_WR) == -1)
207243730Srwatson			return (errno);
208243730Srwatson		return (0);
209243730Srwatson	}
210243730Srwatson
211243730Srwatson	PJDLOG_ASSERT(data != NULL);
212243730Srwatson	PJDLOG_ASSERT(size > 0);
213243730Srwatson
214243730Srwatson	do {
215243730Srwatson		done = recv(sock, data, size, MSG_WAITALL);
216243730Srwatson	} while (done == -1 && errno == EINTR);
217243730Srwatson	if (done == 0) {
218243730Srwatson		return (ENOTCONN);
219243730Srwatson	} else if (done < 0) {
220243730Srwatson		/*
221243730Srwatson		 * If this is blocking socket and we got EAGAIN, this
222243730Srwatson		 * means the request timed out. Translate errno to
223243730Srwatson		 * ETIMEDOUT, to give administrator a hint to
224243730Srwatson		 * eventually increase timeout.
225243730Srwatson		 */
226243730Srwatson		if (errno == EAGAIN && blocking_socket(sock))
227243730Srwatson			errno = ETIMEDOUT;
228243730Srwatson		return (errno);
229243730Srwatson	}
230243730Srwatson	if (fdp == NULL)
231243730Srwatson		return (0);
232243730Srwatson	return (proto_descriptor_recv(sock, fdp));
233243730Srwatson}
234