PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 1ms

/gio/gsocket.c

https://gitlab.com/ImageMagick/glib
C | 5610 lines | 3486 code | 675 blank | 1449 comment | 638 complexity | 1869d0305b5d4bbd678622dbba1691ac 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 (C) 2008 Christian Kellner, Samuel Cormier-Iijima
  4. * Copyright © 2009 Codethink Limited
  5. * Copyright © 2009 Red Hat, Inc
  6. * Copyright © 2015 Collabora, Ltd.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General
  19. * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Authors: Christian Kellner <gicmo@gnome.org>
  22. * Samuel Cormier-Iijima <sciyoshi@gmail.com>
  23. * Ryan Lortie <desrt@desrt.ca>
  24. * Alexander Larsson <alexl@redhat.com>
  25. * Philip Withnall <philip.withnall@collabora.co.uk>
  26. */
  27. #include "config.h"
  28. #include "gsocket.h"
  29. #ifdef G_OS_UNIX
  30. #include "glib-unix.h"
  31. #endif
  32. #include <errno.h>
  33. #include <signal.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #ifndef G_OS_WIN32
  37. # include <fcntl.h>
  38. # include <unistd.h>
  39. # include <sys/ioctl.h>
  40. #endif
  41. #ifdef HAVE_SYS_FILIO_H
  42. # include <sys/filio.h>
  43. #endif
  44. #ifdef G_OS_UNIX
  45. #include <sys/uio.h>
  46. #endif
  47. #define GOBJECT_COMPILATION
  48. #include "gobject/gtype-private.h" /* For _PRELUDE type define */
  49. #undef GOBJECT_COMPILATION
  50. #include "gcancellable.h"
  51. #include "gdatagrambased.h"
  52. #include "gioenumtypes.h"
  53. #include "ginetaddress.h"
  54. #include "ginitable.h"
  55. #include "gioerror.h"
  56. #include "gioenums.h"
  57. #include "gioerror.h"
  58. #include "gnetworkingprivate.h"
  59. #include "gsocketaddress.h"
  60. #include "gsocketcontrolmessage.h"
  61. #include "gcredentials.h"
  62. #include "gcredentialsprivate.h"
  63. #include "glibintl.h"
  64. #ifdef G_OS_WIN32
  65. /* For Windows XP runtime compatibility, but use the system's if_nametoindex() if available */
  66. #include "gwin32networking.h"
  67. #endif
  68. /**
  69. * SECTION:gsocket
  70. * @short_description: Low-level socket object
  71. * @include: gio/gio.h
  72. * @see_also: #GInitable, [<gnetworking.h>][gio-gnetworking.h]
  73. *
  74. * A #GSocket is a low-level networking primitive. It is a more or less
  75. * direct mapping of the BSD socket API in a portable GObject based API.
  76. * It supports both the UNIX socket implementations and winsock2 on Windows.
  77. *
  78. * #GSocket is the platform independent base upon which the higher level
  79. * network primitives are based. Applications are not typically meant to
  80. * use it directly, but rather through classes like #GSocketClient,
  81. * #GSocketService and #GSocketConnection. However there may be cases where
  82. * direct use of #GSocket is useful.
  83. *
  84. * #GSocket implements the #GInitable interface, so if it is manually constructed
  85. * by e.g. g_object_new() you must call g_initable_init() and check the
  86. * results before using the object. This is done automatically in
  87. * g_socket_new() and g_socket_new_from_fd(), so these functions can return
  88. * %NULL.
  89. *
  90. * Sockets operate in two general modes, blocking or non-blocking. When
  91. * in blocking mode all operations (which don’t take an explicit blocking
  92. * parameter) block until the requested operation
  93. * is finished or there is an error. In non-blocking mode all calls that
  94. * would block return immediately with a %G_IO_ERROR_WOULD_BLOCK error.
  95. * To know when a call would successfully run you can call g_socket_condition_check(),
  96. * or g_socket_condition_wait(). You can also use g_socket_create_source() and
  97. * attach it to a #GMainContext to get callbacks when I/O is possible.
  98. * Note that all sockets are always set to non blocking mode in the system, and
  99. * blocking mode is emulated in GSocket.
  100. *
  101. * When working in non-blocking mode applications should always be able to
  102. * handle getting a %G_IO_ERROR_WOULD_BLOCK error even when some other
  103. * function said that I/O was possible. This can easily happen in case
  104. * of a race condition in the application, but it can also happen for other
  105. * reasons. For instance, on Windows a socket is always seen as writable
  106. * until a write returns %G_IO_ERROR_WOULD_BLOCK.
  107. *
  108. * #GSockets can be either connection oriented or datagram based.
  109. * For connection oriented types you must first establish a connection by
  110. * either connecting to an address or accepting a connection from another
  111. * address. For connectionless socket types the target/source address is
  112. * specified or received in each I/O operation.
  113. *
  114. * All socket file descriptors are set to be close-on-exec.
  115. *
  116. * Note that creating a #GSocket causes the signal %SIGPIPE to be
  117. * ignored for the remainder of the program. If you are writing a
  118. * command-line utility that uses #GSocket, you may need to take into
  119. * account the fact that your program will not automatically be killed
  120. * if it tries to write to %stdout after it has been closed.
  121. *
  122. * Like most other APIs in GLib, #GSocket is not inherently thread safe. To use
  123. * a #GSocket concurrently from multiple threads, you must implement your own
  124. * locking.
  125. *
  126. * Since: 2.22
  127. */
  128. static void g_socket_initable_iface_init (GInitableIface *iface);
  129. static gboolean g_socket_initable_init (GInitable *initable,
  130. GCancellable *cancellable,
  131. GError **error);
  132. static void g_socket_datagram_based_iface_init (GDatagramBasedInterface *iface);
  133. static gint g_socket_datagram_based_receive_messages (GDatagramBased *self,
  134. GInputMessage *messages,
  135. guint num_messages,
  136. gint flags,
  137. gint64 timeout,
  138. GCancellable *cancellable,
  139. GError **error);
  140. static gint g_socket_datagram_based_send_messages (GDatagramBased *self,
  141. GOutputMessage *messages,
  142. guint num_messages,
  143. gint flags,
  144. gint64 timeout,
  145. GCancellable *cancellable,
  146. GError **error);
  147. static GSource *g_socket_datagram_based_create_source (GDatagramBased *self,
  148. GIOCondition condition,
  149. GCancellable *cancellable);
  150. static GIOCondition g_socket_datagram_based_condition_check (GDatagramBased *datagram_based,
  151. GIOCondition condition);
  152. static gboolean g_socket_datagram_based_condition_wait (GDatagramBased *datagram_based,
  153. GIOCondition condition,
  154. gint64 timeout,
  155. GCancellable *cancellable,
  156. GError **error);
  157. static GSocketAddress *
  158. cache_recv_address (GSocket *socket, struct sockaddr *native, int native_len);
  159. static gssize
  160. g_socket_receive_message_with_timeout (GSocket *socket,
  161. GSocketAddress **address,
  162. GInputVector *vectors,
  163. gint num_vectors,
  164. GSocketControlMessage ***messages,
  165. gint *num_messages,
  166. gint *flags,
  167. gint64 timeout,
  168. GCancellable *cancellable,
  169. GError **error);
  170. static gint
  171. g_socket_receive_messages_with_timeout (GSocket *socket,
  172. GInputMessage *messages,
  173. guint num_messages,
  174. gint flags,
  175. gint64 timeout,
  176. GCancellable *cancellable,
  177. GError **error);
  178. static gssize
  179. g_socket_send_message_with_timeout (GSocket *socket,
  180. GSocketAddress *address,
  181. GOutputVector *vectors,
  182. gint num_vectors,
  183. GSocketControlMessage **messages,
  184. gint num_messages,
  185. gint flags,
  186. gint64 timeout,
  187. GCancellable *cancellable,
  188. GError **error);
  189. static gint
  190. g_socket_send_messages_with_timeout (GSocket *socket,
  191. GOutputMessage *messages,
  192. guint num_messages,
  193. gint flags,
  194. gint64 timeout,
  195. GCancellable *cancellable,
  196. GError **error);
  197. enum
  198. {
  199. PROP_0,
  200. PROP_FAMILY,
  201. PROP_TYPE,
  202. PROP_PROTOCOL,
  203. PROP_FD,
  204. PROP_BLOCKING,
  205. PROP_LISTEN_BACKLOG,
  206. PROP_KEEPALIVE,
  207. PROP_LOCAL_ADDRESS,
  208. PROP_REMOTE_ADDRESS,
  209. PROP_TIMEOUT,
  210. PROP_TTL,
  211. PROP_BROADCAST,
  212. PROP_MULTICAST_LOOPBACK,
  213. PROP_MULTICAST_TTL
  214. };
  215. /* Size of the receiver cache for g_socket_receive_from() */
  216. #define RECV_ADDR_CACHE_SIZE 8
  217. struct _GSocketPrivate
  218. {
  219. GSocketFamily family;
  220. GSocketType type;
  221. GSocketProtocol protocol;
  222. gint fd;
  223. gint listen_backlog;
  224. guint timeout;
  225. GError *construct_error;
  226. GSocketAddress *remote_address;
  227. guint inited : 1;
  228. guint blocking : 1;
  229. guint keepalive : 1;
  230. guint closed : 1;
  231. guint connected_read : 1;
  232. guint connected_write : 1;
  233. guint listening : 1;
  234. guint timed_out : 1;
  235. guint connect_pending : 1;
  236. #ifdef G_OS_WIN32
  237. WSAEVENT event;
  238. gboolean waiting;
  239. DWORD waiting_result;
  240. int current_events;
  241. int current_errors;
  242. int selected_events;
  243. GList *requested_conditions; /* list of requested GIOCondition * */
  244. GMutex win32_source_lock;
  245. GCond win32_source_cond;
  246. #endif
  247. struct {
  248. GSocketAddress *addr;
  249. struct sockaddr *native;
  250. gint native_len;
  251. guint64 last_used;
  252. } recv_addr_cache[RECV_ADDR_CACHE_SIZE];
  253. };
  254. _G_DEFINE_TYPE_EXTENDED_WITH_PRELUDE (GSocket, g_socket, G_TYPE_OBJECT, 0,
  255. /* Need a prelude for https://bugzilla.gnome.org/show_bug.cgi?id=674885 */
  256. g_type_ensure (G_TYPE_SOCKET_FAMILY);
  257. g_type_ensure (G_TYPE_SOCKET_TYPE);
  258. g_type_ensure (G_TYPE_SOCKET_PROTOCOL);
  259. g_type_ensure (G_TYPE_SOCKET_ADDRESS);
  260. /* And networking init is appropriate for the prelude */
  261. g_networking_init ();
  262. , /* And now the regular type init code */
  263. G_ADD_PRIVATE (GSocket)
  264. G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
  265. g_socket_initable_iface_init);
  266. G_IMPLEMENT_INTERFACE (G_TYPE_DATAGRAM_BASED,
  267. g_socket_datagram_based_iface_init));
  268. static int
  269. get_socket_errno (void)
  270. {
  271. #ifndef G_OS_WIN32
  272. return errno;
  273. #else
  274. return WSAGetLastError ();
  275. #endif
  276. }
  277. static GIOErrorEnum
  278. socket_io_error_from_errno (int err)
  279. {
  280. #ifdef G_OS_WIN32
  281. return g_io_error_from_win32_error (err);
  282. #else
  283. return g_io_error_from_errno (err);
  284. #endif
  285. }
  286. static const char *
  287. socket_strerror (int err)
  288. {
  289. #ifndef G_OS_WIN32
  290. return g_strerror (err);
  291. #else
  292. const char *msg_ret;
  293. char *msg;
  294. msg = g_win32_error_message (err);
  295. msg_ret = g_intern_string (msg);
  296. g_free (msg);
  297. return msg_ret;
  298. #endif
  299. }
  300. /* Wrapper around g_set_error() to avoid doing excess work */
  301. #define socket_set_error_lazy(err, errsv, fmt) \
  302. G_STMT_START { \
  303. GError **__err = (err); \
  304. int __errsv = (errsv); \
  305. \
  306. if (__err) \
  307. { \
  308. int __code = socket_io_error_from_errno (__errsv); \
  309. const char *__strerr = socket_strerror (__errsv); \
  310. \
  311. if (__code == G_IO_ERROR_WOULD_BLOCK) \
  312. g_set_error_literal (__err, G_IO_ERROR, __code, __strerr); \
  313. else \
  314. g_set_error (__err, G_IO_ERROR, __code, fmt, __strerr); \
  315. } \
  316. } G_STMT_END
  317. #ifdef G_OS_WIN32
  318. #define win32_unset_event_mask(_socket, _mask) _win32_unset_event_mask (_socket, _mask)
  319. static void
  320. _win32_unset_event_mask (GSocket *socket, int mask)
  321. {
  322. g_mutex_lock (&socket->priv->win32_source_lock);
  323. socket->priv->current_events &= ~mask;
  324. socket->priv->current_errors &= ~mask;
  325. g_mutex_unlock (&socket->priv->win32_source_lock);
  326. }
  327. #else
  328. #define win32_unset_event_mask(_socket, _mask)
  329. #endif
  330. /* Windows has broken prototypes... */
  331. #ifdef G_OS_WIN32
  332. #define getsockopt(sockfd, level, optname, optval, optlen) \
  333. getsockopt (sockfd, level, optname, (gpointer) optval, (int*) optlen)
  334. #define setsockopt(sockfd, level, optname, optval, optlen) \
  335. setsockopt (sockfd, level, optname, (gpointer) optval, optlen)
  336. #define getsockname(sockfd, addr, addrlen) \
  337. getsockname (sockfd, addr, (int *)addrlen)
  338. #define getpeername(sockfd, addr, addrlen) \
  339. getpeername (sockfd, addr, (int *)addrlen)
  340. #define recv(sockfd, buf, len, flags) \
  341. recv (sockfd, (gpointer)buf, len, flags)
  342. #endif
  343. static gboolean
  344. check_socket (GSocket *socket,
  345. GError **error)
  346. {
  347. if (!socket->priv->inited)
  348. {
  349. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
  350. _("Invalid socket, not initialized"));
  351. return FALSE;
  352. }
  353. if (socket->priv->construct_error)
  354. {
  355. g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
  356. _("Invalid socket, initialization failed due to: %s"),
  357. socket->priv->construct_error->message);
  358. return FALSE;
  359. }
  360. if (socket->priv->closed)
  361. {
  362. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
  363. _("Socket is already closed"));
  364. return FALSE;
  365. }
  366. return TRUE;
  367. }
  368. static gboolean
  369. check_timeout (GSocket *socket,
  370. GError **error)
  371. {
  372. if (socket->priv->timed_out)
  373. {
  374. socket->priv->timed_out = FALSE;
  375. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
  376. _("Socket I/O timed out"));
  377. return FALSE;
  378. }
  379. return TRUE;
  380. }
  381. static void
  382. g_socket_details_from_fd (GSocket *socket)
  383. {
  384. struct sockaddr_storage address;
  385. gint fd;
  386. guint addrlen;
  387. int value, family;
  388. int errsv;
  389. fd = socket->priv->fd;
  390. if (!g_socket_get_option (socket, SOL_SOCKET, SO_TYPE, &value, NULL))
  391. {
  392. errsv = get_socket_errno ();
  393. goto err;
  394. }
  395. switch (value)
  396. {
  397. case SOCK_STREAM:
  398. socket->priv->type = G_SOCKET_TYPE_STREAM;
  399. break;
  400. case SOCK_DGRAM:
  401. socket->priv->type = G_SOCKET_TYPE_DATAGRAM;
  402. break;
  403. case SOCK_SEQPACKET:
  404. socket->priv->type = G_SOCKET_TYPE_SEQPACKET;
  405. break;
  406. default:
  407. socket->priv->type = G_SOCKET_TYPE_INVALID;
  408. break;
  409. }
  410. addrlen = sizeof address;
  411. if (getsockname (fd, (struct sockaddr *) &address, &addrlen) != 0)
  412. {
  413. errsv = get_socket_errno ();
  414. goto err;
  415. }
  416. if (addrlen > 0)
  417. {
  418. g_assert (G_STRUCT_OFFSET (struct sockaddr, sa_family) +
  419. sizeof address.ss_family <= addrlen);
  420. family = address.ss_family;
  421. }
  422. else
  423. {
  424. /* On Solaris, this happens if the socket is not yet connected.
  425. * But we can use SO_DOMAIN as a workaround there.
  426. */
  427. #ifdef SO_DOMAIN
  428. if (!g_socket_get_option (socket, SOL_SOCKET, SO_DOMAIN, &family, NULL))
  429. {
  430. errsv = get_socket_errno ();
  431. goto err;
  432. }
  433. #else
  434. /* This will translate to G_IO_ERROR_FAILED on either unix or windows */
  435. errsv = -1;
  436. goto err;
  437. #endif
  438. }
  439. switch (family)
  440. {
  441. case G_SOCKET_FAMILY_IPV4:
  442. case G_SOCKET_FAMILY_IPV6:
  443. socket->priv->family = address.ss_family;
  444. switch (socket->priv->type)
  445. {
  446. case G_SOCKET_TYPE_STREAM:
  447. socket->priv->protocol = G_SOCKET_PROTOCOL_TCP;
  448. break;
  449. case G_SOCKET_TYPE_DATAGRAM:
  450. socket->priv->protocol = G_SOCKET_PROTOCOL_UDP;
  451. break;
  452. case G_SOCKET_TYPE_SEQPACKET:
  453. socket->priv->protocol = G_SOCKET_PROTOCOL_SCTP;
  454. break;
  455. default:
  456. break;
  457. }
  458. break;
  459. case G_SOCKET_FAMILY_UNIX:
  460. socket->priv->family = G_SOCKET_FAMILY_UNIX;
  461. socket->priv->protocol = G_SOCKET_PROTOCOL_DEFAULT;
  462. break;
  463. default:
  464. socket->priv->family = G_SOCKET_FAMILY_INVALID;
  465. break;
  466. }
  467. if (socket->priv->family != G_SOCKET_FAMILY_INVALID)
  468. {
  469. addrlen = sizeof address;
  470. if (getpeername (fd, (struct sockaddr *) &address, &addrlen) >= 0)
  471. {
  472. socket->priv->connected_read = TRUE;
  473. socket->priv->connected_write = TRUE;
  474. }
  475. }
  476. if (g_socket_get_option (socket, SOL_SOCKET, SO_KEEPALIVE, &value, NULL))
  477. {
  478. socket->priv->keepalive = !!value;
  479. }
  480. else
  481. {
  482. /* Can't read, maybe not supported, assume FALSE */
  483. socket->priv->keepalive = FALSE;
  484. }
  485. return;
  486. err:
  487. g_set_error (&socket->priv->construct_error, G_IO_ERROR,
  488. socket_io_error_from_errno (errsv),
  489. _("creating GSocket from fd: %s"),
  490. socket_strerror (errsv));
  491. }
  492. /* Wrapper around socket() that is shared with gnetworkmonitornetlink.c */
  493. gint
  494. g_socket (gint domain,
  495. gint type,
  496. gint protocol,
  497. GError **error)
  498. {
  499. int fd, errsv;
  500. #ifdef SOCK_CLOEXEC
  501. fd = socket (domain, type | SOCK_CLOEXEC, protocol);
  502. errsv = errno;
  503. if (fd != -1)
  504. return fd;
  505. /* It's possible that libc has SOCK_CLOEXEC but the kernel does not */
  506. if (fd < 0 && (errsv == EINVAL || errsv == EPROTOTYPE))
  507. #endif
  508. fd = socket (domain, type, protocol);
  509. if (fd < 0)
  510. {
  511. int errsv = get_socket_errno ();
  512. g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
  513. _("Unable to create socket: %s"), socket_strerror (errsv));
  514. errno = errsv;
  515. return -1;
  516. }
  517. #ifndef G_OS_WIN32
  518. {
  519. int flags;
  520. /* We always want to set close-on-exec to protect users. If you
  521. need to so some weird inheritance to exec you can re-enable this
  522. using lower level hacks with g_socket_get_fd(). */
  523. flags = fcntl (fd, F_GETFD, 0);
  524. if (flags != -1 &&
  525. (flags & FD_CLOEXEC) == 0)
  526. {
  527. flags |= FD_CLOEXEC;
  528. fcntl (fd, F_SETFD, flags);
  529. }
  530. }
  531. #endif
  532. return fd;
  533. }
  534. static gint
  535. g_socket_create_socket (GSocketFamily family,
  536. GSocketType type,
  537. int protocol,
  538. GError **error)
  539. {
  540. gint native_type;
  541. switch (type)
  542. {
  543. case G_SOCKET_TYPE_STREAM:
  544. native_type = SOCK_STREAM;
  545. break;
  546. case G_SOCKET_TYPE_DATAGRAM:
  547. native_type = SOCK_DGRAM;
  548. break;
  549. case G_SOCKET_TYPE_SEQPACKET:
  550. native_type = SOCK_SEQPACKET;
  551. break;
  552. default:
  553. g_assert_not_reached ();
  554. }
  555. if (family <= 0)
  556. {
  557. g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
  558. _("Unable to create socket: %s"), _("Unknown family was specified"));
  559. return -1;
  560. }
  561. if (protocol == -1)
  562. {
  563. g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
  564. _("Unable to create socket: %s"), _("Unknown protocol was specified"));
  565. return -1;
  566. }
  567. return g_socket (family, native_type, protocol, error);
  568. }
  569. static void
  570. g_socket_constructed (GObject *object)
  571. {
  572. GSocket *socket = G_SOCKET (object);
  573. if (socket->priv->fd >= 0)
  574. /* create socket->priv info from the fd */
  575. g_socket_details_from_fd (socket);
  576. else
  577. /* create the fd from socket->priv info */
  578. socket->priv->fd = g_socket_create_socket (socket->priv->family,
  579. socket->priv->type,
  580. socket->priv->protocol,
  581. &socket->priv->construct_error);
  582. if (socket->priv->fd != -1)
  583. {
  584. #ifndef G_OS_WIN32
  585. GError *error = NULL;
  586. #else
  587. gulong arg;
  588. #endif
  589. /* Always use native nonblocking sockets, as Windows sets sockets to
  590. * nonblocking automatically in certain operations. This way we make
  591. * things work the same on all platforms.
  592. */
  593. #ifndef G_OS_WIN32
  594. if (!g_unix_set_fd_nonblocking (socket->priv->fd, TRUE, &error))
  595. {
  596. g_warning ("Error setting socket nonblocking: %s", error->message);
  597. g_clear_error (&error);
  598. }
  599. #else
  600. arg = TRUE;
  601. if (ioctlsocket (socket->priv->fd, FIONBIO, &arg) == SOCKET_ERROR)
  602. {
  603. int errsv = get_socket_errno ();
  604. g_warning ("Error setting socket status flags: %s", socket_strerror (errsv));
  605. }
  606. #endif
  607. #ifdef SO_NOSIGPIPE
  608. /* See note about SIGPIPE below. */
  609. g_socket_set_option (socket, SOL_SOCKET, SO_NOSIGPIPE, TRUE, NULL);
  610. #endif
  611. }
  612. }
  613. static void
  614. g_socket_get_property (GObject *object,
  615. guint prop_id,
  616. GValue *value,
  617. GParamSpec *pspec)
  618. {
  619. GSocket *socket = G_SOCKET (object);
  620. GSocketAddress *address;
  621. switch (prop_id)
  622. {
  623. case PROP_FAMILY:
  624. g_value_set_enum (value, socket->priv->family);
  625. break;
  626. case PROP_TYPE:
  627. g_value_set_enum (value, socket->priv->type);
  628. break;
  629. case PROP_PROTOCOL:
  630. g_value_set_enum (value, socket->priv->protocol);
  631. break;
  632. case PROP_FD:
  633. g_value_set_int (value, socket->priv->fd);
  634. break;
  635. case PROP_BLOCKING:
  636. g_value_set_boolean (value, socket->priv->blocking);
  637. break;
  638. case PROP_LISTEN_BACKLOG:
  639. g_value_set_int (value, socket->priv->listen_backlog);
  640. break;
  641. case PROP_KEEPALIVE:
  642. g_value_set_boolean (value, socket->priv->keepalive);
  643. break;
  644. case PROP_LOCAL_ADDRESS:
  645. address = g_socket_get_local_address (socket, NULL);
  646. g_value_take_object (value, address);
  647. break;
  648. case PROP_REMOTE_ADDRESS:
  649. address = g_socket_get_remote_address (socket, NULL);
  650. g_value_take_object (value, address);
  651. break;
  652. case PROP_TIMEOUT:
  653. g_value_set_uint (value, socket->priv->timeout);
  654. break;
  655. case PROP_TTL:
  656. g_value_set_uint (value, g_socket_get_ttl (socket));
  657. break;
  658. case PROP_BROADCAST:
  659. g_value_set_boolean (value, g_socket_get_broadcast (socket));
  660. break;
  661. case PROP_MULTICAST_LOOPBACK:
  662. g_value_set_boolean (value, g_socket_get_multicast_loopback (socket));
  663. break;
  664. case PROP_MULTICAST_TTL:
  665. g_value_set_uint (value, g_socket_get_multicast_ttl (socket));
  666. break;
  667. default:
  668. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  669. }
  670. }
  671. static void
  672. g_socket_set_property (GObject *object,
  673. guint prop_id,
  674. const GValue *value,
  675. GParamSpec *pspec)
  676. {
  677. GSocket *socket = G_SOCKET (object);
  678. switch (prop_id)
  679. {
  680. case PROP_FAMILY:
  681. socket->priv->family = g_value_get_enum (value);
  682. break;
  683. case PROP_TYPE:
  684. socket->priv->type = g_value_get_enum (value);
  685. break;
  686. case PROP_PROTOCOL:
  687. socket->priv->protocol = g_value_get_enum (value);
  688. break;
  689. case PROP_FD:
  690. socket->priv->fd = g_value_get_int (value);
  691. break;
  692. case PROP_BLOCKING:
  693. g_socket_set_blocking (socket, g_value_get_boolean (value));
  694. break;
  695. case PROP_LISTEN_BACKLOG:
  696. g_socket_set_listen_backlog (socket, g_value_get_int (value));
  697. break;
  698. case PROP_KEEPALIVE:
  699. g_socket_set_keepalive (socket, g_value_get_boolean (value));
  700. break;
  701. case PROP_TIMEOUT:
  702. g_socket_set_timeout (socket, g_value_get_uint (value));
  703. break;
  704. case PROP_TTL:
  705. g_socket_set_ttl (socket, g_value_get_uint (value));
  706. break;
  707. case PROP_BROADCAST:
  708. g_socket_set_broadcast (socket, g_value_get_boolean (value));
  709. break;
  710. case PROP_MULTICAST_LOOPBACK:
  711. g_socket_set_multicast_loopback (socket, g_value_get_boolean (value));
  712. break;
  713. case PROP_MULTICAST_TTL:
  714. g_socket_set_multicast_ttl (socket, g_value_get_uint (value));
  715. break;
  716. default:
  717. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  718. }
  719. }
  720. static void
  721. g_socket_finalize (GObject *object)
  722. {
  723. GSocket *socket = G_SOCKET (object);
  724. gint i;
  725. g_clear_error (&socket->priv->construct_error);
  726. if (socket->priv->fd != -1 &&
  727. !socket->priv->closed)
  728. g_socket_close (socket, NULL);
  729. if (socket->priv->remote_address)
  730. g_object_unref (socket->priv->remote_address);
  731. #ifdef G_OS_WIN32
  732. if (socket->priv->event != WSA_INVALID_EVENT)
  733. {
  734. WSACloseEvent (socket->priv->event);
  735. socket->priv->event = WSA_INVALID_EVENT;
  736. }
  737. g_assert (socket->priv->requested_conditions == NULL);
  738. g_mutex_clear (&socket->priv->win32_source_lock);
  739. g_cond_clear (&socket->priv->win32_source_cond);
  740. #endif
  741. for (i = 0; i < RECV_ADDR_CACHE_SIZE; i++)
  742. {
  743. if (socket->priv->recv_addr_cache[i].addr)
  744. {
  745. g_object_unref (socket->priv->recv_addr_cache[i].addr);
  746. g_free (socket->priv->recv_addr_cache[i].native);
  747. }
  748. }
  749. if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
  750. (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
  751. }
  752. static void
  753. g_socket_class_init (GSocketClass *klass)
  754. {
  755. GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
  756. #ifdef SIGPIPE
  757. /* There is no portable, thread-safe way to avoid having the process
  758. * be killed by SIGPIPE when calling send() or sendmsg(), so we are
  759. * forced to simply ignore the signal process-wide.
  760. *
  761. * Even if we ignore it though, gdb will still stop if the app
  762. * receives a SIGPIPE, which can be confusing and annoying. So when
  763. * possible, we also use MSG_NOSIGNAL / SO_NOSIGPIPE elsewhere to
  764. * prevent the signal from occurring at all.
  765. */
  766. signal (SIGPIPE, SIG_IGN);
  767. #endif
  768. gobject_class->finalize = g_socket_finalize;
  769. gobject_class->constructed = g_socket_constructed;
  770. gobject_class->set_property = g_socket_set_property;
  771. gobject_class->get_property = g_socket_get_property;
  772. g_object_class_install_property (gobject_class, PROP_FAMILY,
  773. g_param_spec_enum ("family",
  774. P_("Socket family"),
  775. P_("The sockets address family"),
  776. G_TYPE_SOCKET_FAMILY,
  777. G_SOCKET_FAMILY_INVALID,
  778. G_PARAM_CONSTRUCT_ONLY |
  779. G_PARAM_READWRITE |
  780. G_PARAM_STATIC_STRINGS));
  781. g_object_class_install_property (gobject_class, PROP_TYPE,
  782. g_param_spec_enum ("type",
  783. P_("Socket type"),
  784. P_("The sockets type"),
  785. G_TYPE_SOCKET_TYPE,
  786. G_SOCKET_TYPE_STREAM,
  787. G_PARAM_CONSTRUCT_ONLY |
  788. G_PARAM_READWRITE |
  789. G_PARAM_STATIC_STRINGS));
  790. g_object_class_install_property (gobject_class, PROP_PROTOCOL,
  791. g_param_spec_enum ("protocol",
  792. P_("Socket protocol"),
  793. P_("The id of the protocol to use, or -1 for unknown"),
  794. G_TYPE_SOCKET_PROTOCOL,
  795. G_SOCKET_PROTOCOL_UNKNOWN,
  796. G_PARAM_CONSTRUCT_ONLY |
  797. G_PARAM_READWRITE |
  798. G_PARAM_STATIC_STRINGS));
  799. g_object_class_install_property (gobject_class, PROP_FD,
  800. g_param_spec_int ("fd",
  801. P_("File descriptor"),
  802. P_("The sockets file descriptor"),
  803. G_MININT,
  804. G_MAXINT,
  805. -1,
  806. G_PARAM_CONSTRUCT_ONLY |
  807. G_PARAM_READWRITE |
  808. G_PARAM_STATIC_STRINGS));
  809. g_object_class_install_property (gobject_class, PROP_BLOCKING,
  810. g_param_spec_boolean ("blocking",
  811. P_("blocking"),
  812. P_("Whether or not I/O on this socket is blocking"),
  813. TRUE,
  814. G_PARAM_READWRITE |
  815. G_PARAM_STATIC_STRINGS));
  816. g_object_class_install_property (gobject_class, PROP_LISTEN_BACKLOG,
  817. g_param_spec_int ("listen-backlog",
  818. P_("Listen backlog"),
  819. P_("Outstanding connections in the listen queue"),
  820. 0,
  821. SOMAXCONN,
  822. 10,
  823. G_PARAM_READWRITE |
  824. G_PARAM_STATIC_STRINGS));
  825. g_object_class_install_property (gobject_class, PROP_KEEPALIVE,
  826. g_param_spec_boolean ("keepalive",
  827. P_("Keep connection alive"),
  828. P_("Keep connection alive by sending periodic pings"),
  829. FALSE,
  830. G_PARAM_READWRITE |
  831. G_PARAM_STATIC_STRINGS));
  832. g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
  833. g_param_spec_object ("local-address",
  834. P_("Local address"),
  835. P_("The local address the socket is bound to"),
  836. G_TYPE_SOCKET_ADDRESS,
  837. G_PARAM_READABLE |
  838. G_PARAM_STATIC_STRINGS));
  839. g_object_class_install_property (gobject_class, PROP_REMOTE_ADDRESS,
  840. g_param_spec_object ("remote-address",
  841. P_("Remote address"),
  842. P_("The remote address the socket is connected to"),
  843. G_TYPE_SOCKET_ADDRESS,
  844. G_PARAM_READABLE |
  845. G_PARAM_STATIC_STRINGS));
  846. /**
  847. * GSocket:timeout:
  848. *
  849. * The timeout in seconds on socket I/O
  850. *
  851. * Since: 2.26
  852. */
  853. g_object_class_install_property (gobject_class, PROP_TIMEOUT,
  854. g_param_spec_uint ("timeout",
  855. P_("Timeout"),
  856. P_("The timeout in seconds on socket I/O"),
  857. 0,
  858. G_MAXUINT,
  859. 0,
  860. G_PARAM_READWRITE |
  861. G_PARAM_STATIC_STRINGS));
  862. /**
  863. * GSocket:broadcast:
  864. *
  865. * Whether the socket should allow sending to broadcast addresses.
  866. *
  867. * Since: 2.32
  868. */
  869. g_object_class_install_property (gobject_class, PROP_BROADCAST,
  870. g_param_spec_boolean ("broadcast",
  871. P_("Broadcast"),
  872. P_("Whether to allow sending to broadcast addresses"),
  873. FALSE,
  874. G_PARAM_READWRITE |
  875. G_PARAM_STATIC_STRINGS));
  876. /**
  877. * GSocket:ttl:
  878. *
  879. * Time-to-live for outgoing unicast packets
  880. *
  881. * Since: 2.32
  882. */
  883. g_object_class_install_property (gobject_class, PROP_TTL,
  884. g_param_spec_uint ("ttl",
  885. P_("TTL"),
  886. P_("Time-to-live of outgoing unicast packets"),
  887. 0, G_MAXUINT, 0,
  888. G_PARAM_READWRITE |
  889. G_PARAM_STATIC_STRINGS));
  890. /**
  891. * GSocket:multicast-loopback:
  892. *
  893. * Whether outgoing multicast packets loop back to the local host.
  894. *
  895. * Since: 2.32
  896. */
  897. g_object_class_install_property (gobject_class, PROP_MULTICAST_LOOPBACK,
  898. g_param_spec_boolean ("multicast-loopback",
  899. P_("Multicast loopback"),
  900. P_("Whether outgoing multicast packets loop back to the local host"),
  901. TRUE,
  902. G_PARAM_READWRITE |
  903. G_PARAM_STATIC_STRINGS));
  904. /**
  905. * GSocket:multicast-ttl:
  906. *
  907. * Time-to-live out outgoing multicast packets
  908. *
  909. * Since: 2.32
  910. */
  911. g_object_class_install_property (gobject_class, PROP_MULTICAST_TTL,
  912. g_param_spec_uint ("multicast-ttl",
  913. P_("Multicast TTL"),
  914. P_("Time-to-live of outgoing multicast packets"),
  915. 0, G_MAXUINT, 1,
  916. G_PARAM_READWRITE |
  917. G_PARAM_STATIC_STRINGS));
  918. }
  919. static void
  920. g_socket_initable_iface_init (GInitableIface *iface)
  921. {
  922. iface->init = g_socket_initable_init;
  923. }
  924. static void
  925. g_socket_datagram_based_iface_init (GDatagramBasedInterface *iface)
  926. {
  927. iface->receive_messages = g_socket_datagram_based_receive_messages;
  928. iface->send_messages = g_socket_datagram_based_send_messages;
  929. iface->create_source = g_socket_datagram_based_create_source;
  930. iface->condition_check = g_socket_datagram_based_condition_check;
  931. iface->condition_wait = g_socket_datagram_based_condition_wait;
  932. }
  933. static void
  934. g_socket_init (GSocket *socket)
  935. {
  936. socket->priv = g_socket_get_instance_private (socket);
  937. socket->priv->fd = -1;
  938. socket->priv->blocking = TRUE;
  939. socket->priv->listen_backlog = 10;
  940. socket->priv->construct_error = NULL;
  941. #ifdef G_OS_WIN32
  942. socket->priv->event = WSA_INVALID_EVENT;
  943. g_mutex_init (&socket->priv->win32_source_lock);
  944. g_cond_init (&socket->priv->win32_source_cond);
  945. #endif
  946. }
  947. static gboolean
  948. g_socket_initable_init (GInitable *initable,
  949. GCancellable *cancellable,
  950. GError **error)
  951. {
  952. GSocket *socket;
  953. g_return_val_if_fail (G_IS_SOCKET (initable), FALSE);
  954. socket = G_SOCKET (initable);
  955. if (cancellable != NULL)
  956. {
  957. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  958. _("Cancellable initialization not supported"));
  959. return FALSE;
  960. }
  961. socket->priv->inited = TRUE;
  962. if (socket->priv->construct_error)
  963. {
  964. if (error)
  965. *error = g_error_copy (socket->priv->construct_error);
  966. return FALSE;
  967. }
  968. return TRUE;
  969. }
  970. static gboolean
  971. check_datagram_based (GDatagramBased *self,
  972. GError **error)
  973. {
  974. switch (g_socket_get_socket_type (G_SOCKET (self)))
  975. {
  976. case G_SOCKET_TYPE_INVALID:
  977. case G_SOCKET_TYPE_STREAM:
  978. g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  979. _("Cannot use datagram operations on a non-datagram "
  980. "socket."));
  981. return FALSE;
  982. case G_SOCKET_TYPE_DATAGRAM:
  983. case G_SOCKET_TYPE_SEQPACKET:
  984. /* Fall through. */
  985. break;
  986. }
  987. /* Due to us sharing #GSocketSource with the #GSocket implementation, it is
  988. * pretty tricky to split out #GSocket:timeout so that it does not affect
  989. * #GDatagramBased operations (but still affects #GSocket operations). It is
  990. * not worth that effort — just disallow it and require the user to specify
  991. * timeouts on a per-operation basis. */
  992. if (g_socket_get_timeout (G_SOCKET (self)) != 0)
  993. {
  994. g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  995. _("Cannot use datagram operations on a socket with a "
  996. "timeout set."));
  997. return FALSE;
  998. }
  999. return TRUE;
  1000. }
  1001. static gint
  1002. g_socket_datagram_based_receive_messages (GDatagramBased *self,
  1003. GInputMessage *messages,
  1004. guint num_messages,
  1005. gint flags,
  1006. gint64 timeout,
  1007. GCancellable *cancellable,
  1008. GError **error)
  1009. {
  1010. if (!check_datagram_based (self, error))
  1011. return FALSE;
  1012. return g_socket_receive_messages_with_timeout (G_SOCKET (self), messages,
  1013. num_messages, flags, timeout,
  1014. cancellable, error);
  1015. }
  1016. static gint
  1017. g_socket_datagram_based_send_messages (GDatagramBased *self,
  1018. GOutputMessage *messages,
  1019. guint num_messages,
  1020. gint flags,
  1021. gint64 timeout,
  1022. GCancellable *cancellable,
  1023. GError **error)
  1024. {
  1025. if (!check_datagram_based (self, error))
  1026. return FALSE;
  1027. return g_socket_send_messages_with_timeout (G_SOCKET (self), messages,
  1028. num_messages, flags, timeout,
  1029. cancellable, error);
  1030. }
  1031. static GSource *
  1032. g_socket_datagram_based_create_source (GDatagramBased *self,
  1033. GIOCondition condition,
  1034. GCancellable *cancellable)
  1035. {
  1036. if (!check_datagram_based (self, NULL))
  1037. return NULL;
  1038. return g_socket_create_source (G_SOCKET (self), condition, cancellable);
  1039. }
  1040. static GIOCondition
  1041. g_socket_datagram_based_condition_check (GDatagramBased *datagram_based,
  1042. GIOCondition condition)
  1043. {
  1044. if (!check_datagram_based (datagram_based, NULL))
  1045. return G_IO_ERR;
  1046. return g_socket_condition_check (G_SOCKET (datagram_based), condition);
  1047. }
  1048. static gboolean
  1049. g_socket_datagram_based_condition_wait (GDatagramBased *datagram_based,
  1050. GIOCondition condition,
  1051. gint64 timeout,
  1052. GCancellable *cancellable,
  1053. GError **error)
  1054. {
  1055. if (!check_datagram_based (datagram_based, error))
  1056. return FALSE;
  1057. return g_socket_condition_timed_wait (G_SOCKET (datagram_based), condition,
  1058. timeout, cancellable, error);
  1059. }
  1060. /**
  1061. * g_socket_new:
  1062. * @family: the socket family to use, e.g. %G_SOCKET_FAMILY_IPV4.
  1063. * @type: the socket type to use.
  1064. * @protocol: the id of the protocol to use, or 0 for default.
  1065. * @error: #GError for error reporting, or %NULL to ignore.
  1066. *
  1067. * Creates a new #GSocket with the defined family, type and protocol.
  1068. * If @protocol is 0 (%G_SOCKET_PROTOCOL_DEFAULT) the default protocol type
  1069. * for the family and type is used.
  1070. *
  1071. * The @protocol is a family and type specific int that specifies what
  1072. * kind of protocol to use. #GSocketProtocol lists several common ones.
  1073. * Many families only support one protocol, and use 0 for this, others
  1074. * support several and using 0 means to use the default protocol for
  1075. * the family and type.
  1076. *
  1077. * The protocol id is passed directly to the operating
  1078. * system, so you can use protocols not listed in #GSocketProtocol if you
  1079. * know the protocol number used for it.
  1080. *
  1081. * Returns: a #GSocket or %NULL on error.
  1082. * Free the returned object with g_object_unref().
  1083. *
  1084. * Since: 2.22
  1085. */
  1086. GSocket *
  1087. g_socket_new (GSocketFamily family,
  1088. GSocketType type,
  1089. GSocketProtocol protocol,
  1090. GError **error)
  1091. {
  1092. return G_SOCKET (g_initable_new (G_TYPE_SOCKET,
  1093. NULL, error,
  1094. "family", family,
  1095. "type", type,
  1096. "protocol", protocol,
  1097. NULL));
  1098. }
  1099. /**
  1100. * g_socket_new_from_fd:
  1101. * @fd: a native socket file descriptor.
  1102. * @error: #GError for error reporting, or %NULL to ignore.
  1103. *
  1104. * Creates a new #GSocket from a native file descriptor
  1105. * or winsock SOCKET handle.
  1106. *
  1107. * This reads all the settings from the file descriptor so that
  1108. * all properties should work. Note that the file descriptor
  1109. * will be set to non-blocking mode, independent on the blocking
  1110. * mode of the #GSocket.
  1111. *
  1112. * On success, the returned #GSocket takes ownership of @fd. On failure, the
  1113. * caller must close @fd themselves.
  1114. *
  1115. * Since GLib 2.46, it is no longer a fatal error to call this on a non-socket
  1116. * descriptor. Instead, a GError will be set with code %G_IO_ERROR_FAILED
  1117. *
  1118. * Returns: a #GSocket or %NULL on error.
  1119. * Free the returned object with g_object_unref().
  1120. *
  1121. * Since: 2.22
  1122. */
  1123. GSocket *
  1124. g_socket_new_from_fd (gint fd,
  1125. GError **error)
  1126. {
  1127. return G_SOCKET (g_initable_new (G_TYPE_SOCKET,
  1128. NULL, error,
  1129. "fd", fd,
  1130. NULL));
  1131. }
  1132. /**
  1133. * g_socket_set_blocking:
  1134. * @socket: a #GSocket.
  1135. * @blocking: Whether to use blocking I/O or not.
  1136. *
  1137. * Sets the blocking mode of the socket. In blocking mode
  1138. * all operations (which don’t take an explicit blocking parameter) block until
  1139. * they succeed or there is an error. In
  1140. * non-blocking mode all functions return results immediately or
  1141. * with a %G_IO_ERROR_WOULD_BLOCK error.
  1142. *
  1143. * All sockets are created in blocking mode. However, note that the
  1144. * platform level socket is always non-blocking, and blocking mode
  1145. * is a GSocket level feature.
  1146. *
  1147. * Since: 2.22
  1148. */
  1149. void
  1150. g_socket_set_blocking (GSocket *socket,
  1151. gboolean blocking)
  1152. {
  1153. g_return_if_fail (G_IS_SOCKET (socket));
  1154. blocking = !!blocking;
  1155. if (socket->priv->blocking == blocking)
  1156. return;
  1157. socket->priv->blocking = blocking;
  1158. g_object_notify (G_OBJECT (socket), "blocking");
  1159. }
  1160. /**
  1161. * g_socket_get_blocking:
  1162. * @socket: a #GSocket.
  1163. *
  1164. * Gets the blocking mode of the socket. For details on blocking I/O,
  1165. * see g_socket_set_blocking().
  1166. *
  1167. * Returns: %TRUE if blocking I/O is used, %FALSE otherwise.
  1168. *
  1169. * Since: 2.22
  1170. */
  1171. gboolean
  1172. g_socket_get_blocking (GSocket *socket)
  1173. {
  1174. g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
  1175. return socket->priv->blocking;
  1176. }
  1177. /**
  1178. * g_socket_set_keepalive:
  1179. * @socket: a #GSocket.
  1180. * @keepalive: Value for the keepalive flag
  1181. *
  1182. * Sets or unsets the %SO_KEEPALIVE flag on the underlying socket. When
  1183. * this flag is set on a socket, the system will attempt to verify that the
  1184. * remote socket endpoint is still present if a sufficiently long period of
  1185. * time passes with no data being exchanged. If the system is unable to
  1186. * verify the presence of the remote endpoint, it will automatically close
  1187. * the connection.
  1188. *
  1189. * This option is only functional on certain kinds of sockets. (Notably,
  1190. * %G_SOCKET_PROTOCOL_TCP sockets.)
  1191. *
  1192. * The exact time between pings is system- and protocol-dependent, but will
  1193. * normally be at least two hours. Most commonly, you would set this flag
  1194. * on a server socket if you want to allow clients to remain idle for long
  1195. * periods of time, but also want to ensure that connections are eventually
  1196. * garbage-collected if clients crash or become unreachable.
  1197. *
  1198. * Since: 2.22
  1199. */
  1200. void
  1201. g_socket_set_keepalive (GSocket *socket,
  1202. gboolean keepalive)
  1203. {
  1204. GError *error = NULL;
  1205. g_return_if_fail (G_IS_SOCKET (socket));
  1206. keepalive = !!keepalive;
  1207. if (socket->priv->keepalive == keepalive)
  1208. return;
  1209. if (!g_socket_set_option (socket, SOL_SOCKET, SO_KEEPALIVE,
  1210. keepalive, &error))
  1211. {
  1212. g_warning ("error setting keepalive: %s", error->message);
  1213. g_error_free (error);
  1214. return;
  1215. }
  1216. socket->priv->keepalive = keepalive;
  1217. g_object_notify (G_OBJECT (socket), "keepalive");
  1218. }
  1219. /**
  1220. * g_socket_get_keepalive:
  1221. * @socket: a #GSocket.
  1222. *
  1223. * Gets the keepalive mode of the socket. For details on this,
  1224. * see g_socket_set_keepalive().
  1225. *
  1226. * Returns: %TRUE if keepalive is active, %FALSE otherwise.
  1227. *
  1228. * Since: 2.22
  1229. */
  1230. gboolean
  1231. g_socket_get_keepalive (GSocket *socket)
  1232. {
  1233. g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
  1234. return socket->priv->keepalive;
  1235. }
  1236. /**
  1237. * g_socket_get_listen_backlog:
  1238. * @socket: a #GSocket.
  1239. *
  1240. * Gets the listen backlog setting of the socket. For details on this,
  1241. * see g_socket_set_listen_backlog().
  1242. *
  1243. * Returns: the maximum number of pending connections.
  1244. *
  1245. * Since: 2.22
  1246. */
  1247. gint
  1248. g_socket_get_listen_backlog (GSocket *socket)
  1249. {
  1250. g_return_val_if_fail (G_IS_SOCKET (socket), 0);
  1251. return socket->priv->listen_backlog;
  1252. }
  1253. /**
  1254. * g_socket_set_listen_backlog:
  1255. * @socket: a #GSocket.
  1256. * @backlog: the maximum number of pending connections.
  1257. *
  1258. * Sets the maximum number of outstanding connections allowed
  1259. * when listening on this socket. If more clients than this are
  1260. * connecting to the socket and the application is not handling them
  1261. * on time then the new connections will be refused.
  1262. *
  1263. * Note that this must be called before g_socket_listen() and has no
  1264. * effect if called after that.
  1265. *
  1266. * Since: 2.22
  1267. */
  1268. void
  1269. g_socket_set_listen_backlog (GSocket *socket,
  1270. gint backlog)
  1271. {
  1272. g_return_if_fail (G_IS_SOCKET (socket));
  1273. g_return_if_fail (!socket->priv->listening);
  1274. if (backlog != socket->priv->listen_backlog)
  1275. {
  1276. socket->priv->listen_backlog = backlog;
  1277. g_object_notify (G_OBJECT (socket), "listen-backlog");
  1278. }
  1279. }
  1280. /**
  1281. * g_socket_get_timeout:
  1282. * @socket: a #GSocket.
  1283. *
  1284. * Gets the timeout setting of the socket. For details on this, see
  1285. * g_socket_set_timeout().
  1286. *
  1287. * Returns: the timeout in seconds
  1288. *
  1289. * Since: 2.26
  1290. */
  1291. guint
  1292. g_socket_get_timeout (GSocket *socket)
  1293. {
  1294. g_return_val_if_fail (G_IS_SOCKET (socket), 0);
  1295. return socket->priv->timeout;
  1296. }
  1297. /**
  1298. * g_socket_set_timeout:
  1299. * @socket: a #GSocket.
  1300. * @timeout: the timeout for @socket, in seconds, or 0 for none
  1301. *
  1302. * Sets the time in seconds after which I/O operations on @socket will
  1303. * time out if they have not yet completed.
  1304. *
  1305. * On a blocking socket, this means that any blocking #GSocket
  1306. * operation will time out after @timeout seconds of inactivity,
  1307. * returning %G_IO_ERROR_TIMED_OUT.
  1308. *
  1309. * On a non-blocking socket, calls to g_socket_condition_wait() will
  1310. * also fail with %G_IO_ERROR_TIMED_OUT after the given time. Sources
  1311. * created with g_socket_create_source() will trigger after
  1312. * @timeout seconds of inactivity, with the requested condition
  1313. * set, at which point calling g_socket_receive(), g_socket_send(),
  1314. * g_socket_check_connect_result(), etc, will fail with
  1315. * %G_IO_ERROR_TIMED_OUT.
  1316. *
  1317. * If @timeout is 0 (the default), operations will never time out
  1318. * on their own.
  1319. *
  1320. * Note that if an I/O operation is interrupted by a signal, this may
  1321. * cause the timeout to be reset.
  1322. *
  1323. * Since: 2.26
  1324. */
  1325. void
  1326. g_socket_set_timeout (GSocket *socket,
  1327. guint timeout)
  1328. {
  1329. g_return_if_fail (G_IS_SOCKET (socket));
  1330. if (timeout != socket->priv->timeout)
  1331. {
  1332. socket->priv->timeout = timeout;
  1333. g_object_notify (G_OBJECT (socket), "timeout");
  1334. }
  1335. }
  1336. /**
  1337. * g_socket_get_ttl:
  1338. * @socket: a #GSocket.
  1339. *
  1340. * Gets the unicast time-to-live setting on @socket; see
  1341. * g_socket_set_ttl() for more details.
  1342. *
  1343. * Returns: the time-to-live setting on @socket
  1344. *
  1345. * Since: 2.32
  1346. */
  1347. guint
  1348. g_socket_get_ttl (GSocket *socket)
  1349. {
  1350. GError *error = NULL;
  1351. gint value;
  1352. g_return_val_if_fail (G_IS_SOCKET (socket), 0);
  1353. if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
  1354. {
  1355. g_socket_get_option (socket, IPPROTO_IP, IP_TTL,
  1356. &value, &error);
  1357. }
  1358. else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
  1359. {
  1360. g_socket_get_option (socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
  1361. &value, &error);
  1362. }
  1363. else
  1364. g_return_val_if_reached (0);
  1365. if (error)
  1366. {
  1367. g_warning ("error getting unicast ttl: %s", error->message);
  1368. g_error_free (error);
  1369. return 0;
  1370. }
  1371. return value;
  1372. }
  1373. /**
  1374. * g_socket_set_ttl:
  1375. * @socket: a #GSocket.
  1376. * @ttl: the time-to-live value for all unicast packets on @socket
  1377. *
  1378. * Sets the time-to-live for outgoing unicast packets on @socket.
  1379. * By default the platform-specific default value is used.
  1380. *
  1381. * Since: 2.32
  1382. */
  1383. void
  1384. g_socket_set_ttl (GSocket *socket,
  1385. guint ttl)
  1386. {
  1387. GError *error = NULL;
  1388. g_return_if_fail (G_IS_SOCKET (socket));
  1389. if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
  1390. {
  1391. g_socket_set_option (socket, IPPROTO_IP, IP_TTL,
  1392. ttl, &error);
  1393. }
  1394. else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
  1395. {
  1396. g_socket_set_option (socket, IPPROTO_IP, IP_TTL,
  1397. ttl, NULL);
  1398. g_socket_set_option (socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
  1399. ttl, &error);
  1400. }
  1401. else
  1402. g_return_if_reached ();
  1403. if (error)
  1404. {
  1405. g_warning ("error setting unicast ttl: %s", error->message);
  1406. g_error_free (error);
  1407. return;
  1408. }
  1409. g_object_notify (G_OBJECT (socket), "ttl");
  1410. }
  1411. /**
  1412. * g_socket_get_broadcast:
  1413. * @socket: a #GSocket.
  1414. *
  1415. * Gets the broadcast setting on @socket; if %TRUE,
  1416. * it is possible to send packets to broadcast
  1417. * addresses.
  1418. *
  1419. * Returns: the broadcast setting on @socket
  1420. *
  1421. * Since: 2.32
  1422. */
  1423. gboolean
  1424. g_socket_get_broadcast (GSocket *socket)
  1425. {
  1426. GError *error = NULL;
  1427. gint value;
  1428. g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
  1429. if (!g_socket_get_option (socket, SOL_SOCKET, SO_BROADCAST,
  1430. &value, &error))
  1431. {
  1432. g_warning ("error getting broadcast: %s", error->message

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