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