+#ifndef MSG_MORE
+#define MSG_MORE 0x8000
+#endif
+
+#ifndef MSG_DONTWAIT
+#define MSG_DONTWAIT 0x40
+#endif
+
+/*
+ * afpd is sleeping too much while trying to send something.
+ * May be there's no reader or the reader is also sleeping in write,
+ * look if there's some data for us to read, hopefully it will wake up
+ * the reader so we can write again.
+ *
+ * @returns 0 when is possible to send again, -1 on error
+ */
+static int dsi_peek(DSI *dsi)
+{
+ static int warned = 0;
+ fd_set readfds, writefds;
+ int len;
+ int maxfd;
+ int ret;
+
+ LOG(log_debug, logtype_dsi, "dsi_peek");
+
+ maxfd = dsi->socket + 1;
+
+ while (1) {
+ if (dsi->socket == -1)
+ /* eg dsi_disconnect() might have disconnected us */
+ return -1;
+ FD_ZERO(&readfds);
+ FD_ZERO(&writefds);
+
+ if (dsi->eof < dsi->end) {
+ /* space in read buffer */
+ FD_SET( dsi->socket, &readfds);
+ } else {
+ if (!warned) {
+ warned = 1;
+ LOG(log_note, logtype_dsi, "dsi_peek: readahead buffer is full, possibly increase -dsireadbuf option");
+ LOG(log_note, logtype_dsi, "dsi_peek: dsireadbuf: %d, DSI quantum: %d, effective buffer size: %d",
+ dsi->dsireadbuf,
+ dsi->server_quantum ? dsi->server_quantum : DSI_SERVQUANT_DEF,
+ dsi->end - dsi->buffer);
+ }
+ }
+
+ FD_SET( dsi->socket, &writefds);
+
+ /* No timeout: if there's nothing to read nor nothing to write,
+ * we've got nothing to do at all */
+ if ((ret = select( maxfd, &readfds, &writefds, NULL, NULL)) <= 0) {
+ if (ret == -1 && errno == EINTR)
+ /* we might have been interrupted by out timer, so restart select */
+ continue;
+ /* give up */
+ LOG(log_error, logtype_dsi, "dsi_peek: unexpected select return: %d %s",
+ ret, ret < 0 ? strerror(errno) : "");
+ return -1;
+ }
+
+ if (FD_ISSET(dsi->socket, &writefds)) {
+ /* we can write again */
+ LOG(log_debug, logtype_dsi, "dsi_peek: can write again");
+ break;
+ }
+
+ /* Check if there's sth to read, hopefully reading that will unblock the client */
+ if (FD_ISSET(dsi->socket, &readfds)) {
+ len = dsi->end - dsi->eof; /* it's ensured above that there's space */
+
+ if ((len = read(dsi->socket, dsi->eof, len)) <= 0) {
+ if (len == 0) {
+ LOG(log_error, logtype_dsi, "dsi_peek: EOF");
+ return -1;
+ }
+ LOG(log_error, logtype_dsi, "dsi_peek: read: %s", strerror(errno));
+ if (errno == EAGAIN)
+ continue;
+ return -1;
+ }
+ LOG(log_debug, logtype_dsi, "dsi_peek: read %d bytes", len);
+
+ dsi->eof += len;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Return all bytes up to count from dsi->buffer if there are any buffered there
+ */
+static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count)
+{
+ size_t nbe = 0;
+
+ if (dsi->buffer == NULL)
+ /* afpd master has no DSI buffering */
+ return 0;
+
+ LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count);
+
+ nbe = dsi->eof - dsi->start;
+
+ if (nbe > 0) {
+ nbe = min((size_t)nbe, count);
+ memcpy(buf, dsi->start, nbe);
+ dsi->start += nbe;
+
+ if (dsi->eof == dsi->start)
+ dsi->start = dsi->eof = dsi->buffer;
+ }
+
+ LOG(log_debug, logtype_dsi, "from_buf(read: %u, unread:%u , space left: %u): returning %u",
+ dsi->start - dsi->buffer, dsi->eof - dsi->start, dsi->end - dsi->eof, nbe);
+
+ return nbe;
+}
+
+/*
+ * Get bytes from buffer dsi->buffer or read from socket
+ *
+ * 1. Check if there are bytes in the the dsi->buffer buffer.
+ * 2. Return bytes from (1) if yes.
+ * Note: this may return fewer bytes then requested in count !!
+ * 3. If the buffer was empty, read from the socket.
+ */
+static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count)
+{
+ ssize_t len;
+
+ LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count);