sockopt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_network_io.h"
  18. #include "fspr_general.h"
  19. #include "fspr_lib.h"
  20. #include "fspr_strings.h"
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <sys/socket.h>
  24. #include <netinet/tcp.h>
  25. #include <netinet/in.h>
  26. #include <unistd.h>
  27. #include <netdb.h>
  28. #include <sys/so_ioctl.h>
  29. APR_DECLARE(fspr_status_t) fspr_socket_timeout_set(fspr_socket_t *sock,
  30. fspr_interval_time_t t)
  31. {
  32. sock->timeout = t;
  33. return APR_SUCCESS;
  34. }
  35. APR_DECLARE(fspr_status_t) fspr_socket_opt_set(fspr_socket_t *sock,
  36. fspr_int32_t opt, fspr_int32_t on)
  37. {
  38. int one;
  39. struct linger li;
  40. if (on)
  41. one = 1;
  42. else
  43. one = 0;
  44. if (opt & APR_SO_KEEPALIVE) {
  45. if (setsockopt(sock->socketdes, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(int)) == -1) {
  46. return APR_OS2_STATUS(sock_errno());
  47. }
  48. }
  49. if (opt & APR_SO_DEBUG) {
  50. if (setsockopt(sock->socketdes, SOL_SOCKET, SO_DEBUG, (void *)&one, sizeof(int)) == -1) {
  51. return APR_OS2_STATUS(sock_errno());
  52. }
  53. }
  54. if (opt & APR_SO_REUSEADDR) {
  55. if (setsockopt(sock->socketdes, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(int)) == -1) {
  56. return APR_OS2_STATUS(sock_errno());
  57. }
  58. }
  59. if (opt & APR_SO_SNDBUF) {
  60. if (setsockopt(sock->socketdes, SOL_SOCKET, SO_SNDBUF, (void *)&on, sizeof(int)) == -1) {
  61. return APR_OS2_STATUS(sock_errno());
  62. }
  63. }
  64. if (opt & APR_SO_NONBLOCK) {
  65. if (ioctl(sock->socketdes, FIONBIO, (caddr_t)&one, sizeof(one)) == -1) {
  66. return APR_OS2_STATUS(sock_errno());
  67. } else {
  68. sock->nonblock = one;
  69. }
  70. }
  71. if (opt & APR_SO_LINGER) {
  72. li.l_onoff = on;
  73. li.l_linger = APR_MAX_SECS_TO_LINGER;
  74. if (setsockopt(sock->socketdes, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(struct linger)) == -1) {
  75. return APR_OS2_STATUS(sock_errno());
  76. }
  77. }
  78. if (opt & APR_TCP_NODELAY) {
  79. if (setsockopt(sock->socketdes, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(int)) == -1) {
  80. return APR_OS2_STATUS(sock_errno());
  81. }
  82. }
  83. return APR_SUCCESS;
  84. }
  85. APR_DECLARE(fspr_status_t) fspr_socket_timeout_get(fspr_socket_t *sock,
  86. fspr_interval_time_t *t)
  87. {
  88. *t = sock->timeout;
  89. return APR_SUCCESS;
  90. }
  91. APR_DECLARE(fspr_status_t) fspr_socket_opt_get(fspr_socket_t *sock,
  92. fspr_int32_t opt, fspr_int32_t *on)
  93. {
  94. switch(opt) {
  95. default:
  96. return APR_EINVAL;
  97. }
  98. return APR_SUCCESS;
  99. }
  100. APR_DECLARE(int) fspr_socket_fd_get(fspr_socket_t *sock)
  101. {
  102. if (sock) {
  103. return sock->socketdes;
  104. } else {
  105. return 0;
  106. }
  107. }
  108. APR_DECLARE(fspr_status_t) fspr_socket_atmark(fspr_socket_t *sock, int *atmark)
  109. {
  110. int oobmark;
  111. if (ioctl(sock->socketdes, SIOCATMARK, (void*)&oobmark, sizeof(oobmark)) < 0) {
  112. return APR_OS2_STATUS(sock_errno());
  113. }
  114. *atmark = (oobmark != 0);
  115. return APR_SUCCESS;
  116. }
  117. APR_DECLARE(fspr_status_t) fspr_gethostname(char *buf, fspr_int32_t len,
  118. fspr_pool_t *cont)
  119. {
  120. if (gethostname(buf, len) == -1) {
  121. buf[0] = '\0';
  122. return APR_OS2_STATUS(sock_errno());
  123. }
  124. else if (!memchr(buf, '\0', len)) { /* buffer too small */
  125. buf[0] = '\0';
  126. return APR_ENAMETOOLONG;
  127. }
  128. return APR_SUCCESS;
  129. }