plibc: win32 related, socket
[oweals/gnunet.git] / src / util / mst.c
index a5c6f3c3eacb0877d6d6e8553e108ec9c57deadc..4ccf4988f5418338f87b13d115648beca656f31c 100644 (file)
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
-    
+
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+
+     SPDX-License-Identifier: AGPL3.0-or-later
+ */
 
 /**
  * @file util/mst.c
 #define ALIGN_FACTOR 8
 #endif
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util-mst", __VA_ARGS__)
+#define LOG(kind, ...) GNUNET_log_from(kind, "util-mst", __VA_ARGS__)
 
 
 /**
  * Handle to a message stream tokenizer.
  */
-struct GNUNET_MessageStreamTokenizer
-{
-
+struct GNUNET_MessageStreamTokenizer {
   /**
    * Function to call on completed messages.
    */
@@ -70,7 +70,6 @@ struct GNUNET_MessageStreamTokenizer
    * Beginning of the buffer.  Typed like this to force alignment.
    */
   struct GNUNET_MessageHeader *hdr;
-
 };
 
 
@@ -82,13 +81,13 @@ struct GNUNET_MessageStreamTokenizer
  * @return handle to tokenizer
  */
 struct GNUNET_MessageStreamTokenizer *
-GNUNET_MST_create (GNUNET_MessageTokenizerCallback cb,
-                   void *cb_cls)
+GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb,
+                  void *cb_cls)
 {
   struct GNUNET_MessageStreamTokenizer *ret;
 
-  ret = GNUNET_new (struct GNUNET_MessageStreamTokenizer);
-  ret->hdr = GNUNET_malloc (GNUNET_MIN_MESSAGE_SIZE);
+  ret = GNUNET_new(struct GNUNET_MessageStreamTokenizer);
+  ret->hdr = GNUNET_malloc(GNUNET_MIN_MESSAGE_SIZE);
   ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
   ret->cb = cb;
   ret->cb_cls = cb_cls;
@@ -111,11 +110,11 @@ GNUNET_MST_create (GNUNET_MessageTokenizerCallback cb,
  *         #GNUNET_SYSERR if the data stream is corrupt
  */
 int
-GNUNET_MST_from_buffer (struct GNUNET_MessageStreamTokenizer *mst,
-                        const char *buf,
-                        size_t size,
-                        int purge,
-                        int one_shot)
+GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst,
+                       const char *buf,
+                       size_t size,
+                       int purge,
+                       int one_shot)
 {
   const struct GNUNET_MessageHeader *hdr;
   size_t delta;
@@ -126,203 +125,203 @@ GNUNET_MST_from_buffer (struct GNUNET_MessageStreamTokenizer *mst,
   int ret;
   int cbret;
 
-  GNUNET_assert (mst->off <= mst->pos);
-  GNUNET_assert (mst->pos <= mst->curr_buf);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "MST receives %u bytes with %u bytes already in private buffer\n",
-       (unsigned int) size,
-       (unsigned int) (mst->pos - mst->off));
+  GNUNET_assert(mst->off <= mst->pos);
+  GNUNET_assert(mst->pos <= mst->curr_buf);
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+      "MST receives %u bytes with %u bytes already in private buffer\n",
+      (unsigned int)size,
+      (unsigned int)(mst->pos - mst->off));
   ret = GNUNET_OK;
-  ibuf = (char *) mst->hdr;
+  ibuf = (char *)mst->hdr;
   while (mst->pos > 0)
-  {
-do_align:
-    GNUNET_assert (mst->pos >= mst->off);
-    if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
-        (0 != (mst->off % ALIGN_FACTOR)))
-    {
-      /* need to align or need more space */
-      mst->pos -= mst->off;
-      memmove (ibuf,
-              &ibuf[mst->off],
-              mst->pos);
-      mst->off = 0;
-    }
-    if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
-    {
-      delta
-       = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader)
-                     - (mst->pos - mst->off),
-                     size);
-      GNUNET_memcpy (&ibuf[mst->pos],
-                    buf,
-                    delta);
-      mst->pos += delta;
-      buf += delta;
-      size -= delta;
-    }
-    if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
-    {
-      if (purge)
-      {
-        mst->off = 0;
-        mst->pos = 0;
-      }
-      return GNUNET_OK;
-    }
-    hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
-    want = ntohs (hdr->size);
-    if (want < sizeof (struct GNUNET_MessageHeader))
-    {
-      GNUNET_break_op (0);
-      return GNUNET_SYSERR;
-    }
-    if ( (mst->curr_buf - mst->off < want) &&
-        (mst->off > 0) )
-    {
-      /* can get more space by moving */
-      mst->pos -= mst->off;
-      memmove (ibuf,
-              &ibuf[mst->off],
-              mst->pos);
-      mst->off = 0;
-    }
-    if (mst->curr_buf < want)
-    {
-      /* need to get more space by growing buffer */
-      GNUNET_assert (0 == mst->off);
-      mst->hdr = GNUNET_realloc (mst->hdr,
-                                want);
-      ibuf = (char *) mst->hdr;
-      mst->curr_buf = want;
-    }
-    hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
-    if (mst->pos - mst->off < want)
-    {
-      delta = GNUNET_MIN (want - (mst->pos - mst->off),
-                         size);
-      GNUNET_assert (mst->pos + delta <= mst->curr_buf);
-      GNUNET_memcpy (&ibuf[mst->pos],
-                    buf,
-                    delta);
-      mst->pos += delta;
-      buf += delta;
-      size -= delta;
-    }
-    if (mst->pos - mst->off < want)
-    {
-      if (purge)
-      {
-        mst->off = 0;
-        mst->pos = 0;
-      }
-      return GNUNET_OK;
-    }
-    if (one_shot == GNUNET_SYSERR)
-    {
-      /* cannot call callback again, but return value saying that
-       * we have another full message in the buffer */
-      ret = GNUNET_NO;
-      goto copy;
-    }
-    if (one_shot == GNUNET_YES)
-      one_shot = GNUNET_SYSERR;
-    mst->off += want;
-    if (GNUNET_OK !=
-        (cbret = mst->cb (mst->cb_cls,
-                           hdr)))
-    {
-      if (GNUNET_SYSERR == cbret)
-        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                    "Failure processing message of type %u and size %u\n",
-                    ntohs (hdr->type),
-                    ntohs (hdr->size));
-      return GNUNET_SYSERR;
-    }
-    if (mst->off == mst->pos)
     {
-      /* reset to beginning of buffer, it's free right now! */
-      mst->off = 0;
-      mst->pos = 0;
-    }
-  }
-  GNUNET_assert (0 == mst->pos);
-  while (size > 0)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Server-mst has %u bytes left in inbound buffer\n",
-         (unsigned int) size);
-    if (size < sizeof (struct GNUNET_MessageHeader))
-      break;
-    offset = (unsigned long) buf;
-    need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
-    if (GNUNET_NO == need_align)
-    {
-      /* can try to do zero-copy and process directly from original buffer */
-      hdr = (const struct GNUNET_MessageHeader *) buf;
-      want = ntohs (hdr->size);
-      if (want < sizeof (struct GNUNET_MessageHeader))
-      {
-       GNUNET_break_op (0);
-        mst->off = 0;
-        return GNUNET_SYSERR;
-      }
-      if (size < want)
-        break;                  /* or not: buffer incomplete, so copy to private buffer... */
+do_align:
+      GNUNET_assert(mst->pos >= mst->off);
+      if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) ||
+          (0 != (mst->off % ALIGN_FACTOR)))
+        {
+          /* need to align or need more space */
+          mst->pos -= mst->off;
+          memmove(ibuf,
+                  &ibuf[mst->off],
+                  mst->pos);
+          mst->off = 0;
+        }
+      if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
+        {
+          delta
+            = GNUNET_MIN(sizeof(struct GNUNET_MessageHeader)
+                         - (mst->pos - mst->off),
+                         size);
+          GNUNET_memcpy(&ibuf[mst->pos],
+                        buf,
+                        delta);
+          mst->pos += delta;
+          buf += delta;
+          size -= delta;
+        }
+      if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
+        {
+          if (purge)
+            {
+              mst->off = 0;
+              mst->pos = 0;
+            }
+          return GNUNET_OK;
+        }
+      hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off];
+      want = ntohs(hdr->size);
+      if (want < sizeof(struct GNUNET_MessageHeader))
+        {
+          GNUNET_break_op(0);
+          return GNUNET_SYSERR;
+        }
+      if ((mst->curr_buf - mst->off < want) &&
+          (mst->off > 0))
+        {
+          /* can get more space by moving */
+          mst->pos -= mst->off;
+          memmove(ibuf,
+                  &ibuf[mst->off],
+                  mst->pos);
+          mst->off = 0;
+        }
+      if (mst->curr_buf < want)
+        {
+          /* need to get more space by growing buffer */
+          GNUNET_assert(0 == mst->off);
+          mst->hdr = GNUNET_realloc(mst->hdr,
+                                    want);
+          ibuf = (char *)mst->hdr;
+          mst->curr_buf = want;
+        }
+      hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off];
+      if (mst->pos - mst->off < want)
+        {
+          delta = GNUNET_MIN(want - (mst->pos - mst->off),
+                             size);
+          GNUNET_assert(mst->pos + delta <= mst->curr_buf);
+          GNUNET_memcpy(&ibuf[mst->pos],
+                        buf,
+                        delta);
+          mst->pos += delta;
+          buf += delta;
+          size -= delta;
+        }
+      if (mst->pos - mst->off < want)
+        {
+          if (purge)
+            {
+              mst->off = 0;
+              mst->pos = 0;
+            }
+          return GNUNET_OK;
+        }
       if (one_shot == GNUNET_SYSERR)
-      {
-        /* cannot call callback again, but return value saying that
-         * we have another full message in the buffer */
-        ret = GNUNET_NO;
-        goto copy;
-      }
+        {
+          /* cannot call callback again, but return value saying that
+           * we have another full message in the buffer */
+          ret = GNUNET_NO;
+          goto copy;
+        }
       if (one_shot == GNUNET_YES)
         one_shot = GNUNET_SYSERR;
+      mst->off += want;
       if (GNUNET_OK !=
-          (cbret = mst->cb (mst->cb_cls,
-                            hdr)))
-      {
-        if (GNUNET_SYSERR == cbret)
-          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                      "Failure processing message of type %u and size %u\n",
-                      ntohs (hdr->type),
-                      ntohs (hdr->size));
-        return GNUNET_SYSERR;
-      }
-      buf += want;
-      size -= want;
+          (cbret = mst->cb(mst->cb_cls,
+                           hdr)))
+        {
+          if (GNUNET_SYSERR == cbret)
+            GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
+                       "Failure processing message of type %u and size %u\n",
+                       ntohs(hdr->type),
+                       ntohs(hdr->size));
+          return GNUNET_SYSERR;
+        }
+      if (mst->off == mst->pos)
+        {
+          /* reset to beginning of buffer, it's free right now! */
+          mst->off = 0;
+          mst->pos = 0;
+        }
     }
-    else
+  GNUNET_assert(0 == mst->pos);
+  while (size > 0)
     {
-      /* need to copy to private buffer to align;
-       * yes, we go a bit more spagetti than usual here */
-      goto do_align;
+      LOG(GNUNET_ERROR_TYPE_DEBUG,
+          "Server-mst has %u bytes left in inbound buffer\n",
+          (unsigned int)size);
+      if (size < sizeof(struct GNUNET_MessageHeader))
+        break;
+      offset = (unsigned long)buf;
+      need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
+      if (GNUNET_NO == need_align)
+        {
+          /* can try to do zero-copy and process directly from original buffer */
+          hdr = (const struct GNUNET_MessageHeader *)buf;
+          want = ntohs(hdr->size);
+          if (want < sizeof(struct GNUNET_MessageHeader))
+            {
+              GNUNET_break_op(0);
+              mst->off = 0;
+              return GNUNET_SYSERR;
+            }
+          if (size < want)
+            break;              /* or not: buffer incomplete, so copy to private buffer... */
+          if (one_shot == GNUNET_SYSERR)
+            {
+              /* cannot call callback again, but return value saying that
+               * we have another full message in the buffer */
+              ret = GNUNET_NO;
+              goto copy;
+            }
+          if (one_shot == GNUNET_YES)
+            one_shot = GNUNET_SYSERR;
+          if (GNUNET_OK !=
+              (cbret = mst->cb(mst->cb_cls,
+                               hdr)))
+            {
+              if (GNUNET_SYSERR == cbret)
+                GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
+                           "Failure processing message of type %u and size %u\n",
+                           ntohs(hdr->type),
+                           ntohs(hdr->size));
+              return GNUNET_SYSERR;
+            }
+          buf += want;
+          size -= want;
+        }
+      else
+        {
+          /* need to copy to private buffer to align;
+           * yes, we go a bit more spagetti than usual here */
+          goto do_align;
+        }
     }
-  }
 copy:
   if ((size > 0) && (!purge))
-  {
-    if (size + mst->pos > mst->curr_buf)
     {
-      mst->hdr = GNUNET_realloc (mst->hdr,
-                                size + mst->pos);
-      ibuf = (char *) mst->hdr;
-      mst->curr_buf = size + mst->pos;
+      if (size + mst->pos > mst->curr_buf)
+        {
+          mst->hdr = GNUNET_realloc(mst->hdr,
+                                    size + mst->pos);
+          ibuf = (char *)mst->hdr;
+          mst->curr_buf = size + mst->pos;
+        }
+      GNUNET_assert(size + mst->pos <= mst->curr_buf);
+      GNUNET_memcpy(&ibuf[mst->pos],
+                    buf,
+                    size);
+      mst->pos += size;
     }
-    GNUNET_assert (size + mst->pos <= mst->curr_buf);
-    GNUNET_memcpy (&ibuf[mst->pos],
-                  buf,
-                  size);
-    mst->pos += size;
-  }
   if (purge)
-  {
-    mst->off = 0;
-    mst->pos = 0;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Server-mst leaves %u bytes in private buffer\n",
-       (unsigned int) (mst->pos - mst->off));
+    {
+      mst->off = 0;
+      mst->pos = 0;
+    }
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+      "Server-mst leaves %u bytes in private buffer\n",
+      (unsigned int)(mst->pos - mst->off));
   return ret;
 }
 
@@ -342,40 +341,40 @@ copy:
  *         #GNUNET_SYSERR if the data stream is corrupt
  */
 int
-GNUNET_MST_read (struct GNUNET_MessageStreamTokenizer *mst,
-                 struct GNUNET_NETWORK_Handle *sock,
-                 int purge,
-                 int one_shot)
+GNUNET_MST_read(struct GNUNET_MessageStreamTokenizer *mst,
+                struct GNUNET_NETWORK_Handle *sock,
+                int purge,
+                int one_shot)
 {
   ssize_t ret;
   size_t left;
   char *buf;
 
   left = mst->curr_buf - mst->pos;
-  buf = (char *) mst->hdr;
-  ret = GNUNET_NETWORK_socket_recv (sock,
-                                   &buf[mst->pos],
-                                   left);
+  buf = (char *)mst->hdr;
+  ret = GNUNET_NETWORK_socket_recv(sock,
+                                   &buf[mst->pos],
+                                   left);
   if (-1 == ret)
-  {
-    if ( (EAGAIN == errno) ||
-        (EINTR == errno) )
-      return GNUNET_OK;
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
-                        "recv");
-    return GNUNET_SYSERR;
-  }
+    {
+      if ((EAGAIN == errno) ||
+          (EINTR == errno))
+        return GNUNET_OK;
+      GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO,
+                          "recv");
+      return GNUNET_SYSERR;
+    }
   if (0 == ret)
-  {
-    /* other side closed connection, treat as error */
-    return GNUNET_SYSERR;
-  }
+    {
+      /* other side closed connection, treat as error */
+      return GNUNET_SYSERR;
+    }
   mst->pos += ret;
-  return GNUNET_MST_from_buffer (mst,
-                                NULL,
-                                0,
-                                purge,
-                                one_shot);
+  return GNUNET_MST_from_buffer(mst,
+                                NULL,
+                                0,
+                                purge,
+                                one_shot);
 }
 
 
@@ -391,14 +390,14 @@ GNUNET_MST_read (struct GNUNET_MessageStreamTokenizer *mst,
  *         #GNUNET_SYSERR if the data stream is corrupt
  */
 int
-GNUNET_MST_next (struct GNUNET_MessageStreamTokenizer *mst,
-                 int one_shot)
+GNUNET_MST_next(struct GNUNET_MessageStreamTokenizer *mst,
+                int one_shot)
 {
-  return GNUNET_MST_from_buffer (mst,
-                                 NULL,
-                                 0,
-                                 GNUNET_NO,
-                                 one_shot);
+  return GNUNET_MST_from_buffer(mst,
+                                NULL,
+                                0,
+                                GNUNET_NO,
+                                one_shot);
 }
 
 
@@ -408,10 +407,10 @@ GNUNET_MST_next (struct GNUNET_MessageStreamTokenizer *mst,
  * @param mst tokenizer to destroy
  */
 void
-GNUNET_MST_destroy (struct GNUNET_MessageStreamTokenizer *mst)
+GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
 {
-  GNUNET_free (mst->hdr);
-  GNUNET_free (mst);
+  GNUNET_free(mst->hdr);
+  GNUNET_free(mst);
 }