/* libspawner, an implementation of the MTA side of Sendmail's Milter protocol. Copyright (C) 2005-07 Hilko Bengen This library is free software; you can redistribute it and/or modify it under the terms of version 2.1 of the GNU Lesser General Public License as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "protocol.h" #include #include #include #include #include /** \brief Writes a Milter protocol message from msg to socket s @param s Socket @param msg @param t timeout @return 0 on success, -1 on error. */ int write_msg(int s, smfi_msg* msg, time_t t) { u_int32_t total; u_int32_t offset = 0; int res; struct timeval tv; time_t now = time(NULL); time_t deadline = now + t + 1; total = ntohl(msg->size) + 4; do { if (now > deadline) return -1; tv.tv_sec = (deadline - now); tv.tv_usec = 0; if (setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)) == -1) return -1; res = send(s, ((char*)msg)+offset, total-offset, 0); if (res < 0) { if (errno != EINTR) return -1; } else { offset += res; } now = time(NULL); } while (offset < total); return 0; } /** \brief Read Milter protocol message from socket Reads Milter protocol message from socket s and puts it into a newly allocated structure msg. SMFIR_PROGRESS ('p') messages are skipped over. @param s Socket @param msg @param t timeout @return 0 on success, -1 on error. */ /* XXX return msg pointer? */ int read_msg(int s, smfi_msg** msg, time_t t) { int size; int total; int offset; int res; struct timeval tv; time_t now = time(NULL); time_t deadline = now + t + 1; do { tv.tv_sec = (deadline - now); tv.tv_usec = 0; if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) == -1) return -1; res = recv(s, &size, 4, 0); if (res < 4) { return -1; } total = ntohl(size); if (total < 1) { return -1; } *msg = realloc(*msg, total + 4); if (msg == NULL) { return -1; } (*msg)->size = size; offset = 0; now = time(NULL); while (offset < total) { if (now > deadline) return -1; tv.tv_sec = (deadline - now); tv.tv_usec = 0; if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) == -1) return -1; res = recv(s, &((*msg)->cmd)+offset, total-offset, 0); if (res < 0) { if (errno != EINTR) return -1; } else { offset += res; } now = time(NULL); } } while ((*msg)->cmd == SMFIR_PROGRESS); return 0; } /* Local Variables: c-file-style: "bsd" c-basic-offset: 8 indent-tabs-mode: nil End: */