sendrecv.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "apr_arch_networkio.h"
  17. #include "apr_errno.h"
  18. #include "apr_general.h"
  19. #include "apr_network_io.h"
  20. #include "apr_lib.h"
  21. #include <sys/time.h>
  22. APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
  23. apr_size_t *len)
  24. {
  25. apr_ssize_t rv;
  26. int fds, err = 0;
  27. if (*len > 65536) {
  28. *len = 65536;
  29. }
  30. do {
  31. if (!sock->nonblock || err == SOCEWOULDBLOCK) {
  32. fds = sock->socketdes;
  33. rv = select(&fds, 0, 1, 0, sock->timeout >= 0 ? sock->timeout/1000 : -1);
  34. if (rv != 1) {
  35. *len = 0;
  36. err = sock_errno();
  37. if (rv == 0)
  38. return APR_TIMEUP;
  39. if (err == SOCEINTR)
  40. continue;
  41. return APR_OS2_STATUS(err);
  42. }
  43. }
  44. rv = send(sock->socketdes, buf, (*len), 0);
  45. err = rv < 0 ? sock_errno() : 0;
  46. } while (err == SOCEINTR || err == SOCEWOULDBLOCK);
  47. if (err) {
  48. *len = 0;
  49. return APR_OS2_STATUS(err);
  50. }
  51. (*len) = rv;
  52. return APR_SUCCESS;
  53. }
  54. APR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock, char *buf,
  55. apr_size_t *len)
  56. {
  57. apr_ssize_t rv;
  58. int fds, err = 0;
  59. do {
  60. if (!sock->nonblock || (err == SOCEWOULDBLOCK && sock->timeout != 0)) {
  61. fds = sock->socketdes;
  62. rv = select(&fds, 1, 0, 0, sock->timeout >= 0 ? sock->timeout/1000 : -1);
  63. if (rv != 1) {
  64. *len = 0;
  65. err = sock_errno();
  66. if (rv == 0)
  67. return APR_TIMEUP;
  68. if (err == SOCEINTR)
  69. continue;
  70. return APR_OS2_STATUS(err);
  71. }
  72. }
  73. rv = recv(sock->socketdes, buf, (*len), 0);
  74. err = rv < 0 ? sock_errno() : 0;
  75. } while (err == SOCEINTR || (err == SOCEWOULDBLOCK && sock->timeout != 0));
  76. if (err) {
  77. *len = 0;
  78. return APR_OS2_STATUS(err);
  79. }
  80. (*len) = rv;
  81. return rv == 0 ? APR_EOF : APR_SUCCESS;
  82. }
  83. APR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock,
  84. const struct iovec *vec,
  85. apr_int32_t nvec, apr_size_t *len)
  86. {
  87. apr_status_t rv;
  88. struct iovec *tmpvec;
  89. int fds, err = 0;
  90. int nv_tosend, total = 0;
  91. /* Make sure writev() only gets fed 64k at a time */
  92. for ( nv_tosend = 0; nv_tosend < nvec && total + vec[nv_tosend].iov_len < 65536; nv_tosend++ ) {
  93. total += vec[nv_tosend].iov_len;
  94. }
  95. tmpvec = alloca(sizeof(struct iovec) * nv_tosend);
  96. memcpy(tmpvec, vec, sizeof(struct iovec) * nv_tosend);
  97. do {
  98. if (!sock->nonblock || err == SOCEWOULDBLOCK) {
  99. fds = sock->socketdes;
  100. rv = select(&fds, 0, 1, 0, sock->timeout >= 0 ? sock->timeout/1000 : -1);
  101. if (rv != 1) {
  102. *len = 0;
  103. err = sock_errno();
  104. if (rv == 0)
  105. return APR_TIMEUP;
  106. if (err == SOCEINTR)
  107. continue;
  108. return APR_OS2_STATUS(err);
  109. }
  110. }
  111. rv = writev(sock->socketdes, tmpvec, nv_tosend);
  112. err = rv < 0 ? sock_errno() : 0;
  113. } while (err == SOCEINTR || err == SOCEWOULDBLOCK);
  114. if (err) {
  115. *len = 0;
  116. return APR_OS2_STATUS(err);
  117. }
  118. *len = rv;
  119. return APR_SUCCESS;
  120. }