/tags/wired-1.3.2/libwired/libwired/net/wi-socket.c

https://bitbucket.org/balrog/zanka-full · C · 1519 lines · 986 code · 507 blank · 26 comment · 186 complexity · 9db1ffb90d08629ee0c580064a87ab95 MD5 · raw file

  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2003-2007 Axel Andersson
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #include <sys/param.h>
  29. #include <sys/types.h>
  30. #include <sys/time.h>
  31. #include <sys/socket.h>
  32. #include <netinet/in.h>
  33. #include <netinet/in_systm.h>
  34. #include <netinet/ip.h>
  35. #include <netinet/tcp.h>
  36. #include <netdb.h>
  37. #include <net/if.h>
  38. #include <fcntl.h>
  39. #include <unistd.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #ifdef WI_SSL
  44. #include <openssl/err.h>
  45. #include <openssl/ssl.h>
  46. #endif
  47. #ifdef HAVE_IFADDRS_H
  48. #include <ifaddrs.h>
  49. #endif
  50. #include <wired/wi-array.h>
  51. #include <wired/wi-assert.h>
  52. #include <wired/wi-address.h>
  53. #include <wired/wi-date.h>
  54. #include <wired/wi-macros.h>
  55. #include <wired/wi-lock.h>
  56. #include <wired/wi-private.h>
  57. #include <wired/wi-socket.h>
  58. #include <wired/wi-string.h>
  59. #include <wired/wi-system.h>
  60. #include <wired/wi-thread.h>
  61. #define _WI_SOCKET_BUFFER_MAX_SIZE 131072
  62. struct _wi_socket_context {
  63. wi_runtime_base_t base;
  64. #ifdef WI_SSL
  65. SSL_CTX *ssl_ctx;
  66. DH *dh;
  67. RSA *pub_rsa;
  68. RSA *priv_rsa;
  69. wi_boolean_t certificate;
  70. #endif
  71. };
  72. struct _wi_socket {
  73. wi_runtime_base_t base;
  74. wi_address_t *address;
  75. wi_socket_type_t type;
  76. wi_uinteger_t direction;
  77. int sd;
  78. #ifdef WI_SSL
  79. SSL *ssl;
  80. #endif
  81. void *data;
  82. wi_string_t *buffer;
  83. wi_boolean_t interactive;
  84. wi_boolean_t close;
  85. wi_boolean_t broken;
  86. };
  87. #if defined(WI_SSL) && defined(WI_PTHREADS)
  88. static unsigned long _wi_socket_ssl_id_function(void);
  89. static void _wi_socket_ssl_locking_function(int, int, const char *, int);
  90. #endif
  91. static void _wi_socket_context_dealloc(wi_runtime_instance_t *);
  92. static void _wi_socket_dealloc(wi_runtime_instance_t *);
  93. static wi_string_t * _wi_socket_description(wi_runtime_instance_t *);
  94. static wi_boolean_t _wi_socket_set_option(wi_socket_t *, int, int, int);
  95. static wi_boolean_t _wi_socket_get_option(wi_socket_t *, int, int, int *);
  96. #if defined(WI_SSL) && defined(WI_PTHREADS)
  97. static wi_array_t *_wi_socket_ssl_locks;
  98. #endif
  99. static wi_runtime_id_t _wi_socket_context_runtime_id = WI_RUNTIME_ID_NULL;
  100. static wi_runtime_class_t _wi_socket_context_runtime_class = {
  101. "wi_socket_context_t",
  102. _wi_socket_context_dealloc,
  103. NULL,
  104. NULL,
  105. NULL,
  106. NULL
  107. };
  108. static wi_runtime_id_t _wi_socket_runtime_id = WI_RUNTIME_ID_NULL;
  109. static wi_runtime_class_t _wi_socket_runtime_class = {
  110. "wi_socket_t",
  111. _wi_socket_dealloc,
  112. NULL,
  113. NULL,
  114. _wi_socket_description,
  115. NULL
  116. };
  117. void wi_socket_register(void) {
  118. _wi_socket_context_runtime_id = wi_runtime_register_class(&_wi_socket_context_runtime_class);
  119. _wi_socket_runtime_id = wi_runtime_register_class(&_wi_socket_runtime_class);
  120. }
  121. void wi_socket_initialize(void) {
  122. #ifdef WI_SSL
  123. #ifdef WI_PTHREADS
  124. wi_lock_t *lock;
  125. wi_uinteger_t i, count;
  126. #endif
  127. SSL_library_init();
  128. #ifdef WI_PTHREADS
  129. count = CRYPTO_num_locks();
  130. _wi_socket_ssl_locks = wi_array_init_with_capacity(wi_array_alloc(), count);
  131. for(i = 0; i < count; i++) {
  132. lock = wi_lock_init(wi_lock_alloc());
  133. wi_array_add_data(_wi_socket_ssl_locks, lock);
  134. wi_release(lock);
  135. }
  136. CRYPTO_set_id_callback(_wi_socket_ssl_id_function);
  137. CRYPTO_set_locking_callback(_wi_socket_ssl_locking_function);
  138. #endif
  139. #endif
  140. }
  141. #pragma mark -
  142. #if defined(WI_SSL) && defined(WI_PTHREADS)
  143. static unsigned long _wi_socket_ssl_id_function(void) {
  144. return ((unsigned long) wi_thread_current_thread());
  145. }
  146. static void _wi_socket_ssl_locking_function(int mode, int n, const char *file, int line) {
  147. wi_lock_t *lock;
  148. lock = WI_ARRAY(_wi_socket_ssl_locks, n);
  149. if(mode & CRYPTO_LOCK)
  150. wi_lock_lock(lock);
  151. else
  152. wi_lock_unlock(lock);
  153. }
  154. #endif
  155. #pragma mark -
  156. void wi_socket_exit_thread(void) {
  157. #ifdef WI_SSL
  158. ERR_remove_state(0);
  159. #endif
  160. }
  161. #pragma mark -
  162. wi_runtime_id_t wi_socket_context_runtime_id(void) {
  163. return _wi_socket_context_runtime_id;
  164. }
  165. #pragma mark -
  166. wi_socket_context_t * wi_socket_context_alloc(void) {
  167. return wi_runtime_create_instance(_wi_socket_context_runtime_id, sizeof(wi_socket_context_t));
  168. }
  169. wi_socket_context_t * wi_socket_context_init(wi_socket_context_t *context) {
  170. return context;
  171. }
  172. static void _wi_socket_context_dealloc(wi_runtime_instance_t *instance) {
  173. #ifdef WI_SSL
  174. wi_socket_context_t *context = instance;
  175. if(context->ssl_ctx)
  176. SSL_CTX_free(context->ssl_ctx);
  177. if(context->dh)
  178. DH_free(context->dh);
  179. #endif
  180. }
  181. #pragma mark -
  182. wi_boolean_t wi_socket_context_set_ssl_type(wi_socket_context_t *context, wi_socket_ssl_type_t type) {
  183. #ifdef WI_SSL
  184. SSL_METHOD *method = NULL;
  185. switch(type) {
  186. case WI_SOCKET_SSL_CLIENT:
  187. method = TLSv1_client_method();
  188. break;
  189. case WI_SOCKET_SSL_SERVER:
  190. method = TLSv1_server_method();
  191. break;
  192. }
  193. context->ssl_ctx = SSL_CTX_new(method);
  194. if(!context->ssl_ctx) {
  195. wi_error_set_openssl_error();
  196. return false;
  197. }
  198. SSL_CTX_set_mode(context->ssl_ctx, SSL_MODE_AUTO_RETRY);
  199. SSL_CTX_set_quiet_shutdown(context->ssl_ctx, 1);
  200. return true;
  201. #else
  202. wi_error_set_libwired_error(WI_ERROR_SOCKET_NOSSL);
  203. return false;
  204. #endif
  205. }
  206. wi_boolean_t wi_socket_context_set_ssl_certificate(wi_socket_context_t *context, wi_string_t *path) {
  207. #ifdef WI_SSL
  208. const char *certificate;
  209. context->certificate = false;
  210. certificate = wi_string_cstring(path);
  211. if(SSL_CTX_use_certificate_chain_file(context->ssl_ctx, certificate) != 1) {
  212. wi_error_set_openssl_error();
  213. return false;
  214. }
  215. if(SSL_CTX_use_PrivateKey_file(context->ssl_ctx, certificate, SSL_FILETYPE_PEM) != 1) {
  216. wi_error_set_openssl_error();
  217. return false;
  218. }
  219. context->certificate = true;
  220. return true;
  221. #else
  222. wi_error_set_libwired_error(WI_ERROR_SOCKET_NOSSL);
  223. return false;
  224. #endif
  225. }
  226. wi_boolean_t wi_socket_context_set_ssl_privkey(wi_socket_context_t *context, wi_string_t *path) {
  227. #ifdef WI_SSL
  228. FILE *fp;
  229. fp = fopen(wi_string_cstring(path), "r");
  230. if(!fp) {
  231. wi_error_set_errno(errno);
  232. return false;
  233. }
  234. context->priv_rsa = PEM_read_RSAPrivateKey(fp, NULL, 0, NULL);
  235. if(!context->priv_rsa)
  236. wi_error_set_openssl_error();
  237. fclose(fp);
  238. return (context->priv_rsa != NULL);
  239. #else
  240. wi_error_set_libwired_error(WI_ERROR_SOCKET_NOSSL);
  241. return false;
  242. #endif
  243. }
  244. void wi_socket_context_set_ssl_pubkey(wi_socket_context_t *context, void *rsa) {
  245. #ifdef WI_SSL
  246. if(context->pub_rsa != rsa) {
  247. if(context->pub_rsa)
  248. RSA_free(context->pub_rsa);
  249. context->pub_rsa = rsa;
  250. }
  251. #endif
  252. }
  253. wi_boolean_t wi_socket_context_set_ssl_ciphers(wi_socket_context_t *context, wi_string_t *ciphers) {
  254. #ifdef WI_SSL
  255. if(SSL_CTX_set_cipher_list(context->ssl_ctx, wi_string_cstring(ciphers)) != 1) {
  256. wi_error_set_libwired_error(WI_ERROR_SOCKET_NOVALIDCIPHER);
  257. return false;
  258. }
  259. return true;
  260. #else
  261. wi_error_set_libwired_error(WI_ERROR_SOCKET_NOSSL);
  262. return false;
  263. #endif
  264. }
  265. wi_boolean_t wi_socket_context_set_ssl_dh(wi_socket_context_t *context, const unsigned char *p, size_t p_size, const unsigned char *g, size_t g_size) {
  266. #ifdef WI_SSL
  267. context->dh = DH_new();
  268. if(!context->dh) {
  269. wi_error_set_openssl_error();
  270. return false;
  271. }
  272. context->dh->p = BN_bin2bn(p, p_size, NULL);
  273. context->dh->g = BN_bin2bn(g, g_size, NULL);
  274. if(!context->dh->p || !context->dh->g) {
  275. wi_error_set_openssl_error();
  276. DH_free(context->dh);
  277. context->dh = NULL;
  278. return false;
  279. }
  280. return true;
  281. #else
  282. wi_error_set_libwired_error(WI_ERROR_SOCKET_NOSSL);
  283. return false;
  284. #endif
  285. }
  286. #pragma mark -
  287. wi_runtime_id_t wi_socket_runtime_id(void) {
  288. return _wi_socket_runtime_id;
  289. }
  290. #pragma mark -
  291. wi_socket_t * wi_socket_alloc(void) {
  292. return wi_runtime_create_instance(_wi_socket_runtime_id, sizeof(wi_socket_t));
  293. }
  294. wi_socket_t * wi_socket_init_with_address(wi_socket_t *_socket, wi_address_t *address, wi_socket_type_t type) {
  295. _socket->address = wi_copy(address);
  296. _socket->close = true;
  297. _socket->buffer = wi_string_init_with_capacity(wi_string_alloc(), WI_SOCKET_BUFFER_SIZE);
  298. _socket->type = type;
  299. _socket->sd = socket(wi_address_family(_socket->address), _socket->type, 0);
  300. if(_socket->sd < 0) {
  301. wi_error_set_errno(errno);
  302. wi_release(_socket);
  303. return NULL;
  304. }
  305. if(!_wi_socket_set_option(_socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
  306. wi_release(_socket);
  307. return NULL;
  308. }
  309. return _socket;
  310. }
  311. wi_socket_t * wi_socket_init_with_descriptor(wi_socket_t *socket, int sd) {
  312. socket->sd = sd;
  313. socket->buffer = wi_string_init_with_capacity(wi_string_alloc(), WI_SOCKET_BUFFER_SIZE);
  314. return socket;
  315. }
  316. static void _wi_socket_dealloc(wi_runtime_instance_t *instance) {
  317. wi_socket_t *socket = instance;
  318. wi_socket_close(socket);
  319. wi_release(socket->address);
  320. wi_release(socket->buffer);
  321. }
  322. static wi_string_t * _wi_socket_description(wi_runtime_instance_t *instance) {
  323. wi_socket_t *socket = instance;
  324. return wi_string_with_format(WI_STR("<%@ %p>{sd = %d, address = %@}"),
  325. wi_runtime_class_name(socket),
  326. socket,
  327. socket->sd,
  328. socket->address);
  329. }
  330. #pragma mark -
  331. static wi_boolean_t _wi_socket_set_option(wi_socket_t *socket, int level, int name, int option) {
  332. if(setsockopt(socket->sd, level, name, &option, sizeof(option)) < 0) {
  333. wi_error_set_errno(errno);
  334. return false;
  335. }
  336. return true;
  337. }
  338. static wi_boolean_t _wi_socket_get_option(wi_socket_t *socket, int level, int name, int *option) {
  339. socklen_t length;
  340. length = sizeof(*option);
  341. if(getsockopt(socket->sd, level, name, option, &length) < 0) {
  342. wi_error_set_errno(errno);
  343. *option = 0;
  344. return false;
  345. }
  346. return true;
  347. }
  348. #pragma mark -
  349. wi_address_t * wi_socket_address(wi_socket_t *socket) {
  350. return socket->address;
  351. }
  352. int wi_socket_descriptor(wi_socket_t *socket) {
  353. return socket->sd;
  354. }
  355. void * wi_socket_ssl(wi_socket_t *socket) {
  356. #ifdef WI_SSL
  357. return socket->ssl;
  358. #else
  359. return NULL;
  360. #endif
  361. }
  362. void * wi_socket_ssl_pubkey(wi_socket_t *socket) {
  363. #ifdef WI_SSL
  364. RSA *rsa = NULL;
  365. X509 *x509 = NULL;
  366. EVP_PKEY *pkey = NULL;
  367. x509 = SSL_get_peer_certificate(socket->ssl);
  368. if(!x509) {
  369. wi_error_set_openssl_error();
  370. goto end;
  371. }
  372. pkey = X509_get_pubkey(x509);
  373. if(!pkey) {
  374. wi_error_set_openssl_error();
  375. goto end;
  376. }
  377. rsa = EVP_PKEY_get1_RSA(pkey);
  378. if(!rsa)
  379. wi_error_set_openssl_error();
  380. end:
  381. if(x509)
  382. X509_free(x509);
  383. if(pkey)
  384. EVP_PKEY_free(pkey);
  385. return rsa;
  386. #else
  387. return NULL;
  388. #endif
  389. }
  390. wi_string_t * wi_socket_cipher_version(wi_socket_t *socket) {
  391. #ifdef WI_SSL
  392. return wi_string_with_cstring(SSL_get_cipher_version(socket->ssl));
  393. #else
  394. return NULL;
  395. #endif
  396. }
  397. wi_string_t * wi_socket_cipher_name(wi_socket_t *socket) {
  398. #ifdef WI_SSL
  399. return wi_string_with_cstring(SSL_get_cipher_name(socket->ssl));
  400. #else
  401. return NULL;
  402. #endif
  403. }
  404. wi_uinteger_t wi_socket_cipher_bits(wi_socket_t *socket) {
  405. #ifdef WI_SSL
  406. return SSL_get_cipher_bits(socket->ssl, NULL);
  407. #else
  408. return 0;
  409. #endif
  410. }
  411. wi_string_t * wi_socket_certificate_name(wi_socket_t *socket) {
  412. #ifdef WI_SSL
  413. X509 *x509 = NULL;
  414. EVP_PKEY *pkey = NULL;
  415. wi_string_t *string = NULL;
  416. x509 = SSL_get_peer_certificate(socket->ssl);
  417. if(!x509)
  418. goto end;
  419. pkey = X509_get_pubkey(x509);
  420. if(!pkey)
  421. goto end;
  422. switch(EVP_PKEY_type(pkey->type)) {
  423. case EVP_PKEY_RSA:
  424. string = wi_string_init_with_cstring(wi_string_alloc(), "RSA");
  425. break;
  426. case EVP_PKEY_DSA:
  427. string = wi_string_init_with_cstring(wi_string_alloc(), "DSA");
  428. break;
  429. case EVP_PKEY_DH:
  430. string = wi_string_init_with_cstring(wi_string_alloc(), "DH");
  431. break;
  432. default:
  433. break;
  434. }
  435. end:
  436. if(x509)
  437. X509_free(x509);
  438. if(pkey)
  439. EVP_PKEY_free(pkey);
  440. return wi_autorelease(string);
  441. #else
  442. return NULL;
  443. #endif
  444. }
  445. wi_uinteger_t wi_socket_certificate_bits(wi_socket_t *socket) {
  446. #ifdef WI_SSL
  447. X509 *x509 = NULL;
  448. EVP_PKEY *pkey = NULL;
  449. wi_uinteger_t bits = 0;
  450. x509 = SSL_get_peer_certificate(socket->ssl);
  451. if(!x509)
  452. goto end;
  453. pkey = X509_get_pubkey(x509);
  454. if(!pkey)
  455. goto end;
  456. bits = 8 * EVP_PKEY_size(pkey);
  457. end:
  458. if(x509)
  459. X509_free(x509);
  460. if(pkey)
  461. EVP_PKEY_free(pkey);
  462. return bits;
  463. #else
  464. return 0;
  465. #endif
  466. }
  467. wi_string_t * wi_socket_certificate_hostname(wi_socket_t *socket) {
  468. #ifdef WI_SSL
  469. X509 *x509;
  470. wi_string_t *string;
  471. char hostname[MAXHOSTNAMELEN];
  472. x509 = SSL_get_peer_certificate(socket->ssl);
  473. if(!x509)
  474. return NULL;
  475. X509_NAME_get_text_by_NID(X509_get_subject_name(x509),
  476. NID_commonName,
  477. hostname,
  478. sizeof(hostname));
  479. string = wi_string_init_with_cstring(wi_string_alloc(), hostname);
  480. X509_free(x509);
  481. return wi_autorelease(string);
  482. #else
  483. return NULL;
  484. #endif
  485. }
  486. #pragma mark -
  487. void wi_socket_set_port(wi_socket_t *socket, wi_uinteger_t port) {
  488. wi_address_set_port(socket->address, port);
  489. }
  490. wi_uinteger_t wi_socket_port(wi_socket_t *socket) {
  491. return wi_address_port(socket->address);
  492. }
  493. void wi_socket_set_direction(wi_socket_t *socket, wi_uinteger_t direction) {
  494. socket->direction = direction;
  495. }
  496. wi_uinteger_t wi_socket_direction(wi_socket_t *socket) {
  497. return socket->direction;
  498. }
  499. void wi_socket_set_data(wi_socket_t *socket, void *data) {
  500. socket->data = data;
  501. }
  502. void * wi_socket_data(wi_socket_t *socket) {
  503. return socket->data;
  504. }
  505. void wi_socket_set_blocking(wi_socket_t *socket, wi_boolean_t blocking) {
  506. int flags;
  507. flags = fcntl(socket->sd, F_GETFL);
  508. if(flags < 0) {
  509. wi_error_set_errno(errno);
  510. return;
  511. }
  512. if(blocking)
  513. flags &= ~O_NONBLOCK;
  514. else
  515. flags |= O_NONBLOCK;
  516. if(fcntl(socket->sd, F_SETFL, flags) < 0) {
  517. wi_error_set_errno(errno);
  518. return;
  519. }
  520. }
  521. wi_boolean_t wi_socket_blocking(wi_socket_t *socket) {
  522. int flags;
  523. flags = fcntl(socket->sd, F_GETFL);
  524. if(flags < 0) {
  525. wi_error_set_errno(errno);
  526. return false;
  527. }
  528. return !(flags & O_NONBLOCK);
  529. }
  530. void wi_socket_set_interactive(wi_socket_t *socket, wi_boolean_t interactive) {
  531. if(socket->type == WI_SOCKET_TCP && interactive)
  532. _wi_socket_set_option(socket, IPPROTO_TCP, TCP_NODELAY, 1);
  533. if(wi_address_family(socket->address) == WI_ADDRESS_IPV4)
  534. _wi_socket_set_option(socket, IPPROTO_IP, IP_TOS, interactive ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT);
  535. socket->interactive = interactive;
  536. }
  537. wi_boolean_t wi_socket_interactive(wi_socket_t *socket) {
  538. return socket->interactive;
  539. }
  540. int wi_socket_error(wi_socket_t *socket) {
  541. int error;
  542. WI_ASSERT(socket->type == WI_SOCKET_TCP, "%@ is not a TCP socket", socket);
  543. if(!_wi_socket_get_option(socket, SOL_SOCKET, SO_ERROR, &error))
  544. return errno;
  545. return error;
  546. }
  547. #pragma mark -
  548. wi_socket_t * wi_socket_wait_multiple(wi_array_t *array, wi_time_interval_t timeout) {
  549. wi_enumerator_t *enumerator;
  550. wi_socket_t *socket, *accept_socket = NULL;
  551. struct timeval tv;
  552. fd_set rfds, wfds;
  553. int state, max_sd;
  554. tv = wi_dtotv(timeout);
  555. max_sd = -1;
  556. FD_ZERO(&rfds);
  557. FD_ZERO(&wfds);
  558. wi_array_rdlock(array);
  559. enumerator = wi_array_data_enumerator(array);
  560. while((socket = wi_enumerator_next_data(enumerator))) {
  561. if(wi_string_length(socket->buffer) > 0) {
  562. accept_socket = socket;
  563. break;
  564. }
  565. if(socket->direction & WI_SOCKET_READ)
  566. FD_SET(socket->sd, &rfds);
  567. if(socket->direction & WI_SOCKET_WRITE)
  568. FD_SET(socket->sd, &wfds);
  569. if(socket->sd > max_sd)
  570. max_sd = socket->sd;
  571. }
  572. wi_array_unlock(array);
  573. if(accept_socket)
  574. return accept_socket;
  575. state = select(max_sd + 1, &rfds, &wfds, NULL, (timeout > 0.0) ? &tv : NULL);
  576. if(state < 0) {
  577. wi_error_set_errno(errno);
  578. return NULL;
  579. }
  580. wi_array_rdlock(array);
  581. enumerator = wi_array_data_enumerator(array);
  582. while((socket = wi_enumerator_next_data(enumerator))) {
  583. if(FD_ISSET(socket->sd, &rfds) || FD_ISSET(socket->sd, &wfds)) {
  584. accept_socket = socket;
  585. break;
  586. }
  587. }
  588. wi_array_unlock(array);
  589. return accept_socket;
  590. }
  591. wi_socket_state_t wi_socket_wait(wi_socket_t *socket, wi_time_interval_t timeout) {
  592. if(wi_string_length(socket->buffer) > 0)
  593. return WI_SOCKET_READY;
  594. return wi_socket_wait_descriptor(socket->sd,
  595. timeout,
  596. (socket->direction & WI_SOCKET_READ),
  597. (socket->direction & WI_SOCKET_WRITE));
  598. }
  599. wi_socket_state_t wi_socket_wait_descriptor(int sd, wi_time_interval_t timeout, wi_boolean_t read, wi_boolean_t write) {
  600. struct timeval tv;
  601. fd_set rfds, wfds;
  602. int state;
  603. tv = wi_dtotv(timeout);
  604. FD_ZERO(&rfds);
  605. FD_ZERO(&wfds);
  606. if(read)
  607. FD_SET(sd, &rfds);
  608. if(write)
  609. FD_SET(sd, &wfds);
  610. state = select(sd + 1, &rfds, &wfds, NULL, (timeout > 0.0) ? &tv : NULL);
  611. if(state < 0) {
  612. wi_error_set_errno(errno);
  613. return WI_SOCKET_ERROR;
  614. }
  615. else if(state == 0) {
  616. wi_error_set_errno(ETIMEDOUT);
  617. return WI_SOCKET_TIMEOUT;
  618. }
  619. return WI_SOCKET_READY;
  620. }
  621. #pragma mark -
  622. wi_boolean_t wi_socket_listen(wi_socket_t *_socket, wi_uinteger_t backlog) {
  623. struct sockaddr *sa;
  624. wi_uinteger_t length;
  625. sa = wi_address_sa(_socket->address);
  626. length = wi_address_sa_length(_socket->address);
  627. if(bind(_socket->sd, sa, length) < 0) {
  628. wi_error_set_errno(errno);
  629. return false;
  630. }
  631. if(_socket->type == WI_SOCKET_TCP) {
  632. if(listen(_socket->sd, backlog) < 0) {
  633. wi_error_set_errno(errno);
  634. return false;
  635. }
  636. }
  637. _socket->direction = WI_SOCKET_READ;
  638. return true;
  639. }
  640. wi_boolean_t wi_socket_connect(wi_socket_t *socket, wi_socket_context_t *context, wi_time_interval_t timeout) {
  641. struct sockaddr *sa;
  642. wi_socket_state_t state;
  643. wi_uinteger_t length;
  644. int err;
  645. #ifdef WI_SSL
  646. int ret;
  647. #endif
  648. wi_boolean_t blocking;
  649. blocking = wi_socket_blocking(socket);
  650. if(blocking)
  651. wi_socket_set_blocking(socket, false);
  652. sa = wi_address_sa(socket->address);
  653. length = wi_address_sa_length(socket->address);
  654. err = connect(socket->sd, sa, length);
  655. if(err < 0) {
  656. if(errno != EINPROGRESS) {
  657. wi_error_set_errno(errno);
  658. return false;
  659. }
  660. do {
  661. state = wi_socket_wait_descriptor(socket->sd, 1.0, true, true);
  662. timeout -= 1.0;
  663. } while(state == WI_SOCKET_TIMEOUT && timeout >= 0.0);
  664. if(state == WI_SOCKET_ERROR)
  665. return false;
  666. if(timeout <= 0.0) {
  667. wi_error_set_errno(ETIMEDOUT);
  668. return false;
  669. }
  670. err = wi_socket_error(socket);
  671. if(err != 0) {
  672. wi_error_set_errno(err);
  673. return false;
  674. }
  675. }
  676. #ifdef WI_SSL
  677. if(context && context->ssl_ctx) {
  678. socket->ssl = SSL_new(context->ssl_ctx);
  679. if(!socket->ssl) {
  680. wi_error_set_openssl_error();
  681. return false;
  682. }
  683. if(SSL_set_fd(socket->ssl, socket->sd) != 1) {
  684. wi_error_set_openssl_error();
  685. return false;
  686. }
  687. ret = SSL_connect(socket->ssl);
  688. if(ret != 1) {
  689. do {
  690. err = SSL_get_error(socket->ssl, ret);
  691. if(err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) {
  692. wi_error_set_openssl_error();
  693. return false;
  694. }
  695. state = wi_socket_wait_descriptor(socket->sd, 1.0, (err == SSL_ERROR_WANT_READ), (err == SSL_ERROR_WANT_WRITE));
  696. if(state == WI_SOCKET_ERROR)
  697. break;
  698. else if(state == WI_SOCKET_READY) {
  699. ret = SSL_connect(socket->ssl);
  700. if(ret == 1)
  701. break;
  702. }
  703. timeout -= 1.0;
  704. } while(timeout >= 0.0);
  705. if(state == WI_SOCKET_ERROR)
  706. return false;
  707. if(timeout <= 0.0) {
  708. wi_error_set_errno(ETIMEDOUT);
  709. return false;
  710. }
  711. }
  712. }
  713. #endif
  714. if(blocking)
  715. wi_socket_set_blocking(socket, true);
  716. socket->direction = WI_SOCKET_READ;
  717. return true;
  718. }
  719. wi_socket_t * wi_socket_accept_multiple(wi_array_t *array, wi_socket_context_t *context, wi_time_interval_t timeout, wi_address_t **address) {
  720. wi_socket_t *socket;
  721. *address = NULL;
  722. socket = wi_socket_wait_multiple(array, 0.0);
  723. if(!socket)
  724. return NULL;
  725. return wi_socket_accept(socket, context, timeout, address);
  726. }
  727. wi_socket_t * wi_socket_accept(wi_socket_t *accept_socket, wi_socket_context_t *context, wi_time_interval_t timeout, wi_address_t **address) {
  728. wi_socket_t *socket;
  729. #ifdef WI_SSL
  730. SSL *ssl = NULL;
  731. #endif
  732. struct sockaddr_storage ss;
  733. socklen_t length;
  734. int sd;
  735. length = sizeof(ss);
  736. sd = accept(accept_socket->sd, (struct sockaddr *) &ss, &length);
  737. *address = (length > 0) ? wi_autorelease(wi_address_init_with_sa(wi_address_alloc(), (struct sockaddr *) &ss)) : NULL;
  738. if(sd < 0) {
  739. wi_error_set_errno(errno);
  740. goto err;
  741. }
  742. #ifdef WI_SSL
  743. if(context && context->ssl_ctx) {
  744. ssl = SSL_new(context->ssl_ctx);
  745. if(!ssl) {
  746. wi_error_set_openssl_error();
  747. goto err;
  748. }
  749. if(SSL_set_fd(ssl, sd) != 1) {
  750. wi_error_set_openssl_error();
  751. goto err;
  752. }
  753. if(!context->certificate && context->dh) {
  754. if(SSL_set_tmp_dh(ssl, context->dh) != 1) {
  755. wi_error_set_openssl_error();
  756. goto err;
  757. }
  758. }
  759. if(timeout > 0.0) {
  760. if(wi_socket_wait_descriptor(sd, timeout, true, false) != WI_SOCKET_READY)
  761. goto err;
  762. }
  763. if(SSL_accept(ssl) != 1) {
  764. wi_error_set_openssl_error();
  765. goto err;
  766. }
  767. }
  768. #endif
  769. socket = wi_socket_init_with_descriptor(wi_socket_alloc(), sd);
  770. socket->close = true;
  771. socket->address = wi_retain(*address);
  772. socket->type = accept_socket->type;
  773. socket->direction = WI_SOCKET_READ;
  774. socket->interactive = accept_socket->interactive;
  775. #ifdef WI_SSL
  776. socket->ssl = ssl;
  777. #endif
  778. return wi_autorelease(socket);
  779. err:
  780. #ifdef WI_SSL
  781. if(ssl)
  782. SSL_free(ssl);
  783. #endif
  784. if(sd >= 0)
  785. close(sd);
  786. return NULL;
  787. }
  788. void wi_socket_close(wi_socket_t *socket) {
  789. #ifdef WI_SSL
  790. if(socket->ssl) {
  791. if(!socket->broken) {
  792. if(SSL_shutdown(socket->ssl) == 0)
  793. SSL_shutdown(socket->ssl);
  794. }
  795. SSL_free(socket->ssl);
  796. socket->ssl = NULL;
  797. }
  798. #endif
  799. if(socket->close && socket->sd >= 0) {
  800. close(socket->sd);
  801. socket->sd = -1;
  802. }
  803. }
  804. #pragma mark -
  805. wi_integer_t wi_socket_sendto(wi_socket_t *socket, wi_socket_context_t *context, wi_string_t *fmt, ...) {
  806. wi_string_t *string;
  807. int bytes;
  808. va_list ap;
  809. va_start(ap, fmt);
  810. string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap);
  811. va_end(ap);
  812. bytes = wi_socket_sendto_buffer(socket, context, wi_string_cstring(string), wi_string_length(string));
  813. wi_release(string);
  814. return bytes;
  815. }
  816. wi_integer_t wi_socket_sendto_buffer(wi_socket_t *socket, wi_socket_context_t *context, const char *buffer, size_t length) {
  817. wi_address_t *address;
  818. char *outbuffer = NULL;
  819. wi_integer_t bytes;
  820. address = wi_socket_address(socket);
  821. #ifdef WI_SSL
  822. if(context && context->pub_rsa) {
  823. outbuffer = wi_malloc(RSA_size(context->pub_rsa));
  824. bytes = RSA_public_encrypt(length, (unsigned char *) buffer, (unsigned char *) outbuffer,
  825. context->pub_rsa, RSA_PKCS1_OAEP_PADDING);
  826. if(bytes < 0) {
  827. wi_error_set_openssl_error();
  828. goto end;
  829. }
  830. bytes = sendto(socket->sd, outbuffer, bytes, 0,
  831. wi_address_sa(address), wi_address_sa_length(address));
  832. } else {
  833. #endif
  834. bytes = sendto(socket->sd, buffer, length, 0,
  835. wi_address_sa(address), wi_address_sa_length(address));
  836. #ifdef WI_SSL
  837. }
  838. #endif
  839. if(bytes < 0) {
  840. wi_error_set_errno(errno);
  841. goto end;
  842. }
  843. end:
  844. if(outbuffer)
  845. wi_free(outbuffer);
  846. return bytes;
  847. }
  848. wi_integer_t wi_socket_recvfrom_multiple(wi_array_t *array, wi_socket_context_t *context, char *buffer, size_t length, wi_address_t **address) {
  849. wi_socket_t *socket;
  850. *address = NULL;
  851. socket = wi_socket_wait_multiple(array, 0.0);
  852. if(!socket)
  853. return -1;
  854. return wi_socket_recvfrom(socket, context, buffer, length, address);
  855. }
  856. wi_integer_t wi_socket_recvfrom(wi_socket_t *socket, wi_socket_context_t *context, char *buffer, size_t length, wi_address_t **address) {
  857. struct sockaddr_storage ss;
  858. char *inbuffer = NULL;
  859. socklen_t sslength;
  860. wi_integer_t bytes;
  861. sslength = sizeof(ss);
  862. #ifdef WI_SSL
  863. if(context && context->priv_rsa) {
  864. inbuffer = wi_malloc(length);
  865. bytes = recvfrom(socket->sd, inbuffer, length, 0, (struct sockaddr *) &ss, &sslength);
  866. if(bytes < 0) {
  867. wi_error_set_errno(errno);
  868. goto end;
  869. }
  870. bytes = RSA_private_decrypt(bytes, (unsigned char *) inbuffer, (unsigned char *) buffer,
  871. context->priv_rsa, RSA_PKCS1_OAEP_PADDING);
  872. if(bytes < 0) {
  873. wi_error_set_openssl_error();
  874. goto end;
  875. }
  876. } else {
  877. #endif
  878. bytes = recvfrom(socket->sd, buffer, length, 0, (struct sockaddr *) &ss, &sslength);
  879. if(bytes < 0) {
  880. wi_error_set_errno(errno);
  881. goto end;
  882. }
  883. #ifdef WI_SSL
  884. }
  885. #endif
  886. end:
  887. *address = (sslength > 0) ? wi_autorelease(wi_address_init_with_sa(wi_address_alloc(), (struct sockaddr *) &ss)) : NULL;
  888. if(inbuffer)
  889. wi_free(inbuffer);
  890. return bytes;
  891. }
  892. #pragma mark -
  893. wi_integer_t wi_socket_write(wi_socket_t *socket, wi_time_interval_t timeout, wi_string_t *fmt, ...) {
  894. wi_string_t *string;
  895. wi_integer_t bytes;
  896. va_list ap;
  897. va_start(ap, fmt);
  898. string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap);
  899. va_end(ap);
  900. bytes = wi_socket_write_buffer(socket, timeout, wi_string_cstring(string), wi_string_length(string));
  901. wi_release(string);
  902. return bytes;
  903. }
  904. wi_integer_t wi_socket_write_buffer(wi_socket_t *socket, wi_time_interval_t timeout, const void *buffer, size_t length) {
  905. wi_integer_t bytes;
  906. if(timeout > 0.0) {
  907. if(wi_socket_wait_descriptor(socket->sd, timeout, false, true) != WI_SOCKET_READY)
  908. return -1;
  909. }
  910. #ifdef WI_SSL
  911. if(socket->ssl) {
  912. bytes = SSL_write(socket->ssl, buffer, length);
  913. if(bytes < 0) {
  914. wi_error_set_openssl_error();
  915. socket->broken = true;
  916. }
  917. } else {
  918. #endif
  919. bytes = write(socket->sd, buffer, length);
  920. if(bytes < 0)
  921. wi_error_set_errno(errno);
  922. #ifdef WI_SSL
  923. }
  924. #endif
  925. return bytes;
  926. }
  927. wi_string_t * wi_socket_read(wi_socket_t *socket, wi_time_interval_t timeout, size_t length) {
  928. wi_string_t *string;
  929. char buffer[WI_SOCKET_BUFFER_SIZE];
  930. int bytes = -1;
  931. string = wi_string_init_with_capacity(wi_string_alloc(), length);
  932. while(length > sizeof(buffer)) {
  933. bytes = wi_socket_read_buffer(socket, timeout, buffer, sizeof(buffer));
  934. if(bytes <= 0)
  935. goto end;
  936. wi_string_append_bytes(string, buffer, bytes);
  937. length -= bytes;
  938. }
  939. if(length > 0) {
  940. bytes = wi_socket_read_buffer(socket, timeout, buffer, length);
  941. if(bytes <= 0)
  942. goto end;
  943. wi_string_append_bytes(string, buffer, bytes);
  944. }
  945. end:
  946. if(wi_string_length(string) == 0) {
  947. if(bytes < 0) {
  948. wi_release(string);
  949. string = NULL;
  950. }
  951. }
  952. return wi_autorelease(string);
  953. }
  954. wi_string_t * wi_socket_read_to_string(wi_socket_t *socket, wi_time_interval_t timeout, wi_string_t *separator) {
  955. wi_string_t *string, *substring;
  956. wi_uinteger_t index;
  957. index = wi_string_index_of_string(socket->buffer, separator, 0);
  958. if(index != WI_NOT_FOUND) {
  959. substring = wi_string_substring_to_index(socket->buffer, index + wi_string_length(separator));
  960. wi_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring)));
  961. return substring;
  962. }
  963. while((string = wi_socket_read(socket, timeout, WI_SOCKET_BUFFER_SIZE))) {
  964. if(wi_string_length(string) == 0)
  965. return string;
  966. wi_string_append_string(socket->buffer, string);
  967. index = wi_string_index_of_string(socket->buffer, separator, 0);
  968. if(index == WI_NOT_FOUND) {
  969. if(wi_string_length(socket->buffer) > _WI_SOCKET_BUFFER_MAX_SIZE) {
  970. substring = wi_string_substring_to_index(socket->buffer, _WI_SOCKET_BUFFER_MAX_SIZE);
  971. wi_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring)));
  972. return substring;
  973. }
  974. } else {
  975. substring = wi_string_substring_to_index(socket->buffer, index + wi_string_length(separator));
  976. wi_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring)));
  977. return substring;
  978. }
  979. }
  980. return NULL;
  981. }
  982. wi_integer_t wi_socket_read_buffer(wi_socket_t *socket, wi_time_interval_t timeout, void *buffer, size_t length) {
  983. wi_integer_t bytes;
  984. if(timeout > 0.0) {
  985. #ifdef WI_SSL
  986. if(socket->ssl && SSL_pending(socket->ssl) == 0) {
  987. #endif
  988. if(wi_socket_wait_descriptor(socket->sd, timeout, true, false) != WI_SOCKET_READY)
  989. return -1;
  990. #ifdef WI_SSL
  991. }
  992. #endif
  993. }
  994. #ifdef WI_SSL
  995. if(socket->ssl) {
  996. bytes = SSL_read(socket->ssl, buffer, length);
  997. if(bytes <= 0) {
  998. wi_error_set_openssl_error();
  999. socket->broken = true;
  1000. }
  1001. } else {
  1002. #endif
  1003. bytes = read(socket->sd, buffer, length);
  1004. if(bytes < 0)
  1005. wi_error_set_errno(errno);
  1006. else if(bytes == 0)
  1007. wi_error_set_libwired_error(WI_ERROR_SOCKET_EOF);
  1008. #ifdef WI_SSL
  1009. }
  1010. #endif
  1011. return bytes;
  1012. }