iov.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Helpers for getting linearized buffers from iov / filling buffers into iovs
  3. *
  4. * Copyright IBM, Corp. 2007, 2008
  5. * Copyright (C) 2010 Red Hat, Inc.
  6. *
  7. * Author(s):
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Amit Shah <amit.shah@redhat.com>
  10. * Michael Tokarev <mjt@tls.msk.ru>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2. See
  13. * the COPYING file in the top-level directory.
  14. *
  15. * Contributions after 2012-01-13 are licensed under the terms of the
  16. * GNU GPL, version 2 or (at your option) any later version.
  17. */
  18. #include "iov.h"
  19. #ifdef _WIN32
  20. # include <windows.h>
  21. # include <winsock2.h>
  22. #else
  23. # include <sys/types.h>
  24. # include <sys/socket.h>
  25. #endif
  26. size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
  27. size_t offset, const void *buf, size_t bytes)
  28. {
  29. size_t done;
  30. unsigned int i;
  31. for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
  32. if (offset < iov[i].iov_len) {
  33. size_t len = MIN(iov[i].iov_len - offset, bytes - done);
  34. memcpy(iov[i].iov_base + offset, buf + done, len);
  35. done += len;
  36. offset = 0;
  37. } else {
  38. offset -= iov[i].iov_len;
  39. }
  40. }
  41. assert(offset == 0);
  42. return done;
  43. }
  44. size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
  45. size_t offset, void *buf, size_t bytes)
  46. {
  47. size_t done;
  48. unsigned int i;
  49. for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
  50. if (offset < iov[i].iov_len) {
  51. size_t len = MIN(iov[i].iov_len - offset, bytes - done);
  52. memcpy(buf + done, iov[i].iov_base + offset, len);
  53. done += len;
  54. offset = 0;
  55. } else {
  56. offset -= iov[i].iov_len;
  57. }
  58. }
  59. assert(offset == 0);
  60. return done;
  61. }
  62. size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
  63. size_t offset, int fillc, size_t bytes)
  64. {
  65. size_t done;
  66. unsigned int i;
  67. for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
  68. if (offset < iov[i].iov_len) {
  69. size_t len = MIN(iov[i].iov_len - offset, bytes - done);
  70. memset(iov[i].iov_base + offset, fillc, len);
  71. done += len;
  72. offset = 0;
  73. } else {
  74. offset -= iov[i].iov_len;
  75. }
  76. }
  77. assert(offset == 0);
  78. return done;
  79. }
  80. size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
  81. {
  82. size_t len;
  83. unsigned int i;
  84. len = 0;
  85. for (i = 0; i < iov_cnt; i++) {
  86. len += iov[i].iov_len;
  87. }
  88. return len;
  89. }
  90. /* helper function for iov_send_recv() */
  91. static ssize_t
  92. do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
  93. {
  94. #if defined CONFIG_IOVEC && defined CONFIG_POSIX
  95. ssize_t ret;
  96. struct msghdr msg;
  97. memset(&msg, 0, sizeof(msg));
  98. msg.msg_iov = iov;
  99. msg.msg_iovlen = iov_cnt;
  100. do {
  101. ret = do_send
  102. ? sendmsg(sockfd, &msg, 0)
  103. : recvmsg(sockfd, &msg, 0);
  104. } while (ret < 0 && errno == EINTR);
  105. return ret;
  106. #else
  107. /* else send piece-by-piece */
  108. /*XXX Note: windows has WSASend() and WSARecv() */
  109. unsigned i = 0;
  110. ssize_t ret = 0;
  111. while (i < iov_cnt) {
  112. ssize_t r = do_send
  113. ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
  114. : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
  115. if (r > 0) {
  116. ret += r;
  117. } else if (!r) {
  118. break;
  119. } else if (errno == EINTR) {
  120. continue;
  121. } else {
  122. /* else it is some "other" error,
  123. * only return if there was no data processed. */
  124. if (ret == 0) {
  125. ret = -1;
  126. }
  127. break;
  128. }
  129. i++;
  130. }
  131. return ret;
  132. #endif
  133. }
  134. ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
  135. size_t offset, size_t bytes,
  136. bool do_send)
  137. {
  138. ssize_t ret;
  139. unsigned si, ei; /* start and end indexes */
  140. if (bytes == 0) {
  141. /* Catch the do-nothing case early, as otherwise we will pass an
  142. * empty iovec to sendmsg/recvmsg(), and not all implementations
  143. * accept this.
  144. */
  145. return 0;
  146. }
  147. /* Find the start position, skipping `offset' bytes:
  148. * first, skip all full-sized vector elements, */
  149. for (si = 0; si < iov_cnt && offset >= iov[si].iov_len; ++si) {
  150. offset -= iov[si].iov_len;
  151. }
  152. if (offset) {
  153. assert(si < iov_cnt);
  154. /* second, skip `offset' bytes from the (now) first element,
  155. * undo it on exit */
  156. iov[si].iov_base += offset;
  157. iov[si].iov_len -= offset;
  158. }
  159. /* Find the end position skipping `bytes' bytes: */
  160. /* first, skip all full-sized elements */
  161. for (ei = si; ei < iov_cnt && iov[ei].iov_len <= bytes; ++ei) {
  162. bytes -= iov[ei].iov_len;
  163. }
  164. if (bytes) {
  165. /* second, fixup the last element, and remember
  166. * the length we've cut from the end of it in `bytes' */
  167. size_t tail;
  168. assert(ei < iov_cnt);
  169. assert(iov[ei].iov_len > bytes);
  170. tail = iov[ei].iov_len - bytes;
  171. iov[ei].iov_len = bytes;
  172. bytes = tail; /* bytes is now equal to the tail size */
  173. ++ei;
  174. }
  175. ret = do_send_recv(sockfd, iov + si, ei - si, do_send);
  176. /* Undo the changes above */
  177. if (offset) {
  178. iov[si].iov_base -= offset;
  179. iov[si].iov_len += offset;
  180. }
  181. if (bytes) {
  182. iov[ei-1].iov_len += bytes;
  183. }
  184. return ret;
  185. }
  186. void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
  187. FILE *fp, const char *prefix, size_t limit)
  188. {
  189. unsigned int i, v, b;
  190. uint8_t *c;
  191. c = iov[0].iov_base;
  192. for (i = 0, v = 0, b = 0; b < limit; i++, b++) {
  193. if (i == iov[v].iov_len) {
  194. i = 0; v++;
  195. if (v == iov_cnt) {
  196. break;
  197. }
  198. c = iov[v].iov_base;
  199. }
  200. if ((b % 16) == 0) {
  201. fprintf(fp, "%s: %04x:", prefix, b);
  202. }
  203. if ((b % 4) == 0) {
  204. fprintf(fp, " ");
  205. }
  206. fprintf(fp, " %02x", c[i]);
  207. if ((b % 16) == 15) {
  208. fprintf(fp, "\n");
  209. }
  210. }
  211. if ((b % 16) != 0) {
  212. fprintf(fp, "\n");
  213. }
  214. }