sendrecv_udp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_support.h"
  21. #include "fspr_lib.h"
  22. #include <sys/time.h>
  23. APR_DECLARE(fspr_status_t) fspr_socket_sendto(fspr_socket_t *sock,
  24. fspr_sockaddr_t *where,
  25. fspr_int32_t flags, const char *buf,
  26. fspr_size_t *len)
  27. {
  28. fspr_ssize_t rv;
  29. int serrno;
  30. do {
  31. rv = sendto(sock->socketdes, buf, (*len), flags,
  32. (struct sockaddr*)&where->sa,
  33. where->salen);
  34. } while (rv == -1 && (serrno = sock_errno()) == EINTR);
  35. if (rv == -1 && serrno == SOCEWOULDBLOCK && sock->timeout != 0) {
  36. fspr_status_t arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
  37. if (arv != APR_SUCCESS) {
  38. *len = 0;
  39. return arv;
  40. } else {
  41. do {
  42. rv = sendto(sock->socketdes, buf, *len, flags,
  43. (const struct sockaddr*)&where->sa,
  44. where->salen);
  45. } while (rv == -1 && (serrno = sock_errno()) == SOCEINTR);
  46. }
  47. }
  48. if (rv == -1) {
  49. *len = 0;
  50. return APR_FROM_OS_ERROR(serrno);
  51. }
  52. *len = rv;
  53. return APR_SUCCESS;
  54. }
  55. APR_DECLARE(fspr_status_t) fspr_socket_recvfrom(fspr_sockaddr_t *from,
  56. fspr_socket_t *sock,
  57. fspr_int32_t flags, char *buf,
  58. fspr_size_t *len)
  59. {
  60. fspr_ssize_t rv;
  61. int serrno;
  62. do {
  63. rv = recvfrom(sock->socketdes, buf, (*len), flags,
  64. (struct sockaddr*)&from->sa, &from->salen);
  65. } while (rv == -1 && (serrno = sock_errno()) == EINTR);
  66. if (rv == -1 && serrno == SOCEWOULDBLOCK && sock->timeout != 0) {
  67. fspr_status_t arv = fspr_wait_for_io_or_timeout(NULL, sock, 1);
  68. if (arv != APR_SUCCESS) {
  69. *len = 0;
  70. return arv;
  71. } else {
  72. do {
  73. rv = recvfrom(sock->socketdes, buf, *len, flags,
  74. (struct sockaddr*)&from->sa, &from->salen);
  75. } while (rv == -1 && (serrno = sock_errno()) == EINTR);
  76. }
  77. }
  78. if (rv == -1) {
  79. (*len) = 0;
  80. return APR_FROM_OS_ERROR(serrno);
  81. }
  82. (*len) = rv;
  83. if (rv == 0 && sock->type == SOCK_STREAM)
  84. return APR_EOF;
  85. return APR_SUCCESS;
  86. }