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 "fspr_arch_networkio.h"
  17. #include "fspr_network_io.h"
  18. #include "fspr_strings.h"
  19. #include "fspr_support.h"
  20. #include "fspr_portable.h"
  21. #include "fspr_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 fspr_status_t socket_cleanup(void *sock)
  28. {
  29. fspr_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(fspr_socket_t *sock, int family, int type, int protocol)
  45. {
  46. sock->type = type;
  47. sock->protocol = protocol;
  48. fspr_sockaddr_vars_set(sock->local_addr, family, 0);
  49. fspr_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(fspr_socket_t **new, fspr_pool_t *p)
  59. {
  60. *new = (fspr_socket_t *)fspr_pcalloc(p, sizeof(fspr_socket_t));
  61. (*new)->pool = p;
  62. (*new)->local_addr = (fspr_sockaddr_t *)fspr_pcalloc((*new)->pool,
  63. sizeof(fspr_sockaddr_t));
  64. (*new)->local_addr->pool = p;
  65. (*new)->remote_addr = (fspr_sockaddr_t *)fspr_pcalloc((*new)->pool,
  66. sizeof(fspr_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) fspr_pollset_create(&(*new)->pollset, 1, p, 0);
  73. #endif
  74. }
  75. fspr_status_t fspr_socket_protocol_get(fspr_socket_t *sock, int *protocol)
  76. {
  77. *protocol = sock->protocol;
  78. return APR_SUCCESS;
  79. }
  80. fspr_status_t fspr_socket_create(fspr_socket_t **new, int ofamily, int type,
  81. int protocol, fspr_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. fspr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
  127. socket_cleanup);
  128. return APR_SUCCESS;
  129. }
  130. fspr_status_t fspr_socket_shutdown(fspr_socket_t *thesocket,
  131. fspr_shutdown_how_e how)
  132. {
  133. return (shutdown(thesocket->socketdes, how) == -1) ? errno : APR_SUCCESS;
  134. }
  135. fspr_status_t fspr_socket_close(fspr_socket_t *thesocket)
  136. {
  137. return fspr_pool_cleanup_run(thesocket->pool, thesocket, socket_cleanup);
  138. }
  139. fspr_status_t fspr_socket_bind(fspr_socket_t *sock, fspr_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. fspr_status_t fspr_socket_listen(fspr_socket_t *sock, fspr_int32_t backlog)
  155. {
  156. if (listen(sock->socketdes, backlog) == -1)
  157. return errno;
  158. else
  159. return APR_SUCCESS;
  160. }
  161. fspr_status_t fspr_socket_accept(fspr_socket_t **new, fspr_socket_t *sock,
  162. fspr_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 (fspr_is_option_set(sock, APR_TCP_NODELAY) == 1) {
  205. fspr_set_option(*new, APR_TCP_NODELAY, 1);
  206. }
  207. #endif /* TCP_NODELAY_INHERITED */
  208. #if APR_O_NONBLOCK_INHERITED
  209. if (fspr_is_option_set(sock, APR_SO_NONBLOCK) == 1) {
  210. fspr_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. fspr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
  227. socket_cleanup);
  228. return APR_SUCCESS;
  229. }
  230. fspr_status_t fspr_socket_connect(fspr_socket_t *sock, fspr_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 = fspr_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. fspr_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. fspr_status_t fspr_socket_type_get(fspr_socket_t *sock, int *type)
  291. {
  292. *type = sock->type;
  293. return APR_SUCCESS;
  294. }
  295. fspr_status_t fspr_socket_data_get(void **data, const char *key, fspr_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. fspr_status_t fspr_socket_data_set(fspr_socket_t *sock, void *data, const char *key,
  309. fspr_status_t (*cleanup) (void *))
  310. {
  311. sock_userdata_t *new = fspr_palloc(sock->pool, sizeof(sock_userdata_t));
  312. new->key = fspr_pstrdup(sock->pool, key);
  313. new->data = data;
  314. new->next = sock->userdata;
  315. sock->userdata = new;
  316. if (cleanup) {
  317. fspr_pool_cleanup_register(sock->pool, data, cleanup, cleanup);
  318. }
  319. return APR_SUCCESS;
  320. }
  321. fspr_status_t fspr_os_sock_get(fspr_os_sock_t *thesock, fspr_socket_t *sock)
  322. {
  323. *thesock = sock->socketdes;
  324. return APR_SUCCESS;
  325. }
  326. fspr_status_t fspr_os_sock_make(fspr_socket_t **fspr_sock,
  327. fspr_os_sock_info_t *os_sock_info,
  328. fspr_pool_t *cont)
  329. {
  330. alloc_socket(fspr_sock, cont);
  331. set_socket_vars(*fspr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol);
  332. (*fspr_sock)->timeout = -1;
  333. (*fspr_sock)->socketdes = *os_sock_info->os_sock;
  334. if (os_sock_info->local) {
  335. memcpy(&(*fspr_sock)->local_addr->sa.sin,
  336. os_sock_info->local,
  337. (*fspr_sock)->local_addr->salen);
  338. /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
  339. (*fspr_sock)->local_addr->port = ntohs((*fspr_sock)->local_addr->sa.sin.sin_port);
  340. }
  341. else {
  342. (*fspr_sock)->local_port_unknown = (*fspr_sock)->local_interface_unknown = 1;
  343. }
  344. if (os_sock_info->remote) {
  345. #ifndef HAVE_POLL
  346. (*fspr_sock)->connected = 1;
  347. #endif
  348. memcpy(&(*fspr_sock)->remote_addr->sa.sin,
  349. os_sock_info->remote,
  350. (*fspr_sock)->remote_addr->salen);
  351. /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
  352. (*fspr_sock)->remote_addr->port = ntohs((*fspr_sock)->remote_addr->sa.sin.sin_port);
  353. }
  354. else {
  355. (*fspr_sock)->remote_addr_unknown = 1;
  356. }
  357. (*fspr_sock)->inherit = 0;
  358. fspr_pool_cleanup_register((*fspr_sock)->pool, (void *)(*fspr_sock),
  359. socket_cleanup, socket_cleanup);
  360. return APR_SUCCESS;
  361. }
  362. fspr_status_t fspr_os_sock_put(fspr_socket_t **sock, fspr_os_sock_t *thesock,
  363. fspr_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 fspr_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)