sendrecv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_arch_networkio.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_general.h"
  19. #include "fspr_network_io.h"
  20. #include "fspr_lib.h"
  21. #include "fspr_arch_file_io.h"
  22. #if APR_HAVE_TIME_H
  23. #include <time.h>
  24. #endif
  25. /* MAX_SEGMENT_SIZE is the maximum amount of data that will be sent to a client
  26. * in one call of TransmitFile. This number must be small enough to give the
  27. * slowest client time to receive the data before the socket timeout triggers.
  28. * The same problem can exist with fspr_socket_send(). In that case, we rely on
  29. * the application to adjust socket timeouts and max send segment
  30. * sizes appropriately.
  31. * For example, Apache will in most cases call fspr_socket_send() with less
  32. * than 8193 bytes.
  33. */
  34. #define MAX_SEGMENT_SIZE 65536
  35. #define WSABUF_ON_STACK 50
  36. APR_DECLARE(fspr_status_t) fspr_socket_send(fspr_socket_t *sock, const char *buf,
  37. fspr_size_t *len)
  38. {
  39. fspr_ssize_t rv;
  40. WSABUF wsaData;
  41. int lasterror;
  42. DWORD dwBytes = 0;
  43. wsaData.len = (u_long)*len;
  44. wsaData.buf = (char*) buf;
  45. #ifndef _WIN32_WCE
  46. rv = WSASend(sock->socketdes, &wsaData, 1, &dwBytes, 0, NULL, NULL);
  47. #else
  48. rv = send(sock->socketdes, wsaData.buf, wsaData.len, 0);
  49. dwBytes = rv;
  50. #endif
  51. if (rv == SOCKET_ERROR) {
  52. lasterror = fspr_get_netos_error();
  53. *len = 0;
  54. return lasterror;
  55. }
  56. *len = dwBytes;
  57. return APR_SUCCESS;
  58. }
  59. APR_DECLARE(fspr_status_t) fspr_socket_recv(fspr_socket_t *sock, char *buf,
  60. fspr_size_t *len)
  61. {
  62. fspr_ssize_t rv;
  63. WSABUF wsaData;
  64. int lasterror;
  65. DWORD dwBytes = 0;
  66. DWORD flags = 0;
  67. wsaData.len = (u_long)*len;
  68. wsaData.buf = (char*) buf;
  69. #ifndef _WIN32_WCE
  70. rv = WSARecv(sock->socketdes, &wsaData, 1, &dwBytes, &flags, NULL, NULL);
  71. #else
  72. rv = recv(sock->socketdes, wsaData.buf, wsaData.len, 0);
  73. dwBytes = rv;
  74. #endif
  75. if (rv == SOCKET_ERROR) {
  76. lasterror = fspr_get_netos_error();
  77. *len = 0;
  78. return lasterror;
  79. }
  80. *len = dwBytes;
  81. return dwBytes == 0 ? APR_EOF : APR_SUCCESS;
  82. }
  83. APR_DECLARE(fspr_status_t) fspr_socket_sendv(fspr_socket_t *sock,
  84. const struct iovec *vec,
  85. fspr_int32_t in_vec, fspr_size_t *nbytes)
  86. {
  87. fspr_status_t rc = APR_SUCCESS;
  88. fspr_ssize_t rv;
  89. fspr_size_t cur_len;
  90. fspr_int32_t nvec = 0;
  91. int i, j = 0;
  92. DWORD dwBytes = 0;
  93. WSABUF *pWsaBuf;
  94. for (i = 0; i < in_vec; i++) {
  95. cur_len = vec[i].iov_len;
  96. nvec++;
  97. while (cur_len > APR_DWORD_MAX) {
  98. nvec++;
  99. cur_len -= APR_DWORD_MAX;
  100. }
  101. }
  102. pWsaBuf = (nvec <= WSABUF_ON_STACK) ? _alloca(sizeof(WSABUF) * (nvec))
  103. : malloc(sizeof(WSABUF) * (nvec));
  104. if (!pWsaBuf)
  105. return APR_ENOMEM;
  106. for (i = 0; i < in_vec; i++) {
  107. char * base = vec[i].iov_base;
  108. cur_len = vec[i].iov_len;
  109. do {
  110. if (cur_len > APR_DWORD_MAX) {
  111. pWsaBuf[j].buf = base;
  112. pWsaBuf[j].len = APR_DWORD_MAX;
  113. cur_len -= APR_DWORD_MAX;
  114. base += APR_DWORD_MAX;
  115. }
  116. else {
  117. pWsaBuf[j].buf = base;
  118. pWsaBuf[j].len = (DWORD)cur_len;
  119. cur_len = 0;
  120. }
  121. j++;
  122. } while (cur_len > 0);
  123. }
  124. #ifndef _WIN32_WCE
  125. rv = WSASend(sock->socketdes, pWsaBuf, nvec, &dwBytes, 0, NULL, NULL);
  126. if (rv == SOCKET_ERROR) {
  127. rc = fspr_get_netos_error();
  128. }
  129. #else
  130. for (i = 0; i < nvec; i++) {
  131. rv = send(sock->socketdes, pWsaBuf[i].buf, pWsaBuf[i].len, 0);
  132. if (rv == SOCKET_ERROR) {
  133. rc = fspr_get_netos_error();
  134. break;
  135. }
  136. dwBytes += rv;
  137. }
  138. #endif
  139. if (nvec > WSABUF_ON_STACK)
  140. free(pWsaBuf);
  141. *nbytes = dwBytes;
  142. return rc;
  143. }
  144. APR_DECLARE(fspr_status_t) fspr_socket_sendto(fspr_socket_t *sock,
  145. fspr_sockaddr_t *where,
  146. fspr_int32_t flags, const char *buf,
  147. fspr_size_t *len)
  148. {
  149. fspr_ssize_t rv;
  150. rv = sendto(sock->socketdes, buf, (int)*len, flags,
  151. (const struct sockaddr*)&where->sa,
  152. where->salen);
  153. if (rv == SOCKET_ERROR) {
  154. *len = 0;
  155. return fspr_get_netos_error();
  156. }
  157. *len = rv;
  158. return APR_SUCCESS;
  159. }
  160. APR_DECLARE(fspr_status_t) fspr_socket_recvfrom(fspr_sockaddr_t *from,
  161. fspr_socket_t *sock,
  162. fspr_int32_t flags,
  163. char *buf, fspr_size_t *len)
  164. {
  165. fspr_ssize_t rv;
  166. rv = recvfrom(sock->socketdes, buf, (int)*len, flags,
  167. (struct sockaddr*)&from->sa, &from->salen);
  168. if (rv == SOCKET_ERROR) {
  169. (*len) = 0;
  170. return fspr_get_netos_error();
  171. }
  172. (*len) = rv;
  173. if (rv == 0 && sock->type == SOCK_STREAM)
  174. return APR_EOF;
  175. return APR_SUCCESS;
  176. }
  177. static fspr_status_t collapse_iovec(char **off, fspr_size_t *len,
  178. struct iovec *iovec, int numvec,
  179. char *buf, fspr_size_t buflen)
  180. {
  181. if (numvec == 1) {
  182. *off = iovec[0].iov_base;
  183. *len = iovec[0].iov_len;
  184. }
  185. else {
  186. int i;
  187. for (i = 0; i < numvec; i++) {
  188. *len += iovec[i].iov_len;
  189. }
  190. if (*len > buflen) {
  191. *len = 0;
  192. return APR_INCOMPLETE;
  193. }
  194. *off = buf;
  195. for (i = 0; i < numvec; i++) {
  196. memcpy(buf, iovec[i].iov_base, iovec[i].iov_len);
  197. buf += iovec[i].iov_len;
  198. }
  199. }
  200. return APR_SUCCESS;
  201. }
  202. #if APR_HAS_SENDFILE
  203. /*
  204. * fspr_status_t fspr_socket_sendfile(fspr_socket_t *, fspr_file_t *, fspr_hdtr_t *,
  205. * fspr_off_t *, fspr_size_t *, fspr_int32_t flags)
  206. * Send a file from an open file descriptor to a socket, along with
  207. * optional headers and trailers
  208. * arg 1) The socket to which we're writing
  209. * arg 2) The open file from which to read
  210. * arg 3) A structure containing the headers and trailers to send
  211. * arg 4) Offset into the file where we should begin writing
  212. * arg 5) Number of bytes to send out of the file
  213. * arg 6) APR flags that are mapped to OS specific flags
  214. */
  215. APR_DECLARE(fspr_status_t) fspr_socket_sendfile(fspr_socket_t *sock,
  216. fspr_file_t *file,
  217. fspr_hdtr_t *hdtr,
  218. fspr_off_t *offset,
  219. fspr_size_t *len,
  220. fspr_int32_t flags)
  221. {
  222. fspr_status_t status = APR_SUCCESS;
  223. fspr_status_t rv;
  224. fspr_off_t curoff = *offset;
  225. DWORD dwFlags = 0;
  226. fspr_size_t nbytes;
  227. TRANSMIT_FILE_BUFFERS tfb, *ptfb = NULL;
  228. int ptr = 0;
  229. fspr_size_t bytes_to_send; /* Bytes to send out of the file (not including headers) */
  230. int disconnected = 0;
  231. int sendv_trailers = 0;
  232. char hdtrbuf[4096];
  233. if (fspr_os_level < APR_WIN_NT) {
  234. return APR_ENOTIMPL;
  235. }
  236. /* Use len to keep track of number of total bytes sent (including headers) */
  237. bytes_to_send = *len;
  238. *len = 0;
  239. /* Handle the goofy case of sending headers/trailers and a zero byte file */
  240. if (!bytes_to_send && hdtr) {
  241. if (hdtr->numheaders) {
  242. rv = fspr_socket_sendv(sock, hdtr->headers, hdtr->numheaders,
  243. &nbytes);
  244. if (rv != APR_SUCCESS)
  245. return rv;
  246. *len += nbytes;
  247. }
  248. if (hdtr->numtrailers) {
  249. rv = fspr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers,
  250. &nbytes);
  251. if (rv != APR_SUCCESS)
  252. return rv;
  253. *len += nbytes;
  254. }
  255. return APR_SUCCESS;
  256. }
  257. memset(&tfb, '\0', sizeof (tfb));
  258. /* Collapse the headers into a single buffer */
  259. if (hdtr && hdtr->numheaders) {
  260. fspr_size_t head_length = tfb.HeadLength;
  261. ptfb = &tfb;
  262. nbytes = 0;
  263. rv = collapse_iovec((char **)&ptfb->Head, &head_length,
  264. hdtr->headers, hdtr->numheaders,
  265. hdtrbuf, sizeof(hdtrbuf));
  266. tfb.HeadLength = (DWORD)head_length;
  267. /* If not enough buffer, punt to sendv */
  268. if (rv == APR_INCOMPLETE) {
  269. rv = fspr_socket_sendv(sock, hdtr->headers, hdtr->numheaders, &nbytes);
  270. if (rv != APR_SUCCESS)
  271. return rv;
  272. *len += nbytes;
  273. ptfb = NULL;
  274. }
  275. }
  276. /* Initialize the overlapped structure used on TransmitFile
  277. */
  278. if (!sock->overlapped) {
  279. sock->overlapped = fspr_pcalloc(sock->pool, sizeof(OVERLAPPED));
  280. sock->overlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  281. }
  282. while (bytes_to_send) {
  283. DWORD xmitbytes;
  284. if (bytes_to_send > MAX_SEGMENT_SIZE) {
  285. xmitbytes = MAX_SEGMENT_SIZE;
  286. }
  287. else {
  288. /* Last call to TransmitFile() */
  289. xmitbytes = (DWORD)bytes_to_send;
  290. /* Collapse the trailers into a single buffer */
  291. if (hdtr && hdtr->numtrailers) {
  292. fspr_size_t tail_length = tfb.TailLength;
  293. ptfb = &tfb;
  294. rv = collapse_iovec((char**) &ptfb->Tail, &tail_length,
  295. hdtr->trailers, hdtr->numtrailers,
  296. hdtrbuf + ptfb->HeadLength,
  297. sizeof(hdtrbuf) - ptfb->HeadLength);
  298. tfb.TailLength = (DWORD)tail_length;
  299. if (rv == APR_INCOMPLETE) {
  300. /* If not enough buffer, punt to sendv, later */
  301. sendv_trailers = 1;
  302. }
  303. }
  304. /* Disconnect the socket after last send */
  305. if ((flags & APR_SENDFILE_DISCONNECT_SOCKET)
  306. && !sendv_trailers) {
  307. dwFlags |= TF_REUSE_SOCKET;
  308. dwFlags |= TF_DISCONNECT;
  309. disconnected = 1;
  310. }
  311. }
  312. sock->overlapped->Offset = (DWORD)(curoff);
  313. #if APR_HAS_LARGE_FILES
  314. sock->overlapped->OffsetHigh = (DWORD)(curoff >> 32);
  315. #endif
  316. /* XXX BoundsChecker claims dwFlags must not be zero. */
  317. rv = TransmitFile(sock->socketdes, /* socket */
  318. file->filehand, /* open file descriptor of the file to be sent */
  319. xmitbytes, /* number of bytes to send. 0=send all */
  320. 0, /* Number of bytes per send. 0=use default */
  321. sock->overlapped, /* OVERLAPPED structure */
  322. ptfb, /* header and trailer buffers */
  323. dwFlags); /* flags to control various aspects of TransmitFile */
  324. if (!rv) {
  325. status = fspr_get_netos_error();
  326. if ((status == APR_FROM_OS_ERROR(ERROR_IO_PENDING)) ||
  327. (status == APR_FROM_OS_ERROR(WSA_IO_PENDING)))
  328. {
  329. rv = WaitForSingleObject(sock->overlapped->hEvent,
  330. (DWORD)(sock->timeout >= 0
  331. ? sock->timeout_ms : INFINITE));
  332. if (rv == WAIT_OBJECT_0) {
  333. status = APR_SUCCESS;
  334. if (!disconnected) {
  335. if (!WSAGetOverlappedResult(sock->socketdes,
  336. sock->overlapped,
  337. &xmitbytes,
  338. FALSE,
  339. &dwFlags)) {
  340. status = fspr_get_netos_error();
  341. }
  342. /* Ugly code alert: WSAGetOverlappedResult returns
  343. * a count of all bytes sent. This loop only
  344. * tracks bytes sent out of the file.
  345. */
  346. else if (ptfb) {
  347. xmitbytes -= (ptfb->HeadLength + ptfb->TailLength);
  348. }
  349. }
  350. }
  351. else if (rv == WAIT_TIMEOUT) {
  352. status = APR_FROM_OS_ERROR(WAIT_TIMEOUT);
  353. }
  354. else if (rv == WAIT_ABANDONED) {
  355. /* Hummm... WAIT_ABANDONDED is not an error code. It is
  356. * a return specific to the Win32 WAIT functions that
  357. * indicates that a thread exited while holding a
  358. * mutex. Should consider triggering an assert
  359. * to detect the condition...
  360. */
  361. status = APR_FROM_OS_ERROR(WAIT_TIMEOUT);
  362. }
  363. else
  364. status = fspr_get_os_error();
  365. }
  366. }
  367. if (status != APR_SUCCESS)
  368. break;
  369. bytes_to_send -= xmitbytes;
  370. curoff += xmitbytes;
  371. *len += xmitbytes;
  372. /* Adjust len for any headers/trailers sent */
  373. if (ptfb) {
  374. *len += (ptfb->HeadLength + ptfb->TailLength);
  375. memset(&tfb, '\0', sizeof (tfb));
  376. ptfb = NULL;
  377. }
  378. }
  379. if (status == APR_SUCCESS) {
  380. if (sendv_trailers) {
  381. rv = fspr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers, &nbytes);
  382. if (rv != APR_SUCCESS)
  383. return rv;
  384. *len += nbytes;
  385. }
  386. /* Mark the socket as disconnected, but do not close it.
  387. * Note: The application must have stored the socket prior to making
  388. * the call to fspr_socket_sendfile in order to either reuse it
  389. * or close it.
  390. */
  391. if (disconnected) {
  392. sock->disconnected = 1;
  393. sock->socketdes = INVALID_SOCKET;
  394. }
  395. }
  396. return status;
  397. }
  398. #endif