1
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
16 */
17
18 #include "ares_setup.h"
19
20 #ifdef HAVE_LIMITS_H
21 # include <limits.h>
22 #endif
23
24 #include "ares.h"
25 #include "ares_private.h"
26
27 #ifndef HAVE_WRITEV
ares_writev(ares_socket_t s,const struct iovec * iov,int iovcnt)28 ares_ssize_t ares_writev(ares_socket_t s, const struct iovec *iov, int iovcnt)
29 {
30 char *buffer, *bp;
31 int i;
32 size_t bytes = 0;
33 ares_ssize_t result;
34
35 /* Validate iovcnt */
36 if (iovcnt <= 0)
37 {
38 SET_ERRNO(EINVAL);
39 return (-1);
40 }
41
42 /* Validate and find the sum of the iov_len values in the iov array */
43 for (i = 0; i < iovcnt; i++)
44 {
45 if (iov[i].iov_len > INT_MAX - bytes)
46 {
47 SET_ERRNO(EINVAL);
48 return (-1);
49 }
50 bytes += iov[i].iov_len;
51 }
52
53 if (bytes == 0)
54 return (0);
55
56 /* Allocate a temporary buffer to hold the data */
57 buffer = ares_malloc(bytes);
58 if (!buffer)
59 {
60 SET_ERRNO(ENOMEM);
61 return (-1);
62 }
63
64 /* Copy the data into buffer */
65 for (bp = buffer, i = 0; i < iovcnt; ++i)
66 {
67 memcpy (bp, iov[i].iov_base, iov[i].iov_len);
68 bp += iov[i].iov_len;
69 }
70
71 /* Send buffer contents */
72 result = swrite(s, buffer, bytes);
73
74 ares_free(buffer);
75
76 return (result);
77 }
78 #endif
79
80