2 * Copyright (c) 1999-2004 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 SM_RCSID("@(#)$Id: comm.c,v 8.65 2004/07/07 21:41:30 ca Exp $")
14 #include "libmilter.h"
15 #include <sm/errstring.h>
18 static size_t Maxdatasize = MILTER_MAX_DATA_SIZE;
22 ** SMFI_SETMAXDATASIZE -- set limit for milter data read/write.
32 smfi_setmaxdatasize(sz)
41 #endif /* _FFR_MAXDATASIZE */
44 ** MI_RD_CMD -- read a command
47 ** sd -- socket descriptor
48 ** timeout -- maximum time to wait
49 ** cmd -- single character command read from sd
50 ** rlen -- pointer to length of result
51 ** name -- name of milter
54 ** buffer with rest of command
55 ** (malloc()ed here, should be free()d)
56 ** hack: encode error in cmd
60 mi_rd_cmd(sd, timeout, cmd, rlen, name)
62 struct timeval *timeout;
74 char data[MILTER_LEN_BYTES + 1];
82 FD_RD_INIT(sd, rds, excs);
83 ret = FD_RD_READY(sd, rds, excs, timeout);
92 if (FD_IS_RD_EXC(sd, rds, excs))
98 len = MI_SOCK_READ(sd, data + i, sizeof data - i);
99 if (MI_SOCK_READ_FAIL(len))
102 "%s, mi_rd_cmd: read returned %d: %s",
103 name, (int) len, sm_errstring(errno));
104 *cmd = SMFIC_RECVERR;
112 if (len >= (ssize_t) sizeof data - i)
118 *cmd = SMFIC_TIMEOUT;
124 "%s: mi_rd_cmd: select returned %d: %s",
125 name, ret, sm_errstring(errno));
126 *cmd = SMFIC_RECVERR;
130 *cmd = data[MILTER_LEN_BYTES];
131 data[MILTER_LEN_BYTES] = '\0';
132 (void) memcpy((void *) &expl, (void *) &(data[0]), MILTER_LEN_BYTES);
133 expl = ntohl(expl) - 1;
136 if (expl > Maxdatasize)
142 buf = malloc(expl + 1);
143 #else /* _FFR_ADD_NULL */
145 #endif /* _FFR_ADD_NULL */
155 FD_RD_INIT(sd, rds, excs);
156 ret = FD_RD_READY(sd, rds, excs, timeout);
165 if (FD_IS_RD_EXC(sd, rds, excs))
171 len = MI_SOCK_READ(sd, buf + i, expl - i);
172 if (MI_SOCK_READ_FAIL(len))
175 "%s: mi_rd_cmd: read returned %d: %s",
176 name, (int) len, sm_errstring(errno));
188 *cmd = SMFIC_RECVERR;
196 /* makes life simpler for common string routines */
198 #endif /* _FFR_ADD_NULL */
207 /* select returned 0 (timeout) or < 0 (error) */
210 *cmd = SMFIC_TIMEOUT;
216 "%s: mi_rd_cmd: select returned %d: %s",
217 name, ret, sm_errstring(save_errno));
218 *cmd = SMFIC_RECVERR;
221 *cmd = SMFIC_UNKNERR;
226 ** RETRY_WRITEV -- Keep calling the writev() system call
227 ** until all the data is written out or an error occurs.
230 ** fd -- socket descriptor
232 ** iovcnt -- number of elements in io vector
233 ** must NOT exceed UIO_MAXIOV.
234 ** timeout -- maximum time to wait
237 ** success: number of bytes written
238 ** otherwise: MI_FAILURE
242 retry_writev(fd, iov, iovcnt, timeout)
246 struct timeval *timeout;
255 while (iovcnt > 0 && iov[0].iov_len == 0)
264 ** We don't care much about the timeout here,
265 ** it's very long anyway; correct solution would be
266 ** to take the time before the loop and reduce the
267 ** timeout after each invocation.
268 ** FD_SETSIZE is checked when socket is created.
272 i = FD_WR_READY(fd, wrs, timeout);
277 if (errno == EINTR || errno == EAGAIN)
281 n = writev(fd, iov, iovcnt);
284 if (errno == EINTR || errno == EAGAIN)
290 for (i = 0; i < iovcnt; i++)
292 if (iov[i].iov_len > (unsigned int) n)
294 iov[i].iov_base = (char *)iov[i].iov_base + n;
295 iov[i].iov_len -= (unsigned int) n;
298 n -= (int) iov[i].iov_len;
307 ** MI_WR_CMD -- write a cmd to sd
310 ** sd -- socket descriptor
311 ** timeout -- maximum time to wait
312 ** cmd -- single character command to write
313 ** buf -- buffer with further data
314 ** len -- length of buffer (without cmd!)
317 ** MI_SUCCESS/MI_FAILURE
321 mi_wr_cmd(sd, timeout, cmd, buf, len)
323 struct timeval *timeout;
333 char data[MILTER_LEN_BYTES + 1];
335 if (len > Maxdatasize || (len > 0 && buf == NULL))
338 nl = htonl(len + 1); /* add 1 for the cmd char */
339 (void) memcpy(data, (void *) &nl, MILTER_LEN_BYTES);
340 data[MILTER_LEN_BYTES] = (char) cmd;
342 sl = MILTER_LEN_BYTES + 1;
344 /* set up the vector for the size / command */
345 iov[0].iov_base = (void *) data;
348 if (len >= 0 && buf != NULL)
350 iov[1].iov_base = (void *) buf;
351 iov[1].iov_len = len;
355 l = retry_writev(sd, iov, iovcnt, timeout);