PageRenderTime 5986ms CodeModel.GetById 81ms RepoModel.GetById 31ms app.codeStats 1ms

/loudmouth/lm-old-socket.c

https://github.com/engineyard/loudmouth
C | 1062 lines | 789 code | 209 blank | 64 comment | 116 complexity | ccf2ea3c7b2901d85e56f9ee2926d996 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, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. #include <config.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. /* Needed on Mac OS X */
  26. #if HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. /* Needed on Mac OS X */
  30. #if HAVE_ARPA_NAMESER_COMPAT_H
  31. #include <arpa/nameser_compat.h>
  32. #endif
  33. #include <arpa/inet.h>
  34. #include <arpa/nameser.h>
  35. #include <resolv.h>
  36. #include "lm-debug.h"
  37. #include "lm-error.h"
  38. #include "lm-internals.h"
  39. #include "lm-misc.h"
  40. #include "lm-proxy.h"
  41. #include "lm-resolver.h"
  42. #include "lm-ssl.h"
  43. #include "lm-ssl-internals.h"
  44. #include "lm-sock.h"
  45. #include "lm-old-socket.h"
  46. #define IN_BUFFER_SIZE 1024
  47. #define SRV_LEN 8192
  48. struct _LmOldSocket {
  49. LmConnection *connection;
  50. GMainContext *context;
  51. gchar *domain;
  52. gchar *server;
  53. guint port;
  54. LmSSL *ssl;
  55. gboolean ssl_started;
  56. LmProxy *proxy;
  57. GIOChannel *io_channel;
  58. GSource *watch_in;
  59. GSource *watch_err;
  60. GSource *watch_hup;
  61. LmOldSocketT fd;
  62. GSource *watch_connect;
  63. gboolean cancel_open;
  64. GSource *watch_out;
  65. GString *out_buf;
  66. LmConnectData *connect_data;
  67. IncomingDataFunc data_func;
  68. SocketClosedFunc closed_func;
  69. ConnectResultFunc connect_func;
  70. gpointer user_data;
  71. guint ref_count;
  72. LmResolver *resolver;
  73. };
  74. static void socket_free (LmOldSocket *socket);
  75. static gboolean socket_do_connect (LmConnectData *connect_data);
  76. static gboolean socket_connect_cb (GIOChannel *source,
  77. GIOCondition condition,
  78. LmConnectData *connect_data);
  79. static gboolean socket_in_event (GIOChannel *source,
  80. GIOCondition condition,
  81. LmOldSocket *socket);
  82. static gboolean socket_hup_event (GIOChannel *source,
  83. GIOCondition condition,
  84. LmOldSocket *socket);
  85. static gboolean socket_error_event (GIOChannel *source,
  86. GIOCondition condition,
  87. LmOldSocket *socket);
  88. static gboolean socket_buffered_write_cb (GIOChannel *source,
  89. GIOCondition condition,
  90. LmOldSocket *socket);
  91. static void socket_close_io_channel (GIOChannel *io_channel);
  92. static gboolean old_socket_output_is_buffered (LmOldSocket *socket,
  93. const gchar *buffer,
  94. gint len);
  95. static void old_socket_setup_output_buffer (LmOldSocket *socket,
  96. const gchar *buffer,
  97. gint len);
  98. static void
  99. socket_free (LmOldSocket *socket)
  100. {
  101. g_free (socket->server);
  102. g_free (socket->domain);
  103. if (socket->ssl) {
  104. lm_ssl_unref (socket->ssl);
  105. }
  106. if (socket->proxy) {
  107. lm_proxy_unref (socket->proxy);
  108. }
  109. if (socket->out_buf) {
  110. g_string_free (socket->out_buf, TRUE);
  111. }
  112. if (socket->resolver) {
  113. g_object_unref (socket->resolver);
  114. }
  115. g_free (socket);
  116. }
  117. static gint
  118. old_socket_do_write (LmOldSocket *socket, const gchar *buf, guint len)
  119. {
  120. gint b_written;
  121. if (socket->ssl_started) {
  122. b_written = _lm_ssl_send (socket->ssl, buf, len);
  123. } else {
  124. GIOStatus io_status = G_IO_STATUS_AGAIN;
  125. gsize bytes_written;
  126. while (io_status == G_IO_STATUS_AGAIN) {
  127. io_status = g_io_channel_write_chars (socket->io_channel,
  128. buf, len,
  129. &bytes_written,
  130. NULL);
  131. }
  132. b_written = bytes_written;
  133. if (io_status != G_IO_STATUS_NORMAL) {
  134. b_written = -1;
  135. }
  136. }
  137. return b_written;
  138. }
  139. gint
  140. lm_old_socket_write (LmOldSocket *socket, const gchar *buf, gint len)
  141. {
  142. gint b_written;
  143. if (old_socket_output_is_buffered (socket, buf, len)) {
  144. return len;
  145. }
  146. b_written = old_socket_do_write (socket, buf, len);
  147. if (b_written < len && b_written != -1) {
  148. old_socket_setup_output_buffer (socket,
  149. buf + b_written,
  150. len - b_written);
  151. return len;
  152. }
  153. return b_written;
  154. }
  155. static gboolean
  156. socket_read_incoming (LmOldSocket *socket,
  157. gchar *buf,
  158. gsize buf_size,
  159. gsize *bytes_read,
  160. gboolean *hangup,
  161. gint *reason)
  162. {
  163. GIOStatus status;
  164. *hangup = FALSE;
  165. if (socket->ssl_started) {
  166. status = _lm_ssl_read (socket->ssl,
  167. buf, buf_size - 1, bytes_read);
  168. } else {
  169. status = g_io_channel_read_chars (socket->io_channel,
  170. buf, buf_size - 1,
  171. bytes_read,
  172. NULL);
  173. }
  174. if (status != G_IO_STATUS_NORMAL || *bytes_read < 0) {
  175. switch (status) {
  176. case G_IO_STATUS_EOF:
  177. *reason = LM_DISCONNECT_REASON_HUP;
  178. break;
  179. case G_IO_STATUS_AGAIN:
  180. /* No data readable but we didn't hangup */
  181. return FALSE;
  182. break;
  183. case G_IO_STATUS_ERROR:
  184. *reason = LM_DISCONNECT_REASON_ERROR;
  185. break;
  186. default:
  187. *reason = LM_DISCONNECT_REASON_UNKNOWN;
  188. }
  189. /* Notify connection_in_event that we hangup the connection */
  190. *hangup = TRUE;
  191. return FALSE;
  192. }
  193. buf[*bytes_read] = '\0';
  194. /* There is more data to be read */
  195. return TRUE;
  196. }
  197. static gboolean
  198. socket_in_event (GIOChannel *source,
  199. GIOCondition condition,
  200. LmOldSocket *socket)
  201. {
  202. gchar buf[IN_BUFFER_SIZE];
  203. gsize bytes_read = 0;
  204. gboolean read_anything = FALSE;
  205. gboolean hangup = 0;
  206. gint reason = 0;
  207. if (!socket->io_channel) {
  208. return FALSE;
  209. }
  210. while (socket_read_incoming (socket, buf, IN_BUFFER_SIZE,
  211. &bytes_read, &hangup, &reason)) {
  212. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "\nRECV [%d]:\n",
  213. (int)bytes_read);
  214. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  215. "-----------------------------------\n");
  216. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "'%s'\n", buf);
  217. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  218. "-----------------------------------\n");
  219. lm_verbose ("Read: %d chars\n", (int)bytes_read);
  220. (socket->data_func) (socket, buf, socket->user_data);
  221. read_anything = TRUE;
  222. }
  223. /* If we have read something, delay the hangup so that the data can be
  224. * processed. */
  225. if (hangup && !read_anything) {
  226. (socket->closed_func) (socket, reason, socket->user_data);
  227. return FALSE;
  228. }
  229. return TRUE;
  230. }
  231. static gboolean
  232. socket_hup_event (GIOChannel *source,
  233. GIOCondition condition,
  234. LmOldSocket *socket)
  235. {
  236. lm_verbose ("HUP event: %d->'%s'\n",
  237. condition, lm_misc_io_condition_to_str (condition));
  238. if (!socket->io_channel) {
  239. return FALSE;
  240. }
  241. (socket->closed_func) (socket, LM_DISCONNECT_REASON_HUP,
  242. socket->user_data);
  243. return TRUE;
  244. }
  245. static gboolean
  246. socket_error_event (GIOChannel *source,
  247. GIOCondition condition,
  248. LmOldSocket *socket)
  249. {
  250. lm_verbose ("ERROR event: %d->'%s'\n",
  251. condition, lm_misc_io_condition_to_str (condition));
  252. if (!socket->io_channel) {
  253. return FALSE;
  254. }
  255. (socket->closed_func) (socket, LM_DISCONNECT_REASON_ERROR,
  256. socket->user_data);
  257. return TRUE;
  258. }
  259. static gboolean
  260. _lm_old_socket_ssl_init (LmOldSocket *socket, gboolean delayed)
  261. {
  262. GError *error = NULL;
  263. const gchar *ssl_verify_domain = NULL;
  264. lm_verbose ("Setting up SSL...\n");
  265. _lm_ssl_initialize (socket->ssl);
  266. #ifdef HAVE_GNUTLS
  267. /* GNU TLS requires the socket to be blocking */
  268. _lm_sock_set_blocking (socket->fd, TRUE);
  269. #endif
  270. /* If we're using StartTLS, the correct thing is to verify against
  271. * the domain. If we're using old SSL, we should verify against the
  272. * hostname. */
  273. if (delayed)
  274. ssl_verify_domain = socket->domain;
  275. else
  276. ssl_verify_domain = socket->server;
  277. if (!_lm_ssl_begin (socket->ssl, socket->fd, ssl_verify_domain, &error)) {
  278. lm_verbose ("Could not begin SSL\n");
  279. if (error) {
  280. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  281. "%s\n", error->message);
  282. g_error_free (error);
  283. }
  284. _lm_sock_shutdown (socket->fd);
  285. _lm_sock_close (socket->fd);
  286. if (!delayed && socket->connect_func) {
  287. (socket->connect_func) (socket, FALSE, socket->user_data);
  288. }
  289. return FALSE;
  290. }
  291. #ifdef HAVE_GNUTLS
  292. _lm_sock_set_blocking (socket->fd, FALSE);
  293. #endif
  294. socket->ssl_started = TRUE;
  295. return TRUE;
  296. }
  297. gboolean
  298. lm_old_socket_starttls (LmOldSocket *socket)
  299. {
  300. g_return_val_if_fail (lm_ssl_get_use_starttls (socket->ssl) == TRUE, FALSE);
  301. return _lm_old_socket_ssl_init (socket, TRUE);
  302. }
  303. void
  304. _lm_old_socket_succeeded (LmConnectData *connect_data)
  305. {
  306. LmOldSocket *socket;
  307. socket = connect_data->socket;
  308. if (socket->watch_connect) {
  309. g_source_destroy (socket->watch_connect);
  310. socket->watch_connect = NULL;
  311. }
  312. /* Need some way to report error/success */
  313. if (socket->cancel_open) {
  314. lm_verbose ("Cancelling connection...\n");
  315. if (socket->connect_func) {
  316. (socket->connect_func) (socket, FALSE, socket->user_data);
  317. }
  318. return;
  319. }
  320. socket->fd = connect_data->fd;
  321. socket->io_channel = connect_data->io_channel;
  322. g_object_unref (socket->resolver);
  323. socket->resolver = NULL;
  324. socket->connect_data = NULL;
  325. g_free (connect_data);
  326. /* old-style ssl should be started immediately */
  327. if (socket->ssl && (lm_ssl_get_use_starttls (socket->ssl) == FALSE)) {
  328. if (!_lm_old_socket_ssl_init (socket, FALSE)) {
  329. return;
  330. }
  331. }
  332. socket->watch_in =
  333. lm_misc_add_io_watch (socket->context,
  334. socket->io_channel,
  335. G_IO_IN,
  336. (GIOFunc) socket_in_event,
  337. socket);
  338. /* FIXME: if we add these, we don't get ANY
  339. * response from the server, this is to do with the way that
  340. * windows handles watches, see bug #331214.
  341. */
  342. #ifndef G_OS_WIN32
  343. socket->watch_err =
  344. lm_misc_add_io_watch (socket->context,
  345. socket->io_channel,
  346. G_IO_ERR,
  347. (GIOFunc) socket_error_event,
  348. socket);
  349. socket->watch_hup =
  350. lm_misc_add_io_watch (socket->context,
  351. socket->io_channel,
  352. G_IO_HUP,
  353. (GIOFunc) socket_hup_event,
  354. socket);
  355. #endif
  356. if (socket->connect_func) {
  357. (socket->connect_func) (socket, TRUE, socket->user_data);
  358. }
  359. }
  360. gboolean
  361. _lm_old_socket_failed_with_error (LmConnectData *connect_data, int error)
  362. {
  363. LmOldSocket *socket;
  364. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  365. "Connection failed: %s (error %d)\n",
  366. _lm_sock_get_error_str (error), error);
  367. socket = lm_old_socket_ref (connect_data->socket);
  368. connect_data->current_addr = lm_resolver_results_get_next (socket->resolver);
  369. if (socket->watch_connect) {
  370. g_source_destroy (socket->watch_connect);
  371. socket->watch_connect = NULL;
  372. }
  373. if (connect_data->io_channel != NULL) {
  374. socket_close_io_channel (connect_data->io_channel);
  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) {
  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. goto out;
  426. }
  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. ((struct sockaddr_in *) addr->ai_addr)->sin_port = port;
  486. res = getnameinfo (addr->ai_addr,
  487. (socklen_t)addr->ai_addrlen,
  488. name, sizeof (name),
  489. portname, sizeof (portname),
  490. NI_NUMERICHOST | NI_NUMERICSERV);
  491. if (res < 0) {
  492. return _lm_old_socket_failed (connect_data);
  493. }
  494. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  495. "Trying %s port %s...\n", name, portname);
  496. fd = _lm_sock_makesocket (addr->ai_family,
  497. addr->ai_socktype,
  498. addr->ai_protocol);
  499. if (!_LM_SOCK_VALID (fd)) {
  500. g_print("invalid fd\n");
  501. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  502. "Failed making socket, error:%d...\n",
  503. _lm_sock_get_last_error ());
  504. return _lm_old_socket_failed (connect_data);
  505. }
  506. /* Even though it says _unix_new(), it is supported by glib on
  507. * win32 because glib does some cool stuff to find out if it
  508. * can treat it as a FD or a windows SOCKET.
  509. */
  510. connect_data->fd = fd;
  511. connect_data->io_channel = g_io_channel_unix_new (fd);
  512. g_io_channel_set_encoding (connect_data->io_channel, NULL, NULL);
  513. g_io_channel_set_buffered (connect_data->io_channel, FALSE);
  514. _lm_sock_set_blocking (connect_data->fd, FALSE);
  515. if (socket->proxy) {
  516. socket->watch_connect =
  517. lm_misc_add_io_watch (socket->context,
  518. connect_data->io_channel,
  519. G_IO_OUT|G_IO_ERR,
  520. (GIOFunc) _lm_proxy_connect_cb,
  521. connect_data);
  522. } else {
  523. socket->watch_connect =
  524. lm_misc_add_io_watch (socket->context,
  525. connect_data->io_channel,
  526. G_IO_OUT|G_IO_ERR,
  527. (GIOFunc) socket_connect_cb,
  528. connect_data);
  529. }
  530. res = _lm_sock_connect (connect_data->fd,
  531. addr->ai_addr, (int)addr->ai_addrlen);
  532. if (res < 0) {
  533. err = _lm_sock_get_last_error ();
  534. if (!_lm_sock_is_blocking_error (err)) {
  535. _lm_sock_close (connect_data->fd);
  536. g_print("unable to connect\n");
  537. return _lm_old_socket_failed_with_error (connect_data, err);
  538. }
  539. }
  540. return TRUE;
  541. }
  542. static gboolean
  543. old_socket_output_is_buffered (LmOldSocket *socket,
  544. const gchar *buffer,
  545. gint len)
  546. {
  547. if (socket->out_buf) {
  548. lm_verbose ("Appending %d bytes to output buffer\n", len);
  549. g_string_append_len (socket->out_buf, buffer, len);
  550. return TRUE;
  551. }
  552. return FALSE;
  553. }
  554. static void
  555. old_socket_setup_output_buffer (LmOldSocket *socket, const gchar *buffer, gint len)
  556. {
  557. lm_verbose ("OUTPUT BUFFER ENABLED\n");
  558. socket->out_buf = g_string_new_len (buffer, len);
  559. socket->watch_out =
  560. lm_misc_add_io_watch (socket->context,
  561. socket->io_channel,
  562. G_IO_OUT,
  563. (GIOFunc) socket_buffered_write_cb,
  564. socket);
  565. }
  566. static gboolean
  567. socket_buffered_write_cb (GIOChannel *source,
  568. GIOCondition condition,
  569. LmOldSocket *socket)
  570. {
  571. gint b_written;
  572. GString *out_buf;
  573. out_buf = socket->out_buf;
  574. if (!out_buf) {
  575. /* Should not be possible */
  576. return FALSE;
  577. }
  578. b_written = old_socket_do_write (socket, out_buf->str, out_buf->len);
  579. if (b_written < 0) {
  580. (socket->closed_func) (socket, LM_DISCONNECT_REASON_ERROR,
  581. socket->user_data);
  582. return FALSE;
  583. }
  584. g_string_erase (out_buf, 0, (gsize) b_written);
  585. if (out_buf->len == 0) {
  586. lm_verbose ("Output buffer is empty, going back to normal output\n");
  587. if (socket->watch_out) {
  588. g_source_destroy (socket->watch_out);
  589. socket->watch_out = NULL;
  590. }
  591. g_string_free (out_buf, TRUE);
  592. socket->out_buf = NULL;
  593. return FALSE;
  594. }
  595. return TRUE;
  596. }
  597. static void
  598. socket_close_io_channel (GIOChannel *io_channel)
  599. {
  600. gint fd;
  601. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
  602. "Freeing up IOChannel and file descriptor\n");
  603. fd = g_io_channel_unix_get_fd (io_channel);
  604. g_io_channel_unref (io_channel);
  605. _lm_sock_close (fd);
  606. }
  607. static void
  608. old_socket_resolver_host_cb (LmResolver *resolver,
  609. LmResolverResult result,
  610. gpointer user_data)
  611. {
  612. LmOldSocket *socket = (LmOldSocket *) user_data;
  613. char dispbuf[128];
  614. struct sockaddr_in *addr; /* FIXME:IPv6 */
  615. const char *converr;
  616. lm_verbose ("LmOldSocket::host_cb (result=%d)\n", result);
  617. if (result != LM_RESOLVER_RESULT_OK) {
  618. lm_verbose ("error while resolving, bailing out\n");
  619. if (socket->connect_func) {
  620. (socket->connect_func) (socket, FALSE, socket->user_data);
  621. }
  622. /*FIXME: Leaking Resolvers Until Clean Up Can Be Properly Handled
  623. g_object_unref (socket->resolver);
  624. socket->resolver = NULL;*/
  625. g_free (socket->connect_data);
  626. socket->connect_data = NULL;
  627. return;
  628. }
  629. socket->connect_data->current_addr =
  630. lm_resolver_results_get_next (resolver);
  631. if (socket->connect_data->current_addr) { /* FIXME:IPv6 */
  632. addr = (struct sockaddr_in *) (socket->connect_data->current_addr->ai_addr);
  633. converr = inet_ntop(AF_INET,&(addr->sin_addr),dispbuf,sizeof(dispbuf));
  634. if (converr) {
  635. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
  636. "Attempting Connection to %s\n",dispbuf);
  637. } else {
  638. g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
  639. "Attempting Connection (unable to convert address to presentable format)\n");
  640. };
  641. socket_do_connect (socket->connect_data);
  642. } else { /* FIXME: IPv6 Support? */
  643. g_log (LM_LOG_DOMAIN,G_LOG_LEVEL_ERROR,
  644. "Unable to locate server available over IPv4.\n");
  645. };
  646. /* FIXME: What do we do here? How to make the mainloop exit with an
  647. error, while having no ref to said mainloop */
  648. }
  649. /* FIXME: Need to have a way to only get srv reply and then decide if the
  650. * resolver should continue to look the host up.
  651. *
  652. * This is needed for the case when we do a SRV lookup to lookup the
  653. * real host of the service and then connect to it through a proxy.
  654. */
  655. static void
  656. old_socket_resolver_srv_cb (LmResolver *resolver,
  657. LmResolverResult result,
  658. gpointer user_data)
  659. {
  660. LmOldSocket *socket = (LmOldSocket *) user_data;
  661. const gchar *remote_addr;
  662. lm_verbose ("LmOldSocket::srv_cb (result=%d)\n", result);
  663. if (result != LM_RESOLVER_RESULT_OK) {
  664. lm_verbose ("SRV lookup failed, trying jid domain\n");
  665. socket->server = g_strdup (socket->domain);
  666. } else {
  667. g_object_get (resolver, "host", &socket->server, NULL);
  668. g_object_get (resolver, "port", &socket->port, NULL);
  669. }
  670. if (socket->proxy) {
  671. remote_addr = lm_proxy_get_server (socket->proxy);
  672. } else if (socket->server) {
  673. remote_addr = socket->server;
  674. }
  675. else {
  676. remote_addr = socket->domain;
  677. }
  678. g_object_unref (socket->resolver);
  679. socket->resolver =
  680. lm_resolver_new_for_host (remote_addr,
  681. old_socket_resolver_host_cb,
  682. socket);
  683. lm_resolver_lookup (socket->resolver);
  684. }
  685. LmOldSocket *
  686. lm_old_socket_create (GMainContext *context,
  687. IncomingDataFunc data_func,
  688. SocketClosedFunc closed_func,
  689. ConnectResultFunc connect_func,
  690. gpointer user_data,
  691. LmConnection *connection,
  692. const gchar *server,
  693. const gchar *domain,
  694. guint port,
  695. LmSSL *ssl,
  696. LmProxy *proxy,
  697. GError **error)
  698. {
  699. LmOldSocket *socket;
  700. LmConnectData *data;
  701. g_return_val_if_fail (domain != NULL, NULL);
  702. g_return_val_if_fail ((port >= LM_MIN_PORT && port <= LM_MAX_PORT), NULL);
  703. g_return_val_if_fail (data_func != NULL, NULL);
  704. g_return_val_if_fail (closed_func != NULL, NULL);
  705. g_return_val_if_fail (connect_func != NULL, NULL);
  706. socket = g_new0 (LmOldSocket, 1);
  707. socket->ref_count = 1;
  708. socket->connection = connection;
  709. socket->domain = g_strdup (domain);
  710. socket->server = g_strdup (server);
  711. socket->port = port;
  712. socket->cancel_open = FALSE;
  713. socket->ssl = ssl;
  714. socket->ssl_started = FALSE;
  715. socket->proxy = NULL;
  716. if (context) {
  717. socket->context = g_main_context_ref (context);
  718. }
  719. if (proxy) {
  720. socket->proxy = lm_proxy_ref (proxy);
  721. }
  722. data = g_new0 (LmConnectData, 1);
  723. data->socket = socket;
  724. data->connection = socket->connection;
  725. data->fd = -1;
  726. socket->connect_data = data;
  727. if (!server) {
  728. socket->resolver = lm_resolver_new_for_service (socket->domain,
  729. "xmpp-client",
  730. "tcp",
  731. old_socket_resolver_srv_cb,
  732. socket);
  733. } else {
  734. socket->resolver =
  735. lm_resolver_new_for_host (socket->server ? socket->server : socket->domain,
  736. old_socket_resolver_host_cb,
  737. socket);
  738. }
  739. if (socket->context) {
  740. g_object_set (socket->resolver, "context", context, NULL);
  741. }
  742. socket->data_func = data_func;
  743. socket->closed_func = closed_func;
  744. socket->connect_func = connect_func;
  745. socket->user_data = user_data;
  746. lm_resolver_lookup (socket->resolver);
  747. return socket;
  748. }
  749. void
  750. lm_old_socket_flush (LmOldSocket *socket)
  751. {
  752. g_return_if_fail (socket != NULL);
  753. g_return_if_fail (socket->io_channel != NULL);
  754. g_io_channel_flush (socket->io_channel, NULL);
  755. }
  756. void
  757. lm_old_socket_close (LmOldSocket *socket)
  758. {
  759. LmConnectData *data;
  760. g_return_if_fail (socket != NULL);
  761. if (socket->watch_connect) {
  762. g_source_destroy (socket->watch_connect);
  763. socket->watch_connect = NULL;
  764. }
  765. data = socket->connect_data;
  766. if (data) {
  767. socket->connect_data = NULL;
  768. g_free (data);
  769. }
  770. /* FIXME: Leaking Resolvers Until Clean Up Can Be Corrected
  771. if (socket->resolver) {
  772. g_object_unref (socket->resolver);
  773. socket->resolver = NULL;
  774. } */
  775. if (socket->io_channel) {
  776. if (socket->watch_in) {
  777. g_source_destroy (socket->watch_in);
  778. socket->watch_in = NULL;
  779. }
  780. if (socket->watch_err) {
  781. g_source_destroy (socket->watch_err);
  782. socket->watch_err = NULL;
  783. }
  784. if (socket->watch_hup) {
  785. g_source_destroy (socket->watch_hup);
  786. socket->watch_hup = NULL;
  787. }
  788. if (socket->watch_out) {
  789. g_source_destroy (socket->watch_out);
  790. socket->watch_out = NULL;
  791. }
  792. socket_close_io_channel (socket->io_channel);
  793. socket->io_channel = NULL;
  794. socket->fd = -1;
  795. }
  796. if (socket->ssl) {
  797. _lm_ssl_close (socket->ssl);
  798. }
  799. }
  800. gchar *
  801. lm_old_socket_get_local_host (LmOldSocket *socket)
  802. {
  803. return _lm_sock_get_local_host (socket->fd);
  804. }
  805. LmOldSocket *
  806. lm_old_socket_ref (LmOldSocket *socket)
  807. {
  808. g_return_val_if_fail (socket != NULL, NULL);
  809. socket->ref_count++;
  810. return socket;
  811. }
  812. void
  813. lm_old_socket_unref (LmOldSocket *socket)
  814. {
  815. g_return_if_fail (socket != NULL);
  816. socket->ref_count--;
  817. if (socket->ref_count <= 0) {
  818. socket_free (socket);
  819. }
  820. }
  821. gboolean
  822. lm_old_socket_set_keepalive (LmOldSocket *socket, int delay)
  823. {
  824. #ifdef USE_TCP_KEEPALIVES
  825. return _lm_sock_set_keepalive (socket->fd, delay);
  826. #else
  827. return FALSE;
  828. #endif /* USE_TCP_KEEPALIVES */
  829. }
  830. void
  831. lm_old_socket_asyncns_cancel (LmOldSocket *socket)
  832. {
  833. if (!socket->resolver) {
  834. return;
  835. }
  836. lm_resolver_cancel (socket->resolver);
  837. }
  838. gboolean
  839. lm_old_socket_get_use_starttls (LmOldSocket *socket)
  840. {
  841. if (!socket->ssl) {
  842. return FALSE;
  843. }
  844. return lm_ssl_get_use_starttls (socket->ssl);
  845. }
  846. gboolean
  847. lm_old_socket_get_require_starttls (LmOldSocket *socket)
  848. {
  849. if (!socket->ssl) {
  850. return FALSE;
  851. }
  852. return lm_ssl_get_require_starttls (socket->ssl);
  853. }