PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/gio/gsocketclient.c

https://gitlab.com/ImageMagick/glib
C | 1927 lines | 1078 code | 195 blank | 654 comment | 99 complexity | 7fffa86104485a6b930cb7544ee3b062 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /* GIO - GLib Input, Output and Streaming Library
  2. *
  3. * Copyright © 2008, 2009 codethink
  4. * Copyright © 2009 Red Hat, Inc
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General
  17. * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * Authors: Ryan Lortie <desrt@desrt.ca>
  20. * Alexander Larsson <alexl@redhat.com>
  21. */
  22. #include "config.h"
  23. #include "gsocketclient.h"
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <gio/gioenumtypes.h>
  27. #include <gio/gsocketaddressenumerator.h>
  28. #include <gio/gsocketconnectable.h>
  29. #include <gio/gsocketconnection.h>
  30. #include <gio/gioprivate.h>
  31. #include <gio/gproxyaddressenumerator.h>
  32. #include <gio/gproxyaddress.h>
  33. #include <gio/gtask.h>
  34. #include <gio/gcancellable.h>
  35. #include <gio/gioerror.h>
  36. #include <gio/gsocket.h>
  37. #include <gio/gnetworkaddress.h>
  38. #include <gio/gnetworkservice.h>
  39. #include <gio/gproxy.h>
  40. #include <gio/gproxyresolver.h>
  41. #include <gio/gsocketaddress.h>
  42. #include <gio/gtcpconnection.h>
  43. #include <gio/gtcpwrapperconnection.h>
  44. #include <gio/gtlscertificate.h>
  45. #include <gio/gtlsclientconnection.h>
  46. #include <gio/ginetaddress.h>
  47. #include "glibintl.h"
  48. /**
  49. * SECTION:gsocketclient
  50. * @short_description: Helper for connecting to a network service
  51. * @include: gio/gio.h
  52. * @see_also: #GSocketConnection, #GSocketListener
  53. *
  54. * #GSocketClient is a lightweight high-level utility class for connecting to
  55. * a network host using a connection oriented socket type.
  56. *
  57. * You create a #GSocketClient object, set any options you want, and then
  58. * call a sync or async connect operation, which returns a #GSocketConnection
  59. * subclass on success.
  60. *
  61. * The type of the #GSocketConnection object returned depends on the type of
  62. * the underlying socket that is in use. For instance, for a TCP/IP connection
  63. * it will be a #GTcpConnection.
  64. *
  65. * As #GSocketClient is a lightweight object, you don't need to cache it. You
  66. * can just create a new one any time you need one.
  67. *
  68. * Since: 2.22
  69. */
  70. enum
  71. {
  72. EVENT,
  73. LAST_SIGNAL
  74. };
  75. static guint signals[LAST_SIGNAL] = { 0 };
  76. enum
  77. {
  78. PROP_NONE,
  79. PROP_FAMILY,
  80. PROP_TYPE,
  81. PROP_PROTOCOL,
  82. PROP_LOCAL_ADDRESS,
  83. PROP_TIMEOUT,
  84. PROP_ENABLE_PROXY,
  85. PROP_TLS,
  86. PROP_TLS_VALIDATION_FLAGS,
  87. PROP_PROXY_RESOLVER
  88. };
  89. struct _GSocketClientPrivate
  90. {
  91. GSocketFamily family;
  92. GSocketType type;
  93. GSocketProtocol protocol;
  94. GSocketAddress *local_address;
  95. guint timeout;
  96. gboolean enable_proxy;
  97. GHashTable *app_proxies;
  98. gboolean tls;
  99. GTlsCertificateFlags tls_validation_flags;
  100. GProxyResolver *proxy_resolver;
  101. };
  102. G_DEFINE_TYPE_WITH_PRIVATE (GSocketClient, g_socket_client, G_TYPE_OBJECT)
  103. static GSocket *
  104. create_socket (GSocketClient *client,
  105. GSocketAddress *dest_address,
  106. GError **error)
  107. {
  108. GSocketFamily family;
  109. GSocket *socket;
  110. family = client->priv->family;
  111. if (family == G_SOCKET_FAMILY_INVALID &&
  112. client->priv->local_address != NULL)
  113. family = g_socket_address_get_family (client->priv->local_address);
  114. if (family == G_SOCKET_FAMILY_INVALID)
  115. family = g_socket_address_get_family (dest_address);
  116. socket = g_socket_new (family,
  117. client->priv->type,
  118. client->priv->protocol,
  119. error);
  120. if (socket == NULL)
  121. return NULL;
  122. if (client->priv->local_address)
  123. {
  124. if (!g_socket_bind (socket,
  125. client->priv->local_address,
  126. FALSE,
  127. error))
  128. {
  129. g_object_unref (socket);
  130. return NULL;
  131. }
  132. }
  133. if (client->priv->timeout)
  134. g_socket_set_timeout (socket, client->priv->timeout);
  135. return socket;
  136. }
  137. static gboolean
  138. can_use_proxy (GSocketClient *client)
  139. {
  140. GSocketClientPrivate *priv = client->priv;
  141. return priv->enable_proxy
  142. && priv->type == G_SOCKET_TYPE_STREAM;
  143. }
  144. static void
  145. clarify_connect_error (GError *error,
  146. GSocketConnectable *connectable,
  147. GSocketAddress *address)
  148. {
  149. const char *name;
  150. char *tmp_name = NULL;
  151. if (G_IS_PROXY_ADDRESS (address))
  152. {
  153. name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address)));
  154. g_prefix_error (&error, _("Could not connect to proxy server %s: "), name);
  155. }
  156. else
  157. {
  158. if (G_IS_NETWORK_ADDRESS (connectable))
  159. name = g_network_address_get_hostname (G_NETWORK_ADDRESS (connectable));
  160. else if (G_IS_NETWORK_SERVICE (connectable))
  161. name = g_network_service_get_domain (G_NETWORK_SERVICE (connectable));
  162. else if (G_IS_INET_SOCKET_ADDRESS (connectable))
  163. name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (connectable)));
  164. else
  165. name = NULL;
  166. if (name)
  167. g_prefix_error (&error, _("Could not connect to %s: "), name);
  168. else
  169. g_prefix_error (&error, _("Could not connect: "));
  170. }
  171. g_free (tmp_name);
  172. }
  173. static void
  174. g_socket_client_init (GSocketClient *client)
  175. {
  176. client->priv = g_socket_client_get_instance_private (client);
  177. client->priv->type = G_SOCKET_TYPE_STREAM;
  178. client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
  179. g_str_equal,
  180. g_free,
  181. NULL);
  182. }
  183. /**
  184. * g_socket_client_new:
  185. *
  186. * Creates a new #GSocketClient with the default options.
  187. *
  188. * Returns: a #GSocketClient.
  189. * Free the returned object with g_object_unref().
  190. *
  191. * Since: 2.22
  192. */
  193. GSocketClient *
  194. g_socket_client_new (void)
  195. {
  196. return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
  197. }
  198. static void
  199. g_socket_client_finalize (GObject *object)
  200. {
  201. GSocketClient *client = G_SOCKET_CLIENT (object);
  202. g_clear_object (&client->priv->local_address);
  203. g_clear_object (&client->priv->proxy_resolver);
  204. G_OBJECT_CLASS (g_socket_client_parent_class)->finalize (object);
  205. g_hash_table_unref (client->priv->app_proxies);
  206. }
  207. static void
  208. g_socket_client_get_property (GObject *object,
  209. guint prop_id,
  210. GValue *value,
  211. GParamSpec *pspec)
  212. {
  213. GSocketClient *client = G_SOCKET_CLIENT (object);
  214. switch (prop_id)
  215. {
  216. case PROP_FAMILY:
  217. g_value_set_enum (value, client->priv->family);
  218. break;
  219. case PROP_TYPE:
  220. g_value_set_enum (value, client->priv->type);
  221. break;
  222. case PROP_PROTOCOL:
  223. g_value_set_enum (value, client->priv->protocol);
  224. break;
  225. case PROP_LOCAL_ADDRESS:
  226. g_value_set_object (value, client->priv->local_address);
  227. break;
  228. case PROP_TIMEOUT:
  229. g_value_set_uint (value, client->priv->timeout);
  230. break;
  231. case PROP_ENABLE_PROXY:
  232. g_value_set_boolean (value, client->priv->enable_proxy);
  233. break;
  234. case PROP_TLS:
  235. g_value_set_boolean (value, g_socket_client_get_tls (client));
  236. break;
  237. case PROP_TLS_VALIDATION_FLAGS:
  238. g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
  239. break;
  240. case PROP_PROXY_RESOLVER:
  241. g_value_set_object (value, g_socket_client_get_proxy_resolver (client));
  242. break;
  243. default:
  244. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  245. }
  246. }
  247. static void
  248. g_socket_client_set_property (GObject *object,
  249. guint prop_id,
  250. const GValue *value,
  251. GParamSpec *pspec)
  252. {
  253. GSocketClient *client = G_SOCKET_CLIENT (object);
  254. switch (prop_id)
  255. {
  256. case PROP_FAMILY:
  257. g_socket_client_set_family (client, g_value_get_enum (value));
  258. break;
  259. case PROP_TYPE:
  260. g_socket_client_set_socket_type (client, g_value_get_enum (value));
  261. break;
  262. case PROP_PROTOCOL:
  263. g_socket_client_set_protocol (client, g_value_get_enum (value));
  264. break;
  265. case PROP_LOCAL_ADDRESS:
  266. g_socket_client_set_local_address (client, g_value_get_object (value));
  267. break;
  268. case PROP_TIMEOUT:
  269. g_socket_client_set_timeout (client, g_value_get_uint (value));
  270. break;
  271. case PROP_ENABLE_PROXY:
  272. g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
  273. break;
  274. case PROP_TLS:
  275. g_socket_client_set_tls (client, g_value_get_boolean (value));
  276. break;
  277. case PROP_TLS_VALIDATION_FLAGS:
  278. g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
  279. break;
  280. case PROP_PROXY_RESOLVER:
  281. g_socket_client_set_proxy_resolver (client, g_value_get_object (value));
  282. break;
  283. default:
  284. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  285. }
  286. }
  287. /**
  288. * g_socket_client_get_family:
  289. * @client: a #GSocketClient.
  290. *
  291. * Gets the socket family of the socket client.
  292. *
  293. * See g_socket_client_set_family() for details.
  294. *
  295. * Returns: a #GSocketFamily
  296. *
  297. * Since: 2.22
  298. */
  299. GSocketFamily
  300. g_socket_client_get_family (GSocketClient *client)
  301. {
  302. return client->priv->family;
  303. }
  304. /**
  305. * g_socket_client_set_family:
  306. * @client: a #GSocketClient.
  307. * @family: a #GSocketFamily
  308. *
  309. * Sets the socket family of the socket client.
  310. * If this is set to something other than %G_SOCKET_FAMILY_INVALID
  311. * then the sockets created by this object will be of the specified
  312. * family.
  313. *
  314. * This might be useful for instance if you want to force the local
  315. * connection to be an ipv4 socket, even though the address might
  316. * be an ipv6 mapped to ipv4 address.
  317. *
  318. * Since: 2.22
  319. */
  320. void
  321. g_socket_client_set_family (GSocketClient *client,
  322. GSocketFamily family)
  323. {
  324. if (client->priv->family == family)
  325. return;
  326. client->priv->family = family;
  327. g_object_notify (G_OBJECT (client), "family");
  328. }
  329. /**
  330. * g_socket_client_get_socket_type:
  331. * @client: a #GSocketClient.
  332. *
  333. * Gets the socket type of the socket client.
  334. *
  335. * See g_socket_client_set_socket_type() for details.
  336. *
  337. * Returns: a #GSocketFamily
  338. *
  339. * Since: 2.22
  340. */
  341. GSocketType
  342. g_socket_client_get_socket_type (GSocketClient *client)
  343. {
  344. return client->priv->type;
  345. }
  346. /**
  347. * g_socket_client_set_socket_type:
  348. * @client: a #GSocketClient.
  349. * @type: a #GSocketType
  350. *
  351. * Sets the socket type of the socket client.
  352. * The sockets created by this object will be of the specified
  353. * type.
  354. *
  355. * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
  356. * as GSocketClient is used for connection oriented services.
  357. *
  358. * Since: 2.22
  359. */
  360. void
  361. g_socket_client_set_socket_type (GSocketClient *client,
  362. GSocketType type)
  363. {
  364. if (client->priv->type == type)
  365. return;
  366. client->priv->type = type;
  367. g_object_notify (G_OBJECT (client), "type");
  368. }
  369. /**
  370. * g_socket_client_get_protocol:
  371. * @client: a #GSocketClient
  372. *
  373. * Gets the protocol name type of the socket client.
  374. *
  375. * See g_socket_client_set_protocol() for details.
  376. *
  377. * Returns: a #GSocketProtocol
  378. *
  379. * Since: 2.22
  380. */
  381. GSocketProtocol
  382. g_socket_client_get_protocol (GSocketClient *client)
  383. {
  384. return client->priv->protocol;
  385. }
  386. /**
  387. * g_socket_client_set_protocol:
  388. * @client: a #GSocketClient.
  389. * @protocol: a #GSocketProtocol
  390. *
  391. * Sets the protocol of the socket client.
  392. * The sockets created by this object will use of the specified
  393. * protocol.
  394. *
  395. * If @protocol is %0 that means to use the default
  396. * protocol for the socket family and type.
  397. *
  398. * Since: 2.22
  399. */
  400. void
  401. g_socket_client_set_protocol (GSocketClient *client,
  402. GSocketProtocol protocol)
  403. {
  404. if (client->priv->protocol == protocol)
  405. return;
  406. client->priv->protocol = protocol;
  407. g_object_notify (G_OBJECT (client), "protocol");
  408. }
  409. /**
  410. * g_socket_client_get_local_address:
  411. * @client: a #GSocketClient.
  412. *
  413. * Gets the local address of the socket client.
  414. *
  415. * See g_socket_client_set_local_address() for details.
  416. *
  417. * Returns: (transfer none): a #GSocketAddress or %NULL. Do not free.
  418. *
  419. * Since: 2.22
  420. */
  421. GSocketAddress *
  422. g_socket_client_get_local_address (GSocketClient *client)
  423. {
  424. return client->priv->local_address;
  425. }
  426. /**
  427. * g_socket_client_set_local_address:
  428. * @client: a #GSocketClient.
  429. * @address: (nullable): a #GSocketAddress, or %NULL
  430. *
  431. * Sets the local address of the socket client.
  432. * The sockets created by this object will bound to the
  433. * specified address (if not %NULL) before connecting.
  434. *
  435. * This is useful if you want to ensure that the local
  436. * side of the connection is on a specific port, or on
  437. * a specific interface.
  438. *
  439. * Since: 2.22
  440. */
  441. void
  442. g_socket_client_set_local_address (GSocketClient *client,
  443. GSocketAddress *address)
  444. {
  445. if (address)
  446. g_object_ref (address);
  447. if (client->priv->local_address)
  448. {
  449. g_object_unref (client->priv->local_address);
  450. }
  451. client->priv->local_address = address;
  452. g_object_notify (G_OBJECT (client), "local-address");
  453. }
  454. /**
  455. * g_socket_client_get_timeout:
  456. * @client: a #GSocketClient
  457. *
  458. * Gets the I/O timeout time for sockets created by @client.
  459. *
  460. * See g_socket_client_set_timeout() for details.
  461. *
  462. * Returns: the timeout in seconds
  463. *
  464. * Since: 2.26
  465. */
  466. guint
  467. g_socket_client_get_timeout (GSocketClient *client)
  468. {
  469. return client->priv->timeout;
  470. }
  471. /**
  472. * g_socket_client_set_timeout:
  473. * @client: a #GSocketClient.
  474. * @timeout: the timeout
  475. *
  476. * Sets the I/O timeout for sockets created by @client. @timeout is a
  477. * time in seconds, or 0 for no timeout (the default).
  478. *
  479. * The timeout value affects the initial connection attempt as well,
  480. * so setting this may cause calls to g_socket_client_connect(), etc,
  481. * to fail with %G_IO_ERROR_TIMED_OUT.
  482. *
  483. * Since: 2.26
  484. */
  485. void
  486. g_socket_client_set_timeout (GSocketClient *client,
  487. guint timeout)
  488. {
  489. if (client->priv->timeout == timeout)
  490. return;
  491. client->priv->timeout = timeout;
  492. g_object_notify (G_OBJECT (client), "timeout");
  493. }
  494. /**
  495. * g_socket_client_get_enable_proxy:
  496. * @client: a #GSocketClient.
  497. *
  498. * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
  499. *
  500. * Returns: whether proxying is enabled
  501. *
  502. * Since: 2.26
  503. */
  504. gboolean
  505. g_socket_client_get_enable_proxy (GSocketClient *client)
  506. {
  507. return client->priv->enable_proxy;
  508. }
  509. /**
  510. * g_socket_client_set_enable_proxy:
  511. * @client: a #GSocketClient.
  512. * @enable: whether to enable proxies
  513. *
  514. * Sets whether or not @client attempts to make connections via a
  515. * proxy server. When enabled (the default), #GSocketClient will use a
  516. * #GProxyResolver to determine if a proxy protocol such as SOCKS is
  517. * needed, and automatically do the necessary proxy negotiation.
  518. *
  519. * See also g_socket_client_set_proxy_resolver().
  520. *
  521. * Since: 2.26
  522. */
  523. void
  524. g_socket_client_set_enable_proxy (GSocketClient *client,
  525. gboolean enable)
  526. {
  527. enable = !!enable;
  528. if (client->priv->enable_proxy == enable)
  529. return;
  530. client->priv->enable_proxy = enable;
  531. g_object_notify (G_OBJECT (client), "enable-proxy");
  532. }
  533. /**
  534. * g_socket_client_get_tls:
  535. * @client: a #GSocketClient.
  536. *
  537. * Gets whether @client creates TLS connections. See
  538. * g_socket_client_set_tls() for details.
  539. *
  540. * Returns: whether @client uses TLS
  541. *
  542. * Since: 2.28
  543. */
  544. gboolean
  545. g_socket_client_get_tls (GSocketClient *client)
  546. {
  547. return client->priv->tls;
  548. }
  549. /**
  550. * g_socket_client_set_tls:
  551. * @client: a #GSocketClient.
  552. * @tls: whether to use TLS
  553. *
  554. * Sets whether @client creates TLS (aka SSL) connections. If @tls is
  555. * %TRUE, @client will wrap its connections in a #GTlsClientConnection
  556. * and perform a TLS handshake when connecting.
  557. *
  558. * Note that since #GSocketClient must return a #GSocketConnection,
  559. * but #GTlsClientConnection is not a #GSocketConnection, this
  560. * actually wraps the resulting #GTlsClientConnection in a
  561. * #GTcpWrapperConnection when returning it. You can use
  562. * g_tcp_wrapper_connection_get_base_io_stream() on the return value
  563. * to extract the #GTlsClientConnection.
  564. *
  565. * If you need to modify the behavior of the TLS handshake (eg, by
  566. * setting a client-side certificate to use, or connecting to the
  567. * #GTlsConnection::accept-certificate signal), you can connect to
  568. * @client's #GSocketClient::event signal and wait for it to be
  569. * emitted with %G_SOCKET_CLIENT_TLS_HANDSHAKING, which will give you
  570. * a chance to see the #GTlsClientConnection before the handshake
  571. * starts.
  572. *
  573. * Since: 2.28
  574. */
  575. void
  576. g_socket_client_set_tls (GSocketClient *client,
  577. gboolean tls)
  578. {
  579. tls = !!tls;
  580. if (tls == client->priv->tls)
  581. return;
  582. client->priv->tls = tls;
  583. g_object_notify (G_OBJECT (client), "tls");
  584. }
  585. /**
  586. * g_socket_client_get_tls_validation_flags:
  587. * @client: a #GSocketClient.
  588. *
  589. * Gets the TLS validation flags used creating TLS connections via
  590. * @client.
  591. *
  592. * Returns: the TLS validation flags
  593. *
  594. * Since: 2.28
  595. */
  596. GTlsCertificateFlags
  597. g_socket_client_get_tls_validation_flags (GSocketClient *client)
  598. {
  599. return client->priv->tls_validation_flags;
  600. }
  601. /**
  602. * g_socket_client_set_tls_validation_flags:
  603. * @client: a #GSocketClient.
  604. * @flags: the validation flags
  605. *
  606. * Sets the TLS validation flags used when creating TLS connections
  607. * via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
  608. *
  609. * Since: 2.28
  610. */
  611. void
  612. g_socket_client_set_tls_validation_flags (GSocketClient *client,
  613. GTlsCertificateFlags flags)
  614. {
  615. if (client->priv->tls_validation_flags != flags)
  616. {
  617. client->priv->tls_validation_flags = flags;
  618. g_object_notify (G_OBJECT (client), "tls-validation-flags");
  619. }
  620. }
  621. /**
  622. * g_socket_client_get_proxy_resolver:
  623. * @client: a #GSocketClient.
  624. *
  625. * Gets the #GProxyResolver being used by @client. Normally, this will
  626. * be the resolver returned by g_proxy_resolver_get_default(), but you
  627. * can override it with g_socket_client_set_proxy_resolver().
  628. *
  629. * Returns: (transfer none): The #GProxyResolver being used by
  630. * @client.
  631. *
  632. * Since: 2.36
  633. */
  634. GProxyResolver *
  635. g_socket_client_get_proxy_resolver (GSocketClient *client)
  636. {
  637. if (client->priv->proxy_resolver)
  638. return client->priv->proxy_resolver;
  639. else
  640. return g_proxy_resolver_get_default ();
  641. }
  642. /**
  643. * g_socket_client_set_proxy_resolver:
  644. * @client: a #GSocketClient.
  645. * @proxy_resolver: (nullable): a #GProxyResolver, or %NULL for the
  646. * default.
  647. *
  648. * Overrides the #GProxyResolver used by @client. You can call this if
  649. * you want to use specific proxies, rather than using the system
  650. * default proxy settings.
  651. *
  652. * Note that whether or not the proxy resolver is actually used
  653. * depends on the setting of #GSocketClient:enable-proxy, which is not
  654. * changed by this function (but which is %TRUE by default)
  655. *
  656. * Since: 2.36
  657. */
  658. void
  659. g_socket_client_set_proxy_resolver (GSocketClient *client,
  660. GProxyResolver *proxy_resolver)
  661. {
  662. /* We have to be careful to avoid calling
  663. * g_proxy_resolver_get_default() until we're sure we need it,
  664. * because trying to load the default proxy resolver module will
  665. * break some test programs that aren't expecting it (eg,
  666. * tests/gsettings).
  667. */
  668. if (client->priv->proxy_resolver)
  669. g_object_unref (client->priv->proxy_resolver);
  670. client->priv->proxy_resolver = proxy_resolver;
  671. if (client->priv->proxy_resolver)
  672. g_object_ref (client->priv->proxy_resolver);
  673. }
  674. static void
  675. g_socket_client_class_init (GSocketClientClass *class)
  676. {
  677. GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  678. gobject_class->finalize = g_socket_client_finalize;
  679. gobject_class->set_property = g_socket_client_set_property;
  680. gobject_class->get_property = g_socket_client_get_property;
  681. /**
  682. * GSocketClient::event:
  683. * @client: the #GSocketClient
  684. * @event: the event that is occurring
  685. * @connectable: the #GSocketConnectable that @event is occurring on
  686. * @connection: (nullable): the current representation of the connection
  687. *
  688. * Emitted when @client's activity on @connectable changes state.
  689. * Among other things, this can be used to provide progress
  690. * information about a network connection in the UI. The meanings of
  691. * the different @event values are as follows:
  692. *
  693. * - %G_SOCKET_CLIENT_RESOLVING: @client is about to look up @connectable
  694. * in DNS. @connection will be %NULL.
  695. *
  696. * - %G_SOCKET_CLIENT_RESOLVED: @client has successfully resolved
  697. * @connectable in DNS. @connection will be %NULL.
  698. *
  699. * - %G_SOCKET_CLIENT_CONNECTING: @client is about to make a connection
  700. * to a remote host; either a proxy server or the destination server
  701. * itself. @connection is the #GSocketConnection, which is not yet
  702. * connected. Since GLib 2.40, you can access the remote
  703. * address via g_socket_connection_get_remote_address().
  704. *
  705. * - %G_SOCKET_CLIENT_CONNECTED: @client has successfully connected
  706. * to a remote host. @connection is the connected #GSocketConnection.
  707. *
  708. * - %G_SOCKET_CLIENT_PROXY_NEGOTIATING: @client is about to negotiate
  709. * with a proxy to get it to connect to @connectable. @connection is
  710. * the #GSocketConnection to the proxy server.
  711. *
  712. * - %G_SOCKET_CLIENT_PROXY_NEGOTIATED: @client has negotiated a
  713. * connection to @connectable through a proxy server. @connection is
  714. * the stream returned from g_proxy_connect(), which may or may not
  715. * be a #GSocketConnection.
  716. *
  717. * - %G_SOCKET_CLIENT_TLS_HANDSHAKING: @client is about to begin a TLS
  718. * handshake. @connection is a #GTlsClientConnection.
  719. *
  720. * - %G_SOCKET_CLIENT_TLS_HANDSHAKED: @client has successfully completed
  721. * the TLS handshake. @connection is a #GTlsClientConnection.
  722. *
  723. * - %G_SOCKET_CLIENT_COMPLETE: @client has either successfully connected
  724. * to @connectable (in which case @connection is the #GSocketConnection
  725. * that it will be returning to the caller) or has failed (in which
  726. * case @connection is %NULL and the client is about to return an error).
  727. *
  728. * Each event except %G_SOCKET_CLIENT_COMPLETE may be emitted
  729. * multiple times (or not at all) for a given connectable (in
  730. * particular, if @client ends up attempting to connect to more than
  731. * one address). However, if @client emits the #GSocketClient::event
  732. * signal at all for a given connectable, that it will always emit
  733. * it with %G_SOCKET_CLIENT_COMPLETE when it is done.
  734. *
  735. * Note that there may be additional #GSocketClientEvent values in
  736. * the future; unrecognized @event values should be ignored.
  737. *
  738. * Since: 2.32
  739. */
  740. signals[EVENT] =
  741. g_signal_new (I_("event"),
  742. G_TYPE_FROM_CLASS (gobject_class),
  743. G_SIGNAL_RUN_LAST,
  744. G_STRUCT_OFFSET (GSocketClientClass, event),
  745. NULL, NULL,
  746. NULL,
  747. G_TYPE_NONE, 3,
  748. G_TYPE_SOCKET_CLIENT_EVENT,
  749. G_TYPE_SOCKET_CONNECTABLE,
  750. G_TYPE_IO_STREAM);
  751. g_object_class_install_property (gobject_class, PROP_FAMILY,
  752. g_param_spec_enum ("family",
  753. P_("Socket family"),
  754. P_("The sockets address family to use for socket construction"),
  755. G_TYPE_SOCKET_FAMILY,
  756. G_SOCKET_FAMILY_INVALID,
  757. G_PARAM_CONSTRUCT |
  758. G_PARAM_READWRITE |
  759. G_PARAM_STATIC_STRINGS));
  760. g_object_class_install_property (gobject_class, PROP_TYPE,
  761. g_param_spec_enum ("type",
  762. P_("Socket type"),
  763. P_("The sockets type to use for socket construction"),
  764. G_TYPE_SOCKET_TYPE,
  765. G_SOCKET_TYPE_STREAM,
  766. G_PARAM_CONSTRUCT |
  767. G_PARAM_READWRITE |
  768. G_PARAM_STATIC_STRINGS));
  769. g_object_class_install_property (gobject_class, PROP_PROTOCOL,
  770. g_param_spec_enum ("protocol",
  771. P_("Socket protocol"),
  772. P_("The protocol to use for socket construction, or 0 for default"),
  773. G_TYPE_SOCKET_PROTOCOL,
  774. G_SOCKET_PROTOCOL_DEFAULT,
  775. G_PARAM_CONSTRUCT |
  776. G_PARAM_READWRITE |
  777. G_PARAM_STATIC_STRINGS));
  778. g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
  779. g_param_spec_object ("local-address",
  780. P_("Local address"),
  781. P_("The local address constructed sockets will be bound to"),
  782. G_TYPE_SOCKET_ADDRESS,
  783. G_PARAM_CONSTRUCT |
  784. G_PARAM_READWRITE |
  785. G_PARAM_STATIC_STRINGS));
  786. g_object_class_install_property (gobject_class, PROP_TIMEOUT,
  787. g_param_spec_uint ("timeout",
  788. P_("Socket timeout"),
  789. P_("The I/O timeout for sockets, or 0 for none"),
  790. 0, G_MAXUINT, 0,
  791. G_PARAM_CONSTRUCT |
  792. G_PARAM_READWRITE |
  793. G_PARAM_STATIC_STRINGS));
  794. g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
  795. g_param_spec_boolean ("enable-proxy",
  796. P_("Enable proxy"),
  797. P_("Enable proxy support"),
  798. TRUE,
  799. G_PARAM_CONSTRUCT |
  800. G_PARAM_READWRITE |
  801. G_PARAM_STATIC_STRINGS));
  802. g_object_class_install_property (gobject_class, PROP_TLS,
  803. g_param_spec_boolean ("tls",
  804. P_("TLS"),
  805. P_("Whether to create TLS connections"),
  806. FALSE,
  807. G_PARAM_CONSTRUCT |
  808. G_PARAM_READWRITE |
  809. G_PARAM_STATIC_STRINGS));
  810. g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
  811. g_param_spec_flags ("tls-validation-flags",
  812. P_("TLS validation flags"),
  813. P_("TLS validation flags to use"),
  814. G_TYPE_TLS_CERTIFICATE_FLAGS,
  815. G_TLS_CERTIFICATE_VALIDATE_ALL,
  816. G_PARAM_CONSTRUCT |
  817. G_PARAM_READWRITE |
  818. G_PARAM_STATIC_STRINGS));
  819. /**
  820. * GSocketClient:proxy-resolver:
  821. *
  822. * The proxy resolver to use
  823. *
  824. * Since: 2.36
  825. */
  826. g_object_class_install_property (gobject_class, PROP_PROXY_RESOLVER,
  827. g_param_spec_object ("proxy-resolver",
  828. P_("Proxy resolver"),
  829. P_("The proxy resolver to use"),
  830. G_TYPE_PROXY_RESOLVER,
  831. G_PARAM_CONSTRUCT |
  832. G_PARAM_READWRITE |
  833. G_PARAM_STATIC_STRINGS));
  834. }
  835. static void
  836. g_socket_client_emit_event (GSocketClient *client,
  837. GSocketClientEvent event,
  838. GSocketConnectable *connectable,
  839. GIOStream *connection)
  840. {
  841. g_signal_emit (client, signals[EVENT], 0,
  842. event, connectable, connection);
  843. }
  844. /**
  845. * g_socket_client_connect:
  846. * @client: a #GSocketClient.
  847. * @connectable: a #GSocketConnectable specifying the remote address.
  848. * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
  849. * @error: #GError for error reporting, or %NULL to ignore.
  850. *
  851. * Tries to resolve the @connectable and make a network connection to it.
  852. *
  853. * Upon a successful connection, a new #GSocketConnection is constructed
  854. * and returned. The caller owns this new object and must drop their
  855. * reference to it when finished with it.
  856. *
  857. * The type of the #GSocketConnection object returned depends on the type of
  858. * the underlying socket that is used. For instance, for a TCP/IP connection
  859. * it will be a #GTcpConnection.
  860. *
  861. * The socket created will be the same family as the address that the
  862. * @connectable resolves to, unless family is set with g_socket_client_set_family()
  863. * or indirectly via g_socket_client_set_local_address(). The socket type
  864. * defaults to %G_SOCKET_TYPE_STREAM but can be set with
  865. * g_socket_client_set_socket_type().
  866. *
  867. * If a local address is specified with g_socket_client_set_local_address() the
  868. * socket will be bound to this address before connecting.
  869. *
  870. * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
  871. *
  872. * Since: 2.22
  873. */
  874. GSocketConnection *
  875. g_socket_client_connect (GSocketClient *client,
  876. GSocketConnectable *connectable,
  877. GCancellable *cancellable,
  878. GError **error)
  879. {
  880. GIOStream *connection = NULL;
  881. GSocketAddressEnumerator *enumerator = NULL;
  882. GError *last_error, *tmp_error;
  883. last_error = NULL;
  884. if (can_use_proxy (client))
  885. {
  886. enumerator = g_socket_connectable_proxy_enumerate (connectable);
  887. if (client->priv->proxy_resolver &&
  888. G_IS_PROXY_ADDRESS_ENUMERATOR (enumerator))
  889. {
  890. g_object_set (G_OBJECT (enumerator),
  891. "proxy-resolver", client->priv->proxy_resolver,
  892. NULL);
  893. }
  894. }
  895. else
  896. enumerator = g_socket_connectable_enumerate (connectable);
  897. while (connection == NULL)
  898. {
  899. GSocketAddress *address = NULL;
  900. gboolean application_proxy = FALSE;
  901. GSocket *socket;
  902. gboolean using_proxy;
  903. if (g_cancellable_is_cancelled (cancellable))
  904. {
  905. g_clear_error (error);
  906. g_cancellable_set_error_if_cancelled (cancellable, error);
  907. break;
  908. }
  909. tmp_error = NULL;
  910. g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVING,
  911. connectable, NULL);
  912. address = g_socket_address_enumerator_next (enumerator, cancellable,
  913. &tmp_error);
  914. if (address == NULL)
  915. {
  916. if (tmp_error)
  917. {
  918. g_clear_error (&last_error);
  919. g_propagate_error (error, tmp_error);
  920. }
  921. else if (last_error)
  922. {
  923. g_propagate_error (error, last_error);
  924. }
  925. else
  926. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
  927. _("Unknown error on connect"));
  928. break;
  929. }
  930. g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVED,
  931. connectable, NULL);
  932. using_proxy = (G_IS_PROXY_ADDRESS (address) &&
  933. client->priv->enable_proxy);
  934. /* clear error from previous attempt */
  935. g_clear_error (&last_error);
  936. socket = create_socket (client, address, &last_error);
  937. if (socket == NULL)
  938. {
  939. g_object_unref (address);
  940. continue;
  941. }
  942. connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
  943. g_socket_connection_set_cached_remote_address ((GSocketConnection*)connection, address);
  944. g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTING, connectable, connection);
  945. if (g_socket_connection_connect (G_SOCKET_CONNECTION (connection),
  946. address, cancellable, &last_error))
  947. {
  948. g_socket_connection_set_cached_remote_address ((GSocketConnection*)connection, NULL);
  949. g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTED, connectable, connection);
  950. }
  951. else
  952. {
  953. clarify_connect_error (last_error, connectable, address);
  954. g_object_unref (connection);
  955. connection = NULL;
  956. }
  957. if (connection && using_proxy)
  958. {
  959. GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
  960. const gchar *protocol;
  961. GProxy *proxy;
  962. protocol = g_proxy_address_get_protocol (proxy_addr);
  963. /* The connection should not be anything else then TCP Connection,
  964. * but let's put a safety guard in case
  965. */
  966. if (!G_IS_TCP_CONNECTION (connection))
  967. {
  968. g_critical ("Trying to proxy over non-TCP connection, this is "
  969. "most likely a bug in GLib IO library.");
  970. g_set_error_literal (&last_error,
  971. G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  972. _("Proxying over a non-TCP connection is not supported."));
  973. g_object_unref (connection);
  974. connection = NULL;
  975. }
  976. else if (g_hash_table_contains (client->priv->app_proxies, protocol))
  977. {
  978. application_proxy = TRUE;
  979. }
  980. else if ((proxy = g_proxy_get_default_for_protocol (protocol)))
  981. {
  982. GIOStream *proxy_connection;
  983. g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, connectable, connection);
  984. proxy_connection = g_proxy_connect (proxy,
  985. connection,
  986. proxy_addr,
  987. cancellable,
  988. &last_error);
  989. g_object_unref (connection);
  990. connection = proxy_connection;
  991. g_object_unref (proxy);
  992. if (connection)
  993. g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, connectable, connection);
  994. }
  995. else
  996. {
  997. g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  998. _("Proxy protocol “%s” is not supported."),
  999. protocol);
  1000. g_object_unref (connection);
  1001. connection = NULL;
  1002. }
  1003. }
  1004. if (!application_proxy && connection && client->priv->tls)
  1005. {
  1006. GIOStream *tlsconn;
  1007. tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
  1008. g_object_unref (connection);
  1009. connection = tlsconn;
  1010. if (tlsconn)
  1011. {
  1012. g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
  1013. client->priv->tls_validation_flags);
  1014. g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKING, connectable, connection);
  1015. if (g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
  1016. cancellable, &last_error))
  1017. {
  1018. g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKED, connectable, connection);
  1019. }
  1020. else
  1021. {
  1022. g_object_unref (tlsconn);
  1023. connection = NULL;
  1024. }
  1025. }
  1026. }
  1027. if (connection && !G_IS_SOCKET_CONNECTION (connection))
  1028. {
  1029. GSocketConnection *wrapper_connection;
  1030. wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
  1031. g_object_unref (connection);
  1032. connection = (GIOStream *)wrapper_connection;
  1033. }
  1034. g_object_unref (socket);
  1035. g_object_unref (address);
  1036. }
  1037. g_object_unref (enumerator);
  1038. g_socket_client_emit_event (client, G_SOCKET_CLIENT_COMPLETE, connectable, connection);
  1039. return G_SOCKET_CONNECTION (connection);
  1040. }
  1041. /**
  1042. * g_socket_client_connect_to_host:
  1043. * @client: a #GSocketClient
  1044. * @host_and_port: the name and optionally port of the host to connect to
  1045. * @default_port: the default port to connect to
  1046. * @cancellable: (nullable): a #GCancellable, or %NULL
  1047. * @error: a pointer to a #GError, or %NULL
  1048. *
  1049. * This is a helper function for g_socket_client_connect().
  1050. *
  1051. * Attempts to create a TCP connection to the named host.
  1052. *
  1053. * @host_and_port may be in any of a number of recognized formats; an IPv6
  1054. * address, an IPv4 address, or a domain name (in which case a DNS
  1055. * lookup is performed). Quoting with [] is supported for all address
  1056. * types. A port override may be specified in the usual way with a
  1057. * colon. Ports may be given as decimal numbers or symbolic names (in
  1058. * which case an /etc/services lookup is performed).
  1059. *
  1060. * If no port override is given in @host_and_port then @default_port will be
  1061. * used as the port number to connect to.
  1062. *
  1063. * In general, @host_and_port is expected to be provided by the user (allowing
  1064. * them to give the hostname, and a port override if necessary) and
  1065. * @default_port is expected to be provided by the application.
  1066. *
  1067. * In the case that an IP address is given, a single connection
  1068. * attempt is made. In the case that a name is given, multiple
  1069. * connection attempts may be made, in turn and according to the
  1070. * number of address records in DNS, until a connection succeeds.
  1071. *
  1072. * Upon a successful connection, a new #GSocketConnection is constructed
  1073. * and returned. The caller owns this new object and must drop their
  1074. * reference to it when finished with it.
  1075. *
  1076. * In the event of any failure (DNS error, service not found, no hosts
  1077. * connectable) %NULL is returned and @error (if non-%NULL) is set
  1078. * accordingly.
  1079. *
  1080. * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
  1081. *
  1082. * Since: 2.22
  1083. */
  1084. GSocketConnection *
  1085. g_socket_client_connect_to_host (GSocketClient *client,
  1086. const gchar *host_and_port,
  1087. guint16 default_port,
  1088. GCancellable *cancellable,
  1089. GError **error)
  1090. {
  1091. GSocketConnectable *connectable;
  1092. GSocketConnection *connection;
  1093. connectable = g_network_address_parse (host_and_port, default_port, error);
  1094. if (connectable == NULL)
  1095. return NULL;
  1096. connection = g_socket_client_connect (client, connectable,
  1097. cancellable, error);
  1098. g_object_unref (connectable);
  1099. return connection;
  1100. }
  1101. /**
  1102. * g_socket_client_connect_to_service:
  1103. * @client: a #GSocketConnection
  1104. * @domain: a domain name
  1105. * @service: the name of the service to connect to
  1106. * @cancellable: (nullable): a #GCancellable, or %NULL
  1107. * @error: a pointer to a #GError, or %NULL
  1108. *
  1109. * Attempts to create a TCP connection to a service.
  1110. *
  1111. * This call looks up the SRV record for @service at @domain for the
  1112. * "tcp" protocol. It then attempts to connect, in turn, to each of
  1113. * the hosts providing the service until either a connection succeeds
  1114. * or there are no hosts remaining.
  1115. *
  1116. * Upon a successful connection, a new #GSocketConnection is constructed
  1117. * and returned. The caller owns this new object and must drop their
  1118. * reference to it when finished with it.
  1119. *
  1120. * In the event of any failure (DNS error, service not found, no hosts
  1121. * connectable) %NULL is returned and @error (if non-%NULL) is set
  1122. * accordingly.
  1123. *
  1124. * Returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
  1125. */
  1126. GSocketConnection *
  1127. g_socket_client_connect_to_service (GSocketClient *client,
  1128. const gchar *domain,
  1129. const gchar *service,
  1130. GCancellable *cancellable,
  1131. GError **error)
  1132. {
  1133. GSocketConnectable *connectable;
  1134. GSocketConnection *connection;
  1135. connectable = g_network_service_new (service, "tcp", domain);
  1136. connection = g_socket_client_connect (client, connectable,
  1137. cancellable, error);
  1138. g_object_unref (connectable);
  1139. return connection;
  1140. }
  1141. /**
  1142. * g_socket_client_connect_to_uri:
  1143. * @client: a #GSocketClient
  1144. * @uri: A network URI
  1145. * @default_port: the default port to connect to
  1146. * @cancellable: (nullable): a #GCancellable, or %NULL
  1147. * @error: a pointer to a #GError, or %NULL
  1148. *
  1149. * This is a helper function for g_socket_client_connect().
  1150. *
  1151. * Attempts to create a TCP connection with a network URI.
  1152. *
  1153. * @uri may be any valid URI containing an "authority" (hostname/port)
  1154. * component. If a port is not specified in the URI, @default_port
  1155. * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
  1156. * (#GSocketClient does not know to automatically assume TLS for
  1157. * certain URI schemes.)
  1158. *
  1159. * Using this rather than g_socket_client_connect() or
  1160. * g_socket_client_connect_to_host() allows #GSocketClient to
  1161. * determine when to use application-specific proxy protocols.
  1162. *
  1163. * Upon a successful connection, a new #GSocketConnection is constructed
  1164. * and returned. The caller owns this new object and must drop their
  1165. * reference to it when finished with it.
  1166. *
  1167. * In the event of any failure (DNS error, service not found, no hosts
  1168. * connectable) %NULL is returned and @error (if non-%NULL) is set
  1169. * accordingly.
  1170. *
  1171. * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
  1172. *
  1173. * Since: 2.26
  1174. */
  1175. GSocketConnection *
  1176. g_socket_client_connect_to_uri (GSocketClient *client,
  1177. const gchar *uri,
  1178. guint16 default_port,
  1179. GCancellable *cancellable,
  1180. GError **error)
  1181. {
  1182. GSocketConnectable *connectable;
  1183. GSocketConnection *connection;
  1184. connectable = g_network_address_parse_uri (uri, default_port, error);
  1185. if (connectable == NULL)
  1186. return NULL;
  1187. connection = g_socket_client_connect (client, connectable,
  1188. cancellable, error);
  1189. g_object_unref (connectable);
  1190. return connection;
  1191. }
  1192. typedef struct
  1193. {
  1194. GTask *task;
  1195. GSocketClient *client;
  1196. GSocketConnectable *connectable;
  1197. GSocketAddressEnumerator *enumerator;
  1198. GProxyAddress *proxy_addr;
  1199. GSocketAddress *current_addr;
  1200. GSocket *current_socket;
  1201. GIOStream *connection;
  1202. GError *last_error;
  1203. } GSocketClientAsyncConnectData;
  1204. static void
  1205. g_socket_client_async_connect_data_free (GSocketClientAsyncConnectData *data)
  1206. {
  1207. g_clear_object (&data->connectable);
  1208. g_clear_object (&data->enumerator);
  1209. g_clear_object (&data->proxy_addr);
  1210. g_clear_object (&data->current_addr);
  1211. g_clear_object (&data->current_socket);
  1212. g_clear_object (&data->connection);
  1213. g_clear_error (&data->last_error);
  1214. g_slice_free (GSocketClientAsyncConnectData, data);
  1215. }
  1216. static void
  1217. g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
  1218. {
  1219. g_assert (data->connection);
  1220. if (!G_IS_SOCKET_CONNECTION (data->connection))
  1221. {
  1222. GSocketConnection *wrapper_connection;
  1223. wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
  1224. data->current_socket);
  1225. g_object_unref (data->connection);
  1226. data->connection = (GIOStream *)wrapper_connection;
  1227. }
  1228. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, data->connection);
  1229. g_task_return_pointer (data->task, data->connection, g_object_unref);
  1230. data->connection = NULL;
  1231. g_object_unref (data->task);
  1232. }
  1233. static void
  1234. g_socket_client_enumerator_callback (GObject *object,
  1235. GAsyncResult *result,
  1236. gpointer user_data);
  1237. static void
  1238. set_last_error (GSocketClientAsyncConnectData *data,
  1239. GError *error)
  1240. {
  1241. g_clear_error (&data->last_error);
  1242. data->last_error = error;
  1243. }
  1244. static void
  1245. enumerator_next_async (GSocketClientAsyncConnectData *data)
  1246. {
  1247. /* We need to cleanup the state */
  1248. g_clear_object (&data->current_socket);
  1249. g_clear_object (&data->current_addr);
  1250. g_clear_object (&data->proxy_addr);
  1251. g_clear_object (&data->connection);
  1252. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVING, data->connectable, NULL);
  1253. g_socket_address_enumerator_next_async (data->enumerator,
  1254. g_task_get_cancellable (data->task),
  1255. g_socket_client_enumerator_callback,
  1256. data);
  1257. }
  1258. static void
  1259. g_socket_client_tls_handshake_callback (GObject *object,
  1260. GAsyncResult *result,
  1261. gpointer user_data)
  1262. {
  1263. GSocketClientAsyncConnectData *data = user_data;
  1264. if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
  1265. result,
  1266. &data->last_error))
  1267. {
  1268. g_object_unref (data->connection);
  1269. data->connection = G_IO_STREAM (object);
  1270. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKED, data->connectable, data->connection);
  1271. g_socket_client_async_connect_complete (data);
  1272. }
  1273. else
  1274. {
  1275. g_object_unref (object);
  1276. enumerator_next_async (data);
  1277. }
  1278. }
  1279. static void
  1280. g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
  1281. {
  1282. GIOStream *tlsconn;
  1283. if (!data->client->priv->tls)
  1284. {
  1285. g_socket_client_async_connect_complete (data);
  1286. return;
  1287. }
  1288. tlsconn = g_tls_client_connection_new (data->connection,
  1289. data->connectable,
  1290. &data->last_error);
  1291. if (tlsconn)
  1292. {
  1293. g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
  1294. data->client->priv->tls_validation_flags);
  1295. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKING, data->connectable, G_IO_STREAM (tlsconn));
  1296. g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
  1297. G_PRIORITY_DEFAULT,
  1298. g_task_get_cancellable (data->task),
  1299. g_socket_client_tls_handshake_callback,
  1300. data);
  1301. }
  1302. else
  1303. {
  1304. enumerator_next_async (data);
  1305. }
  1306. }
  1307. static void
  1308. g_socket_client_proxy_connect_callback (GObject *object,
  1309. GAsyncResult *result,
  1310. gpointer user_data)
  1311. {
  1312. GSocketClientAsyncConnectData *data = user_data;
  1313. g_object_unref (data->connection);
  1314. data->connection = g_proxy_connect_finish (G_PROXY (object),
  1315. result,
  1316. &data->last_error);
  1317. if (data->connection)
  1318. {
  1319. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, data->connectable, data->connection);
  1320. }
  1321. else
  1322. {
  1323. enumerator_next_async (data);
  1324. return;
  1325. }
  1326. g_socket_client_tls_handshake (data);
  1327. }
  1328. static void
  1329. g_socket_client_connected_callback (GObject *source,
  1330. GAsyncResult *result,
  1331. gpointer user_data)
  1332. {
  1333. GSocketClientAsyncConnectData *data = user_data;
  1334. GError *error = NULL;
  1335. GProxy *proxy;
  1336. const gchar *protocol;
  1337. if (g_task_return_error_if_cancelled (data->task))
  1338. {
  1339. g_object_unref (data->task);
  1340. return;
  1341. }
  1342. if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (source),
  1343. result, &error))
  1344. {
  1345. clarify_connect_error (error, data->connectable,
  1346. data->current_addr);
  1347. set_last_error (data, error);
  1348. /* try next one */
  1349. enumerator_next_async (data);
  1350. return;
  1351. }
  1352. g_socket_connection_set_cached_remote_address ((GSocketConnection*)data->connection, NULL);
  1353. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTED, data->connectable, data->connection);
  1354. /* wrong, but backward compatible */
  1355. g_socket_set_blocking (data->current_socket, TRUE);
  1356. if (!data->proxy_addr)
  1357. {
  1358. g_socket_client_tls_handshake (data);
  1359. return;
  1360. }
  1361. protocol = g_proxy_address_get_protocol (data->proxy_addr);
  1362. /* The connection should not be anything other than TCP,
  1363. * but let's put a safety guard in case
  1364. */
  1365. if (!G_IS_TCP_CONNECTION (data->connection))
  1366. {
  1367. g_critical ("Trying to proxy over non-TCP connection, this is "
  1368. "most likely a bug in GLib IO library.");
  1369. g_set_error_literal (&data->last_error,
  1370. G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  1371. _("Proxying over a non-TCP connection is not supported."));
  1372. enumerator_next_async (data);
  1373. }
  1374. else if (g_hash_table_contains (data->client->priv->app_proxies, protocol))
  1375. {
  1376. /* Simply complete the connection, we don't want to do TLS handshake
  1377. * as the application proxy handling may need proxy handshake first */
  1378. g_socket_client_async_connect_complete (data);
  1379. }
  1380. else if ((proxy = g_proxy_get_default_for_protocol (protocol)))
  1381. {
  1382. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, data->connectable, data->connection);
  1383. g_proxy_connect_async (proxy,
  1384. data->connection,
  1385. data->proxy_addr,
  1386. g_task_get_cancellable (data->task),
  1387. g_socket_client_proxy_connect_callback,
  1388. data);
  1389. g_object_unref (proxy);
  1390. }
  1391. else
  1392. {
  1393. g_clear_error (&data->last_error);
  1394. g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  1395. _("Proxy protocol “%s” is not supported."),
  1396. protocol);
  1397. enumerator_next_async (data);
  1398. }
  1399. }
  1400. static void
  1401. g_socket_client_enumerator_callback (GObject *object,
  1402. GAsyncResult *result,
  1403. gpointer user_data)
  1404. {
  1405. GSocketClientAsyncConnectData *data = user_data;
  1406. GSocketAddress *address = NULL;
  1407. GSocket *socket;
  1408. GError *error = NULL;
  1409. if (g_task_return_error_if_cancelled (data->task))
  1410. {
  1411. g_object_unref (data->task);
  1412. return;
  1413. }
  1414. address = g_socket_address_enumerator_next_finish (data->enumerator,
  1415. result, &error);
  1416. if (address == NULL)
  1417. {
  1418. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
  1419. if (!error)
  1420. {
  1421. if (data->last_error)
  1422. {
  1423. error = data->last_error;
  1424. data->last_error = NULL;
  1425. }
  1426. else
  1427. {
  1428. g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
  1429. _("Unknown error on connect"));
  1430. }
  1431. }
  1432. g_task_return_error (data->task, error);
  1433. g_object_unref (data->task);
  1434. return;
  1435. }
  1436. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVED,
  1437. data->connectable, NULL);
  1438. if (G_IS_PROXY_ADDRESS (address) &&
  1439. data->client->priv->enable_proxy)
  1440. data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
  1441. g_clear_error (&data->last_error);
  1442. socket = create_socket (data->client, address, &data->last_error);
  1443. if (socket == NULL)
  1444. {
  1445. g_object_unref (address);
  1446. enumerator_next_async (data);
  1447. return;
  1448. }
  1449. data->current_socket = socket;
  1450. data->current_addr = address;
  1451. data->connection = (GIOStream *) g_socket_connection_factory_create_connection (socket);
  1452. g_socket_connection_set_cached_remote_address ((GSocketConnection*)data->connection, address);
  1453. g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTING, data->connectable, data->connection);
  1454. g_socket_connection_connect_async (G_SOCKET_CONNECTION (data->connection),
  1455. address,
  1456. g_task_get_cancellable (data->task),
  1457. g_socket_client_connected_callback, data);
  1458. }
  1459. /**
  1460. * g_socket_client_connect_async:
  1461. * @client: a #GSocketClient
  1462. * @connectable: a #GSocketConnectable specifying the remote address.
  1463. * @cancellable: (nullable): a #GCancellable, or %NULL
  1464. * @callback: (scope async): a #GAsyncReadyCallback
  1465. * @user_data: (closure): user data for the callback
  1466. *
  1467. * This is the asynchronous version of g_socket_client_connect().
  1468. *
  1469. * When the operation is finished @callback will be
  1470. * called. You can then call g_socket_client_connect_finish() to get
  1471. * the result of the operation.
  1472. *
  1473. * Since: 2.22
  1474. */
  1475. void
  1476. g_socket_client_connect_async (GSocketClient *client,
  1477. GSocketConnectable *connectable,
  1478. GCancellable *cancellable,
  1479. GAsyncReadyCallback callback,
  1480. gpointer user_data)
  1481. {
  1482. GSocketClientAsyncConnectData *data;
  1483. g_return_if_fail (G_IS_SOCKET_CLIENT (client));
  1484. data = g_slice_new0 (GSocketClientAsyncConnectData);
  1485. data->client = client;
  1486. data->connectable = g_object_ref (connectable);
  1487. if (can_use_proxy (client))
  1488. {
  1489. data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
  1490. if (client

Large files files are truncated, but you can click here to view the full file