2
0

sockets.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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_network_io.h"
  18. #include "apr_strings.h"
  19. #include "apr_support.h"
  20. #include "apr_portable.h"
  21. #include "apr_arch_inherit.h"
  22. #ifdef BEOS_R5
  23. #undef close
  24. #define close closesocket
  25. #endif /* BEOS_R5 */
  26. static char generic_inaddr_any[16] = {0}; /* big enough for IPv4 or IPv6 */
  27. static apr_status_t socket_cleanup(void *sock)
  28. {
  29. apr_socket_t *thesocket = sock;
  30. if (!thesocket) {
  31. return APR_ENOTSOCK;
  32. }
  33. if (thesocket->socketdes == -1) {
  34. return APR_SUCCESS;
  35. }
  36. if (close(thesocket->socketdes) == 0) {
  37. thesocket->socketdes = -1;
  38. return APR_SUCCESS;
  39. }
  40. else {
  41. return errno;
  42. }
  43. }
  44. static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol)
  45. {
  46. sock->type = type;
  47. sock->protocol = protocol;
  48. apr_sockaddr_vars_set(sock->local_addr, family, 0);
  49. apr_sockaddr_vars_set(sock->remote_addr, family, 0);
  50. sock->options = 0;
  51. #if defined(BEOS) && !defined(BEOS_BONE)
  52. /* BeOS pre-BONE has TCP_NODELAY on by default and it can't be
  53. * switched off!
  54. */
  55. sock->options |= APR_TCP_NODELAY;
  56. #endif
  57. }
  58. static void alloc_socket(apr_socket_t **new, apr_pool_t *p)
  59. {
  60. *new = (apr_socket_t *)apr_pcalloc(p, sizeof(apr_socket_t));
  61. (*new)->pool = p;
  62. (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
  63. sizeof(apr_sockaddr_t));
  64. (*new)->local_addr->pool = p;
  65. (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
  66. sizeof(apr_sockaddr_t));
  67. (*new)->remote_addr->pool = p;
  68. (*new)->remote_addr_unknown = 1;
  69. #ifndef WAITIO_USES_POLL
  70. /* Create a pollset with room for one descriptor. */
  71. /* ### check return codes */
  72. (void) apr_pollset_create(&(*new)->pollset, 1, p, 0);
  73. #endif
  74. }
  75. apr_status_t apr_socket_protocol_get(apr_socket_t *sock, int *protocol)
  76. {
  77. *protocol = sock->protocol;
  78. return APR_SUCCESS;
  79. }
  80. apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type,
  81. int protocol, apr_pool_t *cont)
  82. {
  83. int family = ofamily;
  84. if (family == APR_UNSPEC) {
  85. #if APR_HAVE_IPV6
  86. family = APR_INET6;
  87. #else
  88. family = APR_INET;
  89. #endif
  90. }
  91. alloc_socket(new, cont);
  92. #ifndef BEOS_R5
  93. (*new)->socketdes = socket(family, type, protocol);
  94. #else
  95. /* For some reason BeOS R5 has an unconventional protocol numbering,
  96. * so we need to translate here. */
  97. switch (protocol) {
  98. case 0:
  99. (*new)->socketdes = socket(family, type, 0);
  100. break;
  101. case APR_PROTO_TCP:
  102. (*new)->socketdes = socket(family, type, IPPROTO_TCP);
  103. break;
  104. case APR_PROTO_UDP:
  105. (*new)->socketdes = socket(family, type, IPPROTO_UDP);
  106. break;
  107. case APR_PROTO_SCTP:
  108. default:
  109. errno = EPROTONOSUPPORT;
  110. (*new)->socketdes = -1;
  111. break;
  112. }
  113. #endif /* BEOS_R5 */
  114. #if APR_HAVE_IPV6
  115. if ((*new)->socketdes < 0 && ofamily == APR_UNSPEC) {
  116. family = APR_INET;
  117. (*new)->socketdes = socket(family, type, protocol);
  118. }
  119. #endif
  120. if ((*new)->socketdes < 0) {
  121. return errno;
  122. }
  123. set_socket_vars(*new, family, type, protocol);
  124. (*new)->timeout = -1;
  125. (*new)->inherit = 0;
  126. apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
  127. socket_cleanup);
  128. return APR_SUCCESS;
  129. }
  130. apr_status_t apr_socket_shutdown(apr_socket_t *thesocket,
  131. apr_shutdown_how_e how)
  132. {
  133. return (shutdown(thesocket->socketdes, how) == -1) ? errno : APR_SUCCESS;
  134. }
  135. apr_status_t apr_socket_close(apr_socket_t *thesocket)
  136. {
  137. return apr_pool_cleanup_run(thesocket->pool, thesocket, socket_cleanup);
  138. }
  139. apr_status_t apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
  140. {
  141. if (bind(sock->socketdes,
  142. (struct sockaddr *)&sa->sa, sa->salen) == -1) {
  143. return errno;
  144. }
  145. else {
  146. sock->local_addr = sa;
  147. /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
  148. if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */
  149. sock->local_port_unknown = 1; /* kernel got us an ephemeral port */
  150. }
  151. return APR_SUCCESS;
  152. }
  153. }
  154. apr_status_t apr_socket_listen(apr_socket_t *sock, apr_int32_t backlog)
  155. {
  156. if (listen(sock->socketdes, backlog) == -1)
  157. return errno;
  158. else
  159. return APR_SUCCESS;
  160. }
  161. apr_status_t apr_socket_accept(apr_socket_t **new, apr_socket_t *sock,
  162. apr_pool_t *connection_context)
  163. {
  164. alloc_socket(new, connection_context);
  165. set_socket_vars(*new, sock->local_addr->sa.sin.sin_family, SOCK_STREAM, sock->protocol);
  166. #ifndef HAVE_POLL
  167. (*new)->connected = 1;
  168. #endif
  169. (*new)->timeout = -1;
  170. (*new)->socketdes = accept(sock->socketdes,
  171. (struct sockaddr *)&(*new)->remote_addr->sa,
  172. &(*new)->remote_addr->salen);
  173. if ((*new)->socketdes < 0) {
  174. return errno;
  175. }
  176. #ifdef TPF
  177. if ((*new)->socketdes == 0) {
  178. /* 0 is an invalid socket for TPF */
  179. return APR_EINTR;
  180. }
  181. #endif
  182. (*new)->remote_addr_unknown = 0;
  183. *(*new)->local_addr = *sock->local_addr;
  184. /* The above assignment just overwrote the pool entry. Setting the local_addr
  185. pool for the accepted socket back to what it should be. Otherwise all
  186. allocations for this socket will come from a server pool that is not
  187. freed until the process goes down.*/
  188. (*new)->local_addr->pool = connection_context;
  189. /* fix up any pointers which are no longer valid */
  190. if (sock->local_addr->sa.sin.sin_family == AF_INET) {
  191. (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin.sin_addr;
  192. }
  193. #if APR_HAVE_IPV6
  194. else if (sock->local_addr->sa.sin.sin_family == AF_INET6) {
  195. (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin6.sin6_addr;
  196. }
  197. #endif
  198. (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
  199. if (sock->local_port_unknown) {
  200. /* not likely for a listening socket, but theoretically possible :) */
  201. (*new)->local_port_unknown = 1;
  202. }
  203. #if APR_TCP_NODELAY_INHERITED
  204. if (apr_is_option_set(sock, APR_TCP_NODELAY) == 1) {
  205. apr_set_option(*new, APR_TCP_NODELAY, 1);
  206. }
  207. #endif /* TCP_NODELAY_INHERITED */
  208. #if APR_O_NONBLOCK_INHERITED
  209. if (apr_is_option_set(sock, APR_SO_NONBLOCK) == 1) {
  210. apr_set_option(*new, APR_SO_NONBLOCK, 1);
  211. }
  212. #endif /* APR_O_NONBLOCK_INHERITED */
  213. if (sock->local_interface_unknown ||
  214. !memcmp(sock->local_addr->ipaddr_ptr,
  215. generic_inaddr_any,
  216. sock->local_addr->ipaddr_len)) {
  217. /* If the interface address inside the listening socket's local_addr wasn't
  218. * up-to-date, we don't know local interface of the connected socket either.
  219. *
  220. * If the listening socket was not bound to a specific interface, we
  221. * don't know the local_addr of the connected socket.
  222. */
  223. (*new)->local_interface_unknown = 1;
  224. }
  225. (*new)->inherit = 0;
  226. apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
  227. socket_cleanup);
  228. return APR_SUCCESS;
  229. }
  230. apr_status_t apr_socket_connect(apr_socket_t *sock, apr_sockaddr_t *sa)
  231. {
  232. int rc;
  233. do {
  234. rc = connect(sock->socketdes,
  235. (const struct sockaddr *)&sa->sa.sin,
  236. sa->salen);
  237. } while (rc == -1 && errno == EINTR);
  238. /* we can see EINPROGRESS the first time connect is called on a non-blocking
  239. * socket; if called again, we can see EALREADY
  240. */
  241. if ((rc == -1) && (errno == EINPROGRESS || errno == EALREADY)
  242. && (sock->timeout > 0)) {
  243. rc = apr_wait_for_io_or_timeout(NULL, sock, 0);
  244. if (rc != APR_SUCCESS) {
  245. return rc;
  246. }
  247. #ifdef SO_ERROR
  248. {
  249. int error;
  250. apr_socklen_t len = sizeof(error);
  251. if ((rc = getsockopt(sock->socketdes, SOL_SOCKET, SO_ERROR,
  252. (char *)&error, &len)) < 0) {
  253. return errno;
  254. }
  255. if (error) {
  256. return error;
  257. }
  258. }
  259. #endif /* SO_ERROR */
  260. }
  261. if (rc == -1 && errno != EISCONN) {
  262. return errno;
  263. }
  264. if (memcmp(sa->ipaddr_ptr, generic_inaddr_any, sa->ipaddr_len)) {
  265. /* A real remote address was passed in. If the unspecified
  266. * address was used, the actual remote addr will have to be
  267. * determined using getpeername() if required. */
  268. /* ### this should probably be a structure copy + fixup as per
  269. * _accept()'s handling of local_addr */
  270. sock->remote_addr = sa;
  271. sock->remote_addr_unknown = 0;
  272. }
  273. if (sock->local_addr->port == 0) {
  274. /* connect() got us an ephemeral port */
  275. sock->local_port_unknown = 1;
  276. }
  277. if (!memcmp(sock->local_addr->ipaddr_ptr,
  278. generic_inaddr_any,
  279. sock->local_addr->ipaddr_len)) {
  280. /* not bound to specific local interface; connect() had to assign
  281. * one for the socket
  282. */
  283. sock->local_interface_unknown = 1;
  284. }
  285. #ifndef HAVE_POLL
  286. sock->connected=1;
  287. #endif
  288. return APR_SUCCESS;
  289. }
  290. apr_status_t apr_socket_type_get(apr_socket_t *sock, int *type)
  291. {
  292. *type = sock->type;
  293. return APR_SUCCESS;
  294. }
  295. apr_status_t apr_socket_data_get(void **data, const char *key, apr_socket_t *sock)
  296. {
  297. sock_userdata_t *cur = sock->userdata;
  298. *data = NULL;
  299. while (cur) {
  300. if (!strcmp(cur->key, key)) {
  301. *data = cur->data;
  302. break;
  303. }
  304. cur = cur->next;
  305. }
  306. return APR_SUCCESS;
  307. }
  308. apr_status_t apr_socket_data_set(apr_socket_t *sock, void *data, const char *key,
  309. apr_status_t (*cleanup) (void *))
  310. {
  311. sock_userdata_t *new = apr_palloc(sock->pool, sizeof(sock_userdata_t));
  312. new->key = apr_pstrdup(sock->pool, key);
  313. new->data = data;
  314. new->next = sock->userdata;
  315. sock->userdata = new;
  316. if (cleanup) {
  317. apr_pool_cleanup_register(sock->pool, data, cleanup, cleanup);
  318. }
  319. return APR_SUCCESS;
  320. }
  321. apr_status_t apr_os_sock_get(apr_os_sock_t *thesock, apr_socket_t *sock)
  322. {
  323. *thesock = sock->socketdes;
  324. return APR_SUCCESS;
  325. }
  326. apr_status_t apr_os_sock_make(apr_socket_t **apr_sock,
  327. apr_os_sock_info_t *os_sock_info,
  328. apr_pool_t *cont)
  329. {
  330. alloc_socket(apr_sock, cont);
  331. set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol);
  332. (*apr_sock)->timeout = -1;
  333. (*apr_sock)->socketdes = *os_sock_info->os_sock;
  334. if (os_sock_info->local) {
  335. memcpy(&(*apr_sock)->local_addr->sa.sin,
  336. os_sock_info->local,
  337. (*apr_sock)->local_addr->salen);
  338. /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
  339. (*apr_sock)->local_addr->port = ntohs((*apr_sock)->local_addr->sa.sin.sin_port);
  340. }
  341. else {
  342. (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1;
  343. }
  344. if (os_sock_info->remote) {
  345. #ifndef HAVE_POLL
  346. (*apr_sock)->connected = 1;
  347. #endif
  348. memcpy(&(*apr_sock)->remote_addr->sa.sin,
  349. os_sock_info->remote,
  350. (*apr_sock)->remote_addr->salen);
  351. /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
  352. (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port);
  353. }
  354. else {
  355. (*apr_sock)->remote_addr_unknown = 1;
  356. }
  357. (*apr_sock)->inherit = 0;
  358. apr_pool_cleanup_register((*apr_sock)->pool, (void *)(*apr_sock),
  359. socket_cleanup, socket_cleanup);
  360. return APR_SUCCESS;
  361. }
  362. apr_status_t apr_os_sock_put(apr_socket_t **sock, apr_os_sock_t *thesock,
  363. apr_pool_t *cont)
  364. {
  365. /* XXX Bogus assumption that *sock points at anything legit */
  366. if ((*sock) == NULL) {
  367. alloc_socket(sock, cont);
  368. /* XXX IPv6 figure out the family here! */
  369. /* XXX figure out the actual socket type here */
  370. /* *or* just decide that apr_os_sock_put() has to be told the family and type */
  371. set_socket_vars(*sock, APR_INET, SOCK_STREAM, 0);
  372. (*sock)->timeout = -1;
  373. }
  374. (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
  375. (*sock)->remote_addr_unknown = 1;
  376. (*sock)->socketdes = *thesock;
  377. return APR_SUCCESS;
  378. }
  379. APR_POOL_IMPLEMENT_ACCESSOR(socket)
  380. APR_IMPLEMENT_INHERIT_SET(socket, inherit, pool, socket_cleanup)
  381. APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, pool, socket_cleanup)