sockets.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_arch_inherit.h"
  18. #include "apr_network_io.h"
  19. #include "apr_general.h"
  20. #include "apr_portable.h"
  21. #include "apr_lib.h"
  22. #include "apr_strings.h"
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <sys/socket.h>
  26. #include <netinet/tcp.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <netdb.h>
  30. #include "apr_arch_os2calls.h"
  31. static apr_status_t socket_cleanup(void *sock)
  32. {
  33. apr_socket_t *thesocket = sock;
  34. if (thesocket->socketdes < 0) {
  35. return APR_EINVALSOCK;
  36. }
  37. if (soclose(thesocket->socketdes) == 0) {
  38. thesocket->socketdes = -1;
  39. return APR_SUCCESS;
  40. }
  41. else {
  42. return APR_OS2_STATUS(sock_errno());
  43. }
  44. }
  45. static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol)
  46. {
  47. sock->type = type;
  48. sock->protocol = protocol;
  49. apr_sockaddr_vars_set(sock->local_addr, family, 0);
  50. apr_sockaddr_vars_set(sock->remote_addr, family, 0);
  51. }
  52. static void alloc_socket(apr_socket_t **new, apr_pool_t *p)
  53. {
  54. *new = (apr_socket_t *)apr_pcalloc(p, sizeof(apr_socket_t));
  55. (*new)->pool = p;
  56. (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
  57. sizeof(apr_sockaddr_t));
  58. (*new)->local_addr->pool = p;
  59. (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
  60. sizeof(apr_sockaddr_t));
  61. (*new)->remote_addr->pool = p;
  62. (*new)->remote_addr_unknown = 1;
  63. /* Create a pollset with room for one descriptor. */
  64. /* ### check return codes */
  65. (void) apr_pollset_create(&(*new)->pollset, 1, p, 0);
  66. }
  67. APR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock, int *protocol)
  68. {
  69. *protocol = sock->protocol;
  70. return APR_SUCCESS;
  71. }
  72. APR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new, int family, int type,
  73. int protocol, apr_pool_t *cont)
  74. {
  75. int downgrade = (family == AF_UNSPEC);
  76. apr_pollfd_t pfd;
  77. if (family == AF_UNSPEC) {
  78. #if APR_HAVE_IPV6
  79. family = AF_INET6;
  80. #else
  81. family = AF_INET;
  82. #endif
  83. }
  84. alloc_socket(new, cont);
  85. (*new)->socketdes = socket(family, type, protocol);
  86. #if APR_HAVE_IPV6
  87. if ((*new)->socketdes < 0 && downgrade) {
  88. family = AF_INET;
  89. (*new)->socketdes = socket(family, type, protocol);
  90. }
  91. #endif
  92. if ((*new)->socketdes < 0) {
  93. return APR_OS2_STATUS(sock_errno());
  94. }
  95. set_socket_vars(*new, family, type, protocol);
  96. (*new)->timeout = -1;
  97. (*new)->nonblock = FALSE;
  98. apr_pool_cleanup_register((*new)->pool, (void *)(*new),
  99. socket_cleanup, apr_pool_cleanup_null);
  100. return APR_SUCCESS;
  101. }
  102. APR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket,
  103. apr_shutdown_how_e how)
  104. {
  105. if (shutdown(thesocket->socketdes, how) == 0) {
  106. return APR_SUCCESS;
  107. }
  108. else {
  109. return APR_OS2_STATUS(sock_errno());
  110. }
  111. }
  112. APR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket)
  113. {
  114. apr_pool_cleanup_kill(thesocket->pool, thesocket, socket_cleanup);
  115. return socket_cleanup(thesocket);
  116. }
  117. APR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock,
  118. apr_sockaddr_t *sa)
  119. {
  120. if (bind(sock->socketdes,
  121. (struct sockaddr *)&sa->sa,
  122. sa->salen) == -1)
  123. return APR_OS2_STATUS(sock_errno());
  124. else {
  125. sock->local_addr = sa;
  126. /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
  127. if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */
  128. sock->local_port_unknown = 1; /* kernel got us an ephemeral port */
  129. }
  130. return APR_SUCCESS;
  131. }
  132. }
  133. APR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock,
  134. apr_int32_t backlog)
  135. {
  136. if (listen(sock->socketdes, backlog) == -1)
  137. return APR_OS2_STATUS(sock_errno());
  138. else
  139. return APR_SUCCESS;
  140. }
  141. APR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new,
  142. apr_socket_t *sock,
  143. apr_pool_t *connection_context)
  144. {
  145. alloc_socket(new, connection_context);
  146. set_socket_vars(*new, sock->local_addr->sa.sin.sin_family, SOCK_STREAM, sock->protocol);
  147. (*new)->timeout = -1;
  148. (*new)->nonblock = FALSE;
  149. (*new)->socketdes = accept(sock->socketdes,
  150. (struct sockaddr *)&(*new)->remote_addr->sa,
  151. &(*new)->remote_addr->salen);
  152. if ((*new)->socketdes < 0) {
  153. return APR_OS2_STATUS(sock_errno());
  154. }
  155. *(*new)->local_addr = *sock->local_addr;
  156. (*new)->local_addr->pool = connection_context;
  157. (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
  158. /* fix up any pointers which are no longer valid */
  159. if (sock->local_addr->sa.sin.sin_family == AF_INET) {
  160. (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin.sin_addr;
  161. }
  162. apr_pool_cleanup_register((*new)->pool, (void *)(*new),
  163. socket_cleanup, apr_pool_cleanup_null);
  164. return APR_SUCCESS;
  165. }
  166. APR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock,
  167. apr_sockaddr_t *sa)
  168. {
  169. if ((connect(sock->socketdes, (struct sockaddr *)&sa->sa.sin,
  170. sa->salen) < 0) &&
  171. (sock_errno() != SOCEINPROGRESS)) {
  172. return APR_OS2_STATUS(sock_errno());
  173. }
  174. else {
  175. int namelen = sizeof(sock->local_addr->sa.sin);
  176. getsockname(sock->socketdes, (struct sockaddr *)&sock->local_addr->sa.sin,
  177. &namelen);
  178. sock->remote_addr = sa;
  179. return APR_SUCCESS;
  180. }
  181. }
  182. APR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock, int *type)
  183. {
  184. *type = sock->type;
  185. return APR_SUCCESS;
  186. }
  187. APR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key,
  188. apr_socket_t *sock)
  189. {
  190. sock_userdata_t *cur = sock->userdata;
  191. *data = NULL;
  192. while (cur) {
  193. if (!strcmp(cur->key, key)) {
  194. *data = cur->data;
  195. break;
  196. }
  197. cur = cur->next;
  198. }
  199. return APR_SUCCESS;
  200. }
  201. APR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data, const char *key,
  202. apr_status_t (*cleanup) (void *))
  203. {
  204. sock_userdata_t *new = apr_palloc(sock->pool, sizeof(sock_userdata_t));
  205. new->key = apr_pstrdup(sock->pool, key);
  206. new->data = data;
  207. new->next = sock->userdata;
  208. sock->userdata = new;
  209. if (cleanup) {
  210. apr_pool_cleanup_register(sock->pool, data, cleanup, cleanup);
  211. }
  212. return APR_SUCCESS;
  213. }
  214. APR_DECLARE(apr_status_t) apr_os_sock_get(apr_os_sock_t *thesock, apr_socket_t *sock)
  215. {
  216. *thesock = sock->socketdes;
  217. return APR_SUCCESS;
  218. }
  219. APR_DECLARE(apr_status_t) apr_os_sock_make(apr_socket_t **apr_sock,
  220. apr_os_sock_info_t *os_sock_info,
  221. apr_pool_t *cont)
  222. {
  223. alloc_socket(apr_sock, cont);
  224. set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol);
  225. (*apr_sock)->timeout = -1;
  226. (*apr_sock)->socketdes = *os_sock_info->os_sock;
  227. if (os_sock_info->local) {
  228. memcpy(&(*apr_sock)->local_addr->sa.sin,
  229. os_sock_info->local,
  230. (*apr_sock)->local_addr->salen);
  231. /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
  232. (*apr_sock)->local_addr->port = ntohs((*apr_sock)->local_addr->sa.sin.sin_port);
  233. }
  234. else {
  235. (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1;
  236. }
  237. if (os_sock_info->remote) {
  238. memcpy(&(*apr_sock)->remote_addr->sa.sin,
  239. os_sock_info->remote,
  240. (*apr_sock)->remote_addr->salen);
  241. /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
  242. (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port);
  243. }
  244. else {
  245. (*apr_sock)->remote_addr_unknown = 1;
  246. }
  247. apr_pool_cleanup_register((*apr_sock)->pool, (void *)(*apr_sock),
  248. socket_cleanup, apr_pool_cleanup_null);
  249. return APR_SUCCESS;
  250. }
  251. APR_DECLARE(apr_status_t) apr_os_sock_put(apr_socket_t **sock, apr_os_sock_t *thesock, apr_pool_t *cont)
  252. {
  253. if (cont == NULL) {
  254. return APR_ENOPOOL;
  255. }
  256. if ((*sock) == NULL) {
  257. alloc_socket(sock, cont);
  258. set_socket_vars(*sock, AF_INET, SOCK_STREAM, 0);
  259. (*sock)->timeout = -1;
  260. }
  261. (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
  262. (*sock)->remote_addr_unknown = 1;
  263. (*sock)->socketdes = *thesock;
  264. return APR_SUCCESS;
  265. }
  266. APR_POOL_IMPLEMENT_ACCESSOR(socket);
  267. APR_IMPLEMENT_INHERIT_SET(socket, inherit, pool, socket_cleanup)
  268. APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, pool, socket_cleanup)