Blame ares_writev.c

Packit 514978
Packit 514978
Packit 514978
/* Copyright 1998 by the Massachusetts Institute of Technology.
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this
Packit 514978
 * software and its documentation for any purpose and without
Packit 514978
 * fee is hereby granted, provided that the above copyright
Packit 514978
 * notice appear in all copies and that both that copyright
Packit 514978
 * notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in
Packit 514978
 * advertising or publicity pertaining to distribution of the
Packit 514978
 * software without specific, written prior permission.
Packit 514978
 * M.I.T. makes no representations about the suitability of
Packit 514978
 * this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
Packit 514978
#ifdef HAVE_LIMITS_H
Packit 514978
#  include <limits.h>
Packit 514978
#endif
Packit 514978
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
#ifndef HAVE_WRITEV
Packit 514978
ares_ssize_t ares_writev(ares_socket_t s, const struct iovec *iov, int iovcnt)
Packit 514978
{
Packit 514978
  char *buffer, *bp;
Packit 514978
  int i;
Packit 514978
  size_t bytes = 0;
Packit 514978
  ares_ssize_t result;
Packit 514978
Packit 514978
  /* Validate iovcnt */
Packit 514978
  if (iovcnt <= 0)
Packit 514978
  {
Packit 514978
    SET_ERRNO(EINVAL);
Packit 514978
    return (-1);
Packit 514978
  }
Packit 514978
Packit 514978
  /* Validate and find the sum of the iov_len values in the iov array */
Packit 514978
  for (i = 0; i < iovcnt; i++)
Packit 514978
  {
Packit 514978
    if (iov[i].iov_len > INT_MAX - bytes)
Packit 514978
    {
Packit 514978
      SET_ERRNO(EINVAL);
Packit 514978
      return (-1);
Packit 514978
    }
Packit 514978
    bytes += iov[i].iov_len;
Packit 514978
  }
Packit 514978
Packit 514978
  if (bytes == 0)
Packit 514978
    return (0);
Packit 514978
Packit 514978
  /* Allocate a temporary buffer to hold the data */
Packit 514978
  buffer = ares_malloc(bytes);
Packit 514978
  if (!buffer)
Packit 514978
  {
Packit 514978
    SET_ERRNO(ENOMEM);
Packit 514978
    return (-1);
Packit 514978
  }
Packit 514978
Packit 514978
  /* Copy the data into buffer */
Packit 514978
  for (bp = buffer, i = 0; i < iovcnt; ++i)
Packit 514978
  {
Packit 514978
    memcpy (bp, iov[i].iov_base, iov[i].iov_len);
Packit 514978
    bp += iov[i].iov_len;
Packit 514978
  }
Packit 514978
Packit 514978
  /* Send buffer contents */
Packit 514978
  result = swrite(s, buffer, bytes);
Packit 514978
Packit 514978
  ares_free(buffer);
Packit 514978
Packit 514978
  return (result);
Packit 514978
}
Packit 514978
#endif
Packit 514978