PageRenderTime 70ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/loudmouth/lm-old-socket.c

https://github.com/mcabber/loudmouth
C | 1080 lines | 811 code | 206 blank | 63 comment | 116 complexity | d20e393b510accbbb891fc304fd0a1b3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * Copyright (C) 2006-2008 Imendio AB
  4. * Copyright (C) 2006 Nokia Corporation. All rights reserved.
  5. * Copyright (C) 2007 Collabora Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this program; if not, see <https://www.gnu.org/licenses>
  19. */
  20. #include <config.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. /* Needed on Mac OS X */
  25. #if HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. /* Needed on Mac OS X */
  29. #if HAVE_ARPA_NAMESER_COMPAT_H
  30. #include <arpa/nameser_compat.h>
  31. #endif
  32. #include <arpa/inet.h>
  33. #include <arpa/nameser.h>
  34. #include <resolv.h>
  35. #include "lm-debug.h"
  36. #include "lm-error.h"
  37. #include "lm-internals.h"
  38. #include "lm-misc.h"
  39. #include "lm-proxy.h"
  40. #include "lm-resolver.h"
  41. #include "lm-ssl.h"
  42. #include "lm-ssl-internals.h"
  43. #include "lm-sock.h"
  44. #include "lm-old-socket.h"
  45. #define IN_BUFFER_SIZE 1024
  46. #define SRV_LEN 8192
  47. struct _LmOldSocket {
  48. LmConnection *connection;
  49. GMainContext *context;
  50. gchar *domain;
  51. gchar *server;
  52. guint port;
  53. LmSSL *ssl;
  54. gboolean ssl_started;
  55. LmProxy *proxy;
  56. GIOChannel *io_channel;
  57. GSource *watch_in;
  58. GSource *watch_err;
  59. GSource *watch_hup;
  60. LmOldSocketT fd;
  61. GSource *watch_connect;
  62. gboolean cancel_open;
  63. GSource *watch_out;
  64. GString *out_buf;
  65. LmConnectData *connect_data;
  66. IncomingDataFunc data_func;
  67. SocketClosedFunc closed_func;
  68. ConnectResultFunc connect_func;
  69. gpointer user_data;
  70. guint ref_count;
  71. LmResolver *resolver;
  72. };
  73. static void socket_free (LmOldSocket *socket);
  74. static gboolean socket_do_connect (LmConnectData *connect_data);
  75. static gboolean socket_connect_cb (GIOChannel *source,
  76. GIOCondition condition,
  77. LmConnectData *connect_data);
  78. static gboolean socket_in_event (GIOChannel *source,
  79. GIOCondition condition,
  80. LmOldSocket *socket);
  81. static gboolean socket_hup_event (GIOChannel *source,
  82. GIOCondition condition,
  83. LmOldSocket *socket);
  84. static gboolean socket_error_event (GIOChannel *source,
  85. GIOCondition condition,
  86. LmOldSocket *socket);
  87. static gboolean socket_buffered_write_cb (GIOChannel *source,
  88. GIOCondition condition,
  89. LmOldSocket *socket);
  90. static void socket_close_io_channel (GIOChannel *io_channel);
  91. static gboolean old_socket_output_is_buffered (LmOldSocket *socket,
  92. const gchar *buffer,
  93. gint len);
  94. static void old_socket_setup_output_buffer (LmOldSocket *socket,
  95. const gchar *buffer,
  96. gint len);
  97. static void
  98. socket_free (LmOldSocket *socket)
  99. {
  100. g_free (socket->server);
  101. g_free (socket->domain);
  102. if (socket->ssl) {
  103. lm_ssl_unref (socket->ssl);
  104. }
  105. if (socket->proxy) {
  106. lm_proxy_unref (socket->proxy);
  107. }
  108. if (socket->out_buf) {
  109. g_string_free (socket->out_buf, TRUE);
  110. }
  111. if (socket->resolver) {
  112. g_object_unref (socket->resolver);
  113. }
  114. g_free (socket);
  115. }
  116. static gint
  117. old_socket_do_write (LmOldSocket *socket, const gchar *buf, guint len)
  118. {
  119. gint b_written;
  120. if (socket->ssl_started) {
  121. b_written = _lm_ssl_send (socket->ssl, buf, len);
  122. } else {
  123. GIOStatus io_status = G_IO_STATUS_AGAIN;
  124. gsize bytes_written;
  125. while (io_status == G_IO_STATUS_AGAIN) {
  126. io_status = g_io_channel_write_chars (socket->io_channel,
  127. buf, len,
  128. &bytes_written,
  129. NULL);
  130. }
  131. b_written = bytes_written;
  132. if (io_status != G_IO_STATUS_NORMAL) {
  133. b_written = -1;
  134. }
  135. }
  136. return b_written;
  137. }
  138. gint
  139. lm_old_socket_write (LmOldSocket *socket, const gchar *buf, gint len)
  140. {
  141. gint b_written;
  142. if (old_socket_output_is_buffered (socket, buf, len)) {
  143. return len;
  144. }
  145. b_written = old_socket_do_write (socket, buf, len);
  146. if (b_written < len && b_written != -1) {
  147. old_socket_setup_output_buffer (socket,
  148. buf + b_written,
  149. len - b_written);
  150. return len;
  151. }
  152. return b_written;
  153. }
  154. static gboolean
  155. socket_read_incoming (LmOldSocket *socket,
  156. gchar *buf,
  157. gsize buf_size,
  158. gsize *bytes_read,
  159. gboolean *hangup,
  160. gint *reason)
  161. {
  162. GIOStatus status;
  163. *hangup = FALSE;
  164. if (socket->ssl_started) {
  165. status = _lm_ssl_read (socket->ssl,
  166. buf, buf_size - 1, bytes_read);
  167. } else {
  168. status = g_io_channel_read_chars (socket->io_channel,
  169. buf, buf_size - 1,
  170. bytes_read,
  171. NULL);
  172. }
  173. if (status != G_IO_STATUS_NORMAL) {
  174. switch (status) {
  175. case G_IO_STATUS_EOF:
  176. *reason = LM_DISCONNECT_REASON_HUP;
  177. break;
  178. case G_IO_STATUS_AGAIN:
  179. /* No data readable but we didn't hangup */
  180. return FALSE;
  181. break;
  182. case G_IO_STATUS_ERROR:
  183. *reason = LM_DISCONNECT_REASON_ERROR;
  184. break;
  185. default:
  186. *reason = LM_DISCONNECT_REASON_UNKNOWN;
  187. }
  188. /* Notify connection_in_event that we hangup the connection */
  189. *hangup = TRUE;
  190. return FALSE;
  191. }
  192. buf[*bytes_read] = '\0';
  193. /* There is more data to be read */
  194. return TRUE;
  195. }
  196. static gboolean
  197. socket_in_event (GIOChannel *source,
  198. GIOCondition condition,
  199. LmOldSocket *socket)
  200. {
  201. gchar buf[IN_BUFFER_SIZE];
  202. gsize bytes_read = 0;
  203. gboolean read_anything = FALSE;
  204. gboolean hangup = 0;
  205. gint reason = 0;
  206. if (!socket->io_channel) {
  207. return FALSE;
  208. }
  209. while (socket_read_incoming (socket, buf, IN_BUFFER_SIZE,
  210. &bytes_read, &hangup, &reason)) {
  211. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "\nRECV [%d]:\n",
  212. (int)bytes_read);
  213. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  214. "-----------------------------------\n");
  215. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "'%s'\n", buf);
  216. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  217. "-----------------------------------\n");
  218. lm_verbose ("Read: %d chars\n", (int)bytes_read);
  219. (socket->data_func) (socket, buf, socket->user_data);
  220. read_anything = TRUE;
  221. }
  222. /* If we have read something, delay the hangup so that the data can be
  223. * processed. */
  224. if (hangup && !read_anything) {
  225. (socket->closed_func) (socket, reason, socket->user_data);
  226. return FALSE;
  227. }
  228. return TRUE;
  229. }
  230. static gboolean
  231. socket_hup_event (GIOChannel *source,
  232. GIOCondition condition,
  233. LmOldSocket *socket)
  234. {
  235. lm_verbose ("HUP event: %d->'%s'\n",
  236. condition, lm_misc_io_condition_to_str (condition));
  237. if (!socket->io_channel) {
  238. return FALSE;
  239. }
  240. (socket->closed_func) (socket, LM_DISCONNECT_REASON_HUP,
  241. socket->user_data);
  242. return TRUE;
  243. }
  244. static gboolean
  245. socket_error_event (GIOChannel *source,
  246. GIOCondition condition,
  247. LmOldSocket *socket)
  248. {
  249. lm_verbose ("ERROR event: %d->'%s'\n",
  250. condition, lm_misc_io_condition_to_str (condition));
  251. if (!socket->io_channel) {
  252. return FALSE;
  253. }
  254. (socket->closed_func) (socket, LM_DISCONNECT_REASON_ERROR,
  255. socket->user_data);
  256. return TRUE;
  257. }
  258. static gboolean
  259. _lm_old_socket_ssl_init (LmOldSocket *socket, gboolean delayed)
  260. {
  261. GError *error = NULL;
  262. const gchar *ssl_verify_domain = NULL;
  263. lm_verbose ("Setting up SSL...\n");
  264. _lm_ssl_initialize (socket->ssl);
  265. #ifdef HAVE_GNUTLS
  266. /* GNU TLS requires the socket to be blocking */
  267. _lm_sock_set_blocking (socket->fd, TRUE);
  268. #endif
  269. /* If we're using StartTLS, the correct thing is to verify against
  270. * the domain. If we're using old SSL, we should verify against the
  271. * hostname. */
  272. if (delayed)
  273. ssl_verify_domain = socket->domain;
  274. else
  275. ssl_verify_domain = socket->server;
  276. if (!_lm_ssl_begin (socket->ssl, socket->fd, ssl_verify_domain, &error)) {
  277. lm_verbose ("Could not begin SSL\n");
  278. if (error) {
  279. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  280. "%s\n", error->message);
  281. g_error_free (error);
  282. }
  283. _lm_sock_shutdown (socket->fd);
  284. _lm_sock_close (socket->fd);
  285. if (!delayed && socket->connect_func) {
  286. (socket->connect_func) (socket, FALSE, socket->user_data);
  287. }
  288. return FALSE;
  289. }
  290. #ifdef HAVE_GNUTLS
  291. _lm_sock_set_blocking (socket->fd, FALSE);
  292. #endif
  293. socket->ssl_started = TRUE;
  294. return TRUE;
  295. }
  296. gboolean
  297. lm_old_socket_starttls (LmOldSocket *socket)
  298. {
  299. g_return_val_if_fail (lm_ssl_get_use_starttls (socket->ssl) == TRUE, FALSE);
  300. return _lm_old_socket_ssl_init (socket, TRUE);
  301. }
  302. void
  303. _lm_old_socket_succeeded (LmConnectData *connect_data)
  304. {
  305. LmOldSocket *socket;
  306. socket = connect_data->socket;
  307. if (socket->watch_connect) {
  308. g_source_destroy (socket->watch_connect);
  309. socket->watch_connect = NULL;
  310. }
  311. /* Need some way to report error/success */
  312. if (socket->cancel_open) {
  313. lm_verbose ("Cancelling connection...\n");
  314. if (socket->connect_func) {
  315. (socket->connect_func) (socket, FALSE, socket->user_data);
  316. }
  317. return;
  318. }
  319. socket->fd = connect_data->fd;
  320. socket->io_channel = connect_data->io_channel;
  321. g_object_unref (socket->resolver);
  322. socket->resolver = NULL;
  323. socket->connect_data = NULL;
  324. g_free (connect_data);
  325. /* old-style ssl should be started immediately */
  326. if (socket->ssl && (lm_ssl_get_use_starttls (socket->ssl) == FALSE)) {
  327. if (!_lm_old_socket_ssl_init (socket, FALSE)) {
  328. return;
  329. }
  330. }
  331. socket->watch_in =
  332. lm_misc_add_io_watch (socket->context,
  333. socket->io_channel,
  334. G_IO_IN,
  335. (GIOFunc) socket_in_event,
  336. socket);
  337. /* FIXME: if we add these, we don't get ANY
  338. * response from the server, this is to do with the way that
  339. * windows handles watches, see bug #331214.
  340. */
  341. #ifndef G_OS_WIN32
  342. socket->watch_err =
  343. lm_misc_add_io_watch (socket->context,
  344. socket->io_channel,
  345. G_IO_ERR,
  346. (GIOFunc) socket_error_event,
  347. socket);
  348. socket->watch_hup =
  349. lm_misc_add_io_watch (socket->context,
  350. socket->io_channel,
  351. G_IO_HUP,
  352. (GIOFunc) socket_hup_event,
  353. socket);
  354. #endif
  355. if (socket->connect_func) {
  356. (socket->connect_func) (socket, TRUE, socket->user_data);
  357. }
  358. }
  359. gboolean
  360. _lm_old_socket_failed_with_error (LmConnectData *connect_data, int error)
  361. {
  362. LmOldSocket *socket;
  363. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  364. "Connection failed: %s (error %d)\n",
  365. _lm_sock_get_error_str (error), error);
  366. socket = lm_old_socket_ref (connect_data->socket);
  367. connect_data->current_addr = lm_resolver_results_get_next (socket->resolver);
  368. if (socket->watch_connect) {
  369. g_source_destroy (socket->watch_connect);
  370. socket->watch_connect = NULL;
  371. }
  372. if (connect_data->io_channel != NULL) {
  373. socket_close_io_channel (connect_data->io_channel);
  374. connect_data->io_channel = NULL;
  375. }
  376. if (connect_data->current_addr == NULL) { /*Ran Out Of Addresses*/
  377. if (socket->connect_func) {
  378. (socket->connect_func) (socket, FALSE, socket->user_data);
  379. }
  380. /* if the user callback called connection_close(), this is already freed */
  381. if (socket->connect_data != NULL) {
  382. if (socket->resolver) {
  383. g_object_unref (socket->resolver);
  384. }
  385. socket->connect_data = NULL;
  386. g_free (connect_data);
  387. }
  388. } else {
  389. /* try to connect to the next host */
  390. return socket_do_connect (connect_data);
  391. }
  392. lm_old_socket_unref (socket);
  393. return FALSE;
  394. }
  395. gboolean
  396. _lm_old_socket_failed (LmConnectData *connect_data)
  397. {
  398. return _lm_old_socket_failed_with_error (connect_data,
  399. _lm_sock_get_last_error());
  400. }
  401. static gboolean
  402. socket_connect_cb (GIOChannel *source,
  403. GIOCondition condition,
  404. LmConnectData *connect_data)
  405. {
  406. LmOldSocket *socket;
  407. /* struct addrinfo *addr; */
  408. int err;
  409. socklen_t len;
  410. LmOldSocketT fd;
  411. gboolean result = FALSE;
  412. socket = lm_old_socket_ref (connect_data->socket);
  413. /* addr = connect_data->current_addr; */
  414. fd = g_io_channel_unix_get_fd (source);
  415. if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
  416. len = sizeof (err);
  417. _lm_sock_get_error (fd, &err, &len);
  418. if (!_lm_sock_is_blocking_error (err)) {
  419. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
  420. "Connection failed.\n");
  421. /* error condition, but might be possible to recover
  422. * from it (by connecting to the next host) */
  423. if (!_lm_old_socket_failed_with_error (connect_data, err)) {
  424. socket->watch_connect = NULL;
  425. }
  426. goto out;
  427. }
  428. }
  429. #if 0
  430. if (_lm_connection_async_connect_waiting (socket->connection)) {
  431. gint res;
  432. fd = g_io_channel_unix_get_fd (source);
  433. res = _lm_sock_connect (fd, addr->ai_addr, (int)addr->ai_addrlen);
  434. if (res < 0) {
  435. err = _lm_sock_get_last_error ();
  436. if (_lm_sock_is_blocking_success (err)) {
  437. _lm_connection_set_async_connect_waiting (socket->connection, FALSE);
  438. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  439. "Connection success (1).\n");
  440. _lm_old_socket_succeeded (connect_data);
  441. }
  442. if (_lm_connection_async_connect_waiting (socket->connection) &&
  443. !_lm_sock_is_blocking_error (err)) {
  444. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  445. "Connection failed.\n");
  446. _lm_sock_close (connect_data->fd);
  447. _lm_old_socket_failed_with_error (connect_data, err);
  448. socket->watch_connect = NULL;
  449. goto out;
  450. }
  451. }
  452. } else {
  453. #endif
  454. {
  455. /* for blocking sockets, G_IO_OUT means we are connected */
  456. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
  457. "Connection success (2).\n");
  458. _lm_old_socket_succeeded (connect_data);
  459. }
  460. result = TRUE;
  461. out:
  462. lm_old_socket_unref(socket);
  463. return result;
  464. }
  465. static gboolean
  466. socket_do_connect (LmConnectData *connect_data)
  467. {
  468. LmOldSocket *socket;
  469. LmOldSocketT fd;
  470. int res, err;
  471. int port;
  472. char name[NI_MAXHOST];
  473. char portname[NI_MAXSERV];
  474. struct addrinfo *addr;
  475. socket = connect_data->socket;
  476. addr = connect_data->current_addr;
  477. if (socket->port == 0) {
  478. socket->port = 5222;
  479. }
  480. if (socket->proxy) {
  481. port = htons (lm_proxy_get_port (socket->proxy));
  482. } else {
  483. port = htons (socket->port);
  484. }
  485. switch (addr->ai_family) {
  486. case AF_INET:
  487. ((struct sockaddr_in *) addr->ai_addr)->sin_port = port;
  488. break;
  489. case AF_INET6:
  490. ((struct sockaddr_in6 *) addr->ai_addr)->sin6_port = port;
  491. break;
  492. default:
  493. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
  494. "Unknown socket family %d\n", addr->ai_family);
  495. #ifndef G_OS_WIN32
  496. return _lm_old_socket_failed_with_error (connect_data, EAFNOSUPPORT);
  497. #else
  498. return _lm_old_socket_failed_with_error (connect_data, WSAEAFNOSUPPORT);
  499. #endif
  500. }
  501. res = getnameinfo (addr->ai_addr,
  502. (socklen_t)addr->ai_addrlen,
  503. name, sizeof (name),
  504. portname, sizeof (portname),
  505. NI_NUMERICHOST | NI_NUMERICSERV);
  506. if (res < 0) {
  507. return _lm_old_socket_failed (connect_data);
  508. }
  509. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  510. "Trying %s port %s...\n", name, portname);
  511. fd = _lm_sock_makesocket (addr->ai_family,
  512. addr->ai_socktype,
  513. addr->ai_protocol);
  514. if (!_LM_SOCK_VALID (fd)) {
  515. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  516. "Failed making socket, error:%d...\n",
  517. _lm_sock_get_last_error ());
  518. return _lm_old_socket_failed (connect_data);
  519. }
  520. /* Even though it says _unix_new(), it is supported by glib on
  521. * win32 because glib does some cool stuff to find out if it
  522. * can treat it as a FD or a windows SOCKET.
  523. */
  524. connect_data->fd = fd;
  525. connect_data->io_channel = g_io_channel_unix_new (fd);
  526. g_io_channel_set_encoding (connect_data->io_channel, NULL, NULL);
  527. g_io_channel_set_buffered (connect_data->io_channel, FALSE);
  528. _lm_sock_set_blocking (connect_data->fd, FALSE);
  529. if (socket->proxy) {
  530. socket->watch_connect =
  531. lm_misc_add_io_watch (socket->context,
  532. connect_data->io_channel,
  533. G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
  534. (GIOFunc) _lm_proxy_connect_cb,
  535. connect_data);
  536. } else {
  537. socket->watch_connect =
  538. lm_misc_add_io_watch (socket->context,
  539. connect_data->io_channel,
  540. G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
  541. (GIOFunc) socket_connect_cb,
  542. connect_data);
  543. }
  544. res = _lm_sock_connect (connect_data->fd,
  545. addr->ai_addr, (int)addr->ai_addrlen);
  546. if (res < 0) {
  547. err = _lm_sock_get_last_error ();
  548. if (!_lm_sock_is_blocking_error (err)) {
  549. _lm_sock_close (connect_data->fd);
  550. return _lm_old_socket_failed_with_error (connect_data, err);
  551. }
  552. }
  553. return TRUE;
  554. }
  555. static gboolean
  556. old_socket_output_is_buffered (LmOldSocket *socket,
  557. const gchar *buffer,
  558. gint len)
  559. {
  560. if (socket->out_buf) {
  561. lm_verbose ("Appending %d bytes to output buffer\n", len);
  562. g_string_append_len (socket->out_buf, buffer, len);
  563. return TRUE;
  564. }
  565. return FALSE;
  566. }
  567. static void
  568. old_socket_setup_output_buffer (LmOldSocket *socket, const gchar *buffer, gint len)
  569. {
  570. lm_verbose ("OUTPUT BUFFER ENABLED\n");
  571. socket->out_buf = g_string_new_len (buffer, len);
  572. socket->watch_out =
  573. lm_misc_add_io_watch (socket->context,
  574. socket->io_channel,
  575. G_IO_OUT,
  576. (GIOFunc) socket_buffered_write_cb,
  577. socket);
  578. }
  579. static gboolean
  580. socket_buffered_write_cb (GIOChannel *source,
  581. GIOCondition condition,
  582. LmOldSocket *socket)
  583. {
  584. gint b_written;
  585. GString *out_buf;
  586. out_buf = socket->out_buf;
  587. if (!out_buf) {
  588. /* Should not be possible */
  589. return FALSE;
  590. }
  591. b_written = old_socket_do_write (socket, out_buf->str, out_buf->len);
  592. if (b_written < 0) {
  593. (socket->closed_func) (socket, LM_DISCONNECT_REASON_ERROR,
  594. socket->user_data);
  595. return FALSE;
  596. }
  597. g_string_erase (out_buf, 0, (gsize) b_written);
  598. if (out_buf->len == 0) {
  599. lm_verbose ("Output buffer is empty, going back to normal output\n");
  600. if (socket->watch_out) {
  601. g_source_destroy (socket->watch_out);
  602. socket->watch_out = NULL;
  603. }
  604. g_string_free (out_buf, TRUE);
  605. socket->out_buf = NULL;
  606. return FALSE;
  607. }
  608. return TRUE;
  609. }
  610. static void
  611. socket_close_io_channel (GIOChannel *io_channel)
  612. {
  613. gint fd;
  614. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  615. "Freeing up IOChannel and file descriptor\n");
  616. fd = g_io_channel_unix_get_fd (io_channel);
  617. g_io_channel_unref (io_channel);
  618. _lm_sock_close (fd);
  619. }
  620. static void
  621. old_socket_resolver_host_cb (LmResolver *resolver,
  622. LmResolverResult result,
  623. gpointer user_data)
  624. {
  625. LmOldSocket *socket = (LmOldSocket *) user_data;
  626. char dispbuf[128];
  627. struct addrinfo *addr;
  628. void *sock_addr;
  629. lm_verbose ("LmOldSocket::host_cb (result=%d)\n", result);
  630. if (result == LM_RESOLVER_RESULT_OK) {
  631. /* Find and use the first result with a supported address family */
  632. while ((addr = lm_resolver_results_get_next (resolver))) {
  633. switch (addr->ai_family) {
  634. case AF_INET:
  635. sock_addr = & (((struct sockaddr_in *) addr->ai_addr)->sin_addr);
  636. break;
  637. case AF_INET6:
  638. sock_addr = & (((struct sockaddr_in6 *) addr->ai_addr)->sin6_addr);
  639. break;
  640. default:
  641. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
  642. "Resolver returned a socket address with unknown family %d\n", addr->ai_family);
  643. continue;
  644. }
  645. if (inet_ntop (addr->ai_family, sock_addr, dispbuf, sizeof(dispbuf))) {
  646. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
  647. "Attempting Connection to %s\n", dispbuf);
  648. } else {
  649. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
  650. "Attempting Connection (unable to convert address to presentable format)\n");
  651. };
  652. socket->connect_data->current_addr = addr;
  653. socket_do_connect (socket->connect_data);
  654. return;
  655. }
  656. }
  657. lm_verbose ("error while resolving, bailing out\n");
  658. if (socket->connect_func) {
  659. (socket->connect_func) (socket, FALSE, socket->user_data);
  660. }
  661. /*FIXME: Leaking Resolvers Until Clean Up Can Be Properly Handled
  662. g_object_unref (socket->resolver);
  663. socket->resolver = NULL;*/
  664. g_free (socket->connect_data);
  665. socket->connect_data = NULL;
  666. }
  667. /* FIXME: Need to have a way to only get srv reply and then decide if the
  668. * resolver should continue to look the host up.
  669. *
  670. * This is needed for the case when we do a SRV lookup to lookup the
  671. * real host of the service and then connect to it through a proxy.
  672. */
  673. static void
  674. old_socket_resolver_srv_cb (LmResolver *resolver,
  675. LmResolverResult result,
  676. gpointer user_data)
  677. {
  678. LmOldSocket *socket = (LmOldSocket *) user_data;
  679. const gchar *remote_addr;
  680. lm_verbose ("LmOldSocket::srv_cb (result=%d)\n", result);
  681. if (result != LM_RESOLVER_RESULT_OK) {
  682. lm_verbose ("SRV lookup failed, trying jid domain\n");
  683. socket->server = g_strdup (socket->domain);
  684. } else {
  685. g_object_get (resolver, "host", &socket->server, NULL);
  686. g_object_get (resolver, "port", &socket->port, NULL);
  687. }
  688. if (socket->proxy) {
  689. remote_addr = lm_proxy_get_server (socket->proxy);
  690. } else if (socket->server) {
  691. remote_addr = socket->server;
  692. }
  693. else {
  694. remote_addr = socket->domain;
  695. }
  696. g_object_unref (socket->resolver);
  697. socket->resolver =
  698. lm_resolver_new_for_host (remote_addr,
  699. old_socket_resolver_host_cb,
  700. socket);
  701. lm_resolver_lookup (socket->resolver);
  702. }
  703. LmOldSocket *
  704. lm_old_socket_create (GMainContext *context,
  705. IncomingDataFunc data_func,
  706. SocketClosedFunc closed_func,
  707. ConnectResultFunc connect_func,
  708. gpointer user_data,
  709. LmConnection *connection,
  710. const gchar *server,
  711. const gchar *domain,
  712. guint port,
  713. LmSSL *ssl,
  714. LmProxy *proxy,
  715. GError **error)
  716. {
  717. LmOldSocket *socket;
  718. LmConnectData *data;
  719. g_return_val_if_fail (domain != NULL, NULL);
  720. g_return_val_if_fail ((port >= LM_MIN_PORT && port <= LM_MAX_PORT), NULL);
  721. g_return_val_if_fail (data_func != NULL, NULL);
  722. g_return_val_if_fail (closed_func != NULL, NULL);
  723. g_return_val_if_fail (connect_func != NULL, NULL);
  724. socket = g_new0 (LmOldSocket, 1);
  725. socket->ref_count = 1;
  726. socket->connection = connection;
  727. socket->domain = g_strdup (domain);
  728. socket->server = g_strdup (server);
  729. socket->port = port;
  730. socket->cancel_open = FALSE;
  731. socket->ssl = ssl;
  732. socket->ssl_started = FALSE;
  733. socket->proxy = NULL;
  734. if (context) {
  735. socket->context = g_main_context_ref (context);
  736. }
  737. if (proxy) {
  738. socket->proxy = lm_proxy_ref (proxy);
  739. }
  740. data = g_new0 (LmConnectData, 1);
  741. data->socket = socket;
  742. data->connection = socket->connection;
  743. data->fd = -1;
  744. socket->connect_data = data;
  745. if (!server) {
  746. socket->resolver = lm_resolver_new_for_service (socket->domain,
  747. "xmpp-client",
  748. "tcp",
  749. old_socket_resolver_srv_cb,
  750. socket);
  751. } else {
  752. socket->resolver =
  753. lm_resolver_new_for_host (socket->server ? socket->server : socket->domain,
  754. old_socket_resolver_host_cb,
  755. socket);
  756. }
  757. if (socket->context) {
  758. g_object_set (socket->resolver, "context", context, NULL);
  759. }
  760. socket->data_func = data_func;
  761. socket->closed_func = closed_func;
  762. socket->connect_func = connect_func;
  763. socket->user_data = user_data;
  764. lm_resolver_lookup (socket->resolver);
  765. return socket;
  766. }
  767. void
  768. lm_old_socket_flush (LmOldSocket *socket)
  769. {
  770. g_return_if_fail (socket != NULL);
  771. g_return_if_fail (socket->io_channel != NULL);
  772. g_io_channel_flush (socket->io_channel, NULL);
  773. }
  774. void
  775. lm_old_socket_close (LmOldSocket *socket)
  776. {
  777. LmConnectData *data;
  778. g_return_if_fail (socket != NULL);
  779. if (socket->watch_connect) {
  780. g_source_destroy (socket->watch_connect);
  781. socket->watch_connect = NULL;
  782. }
  783. data = socket->connect_data;
  784. if (data) {
  785. if (data->io_channel) {
  786. socket_close_io_channel (data->io_channel);
  787. }
  788. socket->connect_data = NULL;
  789. g_free (data);
  790. }
  791. /* FIXME: Leaking Resolvers Until Clean Up Can Be Corrected
  792. if (socket->resolver) {
  793. g_object_unref (socket->resolver);
  794. socket->resolver = NULL;
  795. } */
  796. if (socket->io_channel) {
  797. if (socket->watch_in) {
  798. g_source_destroy (socket->watch_in);
  799. socket->watch_in = NULL;
  800. }
  801. if (socket->watch_err) {
  802. g_source_destroy (socket->watch_err);
  803. socket->watch_err = NULL;
  804. }
  805. if (socket->watch_hup) {
  806. g_source_destroy (socket->watch_hup);
  807. socket->watch_hup = NULL;
  808. }
  809. if (socket->watch_out) {
  810. g_source_destroy (socket->watch_out);
  811. socket->watch_out = NULL;
  812. }
  813. socket_close_io_channel (socket->io_channel);
  814. socket->io_channel = NULL;
  815. socket->fd = -1;
  816. }
  817. if (socket->ssl) {
  818. _lm_ssl_close (socket->ssl);
  819. }
  820. }
  821. gchar *
  822. lm_old_socket_get_local_host (LmOldSocket *socket)
  823. {
  824. return _lm_sock_get_local_host (socket->fd);
  825. }
  826. LmOldSocket *
  827. lm_old_socket_ref (LmOldSocket *socket)
  828. {
  829. g_return_val_if_fail (socket != NULL, NULL);
  830. socket->ref_count++;
  831. return socket;
  832. }
  833. void
  834. lm_old_socket_unref (LmOldSocket *socket)
  835. {
  836. g_return_if_fail (socket != NULL);
  837. socket->ref_count--;
  838. if (socket->ref_count <= 0) {
  839. socket_free (socket);
  840. }
  841. }
  842. gboolean
  843. lm_old_socket_set_keepalive (LmOldSocket *socket, int delay)
  844. {
  845. #ifdef USE_TCP_KEEPALIVES
  846. return _lm_sock_set_keepalive (socket->fd, delay);
  847. #else
  848. return FALSE;
  849. #endif /* USE_TCP_KEEPALIVES */
  850. }
  851. void
  852. lm_old_socket_asyncns_cancel (LmOldSocket *socket)
  853. {
  854. if (!socket->resolver) {
  855. return;
  856. }
  857. lm_resolver_cancel (socket->resolver);
  858. }
  859. gboolean
  860. lm_old_socket_get_use_starttls (LmOldSocket *socket)
  861. {
  862. if (!socket->ssl) {
  863. return FALSE;
  864. }
  865. return lm_ssl_get_use_starttls (socket->ssl);
  866. }
  867. gboolean
  868. lm_old_socket_get_require_starttls (LmOldSocket *socket)
  869. {
  870. if (!socket->ssl) {
  871. return FALSE;
  872. }
  873. return lm_ssl_get_require_starttls (socket->ssl);
  874. }