]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/dsi/dsi_stream.c
Temp solution from trunk for bailing out on invalid DSI packets.
[netatalk.git] / libatalk / dsi / dsi_stream.c
index 5b275faefd003a043b335243e7fcf4b158a93a7b..026d82042ac0a94888d307140d00705455dbeafc 100644 (file)
@@ -1,4 +1,6 @@
 /*
+ * $Id: dsi_stream.c,v 1.4.2.2 2002-03-12 16:16:55 srittau Exp $
+ *
  * Copyright (c) 1998 Adrian Sun (asun@zoology.washington.edu)
  * All rights reserved. See COPYRIGHT.
  *
  * dsi_stream_receive:  read a DSI header + data.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #define USE_WRITEV
 
 #include <stdio.h>
 #include <stdlib.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif /* HAVE_UNISTD_H */
 #include <string.h>
 #include <errno.h>
 #include <sys/types.h>
 #ifdef USE_WRITEV
 #include <sys/uio.h>
-#endif
+#endif /* USE_WRITEV */
 #include <syslog.h>
 
 #include <atalk/dsi.h>
@@ -44,7 +52,7 @@ size_t dsi_stream_write(DSI *dsi, void *data, const size_t length)
       continue;
 
     if (len < 0) {
-      syslog(LOG_ERR, "dsi_stream_write: %m");
+      syslog(LOG_ERR, "dsi_stream_write: %s", strerror(errno));
       break;
     }
     
@@ -64,14 +72,14 @@ size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
   
   stored = 0;
   while (stored < length) {
-    if ((len = read(dsi->socket, (u_int8_t *) data + stored, 
-                   length - stored)) == -1 && errno == EINTR)
+    len = read(dsi->socket, (u_int8_t *) data + stored, length - stored);
+    if (len == -1 && errno == EINTR)
       continue;
 
-    if (len > 0)
+    else if (len > 0)
       stored += len;
-    else {/* eof or error */
-      syslog(LOG_ERR, "dsi_stream_read(%d): %m", len);
+    else { /* eof or error */
+      syslog(LOG_ERR, "dsi_stream_read(%d): %s", len, strerror(errno));
       break;
     }
   }
@@ -91,7 +99,7 @@ int dsi_stream_send(DSI *dsi, void *buf, size_t length)
   struct iovec iov[2];
   size_t  towrite;
   ssize_t len;
-#endif
+#endif /* USE_WRITEV */
 
   block[0] = dsi->header.dsi_flags;
   block[1] = dsi->header.dsi_command;
@@ -127,7 +135,7 @@ int dsi_stream_send(DSI *dsi, void *buf, size_t length)
     if (len == towrite) /* wrote everything out */
       break;
     else if (len < 0) { /* error */
-      syslog(LOG_ERR, "dsi_stream_send: %m");
+      syslog(LOG_ERR, "dsi_stream_send: %s", strerror(errno));
       sigprocmask(SIG_SETMASK, &oldset, NULL);
       return 0;
     }
@@ -146,14 +154,14 @@ int dsi_stream_send(DSI *dsi, void *buf, size_t length)
     }
   }
   
-#else
+#else /* USE_WRITEV */
   /* write the header then data */
   if ((dsi_stream_write(dsi, block, sizeof(block)) != sizeof(block)) ||
       (dsi_stream_write(dsi, buf, length) != length)) {
     sigprocmask(SIG_SETMASK, &oldset, NULL);
     return 0;
   }
-#endif
+#endif /* USE_WRITEV */
 
   sigprocmask(SIG_SETMASK, &oldset, NULL);
   return 1;
@@ -174,6 +182,14 @@ int dsi_stream_receive(DSI *dsi, void *buf, const int ilength,
 
   dsi->header.dsi_flags = block[0];
   dsi->header.dsi_command = block[1];
+  /* FIXME, not the right place, 
+     but we get a server disconnect without reason in the log
+  */
+  if (!block[1]) {
+      LOG(log_error, logtype_default, "dsi_stream_receive: invalid packet, fatal");
+      return 0;
+  }
+
   memcpy(&dsi->header.dsi_requestID, block + 2, 
         sizeof(dsi->header.dsi_requestID));
   memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));