Coverage Report

Created: 2025-11-16 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib-2.80.0/gio/gsocket.c
Line
Count
Source
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
0
#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
0
_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
get_socket_errno (void)
302
0
{
303
0
#ifndef G_OS_WIN32
304
0
  return errno;
305
#else
306
  return WSAGetLastError ();
307
#endif
308
0
}
309
310
static GIOErrorEnum
311
socket_io_error_from_errno (int err)
312
0
{
313
#ifdef G_OS_WIN32
314
  return g_io_error_from_win32_error (err);
315
#else
316
0
  return g_io_error_from_errno (err);
317
0
#endif
318
0
}
319
320
static const char *
321
socket_strerror (int err)
322
0
{
323
0
#ifndef G_OS_WIN32
324
0
  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
0
}
337
338
/* Wrapper around g_set_error() to avoid doing excess work */
339
#define socket_set_error_lazy(err, errsv, fmt)                          \
340
0
  G_STMT_START {                                                        \
341
0
    GError **__err = (err);                                             \
342
0
    int __errsv = (errsv);                                              \
343
0
                                                                        \
344
0
    if (__err)                                                          \
345
0
      {                                                                 \
346
0
        int __code = socket_io_error_from_errno (__errsv);              \
347
0
        const char *__strerr = socket_strerror (__errsv);               \
348
0
                                                                        \
349
0
        if (__code == G_IO_ERROR_WOULD_BLOCK)                           \
350
0
          g_set_error_literal (__err, G_IO_ERROR, __code, __strerr);    \
351
0
        else                                                            \
352
0
          g_set_error (__err, G_IO_ERROR, __code, fmt, __strerr);       \
353
0
      }                                                                 \
354
0
  } 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
address_to_string (GSocketAddress *address)
386
0
{
387
0
  GString *ret = g_string_new ("");
388
389
0
  if (G_IS_INET_SOCKET_ADDRESS (address))
390
0
    {
391
0
      GInetSocketAddress *isa = G_INET_SOCKET_ADDRESS (address);
392
0
      GInetAddress *ia = g_inet_socket_address_get_address (isa);
393
0
      GSocketFamily family = g_inet_address_get_family (ia);
394
0
      gchar *tmp;
395
396
      /* Represent IPv6 addresses in URL style:
397
       * ::1 port 12345 -> [::1]:12345 */
398
0
      if (family == G_SOCKET_FAMILY_IPV6)
399
0
        g_string_append_c (ret, '[');
400
401
0
      tmp = g_inet_address_to_string (ia);
402
0
      g_string_append (ret, tmp);
403
0
      g_free (tmp);
404
405
0
      if (family == G_SOCKET_FAMILY_IPV6)
406
0
        {
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
0
          g_string_append_c (ret, ']');
413
0
        }
414
415
0
      g_string_append_c (ret, ':');
416
417
0
      g_string_append_printf (ret, "%u", g_inet_socket_address_get_port (isa));
418
0
    }
419
0
  else
420
0
    {
421
      /* For unknown address types, just show the type */
422
0
      g_string_append_printf (ret, "(%s)", G_OBJECT_TYPE_NAME (address));
423
0
    }
424
425
0
  return g_string_free (ret, FALSE);
426
0
}
427
428
static gboolean
429
check_socket (GSocket *socket,
430
        GError **error)
431
0
{
432
0
  if (!socket->priv->inited)
433
0
    {
434
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
435
0
                           _("Invalid socket, not initialized"));
436
0
      return FALSE;
437
0
    }
438
439
0
  if (socket->priv->construct_error)
440
0
    {
441
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
442
0
       _("Invalid socket, initialization failed due to: %s"),
443
0
       socket->priv->construct_error->message);
444
0
      return FALSE;
445
0
    }
446
447
0
  if (socket->priv->closed)
448
0
    {
449
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
450
0
         _("Socket is already closed"));
451
0
      return FALSE;
452
0
    }
453
454
0
  return TRUE;
455
0
}
456
457
static gboolean
458
check_timeout (GSocket *socket,
459
         GError **error)
460
0
{
461
0
  if (socket->priv->timed_out)
462
0
    {
463
0
      socket->priv->timed_out = FALSE;
464
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
465
0
         _("Socket I/O timed out"));
466
0
      return FALSE;
467
0
    }
468
469
0
  return TRUE;
470
0
}
471
472
static void
473
g_socket_details_from_fd (GSocket *socket)
474
0
{
475
0
  union {
476
0
    struct sockaddr_storage storage;
477
0
    struct sockaddr sa;
478
0
  } address;
479
0
  gint fd;
480
0
  socklen_t addrlen;
481
0
  int value, family;
482
0
  int errsv;
483
484
0
  memset (&address, 0, sizeof (address));
485
486
0
  fd = socket->priv->fd;
487
0
  if (!g_socket_get_option (socket, SOL_SOCKET, SO_TYPE, &value, NULL))
488
0
    {
489
0
      errsv = get_socket_errno ();
490
0
      goto err;
491
0
    }
492
493
0
  switch (value)
494
0
    {
495
0
     case SOCK_STREAM:
496
0
      socket->priv->type = G_SOCKET_TYPE_STREAM;
497
0
      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
0
     default:
508
0
      socket->priv->type = G_SOCKET_TYPE_INVALID;
509
0
      break;
510
0
    }
511
512
0
  addrlen = sizeof address;
513
0
  if (getsockname (fd, &address.sa, &addrlen) != 0)
514
0
    {
515
0
      errsv = get_socket_errno ();
516
0
      goto err;
517
0
    }
518
519
0
  if (addrlen > 0)
520
0
    {
521
0
      g_assert (G_STRUCT_OFFSET (struct sockaddr, sa_family) +
522
0
    (socklen_t) sizeof address.storage.ss_family <= addrlen);
523
0
      family = address.storage.ss_family;
524
0
    }
525
0
  else
526
0
    {
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
0
#ifdef SO_DOMAIN
531
0
      if (!g_socket_get_option (socket, SOL_SOCKET, SO_DOMAIN, &family, NULL))
532
0
  {
533
0
    errsv = get_socket_errno ();
534
0
    goto err;
535
0
  }
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
0
    }
542
543
0
  switch (family)
544
0
    {
545
0
     case G_SOCKET_FAMILY_IPV4:
546
0
     case G_SOCKET_FAMILY_IPV6:
547
0
       socket->priv->family = address.storage.ss_family;
548
0
       switch (socket->priv->type)
549
0
   {
550
0
   case G_SOCKET_TYPE_STREAM:
551
0
     socket->priv->protocol = G_SOCKET_PROTOCOL_TCP;
552
0
     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
0
   }
565
0
       break;
566
567
0
     case G_SOCKET_FAMILY_UNIX:
568
0
       socket->priv->family = G_SOCKET_FAMILY_UNIX;
569
0
       socket->priv->protocol = G_SOCKET_PROTOCOL_DEFAULT;
570
0
       break;
571
572
0
     default:
573
0
       socket->priv->family = G_SOCKET_FAMILY_INVALID;
574
0
       break;
575
0
    }
576
577
0
  if (socket->priv->family != G_SOCKET_FAMILY_INVALID)
578
0
    {
579
0
      addrlen = sizeof address;
580
0
      if (getpeername (fd, &address.sa, &addrlen) >= 0)
581
0
        {
582
0
          socket->priv->connected_read = TRUE;
583
0
          socket->priv->connected_write = TRUE;
584
0
        }
585
0
    }
586
587
0
  if (g_socket_get_option (socket, SOL_SOCKET, SO_KEEPALIVE, &value, NULL))
588
0
    {
589
0
      socket->priv->keepalive = !!value;
590
0
    }
591
0
  else
592
0
    {
593
      /* Can't read, maybe not supported, assume FALSE */
594
0
      socket->priv->keepalive = FALSE;
595
0
    }
596
597
0
  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
0
         _("creating GSocket from fd: %s"),
603
0
         socket_strerror (errsv));
604
0
}
605
606
static void
607
socket_set_nonblock (int fd)
608
0
{
609
0
#ifndef G_OS_WIN32
610
0
  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
0
#ifndef G_OS_WIN32
620
0
  if (!g_unix_set_fd_nonblocking (fd, TRUE, &error))
621
0
    {
622
0
      g_warning ("Error setting socket to nonblocking mode: %s", error->message);
623
0
      g_clear_error (&error);
624
0
    }
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
0
}
635
636
/* Wrapper around socket() that is shared with gnetworkmonitornetlink.c.
637
 * It always sets SOCK_CLOEXEC | SOCK_NONBLOCK. */
638
gint
639
g_socket (gint     domain,
640
          gint     type,
641
          gint     protocol,
642
          GError **error)
643
0
{
644
0
  int fd, errsv;
645
646
0
#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
647
0
  fd = socket (domain, type | SOCK_CLOEXEC | SOCK_NONBLOCK, protocol);
648
0
  errsv = errno;
649
0
  if (fd != -1)
650
0
    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
0
#endif
655
0
    fd = socket (domain, type, protocol);
656
657
0
  if (fd < 0)
658
0
    {
659
0
      errsv = get_socket_errno ();
660
661
0
      g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
662
0
       _("Unable to create socket: %s"), socket_strerror (errsv));
663
0
      errno = errsv;
664
0
      return -1;
665
0
    }
666
667
0
#ifndef G_OS_WIN32
668
0
  {
669
0
    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
0
      {
678
0
  flags |= FD_CLOEXEC;
679
0
  (void) fcntl (fd, F_SETFD, flags);
680
0
      }
681
0
  }
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
0
}
699
700
/* Returned socket has SOCK_CLOEXEC | SOCK_NONBLOCK set. */
701
static gint
702
g_socket_create_socket (GSocketFamily   family,
703
      GSocketType     type,
704
      int             protocol,
705
      GError        **error)
706
0
{
707
0
  gint native_type;
708
709
0
  switch (type)
710
0
    {
711
0
     case G_SOCKET_TYPE_STREAM:
712
0
      native_type = SOCK_STREAM;
713
0
      break;
714
715
0
     case G_SOCKET_TYPE_DATAGRAM:
716
0
      native_type = SOCK_DGRAM;
717
0
      break;
718
719
0
     case G_SOCKET_TYPE_SEQPACKET:
720
0
      native_type = SOCK_SEQPACKET;
721
0
      break;
722
723
0
     default:
724
0
      g_assert_not_reached ();
725
0
    }
726
727
0
  if (family <= 0)
728
0
    {
729
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
730
0
                   _("Unable to create socket: %s"), _("Unknown family was specified"));
731
0
      return -1;
732
0
    }
733
734
0
  if (protocol == -1)
735
0
    {
736
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
737
0
       _("Unable to create socket: %s"), _("Unknown protocol was specified"));
738
0
      return -1;
739
0
    }
740
741
0
  return g_socket (family, native_type, protocol, error);
742
0
}
743
744
static void
745
g_socket_constructed (GObject *object)
746
0
{
747
0
  GSocket *socket = G_SOCKET (object);
748
749
0
  if (socket->priv->fd >= 0)
750
0
    {
751
      /* create socket->priv info from the fd and ensure it’s non-blocking */
752
0
      g_socket_details_from_fd (socket);
753
0
      socket_set_nonblock (socket->priv->fd);
754
0
    }
755
0
  else
756
0
    {
757
      /* create the fd from socket->priv info; this sets it non-blocking by construction */
758
0
      socket->priv->fd = g_socket_create_socket (socket->priv->family,
759
0
                   socket->priv->type,
760
0
                   socket->priv->protocol,
761
0
                   &socket->priv->construct_error);
762
0
    }
763
764
0
  if (socket->priv->fd != -1)
765
0
    {
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
0
      if (socket->priv->type == G_SOCKET_TYPE_STREAM)
771
0
        g_socket_set_option (socket, IPPROTO_TCP, TCP_NODELAY, TRUE, NULL);
772
0
    }
773
0
}
774
775
static void
776
g_socket_get_property (GObject    *object,
777
           guint       prop_id,
778
           GValue     *value,
779
           GParamSpec *pspec)
780
0
{
781
0
  GSocket *socket = G_SOCKET (object);
782
0
  GSocketAddress *address;
783
784
0
  switch (prop_id)
785
0
    {
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
0
    }
847
0
}
848
849
static void
850
g_socket_set_property (GObject      *object,
851
           guint         prop_id,
852
           const GValue *value,
853
           GParamSpec   *pspec)
854
0
{
855
0
  GSocket *socket = G_SOCKET (object);
856
857
0
  switch (prop_id)
858
0
    {
859
0
      case PROP_FAMILY:
860
0
  socket->priv->family = g_value_get_enum (value);
861
0
  break;
862
863
0
      case PROP_TYPE:
864
0
  socket->priv->type = g_value_get_enum (value);
865
0
  break;
866
867
0
      case PROP_PROTOCOL:
868
0
  socket->priv->protocol = g_value_get_enum (value);
869
0
  break;
870
871
0
      case PROP_FD:
872
0
  socket->priv->fd = g_value_get_int (value);
873
0
  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
0
    }
910
0
}
911
912
static void
913
g_socket_finalize (GObject *object)
914
0
{
915
0
  GSocket *socket = G_SOCKET (object);
916
0
  gint i;
917
918
0
  g_clear_error (&socket->priv->construct_error);
919
920
0
  if (socket->priv->fd != -1 &&
921
0
      !socket->priv->closed)
922
0
    g_socket_close (socket, NULL);
923
924
0
  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
0
  for (i = 0; i < RECV_ADDR_CACHE_SIZE; i++)
940
0
    {
941
0
      if (socket->priv->recv_addr_cache[i].addr)
942
0
        {
943
0
          g_object_unref (socket->priv->recv_addr_cache[i].addr);
944
0
          g_free (socket->priv->recv_addr_cache[i].native);
945
0
        }
946
0
    }
947
948
0
  if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
949
0
    (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
950
0
}
951
952
static void
953
g_socket_class_init (GSocketClass *klass)
954
0
{
955
0
  GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
956
957
0
#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
0
  signal (SIGPIPE, SIG_IGN);
968
0
#endif
969
970
0
  gobject_class->finalize = g_socket_finalize;
971
0
  gobject_class->constructed = g_socket_constructed;
972
0
  gobject_class->set_property = g_socket_set_property;
973
0
  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
0
  g_object_class_install_property (gobject_class, PROP_FAMILY,
983
0
           g_param_spec_enum ("family", NULL, NULL,
984
0
                  G_TYPE_SOCKET_FAMILY,
985
0
                  G_SOCKET_FAMILY_INVALID,
986
0
                  G_PARAM_CONSTRUCT_ONLY |
987
0
                                                      G_PARAM_READWRITE |
988
0
                                                      G_PARAM_STATIC_STRINGS));
989
990
  /**
991
   * GSocket:type:
992
   *
993
   * The socket’s type.
994
   *
995
   * Since: 2.22
996
   */
997
0
  g_object_class_install_property (gobject_class, PROP_TYPE,
998
0
           g_param_spec_enum ("type", NULL, NULL,
999
0
                  G_TYPE_SOCKET_TYPE,
1000
0
                  G_SOCKET_TYPE_STREAM,
1001
0
                  G_PARAM_CONSTRUCT_ONLY |
1002
0
                                                      G_PARAM_READWRITE |
1003
0
                                                      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
0
  g_object_class_install_property (gobject_class, PROP_PROTOCOL,
1013
0
           g_param_spec_enum ("protocol", NULL, NULL,
1014
0
                  G_TYPE_SOCKET_PROTOCOL,
1015
0
                  G_SOCKET_PROTOCOL_UNKNOWN,
1016
0
                  G_PARAM_CONSTRUCT_ONLY |
1017
0
                                                      G_PARAM_READWRITE |
1018
0
                                                      G_PARAM_STATIC_STRINGS));
1019
1020
  /**
1021
   * GSocket:fd:
1022
   *
1023
   * The socket’s file descriptor.
1024
   *
1025
   * Since: 2.22
1026
   */
1027
0
  g_object_class_install_property (gobject_class, PROP_FD,
1028
0
           g_param_spec_int ("fd", NULL, NULL,
1029
0
                 G_MININT,
1030
0
                 G_MAXINT,
1031
0
                 -1,
1032
0
                 G_PARAM_CONSTRUCT_ONLY |
1033
0
                                                     G_PARAM_READWRITE |
1034
0
                                                     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
0
  g_object_class_install_property (gobject_class, PROP_BLOCKING,
1044
0
           g_param_spec_boolean ("blocking", NULL, NULL,
1045
0
               TRUE,
1046
0
               G_PARAM_READWRITE |
1047
0
                                                         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
0
  g_object_class_install_property (gobject_class, PROP_LISTEN_BACKLOG,
1057
0
           g_param_spec_int ("listen-backlog", NULL, NULL,
1058
0
                 0,
1059
0
                 SOMAXCONN,
1060
0
                 10,
1061
0
                 G_PARAM_READWRITE |
1062
0
                                                     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
0
  g_object_class_install_property (gobject_class, PROP_KEEPALIVE,
1072
0
           g_param_spec_boolean ("keepalive", NULL, NULL,
1073
0
               FALSE,
1074
0
               G_PARAM_READWRITE |
1075
0
                                                         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
0
  g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
1085
0
           g_param_spec_object ("local-address", NULL, NULL,
1086
0
              G_TYPE_SOCKET_ADDRESS,
1087
0
              G_PARAM_READABLE |
1088
0
                                                        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
0
  g_object_class_install_property (gobject_class, PROP_REMOTE_ADDRESS,
1098
0
           g_param_spec_object ("remote-address", NULL, NULL,
1099
0
              G_TYPE_SOCKET_ADDRESS,
1100
0
              G_PARAM_READABLE |
1101
0
                                                        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
0
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
1111
0
           g_param_spec_uint ("timeout", NULL, NULL,
1112
0
                  0,
1113
0
                  G_MAXUINT,
1114
0
                  0,
1115
0
                  G_PARAM_READWRITE |
1116
0
                  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
0
  g_object_class_install_property (gobject_class, PROP_BROADCAST,
1126
0
           g_param_spec_boolean ("broadcast", NULL, NULL,
1127
0
               FALSE,
1128
0
               G_PARAM_READWRITE |
1129
0
                                                         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
0
  g_object_class_install_property (gobject_class, PROP_TTL,
1139
0
           g_param_spec_uint ("ttl", NULL, NULL,
1140
0
                  0, G_MAXUINT, 0,
1141
0
                  G_PARAM_READWRITE |
1142
0
                  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
0
  g_object_class_install_property (gobject_class, PROP_MULTICAST_LOOPBACK,
1152
0
           g_param_spec_boolean ("multicast-loopback", NULL, NULL,
1153
0
               TRUE,
1154
0
               G_PARAM_READWRITE |
1155
0
                                                         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
0
  g_object_class_install_property (gobject_class, PROP_MULTICAST_TTL,
1165
0
           g_param_spec_uint ("multicast-ttl", NULL, NULL,
1166
0
                  0, G_MAXUINT, 1,
1167
0
                  G_PARAM_READWRITE |
1168
0
                  G_PARAM_STATIC_STRINGS));
1169
0
}
1170
1171
static void
1172
g_socket_initable_iface_init (GInitableIface *iface)
1173
0
{
1174
0
  iface->init = g_socket_initable_init;
1175
0
}
1176
1177
static void
1178
g_socket_datagram_based_iface_init (GDatagramBasedInterface *iface)
1179
0
{
1180
0
  iface->receive_messages = g_socket_datagram_based_receive_messages;
1181
0
  iface->send_messages = g_socket_datagram_based_send_messages;
1182
0
  iface->create_source = g_socket_datagram_based_create_source;
1183
0
  iface->condition_check = g_socket_datagram_based_condition_check;
1184
0
  iface->condition_wait = g_socket_datagram_based_condition_wait;
1185
0
}
1186
1187
static void
1188
g_socket_init (GSocket *socket)
1189
0
{
1190
0
  socket->priv = g_socket_get_instance_private (socket);
1191
1192
0
  socket->priv->fd = -1;
1193
0
  socket->priv->blocking = TRUE;
1194
0
  socket->priv->listen_backlog = 10;
1195
0
  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
0
}
1202
1203
static gboolean
1204
g_socket_initable_init (GInitable *initable,
1205
      GCancellable *cancellable,
1206
      GError  **error)
1207
0
{
1208
0
  GSocket  *socket;
1209
1210
0
  g_return_val_if_fail (G_IS_SOCKET (initable), FALSE);
1211
1212
0
  socket = G_SOCKET (initable);
1213
1214
0
  if (cancellable != NULL)
1215
0
    {
1216
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1217
0
                           _("Cancellable initialization not supported"));
1218
0
      return FALSE;
1219
0
    }
1220
1221
0
  socket->priv->inited = TRUE;
1222
1223
0
  if (socket->priv->construct_error)
1224
0
    {
1225
0
      if (error)
1226
0
  *error = g_error_copy (socket->priv->construct_error);
1227
0
      return FALSE;
1228
0
    }
1229
1230
1231
0
  return TRUE;
1232
0
}
1233
1234
static gboolean
1235
check_datagram_based (GDatagramBased  *self,
1236
                      GError         **error)
1237
0
{
1238
0
  switch (g_socket_get_socket_type (G_SOCKET (self)))
1239
0
    {
1240
0
    case G_SOCKET_TYPE_INVALID:
1241
0
    case G_SOCKET_TYPE_STREAM:
1242
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1243
0
                   _("Cannot use datagram operations on a non-datagram "
1244
0
                     "socket."));
1245
0
      return FALSE;
1246
0
    case G_SOCKET_TYPE_DATAGRAM:
1247
0
    case G_SOCKET_TYPE_SEQPACKET:
1248
      /* Fall through. */
1249
0
      break;
1250
0
    }
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
0
    {
1259
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1260
0
                   _("Cannot use datagram operations on a socket with a "
1261
0
                     "timeout set."));
1262
0
      return FALSE;
1263
0
    }
1264
1265
0
  return TRUE;
1266
0
}
1267
1268
static gint
1269
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
0
{
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
0
                                                 num_messages, flags, timeout_us,
1282
0
                                                 cancellable, error);
1283
0
}
1284
1285
static gint
1286
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
0
{
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
0
                                              num_messages, flags, timeout_us,
1299
0
                                              cancellable, error);
1300
0
}
1301
1302
static GSource *
1303
g_socket_datagram_based_create_source (GDatagramBased  *self,
1304
                                       GIOCondition     condition,
1305
                                       GCancellable    *cancellable)
1306
0
{
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
0
}
1312
1313
static GIOCondition
1314
g_socket_datagram_based_condition_check (GDatagramBased  *datagram_based,
1315
                                         GIOCondition     condition)
1316
0
{
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
0
}
1322
1323
static gboolean
1324
g_socket_datagram_based_condition_wait (GDatagramBased  *datagram_based,
1325
                                        GIOCondition     condition,
1326
                                        gint64           timeout_us,
1327
                                        GCancellable    *cancellable,
1328
                                        GError         **error)
1329
0
{
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
0
                                        timeout_us, cancellable, error);
1335
0
}
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
g_socket_new (GSocketFamily     family,
1365
        GSocketType       type,
1366
        GSocketProtocol   protocol,
1367
        GError          **error)
1368
0
{
1369
0
  return G_SOCKET (g_initable_new (G_TYPE_SOCKET,
1370
0
           NULL, error,
1371
0
           "family", family,
1372
0
           "type", type,
1373
0
           "protocol", protocol,
1374
0
           NULL));
1375
0
}
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
g_socket_new_from_fd (gint     fd,
1403
          GError **error)
1404
0
{
1405
0
  return G_SOCKET (g_initable_new (G_TYPE_SOCKET,
1406
0
           NULL, error,
1407
0
           "fd", fd,
1408
0
           NULL));
1409
0
}
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
g_socket_set_blocking (GSocket  *socket,
1430
           gboolean  blocking)
1431
0
{
1432
0
  g_return_if_fail (G_IS_SOCKET (socket));
1433
1434
0
  blocking = !!blocking;
1435
1436
0
  if (socket->priv->blocking == blocking)
1437
0
    return;
1438
1439
0
  socket->priv->blocking = blocking;
1440
0
  g_object_notify (G_OBJECT (socket), "blocking");
1441
0
}
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
g_socket_get_blocking (GSocket *socket)
1456
0
{
1457
0
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
1458
1459
0
  return socket->priv->blocking;
1460
0
}
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
g_socket_set_keepalive (GSocket  *socket,
1487
      gboolean  keepalive)
1488
0
{
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
0
          keepalive, &error))
1499
0
    {
1500
0
      g_warning ("error setting keepalive: %s", error->message);
1501
0
      g_error_free (error);
1502
0
      return;
1503
0
    }
1504
1505
0
  socket->priv->keepalive = keepalive;
1506
0
  g_object_notify (G_OBJECT (socket), "keepalive");
1507
0
}
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
g_socket_get_keepalive (GSocket *socket)
1522
0
{
1523
0
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
1524
1525
0
  return socket->priv->keepalive;
1526
0
}
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
g_socket_get_listen_backlog  (GSocket *socket)
1541
0
{
1542
0
  g_return_val_if_fail (G_IS_SOCKET (socket), 0);
1543
1544
0
  return socket->priv->listen_backlog;
1545
0
}
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
g_socket_set_listen_backlog (GSocket *socket,
1564
           gint     backlog)
1565
0
{
1566
0
  g_return_if_fail (G_IS_SOCKET (socket));
1567
0
  g_return_if_fail (!socket->priv->listening);
1568
1569
0
  if (backlog != socket->priv->listen_backlog)
1570
0
    {
1571
0
      socket->priv->listen_backlog = backlog;
1572
0
      g_object_notify (G_OBJECT (socket), "listen-backlog");
1573
0
    }
1574
0
}
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
g_socket_get_timeout (GSocket *socket)
1589
0
{
1590
0
  g_return_val_if_fail (G_IS_SOCKET (socket), 0);
1591
1592
0
  return socket->priv->timeout;
1593
0
}
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
g_socket_set_timeout (GSocket *socket,
1625
          guint    timeout)
1626
0
{
1627
0
  g_return_if_fail (G_IS_SOCKET (socket));
1628
1629
0
  if (timeout != socket->priv->timeout)
1630
0
    {
1631
0
      socket->priv->timeout = timeout;
1632
0
      g_object_notify (G_OBJECT (socket), "timeout");
1633
0
    }
1634
0
}
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
g_socket_get_ttl (GSocket *socket)
1649
0
{
1650
0
  GError *error = NULL;
1651
0
  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
0
    {
1657
0
      g_socket_get_option (socket, IPPROTO_IP, IP_TTL,
1658
0
         &value, &error);
1659
0
    }
1660
0
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
1661
0
    {
1662
0
      g_socket_get_option (socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
1663
0
         &value, &error);
1664
0
    }
1665
0
  else
1666
0
    g_return_val_if_reached (0);
1667
1668
0
  if (error)
1669
0
    {
1670
0
      g_warning ("error getting unicast ttl: %s", error->message);
1671
0
      g_error_free (error);
1672
0
      return 0;
1673
0
    }
1674
1675
0
  return value;
1676
0
}
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
g_socket_set_ttl (GSocket  *socket,
1690
                  guint     ttl)
1691
0
{
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
0
    {
1698
0
      g_socket_set_option (socket, IPPROTO_IP, IP_TTL,
1699
0
         ttl, &error);
1700
0
    }
1701
0
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
1702
0
    {
1703
0
      g_socket_set_option (socket, IPPROTO_IP, IP_TTL,
1704
0
         ttl, NULL);
1705
0
      g_socket_set_option (socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
1706
0
         ttl, &error);
1707
0
    }
1708
0
  else
1709
0
    g_return_if_reached ();
1710
1711
0
  if (error)
1712
0
    {
1713
0
      g_warning ("error setting unicast ttl: %s", error->message);
1714
0
      g_error_free (error);
1715
0
      return;
1716
0
    }
1717
1718
0
  g_object_notify (G_OBJECT (socket), "ttl");
1719
0
}
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
g_socket_get_broadcast (GSocket *socket)
1735
0
{
1736
0
  GError *error = NULL;
1737
0
  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
0
          &value, &error))
1743
0
    {
1744
0
      g_warning ("error getting broadcast: %s", error->message);
1745
0
      g_error_free (error);
1746
0
      return FALSE;
1747
0
    }
1748
1749
0
  return !!value;
1750
0
}
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
g_socket_set_broadcast (GSocket    *socket,
1765
                        gboolean    broadcast)
1766
0
{
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
0
          broadcast, &error))
1775
0
    {
1776
0
      g_warning ("error setting broadcast: %s", error->message);
1777
0
      g_error_free (error);
1778
0
      return;
1779
0
    }
1780
1781
0
  g_object_notify (G_OBJECT (socket), "broadcast");
1782
0
}
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
g_socket_get_multicast_loopback (GSocket *socket)
1798
0
{
1799
0
  GError *error = NULL;
1800
0
  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
0
    {
1806
0
      g_socket_get_option (socket, IPPROTO_IP, IP_MULTICAST_LOOP,
1807
0
         &value, &error);
1808
0
    }
1809
0
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
1810
0
    {
1811
0
      g_socket_get_option (socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1812
0
         &value, &error);
1813
0
    }
1814
0
  else
1815
0
    g_return_val_if_reached (FALSE);
1816
1817
0
  if (error)
1818
0
    {
1819
0
      g_warning ("error getting multicast loopback: %s", error->message);
1820
0
      g_error_free (error);
1821
0
      return FALSE;
1822
0
    }
1823
1824
0
  return !!value;
1825
0
}
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
g_socket_set_multicast_loopback (GSocket    *socket,
1841
         gboolean    loopback)
1842
0
{
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
0
    {
1851
0
      g_socket_set_option (socket, IPPROTO_IP, IP_MULTICAST_LOOP,
1852
0
         loopback, &error);
1853
0
    }
1854
0
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
1855
0
    {
1856
0
      g_socket_set_option (socket, IPPROTO_IP, IP_MULTICAST_LOOP,
1857
0
         loopback, NULL);
1858
0
      g_socket_set_option (socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1859
0
         loopback, &error);
1860
0
    }
1861
0
  else
1862
0
    g_return_if_reached ();
1863
1864
0
  if (error)
1865
0
    {
1866
0
      g_warning ("error setting multicast loopback: %s", error->message);
1867
0
      g_error_free (error);
1868
0
      return;
1869
0
    }
1870
1871
0
  g_object_notify (G_OBJECT (socket), "multicast-loopback");
1872
0
}
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
g_socket_get_multicast_ttl (GSocket *socket)
1887
0
{
1888
0
  GError *error = NULL;
1889
0
  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
0
    {
1895
0
      g_socket_get_option (socket, IPPROTO_IP, IP_MULTICAST_TTL,
1896
0
         &value, &error);
1897
0
    }
1898
0
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
1899
0
    {
1900
0
      g_socket_get_option (socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
1901
0
         &value, &error);
1902
0
    }
1903
0
  else
1904
0
    g_return_val_if_reached (FALSE);
1905
1906
0
  if (error)
1907
0
    {
1908
0
      g_warning ("error getting multicast ttl: %s", error->message);
1909
0
      g_error_free (error);
1910
0
      return FALSE;
1911
0
    }
1912
1913
0
  return value;
1914
0
}
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
g_socket_set_multicast_ttl (GSocket  *socket,
1929
                            guint     ttl)
1930
0
{
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
0
    {
1937
0
      g_socket_set_option (socket, IPPROTO_IP, IP_MULTICAST_TTL,
1938
0
         ttl, &error);
1939
0
    }
1940
0
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
1941
0
    {
1942
0
      g_socket_set_option (socket, IPPROTO_IP, IP_MULTICAST_TTL,
1943
0
         ttl, NULL);
1944
0
      g_socket_set_option (socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
1945
0
         ttl, &error);
1946
0
    }
1947
0
  else
1948
0
    g_return_if_reached ();
1949
1950
0
  if (error)
1951
0
    {
1952
0
      g_warning ("error setting multicast ttl: %s", error->message);
1953
0
      g_error_free (error);
1954
0
      return;
1955
0
    }
1956
1957
0
  g_object_notify (G_OBJECT (socket), "multicast-ttl");
1958
0
}
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
g_socket_get_family (GSocket *socket)
1972
0
{
1973
0
  g_return_val_if_fail (G_IS_SOCKET (socket), G_SOCKET_FAMILY_INVALID);
1974
1975
0
  return socket->priv->family;
1976
0
}
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
g_socket_get_socket_type (GSocket *socket)
1990
0
{
1991
0
  g_return_val_if_fail (G_IS_SOCKET (socket), G_SOCKET_TYPE_INVALID);
1992
1993
0
  return socket->priv->type;
1994
0
}
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
g_socket_get_protocol (GSocket *socket)
2009
0
{
2010
0
  g_return_val_if_fail (G_IS_SOCKET (socket), -1);
2011
2012
0
  return socket->priv->protocol;
2013
0
}
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
g_socket_get_fd (GSocket *socket)
2031
0
{
2032
0
  g_return_val_if_fail (G_IS_SOCKET (socket), -1);
2033
2034
0
  return socket->priv->fd;
2035
0
}
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
g_socket_get_local_address (GSocket  *socket,
2053
          GError  **error)
2054
0
{
2055
0
  union {
2056
0
    struct sockaddr_storage storage;
2057
0
    struct sockaddr sa;
2058
0
  } buffer;
2059
0
  socklen_t len = sizeof (buffer);
2060
2061
0
  g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
2062
2063
0
  if (getsockname (socket->priv->fd, &buffer.sa, &len) < 0)
2064
0
    {
2065
0
      int errsv = get_socket_errno ();
2066
0
      g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
2067
0
       _("could not get local address: %s"), socket_strerror (errsv));
2068
0
      return NULL;
2069
0
    }
2070
2071
0
  return g_socket_address_new_from_native (&buffer.storage, len);
2072
0
}
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
g_socket_get_remote_address (GSocket  *socket,
2089
           GError  **error)
2090
0
{
2091
0
  union {
2092
0
    struct sockaddr_storage storage;
2093
0
    struct sockaddr sa;
2094
0
  } buffer;
2095
0
  socklen_t len = sizeof (buffer);
2096
2097
0
  g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
2098
2099
0
  if (socket->priv->connect_pending)
2100
0
    {
2101
0
      if (!g_socket_check_connect_result (socket, error))
2102
0
        return NULL;
2103
0
      else
2104
0
        socket->priv->connect_pending = FALSE;
2105
0
    }
2106
2107
0
  if (!socket->priv->remote_address)
2108
0
    {
2109
0
      if (getpeername (socket->priv->fd, &buffer.sa, &len) < 0)
2110
0
  {
2111
0
    int errsv = get_socket_errno ();
2112
0
    g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
2113
0
           _("could not get remote address: %s"), socket_strerror (errsv));
2114
0
    return NULL;
2115
0
  }
2116
2117
0
      socket->priv->remote_address = g_socket_address_new_from_native (&buffer.storage, len);
2118
0
    }
2119
2120
0
  return g_object_ref (socket->priv->remote_address);
2121
0
}
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
g_socket_is_connected (GSocket *socket)
2141
0
{
2142
0
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
2143
2144
0
  return (socket->priv->connected_read || socket->priv->connected_write);
2145
0
}
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
g_socket_listen (GSocket  *socket,
2167
     GError  **error)
2168
0
{
2169
0
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
2170
2171
0
  if (!check_socket (socket, error))
2172
0
    return FALSE;
2173
2174
0
  if (listen (socket->priv->fd, socket->priv->listen_backlog) < 0)
2175
0
    {
2176
0
      int errsv = get_socket_errno ();
2177
2178
0
      g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
2179
0
       _("could not listen: %s"), socket_strerror (errsv));
2180
0
      return FALSE;
2181
0
    }
2182
2183
0
  socket->priv->listening = TRUE;
2184
2185
0
  return TRUE;
2186
0
}
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
g_socket_bind (GSocket         *socket,
2225
         GSocketAddress  *address,
2226
         gboolean         reuse_address,
2227
         GError         **error)
2228
0
{
2229
0
  union {
2230
0
    struct sockaddr_storage storage;
2231
0
    struct sockaddr sa;
2232
0
  } addr;
2233
0
  gboolean so_reuseaddr;
2234
0
#ifdef SO_REUSEPORT
2235
0
  gboolean so_reuseport;
2236
0
#endif
2237
2238
0
  g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
2239
2240
0
  if (!check_socket (socket, error))
2241
0
    return FALSE;
2242
2243
0
  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
0
  so_reuseaddr = !!reuse_address;
2261
0
#endif
2262
2263
0
#ifdef SO_REUSEPORT
2264
0
  so_reuseport = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
2265
0
#endif
2266
2267
  /* Ignore errors here, the only likely error is "not supported", and
2268
   * this is a "best effort" thing mainly.
2269
   */
2270
0
  g_socket_set_option (socket, SOL_SOCKET, SO_REUSEADDR, so_reuseaddr, NULL);
2271
0
#ifdef SO_REUSEPORT
2272
0
  g_socket_set_option (socket, SOL_SOCKET, SO_REUSEPORT, so_reuseport, NULL);
2273
0
#endif
2274
2275
0
  if (bind (socket->priv->fd, &addr.sa,
2276
0
      g_socket_address_get_native_size (address)) < 0)
2277
0
    {
2278
0
      int errsv = get_socket_errno ();
2279
0
      gchar *address_string = address_to_string (address);
2280
2281
0
      g_set_error (error,
2282
0
       G_IO_ERROR, socket_io_error_from_errno (errsv),
2283
0
       _("Error binding to address %s: %s"),
2284
0
       address_string, socket_strerror (errsv));
2285
0
      g_free (address_string);
2286
0
      return FALSE;
2287
0
    }
2288
2289
0
  return TRUE;
2290
0
}
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
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
0
{
2415
0
  const guint8 *native_addr;
2416
0
  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
0
    {
2428
0
#ifdef HAVE_IP_MREQN
2429
0
      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
0
#ifdef HAVE_IP_MREQN
2438
0
      if (iface)
2439
0
        mc_req.imr_ifindex = if_nametoindex (iface);
2440
0
      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
0
  {
2453
0
#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
0
  }
2464
0
      else
2465
0
        optname = join_group ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
2466
0
      result = setsockopt (socket->priv->fd, IPPROTO_IP, optname,
2467
0
         &mc_req, sizeof (mc_req));
2468
0
    }
2469
0
  else if (g_inet_address_get_family (group) == G_SOCKET_FAMILY_IPV6)
2470
0
    {
2471
0
      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
0
#ifdef HAVE_IF_NAMETOINDEX
2476
0
      if (iface)
2477
0
        mc_req_ipv6.ipv6mr_interface = if_nametoindex (iface);
2478
0
      else
2479
0
#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
0
         &mc_req_ipv6, sizeof (mc_req_ipv6));
2485
0
    }
2486
0
  else
2487
0
    g_return_val_if_reached (FALSE);
2488
2489
0
  if (result < 0)
2490
0
    {
2491
0
      int errsv = get_socket_errno ();
2492
2493
0
      g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
2494
0
       join_group ?
2495
0
       _("Error joining multicast group: %s") :
2496
0
       _("Error leaving multicast group: %s"),
2497
0
       socket_strerror (errsv));
2498
0
      return FALSE;
2499
0
    }
2500
2501
0
  return TRUE;
2502
0
}
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
g_socket_join_multicast_group (GSocket       *socket,
2533
             GInetAddress  *group,
2534
                               gboolean       source_specific,
2535
                               const gchar   *iface,
2536
             GError       **error)
2537
0
{
2538
0
  return g_socket_multicast_group_operation (socket, group, source_specific, iface, TRUE, error);
2539
0
}
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
g_socket_leave_multicast_group (GSocket       *socket,
2565
        GInetAddress  *group,
2566
                                gboolean       source_specific,
2567
                                const gchar   *iface,
2568
        GError       **error)
2569
0
{
2570
0
  return g_socket_multicast_group_operation (socket, group, source_specific, iface, FALSE, error);
2571
0
}
2572
2573
static gboolean
2574
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
0
{
2581
0
  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
0
    {
2591
0
      return g_socket_multicast_group_operation (socket, group, FALSE, iface,
2592
0
                                                 join_group, error);
2593
0
    }
2594
2595
0
  if (!check_socket (socket, error))
2596
0
    return FALSE;
2597
2598
0
  switch (g_inet_address_get_family (group))
2599
0
    {
2600
0
    case G_SOCKET_FAMILY_INVALID:
2601
0
    case G_SOCKET_FAMILY_UNIX:
2602
0
      {
2603
0
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2604
0
            join_group ?
2605
0
            _("Error joining multicast group: %s") :
2606
0
            _("Error leaving multicast group: %s"),
2607
0
            _("Unsupported socket family"));
2608
0
        return FALSE;
2609
0
      }
2610
0
      break;
2611
2612
0
    case G_SOCKET_FAMILY_IPV4:
2613
0
      {
2614
0
#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
0
#define S_ADDR_FIELD(src) src.imr_interface.s_addr
2620
0
#endif
2621
2622
0
        gint optname;
2623
0
        struct ip_mreq_source mc_req_src;
2624
2625
0
        if (g_inet_address_get_family (source_specific) !=
2626
0
            G_SOCKET_FAMILY_IPV4)
2627
0
          {
2628
0
            g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2629
0
                join_group ?
2630
0
                _("Error joining multicast group: %s") :
2631
0
                _("Error leaving multicast group: %s"),
2632
0
                _("source-specific not an IPv4 address"));
2633
0
            return FALSE;
2634
0
          }
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
0
          {
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
0
            struct ifreq ifr;
2648
0
            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
0
              {
2655
0
                g_set_error (error, G_IO_ERROR,  G_IO_ERROR_FILENAME_TOO_LONG,
2656
0
                             _("Interface name too long"));
2657
0
                return FALSE;
2658
0
              }
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
0
              {
2666
0
                int errsv = errno;
2667
2668
0
                g_set_error (error, G_IO_ERROR,  g_io_error_from_errno (errsv),
2669
0
                             _("Interface not found: %s"), g_strerror (errsv));
2670
0
                return FALSE;
2671
0
              }
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
0
#endif  /* defined(G_OS_WIN32) && defined (HAVE_IF_NAMETOINDEX) */
2676
0
          }
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
0
                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
0
                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
0
                             &mc_req_src, sizeof (mc_req_src));
2691
2692
0
#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
0
      }
2703
0
      break;
2704
2705
0
    case G_SOCKET_FAMILY_IPV6:
2706
0
      {
2707
0
#ifdef MCAST_JOIN_SOURCE_GROUP
2708
0
        gboolean res;
2709
0
        gint optname;
2710
0
        struct group_source_req mc_req_src;
2711
0
        GSocketAddress *saddr_group, *saddr_source_specific;
2712
0
        guint iface_index = 0;
2713
2714
0
#if defined (HAVE_IF_NAMETOINDEX)
2715
0
        if (iface)
2716
0
          {
2717
0
            iface_index = if_nametoindex (iface);
2718
0
            if (iface_index == 0)
2719
0
              {
2720
0
                int errsv = errno;
2721
2722
0
                g_set_error (error, G_IO_ERROR,  g_io_error_from_errno (errsv),
2723
0
                             _("Interface not found: %s"), g_strerror (errsv));
2724
0
                return FALSE;
2725
0
              }
2726
0
          }
2727
0
#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
0
                                          sizeof (mc_req_src.gsr_group),
2733
0
                                          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
0
                                          &mc_req_src.gsr_source,
2741
0
                                          sizeof (mc_req_src.gsr_source),
2742
0
                                          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
0
                             &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
0
      }
2761
0
      break;
2762
2763
0
    default:
2764
0
      g_return_val_if_reached (FALSE);
2765
0
    }
2766
2767
0
  if (result < 0)
2768
0
    {
2769
0
      int errsv = get_socket_errno ();
2770
2771
0
      g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
2772
0
          join_group ?
2773
0
          _("Error joining multicast group: %s") :
2774
0
          _("Error leaving multicast group: %s"),
2775
0
           socket_strerror (errsv));
2776
0
      return FALSE;
2777
0
    }
2778
2779
0
  return TRUE;
2780
0
}
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
g_socket_join_multicast_group_ssm (GSocket       *socket,
2813
                                   GInetAddress  *group,
2814
                                   GInetAddress  *source_specific,
2815
                                   const gchar   *iface,
2816
                                   GError       **error)
2817
0
{
2818
0
  return g_socket_multicast_group_operation_ssm (socket, group,
2819
0
      source_specific, iface, TRUE, error);
2820
0
}
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
g_socket_leave_multicast_group_ssm (GSocket       *socket,
2844
                                    GInetAddress  *group,
2845
                                    GInetAddress  *source_specific,
2846
                                    const gchar   *iface,
2847
                                    GError       **error)
2848
0
{
2849
0
  return g_socket_multicast_group_operation_ssm (socket, group,
2850
0
      source_specific, iface, FALSE, error);
2851
0
}
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
g_socket_speaks_ipv4 (GSocket *socket)
2873
0
{
2874
0
  switch (socket->priv->family)
2875
0
    {
2876
0
    case G_SOCKET_FAMILY_IPV4:
2877
0
      return TRUE;
2878
2879
0
    case G_SOCKET_FAMILY_IPV6:
2880
0
#if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
2881
0
      {
2882
0
        gint v6_only;
2883
2884
0
        if (!g_socket_get_option (socket,
2885
0
          IPPROTO_IPV6, IPV6_V6ONLY,
2886
0
          &v6_only, NULL))
2887
0
          return FALSE;
2888
2889
0
        return !v6_only;
2890
0
      }
2891
#else
2892
      return FALSE;
2893
#endif
2894
2895
0
    default:
2896
0
      return FALSE;
2897
0
    }
2898
0
}
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
g_socket_accept (GSocket       *socket,
2924
     GCancellable  *cancellable,
2925
     GError       **error)
2926
0
{
2927
0
#ifdef HAVE_ACCEPT4
2928
0
  gboolean try_accept4 = TRUE;
2929
0
#endif
2930
0
  GSocket *new_socket;
2931
0
  gint ret;
2932
2933
0
  g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
2934
2935
0
  if (!check_socket (socket, error))
2936
0
    return NULL;
2937
2938
0
  if (!check_timeout (socket, error))
2939
0
    return NULL;
2940
2941
0
  while (TRUE)
2942
0
    {
2943
0
      gboolean try_accept = TRUE;
2944
2945
0
#ifdef HAVE_ACCEPT4
2946
0
      if (try_accept4)
2947
0
        {
2948
0
          ret = accept4 (socket->priv->fd, NULL, 0, SOCK_CLOEXEC);
2949
0
          if (ret < 0 && errno == ENOSYS)
2950
0
            {
2951
0
              try_accept4 = FALSE;
2952
0
            }
2953
0
          else
2954
0
            {
2955
0
              try_accept = FALSE;
2956
0
            }
2957
0
        }
2958
2959
0
      g_assert (try_accept4 || try_accept);
2960
0
#endif
2961
0
      if (try_accept)
2962
0
        ret = accept (socket->priv->fd, NULL, 0);
2963
2964
0
      if (ret < 0)
2965
0
  {
2966
0
    int errsv = get_socket_errno ();
2967
2968
0
    if (errsv == EINTR)
2969
0
      continue;
2970
2971
#ifdef WSAEWOULDBLOCK
2972
          if (errsv == WSAEWOULDBLOCK)
2973
#else
2974
0
          if (errsv == EWOULDBLOCK ||
2975
0
              errsv == EAGAIN)
2976
0
#endif
2977
0
            {
2978
0
              win32_unset_event_mask (socket, FD_ACCEPT);
2979
2980
0
              if (socket->priv->blocking)
2981
0
                {
2982
0
                  if (!g_socket_condition_wait (socket,
2983
0
                                                G_IO_IN, cancellable, error))
2984
0
                    return NULL;
2985
2986
0
                  continue;
2987
0
                }
2988
0
            }
2989
2990
0
    socket_set_error_lazy (error, errsv, _("Error accepting connection: %s"));
2991
0
    return NULL;
2992
0
  }
2993
0
      break;
2994
0
    }
2995
2996
0
  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
0
  {
3006
0
    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
0
    flags = fcntl (ret, F_GETFD, 0);
3012
0
    if (flags != -1 &&
3013
0
  (flags & FD_CLOEXEC) == 0)
3014
0
      {
3015
0
  flags |= FD_CLOEXEC;
3016
0
  fcntl (ret, F_SETFD, flags);
3017
0
      }
3018
0
  }
3019
0
#endif
3020
3021
0
  new_socket = g_socket_new_from_fd (ret, error);
3022
0
  if (new_socket == NULL)
3023
0
    {
3024
#ifdef G_OS_WIN32
3025
      closesocket (ret);
3026
#else
3027
0
      close (ret);
3028
0
#endif
3029
0
    }
3030
0
  else
3031
0
    new_socket->priv->protocol = socket->priv->protocol;
3032
3033
0
  return new_socket;
3034
0
}
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
g_socket_connect (GSocket         *socket,
3066
      GSocketAddress  *address,
3067
      GCancellable    *cancellable,
3068
      GError         **error)
3069
0
{
3070
0
  union {
3071
0
    struct sockaddr_storage storage;
3072
0
    struct sockaddr sa;
3073
0
  } buffer;
3074
3075
0
  g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
3076
3077
0
  if (!check_socket (socket, error))
3078
0
    return FALSE;
3079
3080
0
  if (!g_socket_address_to_native (address, &buffer.storage, sizeof buffer, error))
3081
0
    return FALSE;
3082
3083
0
  if (socket->priv->remote_address)
3084
0
    g_object_unref (socket->priv->remote_address);
3085
0
  socket->priv->remote_address = g_object_ref (address);
3086
3087
0
  while (1)
3088
0
    {
3089
0
      if (connect (socket->priv->fd, &buffer.sa,
3090
0
       g_socket_address_get_native_size (address)) < 0)
3091
0
  {
3092
0
    int errsv = get_socket_errno ();
3093
3094
0
    if (errsv == EINTR)
3095
0
      continue;
3096
3097
0
#ifndef G_OS_WIN32
3098
0
    if (errsv == EINPROGRESS)
3099
#else
3100
    if (errsv == WSAEWOULDBLOCK)
3101
#endif
3102
0
      {
3103
0
              win32_unset_event_mask (socket, FD_CONNECT);
3104
3105
0
        if (socket->priv->blocking)
3106
0
    {
3107
0
      if (g_socket_condition_wait (socket, G_IO_OUT, cancellable, error))
3108
0
        {
3109
0
          if (g_socket_check_connect_result (socket, error))
3110
0
      break;
3111
0
        }
3112
0
    }
3113
0
        else
3114
0
                {
3115
0
                  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
3116
0
                                       _("Connection in progress"));
3117
0
                  socket->priv->connect_pending = TRUE;
3118
0
                }
3119
0
      }
3120
0
    else
3121
0
      g_set_error_literal (error, G_IO_ERROR,
3122
0
         socket_io_error_from_errno (errsv),
3123
0
         socket_strerror (errsv));
3124
3125
0
    return FALSE;
3126
0
  }
3127
0
      break;
3128
0
    }
3129
3130
0
  win32_unset_event_mask (socket, FD_CONNECT);
3131
3132
0
  socket->priv->connected_read = TRUE;
3133
0
  socket->priv->connected_write = TRUE;
3134
3135
0
  return TRUE;
3136
0
}
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
g_socket_check_connect_result (GSocket  *socket,
3153
             GError  **error)
3154
0
{
3155
0
  int value;
3156
3157
0
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
3158
3159
0
  if (!check_socket (socket, error))
3160
0
    return FALSE;
3161
3162
0
  if (!check_timeout (socket, error))
3163
0
    return FALSE;
3164
3165
0
  if (!g_socket_get_option (socket, SOL_SOCKET, SO_ERROR, &value, error))
3166
0
    {
3167
0
      g_prefix_error (error, _("Unable to get pending error: "));
3168
0
      return FALSE;
3169
0
    }
3170
3171
0
  if (value != 0)
3172
0
    {
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
0
        {
3177
0
          g_object_unref (socket->priv->remote_address);
3178
0
          socket->priv->remote_address = NULL;
3179
0
        }
3180
0
      return FALSE;
3181
0
    }
3182
3183
0
  socket->priv->connected_read = TRUE;
3184
0
  socket->priv->connected_write = TRUE;
3185
3186
0
  return TRUE;
3187
0
}
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
g_socket_get_available_bytes (GSocket *socket)
3213
0
{
3214
0
#ifndef SO_NREAD
3215
0
  const gint bufsize = 64 * 1024;
3216
0
  static guchar *buf = NULL;
3217
0
#endif
3218
#ifdef G_OS_WIN32
3219
  u_long avail;
3220
#else
3221
0
  gint avail;
3222
0
#endif
3223
3224
0
  g_return_val_if_fail (G_IS_SOCKET (socket), -1);
3225
3226
0
  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
0
  if (socket->priv->type == G_SOCKET_TYPE_DATAGRAM)
3234
0
    {
3235
0
      if (G_UNLIKELY (g_once_init_enter_pointer (&buf)))
3236
0
        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
0
      avail = recv (socket->priv->fd, buf, bufsize, MSG_PEEK);
3242
0
      if ((gint) avail == -1)
3243
0
        {
3244
0
          int errsv = get_socket_errno ();
3245
#ifdef G_OS_WIN32
3246
          if (errsv == WSAEWOULDBLOCK)
3247
#else
3248
0
          if (errsv == EWOULDBLOCK || errsv == EAGAIN)
3249
0
#endif
3250
0
            avail = 0;
3251
0
        }
3252
0
    }
3253
0
  else
3254
0
    {
3255
#ifdef G_OS_WIN32
3256
      if (ioctlsocket (socket->priv->fd, FIONREAD, &avail) < 0)
3257
#else
3258
0
      if (ioctl (socket->priv->fd, FIONREAD, &avail) < 0)
3259
0
#endif
3260
0
        avail = -1;
3261
0
    }
3262
0
#endif
3263
3264
0
  return avail;
3265
0
}
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
block_on_timeout (GSocket       *socket,
3272
                  GIOCondition   condition,
3273
                  gint64         timeout_us,
3274
                  gint64         start_time,
3275
                  GCancellable  *cancellable,
3276
                  GError       **error)
3277
0
{
3278
0
  gint64 wait_timeout = -1;
3279
3280
0
  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
0
  if (timeout_us >= 0)
3284
0
    {
3285
0
      gint64 elapsed = g_get_monotonic_time () - start_time;
3286
3287
0
      if (elapsed >= timeout_us)
3288
0
        {
3289
0
          g_set_error_literal (error,
3290
0
                               G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
3291
0
                               _("Socket I/O timed out"));
3292
0
          return FALSE;
3293
0
        }
3294
3295
0
      wait_timeout = timeout_us - elapsed;
3296
0
    }
3297
3298
0
  return g_socket_condition_timed_wait (socket, condition, wait_timeout,
3299
0
                                        cancellable, error);
3300
0
}
3301
3302
static gssize
3303
g_socket_receive_with_timeout (GSocket       *socket,
3304
                               guint8        *buffer,
3305
                               gsize          size,
3306
                               gint64         timeout_us,
3307
                               GCancellable  *cancellable,
3308
                               GError       **error)
3309
0
{
3310
0
  gssize ret;
3311
0
  gint64 start_time;
3312
3313
0
  g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, -1);
3314
3315
0
  start_time = g_get_monotonic_time ();
3316
3317
0
  if (!check_socket (socket, error))
3318
0
    return -1;
3319
3320
0
  if (!check_timeout (socket, error))
3321
0
    return -1;
3322
3323
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
3324
0
    return -1;
3325
3326
0
  while (1)
3327
0
    {
3328
0
      if ((ret = recv (socket->priv->fd, buffer, size, 0)) < 0)
3329
0
  {
3330
0
    int errsv = get_socket_errno ();
3331
3332
0
    if (errsv == EINTR)
3333
0
      continue;
3334
3335
#ifdef WSAEWOULDBLOCK
3336
          if (errsv == WSAEWOULDBLOCK)
3337
#else
3338
0
          if (errsv == EWOULDBLOCK ||
3339
0
              errsv == EAGAIN)
3340
0
#endif
3341
0
            {
3342
0
              win32_unset_event_mask (socket, FD_READ);
3343
3344
0
              if (timeout_us != 0)
3345
0
                {
3346
0
                  if (!block_on_timeout (socket, G_IO_IN, timeout_us, start_time,
3347
0
                                         cancellable, error))
3348
0
                    return -1;
3349
3350
0
                  continue;
3351
0
                }
3352
0
            }
3353
3354
0
    win32_unset_event_mask (socket, FD_READ);
3355
3356
0
    socket_set_error_lazy (error, errsv, _("Error receiving data: %s"));
3357
0
    return -1;
3358
0
  }
3359
3360
0
      win32_unset_event_mask (socket, FD_READ);
3361
3362
0
      break;
3363
0
    }
3364
3365
0
  return ret;
3366
0
}
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
g_socket_receive_bytes (GSocket       *socket,
3393
                        gsize          size,
3394
                        gint64         timeout_us,
3395
                        GCancellable  *cancellable,
3396
                        GError       **error)
3397
0
{
3398
0
  guint8 *data;
3399
0
  gssize res;
3400
0
  GBytes *buf;
3401
3402
0
  g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
3403
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
3404
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3405
3406
0
  data = g_new0 (guint8, size);
3407
0
  res = g_socket_receive_with_timeout (socket, data, size, timeout_us, cancellable, error);
3408
0
  if (res < 0)
3409
0
    {
3410
0
      g_free (data);
3411
0
      return NULL;
3412
0
    }
3413
3414
0
  if ((gsize) res == size)
3415
0
    {
3416
0
      buf = g_bytes_new_take (g_steal_pointer (&data), (gsize) res);
3417
0
    }
3418
0
  else
3419
0
    {
3420
0
      GBytes *sub_buf;
3421
3422
0
      buf = g_bytes_new_take (g_steal_pointer (&data), size);
3423
0
      sub_buf = g_bytes_new_from_bytes (buf, 0, (gsize) res);
3424
0
      g_bytes_unref (buf);
3425
0
      buf = g_steal_pointer (&sub_buf);
3426
0
    }
3427
3428
0
  return g_steal_pointer (&buf);
3429
0
}
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
g_socket_receive (GSocket       *socket,
3471
      gchar         *buffer,
3472
      gsize          size,
3473
      GCancellable  *cancellable,
3474
      GError       **error)
3475
0
{
3476
0
  return g_socket_receive_with_timeout (socket, (guint8 *) buffer, size,
3477
0
                                        socket->priv->blocking ? -1 : 0,
3478
0
                                        cancellable, error);
3479
0
}
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
g_socket_receive_with_blocking (GSocket       *socket,
3502
        gchar         *buffer,
3503
        gsize          size,
3504
        gboolean       blocking,
3505
        GCancellable  *cancellable,
3506
        GError       **error)
3507
0
{
3508
0
  return g_socket_receive_with_timeout (socket, (guint8 *) buffer, size,
3509
0
                                        blocking ? -1 : 0, cancellable, error);
3510
0
}
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
g_socket_receive_bytes_from (GSocket         *socket,
3543
                             GSocketAddress **address,
3544
                             gsize            size,
3545
                             gint64           timeout_us,
3546
                             GCancellable    *cancellable,
3547
                             GError         **error)
3548
0
{
3549
0
  GInputVector v;
3550
0
  gssize res;
3551
0
  GBytes *buf;
3552
3553
0
  g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
3554
0
  g_return_val_if_fail (address == NULL || *address == NULL, NULL);
3555
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
3556
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3557
3558
0
  v.buffer = g_new0 (guint8, size);
3559
0
  v.size = size;
3560
3561
0
  res = g_socket_receive_message_with_timeout (socket,
3562
0
                                               address,
3563
0
                                               &v, 1,
3564
0
                                               NULL, 0, NULL,
3565
0
                                               timeout_us,
3566
0
                                               cancellable,
3567
0
                                               error);
3568
0
  if (res < 0)
3569
0
    {
3570
0
      g_free (v.buffer);
3571
0
      return NULL;
3572
0
    }
3573
3574
0
  if ((gsize) res == size)
3575
0
    {
3576
0
      buf = g_bytes_new_take (g_steal_pointer (&v.buffer), (gsize) res);
3577
0
    }
3578
0
  else
3579
0
    {
3580
0
      GBytes *sub_buf;
3581
3582
0
      buf = g_bytes_new_take (g_steal_pointer (&v.buffer), size);
3583
0
      sub_buf = g_bytes_new_from_bytes (buf, 0, (gsize) res);
3584
0
      g_bytes_unref (buf);
3585
0
      buf = g_steal_pointer (&sub_buf);
3586
0
    }
3587
3588
0
  return g_steal_pointer (&buf);
3589
0
}
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
g_socket_receive_from (GSocket         *socket,
3617
           GSocketAddress **address,
3618
           gchar           *buffer,
3619
           gsize            size,
3620
           GCancellable    *cancellable,
3621
           GError         **error)
3622
0
{
3623
0
  GInputVector v;
3624
3625
0
  v.buffer = buffer;
3626
0
  v.size = size;
3627
3628
0
  return g_socket_receive_message (socket,
3629
0
           address,
3630
0
           &v, 1,
3631
0
           NULL, 0, NULL,
3632
0
           cancellable,
3633
0
           error);
3634
0
}
3635
3636
/* See the comment about SIGPIPE above. */
3637
#ifdef MSG_NOSIGNAL
3638
0
#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
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
0
{
3651
0
  gssize ret;
3652
0
  gint64 start_time;
3653
3654
0
  g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, -1);
3655
3656
0
  start_time = g_get_monotonic_time ();
3657
3658
0
  if (!check_socket (socket, error))
3659
0
    return -1;
3660
3661
0
  if (!check_timeout (socket, error))
3662
0
    return -1;
3663
3664
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
3665
0
    return -1;
3666
3667
0
  while (1)
3668
0
    {
3669
0
      if ((ret = send (socket->priv->fd, (const char *)buffer, size, G_SOCKET_DEFAULT_SEND_FLAGS)) < 0)
3670
0
  {
3671
0
    int errsv = get_socket_errno ();
3672
3673
0
    if (errsv == EINTR)
3674
0
      continue;
3675
3676
#ifdef WSAEWOULDBLOCK
3677
          if (errsv == WSAEWOULDBLOCK)
3678
#else
3679
0
          if (errsv == EWOULDBLOCK ||
3680
0
              errsv == EAGAIN)
3681
0
#endif
3682
0
            {
3683
0
              win32_unset_event_mask (socket, FD_WRITE);
3684
3685
0
              if (timeout_us != 0)
3686
0
                {
3687
0
                  if (!block_on_timeout (socket, G_IO_OUT, timeout_us, start_time,
3688
0
                                         cancellable, error))
3689
0
                    return -1;
3690
3691
0
                  continue;
3692
0
                }
3693
0
            }
3694
3695
0
    socket_set_error_lazy (error, errsv, _("Error sending data: %s"));
3696
0
    return -1;
3697
0
  }
3698
0
      break;
3699
0
    }
3700
3701
0
  return ret;
3702
0
}
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
g_socket_send (GSocket       *socket,
3735
         const gchar   *buffer,
3736
         gsize          size,
3737
         GCancellable  *cancellable,
3738
         GError       **error)
3739
0
{
3740
0
  return g_socket_send_with_blocking (socket, buffer, size,
3741
0
              socket->priv->blocking,
3742
0
              cancellable, error);
3743
0
}
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
g_socket_send_with_blocking (GSocket       *socket,
3766
           const gchar   *buffer,
3767
           gsize          size,
3768
           gboolean       blocking,
3769
           GCancellable  *cancellable,
3770
           GError       **error)
3771
0
{
3772
0
  return g_socket_send_with_timeout (socket, (const guint8 *) buffer, size,
3773
0
                                     blocking ? -1 : 0, cancellable, error);
3774
0
}
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
g_socket_send_to (GSocket         *socket,
3799
      GSocketAddress  *address,
3800
      const gchar     *buffer,
3801
      gsize            size,
3802
      GCancellable    *cancellable,
3803
      GError         **error)
3804
0
{
3805
0
  GOutputVector v;
3806
3807
0
  v.buffer = buffer;
3808
0
  v.size = size;
3809
3810
0
  return g_socket_send_message (socket,
3811
0
        address,
3812
0
        &v, 1,
3813
0
        NULL, 0,
3814
0
        0,
3815
0
        cancellable,
3816
0
        error);
3817
0
}
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
g_socket_shutdown (GSocket   *socket,
3847
       gboolean   shutdown_read,
3848
       gboolean   shutdown_write,
3849
       GError   **error)
3850
0
{
3851
0
  int how;
3852
3853
0
  g_return_val_if_fail (G_IS_SOCKET (socket), TRUE);
3854
3855
0
  if (!check_socket (socket, error))
3856
0
    return FALSE;
3857
3858
  /* Do nothing? */
3859
0
  if (!shutdown_read && !shutdown_write)
3860
0
    return TRUE;
3861
3862
0
#ifndef G_OS_WIN32
3863
0
  if (shutdown_read && shutdown_write)
3864
0
    how = SHUT_RDWR;
3865
0
  else if (shutdown_read)
3866
0
    how = SHUT_RD;
3867
0
  else
3868
0
    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
0
  if (shutdown (socket->priv->fd, how) != 0)
3879
0
    {
3880
0
      int errsv = get_socket_errno ();
3881
0
      g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
3882
0
       _("Unable to shutdown socket: %s"), socket_strerror (errsv));
3883
0
      return FALSE;
3884
0
    }
3885
3886
0
  if (shutdown_read)
3887
0
    socket->priv->connected_read = FALSE;
3888
0
  if (shutdown_write)
3889
0
    socket->priv->connected_write = FALSE;
3890
3891
0
  return TRUE;
3892
0
}
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
g_socket_close (GSocket  *socket,
3935
    GError  **error)
3936
0
{
3937
0
  int res;
3938
3939
0
  g_return_val_if_fail (G_IS_SOCKET (socket), TRUE);
3940
3941
0
  if (socket->priv->closed)
3942
0
    return TRUE; /* Multiple close not an error */
3943
3944
0
  if (!check_socket (socket, error))
3945
0
    return FALSE;
3946
3947
0
  while (1)
3948
0
    {
3949
#ifdef G_OS_WIN32
3950
      res = closesocket (socket->priv->fd);
3951
#else
3952
0
      res = close (socket->priv->fd);
3953
0
#endif
3954
0
      if (res == -1)
3955
0
  {
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
0
           _("Error closing socket: %s"),
3964
0
           socket_strerror (errsv));
3965
0
    return FALSE;
3966
0
  }
3967
0
      break;
3968
0
    }
3969
3970
0
  socket->priv->fd = -1;
3971
0
  socket->priv->connected_read = FALSE;
3972
0
  socket->priv->connected_write = FALSE;
3973
0
  socket->priv->closed = TRUE;
3974
0
  if (socket->priv->remote_address)
3975
0
    {
3976
0
      g_object_unref (socket->priv->remote_address);
3977
0
      socket->priv->remote_address = NULL;
3978
0
    }
3979
3980
0
  return TRUE;
3981
0
}
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
g_socket_is_closed (GSocket *socket)
3995
0
{
3996
0
  return socket->priv->closed;
3997
0
}
3998
3999
/* Broken source, used on errors */
4000
static gboolean
4001
broken_dispatch (GSource     *source,
4002
     GSourceFunc  callback,
4003
     gpointer     user_data)
4004
0
{
4005
0
  return TRUE;
4006
0
}
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
socket_source_prepare (GSource *source,
4197
                       gint    *timeout)
4198
0
{
4199
0
  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
0
  return g_socket_is_closed (socket_source->socket) && socket_source->fd_tag != NULL;
4215
0
#endif
4216
0
}
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
socket_source_dispatch (GSource     *source,
4230
      GSourceFunc  callback,
4231
      gpointer     user_data)
4232
0
{
4233
0
  GSocketSourceFunc func = (GSocketSourceFunc)callback;
4234
0
  GSocketSource *socket_source = (GSocketSource *)source;
4235
0
  GSocket *socket = socket_source->socket;
4236
0
  gint64 timeout;
4237
0
  guint events;
4238
0
  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
0
  if (g_socket_is_closed (socket_source->socket))
4247
0
    {
4248
0
      if (socket_source->fd_tag)
4249
0
        g_source_remove_unix_fd (source, socket_source->fd_tag);
4250
0
      socket_source->fd_tag = NULL;
4251
0
      events = G_IO_NVAL;
4252
0
    }
4253
0
  else
4254
0
    {
4255
0
      events = g_source_query_unix_fd (source, socket_source->fd_tag);
4256
0
    }
4257
0
#endif
4258
4259
0
  timeout = g_source_get_ready_time (source);
4260
0
  if (timeout >= 0 && timeout <= g_source_get_time (source) &&
4261
0
      !g_socket_is_closed (socket_source->socket))
4262
0
    {
4263
0
      socket->priv->timed_out = TRUE;
4264
0
      events |= (G_IO_IN | G_IO_OUT);
4265
0
    }
4266
4267
0
  ret = (*func) (socket, events & socket_source->condition, user_data);
4268
4269
0
  if (socket->priv->timeout && !g_socket_is_closed (socket_source->socket))
4270
0
    g_source_set_ready_time (source, g_get_monotonic_time () + socket->priv->timeout * 1000000);
4271
0
  else
4272
0
    g_source_set_ready_time (source, -1);
4273
4274
0
  return ret;
4275
0
}
4276
4277
static void
4278
socket_source_finalize (GSource *source)
4279
0
{
4280
0
  GSocketSource *socket_source = (GSocketSource *)source;
4281
0
  GSocket *socket;
4282
4283
0
  socket = socket_source->socket;
4284
4285
#ifdef G_OS_WIN32
4286
  remove_condition_watch (socket, &socket_source->condition);
4287
#endif
4288
4289
0
  g_object_unref (socket);
4290
0
}
4291
4292
static gboolean
4293
socket_source_closure_callback (GSocket      *socket,
4294
        GIOCondition  condition,
4295
        gpointer      data)
4296
0
{
4297
0
  GClosure *closure = data;
4298
4299
0
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
4300
0
  GValue result_value = G_VALUE_INIT;
4301
0
  gboolean result;
4302
4303
0
  g_value_init (&result_value, G_TYPE_BOOLEAN);
4304
4305
0
  g_value_init (&params[0], G_TYPE_SOCKET);
4306
0
  g_value_set_object (&params[0], socket);
4307
0
  g_value_init (&params[1], G_TYPE_IO_CONDITION);
4308
0
  g_value_set_flags (&params[1], condition);
4309
4310
0
  g_closure_invoke (closure, &result_value, 2, params, NULL);
4311
4312
0
  result = g_value_get_boolean (&result_value);
4313
0
  g_value_unset (&result_value);
4314
0
  g_value_unset (&params[0]);
4315
0
  g_value_unset (&params[1]);
4316
4317
0
  return result;
4318
0
}
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
socket_source_new (GSocket      *socket,
4336
       GIOCondition  condition,
4337
       GCancellable *cancellable)
4338
0
{
4339
0
  GSource *source;
4340
0
  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
0
  if (!check_socket (socket, NULL))
4353
0
    {
4354
0
      g_warning ("Socket check failed");
4355
0
      return g_source_new (&broken_funcs, sizeof (GSource));
4356
0
    }
4357
4358
0
  condition |= G_IO_HUP | G_IO_ERR | G_IO_NVAL;
4359
4360
0
  source = g_source_new (&socket_source_funcs, sizeof (GSocketSource));
4361
0
  g_source_set_static_name (source, "GSocket");
4362
0
  socket_source = (GSocketSource *)source;
4363
4364
0
  socket_source->socket = g_object_ref (socket);
4365
0
  socket_source->condition = condition;
4366
4367
0
  if (cancellable)
4368
0
    {
4369
0
      GSource *cancellable_source;
4370
4371
0
      cancellable_source = g_cancellable_source_new (cancellable);
4372
0
      g_source_add_child_source (source, cancellable_source);
4373
0
      g_source_set_dummy_callback (cancellable_source);
4374
0
      g_source_unref (cancellable_source);
4375
0
    }
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
0
  socket_source->fd_tag = g_source_add_unix_fd (source, socket->priv->fd, condition);
4385
0
#endif
4386
4387
0
  if (socket->priv->timeout)
4388
0
    g_source_set_ready_time (source, g_get_monotonic_time () + socket->priv->timeout * 1000000);
4389
0
  else
4390
0
    g_source_set_ready_time (source, -1);
4391
4392
0
  return source;
4393
0
}
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
g_socket_create_source (GSocket      *socket,
4428
      GIOCondition  condition,
4429
      GCancellable *cancellable)
4430
0
{
4431
0
  g_return_val_if_fail (G_IS_SOCKET (socket) && (cancellable == NULL || G_IS_CANCELLABLE (cancellable)), NULL);
4432
4433
0
  return socket_source_new (socket, condition, cancellable);
4434
0
}
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
g_socket_condition_check (GSocket      *socket,
4465
        GIOCondition  condition)
4466
0
{
4467
0
  g_return_val_if_fail (G_IS_SOCKET (socket), 0);
4468
4469
0
  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
0
  {
4485
0
    GPollFD poll_fd;
4486
0
    gint result;
4487
0
    poll_fd.fd = socket->priv->fd;
4488
0
    poll_fd.events = condition;
4489
0
    poll_fd.revents = 0;
4490
4491
0
    do
4492
0
      result = g_poll (&poll_fd, 1, 0);
4493
0
    while (result == -1 && get_socket_errno () == EINTR);
4494
4495
0
    return poll_fd.revents;
4496
0
  }
4497
0
#endif
4498
0
}
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
g_socket_condition_wait (GSocket       *socket,
4524
       GIOCondition   condition,
4525
       GCancellable  *cancellable,
4526
       GError       **error)
4527
0
{
4528
0
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
4529
4530
0
  return g_socket_condition_timed_wait (socket, condition, -1,
4531
0
          cancellable, error);
4532
0
}
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
g_socket_condition_timed_wait (GSocket       *socket,
4565
             GIOCondition   condition,
4566
             gint64         timeout_us,
4567
             GCancellable  *cancellable,
4568
             GError       **error)
4569
0
{
4570
0
  gint64 start_time;
4571
0
  gint64 timeout_ms;
4572
4573
0
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
4574
4575
0
  if (!check_socket (socket, error))
4576
0
    return FALSE;
4577
4578
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
4579
0
    return FALSE;
4580
4581
0
  if (socket->priv->timeout &&
4582
0
      (timeout_us < 0 || socket->priv->timeout < timeout_us / G_USEC_PER_SEC))
4583
0
    timeout_ms = (gint64) socket->priv->timeout * 1000;
4584
0
  else if (timeout_us != -1)
4585
0
    timeout_ms = timeout_us / 1000;
4586
0
  else
4587
0
    timeout_ms = -1;
4588
4589
0
  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
0
  {
4689
0
    GPollFD poll_fd[2];
4690
0
    gint result;
4691
0
    gint num;
4692
4693
0
    poll_fd[0].fd = socket->priv->fd;
4694
0
    poll_fd[0].events = condition;
4695
0
    num = 1;
4696
4697
0
    if (g_cancellable_make_pollfd (cancellable, &poll_fd[1]))
4698
0
      num++;
4699
4700
0
    while (TRUE)
4701
0
      {
4702
0
  int errsv;
4703
0
  result = g_poll (poll_fd, num, timeout_ms);
4704
0
  errsv = errno;
4705
0
  if (result != -1 || errsv != EINTR)
4706
0
    break;
4707
4708
0
  if (timeout_ms != -1)
4709
0
    {
4710
0
      timeout_ms -= (g_get_monotonic_time () - start_time) / 1000;
4711
0
      if (timeout_ms < 0)
4712
0
        timeout_ms = 0;
4713
0
    }
4714
0
      }
4715
    
4716
0
    if (num > 1)
4717
0
      g_cancellable_release_fd (cancellable);
4718
4719
0
    if (result == 0)
4720
0
      {
4721
0
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
4722
0
           _("Socket I/O timed out"));
4723
0
  return FALSE;
4724
0
      }
4725
4726
0
    return !g_cancellable_set_error_if_cancelled (cancellable, error);
4727
0
  }
4728
0
  #endif
4729
0
}
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
0
# 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
0
#define output_message_to_msghdr(message, prev_message, msg, prev_msg, error) \
4745
0
G_STMT_START { \
4746
0
  const GOutputMessage  *_message = (message); \
4747
0
  const GOutputMessage *_prev_message = (prev_message); \
4748
0
  struct msghdr *_msg = (msg); \
4749
0
  const struct msghdr *_prev_msg = (prev_msg); \
4750
0
  GError **_error = (error); \
4751
0
 \
4752
0
  _msg->msg_flags = 0; \
4753
0
 \
4754
0
  /* name */ \
4755
0
  if (_prev_message != NULL && _prev_message->address == _message->address) \
4756
0
    { \
4757
0
      _msg->msg_name = _prev_msg->msg_name; \
4758
0
      _msg->msg_namelen = _prev_msg->msg_namelen; \
4759
0
    } \
4760
0
  else if (_message->address != NULL) \
4761
0
    { \
4762
0
      _msg->msg_namelen = g_socket_address_get_native_size (_message->address); \
4763
0
      _msg->msg_name = g_alloca (_msg->msg_namelen); \
4764
0
      if (!g_socket_address_to_native (_message->address, _msg->msg_name, \
4765
0
                                       _msg->msg_namelen, _error)) \
4766
0
        break; \
4767
0
    } \
4768
0
  else \
4769
0
    { \
4770
0
      _msg->msg_name = NULL; \
4771
0
      _msg->msg_namelen = 0; \
4772
0
    } \
4773
0
 \
4774
0
  /* iov */ \
4775
0
  { \
4776
0
    /* this entire expression will be evaluated at compile time */ \
4777
0
    if (sizeof *_msg->msg_iov == sizeof *_message->vectors && \
4778
0
        sizeof _msg->msg_iov->iov_base == sizeof _message->vectors->buffer && \
4779
0
        G_STRUCT_OFFSET (struct iovec, iov_base) == \
4780
0
        G_STRUCT_OFFSET (GOutputVector, buffer) && \
4781
0
        sizeof _msg->msg_iov->iov_len == sizeof _message->vectors->size && \
4782
0
        G_STRUCT_OFFSET (struct iovec, iov_len) == \
4783
0
        G_STRUCT_OFFSET (GOutputVector, size)) \
4784
0
      /* ABI is compatible */ \
4785
0
      { \
4786
0
        _msg->msg_iov = (struct iovec *) _message->vectors; \
4787
0
        _msg->msg_iovlen = _message->num_vectors; \
4788
0
      } \
4789
0
    else \
4790
0
      /* ABI is incompatible */ \
4791
0
      { \
4792
0
        guint i; \
4793
0
 \
4794
0
        _msg->msg_iov = g_newa (struct iovec, _message->num_vectors); \
4795
0
        for (i = 0; i < _message->num_vectors; i++) \
4796
0
          { \
4797
0
            _msg->msg_iov[i].iov_base = (void *) _message->vectors[i].buffer; \
4798
0
            _msg->msg_iov[i].iov_len = _message->vectors[i].size; \
4799
0
          } \
4800
0
        _msg->msg_iovlen = _message->num_vectors; \
4801
0
      } \
4802
0
  } \
4803
0
 \
4804
0
  /* control */ \
4805
0
  { \
4806
0
    struct cmsghdr *cmsg; \
4807
0
    guint i; \
4808
0
 \
4809
0
    _msg->msg_controllen = 0; \
4810
0
    for (i = 0; i < _message->num_control_messages; i++) \
4811
0
      _msg->msg_controllen += CMSG_SPACE (g_socket_control_message_get_size (_message->control_messages[i])); \
4812
0
 \
4813
0
    if (_msg->msg_controllen == 0) \
4814
0
      _msg->msg_control = NULL; \
4815
0
    else \
4816
0
      { \
4817
0
        _msg->msg_control = g_alloca0 (_msg->msg_controllen); \
4818
0
      } \
4819
0
 \
4820
0
    cmsg = CMSG_FIRSTHDR (_msg); \
4821
0
    for (i = 0; i < _message->num_control_messages; i++) \
4822
0
      { \
4823
0
        cmsg->cmsg_level = g_socket_control_message_get_level (_message->control_messages[i]); \
4824
0
        cmsg->cmsg_type = g_socket_control_message_get_msg_type (_message->control_messages[i]); \
4825
0
        cmsg->cmsg_len = CMSG_LEN (g_socket_control_message_get_size (_message->control_messages[i])); \
4826
0
        g_socket_control_message_serialize (_message->control_messages[i], \
4827
0
                                            CMSG_DATA (cmsg)); \
4828
0
        cmsg = CMSG_NXTHDR (_msg, cmsg); \
4829
0
      } \
4830
0
    g_assert (cmsg == NULL); \
4831
0
  } \
4832
0
} G_STMT_END
4833
4834
0
#define input_message_to_msghdr(message, msg) \
4835
0
G_STMT_START { \
4836
0
  const GInputMessage  *_message = (message); \
4837
0
  struct msghdr *_msg = (msg); \
4838
0
 \
4839
0
  /* name */ \
4840
0
  if (_message->address) \
4841
0
    { \
4842
0
      _msg->msg_namelen = sizeof (struct sockaddr_storage); \
4843
0
      _msg->msg_name = g_alloca (_msg->msg_namelen); \
4844
0
    } \
4845
0
  else \
4846
0
    { \
4847
0
      _msg->msg_name = NULL; \
4848
0
      _msg->msg_namelen = 0; \
4849
0
    } \
4850
0
 \
4851
0
  /* iov */ \
4852
0
  /* this entire expression will be evaluated at compile time */ \
4853
0
  if (sizeof *_msg->msg_iov == sizeof *_message->vectors && \
4854
0
      sizeof _msg->msg_iov->iov_base == sizeof _message->vectors->buffer && \
4855
0
      G_STRUCT_OFFSET (struct iovec, iov_base) == \
4856
0
      G_STRUCT_OFFSET (GInputVector, buffer) && \
4857
0
      sizeof _msg->msg_iov->iov_len == sizeof _message->vectors->size && \
4858
0
      G_STRUCT_OFFSET (struct iovec, iov_len) == \
4859
0
      G_STRUCT_OFFSET (GInputVector, size)) \
4860
0
    /* ABI is compatible */ \
4861
0
    { \
4862
0
      _msg->msg_iov = (struct iovec *) _message->vectors; \
4863
0
      _msg->msg_iovlen = _message->num_vectors; \
4864
0
    } \
4865
0
  else \
4866
0
    /* ABI is incompatible */ \
4867
0
    { \
4868
0
      guint i; \
4869
0
 \
4870
0
      _msg->msg_iov = g_newa (struct iovec, _message->num_vectors); \
4871
0
      for (i = 0; i < _message->num_vectors; i++) \
4872
0
        { \
4873
0
          _msg->msg_iov[i].iov_base = _message->vectors[i].buffer; \
4874
0
          _msg->msg_iov[i].iov_len = _message->vectors[i].size; \
4875
0
        } \
4876
0
      _msg->msg_iovlen = _message->num_vectors; \
4877
0
    } \
4878
0
 \
4879
0
  /* control */ \
4880
0
  if (_message->control_messages == NULL) \
4881
0
    { \
4882
0
    _msg->msg_controllen = 0; \
4883
0
    _msg->msg_control = NULL; \
4884
0
    } \
4885
0
  else \
4886
0
    { \
4887
0
      _msg->msg_controllen = G_SOCKET_CONTROL_BUFFER_SIZE_BYTES; \
4888
0
      _msg->msg_control = g_alloca (_msg->msg_controllen); \
4889
0
    } \
4890
0
 \
4891
0
  /* flags */ \
4892
0
  _msg->msg_flags = _message->flags; \
4893
0
} G_STMT_END
4894
4895
static void
4896
input_message_from_msghdr (const struct msghdr  *msg,
4897
                           GInputMessage        *message,
4898
                           GSocket              *socket)
4899
0
{
4900
  /* decode address */
4901
0
  if (message->address != NULL)
4902
0
    {
4903
0
      *message->address = cache_recv_address (socket, msg->msg_name,
4904
0
                                              msg->msg_namelen);
4905
0
    }
4906
4907
  /* decode control messages */
4908
0
  {
4909
0
    GPtrArray *my_messages = NULL;
4910
0
    struct cmsghdr *cmsg;
4911
4912
0
    if (msg->msg_controllen >= (socklen_t) sizeof (struct cmsghdr))
4913
0
      {
4914
0
        g_assert (message->control_messages != NULL);
4915
0
        for (cmsg = CMSG_FIRSTHDR (msg);
4916
0
             cmsg != NULL;
4917
0
             cmsg = CMSG_NXTHDR ((struct msghdr *) msg, cmsg))
4918
0
          {
4919
0
            GSocketControlMessage *control_message;
4920
4921
0
            control_message = g_socket_control_message_deserialize (cmsg->cmsg_level,
4922
0
                                                                    cmsg->cmsg_type,
4923
0
                                                                    cmsg->cmsg_len - ((char *)CMSG_DATA (cmsg) - (char *)cmsg),
4924
0
                                                                    CMSG_DATA (cmsg));
4925
0
            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
0
            if (my_messages == NULL)
4931
0
              my_messages = g_ptr_array_new ();
4932
0
            g_ptr_array_add (my_messages, control_message);
4933
0
           }
4934
0
      }
4935
4936
0
    if (message->num_control_messages)
4937
0
      *message->num_control_messages = my_messages != NULL ? my_messages->len : 0;
4938
4939
0
    if (message->control_messages)
4940
0
      {
4941
0
        if (my_messages == NULL)
4942
0
          {
4943
0
            *message->control_messages = NULL;
4944
0
          }
4945
0
        else
4946
0
          {
4947
0
            g_ptr_array_add (my_messages, NULL);
4948
0
            *message->control_messages = (GSocketControlMessage **) g_ptr_array_free (my_messages, FALSE);
4949
0
          }
4950
0
      }
4951
0
    else
4952
0
      {
4953
0
        g_assert (my_messages == NULL);
4954
0
      }
4955
0
  }
4956
4957
  /* capture the flags */
4958
0
  message->flags = msg->msg_flags;
4959
0
}
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
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
0
{
5035
0
  GPollableReturn res;
5036
0
  gsize bytes_written = 0;
5037
0
  gsize vectors_size = 0;
5038
5039
0
  if (num_vectors != -1)
5040
0
    {
5041
0
      for (gint i = 0; i < num_vectors; i++)
5042
0
        {
5043
          /* No wrap-around for vectors_size */
5044
0
          if (vectors_size > vectors_size + vectors[i].size)
5045
0
            {
5046
0
              g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
5047
0
                           _("Unable to send message: %s"),
5048
0
                           _("Message vectors too large"));
5049
0
              return -1;
5050
0
            }
5051
5052
0
          vectors_size += vectors[i].size;
5053
0
        }
5054
0
    }
5055
0
  else
5056
0
    {
5057
0
      for (gsize i = 0; vectors[i].buffer != NULL; i++)
5058
0
        {
5059
          /* No wrap-around for vectors_size */
5060
0
          if (vectors_size > vectors_size + vectors[i].size)
5061
0
            {
5062
0
              g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
5063
0
                           _("Unable to send message: %s"),
5064
0
                           _("Message vectors too large"));
5065
0
              return -1;
5066
0
            }
5067
5068
0
          vectors_size += vectors[i].size;
5069
0
        }
5070
0
    }
5071
5072
  /* Check if vector's buffers are too big for gssize */
5073
0
  if (vectors_size > G_MAXSSIZE)
5074
0
    {
5075
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
5076
0
                   _("Unable to send message: %s"),
5077
0
                   _("Message vectors too large"));
5078
0
      return -1;
5079
0
    }
5080
5081
0
  res = g_socket_send_message_with_timeout (socket, address,
5082
0
                                            vectors, num_vectors,
5083
0
                                            messages, num_messages, flags,
5084
0
                                            socket->priv->blocking ? -1 : 0,
5085
0
                                            &bytes_written,
5086
0
                                            cancellable, error);
5087
5088
0
  g_assert (res != G_POLLABLE_RETURN_OK || bytes_written <= G_MAXSSIZE);
5089
5090
0
  if (res == G_POLLABLE_RETURN_WOULD_BLOCK)
5091
0
    {
5092
0
#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
0
    }
5098
5099
0
  return res == G_POLLABLE_RETURN_OK ? (gssize) bytes_written : -1;
5100
0
}
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
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
0
{
5145
0
  GOutputVector one_vector;
5146
0
  char zero;
5147
0
  gint64 start_time;
5148
5149
0
  if (bytes_written)
5150
0
    *bytes_written = 0;
5151
5152
0
  g_return_val_if_fail (G_IS_SOCKET (socket), G_POLLABLE_RETURN_FAILED);
5153
0
  g_return_val_if_fail (address == NULL || G_IS_SOCKET_ADDRESS (address), G_POLLABLE_RETURN_FAILED);
5154
0
  g_return_val_if_fail (num_vectors == 0 || vectors != NULL, G_POLLABLE_RETURN_FAILED);
5155
0
  g_return_val_if_fail (num_messages == 0 || messages != NULL, G_POLLABLE_RETURN_FAILED);
5156
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_POLLABLE_RETURN_FAILED);
5157
0
  g_return_val_if_fail (error == NULL || *error == NULL, G_POLLABLE_RETURN_FAILED);
5158
5159
0
  start_time = g_get_monotonic_time ();
5160
5161
0
  if (!check_socket (socket, error))
5162
0
    return G_POLLABLE_RETURN_FAILED;
5163
5164
0
  if (!check_timeout (socket, error))
5165
0
    return G_POLLABLE_RETURN_FAILED;
5166
5167
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
5168
0
    return G_POLLABLE_RETURN_FAILED;
5169
5170
0
  if (num_vectors == -1)
5171
0
    {
5172
0
      for (num_vectors = 0;
5173
0
     vectors[num_vectors].buffer != NULL;
5174
0
     num_vectors++)
5175
0
  ;
5176
0
    }
5177
5178
0
  if (num_messages == -1)
5179
0
    {
5180
0
      for (num_messages = 0;
5181
0
     messages != NULL && messages[num_messages] != NULL;
5182
0
     num_messages++)
5183
0
  ;
5184
0
    }
5185
5186
0
  if (num_vectors == 0)
5187
0
    {
5188
0
      zero = '\0';
5189
5190
0
      one_vector.buffer = &zero;
5191
0
      one_vector.size = 1;
5192
0
      num_vectors = 1;
5193
0
      vectors = &one_vector;
5194
0
    }
5195
5196
0
#ifndef G_OS_WIN32
5197
0
  {
5198
0
    GOutputMessage output_message;
5199
0
    struct msghdr msg;
5200
0
    gssize result;
5201
0
    GError *child_error = NULL;
5202
5203
0
    output_message.address = address;
5204
0
    output_message.vectors = (GOutputVector *) vectors;
5205
0
    output_message.num_vectors = num_vectors;
5206
0
    output_message.bytes_sent = 0;
5207
0
    output_message.control_messages = messages;
5208
0
    output_message.num_control_messages = num_messages;
5209
5210
0
    output_message_to_msghdr (&output_message, NULL, &msg, NULL, &child_error);
5211
5212
0
    if (child_error != NULL)
5213
0
      {
5214
0
        g_propagate_error (error, child_error);
5215
0
        return G_POLLABLE_RETURN_FAILED;
5216
0
      }
5217
5218
0
    while (1)
5219
0
      {
5220
0
  result = sendmsg (socket->priv->fd, &msg, flags | G_SOCKET_DEFAULT_SEND_FLAGS);
5221
0
  if (result < 0)
5222
0
    {
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
0
              {
5230
0
                if (timeout_us != 0)
5231
0
                  {
5232
0
                    if (!block_on_timeout (socket, G_IO_OUT, timeout_us, start_time,
5233
0
                                           cancellable, error))
5234
0
                      return G_POLLABLE_RETURN_FAILED;
5235
5236
0
                    continue;
5237
0
                  }
5238
5239
0
                return G_POLLABLE_RETURN_WOULD_BLOCK;
5240
0
              }
5241
5242
0
            socket_set_error_lazy (error, errsv, _("Error sending message: %s"));
5243
0
            return G_POLLABLE_RETURN_FAILED;
5244
0
    }
5245
0
  break;
5246
0
      }
5247
5248
0
    if (bytes_written)
5249
0
      *bytes_written = result;
5250
5251
0
    return G_POLLABLE_RETURN_OK;
5252
0
  }
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
0
}
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
g_socket_send_messages (GSocket        *socket,
5394
            GOutputMessage *messages,
5395
            guint           num_messages,
5396
            gint            flags,
5397
            GCancellable   *cancellable,
5398
            GError        **error)
5399
0
{
5400
0
  return g_socket_send_messages_with_timeout (socket, messages, num_messages,
5401
0
                                              flags,
5402
0
                                              socket->priv->blocking ? -1 : 0,
5403
0
                                              cancellable, error);
5404
0
}
5405
5406
static gint
5407
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
0
{
5415
0
  gint64 start_time;
5416
5417
0
  g_return_val_if_fail (G_IS_SOCKET (socket), -1);
5418
0
  g_return_val_if_fail (num_messages == 0 || messages != NULL, -1);
5419
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), -1);
5420
0
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
5421
5422
0
  start_time = g_get_monotonic_time ();
5423
5424
0
  if (!check_socket (socket, error))
5425
0
    return -1;
5426
5427
0
  if (!check_timeout (socket, error))
5428
0
    return -1;
5429
5430
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
5431
0
    return -1;
5432
5433
0
  if (num_messages == 0)
5434
0
    return 0;
5435
5436
0
#if !defined (G_OS_WIN32) && defined (HAVE_SENDMMSG)
5437
0
  {
5438
0
    struct mmsghdr *msgvec;
5439
0
    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
0
    if (num_messages > G_IOV_MAX)
5445
0
      num_messages = G_IOV_MAX;
5446
5447
0
    msgvec = g_newa (struct mmsghdr, num_messages);
5448
5449
0
    for (i = 0; i < num_messages; ++i)
5450
0
      {
5451
0
        GOutputMessage *msg = &messages[i];
5452
0
        struct msghdr *msg_hdr = &msgvec[i].msg_hdr;
5453
0
        GError *child_error = NULL;
5454
5455
0
        msgvec[i].msg_len = 0;
5456
5457
0
        output_message_to_msghdr (msg, (i > 0) ? &messages[i - 1] : NULL,
5458
0
                                  msg_hdr, (i > 0) ? &msgvec[i - 1].msg_hdr : NULL,
5459
0
                                  &child_error);
5460
5461
0
        if (child_error != NULL)
5462
0
          {
5463
0
            g_propagate_error (error, child_error);
5464
0
            return -1;
5465
0
          }
5466
0
      }
5467
5468
0
    for (num_sent = 0; num_sent < num_messages;)
5469
0
      {
5470
0
        gint ret;
5471
5472
0
        ret = sendmmsg (socket->priv->fd, msgvec + num_sent, num_messages - num_sent,
5473
0
                        flags | G_SOCKET_DEFAULT_SEND_FLAGS);
5474
5475
0
        if (ret < 0)
5476
0
          {
5477
0
            int errsv = get_socket_errno ();
5478
5479
0
            if (errsv == EINTR)
5480
0
              continue;
5481
5482
0
            if (timeout_us != 0 &&
5483
0
                (errsv == EWOULDBLOCK ||
5484
0
                 errsv == EAGAIN))
5485
0
              {
5486
0
                if (!block_on_timeout (socket, G_IO_OUT, timeout_us, start_time,
5487
0
                                       cancellable, error))
5488
0
                  {
5489
0
                    if (num_sent > 0)
5490
0
                      {
5491
0
                        g_clear_error (error);
5492
0
                        break;
5493
0
                      }
5494
5495
0
                    return -1;
5496
0
                  }
5497
5498
0
                continue;
5499
0
              }
5500
5501
            /* If any messages were successfully sent, do not error. */
5502
0
            if (num_sent > 0)
5503
0
              break;
5504
5505
0
            socket_set_error_lazy (error, errsv, _("Error sending message: %s"));
5506
5507
0
            return -1;
5508
0
          }
5509
5510
0
        num_sent += ret;
5511
0
      }
5512
5513
0
    for (i = 0; i < num_sent; ++i)
5514
0
      messages[i].bytes_sent = msgvec[i].msg_len;
5515
5516
0
    return num_sent;
5517
0
  }
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
0
}
5587
5588
static GSocketAddress *
5589
cache_recv_address (GSocket *socket, struct sockaddr *native, size_t native_len)
5590
0
{
5591
0
  GSocketAddress *saddr;
5592
0
  gint i;
5593
0
  guint64 oldest_time = G_MAXUINT64;
5594
0
  gint oldest_index = 0;
5595
5596
0
  if (native_len == 0)
5597
0
    return NULL;
5598
5599
0
  saddr = NULL;
5600
0
  for (i = 0; i < RECV_ADDR_CACHE_SIZE; i++)
5601
0
    {
5602
0
      GSocketAddress *tmp = socket->priv->recv_addr_cache[i].addr;
5603
0
      gpointer tmp_native = socket->priv->recv_addr_cache[i].native;
5604
0
      gsize tmp_native_len = socket->priv->recv_addr_cache[i].native_len;
5605
5606
0
      if (!tmp)
5607
0
        continue;
5608
5609
0
      if (tmp_native_len != native_len)
5610
0
        continue;
5611
5612
0
      if (memcmp (tmp_native, native, native_len) == 0)
5613
0
        {
5614
0
          saddr = g_object_ref (tmp);
5615
0
          socket->priv->recv_addr_cache[i].last_used = g_get_monotonic_time ();
5616
0
          return saddr;
5617
0
        }
5618
5619
0
      if (socket->priv->recv_addr_cache[i].last_used < oldest_time)
5620
0
        {
5621
0
          oldest_time = socket->priv->recv_addr_cache[i].last_used;
5622
0
          oldest_index = i;
5623
0
        }
5624
0
    }
5625
5626
0
  saddr = g_socket_address_new_from_native (native, native_len);
5627
5628
0
  if (socket->priv->recv_addr_cache[oldest_index].addr)
5629
0
    {
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
0
    }
5633
5634
0
  socket->priv->recv_addr_cache[oldest_index].native = g_memdup2 (native, native_len);
5635
0
  socket->priv->recv_addr_cache[oldest_index].native_len = native_len;
5636
0
  socket->priv->recv_addr_cache[oldest_index].addr = g_object_ref (saddr);
5637
0
  socket->priv->recv_addr_cache[oldest_index].last_used = g_get_monotonic_time ();
5638
5639
0
  return saddr;
5640
0
}
5641
5642
static gssize
5643
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
0
{
5654
0
  GInputVector one_vector;
5655
0
  char one_byte;
5656
0
  gint64 start_time;
5657
5658
0
  g_return_val_if_fail (G_IS_SOCKET (socket), -1);
5659
5660
0
  start_time = g_get_monotonic_time ();
5661
5662
0
  if (!check_socket (socket, error))
5663
0
    return -1;
5664
5665
0
  if (!check_timeout (socket, error))
5666
0
    return -1;
5667
5668
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
5669
0
    return -1;
5670
5671
0
  if (num_vectors == -1)
5672
0
    {
5673
0
      for (num_vectors = 0;
5674
0
     vectors[num_vectors].buffer != NULL;
5675
0
     num_vectors++)
5676
0
  ;
5677
0
    }
5678
5679
0
  if (num_vectors == 0)
5680
0
    {
5681
0
      one_vector.buffer = &one_byte;
5682
0
      one_vector.size = 1;
5683
0
      num_vectors = 1;
5684
0
      vectors = &one_vector;
5685
0
    }
5686
5687
0
#ifndef G_OS_WIN32
5688
0
  {
5689
0
    GInputMessage input_message;
5690
0
    struct msghdr msg;
5691
0
    gssize result;
5692
5693
0
    input_message.address = address;
5694
0
    input_message.vectors = vectors;
5695
0
    input_message.num_vectors = num_vectors;
5696
0
    input_message.bytes_received = 0;
5697
0
    input_message.flags = (flags != NULL) ? *flags : 0;
5698
0
    input_message.control_messages = messages;
5699
0
    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
0
#ifdef MSG_CMSG_CLOEXEC
5706
0
    input_message.flags |= MSG_CMSG_CLOEXEC;
5707
0
#endif
5708
5709
0
    input_message_to_msghdr (&input_message, &msg);
5710
5711
    /* do it */
5712
0
    while (1)
5713
0
      {
5714
0
  result = recvmsg (socket->priv->fd, &msg, msg.msg_flags);
5715
0
#ifdef MSG_CMSG_CLOEXEC 
5716
0
  if (result < 0 && get_socket_errno () == EINVAL)
5717
0
    {
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
0
    }
5722
0
#endif
5723
5724
0
  if (result < 0)
5725
0
    {
5726
0
      int errsv = get_socket_errno ();
5727
5728
0
      if (errsv == EINTR)
5729
0
        continue;
5730
5731
0
      if (timeout_us != 0 &&
5732
0
    (errsv == EWOULDBLOCK ||
5733
0
     errsv == EAGAIN))
5734
0
        {
5735
0
                if (!block_on_timeout (socket, G_IO_IN, timeout_us, start_time,
5736
0
                                       cancellable, error))
5737
0
                  return -1;
5738
5739
0
                continue;
5740
0
        }
5741
5742
0
      socket_set_error_lazy (error, errsv, _("Error receiving message: %s"));
5743
0
      return -1;
5744
0
    }
5745
0
  break;
5746
0
      }
5747
5748
0
    input_message_from_msghdr (&msg, &input_message, socket);
5749
5750
0
    if (flags != NULL)
5751
0
      *flags = input_message.flags;
5752
5753
0
    return result;
5754
0
  }
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
0
}
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
g_socket_receive_messages (GSocket        *socket,
5915
                           GInputMessage  *messages,
5916
                           guint           num_messages,
5917
                           gint            flags,
5918
                           GCancellable   *cancellable,
5919
                           GError        **error)
5920
0
{
5921
0
  if (!check_socket (socket, error) ||
5922
0
      !check_timeout (socket, error))
5923
0
    return -1;
5924
5925
0
  return g_socket_receive_messages_with_timeout (socket, messages, num_messages,
5926
0
                                                 flags,
5927
0
                                                 socket->priv->blocking ? -1 : 0,
5928
0
                                                 cancellable, error);
5929
0
}
5930
5931
static gint
5932
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
0
{
5940
0
  gint64 start_time;
5941
5942
0
  g_return_val_if_fail (G_IS_SOCKET (socket), -1);
5943
0
  g_return_val_if_fail (num_messages == 0 || messages != NULL, -1);
5944
0
  g_return_val_if_fail (cancellable == NULL ||
5945
0
                        G_IS_CANCELLABLE (cancellable), -1);
5946
0
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
5947
5948
0
  start_time = g_get_monotonic_time ();
5949
5950
0
  if (!check_socket (socket, error))
5951
0
    return -1;
5952
5953
0
  if (!check_timeout (socket, error))
5954
0
    return -1;
5955
5956
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
5957
0
    return -1;
5958
5959
0
  if (num_messages == 0)
5960
0
    return 0;
5961
5962
0
#if !defined (G_OS_WIN32) && defined (HAVE_RECVMMSG)
5963
0
  {
5964
0
    struct mmsghdr *msgvec;
5965
0
    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
0
    if (num_messages > G_IOV_MAX)
5971
0
      num_messages = G_IOV_MAX;
5972
5973
0
    msgvec = g_newa (struct mmsghdr, num_messages);
5974
5975
0
    for (i = 0; i < num_messages; ++i)
5976
0
      {
5977
0
        GInputMessage *msg = &messages[i];
5978
0
        struct msghdr *msg_hdr = &msgvec[i].msg_hdr;
5979
5980
0
        input_message_to_msghdr (msg, msg_hdr);
5981
0
        msgvec[i].msg_len = 0;
5982
0
      }
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
0
#ifdef MSG_CMSG_CLOEXEC
5989
0
    flags |= MSG_CMSG_CLOEXEC;
5990
0
#endif
5991
5992
0
    for (num_received = 0; num_received < num_messages;)
5993
0
      {
5994
0
        gint ret;
5995
5996
        /* We operate in non-blocking mode and handle the timeout ourselves. */
5997
0
        ret = recvmmsg (socket->priv->fd,
5998
0
                        msgvec + num_received,
5999
0
                        num_messages - num_received,
6000
0
                        flags | G_SOCKET_DEFAULT_SEND_FLAGS, NULL);
6001
0
#ifdef MSG_CMSG_CLOEXEC
6002
0
        if (ret < 0 && get_socket_errno () == EINVAL)
6003
0
          {
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
0
                            num_messages - num_received,
6009
0
                            flags | G_SOCKET_DEFAULT_SEND_FLAGS, NULL);
6010
0
          }
6011
0
#endif
6012
6013
0
        if (ret < 0)
6014
0
          {
6015
0
            int errsv = get_socket_errno ();
6016
6017
0
            if (errsv == EINTR)
6018
0
              continue;
6019
6020
0
            if (timeout_us != 0 &&
6021
0
                (errsv == EWOULDBLOCK ||
6022
0
                 errsv == EAGAIN))
6023
0
              {
6024
0
                if (!block_on_timeout (socket, G_IO_IN, timeout_us, start_time,
6025
0
                                       cancellable, error))
6026
0
                  {
6027
0
                    if (num_received > 0)
6028
0
                      {
6029
0
                        g_clear_error (error);
6030
0
                        break;
6031
0
                      }
6032
6033
0
                    return -1;
6034
0
                  }
6035
6036
0
                continue;
6037
0
              }
6038
6039
            /* If any messages were successfully received, do not error. */
6040
0
            if (num_received > 0)
6041
0
              break;
6042
6043
0
            socket_set_error_lazy (error, errsv,
6044
0
                                   _("Error receiving message: %s"));
6045
6046
0
            return -1;
6047
0
          }
6048
0
        else if (ret == 0)
6049
0
          {
6050
            /* EOS. */
6051
0
            break;
6052
0
          }
6053
6054
0
        num_received += ret;
6055
0
      }
6056
6057
0
    for (i = 0; i < num_received; ++i)
6058
0
      {
6059
0
        input_message_from_msghdr (&msgvec[i].msg_hdr, &messages[i], socket);
6060
0
        messages[i].bytes_received = msgvec[i].msg_len;
6061
0
      }
6062
6063
0
    return num_received;
6064
0
  }
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
0
}
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
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
0
{
6217
0
  return g_socket_receive_message_with_timeout (socket, address, vectors,
6218
0
                                                 num_vectors, messages,
6219
0
                                                 num_messages, flags,
6220
0
                                                 socket->priv->blocking ? -1 : 0,
6221
0
                                                 cancellable, error);
6222
0
}
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
g_socket_get_credentials (GSocket   *socket,
6257
                          GError   **error)
6258
0
{
6259
0
  GCredentials *ret;
6260
6261
0
  g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
6262
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
6263
6264
0
  if (!check_socket (socket, error))
6265
0
    return NULL;
6266
6267
0
  ret = NULL;
6268
6269
0
#if G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED
6270
6271
0
#ifdef SO_PEERCRED
6272
0
  {
6273
0
    guint8 native_creds_buf[G_CREDENTIALS_NATIVE_SIZE];
6274
0
    socklen_t optlen = sizeof (native_creds_buf);
6275
6276
0
    if (getsockopt (socket->priv->fd,
6277
0
                    SOL_SOCKET,
6278
0
                    SO_PEERCRED,
6279
0
                    native_creds_buf,
6280
0
                    &optlen) == 0)
6281
0
      {
6282
0
        ret = g_credentials_new ();
6283
0
        g_credentials_set_native (ret,
6284
0
                                  G_CREDENTIALS_NATIVE_TYPE,
6285
0
                                  native_creds_buf);
6286
0
      }
6287
0
  }
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
0
  if (!ret)
6397
0
    {
6398
0
      int errsv = get_socket_errno ();
6399
6400
0
      g_set_error (error,
6401
0
                   G_IO_ERROR,
6402
0
                   socket_io_error_from_errno (errsv),
6403
0
                   _("Unable to read socket credentials: %s"),
6404
0
                   socket_strerror (errsv));
6405
0
    }
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
0
  return ret;
6416
0
}
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
g_socket_get_option (GSocket  *socket,
6448
         gint      level,
6449
         gint      optname,
6450
         gint     *value,
6451
         GError  **error)
6452
0
{
6453
0
  socklen_t size;
6454
6455
0
  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
0
  if (socket->priv->inited && !check_socket (socket, error))
6460
0
    return FALSE;
6461
6462
0
  *value = 0;
6463
0
  size = sizeof (gint);
6464
0
  if (getsockopt (socket->priv->fd, level, optname, value, &size) != 0)
6465
0
    {
6466
0
      int errsv = get_socket_errno ();
6467
6468
0
      g_set_error_literal (error,
6469
0
         G_IO_ERROR,
6470
0
         socket_io_error_from_errno (errsv),
6471
0
         socket_strerror (errsv));
6472
0
#ifndef G_OS_WIN32
6473
      /* Reset errno in case the caller wants to look at it */
6474
0
      errno = errsv;
6475
0
#endif
6476
0
      return FALSE;
6477
0
    }
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
0
  return TRUE;
6488
0
}
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
g_socket_set_option (GSocket  *socket,
6516
         gint      level,
6517
         gint      optname,
6518
         gint      value,
6519
         GError  **error)
6520
0
{
6521
0
  gint errsv;
6522
6523
0
  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
0
  if (socket->priv->inited && !check_socket (socket, error))
6528
0
    return FALSE;
6529
6530
0
  if (setsockopt (socket->priv->fd, level, optname, &value, sizeof (gint)) == 0)
6531
0
    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
0
  errsv = get_socket_errno ();
6548
6549
0
  g_set_error_literal (error,
6550
0
                       G_IO_ERROR,
6551
0
                       socket_io_error_from_errno (errsv),
6552
0
                       socket_strerror (errsv));
6553
0
#ifndef G_OS_WIN32
6554
0
  errno = errsv;
6555
0
#endif
6556
0
  return FALSE;
6557
0
}