LCOV - code coverage report
Current view: top level - glib/gio - gsocket.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 819 1391 58.9 %
Date: 2024-04-23 05:16:05 Functions: 78 104 75.0 %
Branches: 404 803 50.3 %

           Branch data     Line data    Source code
       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                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       9                 :            :  *
      10                 :            :  * This library is free software; you can redistribute it and/or
      11                 :            :  * modify it under the terms of the GNU Lesser General Public
      12                 :            :  * License as published by the Free Software Foundation; either
      13                 :            :  * version 2.1 of the License, or (at your option) any later version.
      14                 :            :  *
      15                 :            :  * This library is distributed in the hope that it will be useful,
      16                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      18                 :            :  * Lesser General Public License for more details.
      19                 :            :  *
      20                 :            :  * You should have received a copy of the GNU Lesser General
      21                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      22                 :            :  *
      23                 :            :  * Authors: Christian Kellner <gicmo@gnome.org>
      24                 :            :  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
      25                 :            :  *          Ryan Lortie <desrt@desrt.ca>
      26                 :            :  *          Alexander Larsson <alexl@redhat.com>
      27                 :            :  *          Philip Withnall <philip.withnall@collabora.co.uk>
      28                 :            :  */
      29                 :            : 
      30                 :            : #include "config.h"
      31                 :            : 
      32                 :            : #include "gsocket.h"
      33                 :            : 
      34                 :            : #ifdef G_OS_UNIX
      35                 :            : #include "glib-unix.h"
      36                 :            : #endif
      37                 :            : 
      38                 :            : #include <errno.h>
      39                 :            : #include <signal.h>
      40                 :            : #include <string.h>
      41                 :            : #include <stdlib.h>
      42                 :            : 
      43                 :            : #ifndef G_OS_WIN32
      44                 :            : # include <fcntl.h>
      45                 :            : # include <unistd.h>
      46                 :            : # include <sys/ioctl.h>
      47                 :            : #endif
      48                 :            : 
      49                 :            : #ifdef HAVE_SIOCGIFADDR
      50                 :            : #include <net/if.h>
      51                 :            : #endif
      52                 :            : 
      53                 :            : #ifdef HAVE_SYS_FILIO_H
      54                 :            : # include <sys/filio.h>
      55                 :            : #endif
      56                 :            : 
      57                 :            : #ifdef G_OS_UNIX
      58                 :            : #include <sys/uio.h>
      59                 :            : #endif
      60                 :            : 
      61                 :            : #define GOBJECT_COMPILATION
      62                 :            : #include "gobject/gtype-private.h" /* For _PRELUDE type define */
      63                 :            : #undef GOBJECT_COMPILATION
      64                 :            : #include "gcancellable.h"
      65                 :            : #include "gdatagrambased.h"
      66                 :            : #include "gioenumtypes.h"
      67                 :            : #include "ginetaddress.h"
      68                 :            : #include "ginetsocketaddress.h"
      69                 :            : #include "ginitable.h"
      70                 :            : #include "gioerror.h"
      71                 :            : #include "gioenums.h"
      72                 :            : #include "gioerror.h"
      73                 :            : #include "gnetworkingprivate.h"
      74                 :            : #include "gsocketaddress.h"
      75                 :            : #include "gsocketcontrolmessage.h"
      76                 :            : #include "gcredentials.h"
      77                 :            : #include "gcredentialsprivate.h"
      78                 :            : #include "glibintl.h"
      79                 :            : #include "gioprivate.h"
      80                 :            : 
      81                 :            : #ifdef G_OS_WIN32
      82                 :            : #include "giowin32-afunix.h"
      83                 :            : #endif
      84                 :            : 
      85                 :            : /**
      86                 :            :  * GSocket:
      87                 :            :  *
      88                 :            :  * A `GSocket` is a low-level networking primitive. It is a more or less
      89                 :            :  * direct mapping of the BSD socket API in a portable GObject based API.
      90                 :            :  * It supports both the UNIX socket implementations and winsock2 on Windows.
      91                 :            :  *
      92                 :            :  * `GSocket` is the platform independent base upon which the higher level
      93                 :            :  * network primitives are based. Applications are not typically meant to
      94                 :            :  * use it directly, but rather through classes like [class@Gio.SocketClient],
      95                 :            :  * [class@Gio.SocketService] and [class@Gio.SocketConnection]. However there may
      96                 :            :  * be cases where direct use of `GSocket` is useful.
      97                 :            :  *
      98                 :            :  * `GSocket` implements the [iface@Gio.Initable] interface, so if it is manually
      99                 :            :  * constructed by e.g. [ctor@GObject.Object.new] you must call
     100                 :            :  * [method@Gio.Initable.init] and check the results before using the object.
     101                 :            :  * This is done automatically in [ctor@Gio.Socket.new] and
     102                 :            :  * [ctor@Gio.Socket.new_from_fd], so these functions can return `NULL`.
     103                 :            :  *
     104                 :            :  * Sockets operate in two general modes, blocking or non-blocking. When
     105                 :            :  * in blocking mode all operations (which don’t take an explicit blocking
     106                 :            :  * parameter) block until the requested operation
     107                 :            :  * is finished or there is an error. In non-blocking mode all calls that
     108                 :            :  * would block return immediately with a `G_IO_ERROR_WOULD_BLOCK` error.
     109                 :            :  * To know when a call would successfully run you can call
     110                 :            :  * [method@Gio.Socket.condition_check], or [method@Gio.Socket.condition_wait].
     111                 :            :  * You can also use [method@Gio.Socket.create_source] and attach it to a
     112                 :            :  * [type@GLib.MainContext] to get callbacks when I/O is possible.
     113                 :            :  * Note that all sockets are always set to non blocking mode in the system, and
     114                 :            :  * blocking mode is emulated in `GSocket`.
     115                 :            :  *
     116                 :            :  * When working in non-blocking mode applications should always be able to
     117                 :            :  * handle getting a `G_IO_ERROR_WOULD_BLOCK` error even when some other
     118                 :            :  * function said that I/O was possible. This can easily happen in case
     119                 :            :  * of a race condition in the application, but it can also happen for other
     120                 :            :  * reasons. For instance, on Windows a socket is always seen as writable
     121                 :            :  * until a write returns `G_IO_ERROR_WOULD_BLOCK`.
     122                 :            :  *
     123                 :            :  * `GSocket`s can be either connection oriented or datagram based.
     124                 :            :  * For connection oriented types you must first establish a connection by
     125                 :            :  * either connecting to an address or accepting a connection from another
     126                 :            :  * address. For connectionless socket types the target/source address is
     127                 :            :  * specified or received in each I/O operation.
     128                 :            :  *
     129                 :            :  * All socket file descriptors are set to be close-on-exec.
     130                 :            :  *
     131                 :            :  * Note that creating a `GSocket` causes the signal `SIGPIPE` to be
     132                 :            :  * ignored for the remainder of the program. If you are writing a
     133                 :            :  * command-line utility that uses `GSocket`, you may need to take into
     134                 :            :  * account the fact that your program will not automatically be killed
     135                 :            :  * if it tries to write to `stdout` after it has been closed.
     136                 :            :  *
     137                 :            :  * Like most other APIs in GLib, `GSocket` is not inherently thread safe. To use
     138                 :            :  * a `GSocket` concurrently from multiple threads, you must implement your own
     139                 :            :  * locking.
     140                 :            :  *
     141                 :            :  * ## Nagle’s algorithm
     142                 :            :  *
     143                 :            :  * Since GLib 2.80, `GSocket` will automatically set the `TCP_NODELAY` option on
     144                 :            :  * all `G_SOCKET_TYPE_STREAM` sockets. This disables
     145                 :            :  * [Nagle’s algorithm](https://en.wikipedia.org/wiki/Nagle%27s_algorithm) as it
     146                 :            :  * typically does more harm than good on modern networks.
     147                 :            :  *
     148                 :            :  * If your application needs Nagle’s algorithm enabled, call
     149                 :            :  * [method@Gio.Socket.set_option] after constructing a `GSocket` to enable it:
     150                 :            :  * ```c
     151                 :            :  * socket = g_socket_new (…, G_SOCKET_TYPE_STREAM, …);
     152                 :            :  * if (socket != NULL)
     153                 :            :  *   {
     154                 :            :  *     g_socket_set_option (socket, IPPROTO_TCP, TCP_NODELAY, FALSE, &local_error);
     155                 :            :  *     // handle error if needed
     156                 :            :  *   }
     157                 :            :  * ```
     158                 :            :  *
     159                 :            :  * Since: 2.22
     160                 :            :  */
     161                 :            : 
     162                 :            : static void     g_socket_initable_iface_init (GInitableIface  *iface);
     163                 :            : static gboolean g_socket_initable_init       (GInitable       *initable,
     164                 :            :                                               GCancellable    *cancellable,
     165                 :            :                                               GError         **error);
     166                 :            : 
     167                 :            : static void     g_socket_datagram_based_iface_init       (GDatagramBasedInterface *iface);
     168                 :            : static gint     g_socket_datagram_based_receive_messages (GDatagramBased  *self,
     169                 :            :                                                           GInputMessage   *messages,
     170                 :            :                                                           guint            num_messages,
     171                 :            :                                                           gint             flags,
     172                 :            :                                                           gint64           timeout_us,
     173                 :            :                                                           GCancellable    *cancellable,
     174                 :            :                                                           GError         **error);
     175                 :            : static gint     g_socket_datagram_based_send_messages    (GDatagramBased  *self,
     176                 :            :                                                           GOutputMessage  *messages,
     177                 :            :                                                           guint            num_messages,
     178                 :            :                                                           gint             flags,
     179                 :            :                                                           gint64           timeout_us,
     180                 :            :                                                           GCancellable    *cancellable,
     181                 :            :                                                           GError         **error);
     182                 :            : static GSource *g_socket_datagram_based_create_source    (GDatagramBased           *self,
     183                 :            :                                                           GIOCondition              condition,
     184                 :            :                                                           GCancellable             *cancellable);
     185                 :            : static GIOCondition g_socket_datagram_based_condition_check      (GDatagramBased   *datagram_based,
     186                 :            :                                                                   GIOCondition      condition);
     187                 :            : static gboolean     g_socket_datagram_based_condition_wait       (GDatagramBased   *datagram_based,
     188                 :            :                                                                   GIOCondition      condition,
     189                 :            :                                                                   gint64            timeout_us,
     190                 :            :                                                                   GCancellable     *cancellable,
     191                 :            :                                                                   GError          **error);
     192                 :            : 
     193                 :            : static GSocketAddress *
     194                 :            : cache_recv_address (GSocket *socket, struct sockaddr *native, size_t native_len);
     195                 :            : 
     196                 :            : static gssize
     197                 :            : g_socket_receive_message_with_timeout  (GSocket                 *socket,
     198                 :            :                                         GSocketAddress         **address,
     199                 :            :                                         GInputVector            *vectors,
     200                 :            :                                         gint                     num_vectors,
     201                 :            :                                         GSocketControlMessage ***messages,
     202                 :            :                                         gint                    *num_messages,
     203                 :            :                                         gint                    *flags,
     204                 :            :                                         gint64                   timeout_us,
     205                 :            :                                         GCancellable            *cancellable,
     206                 :            :                                         GError                 **error);
     207                 :            : static gint
     208                 :            : g_socket_receive_messages_with_timeout (GSocket        *socket,
     209                 :            :                                         GInputMessage  *messages,
     210                 :            :                                         guint           num_messages,
     211                 :            :                                         gint            flags,
     212                 :            :                                         gint64          timeout_us,
     213                 :            :                                         GCancellable   *cancellable,
     214                 :            :                                         GError        **error);
     215                 :            : static gint
     216                 :            : g_socket_send_messages_with_timeout    (GSocket        *socket,
     217                 :            :                                         GOutputMessage *messages,
     218                 :            :                                         guint           num_messages,
     219                 :            :                                         gint            flags,
     220                 :            :                                         gint64          timeout_us,
     221                 :            :                                         GCancellable   *cancellable,
     222                 :            :                                         GError        **error);
     223                 :            : 
     224                 :            : enum
     225                 :            : {
     226                 :            :   PROP_0,
     227                 :            :   PROP_FAMILY,
     228                 :            :   PROP_TYPE,
     229                 :            :   PROP_PROTOCOL,
     230                 :            :   PROP_FD,
     231                 :            :   PROP_BLOCKING,
     232                 :            :   PROP_LISTEN_BACKLOG,
     233                 :            :   PROP_KEEPALIVE,
     234                 :            :   PROP_LOCAL_ADDRESS,
     235                 :            :   PROP_REMOTE_ADDRESS,
     236                 :            :   PROP_TIMEOUT,
     237                 :            :   PROP_TTL,
     238                 :            :   PROP_BROADCAST,
     239                 :            :   PROP_MULTICAST_LOOPBACK,
     240                 :            :   PROP_MULTICAST_TTL
     241                 :            : };
     242                 :            : 
     243                 :            : /* Size of the receiver cache for g_socket_receive_from() */
     244                 :            : #define RECV_ADDR_CACHE_SIZE 8
     245                 :            : 
     246                 :            : struct _GSocketPrivate
     247                 :            : {
     248                 :            :   GSocketFamily   family;
     249                 :            :   GSocketType     type;
     250                 :            :   GSocketProtocol protocol;
     251                 :            :   gint            fd;
     252                 :            :   gint            listen_backlog;
     253                 :            :   guint           timeout;
     254                 :            :   GError         *construct_error;
     255                 :            :   GSocketAddress *remote_address;
     256                 :            :   guint           inited : 1;
     257                 :            :   guint           blocking : 1;
     258                 :            :   guint           keepalive : 1;
     259                 :            :   guint           closed : 1;
     260                 :            :   guint           connected_read : 1;
     261                 :            :   guint           connected_write : 1;
     262                 :            :   guint           listening : 1;
     263                 :            :   guint           timed_out : 1;
     264                 :            :   guint           connect_pending : 1;
     265                 :            : #ifdef G_OS_WIN32
     266                 :            :   WSAEVENT        event;
     267                 :            :   gboolean        waiting;
     268                 :            :   DWORD           waiting_result;
     269                 :            :   int             current_events;
     270                 :            :   int             current_errors;
     271                 :            :   int             selected_events;
     272                 :            :   GList          *requested_conditions; /* list of requested GIOCondition * */
     273                 :            :   GMutex          win32_source_lock;
     274                 :            :   GCond           win32_source_cond;
     275                 :            : #endif
     276                 :            : 
     277                 :            :   struct {
     278                 :            :     GSocketAddress *addr;
     279                 :            :     struct sockaddr *native;
     280                 :            :     gsize native_len;
     281                 :            :     guint64 last_used;
     282                 :            :   } recv_addr_cache[RECV_ADDR_CACHE_SIZE];
     283                 :            : };
     284                 :            : 
     285   [ +  +  +  -  :     263268 : _G_DEFINE_TYPE_EXTENDED_WITH_PRELUDE (GSocket, g_socket, G_TYPE_OBJECT, 0,
                   +  + ]
     286                 :            :                                       /* Need a prelude for https://bugzilla.gnome.org/show_bug.cgi?id=674885 */
     287                 :            :                                       g_type_ensure (G_TYPE_SOCKET_FAMILY);
     288                 :            :                                       g_type_ensure (G_TYPE_SOCKET_TYPE);
     289                 :            :                                       g_type_ensure (G_TYPE_SOCKET_PROTOCOL);
     290                 :            :                                       g_type_ensure (G_TYPE_SOCKET_ADDRESS);
     291                 :            :                                       /* And networking init is appropriate for the prelude */
     292                 :            :                                       g_networking_init ();
     293                 :            :                                       , /* And now the regular type init code */
     294                 :            :                                       G_ADD_PRIVATE (GSocket)
     295                 :            :                                       G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
     296                 :            :                                                              g_socket_initable_iface_init);
     297                 :            :                                       G_IMPLEMENT_INTERFACE (G_TYPE_DATAGRAM_BASED,
     298                 :            :                                                              g_socket_datagram_based_iface_init));
     299                 :            : 
     300                 :            : static int
     301                 :       6779 : get_socket_errno (void)
     302                 :            : {
     303                 :            : #ifndef G_OS_WIN32
     304                 :       6779 :   return errno;
     305                 :            : #else
     306                 :            :   return WSAGetLastError ();
     307                 :            : #endif
     308                 :            : }
     309                 :            : 
     310                 :            : static GIOErrorEnum
     311                 :       1642 : socket_io_error_from_errno (int err)
     312                 :            : {
     313                 :            : #ifdef G_OS_WIN32
     314                 :            :   return g_io_error_from_win32_error (err);
     315                 :            : #else
     316                 :       1642 :   return g_io_error_from_errno (err);
     317                 :            : #endif
     318                 :            : }
     319                 :            : 
     320                 :            : static const char *
     321                 :       1642 : socket_strerror (int err)
     322                 :            : {
     323                 :            : #ifndef G_OS_WIN32
     324                 :       1642 :   return g_strerror (err);
     325                 :            : #else
     326                 :            :   const char *msg_ret;
     327                 :            :   char *msg;
     328                 :            : 
     329                 :            :   msg = g_win32_error_message (err);
     330                 :            : 
     331                 :            :   msg_ret = g_intern_string (msg);
     332                 :            :   g_free (msg);
     333                 :            : 
     334                 :            :   return msg_ret;
     335                 :            : #endif
     336                 :            : }
     337                 :            : 
     338                 :            : /* Wrapper around g_set_error() to avoid doing excess work */
     339                 :            : #define socket_set_error_lazy(err, errsv, fmt)                          \
     340                 :            :   G_STMT_START {                                                        \
     341                 :            :     GError **__err = (err);                                             \
     342                 :            :     int __errsv = (errsv);                                              \
     343                 :            :                                                                         \
     344                 :            :     if (__err)                                                          \
     345                 :            :       {                                                                 \
     346                 :            :         int __code = socket_io_error_from_errno (__errsv);              \
     347                 :            :         const char *__strerr = socket_strerror (__errsv);               \
     348                 :            :                                                                         \
     349                 :            :         if (__code == G_IO_ERROR_WOULD_BLOCK)                           \
     350                 :            :           g_set_error_literal (__err, G_IO_ERROR, __code, __strerr);    \
     351                 :            :         else                                                            \
     352                 :            :           g_set_error (__err, G_IO_ERROR, __code, fmt, __strerr);       \
     353                 :            :       }                                                                 \
     354                 :            :   } G_STMT_END
     355                 :            : 
     356                 :            : #ifdef G_OS_WIN32
     357                 :            : #define win32_unset_event_mask(_socket, _mask) _win32_unset_event_mask (_socket, _mask)
     358                 :            : static void
     359                 :            : _win32_unset_event_mask (GSocket *socket, int mask)
     360                 :            : {
     361                 :            :   g_mutex_lock (&socket->priv->win32_source_lock);
     362                 :            :   socket->priv->current_events &= ~mask;
     363                 :            :   socket->priv->current_errors &= ~mask;
     364                 :            :   g_mutex_unlock (&socket->priv->win32_source_lock);
     365                 :            : }
     366                 :            : #else
     367                 :            : #define win32_unset_event_mask(_socket, _mask)
     368                 :            : #endif
     369                 :            : 
     370                 :            : /* Windows has broken prototypes... */
     371                 :            : #ifdef G_OS_WIN32
     372                 :            : #define getsockopt(sockfd, level, optname, optval, optlen) \
     373                 :            :   getsockopt (sockfd, level, optname, (gpointer) optval, (int*) optlen)
     374                 :            : #define setsockopt(sockfd, level, optname, optval, optlen) \
     375                 :            :   setsockopt (sockfd, level, optname, (gpointer) optval, optlen)
     376                 :            : #define getsockname(sockfd, addr, addrlen) \
     377                 :            :   getsockname (sockfd, addr, (int *)addrlen)
     378                 :            : #define getpeername(sockfd, addr, addrlen) \
     379                 :            :   getpeername (sockfd, addr, (int *)addrlen)
     380                 :            : #define recv(sockfd, buf, len, flags) \
     381                 :            :   recv (sockfd, (gpointer)buf, len, flags)
     382                 :            : #endif
     383                 :            : 
     384                 :            : static gchar *
     385                 :          1 : address_to_string (GSocketAddress *address)
     386                 :            : {
     387                 :          1 :   GString *ret = g_string_new ("");
     388                 :            : 
     389   [ -  +  +  -  :          1 :   if (G_IS_INET_SOCKET_ADDRESS (address))
             +  -  +  - ]
     390                 :            :     {
     391                 :          1 :       GInetSocketAddress *isa = G_INET_SOCKET_ADDRESS (address);
     392                 :          1 :       GInetAddress *ia = g_inet_socket_address_get_address (isa);
     393                 :          1 :       GSocketFamily family = g_inet_address_get_family (ia);
     394                 :            :       gchar *tmp;
     395                 :            : 
     396                 :            :       /* Represent IPv6 addresses in URL style:
     397                 :            :        * ::1 port 12345 -> [::1]:12345 */
     398         [ -  + ]:          1 :       if (family == G_SOCKET_FAMILY_IPV6)
     399                 :            :         g_string_append_c (ret, '[');
     400                 :            : 
     401                 :          1 :       tmp = g_inet_address_to_string (ia);
     402                 :            :       g_string_append (ret, tmp);
     403                 :          1 :       g_free (tmp);
     404                 :            : 
     405         [ -  + ]:          1 :       if (family == G_SOCKET_FAMILY_IPV6)
     406                 :            :         {
     407                 :          0 :           guint32 scope = g_inet_socket_address_get_scope_id (isa);
     408                 :            : 
     409         [ #  # ]:          0 :           if (scope != 0)
     410                 :          0 :             g_string_append_printf (ret, "%%%u", scope);
     411                 :            : 
     412                 :            :           g_string_append_c (ret, ']');
     413                 :            :         }
     414                 :            : 
     415                 :            :       g_string_append_c (ret, ':');
     416                 :            : 
     417                 :          1 :       g_string_append_printf (ret, "%u", g_inet_socket_address_get_port (isa));
     418                 :            :     }
     419                 :            :   else
     420                 :            :     {
     421                 :            :       /* For unknown address types, just show the type */
     422                 :          0 :       g_string_append_printf (ret, "(%s)", G_OBJECT_TYPE_NAME (address));
     423                 :            :     }
     424                 :            : 
     425                 :          1 :   return g_string_free (ret, FALSE);
     426                 :            : }
     427                 :            : 
     428                 :            : static gboolean
     429                 :     179514 : check_socket (GSocket *socket,
     430                 :            :               GError **error)
     431                 :            : {
     432         [ -  + ]:     179514 :   if (!socket->priv->inited)
     433                 :            :     {
     434                 :          0 :       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
     435                 :            :                            _("Invalid socket, not initialized"));
     436                 :          0 :       return FALSE;
     437                 :            :     }
     438                 :            : 
     439         [ -  + ]:     179514 :   if (socket->priv->construct_error)
     440                 :            :     {
     441                 :          0 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
     442                 :            :                    _("Invalid socket, initialization failed due to: %s"),
     443                 :          0 :                    socket->priv->construct_error->message);
     444                 :          0 :       return FALSE;
     445                 :            :     }
     446                 :            : 
     447         [ +  + ]:     179514 :   if (socket->priv->closed)
     448                 :            :     {
     449                 :         19 :       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
     450                 :            :                            _("Socket is already closed"));
     451                 :         19 :       return FALSE;
     452                 :            :     }
     453                 :            : 
     454                 :     179495 :   return TRUE;
     455                 :            : }
     456                 :            : 
     457                 :            : static gboolean
     458                 :     107180 : check_timeout (GSocket *socket,
     459                 :            :                GError **error)
     460                 :            : {
     461         [ +  + ]:     107180 :   if (socket->priv->timed_out)
     462                 :            :     {
     463                 :          1 :       socket->priv->timed_out = FALSE;
     464                 :          1 :       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
     465                 :            :                            _("Socket I/O timed out"));
     466                 :          1 :       return FALSE;
     467                 :            :     }
     468                 :            : 
     469                 :     107179 :   return TRUE;
     470                 :            : }
     471                 :            : 
     472                 :            : static void
     473                 :        327 : g_socket_details_from_fd (GSocket *socket)
     474                 :            : {
     475                 :            :   union {
     476                 :            :     struct sockaddr_storage storage;
     477                 :            :     struct sockaddr sa;
     478                 :            :   } address;
     479                 :            :   gint fd;
     480                 :            :   socklen_t addrlen;
     481                 :            :   int value, family;
     482                 :            :   int errsv;
     483                 :            : 
     484                 :        327 :   memset (&address, 0, sizeof (address));
     485                 :            : 
     486                 :        327 :   fd = socket->priv->fd;
     487         [ -  + ]:        327 :   if (!g_socket_get_option (socket, SOL_SOCKET, SO_TYPE, &value, NULL))
     488                 :            :     {
     489                 :          0 :       errsv = get_socket_errno ();
     490                 :          0 :       goto err;
     491                 :            :     }
     492                 :            : 
     493   [ +  -  -  + ]:        327 :   switch (value)
     494                 :            :     {
     495                 :        285 :      case SOCK_STREAM:
     496                 :        285 :       socket->priv->type = G_SOCKET_TYPE_STREAM;
     497                 :        285 :       break;
     498                 :            : 
     499                 :          0 :      case SOCK_DGRAM:
     500                 :          0 :       socket->priv->type = G_SOCKET_TYPE_DATAGRAM;
     501                 :          0 :       break;
     502                 :            : 
     503                 :          0 :      case SOCK_SEQPACKET:
     504                 :          0 :       socket->priv->type = G_SOCKET_TYPE_SEQPACKET;
     505                 :          0 :       break;
     506                 :            : 
     507                 :         42 :      default:
     508                 :         42 :       socket->priv->type = G_SOCKET_TYPE_INVALID;
     509                 :         42 :       break;
     510                 :            :     }
     511                 :            : 
     512                 :        327 :   addrlen = sizeof address;
     513         [ -  + ]:        327 :   if (getsockname (fd, &address.sa, &addrlen) != 0)
     514                 :            :     {
     515                 :          0 :       errsv = get_socket_errno ();
     516                 :          0 :       goto err;
     517                 :            :     }
     518                 :            : 
     519         [ +  - ]:        327 :   if (addrlen > 0)
     520                 :            :     {
     521                 :        327 :       g_assert (G_STRUCT_OFFSET (struct sockaddr, sa_family) +
     522                 :            :                 (socklen_t) sizeof address.storage.ss_family <= addrlen);
     523                 :        327 :       family = address.storage.ss_family;
     524                 :            :     }
     525                 :            :   else
     526                 :            :     {
     527                 :            :       /* On Solaris, this happens if the socket is not yet connected.
     528                 :            :        * But we can use SO_DOMAIN as a workaround there.
     529                 :            :        */
     530                 :            : #ifdef SO_DOMAIN
     531         [ #  # ]:          0 :       if (!g_socket_get_option (socket, SOL_SOCKET, SO_DOMAIN, &family, NULL))
     532                 :            :         {
     533                 :          0 :           errsv = get_socket_errno ();
     534                 :          0 :           goto err;
     535                 :            :         }
     536                 :            : #else
     537                 :            :       /* This will translate to G_IO_ERROR_FAILED on either unix or windows */
     538                 :            :       errsv = -1;
     539                 :            :       goto err;
     540                 :            : #endif
     541                 :            :     }
     542                 :            : 
     543      [ +  +  + ]:        327 :   switch (family)
     544                 :            :     {
     545                 :        120 :      case G_SOCKET_FAMILY_IPV4:
     546                 :            :      case G_SOCKET_FAMILY_IPV6:
     547                 :        120 :        socket->priv->family = address.storage.ss_family;
     548   [ +  -  -  - ]:        120 :        switch (socket->priv->type)
     549                 :            :          {
     550                 :        120 :          case G_SOCKET_TYPE_STREAM:
     551                 :        120 :            socket->priv->protocol = G_SOCKET_PROTOCOL_TCP;
     552                 :        120 :            break;
     553                 :            : 
     554                 :          0 :          case G_SOCKET_TYPE_DATAGRAM:
     555                 :          0 :            socket->priv->protocol = G_SOCKET_PROTOCOL_UDP;
     556                 :          0 :            break;
     557                 :            : 
     558                 :          0 :          case G_SOCKET_TYPE_SEQPACKET:
     559                 :          0 :            socket->priv->protocol = G_SOCKET_PROTOCOL_SCTP;
     560                 :          0 :            break;
     561                 :            : 
     562                 :          0 :          default:
     563                 :          0 :            break;
     564                 :            :          }
     565                 :        120 :        break;
     566                 :            : 
     567                 :        165 :      case G_SOCKET_FAMILY_UNIX:
     568                 :        165 :        socket->priv->family = G_SOCKET_FAMILY_UNIX;
     569                 :        165 :        socket->priv->protocol = G_SOCKET_PROTOCOL_DEFAULT;
     570                 :        165 :        break;
     571                 :            : 
     572                 :         42 :      default:
     573                 :         42 :        socket->priv->family = G_SOCKET_FAMILY_INVALID;
     574                 :         42 :        break;
     575                 :            :     }
     576                 :            : 
     577         [ +  + ]:        327 :   if (socket->priv->family != G_SOCKET_FAMILY_INVALID)
     578                 :            :     {
     579                 :        285 :       addrlen = sizeof address;
     580         [ +  + ]:        285 :       if (getpeername (fd, &address.sa, &addrlen) >= 0)
     581                 :            :         {
     582                 :        283 :           socket->priv->connected_read = TRUE;
     583                 :        283 :           socket->priv->connected_write = TRUE;
     584                 :            :         }
     585                 :            :     }
     586                 :            : 
     587         [ +  - ]:        327 :   if (g_socket_get_option (socket, SOL_SOCKET, SO_KEEPALIVE, &value, NULL))
     588                 :            :     {
     589                 :        327 :       socket->priv->keepalive = !!value;
     590                 :            :     }
     591                 :            :   else
     592                 :            :     {
     593                 :            :       /* Can't read, maybe not supported, assume FALSE */
     594                 :          0 :       socket->priv->keepalive = FALSE;
     595                 :            :     }
     596                 :            : 
     597                 :        327 :   return;
     598                 :            : 
     599                 :          0 :  err:
     600                 :          0 :   g_set_error (&socket->priv->construct_error, G_IO_ERROR,
     601                 :          0 :                socket_io_error_from_errno (errsv),
     602                 :            :                _("creating GSocket from fd: %s"),
     603                 :            :                socket_strerror (errsv));
     604                 :            : }
     605                 :            : 
     606                 :            : static void
     607                 :        327 : socket_set_nonblock (int fd)
     608                 :            : {
     609                 :            : #ifndef G_OS_WIN32
     610                 :        327 :   GError *error = NULL;
     611                 :            : #else
     612                 :            :   gulong arg;
     613                 :            : #endif
     614                 :            : 
     615                 :            :   /* Always use native nonblocking sockets, as Windows sets sockets to
     616                 :            :    * nonblocking automatically in certain operations. This way we make
     617                 :            :    * things work the same on all platforms.
     618                 :            :    */
     619                 :            : #ifndef G_OS_WIN32
     620         [ -  + ]:        327 :   if (!g_unix_set_fd_nonblocking (fd, TRUE, &error))
     621                 :            :     {
     622                 :          0 :       g_warning ("Error setting socket to nonblocking mode: %s", error->message);
     623                 :          0 :       g_clear_error (&error);
     624                 :            :     }
     625                 :            : #else
     626                 :            :   arg = TRUE;
     627                 :            : 
     628                 :            :   if (ioctlsocket (fd, FIONBIO, &arg) == SOCKET_ERROR)
     629                 :            :     {
     630                 :            :       int errsv = get_socket_errno ();
     631                 :            :       g_warning ("Error setting socket status flags: %s", socket_strerror (errsv));
     632                 :            :     }
     633                 :            : #endif
     634                 :        327 : }
     635                 :            : 
     636                 :            : /* Wrapper around socket() that is shared with gnetworkmonitornetlink.c.
     637                 :            :  * It always sets SOCK_CLOEXEC | SOCK_NONBLOCK. */
     638                 :            : gint
     639                 :       1399 : g_socket (gint     domain,
     640                 :            :           gint     type,
     641                 :            :           gint     protocol,
     642                 :            :           GError **error)
     643                 :            : {
     644                 :            :   int fd, errsv;
     645                 :            : 
     646                 :            : #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
     647                 :       1399 :   fd = socket (domain, type | SOCK_CLOEXEC | SOCK_NONBLOCK, protocol);
     648                 :       1399 :   errsv = errno;
     649         [ +  - ]:       1399 :   if (fd != -1)
     650                 :       1399 :     return fd;
     651                 :            : 
     652                 :            :   /* It's possible that libc has SOCK_CLOEXEC and/or SOCK_NONBLOCK but the kernel does not */
     653   [ #  #  #  #  :          0 :   if (fd < 0 && (errsv == EINVAL || errsv == EPROTOTYPE))
                   #  # ]
     654                 :            : #endif
     655                 :          0 :     fd = socket (domain, type, protocol);
     656                 :            : 
     657         [ #  # ]:          0 :   if (fd < 0)
     658                 :            :     {
     659                 :          0 :       errsv = get_socket_errno ();
     660                 :            : 
     661                 :          0 :       g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
     662                 :            :                    _("Unable to create socket: %s"), socket_strerror (errsv));
     663                 :          0 :       errno = errsv;
     664                 :          0 :       return -1;
     665                 :            :     }
     666                 :            : 
     667                 :            : #ifndef G_OS_WIN32
     668                 :            :   {
     669                 :            :     int flags;
     670                 :            : 
     671                 :            :     /* We always want to set close-on-exec to protect users. If you
     672                 :            :        need to so some weird inheritance to exec you can re-enable this
     673                 :            :        using lower level hacks with g_socket_get_fd(). */
     674                 :          0 :     flags = fcntl (fd, F_GETFD, 0);
     675         [ #  # ]:          0 :     if (flags != -1 &&
     676         [ #  # ]:          0 :         (flags & FD_CLOEXEC) == 0)
     677                 :            :       {
     678                 :          0 :         flags |= FD_CLOEXEC;
     679                 :          0 :         (void) fcntl (fd, F_SETFD, flags);
     680                 :            :       }
     681                 :            :   }
     682                 :            : #else
     683                 :            :   if ((domain == AF_INET || domain == AF_INET6) && type == SOCK_DGRAM)
     684                 :            :     {
     685                 :            :       BOOL new_behavior = FALSE;
     686                 :            :       DWORD bytes_returned = 0;
     687                 :            : 
     688                 :            :       /* Disable connection reset error on ICMP port unreachable. */
     689                 :            :       WSAIoctl (fd, SIO_UDP_CONNRESET, &new_behavior, sizeof (new_behavior),
     690                 :            :                 NULL, 0, &bytes_returned, NULL, NULL);
     691                 :            :     }
     692                 :            : #endif
     693                 :            : 
     694                 :            :   /* Ensure the socket is non-blocking. */
     695                 :          0 :   socket_set_nonblock (fd);
     696                 :            : 
     697                 :          0 :   return fd;
     698                 :            : }
     699                 :            : 
     700                 :            : /* Returned socket has SOCK_CLOEXEC | SOCK_NONBLOCK set. */
     701                 :            : static gint
     702                 :       1358 : g_socket_create_socket (GSocketFamily   family,
     703                 :            :                         GSocketType     type,
     704                 :            :                         int             protocol,
     705                 :            :                         GError        **error)
     706                 :            : {
     707                 :            :   gint native_type;
     708                 :            : 
     709   [ +  +  -  - ]:       1358 :   switch (type)
     710                 :            :     {
     711                 :       1346 :      case G_SOCKET_TYPE_STREAM:
     712                 :       1346 :       native_type = SOCK_STREAM;
     713                 :       1346 :       break;
     714                 :            : 
     715                 :         12 :      case G_SOCKET_TYPE_DATAGRAM:
     716                 :         12 :       native_type = SOCK_DGRAM;
     717                 :         12 :       break;
     718                 :            : 
     719                 :          0 :      case G_SOCKET_TYPE_SEQPACKET:
     720                 :          0 :       native_type = SOCK_SEQPACKET;
     721                 :          0 :       break;
     722                 :            : 
     723                 :          0 :      default:
     724                 :            :       g_assert_not_reached ();
     725                 :            :     }
     726                 :            : 
     727         [ +  + ]:       1358 :   if (family <= 0)
     728                 :            :     {
     729                 :          1 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
     730                 :            :                    _("Unable to create socket: %s"), _("Unknown family was specified"));
     731                 :          1 :       return -1;
     732                 :            :     }
     733                 :            : 
     734         [ -  + ]:       1357 :   if (protocol == -1)
     735                 :            :     {
     736                 :          0 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
     737                 :            :                    _("Unable to create socket: %s"), _("Unknown protocol was specified"));
     738                 :          0 :       return -1;
     739                 :            :     }
     740                 :            : 
     741                 :       1357 :   return g_socket (family, native_type, protocol, error);
     742                 :            : }
     743                 :            : 
     744                 :            : static void
     745                 :       1685 : g_socket_constructed (GObject *object)
     746                 :            : {
     747                 :       1685 :   GSocket *socket = G_SOCKET (object);
     748                 :            : 
     749         [ +  + ]:       1685 :   if (socket->priv->fd >= 0)
     750                 :            :     {
     751                 :            :       /* create socket->priv info from the fd and ensure it’s non-blocking */
     752                 :        327 :       g_socket_details_from_fd (socket);
     753                 :        327 :       socket_set_nonblock (socket->priv->fd);
     754                 :            :     }
     755                 :            :   else
     756                 :            :     {
     757                 :            :       /* create the fd from socket->priv info; this sets it non-blocking by construction */
     758                 :       1358 :       socket->priv->fd = g_socket_create_socket (socket->priv->family,
     759                 :       1358 :                                                  socket->priv->type,
     760                 :       1358 :                                                  socket->priv->protocol,
     761                 :       1358 :                                                  &socket->priv->construct_error);
     762                 :            :     }
     763                 :            : 
     764         [ +  + ]:       1685 :   if (socket->priv->fd != -1)
     765                 :            :     {
     766                 :            : #ifdef SO_NOSIGPIPE
     767                 :            :       /* See note about SIGPIPE below. */
     768                 :            :       g_socket_set_option (socket, SOL_SOCKET, SO_NOSIGPIPE, TRUE, NULL);
     769                 :            : #endif
     770         [ +  + ]:       1684 :       if (socket->priv->type == G_SOCKET_TYPE_STREAM)
     771                 :       1630 :         g_socket_set_option (socket, IPPROTO_TCP, TCP_NODELAY, TRUE, NULL);
     772                 :            :     }
     773                 :       1685 : }
     774                 :            : 
     775                 :            : static void
     776                 :          0 : g_socket_get_property (GObject    *object,
     777                 :            :                        guint       prop_id,
     778                 :            :                        GValue     *value,
     779                 :            :                        GParamSpec *pspec)
     780                 :            : {
     781                 :          0 :   GSocket *socket = G_SOCKET (object);
     782                 :            :   GSocketAddress *address;
     783                 :            : 
     784   [ #  #  #  #  :          0 :   switch (prop_id)
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     785                 :            :     {
     786                 :          0 :       case PROP_FAMILY:
     787                 :          0 :         g_value_set_enum (value, socket->priv->family);
     788                 :          0 :         break;
     789                 :            : 
     790                 :          0 :       case PROP_TYPE:
     791                 :          0 :         g_value_set_enum (value, socket->priv->type);
     792                 :          0 :         break;
     793                 :            : 
     794                 :          0 :       case PROP_PROTOCOL:
     795                 :          0 :         g_value_set_enum (value, socket->priv->protocol);
     796                 :          0 :         break;
     797                 :            : 
     798                 :          0 :       case PROP_FD:
     799                 :          0 :         g_value_set_int (value, socket->priv->fd);
     800                 :          0 :         break;
     801                 :            : 
     802                 :          0 :       case PROP_BLOCKING:
     803                 :          0 :         g_value_set_boolean (value, socket->priv->blocking);
     804                 :          0 :         break;
     805                 :            : 
     806                 :          0 :       case PROP_LISTEN_BACKLOG:
     807                 :          0 :         g_value_set_int (value, socket->priv->listen_backlog);
     808                 :          0 :         break;
     809                 :            : 
     810                 :          0 :       case PROP_KEEPALIVE:
     811                 :          0 :         g_value_set_boolean (value, socket->priv->keepalive);
     812                 :          0 :         break;
     813                 :            : 
     814                 :          0 :       case PROP_LOCAL_ADDRESS:
     815                 :          0 :         address = g_socket_get_local_address (socket, NULL);
     816                 :          0 :         g_value_take_object (value, address);
     817                 :          0 :         break;
     818                 :            : 
     819                 :          0 :       case PROP_REMOTE_ADDRESS:
     820                 :          0 :         address = g_socket_get_remote_address (socket, NULL);
     821                 :          0 :         g_value_take_object (value, address);
     822                 :          0 :         break;
     823                 :            : 
     824                 :          0 :       case PROP_TIMEOUT:
     825                 :          0 :         g_value_set_uint (value, socket->priv->timeout);
     826                 :          0 :         break;
     827                 :            : 
     828                 :          0 :       case PROP_TTL:
     829                 :          0 :         g_value_set_uint (value, g_socket_get_ttl (socket));
     830                 :          0 :         break;
     831                 :            : 
     832                 :          0 :       case PROP_BROADCAST:
     833                 :          0 :         g_value_set_boolean (value, g_socket_get_broadcast (socket));
     834                 :          0 :         break;
     835                 :            : 
     836                 :          0 :       case PROP_MULTICAST_LOOPBACK:
     837                 :          0 :         g_value_set_boolean (value, g_socket_get_multicast_loopback (socket));
     838                 :          0 :         break;
     839                 :            : 
     840                 :          0 :       case PROP_MULTICAST_TTL:
     841                 :          0 :         g_value_set_uint (value, g_socket_get_multicast_ttl (socket));
     842                 :          0 :         break;
     843                 :            : 
     844                 :          0 :       default:
     845                 :          0 :         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     846                 :            :     }
     847                 :          0 : }
     848                 :            : 
     849                 :            : static void
     850                 :       6740 : g_socket_set_property (GObject      *object,
     851                 :            :                        guint         prop_id,
     852                 :            :                        const GValue *value,
     853                 :            :                        GParamSpec   *pspec)
     854                 :            : {
     855                 :       6740 :   GSocket *socket = G_SOCKET (object);
     856                 :            : 
     857   [ +  +  +  +  :       6740 :   switch (prop_id)
          -  -  -  -  -  
             -  -  -  - ]
     858                 :            :     {
     859                 :       1685 :       case PROP_FAMILY:
     860                 :       1685 :         socket->priv->family = g_value_get_enum (value);
     861                 :       1685 :         break;
     862                 :            : 
     863                 :       1685 :       case PROP_TYPE:
     864                 :       1685 :         socket->priv->type = g_value_get_enum (value);
     865                 :       1685 :         break;
     866                 :            : 
     867                 :       1685 :       case PROP_PROTOCOL:
     868                 :       1685 :         socket->priv->protocol = g_value_get_enum (value);
     869                 :       1685 :         break;
     870                 :            : 
     871                 :       1685 :       case PROP_FD:
     872                 :       1685 :         socket->priv->fd = g_value_get_int (value);
     873                 :       1685 :         break;
     874                 :            : 
     875                 :          0 :       case PROP_BLOCKING:
     876                 :          0 :         g_socket_set_blocking (socket, g_value_get_boolean (value));
     877                 :          0 :         break;
     878                 :            : 
     879                 :          0 :       case PROP_LISTEN_BACKLOG:
     880                 :          0 :         g_socket_set_listen_backlog (socket, g_value_get_int (value));
     881                 :          0 :         break;
     882                 :            : 
     883                 :          0 :       case PROP_KEEPALIVE:
     884                 :          0 :         g_socket_set_keepalive (socket, g_value_get_boolean (value));
     885                 :          0 :         break;
     886                 :            : 
     887                 :          0 :       case PROP_TIMEOUT:
     888                 :          0 :         g_socket_set_timeout (socket, g_value_get_uint (value));
     889                 :          0 :         break;
     890                 :            : 
     891                 :          0 :       case PROP_TTL:
     892                 :          0 :         g_socket_set_ttl (socket, g_value_get_uint (value));
     893                 :          0 :         break;
     894                 :            : 
     895                 :          0 :       case PROP_BROADCAST:
     896                 :          0 :         g_socket_set_broadcast (socket, g_value_get_boolean (value));
     897                 :          0 :         break;
     898                 :            : 
     899                 :          0 :       case PROP_MULTICAST_LOOPBACK:
     900                 :          0 :         g_socket_set_multicast_loopback (socket, g_value_get_boolean (value));
     901                 :          0 :         break;
     902                 :            : 
     903                 :          0 :       case PROP_MULTICAST_TTL:
     904                 :          0 :         g_socket_set_multicast_ttl (socket, g_value_get_uint (value));
     905                 :          0 :         break;
     906                 :            : 
     907                 :          0 :       default:
     908                 :          0 :         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     909                 :            :     }
     910                 :       6740 : }
     911                 :            : 
     912                 :            : static void
     913                 :       1402 : g_socket_finalize (GObject *object)
     914                 :            : {
     915                 :       1402 :   GSocket *socket = G_SOCKET (object);
     916                 :            :   gint i;
     917                 :            : 
     918                 :       1402 :   g_clear_error (&socket->priv->construct_error);
     919                 :            : 
     920         [ +  + ]:       1402 :   if (socket->priv->fd != -1 &&
     921         [ +  - ]:        145 :       !socket->priv->closed)
     922                 :        145 :     g_socket_close (socket, NULL);
     923                 :            : 
     924         [ -  + ]:       1402 :   if (socket->priv->remote_address)
     925                 :          0 :     g_object_unref (socket->priv->remote_address);
     926                 :            : 
     927                 :            : #ifdef G_OS_WIN32
     928                 :            :   if (socket->priv->event != WSA_INVALID_EVENT)
     929                 :            :     {
     930                 :            :       WSACloseEvent (socket->priv->event);
     931                 :            :       socket->priv->event = WSA_INVALID_EVENT;
     932                 :            :     }
     933                 :            : 
     934                 :            :   g_assert (socket->priv->requested_conditions == NULL);
     935                 :            :   g_mutex_clear (&socket->priv->win32_source_lock);
     936                 :            :   g_cond_clear (&socket->priv->win32_source_cond);
     937                 :            : #endif
     938                 :            : 
     939         [ +  + ]:      12618 :   for (i = 0; i < RECV_ADDR_CACHE_SIZE; i++)
     940                 :            :     {
     941         [ +  + ]:      11216 :       if (socket->priv->recv_addr_cache[i].addr)
     942                 :            :         {
     943                 :         25 :           g_object_unref (socket->priv->recv_addr_cache[i].addr);
     944                 :         25 :           g_free (socket->priv->recv_addr_cache[i].native);
     945                 :            :         }
     946                 :            :     }
     947                 :            : 
     948         [ +  - ]:       1402 :   if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
     949                 :       1401 :     (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
     950                 :       1402 : }
     951                 :            : 
     952                 :            : static void
     953                 :        132 : g_socket_class_init (GSocketClass *klass)
     954                 :            : {
     955                 :        132 :   GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
     956                 :            : 
     957                 :            : #ifdef SIGPIPE
     958                 :            :   /* There is no portable, thread-safe way to avoid having the process
     959                 :            :    * be killed by SIGPIPE when calling send() or sendmsg(), so we are
     960                 :            :    * forced to simply ignore the signal process-wide.
     961                 :            :    *
     962                 :            :    * Even if we ignore it though, gdb will still stop if the app
     963                 :            :    * receives a SIGPIPE, which can be confusing and annoying. So when
     964                 :            :    * possible, we also use MSG_NOSIGNAL / SO_NOSIGPIPE elsewhere to
     965                 :            :    * prevent the signal from occurring at all.
     966                 :            :    */
     967                 :        132 :   signal (SIGPIPE, SIG_IGN);
     968                 :            : #endif
     969                 :            : 
     970                 :        132 :   gobject_class->finalize = g_socket_finalize;
     971                 :        132 :   gobject_class->constructed = g_socket_constructed;
     972                 :        132 :   gobject_class->set_property = g_socket_set_property;
     973                 :        132 :   gobject_class->get_property = g_socket_get_property;
     974                 :            : 
     975                 :            :   /**
     976                 :            :    * GSocket:family:
     977                 :            :    *
     978                 :            :    * The socket’s address family.
     979                 :            :    *
     980                 :            :    * Since: 2.22
     981                 :            :    */
     982                 :        132 :   g_object_class_install_property (gobject_class, PROP_FAMILY,
     983                 :            :                                    g_param_spec_enum ("family", NULL, NULL,
     984                 :            :                                                       G_TYPE_SOCKET_FAMILY,
     985                 :            :                                                       G_SOCKET_FAMILY_INVALID,
     986                 :            :                                                       G_PARAM_CONSTRUCT_ONLY |
     987                 :            :                                                       G_PARAM_READWRITE |
     988                 :            :                                                       G_PARAM_STATIC_STRINGS));
     989                 :            : 
     990                 :            :   /**
     991                 :            :    * GSocket:type:
     992                 :            :    *
     993                 :            :    * The socket’s type.
     994                 :            :    *
     995                 :            :    * Since: 2.22
     996                 :            :    */
     997                 :        132 :   g_object_class_install_property (gobject_class, PROP_TYPE,
     998                 :            :                                    g_param_spec_enum ("type", NULL, NULL,
     999                 :            :                                                       G_TYPE_SOCKET_TYPE,
    1000                 :            :                                                       G_SOCKET_TYPE_STREAM,
    1001                 :            :                                                       G_PARAM_CONSTRUCT_ONLY |
    1002                 :            :                                                       G_PARAM_READWRITE |
    1003                 :            :                                                       G_PARAM_STATIC_STRINGS));
    1004                 :            : 
    1005                 :            :   /**
    1006                 :            :    * GSocket:protocol:
    1007                 :            :    *
    1008                 :            :    * The ID of the protocol to use, or `-1` for unknown.
    1009                 :            :    *
    1010                 :            :    * Since: 2.22
    1011                 :            :    */
    1012                 :        132 :   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
    1013                 :            :                                    g_param_spec_enum ("protocol", NULL, NULL,
    1014                 :            :                                                       G_TYPE_SOCKET_PROTOCOL,
    1015                 :            :                                                       G_SOCKET_PROTOCOL_UNKNOWN,
    1016                 :            :                                                       G_PARAM_CONSTRUCT_ONLY |
    1017                 :            :                                                       G_PARAM_READWRITE |
    1018                 :            :                                                       G_PARAM_STATIC_STRINGS));
    1019                 :            : 
    1020                 :            :   /**
    1021                 :            :    * GSocket:fd:
    1022                 :            :    *
    1023                 :            :    * The socket’s file descriptor.
    1024                 :            :    *
    1025                 :            :    * Since: 2.22
    1026                 :            :    */
    1027                 :        132 :   g_object_class_install_property (gobject_class, PROP_FD,
    1028                 :            :                                    g_param_spec_int ("fd", NULL, NULL,
    1029                 :            :                                                      G_MININT,
    1030                 :            :                                                      G_MAXINT,
    1031                 :            :                                                      -1,
    1032                 :            :                                                      G_PARAM_CONSTRUCT_ONLY |
    1033                 :            :                                                      G_PARAM_READWRITE |
    1034                 :            :                                                      G_PARAM_STATIC_STRINGS));
    1035                 :            : 
    1036                 :            :   /**
    1037                 :            :    * GSocket:blocking:
    1038                 :            :    *
    1039                 :            :    * Whether I/O on this socket is blocking.
    1040                 :            :    *
    1041                 :            :    * Since: 2.22
    1042                 :            :    */
    1043                 :        132 :   g_object_class_install_property (gobject_class, PROP_BLOCKING,
    1044                 :            :                                    g_param_spec_boolean ("blocking", NULL, NULL,
    1045                 :            :                                                          TRUE,
    1046                 :            :                                                          G_PARAM_READWRITE |
    1047                 :            :                                                          G_PARAM_STATIC_STRINGS));
    1048                 :            : 
    1049                 :            :   /**
    1050                 :            :    * GSocket:listen-backlog:
    1051                 :            :    *
    1052                 :            :    * The number of outstanding connections in the listen queue.
    1053                 :            :    *
    1054                 :            :    * Since: 2.22
    1055                 :            :    */
    1056                 :        132 :   g_object_class_install_property (gobject_class, PROP_LISTEN_BACKLOG,
    1057                 :            :                                    g_param_spec_int ("listen-backlog", NULL, NULL,
    1058                 :            :                                                      0,
    1059                 :            :                                                      SOMAXCONN,
    1060                 :            :                                                      10,
    1061                 :            :                                                      G_PARAM_READWRITE |
    1062                 :            :                                                      G_PARAM_STATIC_STRINGS));
    1063                 :            : 
    1064                 :            :   /**
    1065                 :            :    * GSocket:keepalive:
    1066                 :            :    *
    1067                 :            :    * Whether to keep the connection alive by sending periodic pings.
    1068                 :            :    *
    1069                 :            :    * Since: 2.22
    1070                 :            :    */
    1071                 :        132 :   g_object_class_install_property (gobject_class, PROP_KEEPALIVE,
    1072                 :            :                                    g_param_spec_boolean ("keepalive", NULL, NULL,
    1073                 :            :                                                          FALSE,
    1074                 :            :                                                          G_PARAM_READWRITE |
    1075                 :            :                                                          G_PARAM_STATIC_STRINGS));
    1076                 :            : 
    1077                 :            :   /**
    1078                 :            :    * GSocket:local-address:
    1079                 :            :    *
    1080                 :            :    * The local address the socket is bound to.
    1081                 :            :    *
    1082                 :            :    * Since: 2.22
    1083                 :            :    */
    1084                 :        132 :   g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
    1085                 :            :                                    g_param_spec_object ("local-address", NULL, NULL,
    1086                 :            :                                                         G_TYPE_SOCKET_ADDRESS,
    1087                 :            :                                                         G_PARAM_READABLE |
    1088                 :            :                                                         G_PARAM_STATIC_STRINGS));
    1089                 :            : 
    1090                 :            :   /**
    1091                 :            :    * GSocket:remote-address:
    1092                 :            :    *
    1093                 :            :    * The remote address the socket is connected to.
    1094                 :            :    *
    1095                 :            :    * Since: 2.22
    1096                 :            :    */
    1097                 :        132 :   g_object_class_install_property (gobject_class, PROP_REMOTE_ADDRESS,
    1098                 :            :                                    g_param_spec_object ("remote-address", NULL, NULL,
    1099                 :            :                                                         G_TYPE_SOCKET_ADDRESS,
    1100                 :            :                                                         G_PARAM_READABLE |
    1101                 :            :                                                         G_PARAM_STATIC_STRINGS));
    1102                 :            : 
    1103                 :            :   /**
    1104                 :            :    * GSocket:timeout:
    1105                 :            :    *
    1106                 :            :    * The timeout in seconds on socket I/O
    1107                 :            :    *
    1108                 :            :    * Since: 2.26
    1109                 :            :    */
    1110                 :        132 :   g_object_class_install_property (gobject_class, PROP_TIMEOUT,
    1111                 :            :                                    g_param_spec_uint ("timeout", NULL, NULL,
    1112                 :            :                                                       0,
    1113                 :            :                                                       G_MAXUINT,
    1114                 :            :                                                       0,
    1115                 :            :                                                       G_PARAM_READWRITE |
    1116                 :            :                                                       G_PARAM_STATIC_STRINGS));
    1117                 :            : 
    1118                 :            :   /**
    1119                 :            :    * GSocket:broadcast:
    1120                 :            :    *
    1121                 :            :    * Whether the socket should allow sending to broadcast addresses.
    1122                 :            :    *
    1123                 :            :    * Since: 2.32
    1124                 :            :    */
    1125                 :        132 :   g_object_class_install_property (gobject_class, PROP_BROADCAST,
    1126                 :            :                                    g_param_spec_boolean ("broadcast", NULL, NULL,
    1127                 :            :                                                          FALSE,
    1128                 :            :                                                          G_PARAM_READWRITE |
    1129                 :            :                                                          G_PARAM_STATIC_STRINGS));
    1130                 :            : 
    1131                 :            :   /**
    1132                 :            :    * GSocket:ttl:
    1133                 :            :    *
    1134                 :            :    * Time-to-live for outgoing unicast packets
    1135                 :            :    *
    1136                 :            :    * Since: 2.32
    1137                 :            :    */
    1138                 :        132 :   g_object_class_install_property (gobject_class, PROP_TTL,
    1139                 :            :                                    g_param_spec_uint ("ttl", NULL, NULL,
    1140                 :            :                                                       0, G_MAXUINT, 0,
    1141                 :            :                                                       G_PARAM_READWRITE |
    1142                 :            :                                                       G_PARAM_STATIC_STRINGS));
    1143                 :            : 
    1144                 :            :   /**
    1145                 :            :    * GSocket:multicast-loopback:
    1146                 :            :    *
    1147                 :            :    * Whether outgoing multicast packets loop back to the local host.
    1148                 :            :    *
    1149                 :            :    * Since: 2.32
    1150                 :            :    */
    1151                 :        132 :   g_object_class_install_property (gobject_class, PROP_MULTICAST_LOOPBACK,
    1152                 :            :                                    g_param_spec_boolean ("multicast-loopback", NULL, NULL,
    1153                 :            :                                                          TRUE,
    1154                 :            :                                                          G_PARAM_READWRITE |
    1155                 :            :                                                          G_PARAM_STATIC_STRINGS));
    1156                 :            : 
    1157                 :            :   /**
    1158                 :            :    * GSocket:multicast-ttl:
    1159                 :            :    *
    1160                 :            :    * Time-to-live out outgoing multicast packets
    1161                 :            :    *
    1162                 :            :    * Since: 2.32
    1163                 :            :    */
    1164                 :        132 :   g_object_class_install_property (gobject_class, PROP_MULTICAST_TTL,
    1165                 :            :                                    g_param_spec_uint ("multicast-ttl", NULL, NULL,
    1166                 :            :                                                       0, G_MAXUINT, 1,
    1167                 :            :                                                       G_PARAM_READWRITE |
    1168                 :            :                                                       G_PARAM_STATIC_STRINGS));
    1169                 :        132 : }
    1170                 :            : 
    1171                 :            : static void
    1172                 :        132 : g_socket_initable_iface_init (GInitableIface *iface)
    1173                 :            : {
    1174                 :        132 :   iface->init = g_socket_initable_init;
    1175                 :        132 : }
    1176                 :            : 
    1177                 :            : static void
    1178                 :        132 : g_socket_datagram_based_iface_init (GDatagramBasedInterface *iface)
    1179                 :            : {
    1180                 :        132 :   iface->receive_messages = g_socket_datagram_based_receive_messages;
    1181                 :        132 :   iface->send_messages = g_socket_datagram_based_send_messages;
    1182                 :        132 :   iface->create_source = g_socket_datagram_based_create_source;
    1183                 :        132 :   iface->condition_check = g_socket_datagram_based_condition_check;
    1184                 :        132 :   iface->condition_wait = g_socket_datagram_based_condition_wait;
    1185                 :        132 : }
    1186                 :            : 
    1187                 :            : static void
    1188                 :       1685 : g_socket_init (GSocket *socket)
    1189                 :            : {
    1190                 :       1685 :   socket->priv = g_socket_get_instance_private (socket);
    1191                 :            : 
    1192                 :       1685 :   socket->priv->fd = -1;
    1193                 :       1685 :   socket->priv->blocking = TRUE;
    1194                 :       1685 :   socket->priv->listen_backlog = 10;
    1195                 :       1685 :   socket->priv->construct_error = NULL;
    1196                 :            : #ifdef G_OS_WIN32
    1197                 :            :   socket->priv->event = WSA_INVALID_EVENT;
    1198                 :            :   g_mutex_init (&socket->priv->win32_source_lock);
    1199                 :            :   g_cond_init (&socket->priv->win32_source_cond);
    1200                 :            : #endif
    1201                 :       1685 : }
    1202                 :            : 
    1203                 :            : static gboolean
    1204                 :       1685 : g_socket_initable_init (GInitable *initable,
    1205                 :            :                         GCancellable *cancellable,
    1206                 :            :                         GError  **error)
    1207                 :            : {
    1208                 :            :   GSocket  *socket;
    1209                 :            : 
    1210                 :       1685 :   g_return_val_if_fail (G_IS_SOCKET (initable), FALSE);
    1211                 :            : 
    1212                 :       1685 :   socket = G_SOCKET (initable);
    1213                 :            : 
    1214         [ -  + ]:       1685 :   if (cancellable != NULL)
    1215                 :            :     {
    1216                 :          0 :       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    1217                 :            :                            _("Cancellable initialization not supported"));
    1218                 :          0 :       return FALSE;
    1219                 :            :     }
    1220                 :            : 
    1221                 :       1685 :   socket->priv->inited = TRUE;
    1222                 :            : 
    1223         [ +  + ]:       1685 :   if (socket->priv->construct_error)
    1224                 :            :     {
    1225         [ -  + ]:          1 :       if (error)
    1226                 :          0 :         *error = g_error_copy (socket->priv->construct_error);
    1227                 :          1 :       return FALSE;
    1228                 :            :     }
    1229                 :            : 
    1230                 :            : 
    1231                 :       1684 :   return TRUE;
    1232                 :            : }
    1233                 :            : 
    1234                 :            : static gboolean
    1235                 :          0 : check_datagram_based (GDatagramBased  *self,
    1236                 :            :                       GError         **error)
    1237                 :            : {
    1238      [ #  #  # ]:          0 :   switch (g_socket_get_socket_type (G_SOCKET (self)))
    1239                 :            :     {
    1240                 :          0 :     case G_SOCKET_TYPE_INVALID:
    1241                 :            :     case G_SOCKET_TYPE_STREAM:
    1242                 :          0 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    1243                 :            :                    _("Cannot use datagram operations on a non-datagram "
    1244                 :            :                      "socket."));
    1245                 :          0 :       return FALSE;
    1246                 :          0 :     case G_SOCKET_TYPE_DATAGRAM:
    1247                 :            :     case G_SOCKET_TYPE_SEQPACKET:
    1248                 :            :       /* Fall through. */
    1249                 :          0 :       break;
    1250                 :            :     }
    1251                 :            : 
    1252                 :            :   /* Due to us sharing #GSocketSource with the #GSocket implementation, it is
    1253                 :            :    * pretty tricky to split out #GSocket:timeout so that it does not affect
    1254                 :            :    * #GDatagramBased operations (but still affects #GSocket operations). It is
    1255                 :            :    * not worth that effort — just disallow it and require the user to specify
    1256                 :            :    * timeouts on a per-operation basis. */
    1257         [ #  # ]:          0 :   if (g_socket_get_timeout (G_SOCKET (self)) != 0)
    1258                 :            :     {
    1259                 :          0 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    1260                 :            :                    _("Cannot use datagram operations on a socket with a "
    1261                 :            :                      "timeout set."));
    1262                 :          0 :       return FALSE;
    1263                 :            :     }
    1264                 :            : 
    1265                 :          0 :   return TRUE;
    1266                 :            : }
    1267                 :            : 
    1268                 :            : static gint
    1269                 :          0 : g_socket_datagram_based_receive_messages (GDatagramBased  *self,
    1270                 :            :                                           GInputMessage   *messages,
    1271                 :            :                                           guint            num_messages,
    1272                 :            :                                           gint             flags,
    1273                 :            :                                           gint64           timeout_us,
    1274                 :            :                                           GCancellable    *cancellable,
    1275                 :            :                                           GError         **error)
    1276                 :            : {
    1277         [ #  # ]:          0 :   if (!check_datagram_based (self, error))
    1278                 :          0 :     return FALSE;
    1279                 :            : 
    1280                 :          0 :   return g_socket_receive_messages_with_timeout (G_SOCKET (self), messages,
    1281                 :            :                                                  num_messages, flags, timeout_us,
    1282                 :            :                                                  cancellable, error);
    1283                 :            : }
    1284                 :            : 
    1285                 :            : static gint
    1286                 :          0 : g_socket_datagram_based_send_messages (GDatagramBased  *self,
    1287                 :            :                                        GOutputMessage  *messages,
    1288                 :            :                                        guint            num_messages,
    1289                 :            :                                        gint             flags,
    1290                 :            :                                        gint64           timeout_us,
    1291                 :            :                                        GCancellable    *cancellable,
    1292                 :            :                                        GError         **error)
    1293                 :            : {
    1294         [ #  # ]:          0 :   if (!check_datagram_based (self, error))
    1295                 :          0 :     return FALSE;
    1296                 :            : 
    1297                 :          0 :   return g_socket_send_messages_with_timeout (G_SOCKET (self), messages,
    1298                 :            :                                               num_messages, flags, timeout_us,
    1299                 :            :                                               cancellable, error);
    1300                 :            : }
    1301                 :            : 
    1302                 :            : static GSource *
    1303                 :          0 : g_socket_datagram_based_create_source (GDatagramBased  *self,
    1304                 :            :                                        GIOCondition     condition,
    1305                 :            :                                        GCancellable    *cancellable)
    1306                 :            : {
    1307         [ #  # ]:          0 :   if (!check_datagram_based (self, NULL))
    1308                 :          0 :     return NULL;
    1309                 :            : 
    1310                 :          0 :   return g_socket_create_source (G_SOCKET (self), condition, cancellable);
    1311                 :            : }
    1312                 :            : 
    1313                 :            : static GIOCondition
    1314                 :          0 : g_socket_datagram_based_condition_check (GDatagramBased  *datagram_based,
    1315                 :            :                                          GIOCondition     condition)
    1316                 :            : {
    1317         [ #  # ]:          0 :   if (!check_datagram_based (datagram_based, NULL))
    1318                 :          0 :     return G_IO_ERR;
    1319                 :            : 
    1320                 :          0 :   return g_socket_condition_check (G_SOCKET (datagram_based), condition);
    1321                 :            : }
    1322                 :            : 
    1323                 :            : static gboolean
    1324                 :          0 : g_socket_datagram_based_condition_wait (GDatagramBased  *datagram_based,
    1325                 :            :                                         GIOCondition     condition,
    1326                 :            :                                         gint64           timeout_us,
    1327                 :            :                                         GCancellable    *cancellable,
    1328                 :            :                                         GError         **error)
    1329                 :            : {
    1330         [ #  # ]:          0 :   if (!check_datagram_based (datagram_based, error))
    1331                 :          0 :     return FALSE;
    1332                 :            : 
    1333                 :          0 :   return g_socket_condition_timed_wait (G_SOCKET (datagram_based), condition,
    1334                 :            :                                         timeout_us, cancellable, error);
    1335                 :            : }
    1336                 :            : 
    1337                 :            : /**
    1338                 :            :  * g_socket_new:
    1339                 :            :  * @family: the socket family to use, e.g. %G_SOCKET_FAMILY_IPV4.
    1340                 :            :  * @type: the socket type to use.
    1341                 :            :  * @protocol: the id of the protocol to use, or 0 for default.
    1342                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    1343                 :            :  *
    1344                 :            :  * Creates a new #GSocket with the defined family, type and protocol.
    1345                 :            :  * If @protocol is 0 (%G_SOCKET_PROTOCOL_DEFAULT) the default protocol type
    1346                 :            :  * for the family and type is used.
    1347                 :            :  *
    1348                 :            :  * The @protocol is a family and type specific int that specifies what
    1349                 :            :  * kind of protocol to use. #GSocketProtocol lists several common ones.
    1350                 :            :  * Many families only support one protocol, and use 0 for this, others
    1351                 :            :  * support several and using 0 means to use the default protocol for
    1352                 :            :  * the family and type.
    1353                 :            :  *
    1354                 :            :  * The protocol id is passed directly to the operating
    1355                 :            :  * system, so you can use protocols not listed in #GSocketProtocol if you
    1356                 :            :  * know the protocol number used for it.
    1357                 :            :  *
    1358                 :            :  * Returns: a #GSocket or %NULL on error.
    1359                 :            :  *     Free the returned object with g_object_unref().
    1360                 :            :  *
    1361                 :            :  * Since: 2.22
    1362                 :            :  */
    1363                 :            : GSocket *
    1364                 :       1357 : g_socket_new (GSocketFamily     family,
    1365                 :            :               GSocketType       type,
    1366                 :            :               GSocketProtocol   protocol,
    1367                 :            :               GError          **error)
    1368                 :            : {
    1369                 :       1357 :   return G_SOCKET (g_initable_new (G_TYPE_SOCKET,
    1370                 :            :                                    NULL, error,
    1371                 :            :                                    "family", family,
    1372                 :            :                                    "type", type,
    1373                 :            :                                    "protocol", protocol,
    1374                 :            :                                    NULL));
    1375                 :            : }
    1376                 :            : 
    1377                 :            : /**
    1378                 :            :  * g_socket_new_from_fd:
    1379                 :            :  * @fd: a native socket file descriptor.
    1380                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    1381                 :            :  *
    1382                 :            :  * Creates a new #GSocket from a native file descriptor
    1383                 :            :  * or winsock SOCKET handle.
    1384                 :            :  *
    1385                 :            :  * This reads all the settings from the file descriptor so that
    1386                 :            :  * all properties should work. Note that the file descriptor
    1387                 :            :  * will be set to non-blocking mode, independent on the blocking
    1388                 :            :  * mode of the #GSocket.
    1389                 :            :  *
    1390                 :            :  * On success, the returned #GSocket takes ownership of @fd. On failure, the
    1391                 :            :  * caller must close @fd themselves.
    1392                 :            :  *
    1393                 :            :  * Since GLib 2.46, it is no longer a fatal error to call this on a non-socket
    1394                 :            :  * descriptor.  Instead, a GError will be set with code %G_IO_ERROR_FAILED
    1395                 :            :  *
    1396                 :            :  * Returns: a #GSocket or %NULL on error.
    1397                 :            :  *     Free the returned object with g_object_unref().
    1398                 :            :  *
    1399                 :            :  * Since: 2.22
    1400                 :            :  */
    1401                 :            : GSocket *
    1402                 :        327 : g_socket_new_from_fd (gint     fd,
    1403                 :            :                       GError **error)
    1404                 :            : {
    1405                 :        327 :   return G_SOCKET (g_initable_new (G_TYPE_SOCKET,
    1406                 :            :                                    NULL, error,
    1407                 :            :                                    "fd", fd,
    1408                 :            :                                    NULL));
    1409                 :            : }
    1410                 :            : 
    1411                 :            : /**
    1412                 :            :  * g_socket_set_blocking:
    1413                 :            :  * @socket: a #GSocket.
    1414                 :            :  * @blocking: Whether to use blocking I/O or not.
    1415                 :            :  *
    1416                 :            :  * Sets the blocking mode of the socket. In blocking mode
    1417                 :            :  * all operations (which don’t take an explicit blocking parameter) block until
    1418                 :            :  * they succeed or there is an error. In
    1419                 :            :  * non-blocking mode all functions return results immediately or
    1420                 :            :  * with a %G_IO_ERROR_WOULD_BLOCK error.
    1421                 :            :  *
    1422                 :            :  * All sockets are created in blocking mode. However, note that the
    1423                 :            :  * platform level socket is always non-blocking, and blocking mode
    1424                 :            :  * is a GSocket level feature.
    1425                 :            :  *
    1426                 :            :  * Since: 2.22
    1427                 :            :  */
    1428                 :            : void
    1429                 :       1524 : g_socket_set_blocking (GSocket  *socket,
    1430                 :            :                        gboolean  blocking)
    1431                 :            : {
    1432                 :       1524 :   g_return_if_fail (G_IS_SOCKET (socket));
    1433                 :            : 
    1434                 :       1524 :   blocking = !!blocking;
    1435                 :            : 
    1436         [ +  + ]:       1524 :   if (socket->priv->blocking == blocking)
    1437                 :         25 :     return;
    1438                 :            : 
    1439                 :       1499 :   socket->priv->blocking = blocking;
    1440                 :       1499 :   g_object_notify (G_OBJECT (socket), "blocking");
    1441                 :            : }
    1442                 :            : 
    1443                 :            : /**
    1444                 :            :  * g_socket_get_blocking:
    1445                 :            :  * @socket: a #GSocket.
    1446                 :            :  *
    1447                 :            :  * Gets the blocking mode of the socket. For details on blocking I/O,
    1448                 :            :  * see g_socket_set_blocking().
    1449                 :            :  *
    1450                 :            :  * Returns: %TRUE if blocking I/O is used, %FALSE otherwise.
    1451                 :            :  *
    1452                 :            :  * Since: 2.22
    1453                 :            :  */
    1454                 :            : gboolean
    1455                 :          0 : g_socket_get_blocking (GSocket *socket)
    1456                 :            : {
    1457                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    1458                 :            : 
    1459                 :          0 :   return socket->priv->blocking;
    1460                 :            : }
    1461                 :            : 
    1462                 :            : /**
    1463                 :            :  * g_socket_set_keepalive:
    1464                 :            :  * @socket: a #GSocket.
    1465                 :            :  * @keepalive: Value for the keepalive flag
    1466                 :            :  *
    1467                 :            :  * Sets or unsets the %SO_KEEPALIVE flag on the underlying socket. When
    1468                 :            :  * this flag is set on a socket, the system will attempt to verify that the
    1469                 :            :  * remote socket endpoint is still present if a sufficiently long period of
    1470                 :            :  * time passes with no data being exchanged. If the system is unable to
    1471                 :            :  * verify the presence of the remote endpoint, it will automatically close
    1472                 :            :  * the connection.
    1473                 :            :  *
    1474                 :            :  * This option is only functional on certain kinds of sockets. (Notably,
    1475                 :            :  * %G_SOCKET_PROTOCOL_TCP sockets.)
    1476                 :            :  *
    1477                 :            :  * The exact time between pings is system- and protocol-dependent, but will
    1478                 :            :  * normally be at least two hours. Most commonly, you would set this flag
    1479                 :            :  * on a server socket if you want to allow clients to remain idle for long
    1480                 :            :  * periods of time, but also want to ensure that connections are eventually
    1481                 :            :  * garbage-collected if clients crash or become unreachable.
    1482                 :            :  *
    1483                 :            :  * Since: 2.22
    1484                 :            :  */
    1485                 :            : void
    1486                 :          0 : g_socket_set_keepalive (GSocket  *socket,
    1487                 :            :                         gboolean  keepalive)
    1488                 :            : {
    1489                 :          0 :   GError *error = NULL;
    1490                 :            : 
    1491                 :          0 :   g_return_if_fail (G_IS_SOCKET (socket));
    1492                 :            : 
    1493                 :          0 :   keepalive = !!keepalive;
    1494         [ #  # ]:          0 :   if (socket->priv->keepalive == keepalive)
    1495                 :          0 :     return;
    1496                 :            : 
    1497         [ #  # ]:          0 :   if (!g_socket_set_option (socket, SOL_SOCKET, SO_KEEPALIVE,
    1498                 :            :                             keepalive, &error))
    1499                 :            :     {
    1500                 :          0 :       g_warning ("error setting keepalive: %s", error->message);
    1501                 :          0 :       g_error_free (error);
    1502                 :          0 :       return;
    1503                 :            :     }
    1504                 :            : 
    1505                 :          0 :   socket->priv->keepalive = keepalive;
    1506                 :          0 :   g_object_notify (G_OBJECT (socket), "keepalive");
    1507                 :            : }
    1508                 :            : 
    1509                 :            : /**
    1510                 :            :  * g_socket_get_keepalive:
    1511                 :            :  * @socket: a #GSocket.
    1512                 :            :  *
    1513                 :            :  * Gets the keepalive mode of the socket. For details on this,
    1514                 :            :  * see g_socket_set_keepalive().
    1515                 :            :  *
    1516                 :            :  * Returns: %TRUE if keepalive is active, %FALSE otherwise.
    1517                 :            :  *
    1518                 :            :  * Since: 2.22
    1519                 :            :  */
    1520                 :            : gboolean
    1521                 :          0 : g_socket_get_keepalive (GSocket *socket)
    1522                 :            : {
    1523                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    1524                 :            : 
    1525                 :          0 :   return socket->priv->keepalive;
    1526                 :            : }
    1527                 :            : 
    1528                 :            : /**
    1529                 :            :  * g_socket_get_listen_backlog:
    1530                 :            :  * @socket: a #GSocket.
    1531                 :            :  *
    1532                 :            :  * Gets the listen backlog setting of the socket. For details on this,
    1533                 :            :  * see g_socket_set_listen_backlog().
    1534                 :            :  *
    1535                 :            :  * Returns: the maximum number of pending connections.
    1536                 :            :  *
    1537                 :            :  * Since: 2.22
    1538                 :            :  */
    1539                 :            : gint
    1540                 :          0 : g_socket_get_listen_backlog  (GSocket *socket)
    1541                 :            : {
    1542                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), 0);
    1543                 :            : 
    1544                 :          0 :   return socket->priv->listen_backlog;
    1545                 :            : }
    1546                 :            : 
    1547                 :            : /**
    1548                 :            :  * g_socket_set_listen_backlog:
    1549                 :            :  * @socket: a #GSocket.
    1550                 :            :  * @backlog: the maximum number of pending connections.
    1551                 :            :  *
    1552                 :            :  * Sets the maximum number of outstanding connections allowed
    1553                 :            :  * when listening on this socket. If more clients than this are
    1554                 :            :  * connecting to the socket and the application is not handling them
    1555                 :            :  * on time then the new connections will be refused.
    1556                 :            :  *
    1557                 :            :  * Note that this must be called before g_socket_listen() and has no
    1558                 :            :  * effect if called after that.
    1559                 :            :  *
    1560                 :            :  * Since: 2.22
    1561                 :            :  */
    1562                 :            : void
    1563                 :         35 : g_socket_set_listen_backlog (GSocket *socket,
    1564                 :            :                              gint     backlog)
    1565                 :            : {
    1566                 :         35 :   g_return_if_fail (G_IS_SOCKET (socket));
    1567                 :         35 :   g_return_if_fail (!socket->priv->listening);
    1568                 :            : 
    1569         [ -  + ]:         35 :   if (backlog != socket->priv->listen_backlog)
    1570                 :            :     {
    1571                 :          0 :       socket->priv->listen_backlog = backlog;
    1572                 :          0 :       g_object_notify (G_OBJECT (socket), "listen-backlog");
    1573                 :            :     }
    1574                 :            : }
    1575                 :            : 
    1576                 :            : /**
    1577                 :            :  * g_socket_get_timeout:
    1578                 :            :  * @socket: a #GSocket.
    1579                 :            :  *
    1580                 :            :  * Gets the timeout setting of the socket. For details on this, see
    1581                 :            :  * g_socket_set_timeout().
    1582                 :            :  *
    1583                 :            :  * Returns: the timeout in seconds
    1584                 :            :  *
    1585                 :            :  * Since: 2.26
    1586                 :            :  */
    1587                 :            : guint
    1588                 :          2 : g_socket_get_timeout (GSocket *socket)
    1589                 :            : {
    1590                 :          2 :   g_return_val_if_fail (G_IS_SOCKET (socket), 0);
    1591                 :            : 
    1592                 :          2 :   return socket->priv->timeout;
    1593                 :            : }
    1594                 :            : 
    1595                 :            : /**
    1596                 :            :  * g_socket_set_timeout:
    1597                 :            :  * @socket: a #GSocket.
    1598                 :            :  * @timeout: the timeout for @socket, in seconds, or 0 for none
    1599                 :            :  *
    1600                 :            :  * Sets the time in seconds after which I/O operations on @socket will
    1601                 :            :  * time out if they have not yet completed.
    1602                 :            :  *
    1603                 :            :  * On a blocking socket, this means that any blocking #GSocket
    1604                 :            :  * operation will time out after @timeout seconds of inactivity,
    1605                 :            :  * returning %G_IO_ERROR_TIMED_OUT.
    1606                 :            :  *
    1607                 :            :  * On a non-blocking socket, calls to g_socket_condition_wait() will
    1608                 :            :  * also fail with %G_IO_ERROR_TIMED_OUT after the given time. Sources
    1609                 :            :  * created with g_socket_create_source() will trigger after
    1610                 :            :  * @timeout seconds of inactivity, with the requested condition
    1611                 :            :  * set, at which point calling g_socket_receive(), g_socket_send(),
    1612                 :            :  * g_socket_check_connect_result(), etc, will fail with
    1613                 :            :  * %G_IO_ERROR_TIMED_OUT.
    1614                 :            :  *
    1615                 :            :  * If @timeout is 0 (the default), operations will never time out
    1616                 :            :  * on their own.
    1617                 :            :  *
    1618                 :            :  * Note that if an I/O operation is interrupted by a signal, this may
    1619                 :            :  * cause the timeout to be reset.
    1620                 :            :  *
    1621                 :            :  * Since: 2.26
    1622                 :            :  */
    1623                 :            : void
    1624                 :         19 : g_socket_set_timeout (GSocket *socket,
    1625                 :            :                       guint    timeout)
    1626                 :            : {
    1627                 :         19 :   g_return_if_fail (G_IS_SOCKET (socket));
    1628                 :            : 
    1629         [ +  + ]:         19 :   if (timeout != socket->priv->timeout)
    1630                 :            :     {
    1631                 :         17 :       socket->priv->timeout = timeout;
    1632                 :         17 :       g_object_notify (G_OBJECT (socket), "timeout");
    1633                 :            :     }
    1634                 :            : }
    1635                 :            : 
    1636                 :            : /**
    1637                 :            :  * g_socket_get_ttl:
    1638                 :            :  * @socket: a #GSocket.
    1639                 :            :  *
    1640                 :            :  * Gets the unicast time-to-live setting on @socket; see
    1641                 :            :  * g_socket_set_ttl() for more details.
    1642                 :            :  *
    1643                 :            :  * Returns: the time-to-live setting on @socket
    1644                 :            :  *
    1645                 :            :  * Since: 2.32
    1646                 :            :  */
    1647                 :            : guint
    1648                 :          0 : g_socket_get_ttl (GSocket *socket)
    1649                 :            : {
    1650                 :          0 :   GError *error = NULL;
    1651                 :            :   gint value;
    1652                 :            : 
    1653                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), 0);
    1654                 :            : 
    1655         [ #  # ]:          0 :   if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
    1656                 :            :     {
    1657                 :          0 :       g_socket_get_option (socket, IPPROTO_IP, IP_TTL,
    1658                 :            :                            &value, &error);
    1659                 :            :     }
    1660         [ #  # ]:          0 :   else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
    1661                 :            :     {
    1662                 :          0 :       g_socket_get_option (socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
    1663                 :            :                            &value, &error);
    1664                 :            :     }
    1665                 :            :   else
    1666                 :            :     g_return_val_if_reached (0);
    1667                 :            : 
    1668         [ #  # ]:          0 :   if (error)
    1669                 :            :     {
    1670                 :          0 :       g_warning ("error getting unicast ttl: %s", error->message);
    1671                 :          0 :       g_error_free (error);
    1672                 :          0 :       return 0;
    1673                 :            :     }
    1674                 :            : 
    1675                 :          0 :   return value;
    1676                 :            : }
    1677                 :            : 
    1678                 :            : /**
    1679                 :            :  * g_socket_set_ttl:
    1680                 :            :  * @socket: a #GSocket.
    1681                 :            :  * @ttl: the time-to-live value for all unicast packets on @socket
    1682                 :            :  *
    1683                 :            :  * Sets the time-to-live for outgoing unicast packets on @socket.
    1684                 :            :  * By default the platform-specific default value is used.
    1685                 :            :  *
    1686                 :            :  * Since: 2.32
    1687                 :            :  */
    1688                 :            : void
    1689                 :          0 : g_socket_set_ttl (GSocket  *socket,
    1690                 :            :                   guint     ttl)
    1691                 :            : {
    1692                 :          0 :   GError *error = NULL;
    1693                 :            : 
    1694                 :          0 :   g_return_if_fail (G_IS_SOCKET (socket));
    1695                 :            : 
    1696         [ #  # ]:          0 :   if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
    1697                 :            :     {
    1698                 :          0 :       g_socket_set_option (socket, IPPROTO_IP, IP_TTL,
    1699                 :            :                            ttl, &error);
    1700                 :            :     }
    1701         [ #  # ]:          0 :   else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
    1702                 :            :     {
    1703                 :          0 :       g_socket_set_option (socket, IPPROTO_IP, IP_TTL,
    1704                 :            :                            ttl, NULL);
    1705                 :          0 :       g_socket_set_option (socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
    1706                 :            :                            ttl, &error);
    1707                 :            :     }
    1708                 :            :   else
    1709                 :            :     g_return_if_reached ();
    1710                 :            : 
    1711         [ #  # ]:          0 :   if (error)
    1712                 :            :     {
    1713                 :          0 :       g_warning ("error setting unicast ttl: %s", error->message);
    1714                 :          0 :       g_error_free (error);
    1715                 :          0 :       return;
    1716                 :            :     }
    1717                 :            : 
    1718                 :          0 :   g_object_notify (G_OBJECT (socket), "ttl");
    1719                 :            : }
    1720                 :            : 
    1721                 :            : /**
    1722                 :            :  * g_socket_get_broadcast:
    1723                 :            :  * @socket: a #GSocket.
    1724                 :            :  *
    1725                 :            :  * Gets the broadcast setting on @socket; if %TRUE,
    1726                 :            :  * it is possible to send packets to broadcast
    1727                 :            :  * addresses.
    1728                 :            :  *
    1729                 :            :  * Returns: the broadcast setting on @socket
    1730                 :            :  *
    1731                 :            :  * Since: 2.32
    1732                 :            :  */
    1733                 :            : gboolean
    1734                 :          0 : g_socket_get_broadcast (GSocket *socket)
    1735                 :            : {
    1736                 :          0 :   GError *error = NULL;
    1737                 :            :   gint value;
    1738                 :            : 
    1739                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    1740                 :            : 
    1741         [ #  # ]:          0 :   if (!g_socket_get_option (socket, SOL_SOCKET, SO_BROADCAST,
    1742                 :            :                             &value, &error))
    1743                 :            :     {
    1744                 :          0 :       g_warning ("error getting broadcast: %s", error->message);
    1745                 :          0 :       g_error_free (error);
    1746                 :          0 :       return FALSE;
    1747                 :            :     }
    1748                 :            : 
    1749                 :          0 :   return !!value;
    1750                 :            : }
    1751                 :            : 
    1752                 :            : /**
    1753                 :            :  * g_socket_set_broadcast:
    1754                 :            :  * @socket: a #GSocket.
    1755                 :            :  * @broadcast: whether @socket should allow sending to broadcast
    1756                 :            :  *     addresses
    1757                 :            :  *
    1758                 :            :  * Sets whether @socket should allow sending to broadcast addresses.
    1759                 :            :  * This is %FALSE by default.
    1760                 :            :  *
    1761                 :            :  * Since: 2.32
    1762                 :            :  */
    1763                 :            : void
    1764                 :          0 : g_socket_set_broadcast (GSocket    *socket,
    1765                 :            :                         gboolean    broadcast)
    1766                 :            : {
    1767                 :          0 :   GError *error = NULL;
    1768                 :            : 
    1769                 :          0 :   g_return_if_fail (G_IS_SOCKET (socket));
    1770                 :            : 
    1771                 :          0 :   broadcast = !!broadcast;
    1772                 :            : 
    1773         [ #  # ]:          0 :   if (!g_socket_set_option (socket, SOL_SOCKET, SO_BROADCAST,
    1774                 :            :                             broadcast, &error))
    1775                 :            :     {
    1776                 :          0 :       g_warning ("error setting broadcast: %s", error->message);
    1777                 :          0 :       g_error_free (error);
    1778                 :          0 :       return;
    1779                 :            :     }
    1780                 :            : 
    1781                 :          0 :   g_object_notify (G_OBJECT (socket), "broadcast");
    1782                 :            : }
    1783                 :            : 
    1784                 :            : /**
    1785                 :            :  * g_socket_get_multicast_loopback:
    1786                 :            :  * @socket: a #GSocket.
    1787                 :            :  *
    1788                 :            :  * Gets the multicast loopback setting on @socket; if %TRUE (the
    1789                 :            :  * default), outgoing multicast packets will be looped back to
    1790                 :            :  * multicast listeners on the same host.
    1791                 :            :  *
    1792                 :            :  * Returns: the multicast loopback setting on @socket
    1793                 :            :  *
    1794                 :            :  * Since: 2.32
    1795                 :            :  */
    1796                 :            : gboolean
    1797                 :          0 : g_socket_get_multicast_loopback (GSocket *socket)
    1798                 :            : {
    1799                 :          0 :   GError *error = NULL;
    1800                 :            :   gint value;
    1801                 :            : 
    1802                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    1803                 :            : 
    1804         [ #  # ]:          0 :   if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
    1805                 :            :     {
    1806                 :          0 :       g_socket_get_option (socket, IPPROTO_IP, IP_MULTICAST_LOOP,
    1807                 :            :                            &value, &error);
    1808                 :            :     }
    1809         [ #  # ]:          0 :   else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
    1810                 :            :     {
    1811                 :          0 :       g_socket_get_option (socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
    1812                 :            :                            &value, &error);
    1813                 :            :     }
    1814                 :            :   else
    1815                 :            :     g_return_val_if_reached (FALSE);
    1816                 :            : 
    1817         [ #  # ]:          0 :   if (error)
    1818                 :            :     {
    1819                 :          0 :       g_warning ("error getting multicast loopback: %s", error->message);
    1820                 :          0 :       g_error_free (error);
    1821                 :          0 :       return FALSE;
    1822                 :            :     }
    1823                 :            : 
    1824                 :          0 :   return !!value;
    1825                 :            : }
    1826                 :            : 
    1827                 :            : /**
    1828                 :            :  * g_socket_set_multicast_loopback:
    1829                 :            :  * @socket: a #GSocket.
    1830                 :            :  * @loopback: whether @socket should receive messages sent to its
    1831                 :            :  *   multicast groups from the local host
    1832                 :            :  *
    1833                 :            :  * Sets whether outgoing multicast packets will be received by sockets
    1834                 :            :  * listening on that multicast address on the same host. This is %TRUE
    1835                 :            :  * by default.
    1836                 :            :  *
    1837                 :            :  * Since: 2.32
    1838                 :            :  */
    1839                 :            : void
    1840                 :          0 : g_socket_set_multicast_loopback (GSocket    *socket,
    1841                 :            :                                  gboolean    loopback)
    1842                 :            : {
    1843                 :          0 :   GError *error = NULL;
    1844                 :            : 
    1845                 :          0 :   g_return_if_fail (G_IS_SOCKET (socket));
    1846                 :            : 
    1847                 :          0 :   loopback = !!loopback;
    1848                 :            : 
    1849         [ #  # ]:          0 :   if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
    1850                 :            :     {
    1851                 :          0 :       g_socket_set_option (socket, IPPROTO_IP, IP_MULTICAST_LOOP,
    1852                 :            :                            loopback, &error);
    1853                 :            :     }
    1854         [ #  # ]:          0 :   else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
    1855                 :            :     {
    1856                 :          0 :       g_socket_set_option (socket, IPPROTO_IP, IP_MULTICAST_LOOP,
    1857                 :            :                            loopback, NULL);
    1858                 :          0 :       g_socket_set_option (socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
    1859                 :            :                            loopback, &error);
    1860                 :            :     }
    1861                 :            :   else
    1862                 :            :     g_return_if_reached ();
    1863                 :            : 
    1864         [ #  # ]:          0 :   if (error)
    1865                 :            :     {
    1866                 :          0 :       g_warning ("error setting multicast loopback: %s", error->message);
    1867                 :          0 :       g_error_free (error);
    1868                 :          0 :       return;
    1869                 :            :     }
    1870                 :            : 
    1871                 :          0 :   g_object_notify (G_OBJECT (socket), "multicast-loopback");
    1872                 :            : }
    1873                 :            : 
    1874                 :            : /**
    1875                 :            :  * g_socket_get_multicast_ttl:
    1876                 :            :  * @socket: a #GSocket.
    1877                 :            :  *
    1878                 :            :  * Gets the multicast time-to-live setting on @socket; see
    1879                 :            :  * g_socket_set_multicast_ttl() for more details.
    1880                 :            :  *
    1881                 :            :  * Returns: the multicast time-to-live setting on @socket
    1882                 :            :  *
    1883                 :            :  * Since: 2.32
    1884                 :            :  */
    1885                 :            : guint
    1886                 :          0 : g_socket_get_multicast_ttl (GSocket *socket)
    1887                 :            : {
    1888                 :          0 :   GError *error = NULL;
    1889                 :            :   gint value;
    1890                 :            : 
    1891                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), 0);
    1892                 :            : 
    1893         [ #  # ]:          0 :   if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
    1894                 :            :     {
    1895                 :          0 :       g_socket_get_option (socket, IPPROTO_IP, IP_MULTICAST_TTL,
    1896                 :            :                            &value, &error);
    1897                 :            :     }
    1898         [ #  # ]:          0 :   else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
    1899                 :            :     {
    1900                 :          0 :       g_socket_get_option (socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
    1901                 :            :                            &value, &error);
    1902                 :            :     }
    1903                 :            :   else
    1904                 :            :     g_return_val_if_reached (FALSE);
    1905                 :            : 
    1906         [ #  # ]:          0 :   if (error)
    1907                 :            :     {
    1908                 :          0 :       g_warning ("error getting multicast ttl: %s", error->message);
    1909                 :          0 :       g_error_free (error);
    1910                 :          0 :       return FALSE;
    1911                 :            :     }
    1912                 :            : 
    1913                 :          0 :   return value;
    1914                 :            : }
    1915                 :            : 
    1916                 :            : /**
    1917                 :            :  * g_socket_set_multicast_ttl:
    1918                 :            :  * @socket: a #GSocket.
    1919                 :            :  * @ttl: the time-to-live value for all multicast datagrams on @socket
    1920                 :            :  *
    1921                 :            :  * Sets the time-to-live for outgoing multicast datagrams on @socket.
    1922                 :            :  * By default, this is 1, meaning that multicast packets will not leave
    1923                 :            :  * the local network.
    1924                 :            :  *
    1925                 :            :  * Since: 2.32
    1926                 :            :  */
    1927                 :            : void
    1928                 :          0 : g_socket_set_multicast_ttl (GSocket  *socket,
    1929                 :            :                             guint     ttl)
    1930                 :            : {
    1931                 :          0 :   GError *error = NULL;
    1932                 :            : 
    1933                 :          0 :   g_return_if_fail (G_IS_SOCKET (socket));
    1934                 :            : 
    1935         [ #  # ]:          0 :   if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
    1936                 :            :     {
    1937                 :          0 :       g_socket_set_option (socket, IPPROTO_IP, IP_MULTICAST_TTL,
    1938                 :            :                            ttl, &error);
    1939                 :            :     }
    1940         [ #  # ]:          0 :   else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
    1941                 :            :     {
    1942                 :          0 :       g_socket_set_option (socket, IPPROTO_IP, IP_MULTICAST_TTL,
    1943                 :            :                            ttl, NULL);
    1944                 :          0 :       g_socket_set_option (socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
    1945                 :            :                            ttl, &error);
    1946                 :            :     }
    1947                 :            :   else
    1948                 :            :     g_return_if_reached ();
    1949                 :            : 
    1950         [ #  # ]:          0 :   if (error)
    1951                 :            :     {
    1952                 :          0 :       g_warning ("error setting multicast ttl: %s", error->message);
    1953                 :          0 :       g_error_free (error);
    1954                 :          0 :       return;
    1955                 :            :     }
    1956                 :            : 
    1957                 :          0 :   g_object_notify (G_OBJECT (socket), "multicast-ttl");
    1958                 :            : }
    1959                 :            : 
    1960                 :            : /**
    1961                 :            :  * g_socket_get_family:
    1962                 :            :  * @socket: a #GSocket.
    1963                 :            :  *
    1964                 :            :  * Gets the socket family of the socket.
    1965                 :            :  *
    1966                 :            :  * Returns: a #GSocketFamily
    1967                 :            :  *
    1968                 :            :  * Since: 2.22
    1969                 :            :  */
    1970                 :            : GSocketFamily
    1971                 :       1533 : g_socket_get_family (GSocket *socket)
    1972                 :            : {
    1973                 :       1533 :   g_return_val_if_fail (G_IS_SOCKET (socket), G_SOCKET_FAMILY_INVALID);
    1974                 :            : 
    1975                 :       1533 :   return socket->priv->family;
    1976                 :            : }
    1977                 :            : 
    1978                 :            : /**
    1979                 :            :  * g_socket_get_socket_type:
    1980                 :            :  * @socket: a #GSocket.
    1981                 :            :  *
    1982                 :            :  * Gets the socket type of the socket.
    1983                 :            :  *
    1984                 :            :  * Returns: a #GSocketType
    1985                 :            :  *
    1986                 :            :  * Since: 2.22
    1987                 :            :  */
    1988                 :            : GSocketType
    1989                 :       1532 : g_socket_get_socket_type (GSocket *socket)
    1990                 :            : {
    1991                 :       1532 :   g_return_val_if_fail (G_IS_SOCKET (socket), G_SOCKET_TYPE_INVALID);
    1992                 :            : 
    1993                 :       1532 :   return socket->priv->type;
    1994                 :            : }
    1995                 :            : 
    1996                 :            : /**
    1997                 :            :  * g_socket_get_protocol:
    1998                 :            :  * @socket: a #GSocket.
    1999                 :            :  *
    2000                 :            :  * Gets the socket protocol id the socket was created with.
    2001                 :            :  * In case the protocol is unknown, -1 is returned.
    2002                 :            :  *
    2003                 :            :  * Returns: a protocol id, or -1 if unknown
    2004                 :            :  *
    2005                 :            :  * Since: 2.22
    2006                 :            :  */
    2007                 :            : GSocketProtocol
    2008                 :       1531 : g_socket_get_protocol (GSocket *socket)
    2009                 :            : {
    2010                 :       1531 :   g_return_val_if_fail (G_IS_SOCKET (socket), -1);
    2011                 :            : 
    2012                 :       1531 :   return socket->priv->protocol;
    2013                 :            : }
    2014                 :            : 
    2015                 :            : /**
    2016                 :            :  * g_socket_get_fd:
    2017                 :            :  * @socket: a #GSocket.
    2018                 :            :  *
    2019                 :            :  * Returns the underlying OS socket object. On unix this
    2020                 :            :  * is a socket file descriptor, and on Windows this is
    2021                 :            :  * a Winsock2 SOCKET handle. This may be useful for
    2022                 :            :  * doing platform specific or otherwise unusual operations
    2023                 :            :  * on the socket.
    2024                 :            :  *
    2025                 :            :  * Returns: the file descriptor of the socket.
    2026                 :            :  *
    2027                 :            :  * Since: 2.22
    2028                 :            :  */
    2029                 :            : int
    2030                 :          4 : g_socket_get_fd (GSocket *socket)
    2031                 :            : {
    2032                 :          4 :   g_return_val_if_fail (G_IS_SOCKET (socket), -1);
    2033                 :            : 
    2034                 :          4 :   return socket->priv->fd;
    2035                 :            : }
    2036                 :            : 
    2037                 :            : /**
    2038                 :            :  * g_socket_get_local_address:
    2039                 :            :  * @socket: a #GSocket.
    2040                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2041                 :            :  *
    2042                 :            :  * Try to get the local address of a bound socket. This is only
    2043                 :            :  * useful if the socket has been bound to a local address,
    2044                 :            :  * either explicitly or implicitly when connecting.
    2045                 :            :  *
    2046                 :            :  * Returns: (transfer full): a #GSocketAddress or %NULL on error.
    2047                 :            :  *     Free the returned object with g_object_unref().
    2048                 :            :  *
    2049                 :            :  * Since: 2.22
    2050                 :            :  */
    2051                 :            : GSocketAddress *
    2052                 :         52 : g_socket_get_local_address (GSocket  *socket,
    2053                 :            :                             GError  **error)
    2054                 :            : {
    2055                 :            :   union {
    2056                 :            :     struct sockaddr_storage storage;
    2057                 :            :     struct sockaddr sa;
    2058                 :            :   } buffer;
    2059                 :         52 :   socklen_t len = sizeof (buffer);
    2060                 :            : 
    2061                 :         52 :   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
    2062                 :            : 
    2063         [ -  + ]:         52 :   if (getsockname (socket->priv->fd, &buffer.sa, &len) < 0)
    2064                 :            :     {
    2065                 :          0 :       int errsv = get_socket_errno ();
    2066                 :          0 :       g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
    2067                 :            :                    _("could not get local address: %s"), socket_strerror (errsv));
    2068                 :          0 :       return NULL;
    2069                 :            :     }
    2070                 :            : 
    2071                 :         52 :   return g_socket_address_new_from_native (&buffer.storage, len);
    2072                 :            : }
    2073                 :            : 
    2074                 :            : /**
    2075                 :            :  * g_socket_get_remote_address:
    2076                 :            :  * @socket: a #GSocket.
    2077                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2078                 :            :  *
    2079                 :            :  * Try to get the remote address of a connected socket. This is only
    2080                 :            :  * useful for connection oriented sockets that have been connected.
    2081                 :            :  *
    2082                 :            :  * Returns: (transfer full): a #GSocketAddress or %NULL on error.
    2083                 :            :  *     Free the returned object with g_object_unref().
    2084                 :            :  *
    2085                 :            :  * Since: 2.22
    2086                 :            :  */
    2087                 :            : GSocketAddress *
    2088                 :          7 : g_socket_get_remote_address (GSocket  *socket,
    2089                 :            :                              GError  **error)
    2090                 :            : {
    2091                 :            :   union {
    2092                 :            :     struct sockaddr_storage storage;
    2093                 :            :     struct sockaddr sa;
    2094                 :            :   } buffer;
    2095                 :          7 :   socklen_t len = sizeof (buffer);
    2096                 :            : 
    2097                 :          7 :   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
    2098                 :            : 
    2099         [ +  + ]:          7 :   if (socket->priv->connect_pending)
    2100                 :            :     {
    2101         [ -  + ]:          3 :       if (!g_socket_check_connect_result (socket, error))
    2102                 :          0 :         return NULL;
    2103                 :            :       else
    2104                 :          3 :         socket->priv->connect_pending = FALSE;
    2105                 :            :     }
    2106                 :            : 
    2107         [ +  + ]:          7 :   if (!socket->priv->remote_address)
    2108                 :            :     {
    2109         [ -  + ]:          1 :       if (getpeername (socket->priv->fd, &buffer.sa, &len) < 0)
    2110                 :            :         {
    2111                 :          0 :           int errsv = get_socket_errno ();
    2112                 :          0 :           g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
    2113                 :            :                        _("could not get remote address: %s"), socket_strerror (errsv));
    2114                 :          0 :           return NULL;
    2115                 :            :         }
    2116                 :            : 
    2117                 :          1 :       socket->priv->remote_address = g_socket_address_new_from_native (&buffer.storage, len);
    2118                 :            :     }
    2119                 :            : 
    2120                 :          7 :   return g_object_ref (socket->priv->remote_address);
    2121                 :            : }
    2122                 :            : 
    2123                 :            : /**
    2124                 :            :  * g_socket_is_connected:
    2125                 :            :  * @socket: a #GSocket.
    2126                 :            :  *
    2127                 :            :  * Check whether the socket is connected. This is only useful for
    2128                 :            :  * connection-oriented sockets.
    2129                 :            :  *
    2130                 :            :  * If using g_socket_shutdown(), this function will return %TRUE until the
    2131                 :            :  * socket has been shut down for reading and writing. If you do a non-blocking
    2132                 :            :  * connect, this function will not return %TRUE until after you call
    2133                 :            :  * g_socket_check_connect_result().
    2134                 :            :  *
    2135                 :            :  * Returns: %TRUE if socket is connected, %FALSE otherwise.
    2136                 :            :  *
    2137                 :            :  * Since: 2.22
    2138                 :            :  */
    2139                 :            : gboolean
    2140                 :         15 : g_socket_is_connected (GSocket *socket)
    2141                 :            : {
    2142                 :         15 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    2143                 :            : 
    2144   [ -  +  -  - ]:         15 :   return (socket->priv->connected_read || socket->priv->connected_write);
    2145                 :            : }
    2146                 :            : 
    2147                 :            : /**
    2148                 :            :  * g_socket_listen:
    2149                 :            :  * @socket: a #GSocket.
    2150                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2151                 :            :  *
    2152                 :            :  * Marks the socket as a server socket, i.e. a socket that is used
    2153                 :            :  * to accept incoming requests using g_socket_accept().
    2154                 :            :  *
    2155                 :            :  * Before calling this the socket must be bound to a local address using
    2156                 :            :  * g_socket_bind().
    2157                 :            :  *
    2158                 :            :  * To set the maximum amount of outstanding clients, use
    2159                 :            :  * g_socket_set_listen_backlog().
    2160                 :            :  *
    2161                 :            :  * Returns: %TRUE on success, %FALSE on error.
    2162                 :            :  *
    2163                 :            :  * Since: 2.22
    2164                 :            :  */
    2165                 :            : gboolean
    2166                 :         52 : g_socket_listen (GSocket  *socket,
    2167                 :            :                  GError  **error)
    2168                 :            : {
    2169                 :         52 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    2170                 :            : 
    2171         [ -  + ]:         52 :   if (!check_socket (socket, error))
    2172                 :          0 :     return FALSE;
    2173                 :            : 
    2174         [ -  + ]:         52 :   if (listen (socket->priv->fd, socket->priv->listen_backlog) < 0)
    2175                 :            :     {
    2176                 :          0 :       int errsv = get_socket_errno ();
    2177                 :            : 
    2178                 :          0 :       g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
    2179                 :            :                    _("could not listen: %s"), socket_strerror (errsv));
    2180                 :          0 :       return FALSE;
    2181                 :            :     }
    2182                 :            : 
    2183                 :         52 :   socket->priv->listening = TRUE;
    2184                 :            : 
    2185                 :         52 :   return TRUE;
    2186                 :            : }
    2187                 :            : 
    2188                 :            : /**
    2189                 :            :  * g_socket_bind:
    2190                 :            :  * @socket: a #GSocket.
    2191                 :            :  * @address: a #GSocketAddress specifying the local address.
    2192                 :            :  * @allow_reuse: whether to allow reusing this address
    2193                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2194                 :            :  *
    2195                 :            :  * When a socket is created it is attached to an address family, but it
    2196                 :            :  * doesn't have an address in this family. g_socket_bind() assigns the
    2197                 :            :  * address (sometimes called name) of the socket.
    2198                 :            :  *
    2199                 :            :  * It is generally required to bind to a local address before you can
    2200                 :            :  * receive connections. (See g_socket_listen() and g_socket_accept() ).
    2201                 :            :  * In certain situations, you may also want to bind a socket that will be
    2202                 :            :  * used to initiate connections, though this is not normally required.
    2203                 :            :  *
    2204                 :            :  * If @socket is a TCP socket, then @allow_reuse controls the setting
    2205                 :            :  * of the `SO_REUSEADDR` socket option; normally it should be %TRUE for
    2206                 :            :  * server sockets (sockets that you will eventually call
    2207                 :            :  * g_socket_accept() on), and %FALSE for client sockets. (Failing to
    2208                 :            :  * set this flag on a server socket may cause g_socket_bind() to return
    2209                 :            :  * %G_IO_ERROR_ADDRESS_IN_USE if the server program is stopped and then
    2210                 :            :  * immediately restarted.)
    2211                 :            :  *
    2212                 :            :  * If @socket is a UDP socket, then @allow_reuse determines whether or
    2213                 :            :  * not other UDP sockets can be bound to the same address at the same
    2214                 :            :  * time. In particular, you can have several UDP sockets bound to the
    2215                 :            :  * same address, and they will all receive all of the multicast and
    2216                 :            :  * broadcast packets sent to that address. (The behavior of unicast
    2217                 :            :  * UDP packets to an address with multiple listeners is not defined.)
    2218                 :            :  *
    2219                 :            :  * Returns: %TRUE on success, %FALSE on error.
    2220                 :            :  *
    2221                 :            :  * Since: 2.22
    2222                 :            :  */
    2223                 :            : gboolean
    2224                 :         60 : g_socket_bind (GSocket         *socket,
    2225                 :            :                GSocketAddress  *address,
    2226                 :            :                gboolean         reuse_address,
    2227                 :            :                GError         **error)
    2228                 :            : {
    2229                 :            :   union {
    2230                 :            :     struct sockaddr_storage storage;
    2231                 :            :     struct sockaddr sa;
    2232                 :            :   } addr;
    2233                 :            :   gboolean so_reuseaddr;
    2234                 :            : #ifdef SO_REUSEPORT
    2235                 :            :   gboolean so_reuseport;
    2236                 :            : #endif
    2237                 :            : 
    2238                 :         60 :   g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
    2239                 :            : 
    2240         [ -  + ]:         60 :   if (!check_socket (socket, error))
    2241                 :          0 :     return FALSE;
    2242                 :            : 
    2243         [ -  + ]:         60 :   if (!g_socket_address_to_native (address, &addr.storage, sizeof addr, error))
    2244                 :          0 :     return FALSE;
    2245                 :            : 
    2246                 :            :   /* On Windows, SO_REUSEADDR has the semantics we want for UDP
    2247                 :            :    * sockets, but has nasty side effects we don't want for TCP
    2248                 :            :    * sockets.
    2249                 :            :    *
    2250                 :            :    * On other platforms, we set SO_REUSEPORT, if it exists, for
    2251                 :            :    * UDP sockets, and SO_REUSEADDR for all sockets, hoping that
    2252                 :            :    * if SO_REUSEPORT doesn't exist, then SO_REUSEADDR will have
    2253                 :            :    * the desired semantics on UDP (as it does on Linux, although
    2254                 :            :    * Linux has SO_REUSEPORT too as of 3.9).
    2255                 :            :    */
    2256                 :            : 
    2257                 :            : #ifdef G_OS_WIN32
    2258                 :            :   so_reuseaddr = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
    2259                 :            : #else
    2260                 :         60 :   so_reuseaddr = !!reuse_address;
    2261                 :            : #endif
    2262                 :            : 
    2263                 :            : #ifdef SO_REUSEPORT
    2264   [ +  +  +  + ]:         60 :   so_reuseport = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
    2265                 :            : #endif
    2266                 :            : 
    2267                 :            :   /* Ignore errors here, the only likely error is "not supported", and
    2268                 :            :    * this is a "best effort" thing mainly.
    2269                 :            :    */
    2270                 :         60 :   g_socket_set_option (socket, SOL_SOCKET, SO_REUSEADDR, so_reuseaddr, NULL);
    2271                 :            : #ifdef SO_REUSEPORT
    2272                 :         60 :   g_socket_set_option (socket, SOL_SOCKET, SO_REUSEPORT, so_reuseport, NULL);
    2273                 :            : #endif
    2274                 :            : 
    2275         [ +  + ]:         60 :   if (bind (socket->priv->fd, &addr.sa,
    2276                 :         60 :             g_socket_address_get_native_size (address)) < 0)
    2277                 :            :     {
    2278                 :          1 :       int errsv = get_socket_errno ();
    2279                 :          1 :       gchar *address_string = address_to_string (address);
    2280                 :            : 
    2281                 :          2 :       g_set_error (error,
    2282                 :          1 :                    G_IO_ERROR, socket_io_error_from_errno (errsv),
    2283                 :            :                    _("Error binding to address %s: %s"),
    2284                 :            :                    address_string, socket_strerror (errsv));
    2285                 :          1 :       g_free (address_string);
    2286                 :          1 :       return FALSE;
    2287                 :            :     }
    2288                 :            : 
    2289                 :         59 :   return TRUE;
    2290                 :            : }
    2291                 :            : 
    2292                 :            : #ifdef G_OS_WIN32
    2293                 :            : static gulong
    2294                 :            : g_socket_w32_get_adapter_ipv4_addr (const gchar *name_or_ip)
    2295                 :            : {
    2296                 :            :   ULONG bufsize = 15000; /* MS-recommended initial bufsize */
    2297                 :            :   DWORD ret = ERROR_BUFFER_OVERFLOW;
    2298                 :            :   unsigned int malloc_iterations = 0;
    2299                 :            :   PIP_ADAPTER_ADDRESSES addr_buf = NULL, eth_adapter;
    2300                 :            :   wchar_t *wchar_name_or_ip = NULL;
    2301                 :            :   gulong ip_result = 0;
    2302                 :            :   NET_IFINDEX if_index;
    2303                 :            : 
    2304                 :            :   /*
    2305                 :            :    * For Windows OS only - return adapter IPv4 address in network byte order.
    2306                 :            :    *
    2307                 :            :    * Input string can be either friendly name of adapter, IP address of adapter,
    2308                 :            :    * indextoname, or fullname of adapter.
    2309                 :            :    * Example:
    2310                 :            :    *    192.168.1.109   ===> IP address given directly,
    2311                 :            :    *                         convert directly with inet_addr() function
    2312                 :            :    *    Wi-Fi           ===> Adapter friendly name "Wi-Fi",
    2313                 :            :    *                         scan with GetAdapterAddresses and adapter->FriendlyName
    2314                 :            :    *    ethernet_32774  ===> Adapter name as returned by if_indextoname
    2315                 :            :    *    {33E8F5CD-BAEA-4214-BE13-B79AB8080CAB} ===> Adaptername,
    2316                 :            :    *                         as returned in GetAdapterAddresses and adapter->AdapterName
    2317                 :            :    */
    2318                 :            : 
    2319                 :            :   /* Step 1: Check if string is an IP address: */
    2320                 :            :   if (inet_pton (AF_INET, name_or_ip, &ip_result) == 1)
    2321                 :            :     return ip_result;  /* Success, IP address string was given directly */
    2322                 :            : 
    2323                 :            :   /*
    2324                 :            :    *  Step 2: Check if name represents a valid Interface index (e.g. ethernet_75521)
    2325                 :            :    *  function if_nametoindex will return >=1 if a valid index, or 0=no match
    2326                 :            :    *  valid index will be used later in GetAdaptersAddress loop for lookup of adapter IP address
    2327                 :            :    */
    2328                 :            :   if_index = if_nametoindex (name_or_ip);
    2329                 :            : 
    2330                 :            :   /* Step 3: Prepare wchar string for friendly name comparison */
    2331                 :            :   if (if_index == 0)
    2332                 :            :     {
    2333                 :            :       size_t if_name_len = strlen (name_or_ip);
    2334                 :            :       if (if_name_len >= MAX_ADAPTER_NAME_LENGTH + 4)
    2335                 :            :         return INADDR_NONE;
    2336                 :            :       /* Name-check only needed if index=0... */
    2337                 :            :       wchar_name_or_ip = (wchar_t *) g_try_malloc ((if_name_len + 1) * sizeof(wchar_t));
    2338                 :            :       if (wchar_name_or_ip)
    2339                 :            :         mbstowcs (wchar_name_or_ip, name_or_ip, if_name_len + 1);
    2340                 :            :       /* NOTE: Even if malloc fails here, some comparisons can still be done later... so no exit here! */
    2341                 :            :     }
    2342                 :            : 
    2343                 :            :   /*
    2344                 :            :    *  Step 4: Allocate memory and get adapter addresses.
    2345                 :            :    *  Buffer allocation loop recommended by MS, since size can be dynamic
    2346                 :            :    *  https://docs.microsoft.com/en-us/windows/desktop/api/iphlpapi/nf-iphlpapi-getadaptersaddresses
    2347                 :            :    */
    2348                 :            :   #define MAX_ALLOC_ITERATIONS 3
    2349                 :            :   do
    2350                 :            :     {
    2351                 :            :       malloc_iterations++;
    2352                 :            :       addr_buf = (PIP_ADAPTER_ADDRESSES) g_try_realloc (addr_buf, bufsize);
    2353                 :            :       if (addr_buf)
    2354                 :            :         ret = GetAdaptersAddresses (AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, addr_buf, &bufsize);
    2355                 :            :     }
    2356                 :            :   while (addr_buf &&
    2357                 :            :            ret == ERROR_BUFFER_OVERFLOW &&
    2358                 :            :            malloc_iterations < MAX_ALLOC_ITERATIONS);
    2359                 :            :   #undef MAX_ALLOC_ITERATIONS
    2360                 :            : 
    2361                 :            :   if (addr_buf == 0 || ret != NO_ERROR)
    2362                 :            :     {
    2363                 :            :       g_free (addr_buf);
    2364                 :            :       g_free (wchar_name_or_ip);
    2365                 :            :       return INADDR_NONE;
    2366                 :            :     }
    2367                 :            : 
    2368                 :            :   /* Step 5: Loop through adapters and check match for index or name */
    2369                 :            :   for (eth_adapter = addr_buf; eth_adapter != NULL; eth_adapter = eth_adapter->Next)
    2370                 :            :     {
    2371                 :            :       /* Check if match for interface index/name: */
    2372                 :            :       gboolean any_match = (if_index > 0) && (eth_adapter->IfIndex == if_index);
    2373                 :            : 
    2374                 :            :       /* Check if match for friendly name - but only if NO if_index! */
    2375                 :            :       if (!any_match && if_index == 0 && eth_adapter->FriendlyName &&
    2376                 :            :           eth_adapter->FriendlyName[0] != 0 && wchar_name_or_ip != NULL)
    2377                 :            :         any_match = (_wcsicmp (eth_adapter->FriendlyName, wchar_name_or_ip) == 0);
    2378                 :            : 
    2379                 :            :       /* Check if match for adapter low level name - but only if NO if_index: */
    2380                 :            :       if (!any_match && if_index == 0 && eth_adapter->AdapterName &&
    2381                 :            :           eth_adapter->AdapterName[0] != 0)
    2382                 :            :         any_match = (stricmp (eth_adapter->AdapterName, name_or_ip) == 0);
    2383                 :            : 
    2384                 :            :       if (any_match)
    2385                 :            :         {
    2386                 :            :           /* We have match for this adapter, lets get its local unicast IP address! */
    2387                 :            :           PIP_ADAPTER_UNICAST_ADDRESS uni_addr;
    2388                 :            :           for (uni_addr = eth_adapter->FirstUnicastAddress;
    2389                 :            :               uni_addr != NULL; uni_addr = uni_addr->Next)
    2390                 :            :             {
    2391                 :            :               if (uni_addr->Address.lpSockaddr->sa_family == AF_INET)
    2392                 :            :                 {
    2393                 :            :                   ip_result = ((PSOCKADDR_IN) uni_addr->Address.lpSockaddr)->sin_addr.S_un.S_addr;
    2394                 :            :                   break; /* finished, exit unicast addr loop */
    2395                 :            :                 }
    2396                 :            :             }
    2397                 :            :         }
    2398                 :            :     }
    2399                 :            : 
    2400                 :            :   g_free (addr_buf);
    2401                 :            :   g_free (wchar_name_or_ip);
    2402                 :            : 
    2403                 :            :   return ip_result;
    2404                 :            : }
    2405                 :            : #endif
    2406                 :            : 
    2407                 :            : static gboolean
    2408                 :          0 : g_socket_multicast_group_operation (GSocket       *socket,
    2409                 :            :                                     GInetAddress  *group,
    2410                 :            :                                     gboolean       source_specific,
    2411                 :            :                                     const gchar   *iface,
    2412                 :            :                                     gboolean       join_group,
    2413                 :            :                                     GError       **error)
    2414                 :            : {
    2415                 :            :   const guint8 *native_addr;
    2416                 :            :   gint optname, result;
    2417                 :            : 
    2418                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    2419                 :          0 :   g_return_val_if_fail (socket->priv->type == G_SOCKET_TYPE_DATAGRAM, FALSE);
    2420                 :          0 :   g_return_val_if_fail (G_IS_INET_ADDRESS (group), FALSE);
    2421                 :            : 
    2422         [ #  # ]:          0 :   if (!check_socket (socket, error))
    2423                 :          0 :     return FALSE;
    2424                 :            : 
    2425                 :          0 :   native_addr = g_inet_address_to_bytes (group);
    2426         [ #  # ]:          0 :   if (g_inet_address_get_family (group) == G_SOCKET_FAMILY_IPV4)
    2427                 :            :     {
    2428                 :            : #ifdef HAVE_IP_MREQN
    2429                 :            :       struct ip_mreqn mc_req;
    2430                 :            : #else
    2431                 :            :       struct ip_mreq mc_req;
    2432                 :            : #endif
    2433                 :            : 
    2434                 :          0 :       memset (&mc_req, 0, sizeof (mc_req));
    2435                 :          0 :       memcpy (&mc_req.imr_multiaddr, native_addr, sizeof (struct in_addr));
    2436                 :            : 
    2437                 :            : #ifdef HAVE_IP_MREQN
    2438         [ #  # ]:          0 :       if (iface)
    2439                 :          0 :         mc_req.imr_ifindex = if_nametoindex (iface);
    2440                 :            :       else
    2441                 :          0 :         mc_req.imr_ifindex = 0;  /* Pick any.  */
    2442                 :            : #elif defined(G_OS_WIN32)
    2443                 :            :       if (iface)
    2444                 :            :         mc_req.imr_interface.s_addr = g_socket_w32_get_adapter_ipv4_addr (iface);
    2445                 :            :       else
    2446                 :            :         mc_req.imr_interface.s_addr = g_htonl (INADDR_ANY);
    2447                 :            : #else
    2448                 :            :       mc_req.imr_interface.s_addr = g_htonl (INADDR_ANY);
    2449                 :            : #endif
    2450                 :            : 
    2451         [ #  # ]:          0 :       if (source_specific)
    2452                 :            :         {
    2453                 :            : #ifdef IP_ADD_SOURCE_MEMBERSHIP
    2454         [ #  # ]:          0 :           optname = join_group ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
    2455                 :            : #else
    2456                 :            :           g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    2457                 :            :                        join_group ?
    2458                 :            :                        _("Error joining multicast group: %s") :
    2459                 :            :                        _("Error leaving multicast group: %s"),
    2460                 :            :                        _("No support for source-specific multicast"));
    2461                 :            :           return FALSE;
    2462                 :            : #endif
    2463                 :            :         }
    2464                 :            :       else
    2465         [ #  # ]:          0 :         optname = join_group ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
    2466                 :          0 :       result = setsockopt (socket->priv->fd, IPPROTO_IP, optname,
    2467                 :            :                            &mc_req, sizeof (mc_req));
    2468                 :            :     }
    2469         [ #  # ]:          0 :   else if (g_inet_address_get_family (group) == G_SOCKET_FAMILY_IPV6)
    2470                 :            :     {
    2471                 :            :       struct ipv6_mreq mc_req_ipv6;
    2472                 :            : 
    2473                 :          0 :       memset (&mc_req_ipv6, 0, sizeof (mc_req_ipv6));
    2474                 :          0 :       memcpy (&mc_req_ipv6.ipv6mr_multiaddr, native_addr, sizeof (struct in6_addr));
    2475                 :            : #ifdef HAVE_IF_NAMETOINDEX
    2476         [ #  # ]:          0 :       if (iface)
    2477                 :          0 :         mc_req_ipv6.ipv6mr_interface = if_nametoindex (iface);
    2478                 :            :       else
    2479                 :            : #endif
    2480                 :          0 :         mc_req_ipv6.ipv6mr_interface = 0;
    2481                 :            : 
    2482         [ #  # ]:          0 :       optname = join_group ? IPV6_JOIN_GROUP : IPV6_LEAVE_GROUP;
    2483                 :          0 :       result = setsockopt (socket->priv->fd, IPPROTO_IPV6, optname,
    2484                 :            :                            &mc_req_ipv6, sizeof (mc_req_ipv6));
    2485                 :            :     }
    2486                 :            :   else
    2487                 :            :     g_return_val_if_reached (FALSE);
    2488                 :            : 
    2489         [ #  # ]:          0 :   if (result < 0)
    2490                 :            :     {
    2491                 :          0 :       int errsv = get_socket_errno ();
    2492                 :            : 
    2493         [ #  # ]:          0 :       g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
    2494                 :            :                    join_group ?
    2495                 :          0 :                    _("Error joining multicast group: %s") :
    2496                 :          0 :                    _("Error leaving multicast group: %s"),
    2497                 :            :                    socket_strerror (errsv));
    2498                 :          0 :       return FALSE;
    2499                 :            :     }
    2500                 :            : 
    2501                 :          0 :   return TRUE;
    2502                 :            : }
    2503                 :            : 
    2504                 :            : /**
    2505                 :            :  * g_socket_join_multicast_group:
    2506                 :            :  * @socket: a #GSocket.
    2507                 :            :  * @group: a #GInetAddress specifying the group address to join.
    2508                 :            :  * @iface: (nullable): Name of the interface to use, or %NULL
    2509                 :            :  * @source_specific: %TRUE if source-specific multicast should be used
    2510                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2511                 :            :  *
    2512                 :            :  * Registers @socket to receive multicast messages sent to @group.
    2513                 :            :  * @socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have
    2514                 :            :  * been bound to an appropriate interface and port with
    2515                 :            :  * g_socket_bind().
    2516                 :            :  *
    2517                 :            :  * If @iface is %NULL, the system will automatically pick an interface
    2518                 :            :  * to bind to based on @group.
    2519                 :            :  *
    2520                 :            :  * If @source_specific is %TRUE, source-specific multicast as defined
    2521                 :            :  * in RFC 4604 is used. Note that on older platforms this may fail
    2522                 :            :  * with a %G_IO_ERROR_NOT_SUPPORTED error.
    2523                 :            :  *
    2524                 :            :  * To bind to a given source-specific multicast address, use
    2525                 :            :  * g_socket_join_multicast_group_ssm() instead.
    2526                 :            :  *
    2527                 :            :  * Returns: %TRUE on success, %FALSE on error.
    2528                 :            :  *
    2529                 :            :  * Since: 2.32
    2530                 :            :  */
    2531                 :            : gboolean
    2532                 :          0 : g_socket_join_multicast_group (GSocket       *socket,
    2533                 :            :                                GInetAddress  *group,
    2534                 :            :                                gboolean       source_specific,
    2535                 :            :                                const gchar   *iface,
    2536                 :            :                                GError       **error)
    2537                 :            : {
    2538                 :          0 :   return g_socket_multicast_group_operation (socket, group, source_specific, iface, TRUE, error);
    2539                 :            : }
    2540                 :            : 
    2541                 :            : /**
    2542                 :            :  * g_socket_leave_multicast_group:
    2543                 :            :  * @socket: a #GSocket.
    2544                 :            :  * @group: a #GInetAddress specifying the group address to leave.
    2545                 :            :  * @iface: (nullable): Interface used
    2546                 :            :  * @source_specific: %TRUE if source-specific multicast was used
    2547                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2548                 :            :  *
    2549                 :            :  * Removes @socket from the multicast group defined by @group, @iface,
    2550                 :            :  * and @source_specific (which must all have the same values they had
    2551                 :            :  * when you joined the group).
    2552                 :            :  *
    2553                 :            :  * @socket remains bound to its address and port, and can still receive
    2554                 :            :  * unicast messages after calling this.
    2555                 :            :  *
    2556                 :            :  * To unbind to a given source-specific multicast address, use
    2557                 :            :  * g_socket_leave_multicast_group_ssm() instead.
    2558                 :            :  *
    2559                 :            :  * Returns: %TRUE on success, %FALSE on error.
    2560                 :            :  *
    2561                 :            :  * Since: 2.32
    2562                 :            :  */
    2563                 :            : gboolean
    2564                 :          0 : g_socket_leave_multicast_group (GSocket       *socket,
    2565                 :            :                                 GInetAddress  *group,
    2566                 :            :                                 gboolean       source_specific,
    2567                 :            :                                 const gchar   *iface,
    2568                 :            :                                 GError       **error)
    2569                 :            : {
    2570                 :          0 :   return g_socket_multicast_group_operation (socket, group, source_specific, iface, FALSE, error);
    2571                 :            : }
    2572                 :            : 
    2573                 :            : static gboolean
    2574                 :          0 : g_socket_multicast_group_operation_ssm (GSocket       *socket,
    2575                 :            :                                         GInetAddress  *group,
    2576                 :            :                                         GInetAddress  *source_specific,
    2577                 :            :                                         const gchar   *iface,
    2578                 :            :                                         gboolean       join_group,
    2579                 :            :                                         GError       **error)
    2580                 :            : {
    2581                 :            :   gint result;
    2582                 :            : 
    2583                 :          0 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    2584                 :          0 :   g_return_val_if_fail (socket->priv->type == G_SOCKET_TYPE_DATAGRAM, FALSE);
    2585                 :          0 :   g_return_val_if_fail (G_IS_INET_ADDRESS (group), FALSE);
    2586                 :          0 :   g_return_val_if_fail (iface == NULL || *iface != '\0', FALSE);
    2587                 :          0 :   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
    2588                 :            : 
    2589         [ #  # ]:          0 :   if (!source_specific)
    2590                 :            :     {
    2591                 :          0 :       return g_socket_multicast_group_operation (socket, group, FALSE, iface,
    2592                 :            :                                                  join_group, error);
    2593                 :            :     }
    2594                 :            : 
    2595         [ #  # ]:          0 :   if (!check_socket (socket, error))
    2596                 :          0 :     return FALSE;
    2597                 :            : 
    2598   [ #  #  #  # ]:          0 :   switch (g_inet_address_get_family (group))
    2599                 :            :     {
    2600                 :          0 :     case G_SOCKET_FAMILY_INVALID:
    2601                 :            :     case G_SOCKET_FAMILY_UNIX:
    2602                 :            :       {
    2603         [ #  # ]:          0 :         g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    2604                 :            :             join_group ?
    2605                 :          0 :             _("Error joining multicast group: %s") :
    2606                 :          0 :             _("Error leaving multicast group: %s"),
    2607                 :            :             _("Unsupported socket family"));
    2608                 :          0 :         return FALSE;
    2609                 :            :       }
    2610                 :            :       break;
    2611                 :            : 
    2612                 :          0 :     case G_SOCKET_FAMILY_IPV4:
    2613                 :            :       {
    2614                 :            : #ifdef IP_ADD_SOURCE_MEMBERSHIP
    2615                 :            : 
    2616                 :            : #ifdef BROKEN_IP_MREQ_SOURCE_STRUCT
    2617                 :            : #define S_ADDR_FIELD(src) src.imr_interface
    2618                 :            : #else
    2619                 :            : #define S_ADDR_FIELD(src) src.imr_interface.s_addr
    2620                 :            : #endif
    2621                 :            : 
    2622                 :            :         gint optname;
    2623                 :            :         struct ip_mreq_source mc_req_src;
    2624                 :            : 
    2625         [ #  # ]:          0 :         if (g_inet_address_get_family (source_specific) !=
    2626                 :            :             G_SOCKET_FAMILY_IPV4)
    2627                 :            :           {
    2628         [ #  # ]:          0 :             g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    2629                 :            :                 join_group ?
    2630                 :          0 :                 _("Error joining multicast group: %s") :
    2631                 :          0 :                 _("Error leaving multicast group: %s"),
    2632                 :            :                 _("source-specific not an IPv4 address"));
    2633                 :          0 :             return FALSE;
    2634                 :            :           }
    2635                 :            : 
    2636                 :          0 :         memset (&mc_req_src, 0, sizeof (mc_req_src));
    2637                 :            : 
    2638                 :            :         /* By default use the default IPv4 multicast interface. */
    2639                 :          0 :         S_ADDR_FIELD(mc_req_src) = g_htonl (INADDR_ANY);
    2640                 :            : 
    2641         [ #  # ]:          0 :         if (iface)
    2642                 :            :           {
    2643                 :            : #if defined(G_OS_WIN32)
    2644                 :            :             S_ADDR_FIELD(mc_req_src) = g_socket_w32_get_adapter_ipv4_addr (iface);
    2645                 :            : #elif defined (HAVE_SIOCGIFADDR)
    2646                 :            :             int ret;
    2647                 :            :             struct ifreq ifr;
    2648                 :            :             struct sockaddr_in *iface_addr;
    2649                 :          0 :             size_t if_name_len = strlen (iface);
    2650                 :            : 
    2651                 :          0 :             memset (&ifr, 0, sizeof (ifr));
    2652                 :            : 
    2653         [ #  # ]:          0 :             if (if_name_len >= sizeof (ifr.ifr_name))
    2654                 :            :               {
    2655                 :          0 :                 g_set_error (error, G_IO_ERROR,  G_IO_ERROR_FILENAME_TOO_LONG,
    2656                 :            :                              _("Interface name too long"));
    2657                 :          0 :                 return FALSE;
    2658                 :            :               }
    2659                 :            : 
    2660                 :          0 :             memcpy (ifr.ifr_name, iface, if_name_len);
    2661                 :            : 
    2662                 :            :             /* Get the IPv4 address of the given network interface name. */
    2663                 :          0 :             ret = ioctl (socket->priv->fd, SIOCGIFADDR, &ifr);
    2664         [ #  # ]:          0 :             if (ret < 0)
    2665                 :            :               {
    2666                 :          0 :                 int errsv = errno;
    2667                 :            : 
    2668                 :          0 :                 g_set_error (error, G_IO_ERROR,  g_io_error_from_errno (errsv),
    2669                 :            :                              _("Interface not found: %s"), g_strerror (errsv));
    2670                 :          0 :                 return FALSE;
    2671                 :            :               }
    2672                 :            : 
    2673                 :          0 :             iface_addr = (struct sockaddr_in *) &ifr.ifr_addr;
    2674                 :          0 :             S_ADDR_FIELD(mc_req_src) = iface_addr->sin_addr.s_addr;
    2675                 :            : #endif  /* defined(G_OS_WIN32) && defined (HAVE_IF_NAMETOINDEX) */
    2676                 :            :           }
    2677                 :            : 
    2678                 :          0 :         g_assert (g_inet_address_get_native_size (group) == sizeof (mc_req_src.imr_multiaddr));
    2679                 :          0 :         memcpy (&mc_req_src.imr_multiaddr, g_inet_address_to_bytes (group),
    2680                 :            :                 g_inet_address_get_native_size (group));
    2681                 :            : 
    2682                 :          0 :         g_assert (g_inet_address_get_native_size (source_specific) == sizeof (mc_req_src.imr_sourceaddr));
    2683                 :          0 :         memcpy (&mc_req_src.imr_sourceaddr,
    2684                 :          0 :                 g_inet_address_to_bytes (source_specific),
    2685                 :            :                 g_inet_address_get_native_size (source_specific));
    2686                 :            : 
    2687                 :          0 :         optname =
    2688         [ #  # ]:          0 :             join_group ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
    2689                 :          0 :         result = setsockopt (socket->priv->fd, IPPROTO_IP, optname,
    2690                 :            :                              &mc_req_src, sizeof (mc_req_src));
    2691                 :            : 
    2692                 :            : #undef S_ADDR_FIELD
    2693                 :            : 
    2694                 :            : #else
    2695                 :            :         g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    2696                 :            :             join_group ?
    2697                 :            :             _("Error joining multicast group: %s") :
    2698                 :            :             _("Error leaving multicast group: %s"),
    2699                 :            :             _("No support for IPv4 source-specific multicast"));
    2700                 :            :         return FALSE;
    2701                 :            : #endif  /* IP_ADD_SOURCE_MEMBERSHIP */
    2702                 :            :       }
    2703                 :          0 :       break;
    2704                 :            : 
    2705                 :          0 :     case G_SOCKET_FAMILY_IPV6:
    2706                 :            :       {
    2707                 :            : #ifdef MCAST_JOIN_SOURCE_GROUP
    2708                 :            :         gboolean res;
    2709                 :            :         gint optname;
    2710                 :            :         struct group_source_req mc_req_src;
    2711                 :            :         GSocketAddress *saddr_group, *saddr_source_specific;
    2712                 :          0 :         guint iface_index = 0;
    2713                 :            : 
    2714                 :            : #if defined (HAVE_IF_NAMETOINDEX)
    2715         [ #  # ]:          0 :         if (iface)
    2716                 :            :           {
    2717                 :          0 :             iface_index = if_nametoindex (iface);
    2718         [ #  # ]:          0 :             if (iface_index == 0)
    2719                 :            :               {
    2720                 :          0 :                 int errsv = errno;
    2721                 :            : 
    2722                 :          0 :                 g_set_error (error, G_IO_ERROR,  g_io_error_from_errno (errsv),
    2723                 :            :                              _("Interface not found: %s"), g_strerror (errsv));
    2724                 :          0 :                 return FALSE;
    2725                 :            :               }
    2726                 :            :           }
    2727                 :            : #endif  /* defined (HAVE_IF_NAMETOINDEX) */
    2728                 :          0 :         mc_req_src.gsr_interface = iface_index;
    2729                 :            : 
    2730                 :          0 :         saddr_group = g_inet_socket_address_new (group, 0);
    2731                 :          0 :         res = g_socket_address_to_native (saddr_group, &mc_req_src.gsr_group,
    2732                 :            :                                           sizeof (mc_req_src.gsr_group),
    2733                 :            :                                           error);
    2734                 :          0 :         g_object_unref (saddr_group);
    2735         [ #  # ]:          0 :         if (!res)
    2736                 :          0 :           return FALSE;
    2737                 :            : 
    2738                 :          0 :         saddr_source_specific = g_inet_socket_address_new (source_specific, 0);
    2739                 :          0 :         res = g_socket_address_to_native (saddr_source_specific,
    2740                 :            :                                           &mc_req_src.gsr_source,
    2741                 :            :                                           sizeof (mc_req_src.gsr_source),
    2742                 :            :                                           error);
    2743                 :          0 :         g_object_unref (saddr_source_specific);
    2744                 :            : 
    2745         [ #  # ]:          0 :         if (!res)
    2746                 :          0 :           return FALSE;
    2747                 :            : 
    2748                 :          0 :         optname =
    2749         [ #  # ]:          0 :             join_group ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP;
    2750                 :          0 :         result = setsockopt (socket->priv->fd, IPPROTO_IPV6, optname,
    2751                 :            :                              &mc_req_src, sizeof (mc_req_src));
    2752                 :            : #else
    2753                 :            :         g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    2754                 :            :             join_group ?
    2755                 :            :             _("Error joining multicast group: %s") :
    2756                 :            :             _("Error leaving multicast group: %s"),
    2757                 :            :             _("No support for IPv6 source-specific multicast"));
    2758                 :            :         return FALSE;
    2759                 :            : #endif  /* MCAST_JOIN_SOURCE_GROUP */
    2760                 :            :       }
    2761                 :          0 :       break;
    2762                 :            : 
    2763                 :          0 :     default:
    2764                 :            :       g_return_val_if_reached (FALSE);
    2765                 :            :     }
    2766                 :            : 
    2767         [ #  # ]:          0 :   if (result < 0)
    2768                 :            :     {
    2769                 :          0 :       int errsv = get_socket_errno ();
    2770                 :            : 
    2771         [ #  # ]:          0 :       g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
    2772                 :            :           join_group ?
    2773                 :          0 :           _("Error joining multicast group: %s") :
    2774                 :          0 :           _("Error leaving multicast group: %s"),
    2775                 :            :            socket_strerror (errsv));
    2776                 :          0 :       return FALSE;
    2777                 :            :     }
    2778                 :            : 
    2779                 :          0 :   return TRUE;
    2780                 :            : }
    2781                 :            : 
    2782                 :            : /**
    2783                 :            :  * g_socket_join_multicast_group_ssm:
    2784                 :            :  * @socket: a #GSocket.
    2785                 :            :  * @group: a #GInetAddress specifying the group address to join.
    2786                 :            :  * @source_specific: (nullable): a #GInetAddress specifying the
    2787                 :            :  * source-specific multicast address or %NULL to ignore.
    2788                 :            :  * @iface: (nullable): Name of the interface to use, or %NULL
    2789                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2790                 :            :  *
    2791                 :            :  * Registers @socket to receive multicast messages sent to @group.
    2792                 :            :  * @socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have
    2793                 :            :  * been bound to an appropriate interface and port with
    2794                 :            :  * g_socket_bind().
    2795                 :            :  *
    2796                 :            :  * If @iface is %NULL, the system will automatically pick an interface
    2797                 :            :  * to bind to based on @group.
    2798                 :            :  *
    2799                 :            :  * If @source_specific is not %NULL, use source-specific multicast as
    2800                 :            :  * defined in RFC 4604. Note that on older platforms this may fail
    2801                 :            :  * with a %G_IO_ERROR_NOT_SUPPORTED error.
    2802                 :            :  *
    2803                 :            :  * Note that this function can be called multiple times for the same
    2804                 :            :  * @group with different @source_specific in order to receive multicast
    2805                 :            :  * packets from more than one source.
    2806                 :            :  *
    2807                 :            :  * Returns: %TRUE on success, %FALSE on error.
    2808                 :            :  *
    2809                 :            :  * Since: 2.56
    2810                 :            :  */
    2811                 :            : gboolean
    2812                 :          0 : g_socket_join_multicast_group_ssm (GSocket       *socket,
    2813                 :            :                                    GInetAddress  *group,
    2814                 :            :                                    GInetAddress  *source_specific,
    2815                 :            :                                    const gchar   *iface,
    2816                 :            :                                    GError       **error)
    2817                 :            : {
    2818                 :          0 :   return g_socket_multicast_group_operation_ssm (socket, group,
    2819                 :            :       source_specific, iface, TRUE, error);
    2820                 :            : }
    2821                 :            : 
    2822                 :            : /**
    2823                 :            :  * g_socket_leave_multicast_group_ssm:
    2824                 :            :  * @socket: a #GSocket.
    2825                 :            :  * @group: a #GInetAddress specifying the group address to leave.
    2826                 :            :  * @source_specific: (nullable): a #GInetAddress specifying the
    2827                 :            :  * source-specific multicast address or %NULL to ignore.
    2828                 :            :  * @iface: (nullable): Name of the interface to use, or %NULL
    2829                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2830                 :            :  *
    2831                 :            :  * Removes @socket from the multicast group defined by @group, @iface,
    2832                 :            :  * and @source_specific (which must all have the same values they had
    2833                 :            :  * when you joined the group).
    2834                 :            :  *
    2835                 :            :  * @socket remains bound to its address and port, and can still receive
    2836                 :            :  * unicast messages after calling this.
    2837                 :            :  *
    2838                 :            :  * Returns: %TRUE on success, %FALSE on error.
    2839                 :            :  *
    2840                 :            :  * Since: 2.56
    2841                 :            :  */
    2842                 :            : gboolean
    2843                 :          0 : g_socket_leave_multicast_group_ssm (GSocket       *socket,
    2844                 :            :                                     GInetAddress  *group,
    2845                 :            :                                     GInetAddress  *source_specific,
    2846                 :            :                                     const gchar   *iface,
    2847                 :            :                                     GError       **error)
    2848                 :            : {
    2849                 :          0 :   return g_socket_multicast_group_operation_ssm (socket, group,
    2850                 :            :       source_specific, iface, FALSE, error);
    2851                 :            : }
    2852                 :            : 
    2853                 :            : /**
    2854                 :            :  * g_socket_speaks_ipv4:
    2855                 :            :  * @socket: a #GSocket
    2856                 :            :  *
    2857                 :            :  * Checks if a socket is capable of speaking IPv4.
    2858                 :            :  *
    2859                 :            :  * IPv4 sockets are capable of speaking IPv4.  On some operating systems
    2860                 :            :  * and under some combinations of circumstances IPv6 sockets are also
    2861                 :            :  * capable of speaking IPv4.  See RFC 3493 section 3.7 for more
    2862                 :            :  * information.
    2863                 :            :  *
    2864                 :            :  * No other types of sockets are currently considered as being capable
    2865                 :            :  * of speaking IPv4.
    2866                 :            :  *
    2867                 :            :  * Returns: %TRUE if this socket can be used with IPv4.
    2868                 :            :  *
    2869                 :            :  * Since: 2.22
    2870                 :            :  **/
    2871                 :            : gboolean
    2872                 :          4 : g_socket_speaks_ipv4 (GSocket *socket)
    2873                 :            : {
    2874      [ -  +  - ]:          4 :   switch (socket->priv->family)
    2875                 :            :     {
    2876                 :          0 :     case G_SOCKET_FAMILY_IPV4:
    2877                 :          0 :       return TRUE;
    2878                 :            : 
    2879                 :          4 :     case G_SOCKET_FAMILY_IPV6:
    2880                 :            : #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
    2881                 :            :       {
    2882                 :            :         gint v6_only;
    2883                 :            : 
    2884         [ -  + ]:          4 :         if (!g_socket_get_option (socket,
    2885                 :            :                                   IPPROTO_IPV6, IPV6_V6ONLY,
    2886                 :            :                                   &v6_only, NULL))
    2887                 :          0 :           return FALSE;
    2888                 :            : 
    2889                 :          4 :         return !v6_only;
    2890                 :            :       }
    2891                 :            : #else
    2892                 :            :       return FALSE;
    2893                 :            : #endif
    2894                 :            : 
    2895                 :          0 :     default:
    2896                 :          0 :       return FALSE;
    2897                 :            :     }
    2898                 :            : }
    2899                 :            : 
    2900                 :            : /**
    2901                 :            :  * g_socket_accept:
    2902                 :            :  * @socket: a #GSocket.
    2903                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    2904                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    2905                 :            :  *
    2906                 :            :  * Accept incoming connections on a connection-based socket. This removes
    2907                 :            :  * the first outstanding connection request from the listening socket and
    2908                 :            :  * creates a #GSocket object for it.
    2909                 :            :  *
    2910                 :            :  * The @socket must be bound to a local address with g_socket_bind() and
    2911                 :            :  * must be listening for incoming connections (g_socket_listen()).
    2912                 :            :  *
    2913                 :            :  * If there are no outstanding connections then the operation will block
    2914                 :            :  * or return %G_IO_ERROR_WOULD_BLOCK if non-blocking I/O is enabled.
    2915                 :            :  * To be notified of an incoming connection, wait for the %G_IO_IN condition.
    2916                 :            :  *
    2917                 :            :  * Returns: (transfer full): a new #GSocket, or %NULL on error.
    2918                 :            :  *     Free the returned object with g_object_unref().
    2919                 :            :  *
    2920                 :            :  * Since: 2.22
    2921                 :            :  */
    2922                 :            : GSocket *
    2923                 :        297 : g_socket_accept (GSocket       *socket,
    2924                 :            :                  GCancellable  *cancellable,
    2925                 :            :                  GError       **error)
    2926                 :            : {
    2927                 :            : #ifdef HAVE_ACCEPT4
    2928                 :        297 :   gboolean try_accept4 = TRUE;
    2929                 :            : #endif
    2930                 :            :   GSocket *new_socket;
    2931                 :            :   gint ret;
    2932                 :            : 
    2933                 :        297 :   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
    2934                 :            : 
    2935         [ -  + ]:        297 :   if (!check_socket (socket, error))
    2936                 :          0 :     return NULL;
    2937                 :            : 
    2938         [ -  + ]:        297 :   if (!check_timeout (socket, error))
    2939                 :          0 :     return NULL;
    2940                 :            : 
    2941                 :            :   while (TRUE)
    2942                 :         34 :     {
    2943                 :        331 :       gboolean try_accept = TRUE;
    2944                 :            : 
    2945                 :            : #ifdef HAVE_ACCEPT4
    2946         [ +  - ]:        331 :       if (try_accept4)
    2947                 :            :         {
    2948                 :        331 :           ret = accept4 (socket->priv->fd, NULL, 0, SOCK_CLOEXEC);
    2949   [ +  +  -  + ]:        331 :           if (ret < 0 && errno == ENOSYS)
    2950                 :            :             {
    2951                 :          0 :               try_accept4 = FALSE;
    2952                 :            :             }
    2953                 :            :           else
    2954                 :            :             {
    2955                 :        331 :               try_accept = FALSE;
    2956                 :            :             }
    2957                 :            :         }
    2958                 :            : 
    2959                 :        331 :       g_assert (try_accept4 || try_accept);
    2960                 :            : #endif
    2961         [ -  + ]:        331 :       if (try_accept)
    2962                 :          0 :         ret = accept (socket->priv->fd, NULL, 0);
    2963                 :            : 
    2964         [ +  + ]:        331 :       if (ret < 0)
    2965                 :            :         {
    2966                 :         59 :           int errsv = get_socket_errno ();
    2967                 :            : 
    2968         [ -  + ]:         59 :           if (errsv == EINTR)
    2969                 :          0 :             continue;
    2970                 :            : 
    2971                 :            : #ifdef WSAEWOULDBLOCK
    2972                 :            :           if (errsv == WSAEWOULDBLOCK)
    2973                 :            : #else
    2974   [ -  +  -  - ]:         59 :           if (errsv == EWOULDBLOCK ||
    2975                 :            :               errsv == EAGAIN)
    2976                 :            : #endif
    2977                 :            :             {
    2978                 :            :               win32_unset_event_mask (socket, FD_ACCEPT);
    2979                 :            : 
    2980         [ +  - ]:         59 :               if (socket->priv->blocking)
    2981                 :            :                 {
    2982         [ +  + ]:         59 :                   if (!g_socket_condition_wait (socket,
    2983                 :            :                                                 G_IO_IN, cancellable, error))
    2984                 :         25 :                     return NULL;
    2985                 :            : 
    2986                 :         34 :                   continue;
    2987                 :            :                 }
    2988                 :            :             }
    2989                 :            : 
    2990   [ #  #  #  # ]:          0 :           socket_set_error_lazy (error, errsv, _("Error accepting connection: %s"));
    2991                 :          0 :           return NULL;
    2992                 :            :         }
    2993                 :        272 :       break;
    2994                 :            :     }
    2995                 :            : 
    2996                 :            :   win32_unset_event_mask (socket, FD_ACCEPT);
    2997                 :            : 
    2998                 :            : #ifdef G_OS_WIN32
    2999                 :            :   {
    3000                 :            :     /* The socket inherits the accepting sockets event mask and even object,
    3001                 :            :        we need to remove that */
    3002                 :            :     WSAEventSelect (ret, NULL, 0);
    3003                 :            :   }
    3004                 :            : #else
    3005                 :            :   {
    3006                 :            :     int flags;
    3007                 :            : 
    3008                 :            :     /* We always want to set close-on-exec to protect users. If you
    3009                 :            :        need to so some weird inheritance to exec you can re-enable this
    3010                 :            :        using lower level hacks with g_socket_get_fd(). */
    3011                 :        272 :     flags = fcntl (ret, F_GETFD, 0);
    3012         [ +  - ]:        272 :     if (flags != -1 &&
    3013         [ -  + ]:        272 :         (flags & FD_CLOEXEC) == 0)
    3014                 :            :       {
    3015                 :          0 :         flags |= FD_CLOEXEC;
    3016                 :          0 :         fcntl (ret, F_SETFD, flags);
    3017                 :            :       }
    3018                 :            :   }
    3019                 :            : #endif
    3020                 :            : 
    3021                 :        272 :   new_socket = g_socket_new_from_fd (ret, error);
    3022         [ -  + ]:        272 :   if (new_socket == NULL)
    3023                 :            :     {
    3024                 :            : #ifdef G_OS_WIN32
    3025                 :            :       closesocket (ret);
    3026                 :            : #else
    3027                 :          0 :       close (ret);
    3028                 :            : #endif
    3029                 :            :     }
    3030                 :            :   else
    3031                 :        272 :     new_socket->priv->protocol = socket->priv->protocol;
    3032                 :            : 
    3033                 :        272 :   return new_socket;
    3034                 :            : }
    3035                 :            : 
    3036                 :            : /**
    3037                 :            :  * g_socket_connect:
    3038                 :            :  * @socket: a #GSocket.
    3039                 :            :  * @address: a #GSocketAddress specifying the remote address.
    3040                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    3041                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3042                 :            :  *
    3043                 :            :  * Connect the socket to the specified remote address.
    3044                 :            :  *
    3045                 :            :  * For connection oriented socket this generally means we attempt to make
    3046                 :            :  * a connection to the @address. For a connection-less socket it sets
    3047                 :            :  * the default address for g_socket_send() and discards all incoming datagrams
    3048                 :            :  * from other sources.
    3049                 :            :  *
    3050                 :            :  * Generally connection oriented sockets can only connect once, but
    3051                 :            :  * connection-less sockets can connect multiple times to change the
    3052                 :            :  * default address.
    3053                 :            :  *
    3054                 :            :  * If the connect call needs to do network I/O it will block, unless
    3055                 :            :  * non-blocking I/O is enabled. Then %G_IO_ERROR_PENDING is returned
    3056                 :            :  * and the user can be notified of the connection finishing by waiting
    3057                 :            :  * for the G_IO_OUT condition. The result of the connection must then be
    3058                 :            :  * checked with g_socket_check_connect_result().
    3059                 :            :  *
    3060                 :            :  * Returns: %TRUE if connected, %FALSE on error.
    3061                 :            :  *
    3062                 :            :  * Since: 2.22
    3063                 :            :  */
    3064                 :            : gboolean
    3065                 :       1287 : g_socket_connect (GSocket         *socket,
    3066                 :            :                   GSocketAddress  *address,
    3067                 :            :                   GCancellable    *cancellable,
    3068                 :            :                   GError         **error)
    3069                 :            : {
    3070                 :            :   union {
    3071                 :            :     struct sockaddr_storage storage;
    3072                 :            :     struct sockaddr sa;
    3073                 :            :   } buffer;
    3074                 :            : 
    3075                 :       1287 :   g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
    3076                 :            : 
    3077         [ -  + ]:       1287 :   if (!check_socket (socket, error))
    3078                 :          0 :     return FALSE;
    3079                 :            : 
    3080         [ -  + ]:       1287 :   if (!g_socket_address_to_native (address, &buffer.storage, sizeof buffer, error))
    3081                 :          0 :     return FALSE;
    3082                 :            : 
    3083         [ -  + ]:       1287 :   if (socket->priv->remote_address)
    3084                 :          0 :     g_object_unref (socket->priv->remote_address);
    3085                 :       1287 :   socket->priv->remote_address = g_object_ref (address);
    3086                 :            : 
    3087                 :            :   while (1)
    3088                 :            :     {
    3089         [ +  + ]:       1287 :       if (connect (socket->priv->fd, &buffer.sa,
    3090                 :       1287 :                    g_socket_address_get_native_size (address)) < 0)
    3091                 :            :         {
    3092                 :         85 :           int errsv = get_socket_errno ();
    3093                 :            : 
    3094         [ -  + ]:         85 :           if (errsv == EINTR)
    3095                 :          0 :             continue;
    3096                 :            : 
    3097                 :            : #ifndef G_OS_WIN32
    3098         [ +  + ]:         85 :           if (errsv == EINPROGRESS)
    3099                 :            : #else
    3100                 :            :           if (errsv == WSAEWOULDBLOCK)
    3101                 :            : #endif
    3102                 :            :             {
    3103                 :            :               win32_unset_event_mask (socket, FD_CONNECT);
    3104                 :            : 
    3105         [ +  + ]:         59 :               if (socket->priv->blocking)
    3106                 :            :                 {
    3107         [ +  - ]:         38 :                   if (g_socket_condition_wait (socket, G_IO_OUT, cancellable, error))
    3108                 :            :                     {
    3109         [ +  - ]:         38 :                       if (g_socket_check_connect_result (socket, error))
    3110                 :         38 :                         break;
    3111                 :            :                     }
    3112                 :            :                 }
    3113                 :            :               else
    3114                 :            :                 {
    3115                 :         21 :                   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
    3116                 :            :                                        _("Connection in progress"));
    3117                 :         21 :                   socket->priv->connect_pending = TRUE;
    3118                 :            :                 }
    3119                 :            :             }
    3120                 :            :           else
    3121                 :         26 :             g_set_error_literal (error, G_IO_ERROR,
    3122                 :         26 :                                  socket_io_error_from_errno (errsv),
    3123                 :         26 :                                  socket_strerror (errsv));
    3124                 :            : 
    3125                 :         47 :           return FALSE;
    3126                 :            :         }
    3127                 :       1202 :       break;
    3128                 :            :     }
    3129                 :            : 
    3130                 :            :   win32_unset_event_mask (socket, FD_CONNECT);
    3131                 :            : 
    3132                 :       1240 :   socket->priv->connected_read = TRUE;
    3133                 :       1240 :   socket->priv->connected_write = TRUE;
    3134                 :            : 
    3135                 :       1240 :   return TRUE;
    3136                 :            : }
    3137                 :            : 
    3138                 :            : /**
    3139                 :            :  * g_socket_check_connect_result:
    3140                 :            :  * @socket: a #GSocket
    3141                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3142                 :            :  *
    3143                 :            :  * Checks and resets the pending connect error for the socket.
    3144                 :            :  * This is used to check for errors when g_socket_connect() is
    3145                 :            :  * used in non-blocking mode.
    3146                 :            :  *
    3147                 :            :  * Returns: %TRUE if no error, %FALSE otherwise, setting @error to the error
    3148                 :            :  *
    3149                 :            :  * Since: 2.22
    3150                 :            :  */
    3151                 :            : gboolean
    3152                 :         62 : g_socket_check_connect_result (GSocket  *socket,
    3153                 :            :                                GError  **error)
    3154                 :            : {
    3155                 :            :   int value;
    3156                 :            : 
    3157                 :         62 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    3158                 :            : 
    3159         [ -  + ]:         62 :   if (!check_socket (socket, error))
    3160                 :          0 :     return FALSE;
    3161                 :            : 
    3162         [ -  + ]:         62 :   if (!check_timeout (socket, error))
    3163                 :          0 :     return FALSE;
    3164                 :            : 
    3165         [ -  + ]:         62 :   if (!g_socket_get_option (socket, SOL_SOCKET, SO_ERROR, &value, error))
    3166                 :            :     {
    3167                 :          0 :       g_prefix_error (error, _("Unable to get pending error: "));
    3168                 :          0 :       return FALSE;
    3169                 :            :     }
    3170                 :            : 
    3171         [ -  + ]:         62 :   if (value != 0)
    3172                 :            :     {
    3173                 :          0 :       g_set_error_literal (error, G_IO_ERROR, socket_io_error_from_errno (value),
    3174                 :          0 :                            socket_strerror (value));
    3175         [ #  # ]:          0 :       if (socket->priv->remote_address)
    3176                 :            :         {
    3177                 :          0 :           g_object_unref (socket->priv->remote_address);
    3178                 :          0 :           socket->priv->remote_address = NULL;
    3179                 :            :         }
    3180                 :          0 :       return FALSE;
    3181                 :            :     }
    3182                 :            : 
    3183                 :         62 :   socket->priv->connected_read = TRUE;
    3184                 :         62 :   socket->priv->connected_write = TRUE;
    3185                 :            : 
    3186                 :         62 :   return TRUE;
    3187                 :            : }
    3188                 :            : 
    3189                 :            : /**
    3190                 :            :  * g_socket_get_available_bytes:
    3191                 :            :  * @socket: a #GSocket
    3192                 :            :  *
    3193                 :            :  * Get the amount of data pending in the OS input buffer, without blocking.
    3194                 :            :  *
    3195                 :            :  * If @socket is a UDP or SCTP socket, this will return the size of
    3196                 :            :  * just the next packet, even if additional packets are buffered after
    3197                 :            :  * that one.
    3198                 :            :  *
    3199                 :            :  * Note that on Windows, this function is rather inefficient in the
    3200                 :            :  * UDP case, and so if you know any plausible upper bound on the size
    3201                 :            :  * of the incoming packet, it is better to just do a
    3202                 :            :  * g_socket_receive() with a buffer of that size, rather than calling
    3203                 :            :  * g_socket_get_available_bytes() first and then doing a receive of
    3204                 :            :  * exactly the right size.
    3205                 :            :  *
    3206                 :            :  * Returns: the number of bytes that can be read from the socket
    3207                 :            :  * without blocking or truncating, or -1 on error.
    3208                 :            :  *
    3209                 :            :  * Since: 2.32
    3210                 :            :  */
    3211                 :            : gssize
    3212                 :          9 : g_socket_get_available_bytes (GSocket *socket)
    3213                 :            : {
    3214                 :            : #ifndef SO_NREAD
    3215                 :          9 :   const gint bufsize = 64 * 1024;
    3216                 :            :   static guchar *buf = NULL;
    3217                 :            : #endif
    3218                 :            : #ifdef G_OS_WIN32
    3219                 :            :   u_long avail;
    3220                 :            : #else
    3221                 :            :   gint avail;
    3222                 :            : #endif
    3223                 :            : 
    3224                 :          9 :   g_return_val_if_fail (G_IS_SOCKET (socket), -1);
    3225                 :            : 
    3226         [ -  + ]:          9 :   if (!check_socket (socket, NULL))
    3227                 :          0 :     return -1;
    3228                 :            : 
    3229                 :            : #ifdef SO_NREAD
    3230                 :            :   if (!g_socket_get_option (socket, SOL_SOCKET, SO_NREAD, &avail, NULL))
    3231                 :            :       return -1;
    3232                 :            : #else
    3233         [ +  + ]:          9 :   if (socket->priv->type == G_SOCKET_TYPE_DATAGRAM)
    3234                 :            :     {
    3235   [ +  +  +  -  :          4 :       if (G_UNLIKELY (g_once_init_enter_pointer (&buf)))
                   +  + ]
    3236                 :          1 :         g_once_init_leave_pointer (&buf, g_malloc (bufsize));
    3237                 :            : 
    3238                 :            :       /* On datagram sockets, FIONREAD ioctl is not reliable because many
    3239                 :            :        * systems add internal header size to the reported size, making it
    3240                 :            :        * unusable for this function. */
    3241                 :          4 :       avail = recv (socket->priv->fd, buf, bufsize, MSG_PEEK);
    3242         [ +  + ]:          4 :       if ((gint) avail == -1)
    3243                 :            :         {
    3244                 :          1 :           int errsv = get_socket_errno ();
    3245                 :            : #ifdef G_OS_WIN32
    3246                 :            :           if (errsv == WSAEWOULDBLOCK)
    3247                 :            : #else
    3248   [ -  +  -  - ]:          1 :           if (errsv == EWOULDBLOCK || errsv == EAGAIN)
    3249                 :            : #endif
    3250                 :          1 :             avail = 0;
    3251                 :            :         }
    3252                 :            :     }
    3253                 :            :   else
    3254                 :            :     {
    3255                 :            : #ifdef G_OS_WIN32
    3256                 :            :       if (ioctlsocket (socket->priv->fd, FIONREAD, &avail) < 0)
    3257                 :            : #else
    3258         [ -  + ]:          5 :       if (ioctl (socket->priv->fd, FIONREAD, &avail) < 0)
    3259                 :            : #endif
    3260                 :          0 :         avail = -1;
    3261                 :            :     }
    3262                 :            : #endif
    3263                 :            : 
    3264                 :          9 :   return avail;
    3265                 :            : }
    3266                 :            : 
    3267                 :            : /* Block on a timed wait for @condition until (@start_time + @timeout).
    3268                 :            :  * Return %G_IO_ERROR_TIMED_OUT if the timeout is reached; otherwise %TRUE.
    3269                 :            :  */
    3270                 :            : static gboolean
    3271                 :       4983 : block_on_timeout (GSocket       *socket,
    3272                 :            :                   GIOCondition   condition,
    3273                 :            :                   gint64         timeout_us,
    3274                 :            :                   gint64         start_time,
    3275                 :            :                   GCancellable  *cancellable,
    3276                 :            :                   GError       **error)
    3277                 :            : {
    3278                 :       4983 :   gint64 wait_timeout = -1;
    3279                 :            : 
    3280                 :       4983 :   g_return_val_if_fail (timeout_us != 0, TRUE);
    3281                 :            : 
    3282                 :            :   /* check if we've timed out or how much time to wait at most */
    3283         [ +  + ]:       4983 :   if (timeout_us >= 0)
    3284                 :            :     {
    3285                 :          2 :       gint64 elapsed = g_get_monotonic_time () - start_time;
    3286                 :            : 
    3287         [ -  + ]:          2 :       if (elapsed >= timeout_us)
    3288                 :            :         {
    3289                 :          0 :           g_set_error_literal (error,
    3290                 :            :                                G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
    3291                 :            :                                _("Socket I/O timed out"));
    3292                 :          0 :           return FALSE;
    3293                 :            :         }
    3294                 :            : 
    3295                 :          2 :       wait_timeout = timeout_us - elapsed;
    3296                 :            :     }
    3297                 :            : 
    3298                 :       4983 :   return g_socket_condition_timed_wait (socket, condition, wait_timeout,
    3299                 :            :                                         cancellable, error);
    3300                 :            : }
    3301                 :            : 
    3302                 :            : static gssize
    3303                 :      28765 : g_socket_receive_with_timeout (GSocket       *socket,
    3304                 :            :                                guint8        *buffer,
    3305                 :            :                                gsize          size,
    3306                 :            :                                gint64         timeout_us,
    3307                 :            :                                GCancellable  *cancellable,
    3308                 :            :                                GError       **error)
    3309                 :            : {
    3310                 :            :   gssize ret;
    3311                 :            :   gint64 start_time;
    3312                 :            : 
    3313                 :      28765 :   g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, -1);
    3314                 :            : 
    3315                 :      28764 :   start_time = g_get_monotonic_time ();
    3316                 :            : 
    3317         [ -  + ]:      28765 :   if (!check_socket (socket, error))
    3318                 :          0 :     return -1;
    3319                 :            : 
    3320         [ +  + ]:      28765 :   if (!check_timeout (socket, error))
    3321                 :          1 :     return -1;
    3322                 :            : 
    3323         [ +  + ]:      28764 :   if (g_cancellable_set_error_if_cancelled (cancellable, error))
    3324                 :          1 :     return -1;
    3325                 :            : 
    3326                 :            :   while (1)
    3327                 :            :     {
    3328         [ +  + ]:      33713 :       if ((ret = recv (socket->priv->fd, buffer, size, 0)) < 0)
    3329                 :            :         {
    3330                 :       4990 :           int errsv = get_socket_errno ();
    3331                 :            : 
    3332         [ -  + ]:       4991 :           if (errsv == EINTR)
    3333                 :          0 :             continue;
    3334                 :            : 
    3335                 :            : #ifdef WSAEWOULDBLOCK
    3336                 :            :           if (errsv == WSAEWOULDBLOCK)
    3337                 :            : #else
    3338   [ +  +  -  + ]:       4991 :           if (errsv == EWOULDBLOCK ||
    3339                 :            :               errsv == EAGAIN)
    3340                 :            : #endif
    3341                 :            :             {
    3342                 :            :               win32_unset_event_mask (socket, FD_READ);
    3343                 :            : 
    3344         [ +  + ]:       4990 :               if (timeout_us != 0)
    3345                 :            :                 {
    3346         [ +  + ]:       4952 :                   if (!block_on_timeout (socket, G_IO_IN, timeout_us, start_time,
    3347                 :            :                                          cancellable, error))
    3348                 :          2 :                     return -1;
    3349                 :            : 
    3350                 :       4950 :                   continue;
    3351                 :            :                 }
    3352                 :            :             }
    3353                 :            : 
    3354                 :            :           win32_unset_event_mask (socket, FD_READ);
    3355                 :            : 
    3356   [ +  -  +  + ]:         39 :           socket_set_error_lazy (error, errsv, _("Error receiving data: %s"));
    3357                 :         39 :           return -1;
    3358                 :            :         }
    3359                 :            : 
    3360                 :            :       win32_unset_event_mask (socket, FD_READ);
    3361                 :            : 
    3362                 :      28722 :       break;
    3363                 :            :     }
    3364                 :            : 
    3365                 :      28722 :   return ret;
    3366                 :            : }
    3367                 :            : 
    3368                 :            : /**
    3369                 :            :  * g_socket_receive_bytes:
    3370                 :            :  * @socket: a #GSocket
    3371                 :            :  * @size: the number of bytes you want to read from the socket
    3372                 :            :  * @timeout_us: the timeout to wait for, in microseconds, or `-1` to block
    3373                 :            :  *   indefinitely
    3374                 :            :  * @cancellable: (nullable): a %GCancellable, or `NULL`
    3375                 :            :  * @error: return location for a #GError, or `NULL`
    3376                 :            :  *
    3377                 :            :  * Receives data (up to @size bytes) from a socket.
    3378                 :            :  *
    3379                 :            :  * This function is a variant of [method@Gio.Socket.receive] which returns a
    3380                 :            :  * [struct@GLib.Bytes] rather than a plain buffer.
    3381                 :            :  *
    3382                 :            :  * Pass `-1` to @timeout_us to block indefinitely until data is received (or
    3383                 :            :  * the connection is closed, or there is an error). Pass `0` to use the default
    3384                 :            :  * timeout from [property@Gio.Socket:timeout], or pass a positive number to wait
    3385                 :            :  * for that many microseconds for data before returning `G_IO_ERROR_TIMED_OUT`.
    3386                 :            :  *
    3387                 :            :  * Returns: (transfer full): a bytes buffer containing the
    3388                 :            :  *   received bytes, or `NULL` on error
    3389                 :            :  * Since: 2.80
    3390                 :            :  */
    3391                 :            : GBytes *
    3392                 :          4 : g_socket_receive_bytes (GSocket       *socket,
    3393                 :            :                         gsize          size,
    3394                 :            :                         gint64         timeout_us,
    3395                 :            :                         GCancellable  *cancellable,
    3396                 :            :                         GError       **error)
    3397                 :            : {
    3398                 :            :   guint8 *data;
    3399                 :            :   gssize res;
    3400                 :            :   GBytes *buf;
    3401                 :            : 
    3402                 :          4 :   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
    3403                 :          4 :   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
    3404                 :          4 :   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
    3405                 :            : 
    3406                 :          4 :   data = g_new0 (guint8, size);
    3407                 :          4 :   res = g_socket_receive_with_timeout (socket, data, size, timeout_us, cancellable, error);
    3408         [ +  + ]:          4 :   if (res < 0)
    3409                 :            :     {
    3410                 :          2 :       g_free (data);
    3411                 :          2 :       return NULL;
    3412                 :            :     }
    3413                 :            : 
    3414         [ +  + ]:          2 :   if ((gsize) res == size)
    3415                 :            :     {
    3416                 :          1 :       buf = g_bytes_new_take (g_steal_pointer (&data), (gsize) res);
    3417                 :            :     }
    3418                 :            :   else
    3419                 :            :     {
    3420                 :            :       GBytes *sub_buf;
    3421                 :            : 
    3422                 :          1 :       buf = g_bytes_new_take (g_steal_pointer (&data), size);
    3423                 :          1 :       sub_buf = g_bytes_new_from_bytes (buf, 0, (gsize) res);
    3424                 :          1 :       g_bytes_unref (buf);
    3425                 :          1 :       buf = g_steal_pointer (&sub_buf);
    3426                 :            :     }
    3427                 :            : 
    3428                 :          2 :   return g_steal_pointer (&buf);
    3429                 :            : }
    3430                 :            : 
    3431                 :            : /**
    3432                 :            :  * g_socket_receive:
    3433                 :            :  * @socket: a #GSocket
    3434                 :            :  * @buffer: (array length=size) (element-type guint8) (out caller-allocates):
    3435                 :            :  *     a buffer to read data into (which should be at least @size bytes long).
    3436                 :            :  * @size: (in): the number of bytes you want to read from the socket
    3437                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    3438                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3439                 :            :  *
    3440                 :            :  * Receive data (up to @size bytes) from a socket. This is mainly used by
    3441                 :            :  * connection-oriented sockets; it is identical to g_socket_receive_from()
    3442                 :            :  * with @address set to %NULL.
    3443                 :            :  *
    3444                 :            :  * For %G_SOCKET_TYPE_DATAGRAM and %G_SOCKET_TYPE_SEQPACKET sockets,
    3445                 :            :  * g_socket_receive() will always read either 0 or 1 complete messages from
    3446                 :            :  * the socket. If the received message is too large to fit in @buffer, then
    3447                 :            :  * the data beyond @size bytes will be discarded, without any explicit
    3448                 :            :  * indication that this has occurred.
    3449                 :            :  *
    3450                 :            :  * For %G_SOCKET_TYPE_STREAM sockets, g_socket_receive() can return any
    3451                 :            :  * number of bytes, up to @size. If more than @size bytes have been
    3452                 :            :  * received, the additional data will be returned in future calls to
    3453                 :            :  * g_socket_receive().
    3454                 :            :  *
    3455                 :            :  * If the socket is in blocking mode the call will block until there
    3456                 :            :  * is some data to receive, the connection is closed, or there is an
    3457                 :            :  * error. If there is no data available and the socket is in
    3458                 :            :  * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be
    3459                 :            :  * returned. To be notified when data is available, wait for the
    3460                 :            :  * %G_IO_IN condition.
    3461                 :            :  *
    3462                 :            :  * On error -1 is returned and @error is set accordingly.
    3463                 :            :  *
    3464                 :            :  * Returns: Number of bytes read, or 0 if the connection was closed by
    3465                 :            :  * the peer, or -1 on error
    3466                 :            :  *
    3467                 :            :  * Since: 2.22
    3468                 :            :  */
    3469                 :            : gssize
    3470                 :         79 : g_socket_receive (GSocket       *socket,
    3471                 :            :                   gchar         *buffer,
    3472                 :            :                   gsize          size,
    3473                 :            :                   GCancellable  *cancellable,
    3474                 :            :                   GError       **error)
    3475                 :            : {
    3476                 :         79 :   return g_socket_receive_with_timeout (socket, (guint8 *) buffer, size,
    3477         [ +  + ]:         79 :                                         socket->priv->blocking ? -1 : 0,
    3478                 :            :                                         cancellable, error);
    3479                 :            : }
    3480                 :            : 
    3481                 :            : /**
    3482                 :            :  * g_socket_receive_with_blocking:
    3483                 :            :  * @socket: a #GSocket
    3484                 :            :  * @buffer: (array length=size) (element-type guint8) (out caller-allocates):
    3485                 :            :  *     a buffer to read data into (which should be at least @size bytes long).
    3486                 :            :  * @size: (in): the number of bytes you want to read from the socket
    3487                 :            :  * @blocking: whether to do blocking or non-blocking I/O
    3488                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    3489                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3490                 :            :  *
    3491                 :            :  * This behaves exactly the same as g_socket_receive(), except that
    3492                 :            :  * the choice of blocking or non-blocking behavior is determined by
    3493                 :            :  * the @blocking argument rather than by @socket's properties.
    3494                 :            :  *
    3495                 :            :  * Returns: Number of bytes read, or 0 if the connection was closed by
    3496                 :            :  * the peer, or -1 on error
    3497                 :            :  *
    3498                 :            :  * Since: 2.26
    3499                 :            :  */
    3500                 :            : gssize
    3501                 :      28682 : g_socket_receive_with_blocking (GSocket       *socket,
    3502                 :            :                                 gchar         *buffer,
    3503                 :            :                                 gsize          size,
    3504                 :            :                                 gboolean       blocking,
    3505                 :            :                                 GCancellable  *cancellable,
    3506                 :            :                                 GError       **error)
    3507                 :            : {
    3508         [ +  + ]:      28682 :   return g_socket_receive_with_timeout (socket, (guint8 *) buffer, size,
    3509                 :            :                                         blocking ? -1 : 0, cancellable, error);
    3510                 :            : }
    3511                 :            : 
    3512                 :            : /**
    3513                 :            :  * g_socket_receive_bytes_from:
    3514                 :            :  * @socket: a #GSocket
    3515                 :            :  * @address: (out) (optional): return location for a #GSocketAddress
    3516                 :            :  * @size: the number of bytes you want to read from the socket
    3517                 :            :  * @timeout_us: the timeout to wait for, in microseconds, or `-1` to block
    3518                 :            :  *   indefinitely
    3519                 :            :  * @cancellable: (nullable): a #GCancellable, or `NULL`
    3520                 :            :  * @error: return location for a #GError, or `NULL`
    3521                 :            :  *
    3522                 :            :  * Receive data (up to @size bytes) from a socket.
    3523                 :            :  *
    3524                 :            :  * This function is a variant of [method@Gio.Socket.receive_from] which returns
    3525                 :            :  * a [struct@GLib.Bytes] rather than a plain buffer.
    3526                 :            :  *
    3527                 :            :  * If @address is non-%NULL then @address will be set equal to the
    3528                 :            :  * source address of the received packet.
    3529                 :            :  *
    3530                 :            :  * The @address is owned by the caller.
    3531                 :            :  *
    3532                 :            :  * Pass `-1` to @timeout_us to block indefinitely until data is received (or
    3533                 :            :  * the connection is closed, or there is an error). Pass `0` to use the default
    3534                 :            :  * timeout from [property@Gio.Socket:timeout], or pass a positive number to wait
    3535                 :            :  * for that many microseconds for data before returning `G_IO_ERROR_TIMED_OUT`.
    3536                 :            :  *
    3537                 :            :  * Returns: (transfer full): a bytes buffer containing the
    3538                 :            :  *   received bytes, or `NULL` on error
    3539                 :            :  * Since: 2.80
    3540                 :            :  */
    3541                 :            : GBytes *
    3542                 :          4 : g_socket_receive_bytes_from (GSocket         *socket,
    3543                 :            :                              GSocketAddress **address,
    3544                 :            :                              gsize            size,
    3545                 :            :                              gint64           timeout_us,
    3546                 :            :                              GCancellable    *cancellable,
    3547                 :            :                              GError         **error)
    3548                 :            : {
    3549                 :            :   GInputVector v;
    3550                 :            :   gssize res;
    3551                 :            :   GBytes *buf;
    3552                 :            : 
    3553                 :          4 :   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
    3554                 :          4 :   g_return_val_if_fail (address == NULL || *address == NULL, NULL);
    3555                 :          4 :   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
    3556                 :          4 :   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
    3557                 :            : 
    3558                 :          4 :   v.buffer = g_new0 (guint8, size);
    3559                 :          4 :   v.size = size;
    3560                 :            : 
    3561                 :          4 :   res = g_socket_receive_message_with_timeout (socket,
    3562                 :            :                                                address,
    3563                 :            :                                                &v, 1,
    3564                 :            :                                                NULL, 0, NULL,
    3565                 :            :                                                timeout_us,
    3566                 :            :                                                cancellable,
    3567                 :            :                                                error);
    3568         [ +  + ]:          4 :   if (res < 0)
    3569                 :            :     {
    3570                 :          2 :       g_free (v.buffer);
    3571                 :          2 :       return NULL;
    3572                 :            :     }
    3573                 :            : 
    3574         [ +  + ]:          2 :   if ((gsize) res == size)
    3575                 :            :     {
    3576                 :          1 :       buf = g_bytes_new_take (g_steal_pointer (&v.buffer), (gsize) res);
    3577                 :            :     }
    3578                 :            :   else
    3579                 :            :     {
    3580                 :            :       GBytes *sub_buf;
    3581                 :            : 
    3582                 :          1 :       buf = g_bytes_new_take (g_steal_pointer (&v.buffer), size);
    3583                 :          1 :       sub_buf = g_bytes_new_from_bytes (buf, 0, (gsize) res);
    3584                 :          1 :       g_bytes_unref (buf);
    3585                 :          1 :       buf = g_steal_pointer (&sub_buf);
    3586                 :            :     }
    3587                 :            : 
    3588                 :          2 :   return g_steal_pointer (&buf);
    3589                 :            : }
    3590                 :            : 
    3591                 :            : /**
    3592                 :            :  * g_socket_receive_from:
    3593                 :            :  * @socket: a #GSocket
    3594                 :            :  * @address: (out) (optional): a pointer to a #GSocketAddress
    3595                 :            :  *     pointer, or %NULL
    3596                 :            :  * @buffer: (array length=size) (element-type guint8) (out caller-allocates):
    3597                 :            :  *     a buffer to read data into (which should be at least @size bytes long).
    3598                 :            :  * @size: (in): the number of bytes you want to read from the socket
    3599                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    3600                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3601                 :            :  *
    3602                 :            :  * Receive data (up to @size bytes) from a socket.
    3603                 :            :  *
    3604                 :            :  * If @address is non-%NULL then @address will be set equal to the
    3605                 :            :  * source address of the received packet.
    3606                 :            :  * @address is owned by the caller.
    3607                 :            :  *
    3608                 :            :  * See g_socket_receive() for additional information.
    3609                 :            :  *
    3610                 :            :  * Returns: Number of bytes read, or 0 if the connection was closed by
    3611                 :            :  * the peer, or -1 on error
    3612                 :            :  *
    3613                 :            :  * Since: 2.22
    3614                 :            :  */
    3615                 :            : gssize
    3616                 :         35 : g_socket_receive_from (GSocket         *socket,
    3617                 :            :                        GSocketAddress **address,
    3618                 :            :                        gchar           *buffer,
    3619                 :            :                        gsize            size,
    3620                 :            :                        GCancellable    *cancellable,
    3621                 :            :                        GError         **error)
    3622                 :            : {
    3623                 :            :   GInputVector v;
    3624                 :            : 
    3625                 :         35 :   v.buffer = buffer;
    3626                 :         35 :   v.size = size;
    3627                 :            : 
    3628                 :         35 :   return g_socket_receive_message (socket,
    3629                 :            :                                    address,
    3630                 :            :                                    &v, 1,
    3631                 :            :                                    NULL, 0, NULL,
    3632                 :            :                                    cancellable,
    3633                 :            :                                    error);
    3634                 :            : }
    3635                 :            : 
    3636                 :            : /* See the comment about SIGPIPE above. */
    3637                 :            : #ifdef MSG_NOSIGNAL
    3638                 :            : #define G_SOCKET_DEFAULT_SEND_FLAGS MSG_NOSIGNAL
    3639                 :            : #else
    3640                 :            : #define G_SOCKET_DEFAULT_SEND_FLAGS 0
    3641                 :            : #endif
    3642                 :            : 
    3643                 :            : static gssize
    3644                 :       7280 : g_socket_send_with_timeout (GSocket       *socket,
    3645                 :            :                             const guint8  *buffer,
    3646                 :            :                             gsize          size,
    3647                 :            :                             gint64         timeout_us,
    3648                 :            :                             GCancellable  *cancellable,
    3649                 :            :                             GError       **error)
    3650                 :            : {
    3651                 :            :   gssize ret;
    3652                 :            :   gint64 start_time;
    3653                 :            : 
    3654                 :       7280 :   g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, -1);
    3655                 :            : 
    3656                 :       7280 :   start_time = g_get_monotonic_time ();
    3657                 :            : 
    3658         [ -  + ]:       7280 :   if (!check_socket (socket, error))
    3659                 :          0 :     return -1;
    3660                 :            : 
    3661         [ -  + ]:       7280 :   if (!check_timeout (socket, error))
    3662                 :          0 :     return -1;
    3663                 :            : 
    3664         [ -  + ]:       7280 :   if (g_cancellable_set_error_if_cancelled (cancellable, error))
    3665                 :          0 :     return -1;
    3666                 :            : 
    3667                 :            :   while (1)
    3668                 :            :     {
    3669         [ +  + ]:       7280 :       if ((ret = send (socket->priv->fd, (const char *)buffer, size, G_SOCKET_DEFAULT_SEND_FLAGS)) < 0)
    3670                 :            :         {
    3671                 :        157 :           int errsv = get_socket_errno ();
    3672                 :            : 
    3673         [ -  + ]:        157 :           if (errsv == EINTR)
    3674                 :          0 :             continue;
    3675                 :            : 
    3676                 :            : #ifdef WSAEWOULDBLOCK
    3677                 :            :           if (errsv == WSAEWOULDBLOCK)
    3678                 :            : #else
    3679   [ +  +  -  + ]:        157 :           if (errsv == EWOULDBLOCK ||
    3680                 :            :               errsv == EAGAIN)
    3681                 :            : #endif
    3682                 :            :             {
    3683                 :            :               win32_unset_event_mask (socket, FD_WRITE);
    3684                 :            : 
    3685         [ -  + ]:        155 :               if (timeout_us != 0)
    3686                 :            :                 {
    3687         [ #  # ]:          0 :                   if (!block_on_timeout (socket, G_IO_OUT, timeout_us, start_time,
    3688                 :            :                                          cancellable, error))
    3689                 :          0 :                     return -1;
    3690                 :            : 
    3691                 :          0 :                   continue;
    3692                 :            :                 }
    3693                 :            :             }
    3694                 :            : 
    3695   [ +  -  +  + ]:        157 :           socket_set_error_lazy (error, errsv, _("Error sending data: %s"));
    3696                 :        157 :           return -1;
    3697                 :            :         }
    3698                 :       7123 :       break;
    3699                 :            :     }
    3700                 :            : 
    3701                 :       7123 :   return ret;
    3702                 :            : }
    3703                 :            : 
    3704                 :            : /**
    3705                 :            :  * g_socket_send:
    3706                 :            :  * @socket: a #GSocket
    3707                 :            :  * @buffer: (array length=size) (element-type guint8): the buffer
    3708                 :            :  *     containing the data to send.
    3709                 :            :  * @size: the number of bytes to send
    3710                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    3711                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3712                 :            :  *
    3713                 :            :  * Tries to send @size bytes from @buffer on the socket. This is
    3714                 :            :  * mainly used by connection-oriented sockets; it is identical to
    3715                 :            :  * g_socket_send_to() with @address set to %NULL.
    3716                 :            :  *
    3717                 :            :  * If the socket is in blocking mode the call will block until there is
    3718                 :            :  * space for the data in the socket queue. If there is no space available
    3719                 :            :  * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error
    3720                 :            :  * will be returned. To be notified when space is available, wait for the
    3721                 :            :  * %G_IO_OUT condition. Note though that you may still receive
    3722                 :            :  * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously
    3723                 :            :  * notified of a %G_IO_OUT condition. (On Windows in particular, this is
    3724                 :            :  * very common due to the way the underlying APIs work.)
    3725                 :            :  *
    3726                 :            :  * On error -1 is returned and @error is set accordingly.
    3727                 :            :  *
    3728                 :            :  * Returns: Number of bytes written (which may be less than @size), or -1
    3729                 :            :  * on error
    3730                 :            :  *
    3731                 :            :  * Since: 2.22
    3732                 :            :  */
    3733                 :            : gssize
    3734                 :         84 : g_socket_send (GSocket       *socket,
    3735                 :            :                const gchar   *buffer,
    3736                 :            :                gsize          size,
    3737                 :            :                GCancellable  *cancellable,
    3738                 :            :                GError       **error)
    3739                 :            : {
    3740                 :        168 :   return g_socket_send_with_blocking (socket, buffer, size,
    3741                 :         84 :                                       socket->priv->blocking,
    3742                 :            :                                       cancellable, error);
    3743                 :            : }
    3744                 :            : 
    3745                 :            : /**
    3746                 :            :  * g_socket_send_with_blocking:
    3747                 :            :  * @socket: a #GSocket
    3748                 :            :  * @buffer: (array length=size) (element-type guint8): the buffer
    3749                 :            :  *     containing the data to send.
    3750                 :            :  * @size: the number of bytes to send
    3751                 :            :  * @blocking: whether to do blocking or non-blocking I/O
    3752                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    3753                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3754                 :            :  *
    3755                 :            :  * This behaves exactly the same as g_socket_send(), except that
    3756                 :            :  * the choice of blocking or non-blocking behavior is determined by
    3757                 :            :  * the @blocking argument rather than by @socket's properties.
    3758                 :            :  *
    3759                 :            :  * Returns: Number of bytes written (which may be less than @size), or -1
    3760                 :            :  * on error
    3761                 :            :  *
    3762                 :            :  * Since: 2.26
    3763                 :            :  */
    3764                 :            : gssize
    3765                 :       7280 : g_socket_send_with_blocking (GSocket       *socket,
    3766                 :            :                              const gchar   *buffer,
    3767                 :            :                              gsize          size,
    3768                 :            :                              gboolean       blocking,
    3769                 :            :                              GCancellable  *cancellable,
    3770                 :            :                              GError       **error)
    3771                 :            : {
    3772         [ +  + ]:       7280 :   return g_socket_send_with_timeout (socket, (const guint8 *) buffer, size,
    3773                 :            :                                      blocking ? -1 : 0, cancellable, error);
    3774                 :            : }
    3775                 :            : 
    3776                 :            : /**
    3777                 :            :  * g_socket_send_to:
    3778                 :            :  * @socket: a #GSocket
    3779                 :            :  * @address: (nullable): a #GSocketAddress, or %NULL
    3780                 :            :  * @buffer: (array length=size) (element-type guint8): the buffer
    3781                 :            :  *     containing the data to send.
    3782                 :            :  * @size: the number of bytes to send
    3783                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    3784                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3785                 :            :  *
    3786                 :            :  * Tries to send @size bytes from @buffer to @address. If @address is
    3787                 :            :  * %NULL then the message is sent to the default receiver (set by
    3788                 :            :  * g_socket_connect()).
    3789                 :            :  *
    3790                 :            :  * See g_socket_send() for additional information.
    3791                 :            :  *
    3792                 :            :  * Returns: Number of bytes written (which may be less than @size), or -1
    3793                 :            :  * on error
    3794                 :            :  *
    3795                 :            :  * Since: 2.22
    3796                 :            :  */
    3797                 :            : gssize
    3798                 :         28 : g_socket_send_to (GSocket         *socket,
    3799                 :            :                   GSocketAddress  *address,
    3800                 :            :                   const gchar     *buffer,
    3801                 :            :                   gsize            size,
    3802                 :            :                   GCancellable    *cancellable,
    3803                 :            :                   GError         **error)
    3804                 :            : {
    3805                 :            :   GOutputVector v;
    3806                 :            : 
    3807                 :         28 :   v.buffer = buffer;
    3808                 :         28 :   v.size = size;
    3809                 :            : 
    3810                 :         28 :   return g_socket_send_message (socket,
    3811                 :            :                                 address,
    3812                 :            :                                 &v, 1,
    3813                 :            :                                 NULL, 0,
    3814                 :            :                                 0,
    3815                 :            :                                 cancellable,
    3816                 :            :                                 error);
    3817                 :            : }
    3818                 :            : 
    3819                 :            : /**
    3820                 :            :  * g_socket_shutdown:
    3821                 :            :  * @socket: a #GSocket
    3822                 :            :  * @shutdown_read: whether to shut down the read side
    3823                 :            :  * @shutdown_write: whether to shut down the write side
    3824                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3825                 :            :  *
    3826                 :            :  * Shut down part or all of a full-duplex connection.
    3827                 :            :  *
    3828                 :            :  * If @shutdown_read is %TRUE then the receiving side of the connection
    3829                 :            :  * is shut down, and further reading is disallowed.
    3830                 :            :  *
    3831                 :            :  * If @shutdown_write is %TRUE then the sending side of the connection
    3832                 :            :  * is shut down, and further writing is disallowed.
    3833                 :            :  *
    3834                 :            :  * It is allowed for both @shutdown_read and @shutdown_write to be %TRUE.
    3835                 :            :  *
    3836                 :            :  * One example where it is useful to shut down only one side of a connection is
    3837                 :            :  * graceful disconnect for TCP connections where you close the sending side,
    3838                 :            :  * then wait for the other side to close the connection, thus ensuring that the
    3839                 :            :  * other side saw all sent data.
    3840                 :            :  *
    3841                 :            :  * Returns: %TRUE on success, %FALSE on error
    3842                 :            :  *
    3843                 :            :  * Since: 2.22
    3844                 :            :  */
    3845                 :            : gboolean
    3846                 :          7 : g_socket_shutdown (GSocket   *socket,
    3847                 :            :                    gboolean   shutdown_read,
    3848                 :            :                    gboolean   shutdown_write,
    3849                 :            :                    GError   **error)
    3850                 :            : {
    3851                 :            :   int how;
    3852                 :            : 
    3853                 :          7 :   g_return_val_if_fail (G_IS_SOCKET (socket), TRUE);
    3854                 :            : 
    3855         [ -  + ]:          7 :   if (!check_socket (socket, error))
    3856                 :          0 :     return FALSE;
    3857                 :            : 
    3858                 :            :   /* Do nothing? */
    3859   [ +  -  -  + ]:          7 :   if (!shutdown_read && !shutdown_write)
    3860                 :          0 :     return TRUE;
    3861                 :            : 
    3862                 :            : #ifndef G_OS_WIN32
    3863   [ -  +  -  - ]:          7 :   if (shutdown_read && shutdown_write)
    3864                 :          0 :     how = SHUT_RDWR;
    3865         [ -  + ]:          7 :   else if (shutdown_read)
    3866                 :          0 :     how = SHUT_RD;
    3867                 :            :   else
    3868                 :          7 :     how = SHUT_WR;
    3869                 :            : #else
    3870                 :            :   if (shutdown_read && shutdown_write)
    3871                 :            :     how = SD_BOTH;
    3872                 :            :   else if (shutdown_read)
    3873                 :            :     how = SD_RECEIVE;
    3874                 :            :   else
    3875                 :            :     how = SD_SEND;
    3876                 :            : #endif
    3877                 :            : 
    3878         [ -  + ]:          7 :   if (shutdown (socket->priv->fd, how) != 0)
    3879                 :            :     {
    3880                 :          0 :       int errsv = get_socket_errno ();
    3881                 :          0 :       g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
    3882                 :            :                    _("Unable to shutdown socket: %s"), socket_strerror (errsv));
    3883                 :          0 :       return FALSE;
    3884                 :            :     }
    3885                 :            : 
    3886         [ -  + ]:          7 :   if (shutdown_read)
    3887                 :          0 :     socket->priv->connected_read = FALSE;
    3888         [ +  - ]:          7 :   if (shutdown_write)
    3889                 :          7 :     socket->priv->connected_write = FALSE;
    3890                 :            : 
    3891                 :          7 :   return TRUE;
    3892                 :            : }
    3893                 :            : 
    3894                 :            : /**
    3895                 :            :  * g_socket_close:
    3896                 :            :  * @socket: a #GSocket
    3897                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    3898                 :            :  *
    3899                 :            :  * Closes the socket, shutting down any active connection.
    3900                 :            :  *
    3901                 :            :  * Closing a socket does not wait for all outstanding I/O operations
    3902                 :            :  * to finish, so the caller should not rely on them to be guaranteed
    3903                 :            :  * to complete even if the close returns with no error.
    3904                 :            :  *
    3905                 :            :  * Once the socket is closed, all other operations will return
    3906                 :            :  * %G_IO_ERROR_CLOSED. Closing a socket multiple times will not
    3907                 :            :  * return an error.
    3908                 :            :  *
    3909                 :            :  * Sockets will be automatically closed when the last reference
    3910                 :            :  * is dropped, but you might want to call this function to make sure
    3911                 :            :  * resources are released as early as possible.
    3912                 :            :  *
    3913                 :            :  * Beware that due to the way that TCP works, it is possible for
    3914                 :            :  * recently-sent data to be lost if either you close a socket while the
    3915                 :            :  * %G_IO_IN condition is set, or else if the remote connection tries to
    3916                 :            :  * send something to you after you close the socket but before it has
    3917                 :            :  * finished reading all of the data you sent. There is no easy generic
    3918                 :            :  * way to avoid this problem; the easiest fix is to design the network
    3919                 :            :  * protocol such that the client will never send data "out of turn".
    3920                 :            :  * Another solution is for the server to half-close the connection by
    3921                 :            :  * calling g_socket_shutdown() with only the @shutdown_write flag set,
    3922                 :            :  * and then wait for the client to notice this and close its side of the
    3923                 :            :  * connection, after which the server can safely call g_socket_close().
    3924                 :            :  * (This is what #GTcpConnection does if you call
    3925                 :            :  * g_tcp_connection_set_graceful_disconnect(). But of course, this
    3926                 :            :  * only works if the client will close its connection after the server
    3927                 :            :  * does.)
    3928                 :            :  *
    3929                 :            :  * Returns: %TRUE on success, %FALSE on error
    3930                 :            :  *
    3931                 :            :  * Since: 2.22
    3932                 :            :  */
    3933                 :            : gboolean
    3934                 :       1401 : g_socket_close (GSocket  *socket,
    3935                 :            :                 GError  **error)
    3936                 :            : {
    3937                 :            :   int res;
    3938                 :            : 
    3939                 :       1401 :   g_return_val_if_fail (G_IS_SOCKET (socket), TRUE);
    3940                 :            : 
    3941         [ -  + ]:       1401 :   if (socket->priv->closed)
    3942                 :          0 :     return TRUE; /* Multiple close not an error */
    3943                 :            : 
    3944         [ -  + ]:       1401 :   if (!check_socket (socket, error))
    3945                 :          0 :     return FALSE;
    3946                 :            : 
    3947                 :            :   while (1)
    3948                 :            :     {
    3949                 :            : #ifdef G_OS_WIN32
    3950                 :            :       res = closesocket (socket->priv->fd);
    3951                 :            : #else
    3952                 :       1401 :       res = close (socket->priv->fd);
    3953                 :            : #endif
    3954         [ -  + ]:       1401 :       if (res == -1)
    3955                 :            :         {
    3956                 :          0 :           int errsv = get_socket_errno ();
    3957                 :            : 
    3958         [ #  # ]:          0 :           if (errsv == EINTR)
    3959                 :          0 :             continue;
    3960                 :            : 
    3961                 :          0 :           g_set_error (error, G_IO_ERROR,
    3962                 :          0 :                        socket_io_error_from_errno (errsv),
    3963                 :            :                        _("Error closing socket: %s"),
    3964                 :            :                        socket_strerror (errsv));
    3965                 :          0 :           return FALSE;
    3966                 :            :         }
    3967                 :       1401 :       break;
    3968                 :            :     }
    3969                 :            : 
    3970                 :       1401 :   socket->priv->fd = -1;
    3971                 :       1401 :   socket->priv->connected_read = FALSE;
    3972                 :       1401 :   socket->priv->connected_write = FALSE;
    3973                 :       1401 :   socket->priv->closed = TRUE;
    3974         [ +  + ]:       1401 :   if (socket->priv->remote_address)
    3975                 :            :     {
    3976                 :       1228 :       g_object_unref (socket->priv->remote_address);
    3977                 :       1228 :       socket->priv->remote_address = NULL;
    3978                 :            :     }
    3979                 :            : 
    3980                 :       1401 :   return TRUE;
    3981                 :            : }
    3982                 :            : 
    3983                 :            : /**
    3984                 :            :  * g_socket_is_closed:
    3985                 :            :  * @socket: a #GSocket
    3986                 :            :  *
    3987                 :            :  * Checks whether a socket is closed.
    3988                 :            :  *
    3989                 :            :  * Returns: %TRUE if socket is closed, %FALSE otherwise
    3990                 :            :  *
    3991                 :            :  * Since: 2.22
    3992                 :            :  */
    3993                 :            : gboolean
    3994                 :      38536 : g_socket_is_closed (GSocket *socket)
    3995                 :            : {
    3996                 :      38536 :   return socket->priv->closed;
    3997                 :            : }
    3998                 :            : 
    3999                 :            : /* Broken source, used on errors */
    4000                 :            : static gboolean
    4001                 :          0 : broken_dispatch (GSource     *source,
    4002                 :            :                  GSourceFunc  callback,
    4003                 :            :                  gpointer     user_data)
    4004                 :            : {
    4005                 :          0 :   return TRUE;
    4006                 :            : }
    4007                 :            : 
    4008                 :            : static GSourceFuncs broken_funcs =
    4009                 :            : {
    4010                 :            :   NULL,
    4011                 :            :   NULL,
    4012                 :            :   broken_dispatch,
    4013                 :            :   NULL,
    4014                 :            :   NULL,
    4015                 :            :   NULL,
    4016                 :            : };
    4017                 :            : 
    4018                 :            : #ifdef G_OS_WIN32
    4019                 :            : static gint
    4020                 :            : network_events_for_condition (GIOCondition condition)
    4021                 :            : {
    4022                 :            :   int event_mask = 0;
    4023                 :            : 
    4024                 :            :   if (condition & G_IO_IN)
    4025                 :            :     event_mask |= (FD_READ | FD_ACCEPT);
    4026                 :            :   if (condition & G_IO_OUT)
    4027                 :            :     event_mask |= (FD_WRITE | FD_CONNECT);
    4028                 :            :   event_mask |= FD_CLOSE;
    4029                 :            : 
    4030                 :            :   return event_mask;
    4031                 :            : }
    4032                 :            : 
    4033                 :            : static void
    4034                 :            : ensure_event (GSocket *socket)
    4035                 :            : {
    4036                 :            :   if (socket->priv->event == WSA_INVALID_EVENT)
    4037                 :            :     socket->priv->event = WSACreateEvent();
    4038                 :            : }
    4039                 :            : 
    4040                 :            : static void
    4041                 :            : update_select_events (GSocket *socket)
    4042                 :            : {
    4043                 :            :   int event_mask;
    4044                 :            :   GIOCondition *ptr;
    4045                 :            :   GList *l;
    4046                 :            :   WSAEVENT event;
    4047                 :            : 
    4048                 :            :   if (socket->priv->closed)
    4049                 :            :     return;
    4050                 :            : 
    4051                 :            :   ensure_event (socket);
    4052                 :            : 
    4053                 :            :   event_mask = 0;
    4054                 :            :   for (l = socket->priv->requested_conditions; l != NULL; l = l->next)
    4055                 :            :     {
    4056                 :            :       ptr = l->data;
    4057                 :            :       event_mask |= network_events_for_condition (*ptr);
    4058                 :            :     }
    4059                 :            : 
    4060                 :            :   if (event_mask != socket->priv->selected_events)
    4061                 :            :     {
    4062                 :            :       /* If no events selected, disable event so we can unset
    4063                 :            :          nonblocking mode */
    4064                 :            : 
    4065                 :            :       if (event_mask == 0)
    4066                 :            :         event = NULL;
    4067                 :            :       else
    4068                 :            :         event = socket->priv->event;
    4069                 :            : 
    4070                 :            :       if (WSAEventSelect (socket->priv->fd, event, event_mask) == 0)
    4071                 :            :         socket->priv->selected_events = event_mask;
    4072                 :            :     }
    4073                 :            : }
    4074                 :            : 
    4075                 :            : static void
    4076                 :            : add_condition_watch (GSocket      *socket,
    4077                 :            :                      GIOCondition *condition)
    4078                 :            : {
    4079                 :            :   g_mutex_lock (&socket->priv->win32_source_lock);
    4080                 :            :   g_assert (g_list_find (socket->priv->requested_conditions, condition) == NULL);
    4081                 :            : 
    4082                 :            :   socket->priv->requested_conditions =
    4083                 :            :     g_list_prepend (socket->priv->requested_conditions, condition);
    4084                 :            : 
    4085                 :            :   update_select_events (socket);
    4086                 :            :   g_mutex_unlock (&socket->priv->win32_source_lock);
    4087                 :            : }
    4088                 :            : 
    4089                 :            : static void
    4090                 :            : remove_condition_watch (GSocket      *socket,
    4091                 :            :                         GIOCondition *condition)
    4092                 :            : {
    4093                 :            :   g_mutex_lock (&socket->priv->win32_source_lock);
    4094                 :            :   g_assert (g_list_find (socket->priv->requested_conditions, condition) != NULL);
    4095                 :            : 
    4096                 :            :   socket->priv->requested_conditions =
    4097                 :            :     g_list_remove (socket->priv->requested_conditions, condition);
    4098                 :            : 
    4099                 :            :   update_select_events (socket);
    4100                 :            :   g_mutex_unlock (&socket->priv->win32_source_lock);
    4101                 :            : }
    4102                 :            : 
    4103                 :            : static GIOCondition
    4104                 :            : update_condition_unlocked (GSocket *socket)
    4105                 :            : {
    4106                 :            :   WSANETWORKEVENTS events;
    4107                 :            :   GIOCondition condition;
    4108                 :            : 
    4109                 :            :   if (!socket->priv->closed &&
    4110                 :            :       WSAEnumNetworkEvents (socket->priv->fd,
    4111                 :            :                             socket->priv->event,
    4112                 :            :                             &events) == 0)
    4113                 :            :     {
    4114                 :            :       socket->priv->current_events |= events.lNetworkEvents;
    4115                 :            :       if (events.lNetworkEvents & FD_WRITE &&
    4116                 :            :           events.iErrorCode[FD_WRITE_BIT] != 0)
    4117                 :            :         socket->priv->current_errors |= FD_WRITE;
    4118                 :            :       if (events.lNetworkEvents & FD_CONNECT &&
    4119                 :            :           events.iErrorCode[FD_CONNECT_BIT] != 0)
    4120                 :            :         socket->priv->current_errors |= FD_CONNECT;
    4121                 :            :     }
    4122                 :            : 
    4123                 :            :   condition = 0;
    4124                 :            :   if (socket->priv->current_events & (FD_READ | FD_ACCEPT))
    4125                 :            :     condition |= G_IO_IN;
    4126                 :            : 
    4127                 :            :   if (socket->priv->current_events & FD_CLOSE)
    4128                 :            :     {
    4129                 :            :       int r, errsv = NO_ERROR, buffer;
    4130                 :            : 
    4131                 :            :       r = recv (socket->priv->fd, &buffer, sizeof (buffer), MSG_PEEK);
    4132                 :            :       if (r < 0)
    4133                 :            :           errsv = get_socket_errno ();
    4134                 :            : 
    4135                 :            :       if (r > 0 ||
    4136                 :            :           (r < 0 && errsv == WSAENOTCONN))
    4137                 :            :         condition |= G_IO_IN;
    4138                 :            :       else if (r == 0 ||
    4139                 :            :                (r < 0 && (errsv == WSAESHUTDOWN || errsv == WSAECONNRESET ||
    4140                 :            :                           errsv == WSAECONNABORTED || errsv == WSAENETRESET)))
    4141                 :            :         condition |= G_IO_HUP;
    4142                 :            :       else
    4143                 :            :         condition |= G_IO_ERR;
    4144                 :            :     }
    4145                 :            : 
    4146                 :            :   if (socket->priv->closed)
    4147                 :            :     condition |= G_IO_HUP;
    4148                 :            : 
    4149                 :            :   /* Never report both G_IO_OUT and HUP, these are
    4150                 :            :      mutually exclusive (can't write to a closed socket) */
    4151                 :            :   if ((condition & G_IO_HUP) == 0 &&
    4152                 :            :       socket->priv->current_events & FD_WRITE)
    4153                 :            :     {
    4154                 :            :       if (socket->priv->current_errors & FD_WRITE)
    4155                 :            :         condition |= G_IO_ERR;
    4156                 :            :       else
    4157                 :            :         condition |= G_IO_OUT;
    4158                 :            :     }
    4159                 :            :   else
    4160                 :            :     {
    4161                 :            :       if (socket->priv->current_events & FD_CONNECT)
    4162                 :            :         {
    4163                 :            :           if (socket->priv->current_errors & FD_CONNECT)
    4164                 :            :             condition |= (G_IO_HUP | G_IO_ERR);
    4165                 :            :           else
    4166                 :            :             condition |= G_IO_OUT;
    4167                 :            :         }
    4168                 :            :     }
    4169                 :            : 
    4170                 :            :   return condition;
    4171                 :            : }
    4172                 :            : 
    4173                 :            : static GIOCondition
    4174                 :            : update_condition (GSocket *socket)
    4175                 :            : {
    4176                 :            :   GIOCondition res;
    4177                 :            :   g_mutex_lock (&socket->priv->win32_source_lock);
    4178                 :            :   res = update_condition_unlocked (socket);
    4179                 :            :   g_mutex_unlock (&socket->priv->win32_source_lock);
    4180                 :            :   return res;
    4181                 :            : }
    4182                 :            : #endif
    4183                 :            : 
    4184                 :            : typedef struct {
    4185                 :            :   GSource       source;
    4186                 :            : #ifdef G_OS_WIN32
    4187                 :            :   GPollFD       pollfd;
    4188                 :            : #else
    4189                 :            :   gpointer      fd_tag;
    4190                 :            : #endif
    4191                 :            :   GSocket      *socket;
    4192                 :            :   GIOCondition  condition;
    4193                 :            : } GSocketSource;
    4194                 :            : 
    4195                 :            : static gboolean
    4196                 :      30362 : socket_source_prepare (GSource *source,
    4197                 :            :                        gint    *timeout)
    4198                 :            : {
    4199                 :      30362 :   GSocketSource *socket_source = (GSocketSource *)source;
    4200                 :            : 
    4201                 :            : #ifdef G_OS_WIN32
    4202                 :            :   if ((socket_source->pollfd.revents & G_IO_NVAL) != 0)
    4203                 :            :     return TRUE;
    4204                 :            : 
    4205                 :            :   if (g_socket_is_closed (socket_source->socket))
    4206                 :            :     {
    4207                 :            :       g_source_remove_poll (source, &socket_source->pollfd);
    4208                 :            :       socket_source->pollfd.revents = G_IO_NVAL;
    4209                 :            :       return TRUE;
    4210                 :            :     }
    4211                 :            : 
    4212                 :            :   return (update_condition (socket_source->socket) & socket_source->condition) != 0;
    4213                 :            : #else
    4214   [ +  +  +  - ]:      30362 :   return g_socket_is_closed (socket_source->socket) && socket_source->fd_tag != NULL;
    4215                 :            : #endif
    4216                 :            : }
    4217                 :            : 
    4218                 :            : #ifdef G_OS_WIN32
    4219                 :            : static gboolean
    4220                 :            : socket_source_check_win32 (GSource *source)
    4221                 :            : {
    4222                 :            :   int timeout;
    4223                 :            : 
    4224                 :            :   return socket_source_prepare (source, &timeout);
    4225                 :            : }
    4226                 :            : #endif
    4227                 :            : 
    4228                 :            : static gboolean
    4229                 :       8134 : socket_source_dispatch (GSource     *source,
    4230                 :            :                         GSourceFunc  callback,
    4231                 :            :                         gpointer     user_data)
    4232                 :            : {
    4233                 :       8134 :   GSocketSourceFunc func = (GSocketSourceFunc)callback;
    4234                 :       8134 :   GSocketSource *socket_source = (GSocketSource *)source;
    4235                 :       8134 :   GSocket *socket = socket_source->socket;
    4236                 :            :   gint64 timeout;
    4237                 :            :   guint events;
    4238                 :            :   gboolean ret;
    4239                 :            : 
    4240                 :            : #ifdef G_OS_WIN32
    4241                 :            :   if ((socket_source->pollfd.revents & G_IO_NVAL) != 0)
    4242                 :            :     events = G_IO_NVAL;
    4243                 :            :   else
    4244                 :            :     events = update_condition (socket_source->socket);
    4245                 :            : #else
    4246         [ +  + ]:       8134 :   if (g_socket_is_closed (socket_source->socket))
    4247                 :            :     {
    4248         [ +  - ]:         19 :       if (socket_source->fd_tag)
    4249                 :         19 :         g_source_remove_unix_fd (source, socket_source->fd_tag);
    4250                 :         19 :       socket_source->fd_tag = NULL;
    4251                 :         19 :       events = G_IO_NVAL;
    4252                 :            :     }
    4253                 :            :   else
    4254                 :            :     {
    4255                 :       8115 :       events = g_source_query_unix_fd (source, socket_source->fd_tag);
    4256                 :            :     }
    4257                 :            : #endif
    4258                 :            : 
    4259                 :       8134 :   timeout = g_source_get_ready_time (source);
    4260   [ +  +  +  +  :       8135 :   if (timeout >= 0 && timeout <= g_source_get_time (source) &&
                   +  - ]
    4261                 :          1 :       !g_socket_is_closed (socket_source->socket))
    4262                 :            :     {
    4263                 :          1 :       socket->priv->timed_out = TRUE;
    4264                 :          1 :       events |= (G_IO_IN | G_IO_OUT);
    4265                 :            :     }
    4266                 :            : 
    4267                 :       8134 :   ret = (*func) (socket, events & socket_source->condition, user_data);
    4268                 :            : 
    4269   [ +  +  +  - ]:       8134 :   if (socket->priv->timeout && !g_socket_is_closed (socket_source->socket))
    4270                 :          7 :     g_source_set_ready_time (source, g_get_monotonic_time () + socket->priv->timeout * 1000000);
    4271                 :            :   else
    4272                 :       8127 :     g_source_set_ready_time (source, -1);
    4273                 :            : 
    4274                 :       8134 :   return ret;
    4275                 :            : }
    4276                 :            : 
    4277                 :            : static void
    4278                 :       8129 : socket_source_finalize (GSource *source)
    4279                 :            : {
    4280                 :       8129 :   GSocketSource *socket_source = (GSocketSource *)source;
    4281                 :            :   GSocket *socket;
    4282                 :            : 
    4283                 :       8129 :   socket = socket_source->socket;
    4284                 :            : 
    4285                 :            : #ifdef G_OS_WIN32
    4286                 :            :   remove_condition_watch (socket, &socket_source->condition);
    4287                 :            : #endif
    4288                 :            : 
    4289                 :       8129 :   g_object_unref (socket);
    4290                 :       8129 : }
    4291                 :            : 
    4292                 :            : static gboolean
    4293                 :        158 : socket_source_closure_callback (GSocket      *socket,
    4294                 :            :                                 GIOCondition  condition,
    4295                 :            :                                 gpointer      data)
    4296                 :            : {
    4297                 :        158 :   GClosure *closure = data;
    4298                 :            : 
    4299                 :        158 :   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
    4300                 :        158 :   GValue result_value = G_VALUE_INIT;
    4301                 :            :   gboolean result;
    4302                 :            : 
    4303                 :        158 :   g_value_init (&result_value, G_TYPE_BOOLEAN);
    4304                 :            : 
    4305                 :        158 :   g_value_init (&params[0], G_TYPE_SOCKET);
    4306                 :        158 :   g_value_set_object (&params[0], socket);
    4307                 :        158 :   g_value_init (&params[1], G_TYPE_IO_CONDITION);
    4308                 :        158 :   g_value_set_flags (&params[1], condition);
    4309                 :            : 
    4310                 :        158 :   g_closure_invoke (closure, &result_value, 2, params, NULL);
    4311                 :            : 
    4312                 :        158 :   result = g_value_get_boolean (&result_value);
    4313                 :        158 :   g_value_unset (&result_value);
    4314                 :        158 :   g_value_unset (&params[0]);
    4315                 :        158 :   g_value_unset (&params[1]);
    4316                 :            : 
    4317                 :        158 :   return result;
    4318                 :            : }
    4319                 :            : 
    4320                 :            : static GSourceFuncs socket_source_funcs =
    4321                 :            : {
    4322                 :            :   socket_source_prepare,
    4323                 :            : #ifdef G_OS_WIN32
    4324                 :            :   socket_source_check_win32,
    4325                 :            : #else
    4326                 :            :   NULL,
    4327                 :            : #endif
    4328                 :            :   socket_source_dispatch,
    4329                 :            :   socket_source_finalize,
    4330                 :            :   (GSourceFunc)socket_source_closure_callback,
    4331                 :            :   NULL,
    4332                 :            : };
    4333                 :            : 
    4334                 :            : static GSource *
    4335                 :       8215 : socket_source_new (GSocket      *socket,
    4336                 :            :                    GIOCondition  condition,
    4337                 :            :                    GCancellable *cancellable)
    4338                 :            : {
    4339                 :            :   GSource *source;
    4340                 :            :   GSocketSource *socket_source;
    4341                 :            : 
    4342                 :            : #ifdef G_OS_WIN32
    4343                 :            :   ensure_event (socket);
    4344                 :            : 
    4345                 :            :   if (socket->priv->event == WSA_INVALID_EVENT)
    4346                 :            :     {
    4347                 :            :       g_warning ("Failed to create WSAEvent");
    4348                 :            :       return g_source_new (&broken_funcs, sizeof (GSource));
    4349                 :            :     }
    4350                 :            : #endif
    4351                 :            : 
    4352         [ -  + ]:       8215 :   if (!check_socket (socket, NULL))
    4353                 :            :     {
    4354                 :          0 :       g_warning ("Socket check failed");
    4355                 :          0 :       return g_source_new (&broken_funcs, sizeof (GSource));
    4356                 :            :     }
    4357                 :            : 
    4358                 :       8215 :   condition |= G_IO_HUP | G_IO_ERR | G_IO_NVAL;
    4359                 :            : 
    4360                 :       8215 :   source = g_source_new (&socket_source_funcs, sizeof (GSocketSource));
    4361                 :       8215 :   g_source_set_static_name (source, "GSocket");
    4362                 :       8215 :   socket_source = (GSocketSource *)source;
    4363                 :            : 
    4364                 :       8215 :   socket_source->socket = g_object_ref (socket);
    4365                 :       8215 :   socket_source->condition = condition;
    4366                 :            : 
    4367         [ +  + ]:       8215 :   if (cancellable)
    4368                 :            :     {
    4369                 :            :       GSource *cancellable_source;
    4370                 :            : 
    4371                 :       8144 :       cancellable_source = g_cancellable_source_new (cancellable);
    4372                 :       8144 :       g_source_add_child_source (source, cancellable_source);
    4373                 :       8144 :       g_source_set_dummy_callback (cancellable_source);
    4374                 :       8144 :       g_source_unref (cancellable_source);
    4375                 :            :     }
    4376                 :            : 
    4377                 :            : #ifdef G_OS_WIN32
    4378                 :            :   add_condition_watch (socket, &socket_source->condition);
    4379                 :            :   socket_source->pollfd.fd = (gintptr) socket->priv->event;
    4380                 :            :   socket_source->pollfd.events = condition;
    4381                 :            :   socket_source->pollfd.revents = 0;
    4382                 :            :   g_source_add_poll (source, &socket_source->pollfd);
    4383                 :            : #else
    4384                 :       8215 :   socket_source->fd_tag = g_source_add_unix_fd (source, socket->priv->fd, condition);
    4385                 :            : #endif
    4386                 :            : 
    4387         [ +  + ]:       8215 :   if (socket->priv->timeout)
    4388                 :          7 :     g_source_set_ready_time (source, g_get_monotonic_time () + socket->priv->timeout * 1000000);
    4389                 :            :   else
    4390                 :       8208 :     g_source_set_ready_time (source, -1);
    4391                 :            : 
    4392                 :       8215 :   return source;
    4393                 :            : }
    4394                 :            : 
    4395                 :            : /**
    4396                 :            :  * g_socket_create_source: (skip)
    4397                 :            :  * @socket: a #GSocket
    4398                 :            :  * @condition: a #GIOCondition mask to monitor
    4399                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    4400                 :            :  *
    4401                 :            :  * Creates a #GSource that can be attached to a %GMainContext to monitor
    4402                 :            :  * for the availability of the specified @condition on the socket. The #GSource
    4403                 :            :  * keeps a reference to the @socket.
    4404                 :            :  *
    4405                 :            :  * The callback on the source is of the #GSocketSourceFunc type.
    4406                 :            :  *
    4407                 :            :  * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in @condition;
    4408                 :            :  * these conditions will always be reported output if they are true.
    4409                 :            :  *
    4410                 :            :  * @cancellable if not %NULL can be used to cancel the source, which will
    4411                 :            :  * cause the source to trigger, reporting the current condition (which
    4412                 :            :  * is likely 0 unless cancellation happened at the same time as a
    4413                 :            :  * condition change). You can check for this in the callback using
    4414                 :            :  * g_cancellable_is_cancelled().
    4415                 :            :  *
    4416                 :            :  * If @socket has a timeout set, and it is reached before @condition
    4417                 :            :  * occurs, the source will then trigger anyway, reporting %G_IO_IN or
    4418                 :            :  * %G_IO_OUT depending on @condition. However, @socket will have been
    4419                 :            :  * marked as having had a timeout, and so the next #GSocket I/O method
    4420                 :            :  * you call will then fail with a %G_IO_ERROR_TIMED_OUT.
    4421                 :            :  *
    4422                 :            :  * Returns: (transfer full): a newly allocated %GSource, free with g_source_unref().
    4423                 :            :  *
    4424                 :            :  * Since: 2.22
    4425                 :            :  */
    4426                 :            : GSource *
    4427                 :       8215 : g_socket_create_source (GSocket      *socket,
    4428                 :            :                         GIOCondition  condition,
    4429                 :            :                         GCancellable *cancellable)
    4430                 :            : {
    4431                 :       8215 :   g_return_val_if_fail (G_IS_SOCKET (socket) && (cancellable == NULL || G_IS_CANCELLABLE (cancellable)), NULL);
    4432                 :            : 
    4433                 :       8215 :   return socket_source_new (socket, condition, cancellable);
    4434                 :            : }
    4435                 :            : 
    4436                 :            : /**
    4437                 :            :  * g_socket_condition_check:
    4438                 :            :  * @socket: a #GSocket
    4439                 :            :  * @condition: a #GIOCondition mask to check
    4440                 :            :  *
    4441                 :            :  * Checks on the readiness of @socket to perform operations.
    4442                 :            :  * The operations specified in @condition are checked for and masked
    4443                 :            :  * against the currently-satisfied conditions on @socket. The result
    4444                 :            :  * is returned.
    4445                 :            :  *
    4446                 :            :  * Note that on Windows, it is possible for an operation to return
    4447                 :            :  * %G_IO_ERROR_WOULD_BLOCK even immediately after
    4448                 :            :  * g_socket_condition_check() has claimed that the socket is ready for
    4449                 :            :  * writing. Rather than calling g_socket_condition_check() and then
    4450                 :            :  * writing to the socket if it succeeds, it is generally better to
    4451                 :            :  * simply try writing to the socket right away, and try again later if
    4452                 :            :  * the initial attempt returns %G_IO_ERROR_WOULD_BLOCK.
    4453                 :            :  *
    4454                 :            :  * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in condition;
    4455                 :            :  * these conditions will always be set in the output if they are true.
    4456                 :            :  *
    4457                 :            :  * This call never blocks.
    4458                 :            :  *
    4459                 :            :  * Returns: the @GIOCondition mask of the current state
    4460                 :            :  *
    4461                 :            :  * Since: 2.22
    4462                 :            :  */
    4463                 :            : GIOCondition
    4464                 :      55747 : g_socket_condition_check (GSocket      *socket,
    4465                 :            :                           GIOCondition  condition)
    4466                 :            : {
    4467                 :      55747 :   g_return_val_if_fail (G_IS_SOCKET (socket), 0);
    4468                 :            : 
    4469         [ -  + ]:      55747 :   if (!check_socket (socket, NULL))
    4470                 :          0 :     return 0;
    4471                 :            : 
    4472                 :            : #ifdef G_OS_WIN32
    4473                 :            :   {
    4474                 :            :     GIOCondition current_condition;
    4475                 :            : 
    4476                 :            :     condition |= G_IO_ERR | G_IO_HUP;
    4477                 :            : 
    4478                 :            :     add_condition_watch (socket, &condition);
    4479                 :            :     current_condition = update_condition (socket);
    4480                 :            :     remove_condition_watch (socket, &condition);
    4481                 :            :     return condition & current_condition;
    4482                 :            :   }
    4483                 :            : #else
    4484                 :            :   {
    4485                 :            :     GPollFD poll_fd;
    4486                 :            :     gint result;
    4487                 :      55747 :     poll_fd.fd = socket->priv->fd;
    4488                 :      55747 :     poll_fd.events = condition;
    4489                 :      55747 :     poll_fd.revents = 0;
    4490                 :            : 
    4491                 :            :     do
    4492                 :      55747 :       result = g_poll (&poll_fd, 1, 0);
    4493   [ -  +  -  - ]:      55747 :     while (result == -1 && get_socket_errno () == EINTR);
    4494                 :            : 
    4495                 :      55747 :     return poll_fd.revents;
    4496                 :            :   }
    4497                 :            : #endif
    4498                 :            : }
    4499                 :            : 
    4500                 :            : /**
    4501                 :            :  * g_socket_condition_wait:
    4502                 :            :  * @socket: a #GSocket
    4503                 :            :  * @condition: a #GIOCondition mask to wait for
    4504                 :            :  * @cancellable: (nullable): a #GCancellable, or %NULL
    4505                 :            :  * @error: a #GError pointer, or %NULL
    4506                 :            :  *
    4507                 :            :  * Waits for @condition to become true on @socket. When the condition
    4508                 :            :  * is met, %TRUE is returned.
    4509                 :            :  *
    4510                 :            :  * If @cancellable is cancelled before the condition is met, or if the
    4511                 :            :  * socket has a timeout set and it is reached before the condition is
    4512                 :            :  * met, then %FALSE is returned and @error, if non-%NULL, is set to
    4513                 :            :  * the appropriate value (%G_IO_ERROR_CANCELLED or
    4514                 :            :  * %G_IO_ERROR_TIMED_OUT).
    4515                 :            :  *
    4516                 :            :  * See also g_socket_condition_timed_wait().
    4517                 :            :  *
    4518                 :            :  * Returns: %TRUE if the condition was met, %FALSE otherwise
    4519                 :            :  *
    4520                 :            :  * Since: 2.22
    4521                 :            :  */
    4522                 :            : gboolean
    4523                 :        100 : g_socket_condition_wait (GSocket       *socket,
    4524                 :            :                          GIOCondition   condition,
    4525                 :            :                          GCancellable  *cancellable,
    4526                 :            :                          GError       **error)
    4527                 :            : {
    4528                 :        100 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    4529                 :            : 
    4530                 :        100 :   return g_socket_condition_timed_wait (socket, condition, -1,
    4531                 :            :                                         cancellable, error);
    4532                 :            : }
    4533                 :            : 
    4534                 :            : /**
    4535                 :            :  * g_socket_condition_timed_wait:
    4536                 :            :  * @socket: a #GSocket
    4537                 :            :  * @condition: a #GIOCondition mask to wait for
    4538                 :            :  * @timeout_us: the maximum time (in microseconds) to wait, or -1
    4539                 :            :  * @cancellable: (nullable): a #GCancellable, or %NULL
    4540                 :            :  * @error: a #GError pointer, or %NULL
    4541                 :            :  *
    4542                 :            :  * Waits for up to @timeout_us microseconds for @condition to become true
    4543                 :            :  * on @socket. If the condition is met, %TRUE is returned.
    4544                 :            :  *
    4545                 :            :  * If @cancellable is cancelled before the condition is met, or if
    4546                 :            :  * @timeout_us (or the socket's #GSocket:timeout) is reached before the
    4547                 :            :  * condition is met, then %FALSE is returned and @error, if non-%NULL,
    4548                 :            :  * is set to the appropriate value (%G_IO_ERROR_CANCELLED or
    4549                 :            :  * %G_IO_ERROR_TIMED_OUT).
    4550                 :            :  *
    4551                 :            :  * If you don't want a timeout, use g_socket_condition_wait().
    4552                 :            :  * (Alternatively, you can pass -1 for @timeout_us.)
    4553                 :            :  *
    4554                 :            :  * Note that although @timeout_us is in microseconds for consistency with
    4555                 :            :  * other GLib APIs, this function actually only has millisecond
    4556                 :            :  * resolution, and the behavior is undefined if @timeout_us is not an
    4557                 :            :  * exact number of milliseconds.
    4558                 :            :  *
    4559                 :            :  * Returns: %TRUE if the condition was met, %FALSE otherwise
    4560                 :            :  *
    4561                 :            :  * Since: 2.32
    4562                 :            :  */
    4563                 :            : gboolean
    4564                 :       5083 : g_socket_condition_timed_wait (GSocket       *socket,
    4565                 :            :                                GIOCondition   condition,
    4566                 :            :                                gint64         timeout_us,
    4567                 :            :                                GCancellable  *cancellable,
    4568                 :            :                                GError       **error)
    4569                 :            : {
    4570                 :            :   gint64 start_time;
    4571                 :            :   gint64 timeout_ms;
    4572                 :            : 
    4573                 :       5083 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    4574                 :            : 
    4575         [ -  + ]:       5083 :   if (!check_socket (socket, error))
    4576                 :          0 :     return FALSE;
    4577                 :            : 
    4578         [ +  + ]:       5083 :   if (g_cancellable_set_error_if_cancelled (cancellable, error))
    4579                 :         24 :     return FALSE;
    4580                 :            : 
    4581   [ +  +  +  + ]:       5059 :   if (socket->priv->timeout &&
    4582         [ -  + ]:          2 :       (timeout_us < 0 || socket->priv->timeout < timeout_us / G_USEC_PER_SEC))
    4583                 :         27 :     timeout_ms = (gint64) socket->priv->timeout * 1000;
    4584         [ +  + ]:       5032 :   else if (timeout_us != -1)
    4585                 :          2 :     timeout_ms = timeout_us / 1000;
    4586                 :            :   else
    4587                 :       5030 :     timeout_ms = -1;
    4588                 :            : 
    4589                 :       5059 :   start_time = g_get_monotonic_time ();
    4590                 :            : 
    4591                 :            : #ifdef G_OS_WIN32
    4592                 :            :   {
    4593                 :            :     GIOCondition current_condition;
    4594                 :            :     WSAEVENT events[2];
    4595                 :            :     DWORD res;
    4596                 :            :     GPollFD cancel_fd;
    4597                 :            :     int num_events;
    4598                 :            : 
    4599                 :            :     /* Always check these */
    4600                 :            :     condition |=  G_IO_ERR | G_IO_HUP;
    4601                 :            : 
    4602                 :            :     add_condition_watch (socket, &condition);
    4603                 :            : 
    4604                 :            :     num_events = 0;
    4605                 :            :     events[num_events++] = socket->priv->event;
    4606                 :            : 
    4607                 :            :     if (g_cancellable_make_pollfd (cancellable, &cancel_fd))
    4608                 :            :       events[num_events++] = (WSAEVENT)cancel_fd.fd;
    4609                 :            : 
    4610                 :            :     if (timeout_ms == -1)
    4611                 :            :       timeout_ms = WSA_INFINITE;
    4612                 :            : 
    4613                 :            :     g_mutex_lock (&socket->priv->win32_source_lock);
    4614                 :            :     current_condition = update_condition_unlocked (socket);
    4615                 :            :     while ((condition & current_condition) == 0)
    4616                 :            :       {
    4617                 :            :         if (!socket->priv->waiting)
    4618                 :            :           {
    4619                 :            :             socket->priv->waiting = TRUE;
    4620                 :            :             socket->priv->waiting_result = 0;
    4621                 :            :             g_mutex_unlock (&socket->priv->win32_source_lock);
    4622                 :            : 
    4623                 :            :             res = WSAWaitForMultipleEvents (num_events, events, FALSE, timeout_ms, FALSE);
    4624                 :            : 
    4625                 :            :             g_mutex_lock (&socket->priv->win32_source_lock);
    4626                 :            :             socket->priv->waiting = FALSE;
    4627                 :            :             socket->priv->waiting_result = res;
    4628                 :            :             g_cond_broadcast (&socket->priv->win32_source_cond);
    4629                 :            :           }
    4630                 :            :         else
    4631                 :            :           {
    4632                 :            :             if (timeout_ms != WSA_INFINITE)
    4633                 :            :               {
    4634                 :            :                 if (!g_cond_wait_until (&socket->priv->win32_source_cond, &socket->priv->win32_source_lock, timeout_ms))
    4635                 :            :                   {
    4636                 :            :                     res = WSA_WAIT_TIMEOUT;
    4637                 :            :                     break;
    4638                 :            :                   }
    4639                 :            :                 else
    4640                 :            :                   {
    4641                 :            :                     res = socket->priv->waiting_result;
    4642                 :            :                   }
    4643                 :            :               }
    4644                 :            :             else
    4645                 :            :               {
    4646                 :            :                 g_cond_wait (&socket->priv->win32_source_cond, &socket->priv->win32_source_lock);
    4647                 :            :                 res = socket->priv->waiting_result;
    4648                 :            :               }
    4649                 :            :           }
    4650                 :            : 
    4651                 :            :         if (res == WSA_WAIT_FAILED)
    4652                 :            :           {
    4653                 :            :             int errsv = get_socket_errno ();
    4654                 :            : 
    4655                 :            :             g_set_error (error, G_IO_ERROR,
    4656                 :            :                          socket_io_error_from_errno (errsv),
    4657                 :            :                          _("Waiting for socket condition: %s"),
    4658                 :            :                          socket_strerror (errsv));
    4659                 :            :             break;
    4660                 :            :           }
    4661                 :            :         else if (res == WSA_WAIT_TIMEOUT)
    4662                 :            :           {
    4663                 :            :             g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
    4664                 :            :                                  _("Socket I/O timed out"));
    4665                 :            :             break;
    4666                 :            :           }
    4667                 :            : 
    4668                 :            :         if (g_cancellable_set_error_if_cancelled (cancellable, error))
    4669                 :            :           break;
    4670                 :            : 
    4671                 :            :         current_condition = update_condition_unlocked (socket);
    4672                 :            : 
    4673                 :            :         if (timeout_ms != WSA_INFINITE)
    4674                 :            :           {
    4675                 :            :             timeout_ms -= (g_get_monotonic_time () - start_time) * 1000;
    4676                 :            :             if (timeout_ms < 0)
    4677                 :            :               timeout_ms = 0;
    4678                 :            :           }
    4679                 :            :       }
    4680                 :            :     g_mutex_unlock (&socket->priv->win32_source_lock);
    4681                 :            :     remove_condition_watch (socket, &condition);
    4682                 :            :     if (num_events > 1)
    4683                 :            :       g_cancellable_release_fd (cancellable);
    4684                 :            : 
    4685                 :            :     return (condition & current_condition) != 0;
    4686                 :            :   }
    4687                 :            : #else
    4688                 :            :   {
    4689                 :            :     GPollFD poll_fd[2];
    4690                 :            :     gint result;
    4691                 :            :     gint num;
    4692                 :            : 
    4693                 :       5059 :     poll_fd[0].fd = socket->priv->fd;
    4694                 :       5059 :     poll_fd[0].events = condition;
    4695                 :       5059 :     num = 1;
    4696                 :            : 
    4697         [ +  + ]:       5059 :     if (g_cancellable_make_pollfd (cancellable, &poll_fd[1]))
    4698                 :         49 :       num++;
    4699                 :            : 
    4700                 :            :     while (TRUE)
    4701                 :          0 :       {
    4702                 :            :         int errsv;
    4703                 :       5059 :         result = g_poll (poll_fd, num, timeout_ms);
    4704                 :       5059 :         errsv = errno;
    4705   [ -  +  -  - ]:       5059 :         if (result != -1 || errsv != EINTR)
    4706                 :            :           break;
    4707                 :            : 
    4708         [ #  # ]:          0 :         if (timeout_ms != -1)
    4709                 :            :           {
    4710                 :          0 :             timeout_ms -= (g_get_monotonic_time () - start_time) / 1000;
    4711         [ #  # ]:          0 :             if (timeout_ms < 0)
    4712                 :          0 :               timeout_ms = 0;
    4713                 :            :           }
    4714                 :            :       }
    4715                 :            :     
    4716         [ +  + ]:       5059 :     if (num > 1)
    4717                 :         49 :       g_cancellable_release_fd (cancellable);
    4718                 :            : 
    4719         [ +  + ]:       5059 :     if (result == 0)
    4720                 :            :       {
    4721                 :          6 :         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
    4722                 :            :                              _("Socket I/O timed out"));
    4723                 :          6 :         return FALSE;
    4724                 :            :       }
    4725                 :            : 
    4726                 :       5053 :     return !g_cancellable_set_error_if_cancelled (cancellable, error);
    4727                 :            :   }
    4728                 :            :   #endif
    4729                 :            : }
    4730                 :            : 
    4731                 :            : #ifndef G_OS_WIN32
    4732                 :            : 
    4733                 :            : #ifdef HAVE_QNX
    4734                 :            : /* QNX has this weird upper limit, or at least used to back in the 6.x days.
    4735                 :            :  * This was discovered empirically and doesn't appear to be mentioned in any
    4736                 :            :  * of the official documentation. */
    4737                 :            : # define G_SOCKET_CONTROL_BUFFER_SIZE_BYTES 2016
    4738                 :            : #else
    4739                 :            : # define G_SOCKET_CONTROL_BUFFER_SIZE_BYTES 2048
    4740                 :            : #endif
    4741                 :            : 
    4742                 :            : /* Unfortunately these have to be macros rather than inline functions due to
    4743                 :            :  * using alloca(). */
    4744                 :            : #define output_message_to_msghdr(message, prev_message, msg, prev_msg, error) \
    4745                 :            : G_STMT_START { \
    4746                 :            :   const GOutputMessage  *_message = (message); \
    4747                 :            :   const GOutputMessage *_prev_message = (prev_message); \
    4748                 :            :   struct msghdr *_msg = (msg); \
    4749                 :            :   const struct msghdr *_prev_msg = (prev_msg); \
    4750                 :            :   GError **_error = (error); \
    4751                 :            :  \
    4752                 :            :   _msg->msg_flags = 0; \
    4753                 :            :  \
    4754                 :            :   /* name */ \
    4755                 :            :   if (_prev_message != NULL && _prev_message->address == _message->address) \
    4756                 :            :     { \
    4757                 :            :       _msg->msg_name = _prev_msg->msg_name; \
    4758                 :            :       _msg->msg_namelen = _prev_msg->msg_namelen; \
    4759                 :            :     } \
    4760                 :            :   else if (_message->address != NULL) \
    4761                 :            :     { \
    4762                 :            :       _msg->msg_namelen = g_socket_address_get_native_size (_message->address); \
    4763                 :            :       _msg->msg_name = g_alloca (_msg->msg_namelen); \
    4764                 :            :       if (!g_socket_address_to_native (_message->address, _msg->msg_name, \
    4765                 :            :                                        _msg->msg_namelen, _error)) \
    4766                 :            :         break; \
    4767                 :            :     } \
    4768                 :            :   else \
    4769                 :            :     { \
    4770                 :            :       _msg->msg_name = NULL; \
    4771                 :            :       _msg->msg_namelen = 0; \
    4772                 :            :     } \
    4773                 :            :  \
    4774                 :            :   /* iov */ \
    4775                 :            :   { \
    4776                 :            :     /* this entire expression will be evaluated at compile time */ \
    4777                 :            :     if (sizeof *_msg->msg_iov == sizeof *_message->vectors && \
    4778                 :            :         sizeof _msg->msg_iov->iov_base == sizeof _message->vectors->buffer && \
    4779                 :            :         G_STRUCT_OFFSET (struct iovec, iov_base) == \
    4780                 :            :         G_STRUCT_OFFSET (GOutputVector, buffer) && \
    4781                 :            :         sizeof _msg->msg_iov->iov_len == sizeof _message->vectors->size && \
    4782                 :            :         G_STRUCT_OFFSET (struct iovec, iov_len) == \
    4783                 :            :         G_STRUCT_OFFSET (GOutputVector, size)) \
    4784                 :            :       /* ABI is compatible */ \
    4785                 :            :       { \
    4786                 :            :         _msg->msg_iov = (struct iovec *) _message->vectors; \
    4787                 :            :         _msg->msg_iovlen = _message->num_vectors; \
    4788                 :            :       } \
    4789                 :            :     else \
    4790                 :            :       /* ABI is incompatible */ \
    4791                 :            :       { \
    4792                 :            :         guint i; \
    4793                 :            :  \
    4794                 :            :         _msg->msg_iov = g_newa (struct iovec, _message->num_vectors); \
    4795                 :            :         for (i = 0; i < _message->num_vectors; i++) \
    4796                 :            :           { \
    4797                 :            :             _msg->msg_iov[i].iov_base = (void *) _message->vectors[i].buffer; \
    4798                 :            :             _msg->msg_iov[i].iov_len = _message->vectors[i].size; \
    4799                 :            :           } \
    4800                 :            :         _msg->msg_iovlen = _message->num_vectors; \
    4801                 :            :       } \
    4802                 :            :   } \
    4803                 :            :  \
    4804                 :            :   /* control */ \
    4805                 :            :   { \
    4806                 :            :     struct cmsghdr *cmsg; \
    4807                 :            :     guint i; \
    4808                 :            :  \
    4809                 :            :     _msg->msg_controllen = 0; \
    4810                 :            :     for (i = 0; i < _message->num_control_messages; i++) \
    4811                 :            :       _msg->msg_controllen += CMSG_SPACE (g_socket_control_message_get_size (_message->control_messages[i])); \
    4812                 :            :  \
    4813                 :            :     if (_msg->msg_controllen == 0) \
    4814                 :            :       _msg->msg_control = NULL; \
    4815                 :            :     else \
    4816                 :            :       { \
    4817                 :            :         _msg->msg_control = g_alloca0 (_msg->msg_controllen); \
    4818                 :            :       } \
    4819                 :            :  \
    4820                 :            :     cmsg = CMSG_FIRSTHDR (_msg); \
    4821                 :            :     for (i = 0; i < _message->num_control_messages; i++) \
    4822                 :            :       { \
    4823                 :            :         cmsg->cmsg_level = g_socket_control_message_get_level (_message->control_messages[i]); \
    4824                 :            :         cmsg->cmsg_type = g_socket_control_message_get_msg_type (_message->control_messages[i]); \
    4825                 :            :         cmsg->cmsg_len = CMSG_LEN (g_socket_control_message_get_size (_message->control_messages[i])); \
    4826                 :            :         g_socket_control_message_serialize (_message->control_messages[i], \
    4827                 :            :                                             CMSG_DATA (cmsg)); \
    4828                 :            :         cmsg = CMSG_NXTHDR (_msg, cmsg); \
    4829                 :            :       } \
    4830                 :            :     g_assert (cmsg == NULL); \
    4831                 :            :   } \
    4832                 :            : } G_STMT_END
    4833                 :            : 
    4834                 :            : #define input_message_to_msghdr(message, msg) \
    4835                 :            : G_STMT_START { \
    4836                 :            :   const GInputMessage  *_message = (message); \
    4837                 :            :   struct msghdr *_msg = (msg); \
    4838                 :            :  \
    4839                 :            :   /* name */ \
    4840                 :            :   if (_message->address) \
    4841                 :            :     { \
    4842                 :            :       _msg->msg_namelen = sizeof (struct sockaddr_storage); \
    4843                 :            :       _msg->msg_name = g_alloca (_msg->msg_namelen); \
    4844                 :            :     } \
    4845                 :            :   else \
    4846                 :            :     { \
    4847                 :            :       _msg->msg_name = NULL; \
    4848                 :            :       _msg->msg_namelen = 0; \
    4849                 :            :     } \
    4850                 :            :  \
    4851                 :            :   /* iov */ \
    4852                 :            :   /* this entire expression will be evaluated at compile time */ \
    4853                 :            :   if (sizeof *_msg->msg_iov == sizeof *_message->vectors && \
    4854                 :            :       sizeof _msg->msg_iov->iov_base == sizeof _message->vectors->buffer && \
    4855                 :            :       G_STRUCT_OFFSET (struct iovec, iov_base) == \
    4856                 :            :       G_STRUCT_OFFSET (GInputVector, buffer) && \
    4857                 :            :       sizeof _msg->msg_iov->iov_len == sizeof _message->vectors->size && \
    4858                 :            :       G_STRUCT_OFFSET (struct iovec, iov_len) == \
    4859                 :            :       G_STRUCT_OFFSET (GInputVector, size)) \
    4860                 :            :     /* ABI is compatible */ \
    4861                 :            :     { \
    4862                 :            :       _msg->msg_iov = (struct iovec *) _message->vectors; \
    4863                 :            :       _msg->msg_iovlen = _message->num_vectors; \
    4864                 :            :     } \
    4865                 :            :   else \
    4866                 :            :     /* ABI is incompatible */ \
    4867                 :            :     { \
    4868                 :            :       guint i; \
    4869                 :            :  \
    4870                 :            :       _msg->msg_iov = g_newa (struct iovec, _message->num_vectors); \
    4871                 :            :       for (i = 0; i < _message->num_vectors; i++) \
    4872                 :            :         { \
    4873                 :            :           _msg->msg_iov[i].iov_base = _message->vectors[i].buffer; \
    4874                 :            :           _msg->msg_iov[i].iov_len = _message->vectors[i].size; \
    4875                 :            :         } \
    4876                 :            :       _msg->msg_iovlen = _message->num_vectors; \
    4877                 :            :     } \
    4878                 :            :  \
    4879                 :            :   /* control */ \
    4880                 :            :   if (_message->control_messages == NULL) \
    4881                 :            :     { \
    4882                 :            :           _msg->msg_controllen = 0; \
    4883                 :            :           _msg->msg_control = NULL; \
    4884                 :            :     } \
    4885                 :            :   else \
    4886                 :            :     { \
    4887                 :            :       _msg->msg_controllen = G_SOCKET_CONTROL_BUFFER_SIZE_BYTES; \
    4888                 :            :       _msg->msg_control = g_alloca (_msg->msg_controllen); \
    4889                 :            :     } \
    4890                 :            :  \
    4891                 :            :   /* flags */ \
    4892                 :            :   _msg->msg_flags = _message->flags; \
    4893                 :            : } G_STMT_END
    4894                 :            : 
    4895                 :            : static void
    4896                 :      54773 : input_message_from_msghdr (const struct msghdr  *msg,
    4897                 :            :                            GInputMessage        *message,
    4898                 :            :                            GSocket              *socket)
    4899                 :            : {
    4900                 :            :   /* decode address */
    4901         [ +  + ]:      54773 :   if (message->address != NULL)
    4902                 :            :     {
    4903                 :        147 :       *message->address = cache_recv_address (socket, msg->msg_name,
    4904                 :        147 :                                               msg->msg_namelen);
    4905                 :            :     }
    4906                 :            : 
    4907                 :            :   /* decode control messages */
    4908                 :            :   {
    4909                 :      54773 :     GPtrArray *my_messages = NULL;
    4910                 :            :     struct cmsghdr *cmsg;
    4911                 :            : 
    4912         [ +  + ]:      54773 :     if (msg->msg_controllen >= (socklen_t) sizeof (struct cmsghdr))
    4913                 :            :       {
    4914                 :         16 :         g_assert (message->control_messages != NULL);
    4915         [ +  - ]:         16 :         for (cmsg = CMSG_FIRSTHDR (msg);
    4916         [ +  + ]:         32 :              cmsg != NULL;
    4917                 :         16 :              cmsg = CMSG_NXTHDR ((struct msghdr *) msg, cmsg))
    4918                 :            :           {
    4919                 :            :             GSocketControlMessage *control_message;
    4920                 :            : 
    4921                 :         16 :             control_message = g_socket_control_message_deserialize (cmsg->cmsg_level,
    4922                 :            :                                                                     cmsg->cmsg_type,
    4923                 :         16 :                                                                     cmsg->cmsg_len - ((char *)CMSG_DATA (cmsg) - (char *)cmsg),
    4924                 :         16 :                                                                     CMSG_DATA (cmsg));
    4925         [ -  + ]:         16 :             if (control_message == NULL)
    4926                 :            :               /* We've already spewed about the problem in the
    4927                 :            :                  deserialization code, so just continue */
    4928                 :          0 :               continue;
    4929                 :            : 
    4930         [ +  - ]:         16 :             if (my_messages == NULL)
    4931                 :         16 :               my_messages = g_ptr_array_new ();
    4932                 :         16 :             g_ptr_array_add (my_messages, control_message);
    4933                 :            :            }
    4934                 :            :       }
    4935                 :            : 
    4936         [ +  + ]:      54773 :     if (message->num_control_messages)
    4937         [ +  + ]:      54481 :       *message->num_control_messages = my_messages != NULL ? my_messages->len : 0;
    4938                 :            : 
    4939         [ +  + ]:      54773 :     if (message->control_messages)
    4940                 :            :       {
    4941         [ +  + ]:      54481 :         if (my_messages == NULL)
    4942                 :            :           {
    4943                 :      54465 :             *message->control_messages = NULL;
    4944                 :            :           }
    4945                 :            :         else
    4946                 :            :           {
    4947                 :         16 :             g_ptr_array_add (my_messages, NULL);
    4948                 :         16 :             *message->control_messages = (GSocketControlMessage **) g_ptr_array_free (my_messages, FALSE);
    4949                 :            :           }
    4950                 :            :       }
    4951                 :            :     else
    4952                 :            :       {
    4953                 :        292 :         g_assert (my_messages == NULL);
    4954                 :            :       }
    4955                 :            :   }
    4956                 :            : 
    4957                 :            :   /* capture the flags */
    4958                 :      54773 :   message->flags = msg->msg_flags;
    4959                 :      54773 : }
    4960                 :            : #endif
    4961                 :            : 
    4962                 :            : /**
    4963                 :            :  * g_socket_send_message:
    4964                 :            :  * @socket: a #GSocket
    4965                 :            :  * @address: (nullable): a #GSocketAddress, or %NULL
    4966                 :            :  * @vectors: (array length=num_vectors): an array of #GOutputVector structs
    4967                 :            :  * @num_vectors: the number of elements in @vectors, or -1
    4968                 :            :  * @messages: (array length=num_messages) (nullable): a pointer to an
    4969                 :            :  *   array of #GSocketControlMessages, or %NULL.
    4970                 :            :  * @num_messages: number of elements in @messages, or -1.
    4971                 :            :  * @flags: an int containing #GSocketMsgFlags flags, which may additionally
    4972                 :            :  *    contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html)
    4973                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    4974                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    4975                 :            :  *
    4976                 :            :  * Send data to @address on @socket.  For sending multiple messages see
    4977                 :            :  * g_socket_send_messages(); for easier use, see
    4978                 :            :  * g_socket_send() and g_socket_send_to().
    4979                 :            :  *
    4980                 :            :  * If @address is %NULL then the message is sent to the default receiver
    4981                 :            :  * (set by g_socket_connect()).
    4982                 :            :  *
    4983                 :            :  * @vectors must point to an array of #GOutputVector structs and
    4984                 :            :  * @num_vectors must be the length of this array. (If @num_vectors is -1,
    4985                 :            :  * then @vectors is assumed to be terminated by a #GOutputVector with a
    4986                 :            :  * %NULL buffer pointer.) The #GOutputVector structs describe the buffers
    4987                 :            :  * that the sent data will be gathered from. Using multiple
    4988                 :            :  * #GOutputVectors is more memory-efficient than manually copying
    4989                 :            :  * data from multiple sources into a single buffer, and more
    4990                 :            :  * network-efficient than making multiple calls to g_socket_send().
    4991                 :            :  *
    4992                 :            :  * @messages, if non-%NULL, is taken to point to an array of @num_messages
    4993                 :            :  * #GSocketControlMessage instances. These correspond to the control
    4994                 :            :  * messages to be sent on the socket.
    4995                 :            :  * If @num_messages is -1 then @messages is treated as a %NULL-terminated
    4996                 :            :  * array.
    4997                 :            :  *
    4998                 :            :  * @flags modify how the message is sent. The commonly available arguments
    4999                 :            :  * for this are available in the #GSocketMsgFlags enum, but the
    5000                 :            :  * values there are the same as the system values, and the flags
    5001                 :            :  * are passed in as-is, so you can pass in system-specific flags too.
    5002                 :            :  *
    5003                 :            :  * If the socket is in blocking mode the call will block until there is
    5004                 :            :  * space for the data in the socket queue. If there is no space available
    5005                 :            :  * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error
    5006                 :            :  * will be returned. To be notified when space is available, wait for the
    5007                 :            :  * %G_IO_OUT condition. Note though that you may still receive
    5008                 :            :  * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously
    5009                 :            :  * notified of a %G_IO_OUT condition. (On Windows in particular, this is
    5010                 :            :  * very common due to the way the underlying APIs work.)
    5011                 :            :  *
    5012                 :            :  * The sum of the sizes of each #GOutputVector in vectors must not be
    5013                 :            :  * greater than %G_MAXSSIZE. If the message can be larger than this,
    5014                 :            :  * then it is mandatory to use the g_socket_send_message_with_timeout()
    5015                 :            :  * function.
    5016                 :            :  *
    5017                 :            :  * On error -1 is returned and @error is set accordingly.
    5018                 :            :  *
    5019                 :            :  * Returns: Number of bytes written (which may be less than @size), or -1
    5020                 :            :  * on error
    5021                 :            :  *
    5022                 :            :  * Since: 2.22
    5023                 :            :  */
    5024                 :            : gssize
    5025                 :      14862 : g_socket_send_message (GSocket                *socket,
    5026                 :            :                        GSocketAddress         *address,
    5027                 :            :                        GOutputVector          *vectors,
    5028                 :            :                        gint                    num_vectors,
    5029                 :            :                        GSocketControlMessage **messages,
    5030                 :            :                        gint                    num_messages,
    5031                 :            :                        gint                    flags,
    5032                 :            :                        GCancellable           *cancellable,
    5033                 :            :                        GError                **error)
    5034                 :            : {
    5035                 :            :   GPollableReturn res;
    5036                 :      14862 :   gsize bytes_written = 0;
    5037                 :      14862 :   gsize vectors_size = 0;
    5038                 :            : 
    5039         [ +  - ]:      14862 :   if (num_vectors != -1)
    5040                 :            :     {
    5041         [ +  + ]:      29747 :       for (gint i = 0; i < num_vectors; i++)
    5042                 :            :         {
    5043                 :            :           /* No wrap-around for vectors_size */
    5044         [ -  + ]:      14885 :           if (vectors_size > vectors_size + vectors[i].size)
    5045                 :            :             {
    5046                 :          0 :               g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
    5047                 :            :                            _("Unable to send message: %s"),
    5048                 :            :                            _("Message vectors too large"));
    5049                 :          0 :               return -1;
    5050                 :            :             }
    5051                 :            : 
    5052                 :      14885 :           vectors_size += vectors[i].size;
    5053                 :            :         }
    5054                 :            :     }
    5055                 :            :   else
    5056                 :            :     {
    5057         [ #  # ]:          0 :       for (gsize i = 0; vectors[i].buffer != NULL; i++)
    5058                 :            :         {
    5059                 :            :           /* No wrap-around for vectors_size */
    5060         [ #  # ]:          0 :           if (vectors_size > vectors_size + vectors[i].size)
    5061                 :            :             {
    5062                 :          0 :               g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
    5063                 :            :                            _("Unable to send message: %s"),
    5064                 :            :                            _("Message vectors too large"));
    5065                 :          0 :               return -1;
    5066                 :            :             }
    5067                 :            : 
    5068                 :          0 :           vectors_size += vectors[i].size;
    5069                 :            :         }
    5070                 :            :     }
    5071                 :            : 
    5072                 :            :   /* Check if vector's buffers are too big for gssize */
    5073         [ -  + ]:      14862 :   if (vectors_size > G_MAXSSIZE)
    5074                 :            :     {
    5075                 :          0 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
    5076                 :            :                    _("Unable to send message: %s"),
    5077                 :            :                    _("Message vectors too large"));
    5078                 :          0 :       return -1;
    5079                 :            :     }
    5080                 :            : 
    5081                 :      14862 :   res = g_socket_send_message_with_timeout (socket, address,
    5082                 :            :                                             vectors, num_vectors,
    5083                 :            :                                             messages, num_messages, flags,
    5084         [ +  + ]:      14862 :                                             socket->priv->blocking ? -1 : 0,
    5085                 :            :                                             &bytes_written,
    5086                 :            :                                             cancellable, error);
    5087                 :            : 
    5088                 :      14862 :   g_assert (res != G_POLLABLE_RETURN_OK || bytes_written <= G_MAXSSIZE);
    5089                 :            : 
    5090         [ -  + ]:      14862 :   if (res == G_POLLABLE_RETURN_WOULD_BLOCK)
    5091                 :            :     {
    5092                 :            : #ifndef G_OS_WIN32
    5093   [ #  #  #  # ]:          0 :       socket_set_error_lazy (error, EWOULDBLOCK, _("Error sending message: %s"));
    5094                 :            : #else
    5095                 :            :       socket_set_error_lazy (error, WSAEWOULDBLOCK, _("Error sending message: %s"));
    5096                 :            : #endif
    5097                 :            :     }
    5098                 :            : 
    5099         [ +  + ]:      14862 :   return res == G_POLLABLE_RETURN_OK ? (gssize) bytes_written : -1;
    5100                 :            : }
    5101                 :            : 
    5102                 :            : /**
    5103                 :            :  * g_socket_send_message_with_timeout:
    5104                 :            :  * @socket: a #GSocket
    5105                 :            :  * @address: (nullable): a #GSocketAddress, or %NULL
    5106                 :            :  * @vectors: (array length=num_vectors): an array of #GOutputVector structs
    5107                 :            :  * @num_vectors: the number of elements in @vectors, or -1
    5108                 :            :  * @messages: (array length=num_messages) (nullable): a pointer to an
    5109                 :            :  *   array of #GSocketControlMessages, or %NULL.
    5110                 :            :  * @num_messages: number of elements in @messages, or -1.
    5111                 :            :  * @flags: an int containing #GSocketMsgFlags flags, which may additionally
    5112                 :            :  *    contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html)
    5113                 :            :  * @timeout_us: the maximum time (in microseconds) to wait, or -1
    5114                 :            :  * @bytes_written: (out) (optional): location to store the number of bytes that were written to the socket
    5115                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    5116                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    5117                 :            :  *
    5118                 :            :  * This behaves exactly the same as g_socket_send_message(), except that
    5119                 :            :  * the choice of timeout behavior is determined by the @timeout_us argument
    5120                 :            :  * rather than by @socket's properties.
    5121                 :            :  *
    5122                 :            :  * On error %G_POLLABLE_RETURN_FAILED is returned and @error is set accordingly, or
    5123                 :            :  * if the socket is currently not writable %G_POLLABLE_RETURN_WOULD_BLOCK is
    5124                 :            :  * returned. @bytes_written will contain 0 in both cases.
    5125                 :            :  *
    5126                 :            :  * Returns: %G_POLLABLE_RETURN_OK if all data was successfully written,
    5127                 :            :  * %G_POLLABLE_RETURN_WOULD_BLOCK if the socket is currently not writable, or
    5128                 :            :  * %G_POLLABLE_RETURN_FAILED if an error happened and @error is set.
    5129                 :            :  *
    5130                 :            :  * Since: 2.60
    5131                 :            :  */
    5132                 :            : GPollableReturn
    5133                 :      14864 : g_socket_send_message_with_timeout (GSocket                *socket,
    5134                 :            :                                     GSocketAddress         *address,
    5135                 :            :                                     const GOutputVector    *vectors,
    5136                 :            :                                     gint                    num_vectors,
    5137                 :            :                                     GSocketControlMessage **messages,
    5138                 :            :                                     gint                    num_messages,
    5139                 :            :                                     gint                    flags,
    5140                 :            :                                     gint64                  timeout_us,
    5141                 :            :                                     gsize                  *bytes_written,
    5142                 :            :                                     GCancellable           *cancellable,
    5143                 :            :                                     GError                **error)
    5144                 :            : {
    5145                 :            :   GOutputVector one_vector;
    5146                 :            :   char zero;
    5147                 :            :   gint64 start_time;
    5148                 :            : 
    5149         [ +  - ]:      14864 :   if (bytes_written)
    5150                 :      14864 :     *bytes_written = 0;
    5151                 :            : 
    5152                 :      14864 :   g_return_val_if_fail (G_IS_SOCKET (socket), G_POLLABLE_RETURN_FAILED);
    5153                 :      14864 :   g_return_val_if_fail (address == NULL || G_IS_SOCKET_ADDRESS (address), G_POLLABLE_RETURN_FAILED);
    5154                 :      14864 :   g_return_val_if_fail (num_vectors == 0 || vectors != NULL, G_POLLABLE_RETURN_FAILED);
    5155                 :      14864 :   g_return_val_if_fail (num_messages == 0 || messages != NULL, G_POLLABLE_RETURN_FAILED);
    5156                 :      14864 :   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_POLLABLE_RETURN_FAILED);
    5157                 :      14864 :   g_return_val_if_fail (error == NULL || *error == NULL, G_POLLABLE_RETURN_FAILED);
    5158                 :            : 
    5159                 :      14864 :   start_time = g_get_monotonic_time ();
    5160                 :            : 
    5161         [ +  + ]:      14864 :   if (!check_socket (socket, error))
    5162                 :          1 :     return G_POLLABLE_RETURN_FAILED;
    5163                 :            : 
    5164         [ -  + ]:      14863 :   if (!check_timeout (socket, error))
    5165                 :          0 :     return G_POLLABLE_RETURN_FAILED;
    5166                 :            : 
    5167         [ +  + ]:      14863 :   if (g_cancellable_set_error_if_cancelled (cancellable, error))
    5168                 :         12 :     return G_POLLABLE_RETURN_FAILED;
    5169                 :            : 
    5170         [ -  + ]:      14851 :   if (num_vectors == -1)
    5171                 :            :     {
    5172                 :          0 :       for (num_vectors = 0;
    5173         [ #  # ]:          0 :            vectors[num_vectors].buffer != NULL;
    5174                 :          0 :            num_vectors++)
    5175                 :            :         ;
    5176                 :            :     }
    5177                 :            : 
    5178         [ -  + ]:      14851 :   if (num_messages == -1)
    5179                 :            :     {
    5180                 :          0 :       for (num_messages = 0;
    5181   [ #  #  #  # ]:          0 :            messages != NULL && messages[num_messages] != NULL;
    5182                 :          0 :            num_messages++)
    5183                 :            :         ;
    5184                 :            :     }
    5185                 :            : 
    5186         [ +  + ]:      14851 :   if (num_vectors == 0)
    5187                 :            :     {
    5188                 :          1 :       zero = '\0';
    5189                 :            : 
    5190                 :          1 :       one_vector.buffer = &zero;
    5191                 :          1 :       one_vector.size = 1;
    5192                 :          1 :       num_vectors = 1;
    5193                 :          1 :       vectors = &one_vector;
    5194                 :            :     }
    5195                 :            : 
    5196                 :            : #ifndef G_OS_WIN32
    5197                 :            :   {
    5198                 :            :     GOutputMessage output_message;
    5199                 :            :     struct msghdr msg;
    5200                 :            :     gssize result;
    5201                 :      14851 :     GError *child_error = NULL;
    5202                 :            : 
    5203                 :      14851 :     output_message.address = address;
    5204                 :      14851 :     output_message.vectors = (GOutputVector *) vectors;
    5205                 :      14851 :     output_message.num_vectors = num_vectors;
    5206                 :      14851 :     output_message.bytes_sent = 0;
    5207                 :      14851 :     output_message.control_messages = messages;
    5208                 :      14851 :     output_message.num_control_messages = num_messages;
    5209                 :            : 
    5210   [ -  +  -  -  :      32142 :     output_message_to_msghdr (&output_message, NULL, &msg, NULL, &child_error);
          +  +  -  +  +  
          +  +  +  +  -  
          +  +  +  +  -  
                      + ]
    5211                 :            : 
    5212         [ -  + ]:      14851 :     if (child_error != NULL)
    5213                 :            :       {
    5214                 :          0 :         g_propagate_error (error, child_error);
    5215                 :          0 :         return G_POLLABLE_RETURN_FAILED;
    5216                 :            :       }
    5217                 :            : 
    5218                 :            :     while (1)
    5219                 :            :       {
    5220                 :      14851 :         result = sendmsg (socket->priv->fd, &msg, flags | G_SOCKET_DEFAULT_SEND_FLAGS);
    5221         [ -  + ]:      14851 :         if (result < 0)
    5222                 :            :           {
    5223                 :          0 :             int errsv = get_socket_errno ();
    5224                 :            : 
    5225         [ #  # ]:          0 :             if (errsv == EINTR)
    5226                 :          0 :               continue;
    5227                 :            : 
    5228   [ #  #  #  # ]:          0 :             if (errsv == EWOULDBLOCK || errsv == EAGAIN)
    5229                 :            :               {
    5230         [ #  # ]:          0 :                 if (timeout_us != 0)
    5231                 :            :                   {
    5232         [ #  # ]:          0 :                     if (!block_on_timeout (socket, G_IO_OUT, timeout_us, start_time,
    5233                 :            :                                            cancellable, error))
    5234                 :          0 :                       return G_POLLABLE_RETURN_FAILED;
    5235                 :            : 
    5236                 :          0 :                     continue;
    5237                 :            :                   }
    5238                 :            : 
    5239                 :          0 :                 return G_POLLABLE_RETURN_WOULD_BLOCK;
    5240                 :            :               }
    5241                 :            : 
    5242   [ #  #  #  # ]:          0 :             socket_set_error_lazy (error, errsv, _("Error sending message: %s"));
    5243                 :          0 :             return G_POLLABLE_RETURN_FAILED;
    5244                 :            :           }
    5245                 :      14851 :         break;
    5246                 :            :       }
    5247                 :            : 
    5248         [ +  - ]:      14851 :     if (bytes_written)
    5249                 :      14851 :       *bytes_written = result;
    5250                 :            : 
    5251                 :      14851 :     return G_POLLABLE_RETURN_OK;
    5252                 :            :   }
    5253                 :            : #else
    5254                 :            :   {
    5255                 :            :     struct sockaddr_storage addr;
    5256                 :            :     guint addrlen;
    5257                 :            :     DWORD bytes_sent;
    5258                 :            :     int result;
    5259                 :            :     WSABUF *bufs;
    5260                 :            :     gint i;
    5261                 :            : 
    5262                 :            :     /* Win32 doesn't support control messages.
    5263                 :            :        Actually this is possible for raw and datagram sockets
    5264                 :            :        via WSASendMessage on Vista or later, but that doesn't
    5265                 :            :        seem very useful */
    5266                 :            :     if (num_messages != 0)
    5267                 :            :       {
    5268                 :            :         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
    5269                 :            :                              _("GSocketControlMessage not supported on Windows"));
    5270                 :            :         return G_POLLABLE_RETURN_FAILED;
    5271                 :            :       }
    5272                 :            : 
    5273                 :            :     /* iov */
    5274                 :            :     bufs = g_newa (WSABUF, num_vectors);
    5275                 :            :     for (i = 0; i < num_vectors; i++)
    5276                 :            :       {
    5277                 :            :         bufs[i].buf = (char *)vectors[i].buffer;
    5278                 :            :         bufs[i].len = (gulong)vectors[i].size;
    5279                 :            :       }
    5280                 :            : 
    5281                 :            :     /* name */
    5282                 :            :     addrlen = 0; /* Avoid warning */
    5283                 :            :     if (address)
    5284                 :            :       {
    5285                 :            :         addrlen = g_socket_address_get_native_size (address);
    5286                 :            :         if (!g_socket_address_to_native (address, &addr, sizeof addr, error))
    5287                 :            :           return G_POLLABLE_RETURN_FAILED;
    5288                 :            :       }
    5289                 :            : 
    5290                 :            :     while (1)
    5291                 :            :       {
    5292                 :            :         if (address)
    5293                 :            :           result = WSASendTo (socket->priv->fd,
    5294                 :            :                               bufs, num_vectors,
    5295                 :            :                               &bytes_sent, flags,
    5296                 :            :                               (const struct sockaddr *)&addr, addrlen,
    5297                 :            :                               NULL, NULL);
    5298                 :            :         else
    5299                 :            :           result = WSASend (socket->priv->fd,
    5300                 :            :                             bufs, num_vectors,
    5301                 :            :                             &bytes_sent, flags,
    5302                 :            :                             NULL, NULL);
    5303                 :            : 
    5304                 :            :         if (result != 0)
    5305                 :            :           {
    5306                 :            :             int errsv = get_socket_errno ();
    5307                 :            : 
    5308                 :            :             if (errsv == WSAEINTR)
    5309                 :            :               continue;
    5310                 :            : 
    5311                 :            :             if (errsv == WSAEWOULDBLOCK)
    5312                 :            :               {
    5313                 :            :                 win32_unset_event_mask (socket, FD_WRITE);
    5314                 :            : 
    5315                 :            :                 if (timeout_us != 0)
    5316                 :            :                   {
    5317                 :            :                     if (!block_on_timeout (socket, G_IO_OUT, timeout_us,
    5318                 :            :                                            start_time, cancellable, error))
    5319                 :            :                       return G_POLLABLE_RETURN_FAILED;
    5320                 :            : 
    5321                 :            :                     continue;
    5322                 :            :                   }
    5323                 :            : 
    5324                 :            :                 return G_POLLABLE_RETURN_WOULD_BLOCK;
    5325                 :            :               }
    5326                 :            : 
    5327                 :            :             socket_set_error_lazy (error, errsv, _("Error sending message: %s"));
    5328                 :            :             return G_POLLABLE_RETURN_FAILED;
    5329                 :            :           }
    5330                 :            :         break;
    5331                 :            :       }
    5332                 :            : 
    5333                 :            :     if (bytes_written)
    5334                 :            :       *bytes_written = bytes_sent;
    5335                 :            :     return G_POLLABLE_RETURN_OK;
    5336                 :            :   }
    5337                 :            : #endif
    5338                 :            : }
    5339                 :            : 
    5340                 :            : /**
    5341                 :            :  * g_socket_send_messages:
    5342                 :            :  * @socket: a #GSocket
    5343                 :            :  * @messages: (array length=num_messages): an array of #GOutputMessage structs
    5344                 :            :  * @num_messages: the number of elements in @messages
    5345                 :            :  * @flags: an int containing #GSocketMsgFlags flags, which may additionally
    5346                 :            :  *    contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html)
    5347                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    5348                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    5349                 :            :  *
    5350                 :            :  * Send multiple data messages from @socket in one go.  This is the most
    5351                 :            :  * complicated and fully-featured version of this call. For easier use, see
    5352                 :            :  * g_socket_send(), g_socket_send_to(), and g_socket_send_message().
    5353                 :            :  *
    5354                 :            :  * @messages must point to an array of #GOutputMessage structs and
    5355                 :            :  * @num_messages must be the length of this array. Each #GOutputMessage
    5356                 :            :  * contains an address to send the data to, and a pointer to an array of
    5357                 :            :  * #GOutputVector structs to describe the buffers that the data to be sent
    5358                 :            :  * for each message will be gathered from. Using multiple #GOutputVectors is
    5359                 :            :  * more memory-efficient than manually copying data from multiple sources
    5360                 :            :  * into a single buffer, and more network-efficient than making multiple
    5361                 :            :  * calls to g_socket_send(). Sending multiple messages in one go avoids the
    5362                 :            :  * overhead of making a lot of syscalls in scenarios where a lot of data
    5363                 :            :  * packets need to be sent (e.g. high-bandwidth video streaming over RTP/UDP),
    5364                 :            :  * or where the same data needs to be sent to multiple recipients.
    5365                 :            :  *
    5366                 :            :  * @flags modify how the message is sent. The commonly available arguments
    5367                 :            :  * for this are available in the #GSocketMsgFlags enum, but the
    5368                 :            :  * values there are the same as the system values, and the flags
    5369                 :            :  * are passed in as-is, so you can pass in system-specific flags too.
    5370                 :            :  *
    5371                 :            :  * If the socket is in blocking mode the call will block until there is
    5372                 :            :  * space for all the data in the socket queue. If there is no space available
    5373                 :            :  * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error
    5374                 :            :  * will be returned if no data was written at all, otherwise the number of
    5375                 :            :  * messages sent will be returned. To be notified when space is available,
    5376                 :            :  * wait for the %G_IO_OUT condition. Note though that you may still receive
    5377                 :            :  * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously
    5378                 :            :  * notified of a %G_IO_OUT condition. (On Windows in particular, this is
    5379                 :            :  * very common due to the way the underlying APIs work.)
    5380                 :            :  *
    5381                 :            :  * On error -1 is returned and @error is set accordingly. An error will only
    5382                 :            :  * be returned if zero messages could be sent; otherwise the number of messages
    5383                 :            :  * successfully sent before the error will be returned.
    5384                 :            :  *
    5385                 :            :  * Returns: number of messages sent, or -1 on error. Note that the number of
    5386                 :            :  *     messages sent may be smaller than @num_messages if the socket is
    5387                 :            :  *     non-blocking or if @num_messages was larger than UIO_MAXIOV (1024),
    5388                 :            :  *     in which case the caller may re-try to send the remaining messages.
    5389                 :            :  *
    5390                 :            :  * Since: 2.44
    5391                 :            :  */
    5392                 :            : gint
    5393                 :          8 : g_socket_send_messages (GSocket        *socket,
    5394                 :            :                         GOutputMessage *messages,
    5395                 :            :                         guint           num_messages,
    5396                 :            :                         gint            flags,
    5397                 :            :                         GCancellable   *cancellable,
    5398                 :            :                         GError        **error)
    5399                 :            : {
    5400                 :          8 :   return g_socket_send_messages_with_timeout (socket, messages, num_messages,
    5401                 :            :                                               flags,
    5402         [ +  - ]:          8 :                                               socket->priv->blocking ? -1 : 0,
    5403                 :            :                                               cancellable, error);
    5404                 :            : }
    5405                 :            : 
    5406                 :            : static gint
    5407                 :          8 : g_socket_send_messages_with_timeout (GSocket        *socket,
    5408                 :            :                                      GOutputMessage *messages,
    5409                 :            :                                      guint           num_messages,
    5410                 :            :                                      gint            flags,
    5411                 :            :                                      gint64          timeout_us,
    5412                 :            :                                      GCancellable   *cancellable,
    5413                 :            :                                      GError        **error)
    5414                 :            : {
    5415                 :            :   gint64 start_time;
    5416                 :            : 
    5417                 :          8 :   g_return_val_if_fail (G_IS_SOCKET (socket), -1);
    5418                 :          8 :   g_return_val_if_fail (num_messages == 0 || messages != NULL, -1);
    5419                 :          8 :   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), -1);
    5420                 :          8 :   g_return_val_if_fail (error == NULL || *error == NULL, -1);
    5421                 :            : 
    5422                 :          8 :   start_time = g_get_monotonic_time ();
    5423                 :            : 
    5424         [ -  + ]:          8 :   if (!check_socket (socket, error))
    5425                 :          0 :     return -1;
    5426                 :            : 
    5427         [ -  + ]:          8 :   if (!check_timeout (socket, error))
    5428                 :          0 :     return -1;
    5429                 :            : 
    5430         [ -  + ]:          8 :   if (g_cancellable_set_error_if_cancelled (cancellable, error))
    5431                 :          0 :     return -1;
    5432                 :            : 
    5433         [ -  + ]:          8 :   if (num_messages == 0)
    5434                 :          0 :     return 0;
    5435                 :            : 
    5436                 :            : #if !defined (G_OS_WIN32) && defined (HAVE_SENDMMSG)
    5437                 :            :   {
    5438                 :            :     struct mmsghdr *msgvec;
    5439                 :            :     guint i, num_sent;
    5440                 :            : 
    5441                 :            :     /* Clamp the number of vectors if more given than we can write in one go.
    5442                 :            :      * The caller has to handle short writes anyway.
    5443                 :            :      */
    5444         [ -  + ]:          8 :     if (num_messages > G_IOV_MAX)
    5445                 :          0 :       num_messages = G_IOV_MAX;
    5446                 :            : 
    5447                 :          8 :     msgvec = g_newa (struct mmsghdr, num_messages);
    5448                 :            : 
    5449         [ +  + ]:         32 :     for (i = 0; i < num_messages; ++i)
    5450                 :            :       {
    5451                 :         24 :         GOutputMessage *msg = &messages[i];
    5452                 :         24 :         struct msghdr *msg_hdr = &msgvec[i].msg_hdr;
    5453                 :         24 :         GError *child_error = NULL;
    5454                 :            : 
    5455                 :         24 :         msgvec[i].msg_len = 0;
    5456                 :            : 
    5457   [ +  +  +  +  :         48 :         output_message_to_msghdr (msg, (i > 0) ? &messages[i - 1] : NULL,
          +  +  +  +  +  
          +  -  +  -  +  
          +  -  -  -  -  
             +  -  +  -  
                      + ]
    5458                 :            :                                   msg_hdr, (i > 0) ? &msgvec[i - 1].msg_hdr : NULL,
    5459                 :            :                                   &child_error);
    5460                 :            : 
    5461         [ -  + ]:         24 :         if (child_error != NULL)
    5462                 :            :           {
    5463                 :          0 :             g_propagate_error (error, child_error);
    5464                 :          0 :             return -1;
    5465                 :            :           }
    5466                 :            :       }
    5467                 :            : 
    5468         [ +  + ]:         14 :     for (num_sent = 0; num_sent < num_messages;)
    5469                 :            :       {
    5470                 :            :         gint ret;
    5471                 :            : 
    5472                 :         10 :         ret = sendmmsg (socket->priv->fd, msgvec + num_sent, num_messages - num_sent,
    5473                 :            :                         flags | G_SOCKET_DEFAULT_SEND_FLAGS);
    5474                 :            : 
    5475         [ +  + ]:         10 :         if (ret < 0)
    5476                 :            :           {
    5477                 :          4 :             int errsv = get_socket_errno ();
    5478                 :            : 
    5479         [ -  + ]:          4 :             if (errsv == EINTR)
    5480                 :          0 :               continue;
    5481                 :            : 
    5482   [ +  -  +  - ]:          4 :             if (timeout_us != 0 &&
    5483         [ -  + ]:          4 :                 (errsv == EWOULDBLOCK ||
    5484                 :            :                  errsv == EAGAIN))
    5485                 :            :               {
    5486         [ #  # ]:          0 :                 if (!block_on_timeout (socket, G_IO_OUT, timeout_us, start_time,
    5487                 :            :                                        cancellable, error))
    5488                 :            :                   {
    5489         [ #  # ]:          0 :                     if (num_sent > 0)
    5490                 :            :                       {
    5491                 :          0 :                         g_clear_error (error);
    5492                 :          0 :                         break;
    5493                 :            :                       }
    5494                 :            : 
    5495                 :          0 :                     return -1;
    5496                 :            :                   }
    5497                 :            : 
    5498                 :          0 :                 continue;
    5499                 :            :               }
    5500                 :            : 
    5501                 :            :             /* If any messages were successfully sent, do not error. */
    5502         [ +  + ]:          4 :             if (num_sent > 0)
    5503                 :          2 :               break;
    5504                 :            : 
    5505   [ +  -  -  + ]:          2 :             socket_set_error_lazy (error, errsv, _("Error sending message: %s"));
    5506                 :            : 
    5507                 :          2 :             return -1;
    5508                 :            :           }
    5509                 :            : 
    5510                 :          6 :         num_sent += ret;
    5511                 :            :       }
    5512                 :            : 
    5513         [ +  + ]:         20 :     for (i = 0; i < num_sent; ++i)
    5514                 :         14 :       messages[i].bytes_sent = msgvec[i].msg_len;
    5515                 :            : 
    5516                 :          6 :     return num_sent;
    5517                 :            :   }
    5518                 :            : #else
    5519                 :            :   {
    5520                 :            :     gssize result;
    5521                 :            :     guint i;
    5522                 :            :     gint64 wait_timeout;
    5523                 :            : 
    5524                 :            :     wait_timeout = timeout_us;
    5525                 :            : 
    5526                 :            :     for (i = 0; i < num_messages; ++i)
    5527                 :            :       {
    5528                 :            :         GOutputMessage *msg = &messages[i];
    5529                 :            :         GError *msg_error = NULL;
    5530                 :            :         GPollableReturn pollable_result;
    5531                 :            :         gsize bytes_written = 0;
    5532                 :            : 
    5533                 :            :         pollable_result = g_socket_send_message_with_timeout (socket, msg->address,
    5534                 :            :                                                               msg->vectors,
    5535                 :            :                                                               msg->num_vectors,
    5536                 :            :                                                               msg->control_messages,
    5537                 :            :                                                               msg->num_control_messages,
    5538                 :            :                                                               flags, wait_timeout,
    5539                 :            :                                                               &bytes_written,
    5540                 :            :                                                               cancellable, &msg_error);
    5541                 :            : 
    5542                 :            :         if (pollable_result == G_POLLABLE_RETURN_WOULD_BLOCK)
    5543                 :            :           {
    5544                 :            : #ifndef G_OS_WIN32
    5545                 :            :             socket_set_error_lazy (&msg_error, EWOULDBLOCK, _("Error sending message: %s"));
    5546                 :            : #else
    5547                 :            :             socket_set_error_lazy (&msg_error, WSAEWOULDBLOCK, _("Error sending message: %s"));
    5548                 :            : #endif
    5549                 :            :           }
    5550                 :            : 
    5551                 :            :         if (G_MAXSSIZE > bytes_written &&
    5552                 :            :             pollable_result == G_POLLABLE_RETURN_OK)
    5553                 :            :           result = (gssize) bytes_written;
    5554                 :            :         else
    5555                 :            :           result = -1;
    5556                 :            : 
    5557                 :            :         /* check if we've timed out or how much time to wait at most */
    5558                 :            :         if (timeout_us > 0)
    5559                 :            :           {
    5560                 :            :             gint64 elapsed = g_get_monotonic_time () - start_time;
    5561                 :            :             wait_timeout = MAX (timeout_us - elapsed, 1);
    5562                 :            :           }
    5563                 :            : 
    5564                 :            :         if (result < 0)
    5565                 :            :           {
    5566                 :            :             /* if we couldn't send all messages, just return how many we did
    5567                 :            :              * manage to send, provided we managed to send at least one */
    5568                 :            :             if (i > 0)
    5569                 :            :               {
    5570                 :            :                 g_error_free (msg_error);
    5571                 :            :                 return i;
    5572                 :            :               }
    5573                 :            :             else
    5574                 :            :               {
    5575                 :            :                 g_propagate_error (error, msg_error);
    5576                 :            :                 return -1;
    5577                 :            :               }
    5578                 :            :           }
    5579                 :            : 
    5580                 :            :         msg->bytes_sent = result;
    5581                 :            :       }
    5582                 :            : 
    5583                 :            :     return i;
    5584                 :            :   }
    5585                 :            : #endif
    5586                 :            : }
    5587                 :            : 
    5588                 :            : static GSocketAddress *
    5589                 :        147 : cache_recv_address (GSocket *socket, struct sockaddr *native, size_t native_len)
    5590                 :            : {
    5591                 :            :   GSocketAddress *saddr;
    5592                 :            :   gint i;
    5593                 :        147 :   guint64 oldest_time = G_MAXUINT64;
    5594                 :        147 :   gint oldest_index = 0;
    5595                 :            : 
    5596         [ -  + ]:        147 :   if (native_len == 0)
    5597                 :          0 :     return NULL;
    5598                 :            : 
    5599                 :        147 :   saddr = NULL;
    5600         [ +  + ]:        515 :   for (i = 0; i < RECV_ADDR_CACHE_SIZE; i++)
    5601                 :            :     {
    5602                 :        469 :       GSocketAddress *tmp = socket->priv->recv_addr_cache[i].addr;
    5603                 :        469 :       gpointer tmp_native = socket->priv->recv_addr_cache[i].native;
    5604                 :        469 :       gsize tmp_native_len = socket->priv->recv_addr_cache[i].native_len;
    5605                 :            : 
    5606         [ +  + ]:        469 :       if (!tmp)
    5607                 :        368 :         continue;
    5608                 :            : 
    5609         [ -  + ]:        101 :       if (tmp_native_len != native_len)
    5610                 :          0 :         continue;
    5611                 :            : 
    5612         [ +  - ]:        101 :       if (memcmp (tmp_native, native, native_len) == 0)
    5613                 :            :         {
    5614                 :        101 :           saddr = g_object_ref (tmp);
    5615                 :        101 :           socket->priv->recv_addr_cache[i].last_used = g_get_monotonic_time ();
    5616                 :        101 :           return saddr;
    5617                 :            :         }
    5618                 :            : 
    5619         [ #  # ]:          0 :       if (socket->priv->recv_addr_cache[i].last_used < oldest_time)
    5620                 :            :         {
    5621                 :          0 :           oldest_time = socket->priv->recv_addr_cache[i].last_used;
    5622                 :          0 :           oldest_index = i;
    5623                 :            :         }
    5624                 :            :     }
    5625                 :            : 
    5626                 :         46 :   saddr = g_socket_address_new_from_native (native, native_len);
    5627                 :            : 
    5628         [ -  + ]:         46 :   if (socket->priv->recv_addr_cache[oldest_index].addr)
    5629                 :            :     {
    5630                 :          0 :       g_object_unref (socket->priv->recv_addr_cache[oldest_index].addr);
    5631                 :          0 :       g_free (socket->priv->recv_addr_cache[oldest_index].native);
    5632                 :            :     }
    5633                 :            : 
    5634                 :         46 :   socket->priv->recv_addr_cache[oldest_index].native = g_memdup2 (native, native_len);
    5635                 :         46 :   socket->priv->recv_addr_cache[oldest_index].native_len = native_len;
    5636                 :         46 :   socket->priv->recv_addr_cache[oldest_index].addr = g_object_ref (saddr);
    5637                 :         46 :   socket->priv->recv_addr_cache[oldest_index].last_used = g_get_monotonic_time ();
    5638                 :            : 
    5639                 :         46 :   return saddr;
    5640                 :            : }
    5641                 :            : 
    5642                 :            : static gssize
    5643                 :      55908 : g_socket_receive_message_with_timeout (GSocket                 *socket,
    5644                 :            :                                        GSocketAddress         **address,
    5645                 :            :                                        GInputVector            *vectors,
    5646                 :            :                                        gint                     num_vectors,
    5647                 :            :                                        GSocketControlMessage ***messages,
    5648                 :            :                                        gint                    *num_messages,
    5649                 :            :                                        gint                    *flags,
    5650                 :            :                                        gint64                   timeout_us,
    5651                 :            :                                        GCancellable            *cancellable,
    5652                 :            :                                        GError                 **error)
    5653                 :            : {
    5654                 :            :   GInputVector one_vector;
    5655                 :            :   char one_byte;
    5656                 :            :   gint64 start_time;
    5657                 :            : 
    5658                 :      55908 :   g_return_val_if_fail (G_IS_SOCKET (socket), -1);
    5659                 :            : 
    5660                 :      55908 :   start_time = g_get_monotonic_time ();
    5661                 :            : 
    5662         [ +  + ]:      55908 :   if (!check_socket (socket, error))
    5663                 :         18 :     return -1;
    5664                 :            : 
    5665         [ -  + ]:      55890 :   if (!check_timeout (socket, error))
    5666                 :          0 :     return -1;
    5667                 :            : 
    5668         [ +  + ]:      55890 :   if (g_cancellable_set_error_if_cancelled (cancellable, error))
    5669                 :       1119 :     return -1;
    5670                 :            : 
    5671         [ -  + ]:      54771 :   if (num_vectors == -1)
    5672                 :            :     {
    5673                 :          0 :       for (num_vectors = 0;
    5674         [ #  # ]:          0 :            vectors[num_vectors].buffer != NULL;
    5675                 :          0 :            num_vectors++)
    5676                 :            :         ;
    5677                 :            :     }
    5678                 :            : 
    5679         [ +  + ]:      54771 :   if (num_vectors == 0)
    5680                 :            :     {
    5681                 :          1 :       one_vector.buffer = &one_byte;
    5682                 :          1 :       one_vector.size = 1;
    5683                 :          1 :       num_vectors = 1;
    5684                 :          1 :       vectors = &one_vector;
    5685                 :            :     }
    5686                 :            : 
    5687                 :            : #ifndef G_OS_WIN32
    5688                 :            :   {
    5689                 :            :     GInputMessage input_message;
    5690                 :            :     struct msghdr msg;
    5691                 :            :     gssize result;
    5692                 :            : 
    5693                 :      54771 :     input_message.address = address;
    5694                 :      54771 :     input_message.vectors = vectors;
    5695                 :      54771 :     input_message.num_vectors = num_vectors;
    5696                 :      54771 :     input_message.bytes_received = 0;
    5697         [ +  + ]:      54771 :     input_message.flags = (flags != NULL) ? *flags : 0;
    5698                 :      54771 :     input_message.control_messages = messages;
    5699                 :      54771 :     input_message.num_control_messages = (guint *) num_messages;
    5700                 :            : 
    5701                 :            :     /* We always set the close-on-exec flag so we don't leak file
    5702                 :            :      * descriptors into child processes.  Note that gunixfdmessage.c
    5703                 :            :      * will later call fcntl (fd, FD_CLOEXEC), but that isn't atomic.
    5704                 :            :      */
    5705                 :            : #ifdef MSG_CMSG_CLOEXEC
    5706                 :      54771 :     input_message.flags |= MSG_CMSG_CLOEXEC;
    5707                 :            : #endif
    5708                 :            : 
    5709   [ +  +  +  + ]:      54771 :     input_message_to_msghdr (&input_message, &msg);
    5710                 :            : 
    5711                 :            :     /* do it */
    5712                 :            :     while (1)
    5713                 :            :       {
    5714                 :      54791 :         result = recvmsg (socket->priv->fd, &msg, msg.msg_flags);
    5715                 :            : #ifdef MSG_CMSG_CLOEXEC 
    5716   [ +  +  -  + ]:      54791 :         if (result < 0 && get_socket_errno () == EINVAL)
    5717                 :            :           {
    5718                 :            :             /* We must be running on an old kernel.  Call without the flag. */
    5719                 :          0 :             msg.msg_flags &= ~(MSG_CMSG_CLOEXEC);
    5720                 :          0 :             result = recvmsg (socket->priv->fd, &msg, msg.msg_flags);
    5721                 :            :           }
    5722                 :            : #endif
    5723                 :            : 
    5724         [ +  + ]:      54791 :         if (result < 0)
    5725                 :            :           {
    5726                 :         24 :             int errsv = get_socket_errno ();
    5727                 :            : 
    5728         [ -  + ]:         24 :             if (errsv == EINTR)
    5729                 :          0 :               continue;
    5730                 :            : 
    5731   [ +  -  -  + ]:         24 :             if (timeout_us != 0 &&
    5732         [ #  # ]:          0 :                 (errsv == EWOULDBLOCK ||
    5733                 :            :                  errsv == EAGAIN))
    5734                 :            :               {
    5735         [ +  + ]:         24 :                 if (!block_on_timeout (socket, G_IO_IN, timeout_us, start_time,
    5736                 :            :                                        cancellable, error))
    5737                 :          4 :                   return -1;
    5738                 :            : 
    5739                 :         20 :                 continue;
    5740                 :            :               }
    5741                 :            : 
    5742   [ #  #  #  # ]:          0 :             socket_set_error_lazy (error, errsv, _("Error receiving message: %s"));
    5743                 :          0 :             return -1;
    5744                 :            :           }
    5745                 :      54767 :         break;
    5746                 :            :       }
    5747                 :            : 
    5748                 :      54767 :     input_message_from_msghdr (&msg, &input_message, socket);
    5749                 :            : 
    5750         [ +  + ]:      54767 :     if (flags != NULL)
    5751                 :        127 :       *flags = input_message.flags;
    5752                 :            : 
    5753                 :      54767 :     return result;
    5754                 :            :   }
    5755                 :            : #else
    5756                 :            :   {
    5757                 :            :     struct sockaddr_storage addr;
    5758                 :            :     int addrlen;
    5759                 :            :     DWORD bytes_received;
    5760                 :            :     DWORD win_flags;
    5761                 :            :     int result;
    5762                 :            :     WSABUF *bufs;
    5763                 :            :     gint i;
    5764                 :            : 
    5765                 :            :     /* iov */
    5766                 :            :     bufs = g_newa (WSABUF, num_vectors);
    5767                 :            :     for (i = 0; i < num_vectors; i++)
    5768                 :            :       {
    5769                 :            :         bufs[i].buf = (char *)vectors[i].buffer;
    5770                 :            :         bufs[i].len = (gulong)vectors[i].size;
    5771                 :            :       }
    5772                 :            : 
    5773                 :            :     /* flags */
    5774                 :            :     if (flags != NULL)
    5775                 :            :       win_flags = *flags;
    5776                 :            :     else
    5777                 :            :       win_flags = 0;
    5778                 :            : 
    5779                 :            :     /* do it */
    5780                 :            :     while (1)
    5781                 :            :       {
    5782                 :            :         /* addrlen has to be of type int because that’s how WSARecvFrom() is defined */
    5783                 :            :         G_STATIC_ASSERT (sizeof addr <= G_MAXINT);
    5784                 :            : 
    5785                 :            :         addrlen = sizeof addr;
    5786                 :            :         if (address)
    5787                 :            :           result = WSARecvFrom (socket->priv->fd,
    5788                 :            :                                 bufs, num_vectors,
    5789                 :            :                                 &bytes_received, &win_flags,
    5790                 :            :                                 (struct sockaddr *)&addr, &addrlen,
    5791                 :            :                                 NULL, NULL);
    5792                 :            :         else
    5793                 :            :           result = WSARecv (socket->priv->fd,
    5794                 :            :                             bufs, num_vectors,
    5795                 :            :                             &bytes_received, &win_flags,
    5796                 :            :                             NULL, NULL);
    5797                 :            :         if (result != 0)
    5798                 :            :           {
    5799                 :            :             int errsv = get_socket_errno ();
    5800                 :            : 
    5801                 :            :             if (errsv == WSAEINTR)
    5802                 :            :               continue;
    5803                 :            : 
    5804                 :            :             win32_unset_event_mask (socket, FD_READ);
    5805                 :            : 
    5806                 :            :             if (errsv == WSAEWOULDBLOCK)
    5807                 :            :               {
    5808                 :            :                 if (timeout_us != 0)
    5809                 :            :                   {
    5810                 :            :                     if (!block_on_timeout (socket, G_IO_IN, timeout_us,
    5811                 :            :                                            start_time, cancellable, error))
    5812                 :            :                       return -1;
    5813                 :            : 
    5814                 :            :                     continue;
    5815                 :            :                   }
    5816                 :            :               }
    5817                 :            : 
    5818                 :            :             socket_set_error_lazy (error, errsv, _("Error receiving message: %s"));
    5819                 :            :             return -1;
    5820                 :            :           }
    5821                 :            :         win32_unset_event_mask (socket, FD_READ);
    5822                 :            :         break;
    5823                 :            :       }
    5824                 :            : 
    5825                 :            :     /* decode address */
    5826                 :            :     if (address != NULL)
    5827                 :            :       {
    5828                 :            :         *address = cache_recv_address (socket, (struct sockaddr *)&addr, addrlen);
    5829                 :            :       }
    5830                 :            : 
    5831                 :            :     /* capture the flags */
    5832                 :            :     if (flags != NULL)
    5833                 :            :       *flags = win_flags;
    5834                 :            : 
    5835                 :            :     if (messages != NULL)
    5836                 :            :       *messages = NULL;
    5837                 :            :     if (num_messages != NULL)
    5838                 :            :       *num_messages = 0;
    5839                 :            : 
    5840                 :            :     return bytes_received;
    5841                 :            :   }
    5842                 :            : #endif
    5843                 :            : }
    5844                 :            : 
    5845                 :            : /**
    5846                 :            :  * g_socket_receive_messages:
    5847                 :            :  * @socket: a #GSocket
    5848                 :            :  * @messages: (array length=num_messages): an array of #GInputMessage structs
    5849                 :            :  * @num_messages: the number of elements in @messages
    5850                 :            :  * @flags: an int containing #GSocketMsgFlags flags for the overall operation,
    5851                 :            :  *    which may additionally contain
    5852                 :            :  *    [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html)
    5853                 :            :  * @cancellable: (nullable): a %GCancellable or %NULL
    5854                 :            :  * @error: #GError for error reporting, or %NULL to ignore
    5855                 :            :  *
    5856                 :            :  * Receive multiple data messages from @socket in one go.  This is the most
    5857                 :            :  * complicated and fully-featured version of this call. For easier use, see
    5858                 :            :  * g_socket_receive(), g_socket_receive_from(), and g_socket_receive_message().
    5859                 :            :  *
    5860                 :            :  * @messages must point to an array of #GInputMessage structs and
    5861                 :            :  * @num_messages must be the length of this array. Each #GInputMessage
    5862                 :            :  * contains a pointer to an array of #GInputVector structs describing the
    5863                 :            :  * buffers that the data received in each message will be written to. Using
    5864                 :            :  * multiple #GInputVectors is more memory-efficient than manually copying data
    5865                 :            :  * out of a single buffer to multiple sources, and more system-call-efficient
    5866                 :            :  * than making multiple calls to g_socket_receive(), such as in scenarios where
    5867                 :            :  * a lot of data packets need to be received (e.g. high-bandwidth video
    5868                 :            :  * streaming over RTP/UDP).
    5869                 :            :  *
    5870                 :            :  * @flags modify how all messages are received. The commonly available
    5871                 :            :  * arguments for this are available in the #GSocketMsgFlags enum, but the
    5872                 :            :  * values there are the same as the system values, and the flags
    5873                 :            :  * are passed in as-is, so you can pass in system-specific flags too. These
    5874                 :            :  * flags affect the overall receive operation. Flags affecting individual
    5875                 :            :  * messages are returned in #GInputMessage.flags.
    5876                 :            :  *
    5877                 :            :  * The other members of #GInputMessage are treated as described in its
    5878                 :            :  * documentation.
    5879                 :            :  *
    5880                 :            :  * If #GSocket:blocking is %TRUE the call will block until @num_messages have
    5881                 :            :  * been received, or the end of the stream is reached.
    5882                 :            :  *
    5883                 :            :  * If #GSocket:blocking is %FALSE the call will return up to @num_messages
    5884                 :            :  * without blocking, or %G_IO_ERROR_WOULD_BLOCK if no messages are queued in the
    5885                 :            :  * operating system to be received.
    5886                 :            :  *
    5887                 :            :  * In blocking mode, if #GSocket:timeout is positive and is reached before any
    5888                 :            :  * messages are received, %G_IO_ERROR_TIMED_OUT is returned, otherwise up to
    5889                 :            :  * @num_messages are returned. (Note: This is effectively the
    5890                 :            :  * behaviour of `MSG_WAITFORONE` with recvmmsg().)
    5891                 :            :  *
    5892                 :            :  * To be notified when messages are available, wait for the
    5893                 :            :  * %G_IO_IN condition. Note though that you may still receive
    5894                 :            :  * %G_IO_ERROR_WOULD_BLOCK from g_socket_receive_messages() even if you were
    5895                 :            :  * previously notified of a %G_IO_IN condition.
    5896                 :            :  *
    5897                 :            :  * If the remote peer closes the connection, any messages queued in the
    5898                 :            :  * operating system will be returned, and subsequent calls to
    5899                 :            :  * g_socket_receive_messages() will return 0 (with no error set).
    5900                 :            :  *
    5901                 :            :  * On error -1 is returned and @error is set accordingly. An error will only
    5902                 :            :  * be returned if zero messages could be received; otherwise the number of
    5903                 :            :  * messages successfully received before the error will be returned.
    5904                 :            :  *
    5905                 :            :  * Returns: number of messages received, or -1 on error. Note that the number
    5906                 :            :  *     of messages received may be smaller than @num_messages if in non-blocking
    5907                 :            :  *     mode, if the peer closed the connection, or if @num_messages
    5908                 :            :  *     was larger than `UIO_MAXIOV` (1024), in which case the caller may re-try
    5909                 :            :  *     to receive the remaining messages.
    5910                 :            :  *
    5911                 :            :  * Since: 2.48
    5912                 :            :  */
    5913                 :            : gint
    5914                 :          8 : g_socket_receive_messages (GSocket        *socket,
    5915                 :            :                            GInputMessage  *messages,
    5916                 :            :                            guint           num_messages,
    5917                 :            :                            gint            flags,
    5918                 :            :                            GCancellable   *cancellable,
    5919                 :            :                            GError        **error)
    5920                 :            : {
    5921   [ +  -  -  + ]:         16 :   if (!check_socket (socket, error) ||
    5922                 :          8 :       !check_timeout (socket, error))
    5923                 :          0 :     return -1;
    5924                 :            : 
    5925                 :          8 :   return g_socket_receive_messages_with_timeout (socket, messages, num_messages,
    5926                 :            :                                                  flags,
    5927         [ +  + ]:          8 :                                                  socket->priv->blocking ? -1 : 0,
    5928                 :            :                                                  cancellable, error);
    5929                 :            : }
    5930                 :            : 
    5931                 :            : static gint
    5932                 :          8 : g_socket_receive_messages_with_timeout (GSocket        *socket,
    5933                 :            :                                         GInputMessage  *messages,
    5934                 :            :                                         guint           num_messages,
    5935                 :            :                                         gint            flags,
    5936                 :            :                                         gint64          timeout_us,
    5937                 :            :                                         GCancellable   *cancellable,
    5938                 :            :                                         GError        **error)
    5939                 :            : {
    5940                 :            :   gint64 start_time;
    5941                 :            : 
    5942                 :          8 :   g_return_val_if_fail (G_IS_SOCKET (socket), -1);
    5943                 :          8 :   g_return_val_if_fail (num_messages == 0 || messages != NULL, -1);
    5944                 :          8 :   g_return_val_if_fail (cancellable == NULL ||
    5945                 :            :                         G_IS_CANCELLABLE (cancellable), -1);
    5946                 :          8 :   g_return_val_if_fail (error == NULL || *error == NULL, -1);
    5947                 :            : 
    5948                 :          8 :   start_time = g_get_monotonic_time ();
    5949                 :            : 
    5950         [ -  + ]:          8 :   if (!check_socket (socket, error))
    5951                 :          0 :     return -1;
    5952                 :            : 
    5953         [ -  + ]:          8 :   if (!check_timeout (socket, error))
    5954                 :          0 :     return -1;
    5955                 :            : 
    5956         [ -  + ]:          8 :   if (g_cancellable_set_error_if_cancelled (cancellable, error))
    5957                 :          0 :     return -1;
    5958                 :            : 
    5959         [ -  + ]:          8 :   if (num_messages == 0)
    5960                 :          0 :     return 0;
    5961                 :            : 
    5962                 :            : #if !defined (G_OS_WIN32) && defined (HAVE_RECVMMSG)
    5963                 :            :   {
    5964                 :            :     struct mmsghdr *msgvec;
    5965                 :            :     guint i, num_received;
    5966                 :            : 
    5967                 :            :     /* Clamp the number of vectors if more given than we can write in one go.
    5968                 :            :      * The caller has to handle short writes anyway.
    5969                 :            :      */
    5970         [ -  + ]:          8 :     if (num_messages > G_IOV_MAX)
    5971                 :          0 :       num_messages = G_IOV_MAX;
    5972                 :            : 
    5973                 :          8 :     msgvec = g_newa (struct mmsghdr, num_messages);
    5974                 :            : 
    5975         [ +  + ]:         20 :     for (i = 0; i < num_messages; ++i)
    5976                 :            :       {
    5977                 :         12 :         GInputMessage *msg = &messages[i];
    5978                 :         12 :         struct msghdr *msg_hdr = &msgvec[i].msg_hdr;
    5979                 :            : 
    5980   [ -  +  +  - ]:         12 :         input_message_to_msghdr (msg, msg_hdr);
    5981                 :         12 :         msgvec[i].msg_len = 0;
    5982                 :            :       }
    5983                 :            : 
    5984                 :            :     /* We always set the close-on-exec flag so we don't leak file
    5985                 :            :      * descriptors into child processes.  Note that gunixfdmessage.c
    5986                 :            :      * will later call fcntl (fd, FD_CLOEXEC), but that isn't atomic.
    5987                 :            :      */
    5988                 :            : #ifdef MSG_CMSG_CLOEXEC
    5989                 :          8 :     flags |= MSG_CMSG_CLOEXEC;
    5990                 :            : #endif
    5991                 :            : 
    5992         [ +  + ]:         15 :     for (num_received = 0; num_received < num_messages;)
    5993                 :            :       {
    5994                 :            :         gint ret;
    5995                 :            : 
    5996                 :            :         /* We operate in non-blocking mode and handle the timeout ourselves. */
    5997                 :         13 :         ret = recvmmsg (socket->priv->fd,
    5998                 :         13 :                         msgvec + num_received,
    5999                 :            :                         num_messages - num_received,
    6000                 :            :                         flags | G_SOCKET_DEFAULT_SEND_FLAGS, NULL);
    6001                 :            : #ifdef MSG_CMSG_CLOEXEC
    6002   [ +  +  -  + ]:         13 :         if (ret < 0 && get_socket_errno () == EINVAL)
    6003                 :            :           {
    6004                 :            :             /* We must be running on an old kernel. Call without the flag. */
    6005                 :          0 :             flags &= ~(MSG_CMSG_CLOEXEC);
    6006                 :          0 :             ret = recvmmsg (socket->priv->fd,
    6007                 :          0 :                             msgvec + num_received,
    6008                 :            :                             num_messages - num_received,
    6009                 :            :                             flags | G_SOCKET_DEFAULT_SEND_FLAGS, NULL);
    6010                 :            :           }
    6011                 :            : #endif
    6012                 :            : 
    6013         [ +  + ]:         13 :         if (ret < 0)
    6014                 :            :           {
    6015                 :          9 :             int errsv = get_socket_errno ();
    6016                 :            : 
    6017         [ -  + ]:          9 :             if (errsv == EINTR)
    6018                 :          0 :               continue;
    6019                 :            : 
    6020   [ +  +  -  + ]:          9 :             if (timeout_us != 0 &&
    6021         [ #  # ]:          0 :                 (errsv == EWOULDBLOCK ||
    6022                 :            :                  errsv == EAGAIN))
    6023                 :            :               {
    6024         [ +  + ]:          7 :                 if (!block_on_timeout (socket, G_IO_IN, timeout_us, start_time,
    6025                 :            :                                        cancellable, error))
    6026                 :            :                   {
    6027         [ -  + ]:          4 :                     if (num_received > 0)
    6028                 :            :                       {
    6029                 :          0 :                         g_clear_error (error);
    6030                 :          0 :                         break;
    6031                 :            :                       }
    6032                 :            : 
    6033                 :          4 :                     return -1;
    6034                 :            :                   }
    6035                 :            : 
    6036                 :          3 :                 continue;
    6037                 :            :               }
    6038                 :            : 
    6039                 :            :             /* If any messages were successfully received, do not error. */
    6040         [ -  + ]:          2 :             if (num_received > 0)
    6041                 :          0 :               break;
    6042                 :            : 
    6043   [ +  -  +  - ]:          2 :             socket_set_error_lazy (error, errsv,
    6044                 :            :                                    _("Error receiving message: %s"));
    6045                 :            : 
    6046                 :          2 :             return -1;
    6047                 :            :           }
    6048         [ -  + ]:          4 :         else if (ret == 0)
    6049                 :            :           {
    6050                 :            :             /* EOS. */
    6051                 :          0 :             break;
    6052                 :            :           }
    6053                 :            : 
    6054                 :          4 :         num_received += ret;
    6055                 :            :       }
    6056                 :            : 
    6057         [ +  + ]:          8 :     for (i = 0; i < num_received; ++i)
    6058                 :            :       {
    6059                 :          6 :         input_message_from_msghdr (&msgvec[i].msg_hdr, &messages[i], socket);
    6060                 :          6 :         messages[i].bytes_received = msgvec[i].msg_len;
    6061                 :            :       }
    6062                 :            : 
    6063                 :          2 :     return num_received;
    6064                 :            :   }
    6065                 :            : #else
    6066                 :            :   {
    6067                 :            :     guint i;
    6068                 :            :     gint64 wait_timeout;
    6069                 :            : 
    6070                 :            :     wait_timeout = timeout_us;
    6071                 :            : 
    6072                 :            :     for (i = 0; i < num_messages; i++)
    6073                 :            :       {
    6074                 :            :         GInputMessage *msg = &messages[i];
    6075                 :            :         gssize len;
    6076                 :            :         GError *msg_error = NULL;
    6077                 :            : 
    6078                 :            :         msg->flags = flags;  /* in-out parameter */
    6079                 :            : 
    6080                 :            :         len = g_socket_receive_message_with_timeout (socket,
    6081                 :            :                                                      msg->address,
    6082                 :            :                                                      msg->vectors,
    6083                 :            :                                                      msg->num_vectors,
    6084                 :            :                                                      msg->control_messages,
    6085                 :            :                                                      (gint *) msg->num_control_messages,
    6086                 :            :                                                      &msg->flags,
    6087                 :            :                                                      wait_timeout,
    6088                 :            :                                                      cancellable,
    6089                 :            :                                                      &msg_error);
    6090                 :            : 
    6091                 :            :         /* check if we've timed out or how much time to wait at most */
    6092                 :            :         if (timeout_us > 0)
    6093                 :            :           {
    6094                 :            :             gint64 elapsed = g_get_monotonic_time () - start_time;
    6095                 :            :             wait_timeout = MAX (timeout_us - elapsed, 1);
    6096                 :            :           }
    6097                 :            : 
    6098                 :            :         if (len >= 0)
    6099                 :            :           msg->bytes_received = len;
    6100                 :            : 
    6101                 :            :         if (i != 0 &&
    6102                 :            :             (g_error_matches (msg_error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK) ||
    6103                 :            :              g_error_matches (msg_error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)))
    6104                 :            :           {
    6105                 :            :             g_clear_error (&msg_error);
    6106                 :            :             break;
    6107                 :            :           }
    6108                 :            : 
    6109                 :            :         if (msg_error != NULL)
    6110                 :            :           {
    6111                 :            :             g_propagate_error (error, msg_error);
    6112                 :            :             return -1;
    6113                 :            :           }
    6114                 :            : 
    6115                 :            :         if (len == 0)
    6116                 :            :           break;
    6117                 :            :       }
    6118                 :            : 
    6119                 :            :     return i;
    6120                 :            :   }
    6121                 :            : #endif
    6122                 :            : }
    6123                 :            : 
    6124                 :            : /**
    6125                 :            :  * g_socket_receive_message:
    6126                 :            :  * @socket: a #GSocket
    6127                 :            :  * @address: (out) (optional): a pointer to a #GSocketAddress
    6128                 :            :  *     pointer, or %NULL
    6129                 :            :  * @vectors: (array length=num_vectors): an array of #GInputVector structs
    6130                 :            :  * @num_vectors: the number of elements in @vectors, or -1
    6131                 :            :  * @messages: (array length=num_messages) (out) (optional) (nullable): a pointer
    6132                 :            :  *    which may be filled with an array of #GSocketControlMessages, or %NULL
    6133                 :            :  * @num_messages: (out): a pointer which will be filled with the number of
    6134                 :            :  *    elements in @messages, or %NULL
    6135                 :            :  * @flags: (inout): a pointer to an int containing #GSocketMsgFlags flags,
    6136                 :            :  *    which may additionally contain
    6137                 :            :  *    [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html)
    6138                 :            :  * @cancellable: a %GCancellable or %NULL
    6139                 :            :  * @error: a #GError pointer, or %NULL
    6140                 :            :  *
    6141                 :            :  * Receive data from a socket.  For receiving multiple messages, see
    6142                 :            :  * g_socket_receive_messages(); for easier use, see
    6143                 :            :  * g_socket_receive() and g_socket_receive_from().
    6144                 :            :  *
    6145                 :            :  * If @address is non-%NULL then @address will be set equal to the
    6146                 :            :  * source address of the received packet.
    6147                 :            :  * @address is owned by the caller.
    6148                 :            :  *
    6149                 :            :  * @vector must point to an array of #GInputVector structs and
    6150                 :            :  * @num_vectors must be the length of this array.  These structs
    6151                 :            :  * describe the buffers that received data will be scattered into.
    6152                 :            :  * If @num_vectors is -1, then @vectors is assumed to be terminated
    6153                 :            :  * by a #GInputVector with a %NULL buffer pointer.
    6154                 :            :  *
    6155                 :            :  * As a special case, if @num_vectors is 0 (in which case, @vectors
    6156                 :            :  * may of course be %NULL), then a single byte is received and
    6157                 :            :  * discarded. This is to facilitate the common practice of sending a
    6158                 :            :  * single '\0' byte for the purposes of transferring ancillary data.
    6159                 :            :  *
    6160                 :            :  * @messages, if non-%NULL, will be set to point to a newly-allocated
    6161                 :            :  * array of #GSocketControlMessage instances or %NULL if no such
    6162                 :            :  * messages was received. These correspond to the control messages
    6163                 :            :  * received from the kernel, one #GSocketControlMessage per message
    6164                 :            :  * from the kernel. This array is %NULL-terminated and must be freed
    6165                 :            :  * by the caller using g_free() after calling g_object_unref() on each
    6166                 :            :  * element. If @messages is %NULL, any control messages received will
    6167                 :            :  * be discarded.
    6168                 :            :  *
    6169                 :            :  * @num_messages, if non-%NULL, will be set to the number of control
    6170                 :            :  * messages received.
    6171                 :            :  *
    6172                 :            :  * If both @messages and @num_messages are non-%NULL, then
    6173                 :            :  * @num_messages gives the number of #GSocketControlMessage instances
    6174                 :            :  * in @messages (ie: not including the %NULL terminator).
    6175                 :            :  *
    6176                 :            :  * @flags is an in/out parameter. The commonly available arguments
    6177                 :            :  * for this are available in the #GSocketMsgFlags enum, but the
    6178                 :            :  * values there are the same as the system values, and the flags
    6179                 :            :  * are passed in as-is, so you can pass in system-specific flags too
    6180                 :            :  * (and g_socket_receive_message() may pass system-specific flags out).
    6181                 :            :  * Flags passed in to the parameter affect the receive operation; flags returned
    6182                 :            :  * out of it are relevant to the specific returned message.
    6183                 :            :  *
    6184                 :            :  * As with g_socket_receive(), data may be discarded if @socket is
    6185                 :            :  * %G_SOCKET_TYPE_DATAGRAM or %G_SOCKET_TYPE_SEQPACKET and you do not
    6186                 :            :  * provide enough buffer space to read a complete message. You can pass
    6187                 :            :  * %G_SOCKET_MSG_PEEK in @flags to peek at the current message without
    6188                 :            :  * removing it from the receive queue, but there is no portable way to find
    6189                 :            :  * out the length of the message other than by reading it into a
    6190                 :            :  * sufficiently-large buffer.
    6191                 :            :  *
    6192                 :            :  * If the socket is in blocking mode the call will block until there
    6193                 :            :  * is some data to receive, the connection is closed, or there is an
    6194                 :            :  * error. If there is no data available and the socket is in
    6195                 :            :  * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be
    6196                 :            :  * returned. To be notified when data is available, wait for the
    6197                 :            :  * %G_IO_IN condition.
    6198                 :            :  *
    6199                 :            :  * On error -1 is returned and @error is set accordingly.
    6200                 :            :  *
    6201                 :            :  * Returns: Number of bytes read, or 0 if the connection was closed by
    6202                 :            :  * the peer, or -1 on error
    6203                 :            :  *
    6204                 :            :  * Since: 2.22
    6205                 :            :  */
    6206                 :            : gssize
    6207                 :      55904 : g_socket_receive_message (GSocket                 *socket,
    6208                 :            :                           GSocketAddress         **address,
    6209                 :            :                           GInputVector            *vectors,
    6210                 :            :                           gint                     num_vectors,
    6211                 :            :                           GSocketControlMessage ***messages,
    6212                 :            :                           gint                    *num_messages,
    6213                 :            :                           gint                    *flags,
    6214                 :            :                           GCancellable            *cancellable,
    6215                 :            :                           GError                 **error)
    6216                 :            : {
    6217                 :      55904 :   return g_socket_receive_message_with_timeout (socket, address, vectors,
    6218                 :            :                                                  num_vectors, messages,
    6219                 :            :                                                  num_messages, flags,
    6220         [ +  + ]:      55904 :                                                  socket->priv->blocking ? -1 : 0,
    6221                 :            :                                                  cancellable, error);
    6222                 :            : }
    6223                 :            : 
    6224                 :            : /**
    6225                 :            :  * g_socket_get_credentials:
    6226                 :            :  * @socket: a #GSocket.
    6227                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    6228                 :            :  *
    6229                 :            :  * Returns the credentials of the foreign process connected to this
    6230                 :            :  * socket, if any (e.g. it is only supported for %G_SOCKET_FAMILY_UNIX
    6231                 :            :  * sockets).
    6232                 :            :  *
    6233                 :            :  * If this operation isn't supported on the OS, the method fails with
    6234                 :            :  * the %G_IO_ERROR_NOT_SUPPORTED error. On Linux this is implemented
    6235                 :            :  * by reading the %SO_PEERCRED option on the underlying socket.
    6236                 :            :  *
    6237                 :            :  * This method can be expected to be available on the following platforms:
    6238                 :            :  *
    6239                 :            :  * - Linux since GLib 2.26
    6240                 :            :  * - OpenBSD since GLib 2.30
    6241                 :            :  * - Solaris, Illumos and OpenSolaris since GLib 2.40
    6242                 :            :  * - NetBSD since GLib 2.42
    6243                 :            :  * - macOS, tvOS, iOS since GLib 2.66
    6244                 :            :  *
    6245                 :            :  * Other ways to obtain credentials from a foreign peer includes the
    6246                 :            :  * #GUnixCredentialsMessage type and
    6247                 :            :  * g_unix_connection_send_credentials() /
    6248                 :            :  * g_unix_connection_receive_credentials() functions.
    6249                 :            :  *
    6250                 :            :  * Returns: (transfer full): %NULL if @error is set, otherwise a #GCredentials object
    6251                 :            :  * that must be freed with g_object_unref().
    6252                 :            :  *
    6253                 :            :  * Since: 2.26
    6254                 :            :  */
    6255                 :            : GCredentials *
    6256                 :        226 : g_socket_get_credentials (GSocket   *socket,
    6257                 :            :                           GError   **error)
    6258                 :            : {
    6259                 :            :   GCredentials *ret;
    6260                 :            : 
    6261                 :        226 :   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
    6262                 :        226 :   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
    6263                 :            : 
    6264         [ -  + ]:        226 :   if (!check_socket (socket, error))
    6265                 :          0 :     return NULL;
    6266                 :            : 
    6267                 :        226 :   ret = NULL;
    6268                 :            : 
    6269                 :            : #if G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED
    6270                 :            : 
    6271                 :            : #ifdef SO_PEERCRED
    6272                 :            :   {
    6273                 :            :     guint8 native_creds_buf[G_CREDENTIALS_NATIVE_SIZE];
    6274                 :        226 :     socklen_t optlen = sizeof (native_creds_buf);
    6275                 :            : 
    6276         [ +  - ]:        226 :     if (getsockopt (socket->priv->fd,
    6277                 :            :                     SOL_SOCKET,
    6278                 :            :                     SO_PEERCRED,
    6279                 :            :                     native_creds_buf,
    6280                 :            :                     &optlen) == 0)
    6281                 :            :       {
    6282                 :        226 :         ret = g_credentials_new ();
    6283                 :        226 :         g_credentials_set_native (ret,
    6284                 :            :                                   G_CREDENTIALS_NATIVE_TYPE,
    6285                 :            :                                   native_creds_buf);
    6286                 :            :       }
    6287                 :            :   }
    6288                 :            : #elif G_CREDENTIALS_USE_APPLE_XUCRED
    6289                 :            :   {
    6290                 :            :     struct xucred cred;
    6291                 :            :     socklen_t optlen = sizeof (cred);
    6292                 :            : 
    6293                 :            :     if (getsockopt (socket->priv->fd,
    6294                 :            :                     SOL_LOCAL,
    6295                 :            :                     LOCAL_PEERCRED,
    6296                 :            :                     &cred,
    6297                 :            :                     &optlen) == 0
    6298                 :            :         && optlen != 0)
    6299                 :            :       {
    6300                 :            :         if (cred.cr_version == XUCRED_VERSION)
    6301                 :            :           {
    6302                 :            :             pid_t pid;
    6303                 :            :             socklen_t optlen = sizeof (pid);
    6304                 :            : 
    6305                 :            :             ret = g_credentials_new ();
    6306                 :            :             g_credentials_set_native (ret,
    6307                 :            :                                       G_CREDENTIALS_NATIVE_TYPE,
    6308                 :            :                                       &cred);
    6309                 :            : 
    6310                 :            : #ifdef LOCAL_PEERPID
    6311                 :            :             if (getsockopt (socket->priv->fd,
    6312                 :            :                             SOL_LOCAL,
    6313                 :            :                             LOCAL_PEERPID,
    6314                 :            :                             &pid,
    6315                 :            :                             &optlen) == 0)
    6316                 :            :               _g_credentials_set_local_peerid (ret, pid);
    6317                 :            : #endif
    6318                 :            :           }
    6319                 :            :         else
    6320                 :            :           {
    6321                 :            :             g_set_error (error,
    6322                 :            :                          G_IO_ERROR,
    6323                 :            :                          G_IO_ERROR_NOT_SUPPORTED,
    6324                 :            :                          /* No point in translating this! */
    6325                 :            :                          "struct xucred cr_version %u != %u",
    6326                 :            :                          cred.cr_version, XUCRED_VERSION);
    6327                 :            :             /* Reuse a translatable string we already have */
    6328                 :            :             g_prefix_error (error,
    6329                 :            :                             _("Unable to read socket credentials: %s"),
    6330                 :            :                             "");
    6331                 :            : 
    6332                 :            :             return NULL;
    6333                 :            :           }
    6334                 :            :       }
    6335                 :            :     else if (optlen == 0 || errno == EINVAL)
    6336                 :            :       {
    6337                 :            :         g_set_error (error,
    6338                 :            :                      G_IO_ERROR,
    6339                 :            :                      G_IO_ERROR_NOT_SUPPORTED,
    6340                 :            :                      _("Unable to read socket credentials: %s"),
    6341                 :            :                      "unsupported socket type");
    6342                 :            :         return NULL;
    6343                 :            :       }
    6344                 :            :   }
    6345                 :            : #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
    6346                 :            :   {
    6347                 :            :     struct unpcbid cred;
    6348                 :            :     socklen_t optlen = sizeof (cred);
    6349                 :            : 
    6350                 :            :     if (getsockopt (socket->priv->fd,
    6351                 :            :                     0,
    6352                 :            :                     LOCAL_PEEREID,
    6353                 :            :                     &cred,
    6354                 :            :                     &optlen) == 0)
    6355                 :            :       {
    6356                 :            :         ret = g_credentials_new ();
    6357                 :            :         g_credentials_set_native (ret,
    6358                 :            :                                   G_CREDENTIALS_NATIVE_TYPE,
    6359                 :            :                                   &cred);
    6360                 :            :       }
    6361                 :            :   }
    6362                 :            : #elif G_CREDENTIALS_USE_SOLARIS_UCRED
    6363                 :            :   {
    6364                 :            :     ucred_t *ucred = NULL;
    6365                 :            : 
    6366                 :            :     if (getpeerucred (socket->priv->fd, &ucred) == 0)
    6367                 :            :       {
    6368                 :            :         ret = g_credentials_new ();
    6369                 :            :         g_credentials_set_native (ret,
    6370                 :            :                                   G_CREDENTIALS_TYPE_SOLARIS_UCRED,
    6371                 :            :                                   ucred);
    6372                 :            :         ucred_free (ucred);
    6373                 :            :       }
    6374                 :            :   }
    6375                 :            : #elif G_CREDENTIALS_USE_WIN32_PID
    6376                 :            :   {
    6377                 :            :     DWORD peerid, drc;
    6378                 :            : 
    6379                 :            :     if (WSAIoctl (socket->priv->fd, SIO_AF_UNIX_GETPEERPID,
    6380                 :            :                   NULL, 0U,
    6381                 :            :                   &peerid, sizeof(peerid),
    6382                 :            :                   /* Windows bug: always 0 https://github.com/microsoft/WSL/issues/4676 */
    6383                 :            :                   &drc,
    6384                 :            :                   NULL, NULL) == 0)
    6385                 :            :       {
    6386                 :            :         ret = g_credentials_new ();
    6387                 :            :         g_credentials_set_native (ret,
    6388                 :            :                                   G_CREDENTIALS_TYPE_WIN32_PID,
    6389                 :            :                                   &peerid);
    6390                 :            :       }
    6391                 :            :   }
    6392                 :            : #else
    6393                 :            :   #error "G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED is set but this is no code for this platform"
    6394                 :            : #endif
    6395                 :            : 
    6396         [ -  + ]:        226 :   if (!ret)
    6397                 :            :     {
    6398                 :          0 :       int errsv = get_socket_errno ();
    6399                 :            : 
    6400                 :          0 :       g_set_error (error,
    6401                 :            :                    G_IO_ERROR,
    6402                 :          0 :                    socket_io_error_from_errno (errsv),
    6403                 :            :                    _("Unable to read socket credentials: %s"),
    6404                 :            :                    socket_strerror (errsv));
    6405                 :            :     }
    6406                 :            : 
    6407                 :            : #else
    6408                 :            : 
    6409                 :            :   g_set_error_literal (error,
    6410                 :            :                        G_IO_ERROR,
    6411                 :            :                        G_IO_ERROR_NOT_SUPPORTED,
    6412                 :            :                        _("g_socket_get_credentials not implemented for this OS"));
    6413                 :            : #endif
    6414                 :            : 
    6415                 :        226 :   return ret;
    6416                 :            : }
    6417                 :            : 
    6418                 :            : /**
    6419                 :            :  * g_socket_get_option:
    6420                 :            :  * @socket: a #GSocket
    6421                 :            :  * @level: the "API level" of the option (eg, `SOL_SOCKET`)
    6422                 :            :  * @optname: the "name" of the option (eg, `SO_BROADCAST`)
    6423                 :            :  * @value: (out): return location for the option value
    6424                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    6425                 :            :  *
    6426                 :            :  * Gets the value of an integer-valued option on @socket, as with
    6427                 :            :  * getsockopt(). (If you need to fetch a  non-integer-valued option,
    6428                 :            :  * you will need to call getsockopt() directly.)
    6429                 :            :  *
    6430                 :            :  * The [<gio/gnetworking.h>][gio-gnetworking.h]
    6431                 :            :  * header pulls in system headers that will define most of the
    6432                 :            :  * standard/portable socket options. For unusual socket protocols or
    6433                 :            :  * platform-dependent options, you may need to include additional
    6434                 :            :  * headers.
    6435                 :            :  *
    6436                 :            :  * Note that even for socket options that are a single byte in size,
    6437                 :            :  * @value is still a pointer to a #gint variable, not a #guchar;
    6438                 :            :  * g_socket_get_option() will handle the conversion internally.
    6439                 :            :  *
    6440                 :            :  * Returns: success or failure. On failure, @error will be set, and
    6441                 :            :  *   the system error value (`errno` or WSAGetLastError()) will still
    6442                 :            :  *   be set to the result of the getsockopt() call.
    6443                 :            :  *
    6444                 :            :  * Since: 2.36
    6445                 :            :  */
    6446                 :            : gboolean
    6447                 :        720 : g_socket_get_option (GSocket  *socket,
    6448                 :            :                      gint      level,
    6449                 :            :                      gint      optname,
    6450                 :            :                      gint     *value,
    6451                 :            :                      GError  **error)
    6452                 :            : {
    6453                 :            :   socklen_t size;
    6454                 :            : 
    6455                 :        720 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    6456                 :            : 
    6457                 :            :   /* g_socket_get_option() is called during socket init, so skip the init checks
    6458                 :            :    * in check_socket() */
    6459   [ +  +  -  + ]:        720 :   if (socket->priv->inited && !check_socket (socket, error))
    6460                 :          0 :     return FALSE;
    6461                 :            : 
    6462                 :        720 :   *value = 0;
    6463                 :        720 :   size = sizeof (gint);
    6464         [ -  + ]:        720 :   if (getsockopt (socket->priv->fd, level, optname, value, &size) != 0)
    6465                 :            :     {
    6466                 :          0 :       int errsv = get_socket_errno ();
    6467                 :            : 
    6468                 :          0 :       g_set_error_literal (error,
    6469                 :            :                            G_IO_ERROR,
    6470                 :          0 :                            socket_io_error_from_errno (errsv),
    6471                 :          0 :                            socket_strerror (errsv));
    6472                 :            : #ifndef G_OS_WIN32
    6473                 :            :       /* Reset errno in case the caller wants to look at it */
    6474                 :          0 :       errno = errsv;
    6475                 :            : #endif
    6476                 :          0 :       return FALSE;
    6477                 :            :     }
    6478                 :            : 
    6479                 :            : #if G_BYTE_ORDER == G_BIG_ENDIAN
    6480                 :            :   /* If the returned value is smaller than an int then we need to
    6481                 :            :    * slide it over into the low-order bytes of *value.
    6482                 :            :    */
    6483                 :            :   if (size != sizeof (gint))
    6484                 :            :     *value = *value >> (8 * (sizeof (gint) - size));
    6485                 :            : #endif
    6486                 :            : 
    6487                 :        720 :   return TRUE;
    6488                 :            : }
    6489                 :            : 
    6490                 :            : /**
    6491                 :            :  * g_socket_set_option:
    6492                 :            :  * @socket: a #GSocket
    6493                 :            :  * @level: the "API level" of the option (eg, `SOL_SOCKET`)
    6494                 :            :  * @optname: the "name" of the option (eg, `SO_BROADCAST`)
    6495                 :            :  * @value: the value to set the option to
    6496                 :            :  * @error: #GError for error reporting, or %NULL to ignore.
    6497                 :            :  *
    6498                 :            :  * Sets the value of an integer-valued option on @socket, as with
    6499                 :            :  * setsockopt(). (If you need to set a non-integer-valued option,
    6500                 :            :  * you will need to call setsockopt() directly.)
    6501                 :            :  *
    6502                 :            :  * The [<gio/gnetworking.h>][gio-gnetworking.h]
    6503                 :            :  * header pulls in system headers that will define most of the
    6504                 :            :  * standard/portable socket options. For unusual socket protocols or
    6505                 :            :  * platform-dependent options, you may need to include additional
    6506                 :            :  * headers.
    6507                 :            :  *
    6508                 :            :  * Returns: success or failure. On failure, @error will be set, and
    6509                 :            :  *   the system error value (`errno` or WSAGetLastError()) will still
    6510                 :            :  *   be set to the result of the setsockopt() call.
    6511                 :            :  *
    6512                 :            :  * Since: 2.36
    6513                 :            :  */
    6514                 :            : gboolean
    6515                 :       1794 : g_socket_set_option (GSocket  *socket,
    6516                 :            :                      gint      level,
    6517                 :            :                      gint      optname,
    6518                 :            :                      gint      value,
    6519                 :            :                      GError  **error)
    6520                 :            : {
    6521                 :            :   gint errsv;
    6522                 :            : 
    6523                 :       1794 :   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
    6524                 :            : 
    6525                 :            :   /* g_socket_set_option() is called during socket init, so skip the init checks
    6526                 :            :    * in check_socket() */
    6527   [ +  +  -  + ]:       1794 :   if (socket->priv->inited && !check_socket (socket, error))
    6528                 :          0 :     return FALSE;
    6529                 :            : 
    6530         [ +  + ]:       1794 :   if (setsockopt (socket->priv->fd, level, optname, &value, sizeof (gint)) == 0)
    6531                 :        379 :     return TRUE;
    6532                 :            : 
    6533                 :            : #if !defined (__linux__) && !defined (G_OS_WIN32)
    6534                 :            :   /* Linux and Windows let you set a single-byte value from an int,
    6535                 :            :    * but most other platforms don't.
    6536                 :            :    */
    6537                 :            :   if (errno == EINVAL && value >= SCHAR_MIN && value <= CHAR_MAX)
    6538                 :            :     {
    6539                 :            : #if G_BYTE_ORDER == G_BIG_ENDIAN
    6540                 :            :       value = value << (8 * (sizeof (gint) - 1));
    6541                 :            : #endif
    6542                 :            :       if (setsockopt (socket->priv->fd, level, optname, &value, 1) == 0)
    6543                 :            :         return TRUE;
    6544                 :            :     }
    6545                 :            : #endif
    6546                 :            : 
    6547                 :       1415 :   errsv = get_socket_errno ();
    6548                 :            : 
    6549                 :       1415 :   g_set_error_literal (error,
    6550                 :            :                        G_IO_ERROR,
    6551                 :       1415 :                        socket_io_error_from_errno (errsv),
    6552                 :       1415 :                        socket_strerror (errsv));
    6553                 :            : #ifndef G_OS_WIN32
    6554                 :       1415 :   errno = errsv;
    6555                 :            : #endif
    6556                 :       1415 :   return FALSE;
    6557                 :            : }

Generated by: LCOV version 1.14